Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 1 | //===- LoopExtractor.cpp - Extract each loop into a new function ----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | efddcfa | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | efddcfa | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | 9401deb | 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 | 38b8fd1 | 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 | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "loop-extract" |
Chris Lattner | 1e3cb34 | 2004-03-14 02:37:16 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
Chris Lattner | 369287b | 2004-03-18 03:48:06 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 16d0eb0 | 2004-03-14 04:01:06 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 30 | STATISTIC(NumExtracted, "Number of loops extracted"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 86453c5 | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 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. |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 36 | struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 37 | static char ID; // Pass identifcation, replacement for typeid |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 38 | unsigned NumLoops; |
| 39 | |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 40 | LoopExtractor(unsigned numLoops = ~0) |
| 41 | : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {} |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 42 | |
Chris Lattner | efddcfa | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 43 | virtual bool runOnFunction(Function &F); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 44 | |
Chris Lattner | efddcfa | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | fdded9f | 2004-03-18 05:43:18 +0000 | [diff] [blame] | 46 | AU.addRequiredID(BreakCriticalEdgesID); |
| 47 | AU.addRequiredID(LoopSimplifyID); |
Owen Anderson | c6fcf29 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 48 | AU.addRequired<ETForest>(); |
| 49 | AU.addRequired<DominatorTree>(); |
Chris Lattner | efddcfa | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 50 | AU.addRequired<LoopInfo>(); |
| 51 | } |
| 52 | }; |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 53 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 54 | char LoopExtractor::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 55 | RegisterPass<LoopExtractor> |
Chris Lattner | efddcfa | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 56 | X("loop-extract", "Extract loops into new functions"); |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 57 | |
| 58 | /// SingleLoopExtractor - For bugpoint. |
| 59 | struct SingleLoopExtractor : public LoopExtractor { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 60 | static char ID; // Pass identifcation, replacement for typeid |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 61 | SingleLoopExtractor() : LoopExtractor(1) {} |
| 62 | }; |
| 63 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 64 | char SingleLoopExtractor::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 65 | RegisterPass<SingleLoopExtractor> |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 66 | Y("loop-extract-single", "Extract at most one loop into a new function"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 67 | } // End anonymous namespace |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 68 | |
Jeff Cohen | bf65268 | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 69 | // createLoopExtractorPass - This pass extracts all natural loops from the |
| 70 | // program into a function if it can. |
| 71 | // |
Jeff Cohen | ecc1cef | 2005-01-10 04:23:32 +0000 | [diff] [blame] | 72 | FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); } |
Jeff Cohen | bf65268 | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 73 | |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 74 | bool LoopExtractor::runOnFunction(Function &F) { |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 75 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 76 | |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 77 | // If this function has no loops, there is nothing to do. |
| 78 | if (LI.begin() == LI.end()) |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 79 | return false; |
| 80 | |
Owen Anderson | c6fcf29 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 81 | ETForest &EF = getAnalysis<ETForest>(); |
| 82 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | 369287b | 2004-03-18 03:48:06 +0000 | [diff] [blame] | 83 | |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 84 | // If there is more than one top-level loop in this function, extract all of |
| 85 | // the loops. |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 86 | bool Changed = false; |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 87 | if (LI.end()-LI.begin() > 1) { |
| 88 | for (LoopInfo::iterator i = LI.begin(), e = LI.end(); i != e; ++i) { |
| 89 | if (NumLoops == 0) return Changed; |
| 90 | --NumLoops; |
Owen Anderson | c6fcf29 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 91 | Changed |= ExtractLoop(EF, DT, *i) != 0; |
Chris Lattner | f6e43bc | 2004-03-18 05:46:10 +0000 | [diff] [blame] | 92 | ++NumExtracted; |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 93 | } |
| 94 | } else { |
| 95 | // Otherwise there is exactly one top-level loop. If this function is more |
| 96 | // than a minimal wrapper around the loop, extract the loop. |
| 97 | Loop *TLL = *LI.begin(); |
| 98 | bool ShouldExtractLoop = false; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 100 | // Extract the loop if the entry block doesn't branch to the loop header. |
| 101 | TerminatorInst *EntryTI = F.getEntryBlock().getTerminator(); |
| 102 | if (!isa<BranchInst>(EntryTI) || |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 103 | !cast<BranchInst>(EntryTI)->isUnconditional() || |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 104 | EntryTI->getSuccessor(0) != TLL->getHeader()) |
| 105 | ShouldExtractLoop = true; |
| 106 | else { |
| 107 | // Check to see if any exits from the loop are more than just return |
| 108 | // blocks. |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 109 | std::vector<BasicBlock*> ExitBlocks; |
| 110 | TLL->getExitBlocks(ExitBlocks); |
| 111 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 112 | if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) { |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 113 | ShouldExtractLoop = true; |
| 114 | break; |
| 115 | } |
| 116 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 118 | if (ShouldExtractLoop) { |
| 119 | if (NumLoops == 0) return Changed; |
| 120 | --NumLoops; |
Owen Anderson | c6fcf29 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 121 | Changed |= ExtractLoop(EF, DT, TLL) != 0; |
Chris Lattner | f6e43bc | 2004-03-18 05:46:10 +0000 | [diff] [blame] | 122 | ++NumExtracted; |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 123 | } else { |
| 124 | // Okay, this function is a minimal container around the specified loop. |
| 125 | // If we extract the loop, we will continue to just keep extracting it |
| 126 | // infinitely... so don't extract it. However, if the loop contains any |
| 127 | // subloops, extract them. |
| 128 | for (Loop::iterator i = TLL->begin(), e = TLL->end(); i != e; ++i) { |
| 129 | if (NumLoops == 0) return Changed; |
| 130 | --NumLoops; |
Owen Anderson | c6fcf29 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 131 | Changed |= ExtractLoop(EF, DT, *i) != 0; |
Chris Lattner | f6e43bc | 2004-03-18 05:46:10 +0000 | [diff] [blame] | 132 | ++NumExtracted; |
Chris Lattner | 5156c39 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 135 | } |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 136 | |
| 137 | return Changed; |
| 138 | } |
| 139 | |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 140 | // createSingleLoopExtractorPass - This pass extracts one natural loop from the |
| 141 | // program into a function if it can. This is used by bugpoint. |
| 142 | // |
Jeff Cohen | ecc1cef | 2005-01-10 04:23:32 +0000 | [diff] [blame] | 143 | FunctionPass *llvm::createSingleLoopExtractorPass() { |
Chris Lattner | 41bc0b0 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 144 | return new SingleLoopExtractor(); |
Misha Brukman | 9401deb | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 146 | |
| 147 | |
| 148 | namespace { |
| 149 | /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks |
| 150 | /// from the module into their own functions except for those specified by the |
| 151 | /// BlocksToNotExtract list. |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 152 | class BlockExtractorPass : public ModulePass { |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 153 | std::vector<BasicBlock*> BlocksToNotExtract; |
| 154 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 155 | static char ID; // Pass identifcation, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 156 | BlockExtractorPass(std::vector<BasicBlock*> &B) |
| 157 | : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {} |
| 158 | BlockExtractorPass() : ModulePass((intptr_t)&ID) {} |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 159 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 160 | bool runOnModule(Module &M); |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 161 | }; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 162 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 163 | char BlockExtractorPass::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 164 | RegisterPass<BlockExtractorPass> |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 165 | XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)"); |
| 166 | } |
| 167 | |
| 168 | // createBlockExtractorPass - This pass extracts all blocks (except those |
| 169 | // specified in the argument list) from the functions in the module. |
| 170 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 171 | ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) { |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 172 | return new BlockExtractorPass(BTNE); |
| 173 | } |
| 174 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 175 | bool BlockExtractorPass::runOnModule(Module &M) { |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 176 | std::set<BasicBlock*> TranslatedBlocksToNotExtract; |
| 177 | for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) { |
| 178 | BasicBlock *BB = BlocksToNotExtract[i]; |
| 179 | Function *F = BB->getParent(); |
| 180 | |
| 181 | // Map the corresponding function in this module. |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 182 | Function *MF = M.getFunction(F->getName()); |
| 183 | assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?"); |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 184 | |
| 185 | // Figure out which index the basic block is in its function. |
| 186 | Function::iterator BBI = MF->begin(); |
| 187 | std::advance(BBI, std::distance(F->begin(), Function::iterator(BB))); |
| 188 | TranslatedBlocksToNotExtract.insert(BBI); |
| 189 | } |
| 190 | |
| 191 | // Now that we know which blocks to not extract, figure out which ones we WANT |
| 192 | // to extract. |
| 193 | std::vector<BasicBlock*> BlocksToExtract; |
| 194 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) |
| 195 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 196 | if (!TranslatedBlocksToNotExtract.count(BB)) |
| 197 | BlocksToExtract.push_back(BB); |
| 198 | |
| 199 | for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) |
| 200 | ExtractBasicBlock(BlocksToExtract[i]); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 201 | |
Chris Lattner | 8528672 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 202 | return !BlocksToExtract.empty(); |
| 203 | } |