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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/Analysis/Dominators.h" |
| 21 | #include "llvm/Analysis/LoopPass.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame^] | 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/Module.h" |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 6c3e8c7 | 2004-03-14 04:01:06 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 29 | #include <fstream> |
| 30 | #include <set> |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 33 | STATISTIC(NumExtracted, "Number of loops extracted"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 1631bcb | 2006-12-19 22:09:18 +0000 | [diff] [blame] | 35 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 36 | struct LoopExtractor : public LoopPass { |
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) |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 41 | : LoopPass(ID), NumLoops(numLoops) { |
| 42 | initializeLoopExtractorPass(*PassRegistry::getPassRegistry()); |
| 43 | } |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 44 | |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 45 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 47 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 5bce0c8 | 2004-03-18 05:43:18 +0000 | [diff] [blame] | 48 | AU.addRequiredID(BreakCriticalEdgesID); |
| 49 | AU.addRequiredID(LoopSimplifyID); |
Owen Anderson | f095bf3 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 50 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 692a47a | 2004-03-14 02:34:07 +0000 | [diff] [blame] | 51 | } |
| 52 | }; |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 53 | } |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 54 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 55 | char LoopExtractor::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 56 | INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract", |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 57 | "Extract loops into new functions", false, false) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 58 | INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges) |
| 59 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 60 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 61 | INITIALIZE_PASS_END(LoopExtractor, "loop-extract", |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 62 | "Extract loops into new functions", false, false) |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 63 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 64 | namespace { |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 65 | /// SingleLoopExtractor - For bugpoint. |
| 66 | struct SingleLoopExtractor : public LoopExtractor { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 67 | static char ID; // Pass identification, replacement for typeid |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 68 | SingleLoopExtractor() : LoopExtractor(1) {} |
| 69 | }; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 70 | } // End anonymous namespace |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 71 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 72 | char SingleLoopExtractor::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 73 | INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 74 | "Extract at most one loop into a new function", false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 75 | |
Jeff Cohen | 677babc | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 76 | // createLoopExtractorPass - This pass extracts all natural loops from the |
| 77 | // program into a function if it can. |
| 78 | // |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 79 | Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); } |
Jeff Cohen | 677babc | 2005-01-08 17:21:40 +0000 | [diff] [blame] | 80 | |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 81 | bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) { |
| 82 | // Only visit top-level loops. |
| 83 | if (L->getParentLoop()) |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 84 | return false; |
| 85 | |
Dan Gohman | a83ac2d | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 86 | // If LoopSimplify form is not available, stay out of trouble. |
| 87 | if (!L->isLoopSimplifyForm()) |
| 88 | return false; |
| 89 | |
Owen Anderson | f095bf3 | 2007-04-07 05:31:27 +0000 | [diff] [blame] | 90 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 91 | bool Changed = false; |
Chris Lattner | e9235d2 | 2004-03-18 03:48:06 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 93 | // If there is more than one top-level loop in this function, extract all of |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 94 | // the loops. Otherwise there is exactly one top-level loop; in this case if |
| 95 | // this function is more than a minimal wrapper around the loop, extract |
| 96 | // the loop. |
| 97 | bool ShouldExtractLoop = false; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 99 | // Extract the loop if the entry block doesn't branch to the loop header. |
| 100 | TerminatorInst *EntryTI = |
| 101 | L->getHeader()->getParent()->getEntryBlock().getTerminator(); |
| 102 | if (!isa<BranchInst>(EntryTI) || |
| 103 | !cast<BranchInst>(EntryTI)->isUnconditional() || |
Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 104 | EntryTI->getSuccessor(0) != L->getHeader()) { |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 105 | ShouldExtractLoop = true; |
Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 106 | } else { |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 107 | // Check to see if any exits from the loop are more than just return |
Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 108 | // blocks. |
| 109 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 110 | L->getExitBlocks(ExitBlocks); |
| 111 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 112 | if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) { |
| 113 | ShouldExtractLoop = true; |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if (ShouldExtractLoop) { |
| 119 | // We must omit landing pads. Landing pads must accompany the invoke |
| 120 | // instruction. But this would result in a loop in the extracted |
Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 121 | // function. An infinite cycle occurs when it tries to extract that loop as |
| 122 | // well. |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 123 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 124 | L->getExitBlocks(ExitBlocks); |
Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 125 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
Bill Wendling | 0058520 | 2011-09-20 22:23:09 +0000 | [diff] [blame] | 126 | if (ExitBlocks[i]->isLandingPad()) { |
| 127 | ShouldExtractLoop = false; |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 128 | break; |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 129 | } |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 130 | } |
Bill Wendling | 04289fc | 2011-09-20 22:27:16 +0000 | [diff] [blame] | 131 | |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 132 | if (ShouldExtractLoop) { |
| 133 | if (NumLoops == 0) return Changed; |
| 134 | --NumLoops; |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 135 | CodeExtractor Extractor(DT, *L); |
| 136 | if (Extractor.extractCodeRegion() != 0) { |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 137 | Changed = true; |
| 138 | // After extraction, the loop is replaced by a function call, so |
| 139 | // we shouldn't try to run any more loop passes on it. |
| 140 | LPM.deleteLoopFromQueue(L); |
Chris Lattner | 2f155d8 | 2004-03-15 00:02:02 +0000 | [diff] [blame] | 141 | } |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 142 | ++NumExtracted; |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 143 | } |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 144 | |
| 145 | return Changed; |
| 146 | } |
| 147 | |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 148 | // createSingleLoopExtractorPass - This pass extracts one natural loop from the |
| 149 | // program into a function if it can. This is used by bugpoint. |
| 150 | // |
Dan Gohman | 9a7320c | 2009-09-28 14:37:51 +0000 | [diff] [blame] | 151 | Pass *llvm::createSingleLoopExtractorPass() { |
Chris Lattner | a1672c1 | 2004-03-14 20:01:36 +0000 | [diff] [blame] | 152 | return new SingleLoopExtractor(); |
Misha Brukman | 03a1134 | 2004-02-28 03:33:01 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 154 | |
| 155 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 156 | // BlockFile - A file which contains a list of blocks that should not be |
| 157 | // extracted. |
| 158 | static cl::opt<std::string> |
| 159 | BlockFile("extract-blocks-file", cl::value_desc("filename"), |
| 160 | cl::desc("A file containing list of basic blocks to not extract"), |
| 161 | cl::Hidden); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 162 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 163 | namespace { |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 164 | /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks |
| 165 | /// from the module into their own functions except for those specified by the |
| 166 | /// BlocksToNotExtract list. |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 167 | class BlockExtractorPass : public ModulePass { |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 168 | void LoadFile(const char *Filename); |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 169 | void SplitLandingPadPreds(Function *F); |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 171 | std::vector<BasicBlock*> BlocksToNotExtract; |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 172 | std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName; |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 173 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 174 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 175 | BlockExtractorPass() : ModulePass(ID) { |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 176 | if (!BlockFile.empty()) |
| 177 | LoadFile(BlockFile.c_str()); |
| 178 | } |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 180 | bool runOnModule(Module &M); |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 181 | }; |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 184 | char BlockExtractorPass::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 185 | INITIALIZE_PASS(BlockExtractorPass, "extract-blocks", |
| 186 | "Extract Basic Blocks From Module (for bugpoint use)", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 187 | false, false) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 188 | |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 189 | // createBlockExtractorPass - This pass extracts all blocks (except those |
| 190 | // specified in the argument list) from the functions in the module. |
| 191 | // |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 192 | ModulePass *llvm::createBlockExtractorPass() { |
Rafael Espindola | 40f1883 | 2010-07-31 00:32:17 +0000 | [diff] [blame] | 193 | return new BlockExtractorPass(); |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 196 | void BlockExtractorPass::LoadFile(const char *Filename) { |
| 197 | // Load the BlockFile... |
| 198 | std::ifstream In(Filename); |
| 199 | if (!In.good()) { |
Chris Lattner | 4883d90 | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 200 | errs() << "WARNING: BlockExtractor couldn't load file '" << Filename |
| 201 | << "'!\n"; |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | while (In) { |
| 205 | std::string FunctionName, BlockName; |
| 206 | In >> FunctionName; |
| 207 | In >> BlockName; |
| 208 | if (!BlockName.empty()) |
| 209 | BlocksToNotExtractByName.push_back( |
| 210 | std::make_pair(FunctionName, BlockName)); |
| 211 | } |
| 212 | } |
| 213 | |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 214 | /// SplitLandingPadPreds - The landing pad needs to be extracted with the invoke |
| 215 | /// instruction. The critical edge breaker will refuse to break critical edges |
| 216 | /// to a landing pad. So do them here. After this method runs, all landing pads |
| 217 | /// should have only one predecessor. |
| 218 | void BlockExtractorPass::SplitLandingPadPreds(Function *F) { |
| 219 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 220 | InvokeInst *II = dyn_cast<InvokeInst>(I); |
| 221 | if (!II) continue; |
| 222 | BasicBlock *Parent = II->getParent(); |
| 223 | BasicBlock *LPad = II->getUnwindDest(); |
| 224 | |
| 225 | // Look through the landing pad's predecessors. If one of them ends in an |
| 226 | // 'invoke', then we want to split the landing pad. |
| 227 | bool Split = false; |
| 228 | for (pred_iterator |
| 229 | PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) { |
| 230 | BasicBlock *BB = *PI; |
| 231 | if (BB->isLandingPad() && BB != Parent && |
| 232 | isa<InvokeInst>(Parent->getTerminator())) { |
| 233 | Split = true; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (!Split) continue; |
| 239 | |
| 240 | SmallVector<BasicBlock*, 2> NewBBs; |
| 241 | SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", 0, NewBBs); |
| 242 | } |
| 243 | } |
| 244 | |
Chris Lattner | 4f2cf03 | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 245 | bool BlockExtractorPass::runOnModule(Module &M) { |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 246 | std::set<BasicBlock*> TranslatedBlocksToNotExtract; |
| 247 | for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) { |
| 248 | BasicBlock *BB = BlocksToNotExtract[i]; |
| 249 | Function *F = BB->getParent(); |
| 250 | |
| 251 | // Map the corresponding function in this module. |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 252 | Function *MF = M.getFunction(F->getName()); |
| 253 | assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?"); |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 254 | |
| 255 | // Figure out which index the basic block is in its function. |
| 256 | Function::iterator BBI = MF->begin(); |
| 257 | std::advance(BBI, std::distance(F->begin(), Function::iterator(BB))); |
| 258 | TranslatedBlocksToNotExtract.insert(BBI); |
| 259 | } |
| 260 | |
Nick Lewycky | c624302 | 2007-11-14 06:47:06 +0000 | [diff] [blame] | 261 | while (!BlocksToNotExtractByName.empty()) { |
| 262 | // There's no way to find BBs by name without looking at every BB inside |
| 263 | // every Function. Fortunately, this is always empty except when used by |
| 264 | // bugpoint in which case correctness is more important than performance. |
| 265 | |
| 266 | std::string &FuncName = BlocksToNotExtractByName.back().first; |
| 267 | std::string &BlockName = BlocksToNotExtractByName.back().second; |
| 268 | |
| 269 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) { |
| 270 | Function &F = *FI; |
| 271 | if (F.getName() != FuncName) continue; |
| 272 | |
| 273 | for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { |
| 274 | BasicBlock &BB = *BI; |
| 275 | if (BB.getName() != BlockName) continue; |
| 276 | |
| 277 | TranslatedBlocksToNotExtract.insert(BI); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | BlocksToNotExtractByName.pop_back(); |
| 282 | } |
| 283 | |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 284 | // Now that we know which blocks to not extract, figure out which ones we WANT |
| 285 | // to extract. |
| 286 | std::vector<BasicBlock*> BlocksToExtract; |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 287 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 288 | SplitLandingPadPreds(&*F); |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 289 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 290 | if (!TranslatedBlocksToNotExtract.count(BB)) |
| 291 | BlocksToExtract.push_back(BB); |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 292 | } |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 293 | |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 294 | for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) { |
| 295 | SmallVector<BasicBlock*, 2> BlocksToExtractVec; |
| 296 | BlocksToExtractVec.push_back(BlocksToExtract[i]); |
Bill Wendling | 3d48f59 | 2011-09-20 20:20:50 +0000 | [diff] [blame] | 297 | if (const InvokeInst *II = |
| 298 | dyn_cast<InvokeInst>(BlocksToExtract[i]->getTerminator())) |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 299 | BlocksToExtractVec.push_back(II->getUnwindDest()); |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 300 | CodeExtractor(BlocksToExtractVec).extractCodeRegion(); |
Bill Wendling | c1da6ea | 2011-09-20 19:10:24 +0000 | [diff] [blame] | 301 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 302 | |
Chris Lattner | 7386e63 | 2004-08-13 03:05:17 +0000 | [diff] [blame] | 303 | return !BlocksToExtract.empty(); |
| 304 | } |