LLVM  3.7.0
LoopUnswitch.cpp
Go to the documentation of this file.
1 //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass transforms loops that contain branches on loop-invariant conditions
11 // to have multiple loops. For example, it turns the left into the right code:
12 //
13 // for (...) if (lic)
14 // A for (...)
15 // if (lic) A; B; C
16 // B else
17 // C for (...)
18 // A; C
19 //
20 // This can increase the size of the code exponentially (doubling it every time
21 // a loop is unswitched) so we only unswitch if the resultant code will be
22 // smaller than a threshold.
23 //
24 // This pass expects LICM to be run before it to hoist invariant conditions out
25 // of the loop, to make the unswitching opportunity obvious.
26 //
27 //===----------------------------------------------------------------------===//
28 
29 #include "llvm/Transforms/Scalar.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Analysis/LoopInfo.h"
37 #include "llvm/Analysis/LoopPass.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/Dominators.h"
43 #include "llvm/IR/Function.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/Module.h"
46 #include "llvm/IR/MDBuilder.h"
48 #include "llvm/Support/Debug.h"
53 #include <algorithm>
54 #include <map>
55 #include <set>
56 using namespace llvm;
57 
58 #define DEBUG_TYPE "loop-unswitch"
59 
60 STATISTIC(NumBranches, "Number of branches unswitched");
61 STATISTIC(NumSwitches, "Number of switches unswitched");
62 STATISTIC(NumSelects , "Number of selects unswitched");
63 STATISTIC(NumTrivial , "Number of unswitches that are trivial");
64 STATISTIC(NumSimplify, "Number of simplifications of unswitched code");
65 STATISTIC(TotalInsts, "Total number of instructions analyzed");
66 
67 // The specific value of 100 here was chosen based only on intuition and a
68 // few specific examples.
69 static cl::opt<unsigned>
70 Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"),
71  cl::init(100), cl::Hidden);
72 
73 namespace {
74 
75  class LUAnalysisCache {
76 
78  UnswitchedValsMap;
79 
80  typedef UnswitchedValsMap::iterator UnswitchedValsIt;
81 
82  struct LoopProperties {
83  unsigned CanBeUnswitchedCount;
84  unsigned WasUnswitchedCount;
85  unsigned SizeEstimation;
86  UnswitchedValsMap UnswitchedVals;
87  };
88 
89  // Here we use std::map instead of DenseMap, since we need to keep valid
90  // LoopProperties pointer for current loop for better performance.
91  typedef std::map<const Loop*, LoopProperties> LoopPropsMap;
92  typedef LoopPropsMap::iterator LoopPropsMapIt;
93 
94  LoopPropsMap LoopsProperties;
95  UnswitchedValsMap *CurLoopInstructions;
96  LoopProperties *CurrentLoopProperties;
97 
98  // A loop unswitching with an estimated cost above this threshold
99  // is not performed. MaxSize is turned into unswitching quota for
100  // the current loop, and reduced correspondingly, though note that
101  // the quota is returned by releaseMemory() when the loop has been
102  // processed, so that MaxSize will return to its previous
103  // value. So in most cases MaxSize will equal the Threshold flag
104  // when a new loop is processed. An exception to that is that
105  // MaxSize will have a smaller value while processing nested loops
106  // that were introduced due to loop unswitching of an outer loop.
107  //
108  // FIXME: The way that MaxSize works is subtle and depends on the
109  // pass manager processing loops and calling releaseMemory() in a
110  // specific order. It would be good to find a more straightforward
111  // way of doing what MaxSize does.
112  unsigned MaxSize;
113 
114  public:
115  LUAnalysisCache()
116  : CurLoopInstructions(nullptr), CurrentLoopProperties(nullptr),
117  MaxSize(Threshold) {}
118 
119  // Analyze loop. Check its size, calculate is it possible to unswitch
120  // it. Returns true if we can unswitch this loop.
121  bool countLoop(const Loop *L, const TargetTransformInfo &TTI,
122  AssumptionCache *AC);
123 
124  // Clean all data related to given loop.
125  void forgetLoop(const Loop *L);
126 
127  // Mark case value as unswitched.
128  // Since SI instruction can be partly unswitched, in order to avoid
129  // extra unswitching in cloned loops keep track all unswitched values.
130  void setUnswitched(const SwitchInst *SI, const Value *V);
131 
132  // Check was this case value unswitched before or not.
133  bool isUnswitched(const SwitchInst *SI, const Value *V);
134 
135  // Returns true if another unswitching could be done within the cost
136  // threshold.
137  bool CostAllowsUnswitching();
138 
139  // Clone all loop-unswitch related loop properties.
140  // Redistribute unswitching quotas.
141  // Note, that new loop data is stored inside the VMap.
142  void cloneData(const Loop *NewLoop, const Loop *OldLoop,
143  const ValueToValueMapTy &VMap);
144  };
145 
146  class LoopUnswitch : public LoopPass {
147  LoopInfo *LI; // Loop information
148  LPPassManager *LPM;
149  AssumptionCache *AC;
150 
151  // LoopProcessWorklist - Used to check if second loop needs processing
152  // after RewriteLoopBodyWithConditionConstant rewrites first loop.
153  std::vector<Loop*> LoopProcessWorklist;
154 
155  LUAnalysisCache BranchesInfo;
156 
157  bool OptimizeForSize;
158  bool redoLoop;
159 
160  Loop *currentLoop;
161  DominatorTree *DT;
162  BasicBlock *loopHeader;
163  BasicBlock *loopPreheader;
164 
165  // LoopBlocks contains all of the basic blocks of the loop, including the
166  // preheader of the loop, the body of the loop, and the exit blocks of the
167  // loop, in that order.
168  std::vector<BasicBlock*> LoopBlocks;
169  // NewBlocks contained cloned copy of basic blocks from LoopBlocks.
170  std::vector<BasicBlock*> NewBlocks;
171 
172  public:
173  static char ID; // Pass ID, replacement for typeid
174  explicit LoopUnswitch(bool Os = false) :
175  LoopPass(ID), OptimizeForSize(Os), redoLoop(false),
176  currentLoop(nullptr), DT(nullptr), loopHeader(nullptr),
177  loopPreheader(nullptr) {
179  }
180 
181  bool runOnLoop(Loop *L, LPPassManager &LPM) override;
182  bool processCurrentLoop();
183 
184  /// This transformation requires natural loop information & requires that
185  /// loop preheaders be inserted into the CFG.
186  ///
187  void getAnalysisUsage(AnalysisUsage &AU) const override {
198  }
199 
200  private:
201 
202  void releaseMemory() override {
203  BranchesInfo.forgetLoop(currentLoop);
204  }
205 
206  void initLoopData() {
207  loopHeader = currentLoop->getHeader();
208  loopPreheader = currentLoop->getLoopPreheader();
209  }
210 
211  /// Split all of the edges from inside the loop to their exit blocks.
212  /// Update the appropriate Phi nodes as we do so.
213  void SplitExitEdges(Loop *L, const SmallVectorImpl<BasicBlock *> &ExitBlocks);
214 
215  bool UnswitchIfProfitable(Value *LoopCond, Constant *Val,
216  TerminatorInst *TI = nullptr);
217  void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
218  BasicBlock *ExitBlock, TerminatorInst *TI);
219  void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L,
220  TerminatorInst *TI);
221 
222  void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
223  Constant *Val, bool isEqual);
224 
225  void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
226  BasicBlock *TrueDest,
227  BasicBlock *FalseDest,
228  Instruction *InsertPt,
229  TerminatorInst *TI);
230 
231  void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L);
232  bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = nullptr,
233  BasicBlock **LoopExit = nullptr);
234 
235  };
236 }
237 
238 // Analyze loop. Check its size, calculate is it possible to unswitch
239 // it. Returns true if we can unswitch this loop.
240 bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI,
241  AssumptionCache *AC) {
242 
243  LoopPropsMapIt PropsIt;
244  bool Inserted;
245  std::tie(PropsIt, Inserted) =
246  LoopsProperties.insert(std::make_pair(L, LoopProperties()));
247 
248  LoopProperties &Props = PropsIt->second;
249 
250  if (Inserted) {
251  // New loop.
252 
253  // Limit the number of instructions to avoid causing significant code
254  // expansion, and the number of basic blocks, to avoid loops with
255  // large numbers of branches which cause loop unswitching to go crazy.
256  // This is a very ad-hoc heuristic.
257 
259  CodeMetrics::collectEphemeralValues(L, AC, EphValues);
260 
261  // FIXME: This is overly conservative because it does not take into
262  // consideration code simplification opportunities and code that can
263  // be shared by the resultant unswitched loops.
265  for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); I != E;
266  ++I)
267  Metrics.analyzeBasicBlock(*I, TTI, EphValues);
268 
269  Props.SizeEstimation = Metrics.NumInsts;
270  Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation);
271  Props.WasUnswitchedCount = 0;
272  MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount;
273 
274  if (Metrics.notDuplicatable) {
275  DEBUG(dbgs() << "NOT unswitching loop %"
276  << L->getHeader()->getName() << ", contents cannot be "
277  << "duplicated!\n");
278  return false;
279  }
280  }
281 
282  // Be careful. This links are good only before new loop addition.
283  CurrentLoopProperties = &Props;
284  CurLoopInstructions = &Props.UnswitchedVals;
285 
286  return true;
287 }
288 
289 // Clean all data related to given loop.
290 void LUAnalysisCache::forgetLoop(const Loop *L) {
291 
292  LoopPropsMapIt LIt = LoopsProperties.find(L);
293 
294  if (LIt != LoopsProperties.end()) {
295  LoopProperties &Props = LIt->second;
296  MaxSize += (Props.CanBeUnswitchedCount + Props.WasUnswitchedCount) *
297  Props.SizeEstimation;
298  LoopsProperties.erase(LIt);
299  }
300 
301  CurrentLoopProperties = nullptr;
302  CurLoopInstructions = nullptr;
303 }
304 
305 // Mark case value as unswitched.
306 // Since SI instruction can be partly unswitched, in order to avoid
307 // extra unswitching in cloned loops keep track all unswitched values.
308 void LUAnalysisCache::setUnswitched(const SwitchInst *SI, const Value *V) {
309  (*CurLoopInstructions)[SI].insert(V);
310 }
311 
312 // Check was this case value unswitched before or not.
313 bool LUAnalysisCache::isUnswitched(const SwitchInst *SI, const Value *V) {
314  return (*CurLoopInstructions)[SI].count(V);
315 }
316 
317 bool LUAnalysisCache::CostAllowsUnswitching() {
318  return CurrentLoopProperties->CanBeUnswitchedCount > 0;
319 }
320 
321 // Clone all loop-unswitch related loop properties.
322 // Redistribute unswitching quotas.
323 // Note, that new loop data is stored inside the VMap.
324 void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop,
325  const ValueToValueMapTy &VMap) {
326 
327  LoopProperties &NewLoopProps = LoopsProperties[NewLoop];
328  LoopProperties &OldLoopProps = *CurrentLoopProperties;
329  UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals;
330 
331  // Reallocate "can-be-unswitched quota"
332 
333  --OldLoopProps.CanBeUnswitchedCount;
334  ++OldLoopProps.WasUnswitchedCount;
335  NewLoopProps.WasUnswitchedCount = 0;
336  unsigned Quota = OldLoopProps.CanBeUnswitchedCount;
337  NewLoopProps.CanBeUnswitchedCount = Quota / 2;
338  OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2;
339 
340  NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation;
341 
342  // Clone unswitched values info:
343  // for new loop switches we clone info about values that was
344  // already unswitched and has redundant successors.
345  for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) {
346  const SwitchInst *OldInst = I->first;
347  Value *NewI = VMap.lookup(OldInst);
348  const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI);
349  assert(NewInst && "All instructions that are in SrcBB must be in VMap.");
350 
351  NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst];
352  }
353 }
354 
355 char LoopUnswitch::ID = 0;
356 INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops",
357  false, false)
360 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
363 INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops",
364  false, false)
365 
366 Pass *llvm::createLoopUnswitchPass(bool Os) {
367  return new LoopUnswitch(Os);
368 }
369 
370 /// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is
371 /// invariant in the loop, or has an invariant piece, return the invariant.
372 /// Otherwise, return null.
373 static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) {
374 
375  // We started analyze new instruction, increment scanned instructions counter.
376  ++TotalInsts;
377 
378  // We can never unswitch on vector conditions.
379  if (Cond->getType()->isVectorTy())
380  return nullptr;
381 
382  // Constants should be folded, not unswitched on!
383  if (isa<Constant>(Cond)) return nullptr;
384 
385  // TODO: Handle: br (VARIANT|INVARIANT).
386 
387  // Hoist simple values out.
388  if (L->makeLoopInvariant(Cond, Changed))
389  return Cond;
390 
391  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond))
392  if (BO->getOpcode() == Instruction::And ||
393  BO->getOpcode() == Instruction::Or) {
394  // If either the left or right side is invariant, we can unswitch on this,
395  // which will cause the branch to go away in one loop and the condition to
396  // simplify in the other one.
397  if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed))
398  return LHS;
399  if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed))
400  return RHS;
401  }
402 
403  return nullptr;
404 }
405 
406 bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
407  if (skipOptnoneFunction(L))
408  return false;
409 
410  AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
411  *L->getHeader()->getParent());
412  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
413  LPM = &LPM_Ref;
415  getAnalysisIfAvailable<DominatorTreeWrapperPass>();
416  DT = DTWP ? &DTWP->getDomTree() : nullptr;
417  currentLoop = L;
418  Function *F = currentLoop->getHeader()->getParent();
419  bool Changed = false;
420  do {
421  assert(currentLoop->isLCSSAForm(*DT));
422  redoLoop = false;
423  Changed |= processCurrentLoop();
424  } while(redoLoop);
425 
426  if (Changed) {
427  // FIXME: Reconstruct dom info, because it is not preserved properly.
428  if (DT)
429  DT->recalculate(*F);
430  }
431  return Changed;
432 }
433 
434 /// processCurrentLoop - Do actual work and unswitch loop if possible
435 /// and profitable.
436 bool LoopUnswitch::processCurrentLoop() {
437  bool Changed = false;
438 
439  initLoopData();
440 
441  // If LoopSimplify was unable to form a preheader, don't do any unswitching.
442  if (!loopPreheader)
443  return false;
444 
445  // Loops with indirectbr cannot be cloned.
446  if (!currentLoop->isSafeToClone())
447  return false;
448 
449  // Without dedicated exits, splitting the exit edge may fail.
450  if (!currentLoop->hasDedicatedExits())
451  return false;
452 
453  LLVMContext &Context = loopHeader->getContext();
454 
455  // Probably we reach the quota of branches for this loop. If so
456  // stop unswitching.
457  if (!BranchesInfo.countLoop(
458  currentLoop, getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
459  *currentLoop->getHeader()->getParent()),
460  AC))
461  return false;
462 
463  // Loop over all of the basic blocks in the loop. If we find an interior
464  // block that is branching on a loop-invariant condition, we can unswitch this
465  // loop.
466  for (Loop::block_iterator I = currentLoop->block_begin(),
467  E = currentLoop->block_end(); I != E; ++I) {
468  TerminatorInst *TI = (*I)->getTerminator();
469  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
470  // If this isn't branching on an invariant condition, we can't unswitch
471  // it.
472  if (BI->isConditional()) {
473  // See if this, or some part of it, is loop invariant. If so, we can
474  // unswitch on it if we desire.
475  Value *LoopCond = FindLIVLoopCondition(BI->getCondition(),
476  currentLoop, Changed);
477  if (LoopCond &&
478  UnswitchIfProfitable(LoopCond, ConstantInt::getTrue(Context), TI)) {
479  ++NumBranches;
480  return true;
481  }
482  }
483  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
484  Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
485  currentLoop, Changed);
486  unsigned NumCases = SI->getNumCases();
487  if (LoopCond && NumCases) {
488  // Find a value to unswitch on:
489  // FIXME: this should chose the most expensive case!
490  // FIXME: scan for a case with a non-critical edge?
491  Constant *UnswitchVal = nullptr;
492 
493  // Do not process same value again and again.
494  // At this point we have some cases already unswitched and
495  // some not yet unswitched. Let's find the first not yet unswitched one.
496  for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
497  i != e; ++i) {
498  Constant *UnswitchValCandidate = i.getCaseValue();
499  if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
500  UnswitchVal = UnswitchValCandidate;
501  break;
502  }
503  }
504 
505  if (!UnswitchVal)
506  continue;
507 
508  if (UnswitchIfProfitable(LoopCond, UnswitchVal)) {
509  ++NumSwitches;
510  return true;
511  }
512  }
513  }
514 
515  // Scan the instructions to check for unswitchable values.
516  for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end();
517  BBI != E; ++BBI)
518  if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
519  Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
520  currentLoop, Changed);
521  if (LoopCond && UnswitchIfProfitable(LoopCond,
522  ConstantInt::getTrue(Context))) {
523  ++NumSelects;
524  return true;
525  }
526  }
527  }
528  return Changed;
529 }
530 
531 /// isTrivialLoopExitBlock - Check to see if all paths from BB exit the
532 /// loop with no side effects (including infinite loops).
533 ///
534 /// If true, we return true and set ExitBB to the block we
535 /// exit through.
536 ///
538  BasicBlock *&ExitBB,
539  std::set<BasicBlock*> &Visited) {
540  if (!Visited.insert(BB).second) {
541  // Already visited. Without more analysis, this could indicate an infinite
542  // loop.
543  return false;
544  }
545  if (!L->contains(BB)) {
546  // Otherwise, this is a loop exit, this is fine so long as this is the
547  // first exit.
548  if (ExitBB) return false;
549  ExitBB = BB;
550  return true;
551  }
552 
553  // Otherwise, this is an unvisited intra-loop node. Check all successors.
554  for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
555  // Check to see if the successor is a trivial loop exit.
556  if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
557  return false;
558  }
559 
560  // Okay, everything after this looks good, check to make sure that this block
561  // doesn't include any side effects.
562  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
563  if (I->mayHaveSideEffects())
564  return false;
565 
566  return true;
567 }
568 
569 /// isTrivialLoopExitBlock - Return true if the specified block unconditionally
570 /// leads to an exit from the specified loop, and has no side-effects in the
571 /// process. If so, return the block that is exited to, otherwise return null.
573  std::set<BasicBlock*> Visited;
574  Visited.insert(L->getHeader()); // Branches to header make infinite loops.
575  BasicBlock *ExitBB = nullptr;
576  if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited))
577  return ExitBB;
578  return nullptr;
579 }
580 
581 /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is
582 /// trivial: that is, that the condition controls whether or not the loop does
583 /// anything at all. If this is a trivial condition, unswitching produces no
584 /// code duplications (equivalently, it produces a simpler loop and a new empty
585 /// loop, which gets deleted).
586 ///
587 /// If this is a trivial condition, return true, otherwise return false. When
588 /// returning true, this sets Cond and Val to the condition that controls the
589 /// trivial condition: when Cond dynamically equals Val, the loop is known to
590 /// exit. Finally, this sets LoopExit to the BB that the loop exits to when
591 /// Cond == Val.
592 ///
593 bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val,
594  BasicBlock **LoopExit) {
595  BasicBlock *Header = currentLoop->getHeader();
596  TerminatorInst *HeaderTerm = Header->getTerminator();
597  LLVMContext &Context = Header->getContext();
598 
599  BasicBlock *LoopExitBB = nullptr;
600  if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) {
601  // If the header block doesn't end with a conditional branch on Cond, we
602  // can't handle it.
603  if (!BI->isConditional() || BI->getCondition() != Cond)
604  return false;
605 
606  // Check to see if a successor of the branch is guaranteed to
607  // exit through a unique exit block without having any
608  // side-effects. If so, determine the value of Cond that causes it to do
609  // this.
610  if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
611  BI->getSuccessor(0)))) {
612  if (Val) *Val = ConstantInt::getTrue(Context);
613  } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
614  BI->getSuccessor(1)))) {
615  if (Val) *Val = ConstantInt::getFalse(Context);
616  }
617  } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
618  // If this isn't a switch on Cond, we can't handle it.
619  if (SI->getCondition() != Cond) return false;
620 
621  // Check to see if a successor of the switch is guaranteed to go to the
622  // latch block or exit through a one exit block without having any
623  // side-effects. If so, determine the value of Cond that causes it to do
624  // this.
625  // Note that we can't trivially unswitch on the default case or
626  // on already unswitched cases.
627  for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
628  i != e; ++i) {
629  BasicBlock *LoopExitCandidate;
630  if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
631  i.getCaseSuccessor()))) {
632  // Okay, we found a trivial case, remember the value that is trivial.
633  ConstantInt *CaseVal = i.getCaseValue();
634 
635  // Check that it was not unswitched before, since already unswitched
636  // trivial vals are looks trivial too.
637  if (BranchesInfo.isUnswitched(SI, CaseVal))
638  continue;
639  LoopExitBB = LoopExitCandidate;
640  if (Val) *Val = CaseVal;
641  break;
642  }
643  }
644  }
645 
646  // If we didn't find a single unique LoopExit block, or if the loop exit block
647  // contains phi nodes, this isn't trivial.
648  if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin()))
649  return false; // Can't handle this.
650 
651  if (LoopExit) *LoopExit = LoopExitBB;
652 
653  // We already know that nothing uses any scalar values defined inside of this
654  // loop. As such, we just have to check to see if this loop will execute any
655  // side-effecting instructions (e.g. stores, calls, volatile loads) in the
656  // part of the loop that the code *would* execute. We already checked the
657  // tail, check the header now.
658  for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
659  if (I->mayHaveSideEffects())
660  return false;
661  return true;
662 }
663 
664 /// UnswitchIfProfitable - We have found that we can unswitch currentLoop when
665 /// LoopCond == Val to simplify the loop. If we decide that this is profitable,
666 /// unswitch the loop, reprocess the pieces, then return true.
667 bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,
668  TerminatorInst *TI) {
669  Function *F = loopHeader->getParent();
670  Constant *CondVal = nullptr;
671  BasicBlock *ExitBlock = nullptr;
672 
673  if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) {
674  // If the condition is trivial, always unswitch. There is no code growth
675  // for this case.
676  UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock, TI);
677  return true;
678  }
679 
680  // Check to see if it would be profitable to unswitch current loop.
681  if (!BranchesInfo.CostAllowsUnswitching()) {
682  DEBUG(dbgs() << "NOT unswitching loop %"
683  << currentLoop->getHeader()->getName()
684  << " at non-trivial condition '" << *Val
685  << "' == " << *LoopCond << "\n"
686  << ". Cost too high.\n");
687  return false;
688  }
689 
690  // Do not do non-trivial unswitch while optimizing for size.
691  if (OptimizeForSize || F->hasFnAttribute(Attribute::OptimizeForSize))
692  return false;
693 
694  UnswitchNontrivialCondition(LoopCond, Val, currentLoop, TI);
695  return true;
696 }
697 
698 /// CloneLoop - Recursively clone the specified loop and all of its children,
699 /// mapping the blocks with the specified map.
701  LoopInfo *LI, LPPassManager *LPM) {
702  Loop *New = new Loop();
703  LPM->insertLoop(New, PL);
704 
705  // Add all of the blocks in L to the new loop.
706  for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
707  I != E; ++I)
708  if (LI->getLoopFor(*I) == L)
709  New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI);
710 
711  // Add all of the subloops to the new loop.
712  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
713  CloneLoop(*I, New, VM, LI, LPM);
714 
715  return New;
716 }
717 
718 static void copyMetadata(Instruction *DstInst, const Instruction *SrcInst,
719  bool Swapped) {
720  if (!SrcInst || !SrcInst->hasMetadata())
721  return;
722 
724  SrcInst->getAllMetadata(MDs);
725  for (auto &MD : MDs) {
726  switch (MD.first) {
727  default:
728  break;
730  if (Swapped && MD.second->getNumOperands() == 3 &&
731  isa<MDString>(MD.second->getOperand(0))) {
732  MDString *MDName = cast<MDString>(MD.second->getOperand(0));
733  if (MDName->getString() == "branch_weights") {
734  auto *ValT = cast_or_null<ConstantAsMetadata>(
735  MD.second->getOperand(1))->getValue();
736  auto *ValF = cast_or_null<ConstantAsMetadata>(
737  MD.second->getOperand(2))->getValue();
738  assert(ValT && ValF && "Invalid Operands of branch_weights");
739  auto NewMD =
740  MDBuilder(DstInst->getParent()->getContext())
741  .createBranchWeights(cast<ConstantInt>(ValF)->getZExtValue(),
742  cast<ConstantInt>(ValT)->getZExtValue());
743  MD.second = NewMD;
744  }
745  }
746  // fallthrough.
747  case LLVMContext::MD_dbg:
748  DstInst->setMetadata(MD.first, MD.second);
749  }
750  }
751 }
752 
753 /// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values
754 /// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the
755 /// code immediately before InsertPt.
756 void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
757  BasicBlock *TrueDest,
758  BasicBlock *FalseDest,
759  Instruction *InsertPt,
760  TerminatorInst *TI) {
761  // Insert a conditional branch on LIC to the two preheaders. The original
762  // code is the true version and the new code is the false version.
763  Value *BranchVal = LIC;
764  bool Swapped = false;
765  if (!isa<ConstantInt>(Val) ||
766  Val->getType() != Type::getInt1Ty(LIC->getContext()))
767  BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val);
768  else if (Val != ConstantInt::getTrue(Val->getContext())) {
769  // We want to enter the new loop when the condition is true.
770  std::swap(TrueDest, FalseDest);
771  Swapped = true;
772  }
773 
774  // Insert the new branch.
775  BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
776  copyMetadata(BI, TI, Swapped);
777 
778  // If either edge is critical, split it. This helps preserve LoopSimplify
779  // form for enclosing loops.
780  auto Options = CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA();
781  SplitCriticalEdge(BI, 0, Options);
782  SplitCriticalEdge(BI, 1, Options);
783 }
784 
785 /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
786 /// condition in it (a cond branch from its header block to its latch block,
787 /// where the path through the loop that doesn't execute its body has no
788 /// side-effects), unswitch it. This doesn't involve any code duplication, just
789 /// moving the conditional branch outside of the loop and updating loop info.
790 void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
791  BasicBlock *ExitBlock,
792  TerminatorInst *TI) {
793  DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %"
794  << loopHeader->getName() << " [" << L->getBlocks().size()
795  << " blocks] in Function "
796  << L->getHeader()->getParent()->getName() << " on cond: " << *Val
797  << " == " << *Cond << "\n");
798 
799  // First step, split the preheader, so that we know that there is a safe place
800  // to insert the conditional branch. We will change loopPreheader to have a
801  // conditional branch on Cond.
802  BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, DT, LI);
803 
804  // Now that we have a place to insert the conditional branch, create a place
805  // to branch to: this is the exit block out of the loop that we should
806  // short-circuit to.
807 
808  // Split this block now, so that the loop maintains its exit block, and so
809  // that the jump from the preheader can execute the contents of the exit block
810  // without actually branching to it (the exit block should be dominated by the
811  // loop header, not the preheader).
812  assert(!L->contains(ExitBlock) && "Exit block is in the loop?");
813  BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin(), DT, LI);
814 
815  // Okay, now we have a position to branch from and a position to branch to,
816  // insert the new conditional branch.
817  EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH,
818  loopPreheader->getTerminator(), TI);
819  LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L);
820  loopPreheader->getTerminator()->eraseFromParent();
821 
822  // We need to reprocess this loop, it could be unswitched again.
823  redoLoop = true;
824 
825  // Now that we know that the loop is never entered when this condition is a
826  // particular value, rewrite the loop with this info. We know that this will
827  // at least eliminate the old branch.
828  RewriteLoopBodyWithConditionConstant(L, Cond, Val, false);
829  ++NumTrivial;
830 }
831 
832 /// SplitExitEdges - Split all of the edges from inside the loop to their exit
833 /// blocks. Update the appropriate Phi nodes as we do so.
834 void LoopUnswitch::SplitExitEdges(Loop *L,
835  const SmallVectorImpl<BasicBlock *> &ExitBlocks){
836 
837  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
838  BasicBlock *ExitBlock = ExitBlocks[i];
839  SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock),
840  pred_end(ExitBlock));
841 
842  // Although SplitBlockPredecessors doesn't preserve loop-simplify in
843  // general, if we call it on all predecessors of all exits then it does.
844  SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa",
845  /*AliasAnalysis*/ nullptr, DT, LI,
846  /*PreserveLCSSA*/ true);
847  }
848 }
849 
850 /// UnswitchNontrivialCondition - We determined that the loop is profitable
851 /// to unswitch when LIC equal Val. Split it into loop versions and test the
852 /// condition outside of either loop. Return the loops created as Out1/Out2.
853 void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
854  Loop *L, TerminatorInst *TI) {
855  Function *F = loopHeader->getParent();
856  DEBUG(dbgs() << "loop-unswitch: Unswitching loop %"
857  << loopHeader->getName() << " [" << L->getBlocks().size()
858  << " blocks] in Function " << F->getName()
859  << " when '" << *Val << "' == " << *LIC << "\n");
860 
861  if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
862  SE->forgetLoop(L);
863 
864  LoopBlocks.clear();
865  NewBlocks.clear();
866 
867  // First step, split the preheader and exit blocks, and add these blocks to
868  // the LoopBlocks list.
869  BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, DT, LI);
870  LoopBlocks.push_back(NewPreheader);
871 
872  // We want the loop to come after the preheader, but before the exit blocks.
873  LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
874 
875  SmallVector<BasicBlock*, 8> ExitBlocks;
876  L->getUniqueExitBlocks(ExitBlocks);
877 
878  // Split all of the edges from inside the loop to their exit blocks. Update
879  // the appropriate Phi nodes as we do so.
880  SplitExitEdges(L, ExitBlocks);
881 
882  // The exit blocks may have been changed due to edge splitting, recompute.
883  ExitBlocks.clear();
884  L->getUniqueExitBlocks(ExitBlocks);
885 
886  // Add exit blocks to the loop blocks.
887  LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
888 
889  // Next step, clone all of the basic blocks that make up the loop (including
890  // the loop preheader and exit blocks), keeping track of the mapping between
891  // the instructions and blocks.
892  NewBlocks.reserve(LoopBlocks.size());
893  ValueToValueMapTy VMap;
894  for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
895  BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F);
896 
897  NewBlocks.push_back(NewBB);
898  VMap[LoopBlocks[i]] = NewBB; // Keep the BB mapping.
899  LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L);
900  }
901 
902  // Splice the newly inserted blocks into the function right before the
903  // original preheader.
904  F->getBasicBlockList().splice(NewPreheader, F->getBasicBlockList(),
905  NewBlocks[0], F->end());
906 
907  // FIXME: We could register any cloned assumptions instead of clearing the
908  // whole function's cache.
909  AC->clear();
910 
911  // Now we create the new Loop object for the versioned loop.
912  Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM);
913 
914  // Recalculate unswitching quota, inherit simplified switches info for NewBB,
915  // Probably clone more loop-unswitch related loop properties.
916  BranchesInfo.cloneData(NewLoop, L, VMap);
917 
918  Loop *ParentLoop = L->getParentLoop();
919  if (ParentLoop) {
920  // Make sure to add the cloned preheader and exit blocks to the parent loop
921  // as well.
922  ParentLoop->addBasicBlockToLoop(NewBlocks[0], *LI);
923  }
924 
925  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
926  BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]);
927  // The new exit block should be in the same loop as the old one.
928  if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i]))
929  ExitBBLoop->addBasicBlockToLoop(NewExit, *LI);
930 
931  assert(NewExit->getTerminator()->getNumSuccessors() == 1 &&
932  "Exit block should have been split to have one successor!");
933  BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0);
934 
935  // If the successor of the exit block had PHI nodes, add an entry for
936  // NewExit.
937  for (BasicBlock::iterator I = ExitSucc->begin();
938  PHINode *PN = dyn_cast<PHINode>(I); ++I) {
939  Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]);
940  ValueToValueMapTy::iterator It = VMap.find(V);
941  if (It != VMap.end()) V = It->second;
942  PN->addIncoming(V, NewExit);
943  }
944 
945  if (LandingPadInst *LPad = NewExit->getLandingPadInst()) {
946  PHINode *PN = PHINode::Create(LPad->getType(), 0, "",
947  ExitSucc->getFirstInsertionPt());
948 
949  for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc);
950  I != E; ++I) {
951  BasicBlock *BB = *I;
952  LandingPadInst *LPI = BB->getLandingPadInst();
953  LPI->replaceAllUsesWith(PN);
954  PN->addIncoming(LPI, BB);
955  }
956  }
957  }
958 
959  // Rewrite the code to refer to itself.
960  for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
961  for (BasicBlock::iterator I = NewBlocks[i]->begin(),
962  E = NewBlocks[i]->end(); I != E; ++I)
964 
965  // Rewrite the original preheader to select between versions of the loop.
966  BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator());
967  assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
968  "Preheader splitting did not work correctly!");
969 
970  // Emit the new branch that selects between the two versions of this loop.
971  EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR,
972  TI);
973  LPM->deleteSimpleAnalysisValue(OldBR, L);
974  OldBR->eraseFromParent();
975 
976  LoopProcessWorklist.push_back(NewLoop);
977  redoLoop = true;
978 
979  // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody
980  // deletes the instruction (for example by simplifying a PHI that feeds into
981  // the condition that we're unswitching on), we don't rewrite the second
982  // iteration.
983  WeakVH LICHandle(LIC);
984 
985  // Now we rewrite the original code to know that the condition is true and the
986  // new code to know that the condition is false.
987  RewriteLoopBodyWithConditionConstant(L, LIC, Val, false);
988 
989  // It's possible that simplifying one loop could cause the other to be
990  // changed to another value or a constant. If its a constant, don't simplify
991  // it.
992  if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop &&
993  LICHandle && !isa<Constant>(LICHandle))
994  RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true);
995 }
996 
997 /// RemoveFromWorklist - Remove all instances of I from the worklist vector
998 /// specified.
1000  std::vector<Instruction*> &Worklist) {
1001 
1002  Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I),
1003  Worklist.end());
1004 }
1005 
1006 /// ReplaceUsesOfWith - When we find that I really equals V, remove I from the
1007 /// program, replacing all uses with V and update the worklist.
1009  std::vector<Instruction*> &Worklist,
1010  Loop *L, LPPassManager *LPM) {
1011  DEBUG(dbgs() << "Replace with '" << *V << "': " << *I);
1012 
1013  // Add uses to the worklist, which may be dead now.
1014  for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1015  if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
1016  Worklist.push_back(Use);
1017 
1018  // Add users to the worklist which may be simplified now.
1019  for (User *U : I->users())
1020  Worklist.push_back(cast<Instruction>(U));
1021  LPM->deleteSimpleAnalysisValue(I, L);
1022  RemoveFromWorklist(I, Worklist);
1023  I->replaceAllUsesWith(V);
1024  I->eraseFromParent();
1025  ++NumSimplify;
1026 }
1027 
1028 // RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
1029 // the value specified by Val in the specified loop, or we know it does NOT have
1030 // that value. Rewrite any uses of LIC or of properties correlated to it.
1031 void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
1032  Constant *Val,
1033  bool IsEqual) {
1034  assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
1035 
1036  // FIXME: Support correlated properties, like:
1037  // for (...)
1038  // if (li1 < li2)
1039  // ...
1040  // if (li1 > li2)
1041  // ...
1042 
1043  // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches,
1044  // selects, switches.
1045  std::vector<Instruction*> Worklist;
1046  LLVMContext &Context = Val->getContext();
1047 
1048  // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC
1049  // in the loop with the appropriate one directly.
1050  if (IsEqual || (isa<ConstantInt>(Val) &&
1051  Val->getType()->isIntegerTy(1))) {
1052  Value *Replacement;
1053  if (IsEqual)
1054  Replacement = Val;
1055  else
1056  Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()),
1057  !cast<ConstantInt>(Val)->getZExtValue());
1058 
1059  for (User *U : LIC->users()) {
1060  Instruction *UI = dyn_cast<Instruction>(U);
1061  if (!UI || !L->contains(UI))
1062  continue;
1063  Worklist.push_back(UI);
1064  }
1065 
1066  for (std::vector<Instruction*>::iterator UI = Worklist.begin(),
1067  UE = Worklist.end(); UI != UE; ++UI)
1068  (*UI)->replaceUsesOfWith(LIC, Replacement);
1069 
1070  SimplifyCode(Worklist, L);
1071  return;
1072  }
1073 
1074  // Otherwise, we don't know the precise value of LIC, but we do know that it
1075  // is certainly NOT "Val". As such, simplify any uses in the loop that we
1076  // can. This case occurs when we unswitch switch statements.
1077  for (User *U : LIC->users()) {
1078  Instruction *UI = dyn_cast<Instruction>(U);
1079  if (!UI || !L->contains(UI))
1080  continue;
1081 
1082  Worklist.push_back(UI);
1083 
1084  // TODO: We could do other simplifications, for example, turning
1085  // 'icmp eq LIC, Val' -> false.
1086 
1087  // If we know that LIC is not Val, use this info to simplify code.
1088  SwitchInst *SI = dyn_cast<SwitchInst>(UI);
1089  if (!SI || !isa<ConstantInt>(Val)) continue;
1090 
1091  SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val));
1092  // Default case is live for multiple values.
1093  if (DeadCase == SI->case_default()) continue;
1094 
1095  // Found a dead case value. Don't remove PHI nodes in the
1096  // successor if they become single-entry, those PHI nodes may
1097  // be in the Users list.
1098 
1099  BasicBlock *Switch = SI->getParent();
1100  BasicBlock *SISucc = DeadCase.getCaseSuccessor();
1101  BasicBlock *Latch = L->getLoopLatch();
1102 
1103  BranchesInfo.setUnswitched(SI, Val);
1104 
1105  if (!SI->findCaseDest(SISucc)) continue; // Edge is critical.
1106  // If the DeadCase successor dominates the loop latch, then the
1107  // transformation isn't safe since it will delete the sole predecessor edge
1108  // to the latch.
1109  if (Latch && DT->dominates(SISucc, Latch))
1110  continue;
1111 
1112  // FIXME: This is a hack. We need to keep the successor around
1113  // and hooked up so as to preserve the loop structure, because
1114  // trying to update it is complicated. So instead we preserve the
1115  // loop structure and put the block on a dead code path.
1116  SplitEdge(Switch, SISucc, DT, LI);
1117  // Compute the successors instead of relying on the return value
1118  // of SplitEdge, since it may have split the switch successor
1119  // after PHI nodes.
1120  BasicBlock *NewSISucc = DeadCase.getCaseSuccessor();
1121  BasicBlock *OldSISucc = *succ_begin(NewSISucc);
1122  // Create an "unreachable" destination.
1123  BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
1124  Switch->getParent(),
1125  OldSISucc);
1126  new UnreachableInst(Context, Abort);
1127  // Force the new case destination to branch to the "unreachable"
1128  // block while maintaining a (dead) CFG edge to the old block.
1129  NewSISucc->getTerminator()->eraseFromParent();
1130  BranchInst::Create(Abort, OldSISucc,
1131  ConstantInt::getTrue(Context), NewSISucc);
1132  // Release the PHI operands for this edge.
1133  for (BasicBlock::iterator II = NewSISucc->begin();
1134  PHINode *PN = dyn_cast<PHINode>(II); ++II)
1135  PN->setIncomingValue(PN->getBasicBlockIndex(Switch),
1136  UndefValue::get(PN->getType()));
1137  // Tell the domtree about the new block. We don't fully update the
1138  // domtree here -- instead we force it to do a full recomputation
1139  // after the pass is complete -- but we do need to inform it of
1140  // new blocks.
1141  if (DT)
1142  DT->addNewBlock(Abort, NewSISucc);
1143  }
1144 
1145  SimplifyCode(Worklist, L);
1146 }
1147 
1148 /// SimplifyCode - Okay, now that we have simplified some instructions in the
1149 /// loop, walk over it and constant prop, dce, and fold control flow where
1150 /// possible. Note that this is effectively a very simple loop-structure-aware
1151 /// optimizer. During processing of this loop, L could very well be deleted, so
1152 /// it must not be used.
1153 ///
1154 /// FIXME: When the loop optimizer is more mature, separate this out to a new
1155 /// pass.
1156 ///
1157 void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
1158  const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
1159  while (!Worklist.empty()) {
1160  Instruction *I = Worklist.back();
1161  Worklist.pop_back();
1162 
1163  // Simple DCE.
1164  if (isInstructionTriviallyDead(I)) {
1165  DEBUG(dbgs() << "Remove dead instruction '" << *I);
1166 
1167  // Add uses to the worklist, which may be dead now.
1168  for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1169  if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
1170  Worklist.push_back(Use);
1171  LPM->deleteSimpleAnalysisValue(I, L);
1172  RemoveFromWorklist(I, Worklist);
1173  I->eraseFromParent();
1174  ++NumSimplify;
1175  continue;
1176  }
1177 
1178  // See if instruction simplification can hack this up. This is common for
1179  // things like "select false, X, Y" after unswitching made the condition be
1180  // 'false'. TODO: update the domtree properly so we can pass it here.
1181  if (Value *V = SimplifyInstruction(I, DL))
1182  if (LI->replacementPreservesLCSSAForm(I, V)) {
1183  ReplaceUsesOfWith(I, V, Worklist, L, LPM);
1184  continue;
1185  }
1186 
1187  // Special case hacks that appear commonly in unswitched code.
1188  if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
1189  if (BI->isUnconditional()) {
1190  // If BI's parent is the only pred of the successor, fold the two blocks
1191  // together.
1192  BasicBlock *Pred = BI->getParent();
1193  BasicBlock *Succ = BI->getSuccessor(0);
1194  BasicBlock *SinglePred = Succ->getSinglePredecessor();
1195  if (!SinglePred) continue; // Nothing to do.
1196  assert(SinglePred == Pred && "CFG broken");
1197 
1198  DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- "
1199  << Succ->getName() << "\n");
1200 
1201  // Resolve any single entry PHI nodes in Succ.
1202  while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
1203  ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
1204 
1205  // If Succ has any successors with PHI nodes, update them to have
1206  // entries coming from Pred instead of Succ.
1207  Succ->replaceAllUsesWith(Pred);
1208 
1209  // Move all of the successor contents from Succ to Pred.
1210  Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
1211  Succ->end());
1212  LPM->deleteSimpleAnalysisValue(BI, L);
1213  BI->eraseFromParent();
1214  RemoveFromWorklist(BI, Worklist);
1215 
1216  // Remove Succ from the loop tree.
1217  LI->removeBlock(Succ);
1218  LPM->deleteSimpleAnalysisValue(Succ, L);
1219  Succ->eraseFromParent();
1220  ++NumSimplify;
1221  continue;
1222  }
1223 
1224  continue;
1225  }
1226  }
1227 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
void initializeLoopUnswitchPass(PassRegistry &)
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:537
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static IntegerType * getInt1Ty(LLVMContext &C)
Definition: Type.cpp:236
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitBlock - Split the specified block at the specified instruction - every thing before SplitPt stay...
INITIALIZE_PASS_BEGIN(LoopUnswitch,"loop-unswitch","Unswitch loops", false, false) INITIALIZE_PASS_END(LoopUnswitch
iterator end()
Definition: Function.h:459
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: ValueMap.h:141
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
unsigned getNumOperands() const
Definition: User.h:138
ScalarEvolution - This class is the main scalar evolution driver.
An immutable pass that tracks lazily created AssumptionCache objects.
loop Unswitch loops
CaseIt case_begin()
Returns a read/write iterator that points to the first case in SwitchInst.
A cache of .assume calls within a function.
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:232
LoopT * getParentLoop() const
Definition: LoopInfo.h:97
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
F(f)
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
getAllMetadata - Get all metadata attached to this Instruction.
Definition: Instruction.h:183
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
Definition: LoopInfo.h:540
bool notDuplicatable
True if this function cannot be duplicated.
Definition: CodeMetrics.h:54
const std::vector< BlockT * > & getBlocks() const
getBlocks - Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:139
BlockT * getHeader() const
Definition: LoopInfo.h:96
ConstantInt * findCaseDest(BasicBlock *BB)
findCaseDest - Finds the unique case value for a given successor.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
BlockT * getLoopLatch() const
getLoopLatch - If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:156
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
static void copyMetadata(Instruction *DstInst, const Instruction *SrcInst, bool Swapped)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
bool isUnconditional() const
void push_back(NodeTy *val)
Definition: ilist.h:554
static Loop * CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM, LoopInfo *LI, LPPassManager *LPM)
CloneLoop - Recursively clone the specified loop and all of its children, mapping the blocks with the...
SelectInst - This class represents the LLVM 'select' instruction.
Option class for critical edge splitting.
loop Unswitch false
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
DominatorTree & getDomTree()
Definition: Dominators.h:213
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:104
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
addBasicBlockToLoop - This method is used by other analyses to update loop information.
Definition: LoopInfoImpl.h:187
static Value * FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed)
FindLIVLoopCondition - Cond is a condition that occurs in L.
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:141
Pass * createLoopUnswitchPass(bool OptimizeForSize=false)
BasicBlock * getSuccessor(unsigned i) const
iterator find(const KeyT &Val)
Definition: ValueMap.h:132
AnalysisUsage & addPreservedID(const void *ID)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
RF_NoModuleLevelChanges - If this flag is set, the remapper knows that only local values within a fun...
Definition: ValueMapper.h:57
RF_IgnoreMissingEntries - If this flag is set, the remapper ignores entries that are not in the value...
Definition: ValueMapper.h:62
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:107
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
void analyzeBasicBlock(const BasicBlock *BB, const TargetTransformInfo &TTI, SmallPtrSetImpl< const Value * > &EphValues)
Add information about a block to the current state.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Definition: InstrTypes.h:57
static void RemoveFromWorklist(Instruction *I, std::vector< Instruction * > &Worklist)
RemoveFromWorklist - Remove all instances of I from the worklist vector specified.
void clear()
Clear the cache of .assume intrinsics for a function.
BasicBlock * SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
SplitCriticalEdge - If this edge is a critical edge, insert a new node to split the critical edge...
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
friend const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
iterator begin() const
Definition: LoopInfo.h:131
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:35
Wrapper pass for TargetTransformInfo.
void insertLoop(Loop *L, Loop *ParentLoop)
Definition: LoopPass.cpp:104
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
Definition: InstrTypes.h:62
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
BranchInst - Conditional or Unconditional Branch instruction.
bool isVectorTy() const
isVectorTy - True if this is an instance of VectorType.
Definition: Type.h:226
UnreachableInst - This function has undefined behavior.
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
Definition: APInt.h:1895
LandingPadInst * getLandingPadInst()
Return the landingpad instruction associated with the landing pad.
Definition: BasicBlock.cpp:418
char & LCSSAID
Definition: LCSSA.cpp:312
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:114
bool hasMetadata() const
hasMetadata() - Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:157
iterator end() const
Definition: LoopInfo.h:132
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:105
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:252
This instruction compares its operands according to the predicate given to the constructor.
Value * getOperand(unsigned i) const
Definition: User.h:118
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:117
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:103
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
void getUniqueExitBlocks(SmallVectorImpl< BasicBlock * > &ExitBlocks) const
getUniqueExitBlocks - Return all unique successor blocks of this loop.
Definition: LoopInfo.cpp:347
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:519
loop unswitch
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1083
char & LoopSimplifyID
iterator end()
Definition: ValueMap.h:112
StringRef getString() const
Definition: Metadata.cpp:375
void deleteSimpleAnalysisValue(Value *V, Loop *L)
deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes that implement simple anal...
Definition: LoopPass.cpp:157
machine trace Machine Trace Metrics
const BasicBlockListType & getBasicBlockList() const
Definition: Function.h:436
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
iterator end()
Definition: BasicBlock.h:233
bool makeLoopInvariant(Value *V, bool &Changed, Instruction *InsertPt=nullptr) const
makeLoopInvariant - If the given value is an instruction inside of the loop and it can be hoisted...
Definition: LoopInfo.cpp:80
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:276
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Utility to calculate the size and a few similar metrics for a set of basic blocks.
Definition: CodeMetrics.h:42
CriticalEdgeSplittingOptions & setPreserveLCSSA()
BasicBlockTy * getCaseSuccessor()
Resolves successor for current case.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
CaseIt findCaseValue(const ConstantInt *C)
findCaseValue - Search all of the case values for the specified constant.
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:530
void splice(iterator where, iplist &L2)
Definition: ilist.h:570
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:576
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
isIntegerTy - True if this is an instance of IntegerType.
Definition: Type.h:193
iterator_range< user_iterator > users()
Definition: Value.h:300
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:211
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
RemapInstruction - Convert the instruction operands from referencing the current values into those sp...
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
std::vector< BlockT * >::const_iterator block_iterator
Definition: LoopInfo.h:140
Value * getCondition() const
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Definition: APInt.h:1890
static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB, BasicBlock *&ExitBB, std::set< BasicBlock * > &Visited)
isTrivialLoopExitBlock - Check to see if all paths from BB exit the loop with no side effects (includ...
block_iterator block_end() const
Definition: LoopInfo.h:142
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:217
iplist< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition: BasicBlock.cpp:97
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
CaseIt case_default()
Returns an iterator that points to the default case.
unsigned getNumCases() const
getNumCases - return the number of 'cases' in this switch instruction, except the default case ...
SwitchInst - Multiway switch.
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:32
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
isInstructionTriviallyDead - Return true if the result produced by the instruction is not used...
Definition: Local.cpp:282
LLVM Value Representation.
Definition: Value.h:69
static void ReplaceUsesOfWith(Instruction *I, Value *V, std::vector< Instruction * > &Worklist, Loop *L, LPPassManager *LPM)
ReplaceUsesOfWith - When we find that I really equals V, remove I from the program, replacing all uses with V and update the worklist.
#define DEBUG(X)
Definition: Debug.h:92
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitEdge - Split the edge connecting specified block.
block_iterator block_begin() const
Definition: LoopInfo.h:141
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:737
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, AliasAnalysis *AA=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
SplitBlockPredecessors - This method introduces at least one new basic block into the function and mo...
A single uniqued string.
Definition: Metadata.h:508
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
This pass exposes codegen information to IR-level passes.
std::vector< LoopT * >::const_iterator iterator
Definition: LoopInfo.h:128
static void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value * > &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop)...
Definition: CodeMetrics.cpp:70
void setIncomingValue(unsigned i, Value *V)
unsigned NumInsts
Number of instructions in the analyzed blocks.
Definition: CodeMetrics.h:60
static BasicBlock * isTrivialLoopExitBlock(Loop *L, BasicBlock *BB)
isTrivialLoopExitBlock - Return true if the specified block unconditionally leads to an exit from the...
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr)
CloneBasicBlock - Return a copy of the specified basic block, but without embedding the block into a ...
int getBasicBlockIndex(const BasicBlock *BB) const
getBasicBlockIndex - Return the first index of the specified basic block in the value list for this P...
const BasicBlock * getParent() const
Definition: Instruction.h:72