Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 1 | //===- LoopExtractor.cpp - Extract each loop into a new function ----------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 692a47a | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 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 | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "loop-extract" |
Chris Lattner | 78a996a | 2004-03-14 02:37:16 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/IPO.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
Chris Lattner | e9235d2 | 2004-03-18 03:48:06 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
Reid Spencer | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 6c3e8c7 | 2004-03-14 04:01:06 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 30 | STATISTIC(NumExtracted, "Number of loops extracted"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 32 | namespace { |
Chris Lattner | a1672c1 | 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 | 557ab15 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 36 | struct VISIBILITY_HIDDEN LoopExtractor : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 37 | static char ID; // Pass identification, replacement for typeid |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 38 | unsigned NumLoops; |
| 39 | |
Dan Gohman | 34d442f | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 40 | explicit LoopExtractor(unsigned numLoops = ~0) |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 41 | : FunctionPass((intptr_t)&ID), NumLoops(numLoops) {} |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 43 | virtual bool runOnFunction(Function &F); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5bce0c8 | 2004-03-18 05:43:18 +0000 | [diff] [blame] | 46 | AU.addRequiredID(BreakCriticalEdgesID); |
| 47 | AU.addRequiredID(LoopSimplifyID); |
Owen Anderson | f095bf3 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 48 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 49 | AU.addRequired<LoopInfo>(); |
| 50 | } |
| 51 | }; |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 52 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 53 | char LoopExtractor::ID = 0; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 54 | RegisterPass<LoopExtractor> |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 55 | X("loop-extract", "Extract loops into new functions"); |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 56 | |
| 57 | /// SingleLoopExtractor - For bugpoint. |
| 58 | struct SingleLoopExtractor : public LoopExtractor { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 59 | static char ID; // Pass identification, replacement for typeid |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 60 | SingleLoopExtractor() : LoopExtractor(1) {} |
| 61 | }; |
| 62 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 63 | char SingleLoopExtractor::ID = 0; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 64 | RegisterPass<SingleLoopExtractor> |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 65 | Y("loop-extract-single", "Extract at most one loop into a new function"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 66 | } // End anonymous namespace |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 67 | |
Jeff Cohen | 677babc | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 68 | // createLoopExtractorPass - This pass extracts all natural loops from the |
| 69 | // program into a function if it can. |
| 70 | // |
Jeff Cohen | 3e62e7c | 2005-01-10 04:23:32 +0000 | [diff] [blame] | 71 | FunctionPass *llvm::createLoopExtractorPass() { return new LoopExtractor(); } |
Jeff Cohen | 677babc | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 72 | |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 73 | bool LoopExtractor::runOnFunction(Function &F) { |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 74 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 75 | |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 76 | // If this function has no loops, there is nothing to do. |
| 77 | if (LI.begin() == LI.end()) |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 78 | return false; |
| 79 | |
Owen Anderson | f095bf3 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 80 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Chris Lattner | e9235d2 | 2004-03-18 03:48:06 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 82 | // If there is more than one top-level loop in this function, extract all of |
| 83 | // the loops. |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 84 | bool Changed = false; |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 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; |
| 88 | --NumLoops; |
Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 89 | Changed |= ExtractLoop(DT, *i) != 0; |
Chris Lattner | e836935 | 2004-03-18 05:46:10 +0000 | [diff] [blame] | 90 | ++NumExtracted; |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 91 | } |
| 92 | } else { |
| 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; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 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) || |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 101 | !cast<BranchInst>(EntryTI)->isUnconditional() || |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 102 | EntryTI->getSuccessor(0) != TLL->getHeader()) |
| 103 | ShouldExtractLoop = true; |
| 104 | else { |
| 105 | // Check to see if any exits from the loop are more than just return |
| 106 | // blocks. |
Devang Patel | b5933bb | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 107 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | d72c3eb | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 108 | TLL->getExitBlocks(ExitBlocks); |
| 109 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 110 | if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) { |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 111 | ShouldExtractLoop = true; |
| 112 | break; |
| 113 | } |
| 114 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 116 | if (ShouldExtractLoop) { |
| 117 | if (NumLoops == 0) return Changed; |
| 118 | --NumLoops; |
Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 119 | Changed |= ExtractLoop(DT, TLL) != 0; |
Chris Lattner | e836935 | 2004-03-18 05:46:10 +0000 | [diff] [blame] | 120 | ++NumExtracted; |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 121 | } else { |
| 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; |
| 128 | --NumLoops; |
Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 129 | Changed |= ExtractLoop(DT, *i) != 0; |
Chris Lattner | e836935 | 2004-03-18 05:46:10 +0000 | [diff] [blame] | 130 | ++NumExtracted; |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 133 | } |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 134 | |
| 135 | return Changed; |
| 136 | } |
| 137 | |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 138 | // createSingleLoopExtractorPass - This pass extracts one natural loop from the |
| 139 | // program into a function if it can. This is used by bugpoint. |
| 140 | // |
Jeff Cohen | 3e62e7c | 2005-01-10 04:23:32 +0000 | [diff] [blame] | 141 | FunctionPass *llvm::createSingleLoopExtractorPass() { |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 142 | return new SingleLoopExtractor(); |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 143 | } |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 144 | |
| 145 | |
| 146 | namespace { |
| 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. |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 150 | class BlockExtractorPass : public ModulePass { |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 151 | std::vector<BasicBlock*> BlocksToNotExtract; |
| 152 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 153 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 34d442f | 2007-08-01 15:32:29 +0000 | [diff] [blame] | 154 | explicit BlockExtractorPass(std::vector<BasicBlock*> &B) |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 155 | : ModulePass((intptr_t)&ID), BlocksToNotExtract(B) {} |
| 156 | BlockExtractorPass() : ModulePass((intptr_t)&ID) {} |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 158 | bool runOnModule(Module &M); |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 159 | }; |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 160 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 161 | char BlockExtractorPass::ID = 0; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 162 | RegisterPass<BlockExtractorPass> |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 163 | XX("extract-blocks", "Extract Basic Blocks From Module (for bugpoint use)"); |
| 164 | } |
| 165 | |
| 166 | // createBlockExtractorPass - This pass extracts all blocks (except those |
| 167 | // specified in the argument list) from the functions in the module. |
| 168 | // |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 169 | ModulePass *llvm::createBlockExtractorPass(std::vector<BasicBlock*> &BTNE) { |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 170 | return new BlockExtractorPass(BTNE); |
| 171 | } |
| 172 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 173 | bool BlockExtractorPass::runOnModule(Module &M) { |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 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(); |
| 178 | |
| 179 | // Map the corresponding function in this module. |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 180 | Function *MF = M.getFunction(F->getName()); |
| 181 | assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?"); |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 182 | |
| 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); |
| 187 | } |
| 188 | |
| 189 | // Now that we know which blocks to not extract, figure out which ones we WANT |
| 190 | // to extract. |
| 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); |
| 196 | |
| 197 | for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) |
| 198 | ExtractBasicBlock(BlocksToExtract[i]); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 200 | return !BlocksToExtract.empty(); |
| 201 | } |