5a6e7671d1b9cf3b452f4b6810c2d0ca271b637d
[oota-llvm.git] / lib / Transforms / IPO / LoopExtractor.cpp
1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 #define DEBUG_TYPE "loop-extract"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Module.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Analysis/Dominators.h"
23 #include "llvm/Analysis/LoopInfo.h"
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Transforms/Utils/FunctionUtils.h"
26 #include "llvm/ADT/Statistic.h"
27 using namespace llvm;
28
29 STATISTIC(NumExtracted, "Number of loops extracted");
30
31 namespace {
32   // FIXME: This is not a function pass, but the PassManager doesn't allow
33   // Module passes to require FunctionPasses, so we can't get loop info if we're
34   // not a function pass.
35   struct LoopExtractor : public FunctionPass {
36     unsigned NumLoops;
37
38     LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
39
40     virtual bool runOnFunction(Function &F);
41
42     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43       AU.addRequiredID(BreakCriticalEdgesID);
44       AU.addRequiredID(LoopSimplifyID);
45       AU.addRequired<DominatorSet>();
46       AU.addRequired<LoopInfo>();
47     }
48   };
49
50   RegisterPass<LoopExtractor>
51   X("loop-extract", "Extract loops into new functions");
52
53   /// SingleLoopExtractor - For bugpoint.
54   struct SingleLoopExtractor : public LoopExtractor {
55     SingleLoopExtractor() : LoopExtractor(1) {}
56   };
57
58   RegisterPass<SingleLoopExtractor>
59   Y("loop-extract-single", "Extract at most one loop into a new function");
60 } // End anonymous namespace
61
62 // createLoopExtractorPass - This pass extracts all natural loops from the
63 // program into a function if it can.
64 //
65 FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
66
67 bool LoopExtractor::runOnFunction(Function &F) {
68   LoopInfo &LI = getAnalysis<LoopInfo>();
69
70   // If this function has no loops, there is nothing to do.
71   if (LI.begin() == LI.end())
72     return false;
73
74   DominatorSet &DS = getAnalysis<DominatorSet>();
75
76   // If there is more than one top-level loop in this function, extract all of
77   // the loops.
78   bool Changed = false;
79   if (LI.end()-LI.begin() > 1) {
80     for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
81       if (NumLoops == 0) return Changed;
82       --NumLoops;
83       Changed |= ExtractLoop(DS, *i) != 0;
84       ++NumExtracted;
85     }
86   } else {
87     // Otherwise there is exactly one top-level loop.  If this function is more
88     // than a minimal wrapper around the loop, extract the loop.
89     Loop *TLL = *LI.begin();
90     bool ShouldExtractLoop = false;
91
92     // Extract the loop if the entry block doesn't branch to the loop header.
93     TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
94     if (!isa<BranchInst>(EntryTI) ||
95         !cast<BranchInst>(EntryTI)->isUnconditional() ||
96         EntryTI->getSuccessor(0) != TLL->getHeader())
97       ShouldExtractLoop = true;
98     else {
99       // Check to see if any exits from the loop are more than just return
100       // blocks.
101       std::vector<BasicBlock*> ExitBlocks;
102       TLL->getExitBlocks(ExitBlocks);
103       for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
104         if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
105           ShouldExtractLoop = true;
106           break;
107         }
108     }
109
110     if (ShouldExtractLoop) {
111       if (NumLoops == 0) return Changed;
112       --NumLoops;
113       Changed |= ExtractLoop(DS, TLL) != 0;
114       ++NumExtracted;
115     } else {
116       // Okay, this function is a minimal container around the specified loop.
117       // If we extract the loop, we will continue to just keep extracting it
118       // infinitely... so don't extract it.  However, if the loop contains any
119       // subloops, extract them.
120       for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
121         if (NumLoops == 0) return Changed;
122         --NumLoops;
123         Changed |= ExtractLoop(DS, *i) != 0;
124         ++NumExtracted;
125       }
126     }
127   }
128
129   return Changed;
130 }
131
132 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
133 // program into a function if it can.  This is used by bugpoint.
134 //
135 FunctionPass *llvm::createSingleLoopExtractorPass() {
136   return new SingleLoopExtractor();
137 }
138
139
140 namespace {
141   /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
142   /// from the module into their own functions except for those specified by the
143   /// BlocksToNotExtract list.
144   class BlockExtractorPass : public ModulePass {
145     std::vector<BasicBlock*> BlocksToNotExtract;
146   public:
147     BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
148     BlockExtractorPass() {}
149
150     bool runOnModule(Module &M);
151   };
152   RegisterPass<BlockExtractorPass>
153   XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
154 }
155
156 // createBlockExtractorPass - This pass extracts all blocks (except those
157 // specified in the argument list) from the functions in the module.
158 //
159 ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
160   return new BlockExtractorPass(BTNE);
161 }
162
163 bool BlockExtractorPass::runOnModule(Module &M) {
164   std::set<BasicBlock*> TranslatedBlocksToNotExtract;
165   for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
166     BasicBlock *BB = BlocksToNotExtract[i];
167     Function *F = BB->getParent();
168
169     // Map the corresponding function in this module.
170     Function *MF = M.getFunction(F->getName(), F->getFunctionType());
171
172     // Figure out which index the basic block is in its function.
173     Function::iterator BBI = MF->begin();
174     std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
175     TranslatedBlocksToNotExtract.insert(BBI);
176   }
177
178   // Now that we know which blocks to not extract, figure out which ones we WANT
179   // to extract.
180   std::vector<BasicBlock*> BlocksToExtract;
181   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
182     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
183       if (!TranslatedBlocksToNotExtract.count(BB))
184         BlocksToExtract.push_back(BB);
185
186   for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
187     ExtractBasicBlock(BlocksToExtract[i]);
188
189   return !BlocksToExtract.empty();
190 }