1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
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/Support/Compiler.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Transforms/Utils/FunctionUtils.h"
27 #include "llvm/ADT/Statistic.h"
30 STATISTIC(NumExtracted, "Number of loops extracted");
33 // FIXME: This is not a function pass, but the PassManager doesn't allow
34 // Module passes to require FunctionPasses, so we can't get loop info if we're
35 // not a function pass.
36 struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass {
37 static char ID; // Pass identification, replacement for typeid
40 explicit LoopExtractor(unsigned numLoops = ~0)
41 : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {}
43 virtual bool runOnFunction(Function &F);
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.addRequiredID(BreakCriticalEdgesID);
47 AU.addRequiredID(LoopSimplifyID);
48 AU.addRequired<DominatorTree>();
49 AU.addRequired<LoopInfo>();
53 char LoopExtractor::ID = 0;
54 RegisterPass<LoopExtractor>
55 X("loop-extract", "Extract loops into new functions");
57 /// SingleLoopExtractor - For bugpoint.
58 struct SingleLoopExtractor : public LoopExtractor {
59 static char ID; // Pass identification, replacement for typeid
60 SingleLoopExtractor() : LoopExtractor(1) {}
63 char SingleLoopExtractor::ID = 0;
64 RegisterPass<SingleLoopExtractor>
65 Y("loop-extract-single", "Extract at most one loop into a new function");
66 } // End anonymous namespace
68 // createLoopExtractorPass - This pass extracts all natural loops from the
69 // program into a function if it can.
71 FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
73 bool LoopExtractor::runOnFunction(Function &F) {
74 LoopInfo &LI = getAnalysis<LoopInfo>();
76 // If this function has no loops, there is nothing to do.
77 if (LI.begin() == LI.end())
80 DominatorTree &DT = getAnalysis<DominatorTree>();
82 // If there is more than one top-level loop in this function, extract all of
85 if (LI.end()-LI.begin() > 1) {
86 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
87 if (NumLoops == 0) return Changed;
89 Changed |= ExtractLoop(DT, *i) != 0;
93 // Otherwise there is exactly one top-level loop. If this function is more
94 // than a minimal wrapper around the loop, extract the loop.
95 Loop *TLL = *LI.begin();
96 bool ShouldExtractLoop = false;
98 // Extract the loop if the entry block doesn't branch to the loop header.
99 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
100 if (!isa<BranchInst>(EntryTI) ||
101 !cast<BranchInst>(EntryTI)->isUnconditional() ||
102 EntryTI->getSuccessor(0) != TLL->getHeader())
103 ShouldExtractLoop = true;
105 // Check to see if any exits from the loop are more than just return
107 SmallVector<BasicBlock*, 8> ExitBlocks;
108 TLL->getExitBlocks(ExitBlocks);
109 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
110 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
111 ShouldExtractLoop = true;
116 if (ShouldExtractLoop) {
117 if (NumLoops == 0) return Changed;
119 Changed |= ExtractLoop(DT, TLL) != 0;
122 // Okay, this function is a minimal container around the specified loop.
123 // If we extract the loop, we will continue to just keep extracting it
124 // infinitely... so don't extract it. However, if the loop contains any
125 // subloops, extract them.
126 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
127 if (NumLoops == 0) return Changed;
129 Changed |= ExtractLoop(DT, *i) != 0;
138 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
139 // program into a function if it can. This is used by bugpoint.
141 FunctionPass *llvm::createSingleLoopExtractorPass() {
142 return new SingleLoopExtractor();
147 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
148 /// from the module into their own functions except for those specified by the
149 /// BlocksToNotExtract list.
150 class BlockExtractorPass : public ModulePass {
151 std::vector<BasicBlock*> BlocksToNotExtract;
153 static char ID; // Pass identification, replacement for typeid
154 explicit BlockExtractorPass(std::vector<BasicBlock*> &B)
155 : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {}
156 BlockExtractorPass() : ModulePass((intptr_t)&ID) {}
158 bool runOnModule(Module &M);
161 char BlockExtractorPass::ID = 0;
162 RegisterPass<BlockExtractorPass>
163 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
166 // createBlockExtractorPass - This pass extracts all blocks (except those
167 // specified in the argument list) from the functions in the module.
169 ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
170 return new BlockExtractorPass(BTNE);
173 bool BlockExtractorPass::runOnModule(Module &M) {
174 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
175 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
176 BasicBlock *BB = BlocksToNotExtract[i];
177 Function *F = BB->getParent();
179 // Map the corresponding function in this module.
180 Function *MF = M.getFunction(F->getName());
181 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
183 // Figure out which index the basic block is in its function.
184 Function::iterator BBI = MF->begin();
185 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
186 TranslatedBlocksToNotExtract.insert(BBI);
189 // Now that we know which blocks to not extract, figure out which ones we WANT
191 std::vector<BasicBlock*> BlocksToExtract;
192 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
193 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
194 if (!TranslatedBlocksToNotExtract.count(BB))
195 BlocksToExtract.push_back(BB);
197 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
198 ExtractBasicBlock(BlocksToExtract[i]);
200 return !BlocksToExtract.empty();