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/Transforms/Scalar.h"
25 #include "llvm/Transforms/Utils/FunctionUtils.h"
26 #include "llvm/ADT/Statistic.h"
29 STATISTIC(NumExtracted, "Number of loops extracted");
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 {
38 LoopExtractor(unsigned numLoops = ~0) : NumLoops(numLoops) {}
40 virtual bool runOnFunction(Function &F);
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequiredID(BreakCriticalEdgesID);
44 AU.addRequiredID(LoopSimplifyID);
45 AU.addRequired<DominatorSet>();
46 AU.addRequired<LoopInfo>();
50 RegisterPass<LoopExtractor>
51 X("loop-extract", "Extract loops into new functions");
53 /// SingleLoopExtractor - For bugpoint.
54 struct SingleLoopExtractor : public LoopExtractor {
55 SingleLoopExtractor() : LoopExtractor(1) {}
58 RegisterPass<SingleLoopExtractor>
59 Y("loop-extract-single", "Extract at most one loop into a new function");
60 } // End anonymous namespace
62 // createLoopExtractorPass - This pass extracts all natural loops from the
63 // program into a function if it can.
65 FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
67 bool LoopExtractor::runOnFunction(Function &F) {
68 LoopInfo &LI = getAnalysis<LoopInfo>();
70 // If this function has no loops, there is nothing to do.
71 if (LI.begin() == LI.end())
74 DominatorSet &DS = getAnalysis<DominatorSet>();
76 // If there is more than one top-level loop in this function, extract all of
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;
83 Changed |= ExtractLoop(DS, *i) != 0;
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;
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;
99 // Check to see if any exits from the loop are more than just return
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;
110 if (ShouldExtractLoop) {
111 if (NumLoops == 0) return Changed;
113 Changed |= ExtractLoop(DS, TLL) != 0;
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;
123 Changed |= ExtractLoop(DS, *i) != 0;
132 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
133 // program into a function if it can. This is used by bugpoint.
135 FunctionPass *llvm::createSingleLoopExtractorPass() {
136 return new SingleLoopExtractor();
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;
147 BlockExtractorPass(std::vector<BasicBlock*> &B) : BlocksToNotExtract(B) {}
148 BlockExtractorPass() {}
150 bool runOnModule(Module &M);
152 RegisterPass<BlockExtractorPass>
153 XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)");
156 // createBlockExtractorPass - This pass extracts all blocks (except those
157 // specified in the argument list) from the functions in the module.
159 ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) {
160 return new BlockExtractorPass(BTNE);
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();
169 // Map the corresponding function in this module.
170 Function *MF = M.getFunction(F->getName(), F->getFunctionType());
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);
178 // Now that we know which blocks to not extract, figure out which ones we WANT
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);
186 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
187 ExtractBasicBlock(BlocksToExtract[i]);
189 return !BlocksToExtract.empty();