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