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 #include "llvm/Transforms/IPO.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Module.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Analysis/Dominators.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Utils/FunctionUtils.h"
25 #include "llvm/ADT/Statistic.h"
29 Statistic<> NumExtracted("loop-extract", "Number of loops extracted");
31 // FIXME: This is not a function pass, but the PassManager doesn't allow
32 // Module passes to require FunctionPasses, so we can't get loop info if we're
33 // not a function pass.
34 struct LoopExtractor : public FunctionPass {
37 LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
39 virtual bool runOnFunction(Function &F);
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequiredID(BreakCriticalEdgesID);
43 AU.addRequiredID(LoopSimplifyID);
44 AU.addRequired<DominatorSet>();
45 AU.addRequired<LoopInfo>();
49 RegisterPass<LoopExtractor>
50 X("loop-extract", "Extract loops into new functions");
52 /// SingleLoopExtractor - For bugpoint.
53 struct SingleLoopExtractor : public LoopExtractor {
54 SingleLoopExtractor() : LoopExtractor(1) {}
57 RegisterPass<SingleLoopExtractor>
58 Y("loop-extract-single", "Extract at most one loop into a new function");
59 } // End anonymous namespace
61 // createLoopExtractorPass - This pass extracts all natural loops from the
62 // program into a function if it can.
64 FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
66 bool LoopExtractor::runOnFunction(Function &F) {
67 LoopInfo &LI = getAnalysis<LoopInfo>();
69 // If this function has no loops, there is nothing to do.
70 if (LI.begin() == LI.end())
73 DominatorSet &DS = getAnalysis<DominatorSet>();
75 // If there is more than one top-level loop in this function, extract all of
78 if (LI.end()-LI.begin() > 1) {
79 for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) {
80 if (NumLoops == 0) return Changed;
82 Changed |= ExtractLoop(DS, *i) != 0;
86 // Otherwise there is exactly one top-level loop. If this function is more
87 // than a minimal wrapper around the loop, extract the loop.
88 Loop *TLL = *LI.begin();
89 bool ShouldExtractLoop = false;
91 // Extract the loop if the entry block doesn't branch to the loop header.
92 TerminatorInst *EntryTI = F.getEntryBlock().getTerminator();
93 if (!isa<BranchInst>(EntryTI) ||
94 !cast<BranchInst>(EntryTI)->isUnconditional() ||
95 EntryTI->getSuccessor(0) != TLL->getHeader())
96 ShouldExtractLoop = true;
98 // Check to see if any exits from the loop are more than just return
100 std::vector<BasicBlock*> ExitBlocks;
101 TLL->getExitBlocks(ExitBlocks);
102 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
103 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
104 ShouldExtractLoop = true;
109 if (ShouldExtractLoop) {
110 if (NumLoops == 0) return Changed;
112 Changed |= ExtractLoop(DS, TLL) != 0;
115 // Okay, this function is a minimal container around the specified loop.
116 // If we extract the loop, we will continue to just keep extracting it
117 // infinitely... so don't extract it. However, if the loop contains any
118 // subloops, extract them.
119 for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) {
120 if (NumLoops == 0) return Changed;
122 Changed |= ExtractLoop(DS, *i) != 0;
131 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
132 // program into a function if it can. This is used by bugpoint.
134 FunctionPass *llvm::createSingleLoopExtractorPass() {
135 return new SingleLoopExtractor();
140 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
141 /// from the module into their own functions except for those specified by the
142 /// BlocksToNotExtract list.
143 class BlockExtractorPass : public ModulePass {
144 std::vector<BasicBlock*> BlocksToNotExtract;
146 BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
147 BlockExtractorPass() {}
149 bool runOnModule(Module &M);
151 RegisterPass<BlockExtractorPass>
152 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
155 // createBlockExtractorPass - This pass extracts all blocks (except those
156 // specified in the argument list) from the functions in the module.
158 ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
159 return new BlockExtractorPass(BTNE);
162 bool BlockExtractorPass::runOnModule(Module &M) {
163 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
164 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
165 BasicBlock *BB = BlocksToNotExtract[i];
166 Function *F = BB->getParent();
168 // Map the corresponding function in this module.
169 Function *MF = M.getFunction(F->getName(), F->getFunctionType());
171 // Figure out which index the basic block is in its function.
172 Function::iterator BBI = MF->begin();
173 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
174 TranslatedBlocksToNotExtract.insert(BBI);
177 // Now that we know which blocks to not extract, figure out which ones we WANT
179 std::vector<BasicBlock*> BlocksToExtract;
180 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
181 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
182 if (!TranslatedBlocksToNotExtract.count(BB))
183 BlocksToExtract.push_back(BB);
185 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
186 ExtractBasicBlock(BlocksToExtract[i]);
188 return !BlocksToExtract.empty();