LLVM  4.0.0
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"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/Transforms/Scalar.h"
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "loop-simplifycfg"
38 
39 static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI) {
40  bool Changed = false;
41  // Copy blocks into a temporary array to avoid iterator invalidation issues
42  // as we remove them.
43  SmallVector<WeakVH, 16> Blocks(L.blocks());
44 
45  for (auto &Block : Blocks) {
46  // Attempt to merge blocks in the trivial case. Don't modify blocks which
47  // belong to other loops.
48  BasicBlock *Succ = cast_or_null<BasicBlock>(Block);
49  if (!Succ)
50  continue;
51 
52  BasicBlock *Pred = Succ->getSinglePredecessor();
53  if (!Pred || !Pred->getSingleSuccessor() || LI.getLoopFor(Pred) != &L)
54  continue;
55 
56  // Pred is going to disappear, so we need to update the loop info.
57  if (L.getHeader() == Pred)
58  L.moveToHeader(Succ);
59  LI.removeBlock(Pred);
60  MergeBasicBlockIntoOnlyPred(Succ, &DT);
61  Changed = true;
62  }
63 
64  return Changed;
65 }
66 
69  LPMUpdater &) {
70  if (!simplifyLoopCFG(L, AR.DT, AR.LI))
71  return PreservedAnalyses::all();
73 }
74 
75 namespace {
76 class LoopSimplifyCFGLegacyPass : public LoopPass {
77 public:
78  static char ID; // Pass ID, replacement for typeid
79  LoopSimplifyCFGLegacyPass() : LoopPass(ID) {
81  }
82 
83  bool runOnLoop(Loop *L, LPPassManager &) override {
84  if (skipLoop(L))
85  return false;
86 
87  DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
88  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
89  return simplifyLoopCFG(*L, DT, LI);
90  }
91 
92  void getAnalysisUsage(AnalysisUsage &AU) const override {
95  }
96 };
97 }
98 
100 INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
101  "Simplify loop CFG", false, false)
103 INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass, "loop-simplifycfg",
104  "Simplify loop CFG", false, false)
105 
107  return new LoopSimplifyCFGLegacyPass();
108 }
MachineLoop * L
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
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.
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:637
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.
Legacy pass manager pass to access dependence information.
void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT=nullptr)
BB is a block with one predecessor and its predecessor is known to have one successor (BB!)...
Definition: Local.cpp:572
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:575
BlockT * getHeader() const
Definition: LoopInfo.h:102
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
This is the interface for a SCEV-based alias analysis.
loop simplifycfg
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:143
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
loop Simplify loop false
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Pass * createLoopSimplifyCFGPass()
loop Simplify loop CFG
BasicBlock * getSingleSuccessor()
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:253
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:226
void initializeLoopSimplifyCFGLegacyPassPass(PassRegistry &)
void moveToHeader(BlockT *BB)
This method is used to move BB (which must be part of this loop) to be the loop header of the loop (t...
Definition: LoopInfo.h:316
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition: LoopUtils.cpp:938
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
This is the interface for LLVM'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.
INITIALIZE_PASS_BEGIN(LoopSimplifyCFGLegacyPass,"loop-simplifycfg","Simplify loop CFG", false, false) INITIALIZE_PASS_END(LoopSimplifyCFGLegacyPass
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI)