LLVM  8.0.1
LoopSimplifyCFG.cpp
Go to the documentation of this file.
1 //===--------- LoopSimplifyCFG.cpp - Loop CFG Simplification Pass ---------===//
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 file implements the Loop SimplifyCFG Pass. This pass is responsible for
11 // basic loop CFG cleanup, primarily to assist other loop passes. If you
12 // encounter a noncanonical CFG construct that causes another loop pass to
13 // perform suboptimally, this is the place to fix it up.
14 //
15 //===----------------------------------------------------------------------===//
16 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Analysis/LoopPass.h"
32 #include "llvm/IR/DomTreeUpdater.h"
33 #include "llvm/IR/Dominators.h"
34 #include "llvm/Transforms/Scalar.h"
36 #include "llvm/Transforms/Utils.h"
40 using namespace llvm;
41 
42 #define DEBUG_TYPE "loop-simplifycfg"
43 
44 static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding",
45  cl::init(false));
46 
47 STATISTIC(NumTerminatorsFolded,
48  "Number of terminators folded to unconditional branches");
49 STATISTIC(NumLoopBlocksDeleted,
50  "Number of loop blocks deleted");
51 STATISTIC(NumLoopExitsDeleted,
52  "Number of loop exiting edges deleted");
53 
54 /// If \p BB is a switch or a conditional branch, but only one of its successors
55 /// can be reached from this block in runtime, return this successor. Otherwise,
56 /// return nullptr.
58  Instruction *TI = BB->getTerminator();
59  if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
60  if (BI->isUnconditional())
61  return nullptr;
62  if (BI->getSuccessor(0) == BI->getSuccessor(1))
63  return BI->getSuccessor(0);
64  ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
65  if (!Cond)
66  return nullptr;
67  return Cond->isZero() ? BI->getSuccessor(1) : BI->getSuccessor(0);
68  }
69 
70  if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
71  auto *CI = dyn_cast<ConstantInt>(SI->getCondition());
72  if (!CI)
73  return nullptr;
74  for (auto Case : SI->cases())
75  if (Case.getCaseValue() == CI)
76  return Case.getCaseSuccessor();
77  return SI->getDefaultDest();
78  }
79 
80  return nullptr;
81 }
82 
83 namespace {
84 /// Helper class that can turn branches and switches with constant conditions
85 /// into unconditional branches.
86 class ConstantTerminatorFoldingImpl {
87 private:
88  Loop &L;
89  LoopInfo &LI;
90  DominatorTree &DT;
91  ScalarEvolution &SE;
92  MemorySSAUpdater *MSSAU;
93 
94  // Whether or not the current loop has irreducible CFG.
95  bool HasIrreducibleCFG = false;
96  // Whether or not the current loop will still exist after terminator constant
97  // folding will be done. In theory, there are two ways how it can happen:
98  // 1. Loop's latch(es) become unreachable from loop header;
99  // 2. Loop's header becomes unreachable from method entry.
100  // In practice, the second situation is impossible because we only modify the
101  // current loop and its preheader and do not affect preheader's reachibility
102  // from any other block. So this variable set to true means that loop's latch
103  // has become unreachable from loop header.
104  bool DeleteCurrentLoop = false;
105 
106  // The blocks of the original loop that will still be reachable from entry
107  // after the constant folding.
108  SmallPtrSet<BasicBlock *, 8> LiveLoopBlocks;
109  // The blocks of the original loop that will become unreachable from entry
110  // after the constant folding.
111  SmallVector<BasicBlock *, 8> DeadLoopBlocks;
112  // The exits of the original loop that will still be reachable from entry
113  // after the constant folding.
114  SmallPtrSet<BasicBlock *, 8> LiveExitBlocks;
115  // The exits of the original loop that will become unreachable from entry
116  // after the constant folding.
117  SmallVector<BasicBlock *, 8> DeadExitBlocks;
118  // The blocks that will still be a part of the current loop after folding.
119  SmallPtrSet<BasicBlock *, 8> BlocksInLoopAfterFolding;
120  // The blocks that have terminators with constant condition that can be
121  // folded. Note: fold candidates should be in L but not in any of its
122  // subloops to avoid complex LI updates.
123  SmallVector<BasicBlock *, 8> FoldCandidates;
124 
125  void dump() const {
126  dbgs() << "Constant terminator folding for loop " << L << "\n";
127  dbgs() << "After terminator constant-folding, the loop will";
128  if (!DeleteCurrentLoop)
129  dbgs() << " not";
130  dbgs() << " be destroyed\n";
131  auto PrintOutVector = [&](const char *Message,
133  dbgs() << Message << "\n";
134  for (const BasicBlock *BB : S)
135  dbgs() << "\t" << BB->getName() << "\n";
136  };
137  auto PrintOutSet = [&](const char *Message,
139  dbgs() << Message << "\n";
140  for (const BasicBlock *BB : S)
141  dbgs() << "\t" << BB->getName() << "\n";
142  };
143  PrintOutVector("Blocks in which we can constant-fold terminator:",
144  FoldCandidates);
145  PrintOutSet("Live blocks from the original loop:", LiveLoopBlocks);
146  PrintOutVector("Dead blocks from the original loop:", DeadLoopBlocks);
147  PrintOutSet("Live exit blocks:", LiveExitBlocks);
148  PrintOutVector("Dead exit blocks:", DeadExitBlocks);
149  if (!DeleteCurrentLoop)
150  PrintOutSet("The following blocks will still be part of the loop:",
151  BlocksInLoopAfterFolding);
152  }
153 
154  /// Whether or not the current loop has irreducible CFG.
155  bool hasIrreducibleCFG(LoopBlocksDFS &DFS) {
156  assert(DFS.isComplete() && "DFS is expected to be finished");
157  // Index of a basic block in RPO traversal.
159  unsigned Current = 0;
160  for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I)
161  RPO[*I] = Current++;
162 
163  for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
164  BasicBlock *BB = *I;
165  for (auto *Succ : successors(BB))
166  if (L.contains(Succ) && !LI.isLoopHeader(Succ) && RPO[BB] > RPO[Succ])
167  // If an edge goes from a block with greater order number into a block
168  // with lesses number, and it is not a loop backedge, then it can only
169  // be a part of irreducible non-loop cycle.
170  return true;
171  }
172  return false;
173  }
174 
175  /// Fill all information about status of blocks and exits of the current loop
176  /// if constant folding of all branches will be done.
177  void analyze() {
178  LoopBlocksDFS DFS(&L);
179  DFS.perform(&LI);
180  assert(DFS.isComplete() && "DFS is expected to be finished");
181 
182  // TODO: The algorithm below relies on both RPO and Postorder traversals.
183  // When the loop has only reducible CFG inside, then the invariant "all
184  // predecessors of X are processed before X in RPO" is preserved. However
185  // an irreducible loop can break this invariant (e.g. latch does not have to
186  // be the last block in the traversal in this case, and the algorithm relies
187  // on this). We can later decide to support such cases by altering the
188  // algorithms, but so far we just give up analyzing them.
189  if (hasIrreducibleCFG(DFS)) {
190  HasIrreducibleCFG = true;
191  return;
192  }
193 
194  // Collect live and dead loop blocks and exits.
195  LiveLoopBlocks.insert(L.getHeader());
196  for (auto I = DFS.beginRPO(), E = DFS.endRPO(); I != E; ++I) {
197  BasicBlock *BB = *I;
198 
199  // If a loop block wasn't marked as live so far, then it's dead.
200  if (!LiveLoopBlocks.count(BB)) {
201  DeadLoopBlocks.push_back(BB);
202  continue;
203  }
204 
205  BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
206 
207  // If a block has only one live successor, it's a candidate on constant
208  // folding. Only handle blocks from current loop: branches in child loops
209  // are skipped because if they can be folded, they should be folded during
210  // the processing of child loops.
211  if (TheOnlySucc && LI.getLoopFor(BB) == &L)
212  FoldCandidates.push_back(BB);
213 
214  // Handle successors.
215  for (BasicBlock *Succ : successors(BB))
216  if (!TheOnlySucc || TheOnlySucc == Succ) {
217  if (L.contains(Succ))
218  LiveLoopBlocks.insert(Succ);
219  else
220  LiveExitBlocks.insert(Succ);
221  }
222  }
223 
224  // Sanity check: amount of dead and live loop blocks should match the total
225  // number of blocks in loop.
226  assert(L.getNumBlocks() == LiveLoopBlocks.size() + DeadLoopBlocks.size() &&
227  "Malformed block sets?");
228 
229  // Now, all exit blocks that are not marked as live are dead.
230  SmallVector<BasicBlock *, 8> ExitBlocks;
231  L.getExitBlocks(ExitBlocks);
232  for (auto *ExitBlock : ExitBlocks)
233  if (!LiveExitBlocks.count(ExitBlock))
234  DeadExitBlocks.push_back(ExitBlock);
235 
236  // Whether or not the edge From->To will still be present in graph after the
237  // folding.
238  auto IsEdgeLive = [&](BasicBlock *From, BasicBlock *To) {
239  if (!LiveLoopBlocks.count(From))
240  return false;
241  BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(From);
242  return !TheOnlySucc || TheOnlySucc == To;
243  };
244 
245  // The loop will not be destroyed if its latch is live.
246  DeleteCurrentLoop = !IsEdgeLive(L.getLoopLatch(), L.getHeader());
247 
248  // If we are going to delete the current loop completely, no extra analysis
249  // is needed.
250  if (DeleteCurrentLoop)
251  return;
252 
253  // Otherwise, we should check which blocks will still be a part of the
254  // current loop after the transform.
255  BlocksInLoopAfterFolding.insert(L.getLoopLatch());
256  // If the loop is live, then we should compute what blocks are still in
257  // loop after all branch folding has been done. A block is in loop if
258  // it has a live edge to another block that is in the loop; by definition,
259  // latch is in the loop.
260  auto BlockIsInLoop = [&](BasicBlock *BB) {
261  return any_of(successors(BB), [&](BasicBlock *Succ) {
262  return BlocksInLoopAfterFolding.count(Succ) && IsEdgeLive(BB, Succ);
263  });
264  };
265  for (auto I = DFS.beginPostorder(), E = DFS.endPostorder(); I != E; ++I) {
266  BasicBlock *BB = *I;
267  if (BlockIsInLoop(BB))
268  BlocksInLoopAfterFolding.insert(BB);
269  }
270 
271  // Sanity check: header must be in loop.
272  assert(BlocksInLoopAfterFolding.count(L.getHeader()) &&
273  "Header not in loop?");
274  assert(BlocksInLoopAfterFolding.size() <= LiveLoopBlocks.size() &&
275  "All blocks that stay in loop should be live!");
276  }
277 
278  /// We need to preserve static reachibility of all loop exit blocks (this is)
279  /// required by loop pass manager. In order to do it, we make the following
280  /// trick:
281  ///
282  /// preheader:
283  /// <preheader code>
284  /// br label %loop_header
285  ///
286  /// loop_header:
287  /// ...
288  /// br i1 false, label %dead_exit, label %loop_block
289  /// ...
290  ///
291  /// We cannot simply remove edge from the loop to dead exit because in this
292  /// case dead_exit (and its successors) may become unreachable. To avoid that,
293  /// we insert the following fictive preheader:
294  ///
295  /// preheader:
296  /// <preheader code>
297  /// switch i32 0, label %preheader-split,
298  /// [i32 1, label %dead_exit_1],
299  /// [i32 2, label %dead_exit_2],
300  /// ...
301  /// [i32 N, label %dead_exit_N],
302  ///
303  /// preheader-split:
304  /// br label %loop_header
305  ///
306  /// loop_header:
307  /// ...
308  /// br i1 false, label %dead_exit_N, label %loop_block
309  /// ...
310  ///
311  /// Doing so, we preserve static reachibility of all dead exits and can later
312  /// remove edges from the loop to these blocks.
313  void handleDeadExits() {
314  // If no dead exits, nothing to do.
315  if (DeadExitBlocks.empty())
316  return;
317 
318  // Construct split preheader and the dummy switch to thread edges from it to
319  // dead exits.
321  BasicBlock *Preheader = L.getLoopPreheader();
322  BasicBlock *NewPreheader = Preheader->splitBasicBlock(
323  Preheader->getTerminator(),
324  Twine(Preheader->getName()).concat("-split"));
325  DTU.deleteEdge(Preheader, L.getHeader());
326  DTU.insertEdge(NewPreheader, L.getHeader());
327  DTU.insertEdge(Preheader, NewPreheader);
328  IRBuilder<> Builder(Preheader->getTerminator());
329  SwitchInst *DummySwitch =
330  Builder.CreateSwitch(Builder.getInt32(0), NewPreheader);
331  Preheader->getTerminator()->eraseFromParent();
332 
333  unsigned DummyIdx = 1;
334  for (BasicBlock *BB : DeadExitBlocks) {
336  for (auto &PN : BB->phis())
337  DeadPhis.push_back(&PN);
338 
339  // Eliminate all Phis from dead exits.
340  for (Instruction *PN : DeadPhis) {
341  PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
342  PN->eraseFromParent();
343  }
344  assert(DummyIdx != 0 && "Too many dead exits!");
345  DummySwitch->addCase(Builder.getInt32(DummyIdx++), BB);
346  DTU.insertEdge(Preheader, BB);
347  ++NumLoopExitsDeleted;
348  }
349 
350  assert(L.getLoopPreheader() == NewPreheader && "Malformed CFG?");
351  if (Loop *OuterLoop = LI.getLoopFor(Preheader)) {
352  OuterLoop->addBasicBlockToLoop(NewPreheader, LI);
353 
354  // When we break dead edges, the outer loop may become unreachable from
355  // the current loop. We need to fix loop info accordingly. For this, we
356  // find the most nested loop that still contains L and remove L from all
357  // loops that are inside of it.
358  Loop *StillReachable = nullptr;
359  for (BasicBlock *BB : LiveExitBlocks) {
360  Loop *BBL = LI.getLoopFor(BB);
361  if (BBL && BBL->contains(L.getHeader()))
362  if (!StillReachable ||
363  BBL->getLoopDepth() > StillReachable->getLoopDepth())
364  StillReachable = BBL;
365  }
366 
367  // Okay, our loop is no longer in the outer loop (and maybe not in some of
368  // its parents as well). Make the fixup.
369  if (StillReachable != OuterLoop) {
370  LI.changeLoopFor(NewPreheader, StillReachable);
371  for (Loop *NotContaining = OuterLoop; NotContaining != StillReachable;
372  NotContaining = NotContaining->getParentLoop()) {
373  NotContaining->removeBlockFromLoop(NewPreheader);
374  for (auto *BB : L.blocks())
375  NotContaining->removeBlockFromLoop(BB);
376  }
377  OuterLoop->removeChildLoop(&L);
378  if (StillReachable)
379  StillReachable->addChildLoop(&L);
380  else
381  LI.addTopLevelLoop(&L);
382  }
383  }
384  }
385 
386  /// Delete loop blocks that have become unreachable after folding. Make all
387  /// relevant updates to DT and LI.
388  void deleteDeadLoopBlocks() {
390  if (MSSAU) {
391  SmallPtrSet<BasicBlock *, 8> DeadLoopBlocksSet(DeadLoopBlocks.begin(),
392  DeadLoopBlocks.end());
393  MSSAU->removeBlocks(DeadLoopBlocksSet);
394  }
395  for (auto *BB : DeadLoopBlocks) {
396  assert(BB != L.getHeader() &&
397  "Header of the current loop cannot be dead!");
398  LLVM_DEBUG(dbgs() << "Deleting dead loop block " << BB->getName()
399  << "\n");
400  if (LI.isLoopHeader(BB)) {
401  assert(LI.getLoopFor(BB) != &L && "Attempt to remove current loop!");
402  LI.erase(LI.getLoopFor(BB));
403  }
404  LI.removeBlock(BB);
405  DeleteDeadBlock(BB, &DTU);
406  ++NumLoopBlocksDeleted;
407  }
408  }
409 
410  /// Constant-fold terminators of blocks acculumated in FoldCandidates into the
411  /// unconditional branches.
412  void foldTerminators() {
414 
415  for (BasicBlock *BB : FoldCandidates) {
416  assert(LI.getLoopFor(BB) == &L && "Should be a loop block!");
417  BasicBlock *TheOnlySucc = getOnlyLiveSuccessor(BB);
418  assert(TheOnlySucc && "Should have one live successor!");
419 
420  LLVM_DEBUG(dbgs() << "Replacing terminator of " << BB->getName()
421  << " with an unconditional branch to the block "
422  << TheOnlySucc->getName() << "\n");
423 
424  SmallPtrSet<BasicBlock *, 2> DeadSuccessors;
425  // Remove all BB's successors except for the live one.
426  unsigned TheOnlySuccDuplicates = 0;
427  for (auto *Succ : successors(BB))
428  if (Succ != TheOnlySucc) {
429  DeadSuccessors.insert(Succ);
430  // If our successor lies in a different loop, we don't want to remove
431  // the one-input Phi because it is a LCSSA Phi.
432  bool PreserveLCSSAPhi = !L.contains(Succ);
433  Succ->removePredecessor(BB, PreserveLCSSAPhi);
434  if (MSSAU)
435  MSSAU->removeEdge(BB, Succ);
436  } else
437  ++TheOnlySuccDuplicates;
438 
439  assert(TheOnlySuccDuplicates > 0 && "Should be!");
440  // If TheOnlySucc was BB's successor more than once, after transform it
441  // will be its successor only once. Remove redundant inputs from
442  // TheOnlySucc's Phis.
443  bool PreserveLCSSAPhi = !L.contains(TheOnlySucc);
444  for (unsigned Dup = 1; Dup < TheOnlySuccDuplicates; ++Dup)
445  TheOnlySucc->removePredecessor(BB, PreserveLCSSAPhi);
446  if (MSSAU && TheOnlySuccDuplicates > 1)
447  MSSAU->removeDuplicatePhiEdgesBetween(BB, TheOnlySucc);
448 
449  IRBuilder<> Builder(BB->getContext());
450  Instruction *Term = BB->getTerminator();
451  Builder.SetInsertPoint(Term);
452  Builder.CreateBr(TheOnlySucc);
453  Term->eraseFromParent();
454 
455  for (auto *DeadSucc : DeadSuccessors)
456  DTU.deleteEdge(BB, DeadSucc);
457 
458  ++NumTerminatorsFolded;
459  }
460  }
461 
462 public:
463  ConstantTerminatorFoldingImpl(Loop &L, LoopInfo &LI, DominatorTree &DT,
464  ScalarEvolution &SE,
465  MemorySSAUpdater *MSSAU)
466  : L(L), LI(LI), DT(DT), SE(SE), MSSAU(MSSAU) {}
467  bool run() {
468  assert(L.getLoopLatch() && "Should be single latch!");
469 
470  // Collect all available information about status of blocks after constant
471  // folding.
472  analyze();
473 
474  LLVM_DEBUG(dbgs() << "In function " << L.getHeader()->getParent()->getName()
475  << ": ");
476 
477  if (HasIrreducibleCFG) {
478  LLVM_DEBUG(dbgs() << "Loops with irreducible CFG are not supported!\n");
479  return false;
480  }
481 
482  // Nothing to constant-fold.
483  if (FoldCandidates.empty()) {
484  LLVM_DEBUG(
485  dbgs() << "No constant terminator folding candidates found in loop "
486  << L.getHeader()->getName() << "\n");
487  return false;
488  }
489 
490  // TODO: Support deletion of the current loop.
491  if (DeleteCurrentLoop) {
492  LLVM_DEBUG(
493  dbgs()
494  << "Give up constant terminator folding in loop "
495  << L.getHeader()->getName()
496  << ": we don't currently support deletion of the current loop.\n");
497  return false;
498  }
499 
500  // TODO: Support blocks that are not dead, but also not in loop after the
501  // folding.
502  if (BlocksInLoopAfterFolding.size() + DeadLoopBlocks.size() !=
503  L.getNumBlocks()) {
504  LLVM_DEBUG(
505  dbgs() << "Give up constant terminator folding in loop "
506  << L.getHeader()->getName()
507  << ": we don't currently"
508  " support blocks that are not dead, but will stop "
509  "being a part of the loop after constant-folding.\n");
510  return false;
511  }
512 
513  SE.forgetTopmostLoop(&L);
514  // Dump analysis results.
515  LLVM_DEBUG(dump());
516 
517  LLVM_DEBUG(dbgs() << "Constant-folding " << FoldCandidates.size()
518  << " terminators in loop " << L.getHeader()->getName()
519  << "\n");
520 
521  // Make the actual transforms.
522  handleDeadExits();
523  foldTerminators();
524 
525  if (!DeadLoopBlocks.empty()) {
526  LLVM_DEBUG(dbgs() << "Deleting " << DeadLoopBlocks.size()
527  << " dead blocks in loop " << L.getHeader()->getName()
528  << "\n");
529  deleteDeadLoopBlocks();
530  }
531 
532 #ifndef NDEBUG
533  // Make sure that we have preserved all data structures after the transform.
534  DT.verify();
536  LI.verify(DT);
537 #endif
538 
539  return true;
540  }
541 };
542 } // namespace
543 
544 /// Turn branches and switches with known constant conditions into unconditional
545 /// branches.
547  ScalarEvolution &SE,
548  MemorySSAUpdater *MSSAU) {
549  if (!EnableTermFolding)
550  return false;
551 
552  // To keep things simple, only process loops with single latch. We
553  // canonicalize most loops to this form. We can support multi-latch if needed.
554  if (!L.getLoopLatch())
555  return false;
556 
557  ConstantTerminatorFoldingImpl BranchFolder(L, LI, DT, SE, MSSAU);
558  return BranchFolder.run();
559 }
560 
562  LoopInfo &LI, MemorySSAUpdater *MSSAU) {
563  bool Changed = false;
565  // Copy blocks into a temporary array to avoid iterator invalidation issues
566  // as we remove them.
568 
569  for (auto &Block : Blocks) {
570  // Attempt to merge blocks in the trivial case. Don't modify blocks which
571  // belong to other loops.
572  BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
573  if (!Succ)
574  continue;
575 
576  BasicBlock *Pred = Succ->getSinglePredecessor();
577  if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
578  continue;
579 
580  // Merge Succ into Pred and delete it.
581  MergeBlockIntoPredecessor(Succ, &DTU, &LI, MSSAU);
582 
583  Changed = true;
584  }
585 
586  return Changed;
587 }
588 
589 static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
590  ScalarEvolution &SE, MemorySSAUpdater *MSSAU) {
591  bool Changed = false;
592 
593  // Constant-fold terminators with known constant conditions.
594  Changed |= constantFoldTerminators(L, DT, LI, SE, MSSAU);
595 
596  // Eliminate unconditional branches by merging blocks into their predecessors.
597  Changed |= mergeBlocksIntoPredecessors(L, DT, LI, MSSAU);
598 
599  if (Changed)
600  SE.forgetTopmostLoop(&L);
601 
602  return Changed;
603 }
604 
607  LPMUpdater &) {
609  if (EnableMSSALoopDependency && AR.MSSA)
610  MSSAU = MemorySSAUpdater(AR.MSSA);
611  if (!simplifyLoopCFG(L, AR.DT, AR.LI, AR.SE,
612  MSSAU.hasValue() ? MSSAU.getPointer() : nullptr))
613  return PreservedAnalyses::all();
614 
616 }
617 
618 namespace {
619 class LoopSimplifyCFGLegacyPass : public LoopPass {
620 public:
621  static char ID; // Pass ID, replacement for typeid
622  LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
624  }
625 
626  bool runOnLoop(Loop *L, LPPassManager &) override {
627  if (skipLoop(L))
628  return false;
629 
630  DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
631  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
632  ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
635  MemorySSA *MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
636  MSSAU = MemorySSAUpdater(MSSA);
637  if (VerifyMemorySSA)
638  MSSA->verifyMemorySSA();
639  }
640  return simplifyLoopCFG(*L, DT, LI, SE,
641  MSSAU.hasValue() ? MSSAU.getPointer() : nullptr);
642  }
643 
644  void getAnalysisUsage(AnalysisUsage &AU) const override {
648  }
651  }
652 };
653 }
654 
656 INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
657  "Simplify loop CFG", false, false)
660 INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
661  "Simplify loop CFG", false, false)
662 
664  return new LoopSimplifyCFGLegacyPass();
665 }
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&... Ranges)
Concatenated range across two or more ranges.
Definition: STLExtras.h:947
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, MemorySSAUpdater *MSSAU)
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:225
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition: MemorySSA.cpp:83
This class represents lattice values for constants.
Definition: AllocatorList.h:24
This header provides classes for managing a pipeline of passes over loops in LLVM IR...
This is the interface for a simple mod/ref and alias analysis over globals.
static cl::opt< bool > EnableTermFolding("enable-loop-simplifycfg-term-folding", cl::init(false))
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr)
Attempts to merge a block into its predecessor, if possible.
void removeDuplicatePhiEdgesBetween(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To to have a single incoming edge from From, following a CFG change that repl...
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT, LoopInfo &LI, MemorySSAUpdater *MSSAU)
Legacy pass manager pass to access dependence information.
unsigned getLoopDepth() const
Return the nesting level of this loop.
Definition: LoopInfo.h:92
The main scalar evolution driver.
void removeBlocks(const SmallPtrSetImpl< BasicBlock *> &DeadBlocks)
Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:174
static BasicBlock * getOnlyLiveSuccessor(BasicBlock *BB)
If BB is a switch or a conditional branch, but only one of its successors can be reached from this bl...
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
Delete the specified block, which must have no predecessors.
STATISTIC(NumFunctions, "Total number of functions")
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:138
bool isComplete() const
Return true if postorder numbers are assigned to all loop blocks.
Definition: LoopIterator.h:127
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:300
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
Definition: LoopInfoImpl.h:704
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:344
void removeEdge(BasicBlock *From, BasicBlock *To)
Update the MemoryPhi in To following an edge deletion between From and To.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:956
This is the interface for a SCEV-based alias analysis.
POIterator beginPostorder() const
Iterate over the cached postorder blocks.
Definition: LoopIterator.h:130
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
loop simplifycfg
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:690
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:701
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition: LoopInfo.cpp:611
RPOIterator endRPO() const
Definition: LoopIterator.h:141
BlockT * getHeader() const
Definition: LoopInfo.h:100
void getExitBlocks(SmallVectorImpl< BlockT *> &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:63
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
Definition: LoopInfo.h:741
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:269
static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI, ScalarEvolution &SE, MemorySSAUpdater *MSSAU)
Turn branches and switches with known constant conditions into unconditional branches.
void perform(LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:817
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:234
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:371
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
const T * getPointer() const
Definition: Optional.h:153
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1415
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
size_t size() const
Definition: SmallVector.h:53
void deleteEdge(BasicBlock *From, BasicBlock *To)
Notify all available trees on an edge deletion.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
size_type size() const
Definition: SmallPtrSet.h:93
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:110
void insertEdge(BasicBlock *From, BasicBlock *To)
Notify all available trees on an edge insertion.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
BlockVerifier::State From
void verifyMemorySSA() const
Verify that MemorySSA is self consistent (IE definitions dominate all uses, uses appear in the right ...
Definition: MemorySSA.cpp:1776
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Pass * createLoopSimplifyCFGPass()
loop Simplify loop CFG
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
Store the result of a depth first search within basic blocks contained by a single loop...
Definition: LoopIterator.h:98
static void DFS(BasicBlock *Root, SetVector< BasicBlock *> &Set)
bool isLoopHeader(const BlockT *BB) const
Definition: LoopInfo.h:703
void initializeLoopSimplifyCFGLegacyPassPass(PassRegistry &)
POIterator endPostorder() const
Definition: LoopIterator.h:134
LoopT * getParentLoop() const
Definition: LoopInfo.h:101
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
Definition: LoopInfo.h:163
bool hasValue() const
Definition: Optional.h:165
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
Definition: LoopInfo.h:331
INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg", "Simplify loop CFG", false, false) INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:465
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:107
#define I(x, y, z)
Definition: MD5.cpp:58
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:193
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass&#39;s AnalysisUsage.
Definition: LoopUtils.cpp:132
LLVM_NODISCARD 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:323
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
Definition: LoopInfo.h:722
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:408
Multiway switch.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
rpo Deduce function attributes in RPO
succ_range successors(Instruction *I)
Definition: CFG.h:264
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
Definition: LoopIterator.h:137
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
This is the interface for LLVM&#39;s primary stateless and local alias analysis.
A container for analyses that lazily runs them and caches their results.
This pass exposes codegen information to IR-level passes.
#define LLVM_DEBUG(X)
Definition: Debug.h:123
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:156
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Definition: LoopInfo.h:749
void forgetTopmostLoop(const Loop *L)
cl::opt< bool > EnableMSSALoopDependency
Enables memory ssa as a dependency for loop passes.