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