LLVM  8.0.0svn
LoopExtractor.cpp
Go to the documentation of this file.
1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
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 // A pass wrapper around the ExtractLoop() scalar transformation to extract each
11 // top-level loop into its own new function. If the loop is the ONLY loop in a
12 // given function, it is not touched. This is a pass most useful for debugging
13 // via bugpoint.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/LoopPass.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
24 #include "llvm/Transforms/IPO.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Transforms/Utils.h"
29 #include <fstream>
30 #include <set>
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "loop-extract"
34 
35 STATISTIC(NumExtracted, "Number of loops extracted");
36 
37 namespace {
38  struct LoopExtractor : public LoopPass {
39  static char ID; // Pass identification, replacement for typeid
40  unsigned NumLoops;
41 
42  explicit LoopExtractor(unsigned numLoops = ~0)
43  : LoopPass(ID), NumLoops(numLoops) {
45  }
46 
47  bool runOnLoop(Loop *L, LPPassManager &) override;
48 
49  void getAnalysisUsage(AnalysisUsage &AU) const override {
54  }
55  };
56 }
57 
58 char LoopExtractor::ID = 0;
59 INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
60  "Extract loops into new functions", false, false)
61 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
62 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
64 INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
65  "Extract loops into new functions", false, false)
66 
67 namespace {
68  /// SingleLoopExtractor - For bugpoint.
69  struct SingleLoopExtractor : public LoopExtractor {
70  static char ID; // Pass identification, replacement for typeid
71  SingleLoopExtractor() : LoopExtractor(1) {}
72  };
73 } // End anonymous namespace
74 
76 INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
77  "Extract at most one loop into a new function", false, false)
78 
79 // createLoopExtractorPass - This pass extracts all natural loops from the
80 // program into a function if it can.
81 //
82 Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
83 
84 bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
85  if (skipLoop(L))
86  return false;
87 
88  // Only visit top-level loops.
89  if (L->getParentLoop())
90  return false;
91 
92  // If LoopSimplify form is not available, stay out of trouble.
93  if (!L->isLoopSimplifyForm())
94  return false;
95 
96  DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
97  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
98  bool Changed = false;
99 
100  // If there is more than one top-level loop in this function, extract all of
101  // the loops. Otherwise there is exactly one top-level loop; in this case if
102  // this function is more than a minimal wrapper around the loop, extract
103  // the loop.
104  bool ShouldExtractLoop = false;
105 
106  // Extract the loop if the entry block doesn't branch to the loop header.
107  TerminatorInst *EntryTI =
109  if (!isa<BranchInst>(EntryTI) ||
110  !cast<BranchInst>(EntryTI)->isUnconditional() ||
111  EntryTI->getSuccessor(0) != L->getHeader()) {
112  ShouldExtractLoop = true;
113  } else {
114  // Check to see if any exits from the loop are more than just return
115  // blocks.
116  SmallVector<BasicBlock*, 8> ExitBlocks;
117  L->getExitBlocks(ExitBlocks);
118  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
119  if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
120  ShouldExtractLoop = true;
121  break;
122  }
123  }
124 
125  if (ShouldExtractLoop) {
126  // We must omit EH pads. EH pads must accompany the invoke
127  // instruction. But this would result in a loop in the extracted
128  // function. An infinite cycle occurs when it tries to extract that loop as
129  // well.
130  SmallVector<BasicBlock*, 8> ExitBlocks;
131  L->getExitBlocks(ExitBlocks);
132  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
133  if (ExitBlocks[i]->isEHPad()) {
134  ShouldExtractLoop = false;
135  break;
136  }
137  }
138 
139  if (ShouldExtractLoop) {
140  if (NumLoops == 0) return Changed;
141  --NumLoops;
142  CodeExtractor Extractor(DT, *L);
143  if (Extractor.extractCodeRegion() != nullptr) {
144  Changed = true;
145  // After extraction, the loop is replaced by a function call, so
146  // we shouldn't try to run any more loop passes on it.
147  LPM.markLoopAsDeleted(*L);
148  LI.erase(L);
149  }
150  ++NumExtracted;
151  }
152 
153  return Changed;
154 }
155 
156 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
157 // program into a function if it can. This is used by bugpoint.
158 //
160  return new SingleLoopExtractor();
161 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
Utility class for extracting code into a new function.
Definition: CodeExtractor.h:50
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void initializeLoopExtractorPass(PassRegistry &)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
Function * extractCodeRegion()
Perform the extraction, returning the new function.
Pass * createSingleLoopExtractorPass()
createSingleLoopExtractorPass - This pass extracts one natural loop from the program into a function ...
STATISTIC(NumFunctions, "Total number of functions")
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition: LoopInfo.cpp:580
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
SingleLoopExtractor - For bugpoint.
loop Extract loops into new functions
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
const BasicBlock & getEntryBlock() const
Definition: Function.h:640
Subclasses of this class are all able to terminate a basic block.
Definition: InstrTypes.h:55
char & BreakCriticalEdgesID
Represent the analysis usage information of a pass.
size_t size() const
Definition: SmallVector.h:53
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
char & LoopSimplifyID
INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single", "Extract at most one loop into a new function", false, false) Pass *llvm
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:299
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract", "Extract loops into new functions", false, false) INITIALIZE_PASS_END(LoopExtractor
void markLoopAsDeleted(Loop &L)
Definition: LoopPass.cpp:143
Pass * createLoopExtractorPass()
createLoopExtractorPass - This pass extracts all natural loops from the program into a function if it...
loop extract
LoopT * getParentLoop() const
Definition: LoopInfo.h:101
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to...
Definition: LoopInfo.cpp:192
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
The legacy pass manager&#39;s analysis pass to compute loop information.
Definition: LoopInfo.h:964
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:260
const TerminatorInst * 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
loops
Definition: LoopInfo.cpp:707