Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1 | //===- CodeExtractor.cpp - Pull code region into a new function -----------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +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 | // |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the interface to tear out a code region, such as an |
| 11 | // individual loop or a parallel section, into a new function, replacing it with |
| 12 | // a call to the new function. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Jakub Staszak | f23980a | 2013-02-09 01:04:28 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
| 21 | #include "llvm/Analysis/RegionInfo.h" |
| 22 | #include "llvm/Analysis/RegionIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| 24 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Instructions.h" |
| 27 | #include "llvm/IR/Intrinsics.h" |
| 28 | #include "llvm/IR/LLVMContext.h" |
| 29 | #include "llvm/IR/Module.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Verifier.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
| 33 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 37 | #include <algorithm> |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 38 | #include <set> |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "code-extractor" |
| 42 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 43 | // Provide a command-line option to aggregate function arguments into a struct |
Misha Brukman | 234b44a | 2008-12-13 05:21:37 +0000 | [diff] [blame] | 44 | // for functions produced by the code extractor. This is useful when converting |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 45 | // extracted functions to pthread-based code, as only one argument (void*) can |
| 46 | // be passed in to pthread_create(). |
| 47 | static cl::opt<bool> |
| 48 | AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, |
| 49 | cl::desc("Aggregate arguments to code-extracted functions")); |
| 50 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 51 | /// \brief Test whether a block is valid for extraction. |
| 52 | static bool isBlockValidForExtraction(const BasicBlock &BB) { |
| 53 | // Landing pads must be in the function where they were inserted for cleanup. |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 54 | if (BB.isEHPad()) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 55 | return false; |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 56 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 57 | // Don't hoist code containing allocas, invokes, or vastarts. |
| 58 | for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |
| 59 | if (isa<AllocaInst>(I) || isa<InvokeInst>(I)) |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 60 | return false; |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 61 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 62 | if (const Function *F = CI->getCalledFunction()) |
| 63 | if (F->getIntrinsicID() == Intrinsic::vastart) |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | /// \brief Build a set of blocks to extract if the input blocks are viable. |
Chandler Carruth | 6781821 | 2012-05-04 21:33:30 +0000 | [diff] [blame] | 71 | template <typename IteratorT> |
| 72 | static SetVector<BasicBlock *> buildExtractionBlockSet(IteratorT BBBegin, |
| 73 | IteratorT BBEnd) { |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 74 | SetVector<BasicBlock *> Result; |
| 75 | |
Chandler Carruth | 6781821 | 2012-05-04 21:33:30 +0000 | [diff] [blame] | 76 | assert(BBBegin != BBEnd); |
Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 77 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 78 | // Loop over the blocks, adding them to our set-vector, and aborting with an |
| 79 | // empty set if we encounter invalid blocks. |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 80 | do { |
| 81 | if (!Result.insert(*BBBegin)) |
Chandler Carruth | 44e1391 | 2012-05-04 11:17:06 +0000 | [diff] [blame] | 82 | llvm_unreachable("Repeated basic blocks in extraction input"); |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 83 | |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 84 | if (!isBlockValidForExtraction(**BBBegin)) { |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 85 | Result.clear(); |
Chandler Carruth | 0a57055 | 2012-05-04 11:14:19 +0000 | [diff] [blame] | 86 | return Result; |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 87 | } |
Benjamin Kramer | 4fed928 | 2016-05-27 12:30:51 +0000 | [diff] [blame] | 88 | } while (++BBBegin != BBEnd); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 89 | |
Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 90 | #ifndef NDEBUG |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 91 | for (SetVector<BasicBlock *>::iterator I = std::next(Result.begin()), |
Chandler Carruth | 6781821 | 2012-05-04 21:33:30 +0000 | [diff] [blame] | 92 | E = Result.end(); |
Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 93 | I != E; ++I) |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 94 | for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I); |
| 95 | PI != PE; ++PI) |
| 96 | assert(Result.count(*PI) && |
Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 97 | "No blocks in this region may have entries from outside the region" |
| 98 | " except for the first block!"); |
| 99 | #endif |
| 100 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 101 | return Result; |
| 102 | } |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 103 | |
Chandler Carruth | 6781821 | 2012-05-04 21:33:30 +0000 | [diff] [blame] | 104 | /// \brief Helper to call buildExtractionBlockSet with an ArrayRef. |
| 105 | static SetVector<BasicBlock *> |
| 106 | buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs) { |
| 107 | return buildExtractionBlockSet(BBs.begin(), BBs.end()); |
| 108 | } |
| 109 | |
| 110 | /// \brief Helper to call buildExtractionBlockSet with a RegionNode. |
| 111 | static SetVector<BasicBlock *> |
| 112 | buildExtractionBlockSet(const RegionNode &RN) { |
| 113 | if (!RN.isSubRegion()) |
| 114 | // Just a single BasicBlock. |
| 115 | return buildExtractionBlockSet(RN.getNodeAs<BasicBlock>()); |
| 116 | |
| 117 | const Region &R = *RN.getNodeAs<Region>(); |
| 118 | |
| 119 | return buildExtractionBlockSet(R.block_begin(), R.block_end()); |
| 120 | } |
| 121 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 122 | CodeExtractor::CodeExtractor(BasicBlock *BB, bool AggregateArgs) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 123 | : DT(nullptr), AggregateArgs(AggregateArgs||AggregateArgsOpt), |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 124 | Blocks(buildExtractionBlockSet(BB)), NumExitBlocks(~0U) {} |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 125 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 126 | CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT, |
| 127 | bool AggregateArgs) |
| 128 | : DT(DT), AggregateArgs(AggregateArgs||AggregateArgsOpt), |
| 129 | Blocks(buildExtractionBlockSet(BBs)), NumExitBlocks(~0U) {} |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 130 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 131 | CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs) |
| 132 | : DT(&DT), AggregateArgs(AggregateArgs||AggregateArgsOpt), |
| 133 | Blocks(buildExtractionBlockSet(L.getBlocks())), NumExitBlocks(~0U) {} |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 134 | |
Chandler Carruth | 6781821 | 2012-05-04 21:33:30 +0000 | [diff] [blame] | 135 | CodeExtractor::CodeExtractor(DominatorTree &DT, const RegionNode &RN, |
| 136 | bool AggregateArgs) |
| 137 | : DT(&DT), AggregateArgs(AggregateArgs||AggregateArgsOpt), |
| 138 | Blocks(buildExtractionBlockSet(RN)), NumExitBlocks(~0U) {} |
| 139 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 140 | /// definedInRegion - Return true if the specified value is defined in the |
| 141 | /// extracted region. |
| 142 | static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) { |
| 143 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 144 | if (Blocks.count(I->getParent())) |
| 145 | return true; |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | /// definedInCaller - Return true if the specified value is defined in the |
| 150 | /// function being code extracted, but not in the region being extracted. |
| 151 | /// These values must be passed in as live-ins to the function. |
| 152 | static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) { |
| 153 | if (isa<Argument>(V)) return true; |
| 154 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 155 | if (!Blocks.count(I->getParent())) |
| 156 | return true; |
| 157 | return false; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 160 | void CodeExtractor::findInputsOutputs(ValueSet &Inputs, |
| 161 | ValueSet &Outputs) const { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 162 | for (BasicBlock *BB : Blocks) { |
Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 163 | // If a used value is defined outside the region, it's an input. If an |
| 164 | // instruction is used outside the region, it's an output. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 165 | for (Instruction &II : *BB) { |
| 166 | for (User::op_iterator OI = II.op_begin(), OE = II.op_end(); OI != OE; |
| 167 | ++OI) |
Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 168 | if (definedInCaller(Blocks, *OI)) |
| 169 | Inputs.insert(*OI); |
| 170 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 171 | for (User *U : II.users()) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 172 | if (!definedInRegion(Blocks, U)) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 173 | Outputs.insert(&II); |
Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 180 | /// severSplitPHINodes - If a PHI node has multiple inputs from outside of the |
| 181 | /// region, we need to split the entry block of the region so that the PHI node |
| 182 | /// is easier to deal with. |
| 183 | void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) { |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 184 | unsigned NumPredsFromRegion = 0; |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 185 | unsigned NumPredsOutsideRegion = 0; |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 186 | |
Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 187 | if (Header != &Header->getParent()->getEntryBlock()) { |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 188 | PHINode *PN = dyn_cast<PHINode>(Header->begin()); |
| 189 | if (!PN) return; // No PHI nodes. |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 191 | // If the header node contains any PHI nodes, check to see if there is more |
| 192 | // than one entry from outside the region. If so, we need to sever the |
| 193 | // header block into two. |
| 194 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 195 | if (Blocks.count(PN->getIncomingBlock(i))) |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 196 | ++NumPredsFromRegion; |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 197 | else |
| 198 | ++NumPredsOutsideRegion; |
| 199 | |
| 200 | // If there is one (or fewer) predecessor from outside the region, we don't |
| 201 | // need to do anything special. |
| 202 | if (NumPredsOutsideRegion <= 1) return; |
| 203 | } |
| 204 | |
| 205 | // Otherwise, we need to split the header block into two pieces: one |
| 206 | // containing PHI nodes merging values from outside of the region, and a |
| 207 | // second that contains all of the code for the block and merges back any |
| 208 | // incoming values from inside of the region. |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 209 | BasicBlock::iterator AfterPHIs = Header->getFirstNonPHI()->getIterator(); |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 210 | BasicBlock *NewBB = Header->splitBasicBlock(AfterPHIs, |
| 211 | Header->getName()+".ce"); |
| 212 | |
| 213 | // We only want to code extract the second block now, and it becomes the new |
| 214 | // header of the region. |
| 215 | BasicBlock *OldPred = Header; |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 216 | Blocks.remove(OldPred); |
| 217 | Blocks.insert(NewBB); |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 218 | Header = NewBB; |
| 219 | |
| 220 | // Okay, update dominator sets. The blocks that dominate the new one are the |
| 221 | // blocks that dominate TIBB plus the new block itself. |
Devang Patel | d5258a23 | 2007-06-21 17:23:45 +0000 | [diff] [blame] | 222 | if (DT) |
| 223 | DT->splitBlock(NewBB); |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 224 | |
| 225 | // Okay, now we need to adjust the PHI nodes and any branches from within the |
| 226 | // region to go to the new header block instead of the old header block. |
Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 227 | if (NumPredsFromRegion) { |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 228 | PHINode *PN = cast<PHINode>(OldPred->begin()); |
| 229 | // Loop over all of the predecessors of OldPred that are in the region, |
| 230 | // changing them to branch to NewBB instead. |
| 231 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 232 | if (Blocks.count(PN->getIncomingBlock(i))) { |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 233 | TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator(); |
| 234 | TI->replaceUsesOfWith(OldPred, NewBB); |
| 235 | } |
| 236 | |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 237 | // Okay, everything within the region is now branching to the right block, we |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 238 | // just have to update the PHI nodes now, inserting PHI nodes into NewBB. |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 239 | for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) { |
| 240 | PHINode *PN = cast<PHINode>(AfterPHIs); |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 241 | // Create a new PHI node in the new region, which has an incoming value |
| 242 | // from OldPred of PN. |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 243 | PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion, |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 244 | PN->getName() + ".ce", &NewBB->front()); |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 245 | NewPN->addIncoming(PN, OldPred); |
| 246 | |
| 247 | // Loop over all of the incoming value in PN, moving them to NewPN if they |
| 248 | // are from the extracted region. |
| 249 | for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) { |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 250 | if (Blocks.count(PN->getIncomingBlock(i))) { |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 251 | NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i)); |
| 252 | PN->removeIncomingValue(i); |
| 253 | --i; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 258 | } |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 260 | void CodeExtractor::splitReturnBlocks() { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 261 | for (BasicBlock *Block : Blocks) |
| 262 | if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) { |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 263 | BasicBlock *New = |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 264 | Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret"); |
Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 265 | if (DT) { |
Gabor Greif | 2f5f696 | 2010-09-10 22:25:58 +0000 | [diff] [blame] | 266 | // Old dominates New. New node dominates all other nodes dominated |
| 267 | // by Old. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 268 | DomTreeNode *OldNode = DT->getNode(Block); |
| 269 | SmallVector<DomTreeNode *, 8> Children(OldNode->begin(), |
| 270 | OldNode->end()); |
Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 271 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 272 | DomTreeNode *NewNode = DT->addNewBlock(New, Block); |
Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 273 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 274 | for (DomTreeNode *I : Children) |
| 275 | DT->changeImmediateDominator(I, NewNode); |
Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 280 | /// constructFunction - make a function based on inputs and outputs, as follows: |
| 281 | /// f(in0, ..., inN, out0, ..., outN) |
| 282 | /// |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 283 | Function *CodeExtractor::constructFunction(const ValueSet &inputs, |
| 284 | const ValueSet &outputs, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 285 | BasicBlock *header, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 286 | BasicBlock *newRootNode, |
| 287 | BasicBlock *newHeader, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 288 | Function *oldFunction, |
| 289 | Module *M) { |
David Greene | 0ad6dce | 2010-01-05 01:26:44 +0000 | [diff] [blame] | 290 | DEBUG(dbgs() << "inputs: " << inputs.size() << "\n"); |
| 291 | DEBUG(dbgs() << "outputs: " << outputs.size() << "\n"); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 292 | |
| 293 | // This function returns unsigned, outputs will go back by reference. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 294 | switch (NumExitBlocks) { |
| 295 | case 0: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 296 | case 1: RetTy = Type::getVoidTy(header->getContext()); break; |
| 297 | case 2: RetTy = Type::getInt1Ty(header->getContext()); break; |
| 298 | default: RetTy = Type::getInt16Ty(header->getContext()); break; |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 301 | std::vector<Type*> paramTy; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 302 | |
| 303 | // Add the types of the input values to the function's argument list |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 304 | for (Value *value : inputs) { |
David Greene | 0ad6dce | 2010-01-05 01:26:44 +0000 | [diff] [blame] | 305 | DEBUG(dbgs() << "value used in func: " << *value << "\n"); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 306 | paramTy.push_back(value->getType()); |
| 307 | } |
| 308 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 309 | // Add the types of the output values to the function's argument list. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 310 | for (Value *output : outputs) { |
| 311 | DEBUG(dbgs() << "instr used in func: " << *output << "\n"); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 312 | if (AggregateArgs) |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 313 | paramTy.push_back(output->getType()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 314 | else |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 315 | paramTy.push_back(PointerType::getUnqual(output->getType())); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Benjamin Kramer | 706e4883 | 2016-06-26 13:39:33 +0000 | [diff] [blame^] | 318 | DEBUG({ |
| 319 | dbgs() << "Function type: " << *RetTy << " f("; |
| 320 | for (Type *i : paramTy) |
| 321 | dbgs() << *i << ", "; |
| 322 | dbgs() << ")\n"; |
| 323 | }); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 324 | |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 325 | StructType *StructTy; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 326 | if (AggregateArgs && (inputs.size() + outputs.size() > 0)) { |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 327 | StructTy = StructType::get(M->getContext(), paramTy); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 328 | paramTy.clear(); |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 329 | paramTy.push_back(PointerType::getUnqual(StructTy)); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 331 | FunctionType *funcType = |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 332 | FunctionType::get(RetTy, paramTy, false); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 333 | |
| 334 | // Create the new function |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 335 | Function *newFunction = Function::Create(funcType, |
| 336 | GlobalValue::InternalLinkage, |
| 337 | oldFunction->getName() + "_" + |
| 338 | header->getName(), M); |
Chris Lattner | 4caf5eb | 2008-12-18 05:52:56 +0000 | [diff] [blame] | 339 | // If the old function is no-throw, so is the new one. |
| 340 | if (oldFunction->doesNotThrow()) |
Bill Wendling | f319e99 | 2012-10-10 03:12:49 +0000 | [diff] [blame] | 341 | newFunction->setDoesNotThrow(); |
Chris Lattner | 4caf5eb | 2008-12-18 05:52:56 +0000 | [diff] [blame] | 342 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 343 | newFunction->getBasicBlockList().push_back(newRootNode); |
| 344 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 345 | // Create an iterator to name all of the arguments we inserted. |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 346 | Function::arg_iterator AI = newFunction->arg_begin(); |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 347 | |
| 348 | // Rewrite all users of the inputs in the extracted region to use the |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 349 | // arguments (or appropriate addressing into struct) instead. |
| 350 | for (unsigned i = 0, e = inputs.size(); i != e; ++i) { |
| 351 | Value *RewriteVal; |
| 352 | if (AggregateArgs) { |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 353 | Value *Idx[2]; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 354 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext())); |
| 355 | Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 356 | TerminatorInst *TI = newFunction->begin()->getTerminator(); |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 357 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 358 | StructTy, &*AI, Idx, "gep_" + inputs[i]->getName(), TI); |
Daniel Dunbar | 12368685 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 359 | RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 360 | } else |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 361 | RewriteVal = &*AI++; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 362 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 363 | std::vector<User*> Users(inputs[i]->user_begin(), inputs[i]->user_end()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 364 | for (User *use : Users) |
| 365 | if (Instruction *inst = dyn_cast<Instruction>(use)) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 366 | if (Blocks.count(inst->getParent())) |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 367 | inst->replaceUsesOfWith(inputs[i], RewriteVal); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 370 | // Set names for input and output arguments. |
| 371 | if (!AggregateArgs) { |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 372 | AI = newFunction->arg_begin(); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 373 | for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI) |
Owen Anderson | 7629b71 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 374 | AI->setName(inputs[i]->getName()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 375 | for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI) |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 376 | AI->setName(outputs[i]->getName()+".out"); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 378 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 379 | // Rewrite branches to basic blocks outside of the loop to new dummy blocks |
| 380 | // within the new function. This must be done before we lose track of which |
| 381 | // blocks were originally in the code region. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 382 | std::vector<User*> Users(header->user_begin(), header->user_end()); |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 383 | for (unsigned i = 0, e = Users.size(); i != e; ++i) |
| 384 | // The BasicBlock which contains the branch is not in the region |
| 385 | // modify the branch target to a new block |
| 386 | if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i])) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 387 | if (!Blocks.count(TI->getParent()) && |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 388 | TI->getParent()->getParent() == oldFunction) |
| 389 | TI->replaceUsesOfWith(header, newHeader); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 390 | |
| 391 | return newFunction; |
| 392 | } |
| 393 | |
Owen Anderson | 4e9ac2a | 2009-08-25 17:42:07 +0000 | [diff] [blame] | 394 | /// FindPhiPredForUseInBlock - Given a value and a basic block, find a PHI |
| 395 | /// that uses the value within the basic block, and return the predecessor |
| 396 | /// block associated with that use, or return 0 if none is found. |
Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 397 | static BasicBlock* FindPhiPredForUseInBlock(Value* Used, BasicBlock* BB) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 398 | for (Use &U : Used->uses()) { |
| 399 | PHINode *P = dyn_cast<PHINode>(U.getUser()); |
Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 400 | if (P && P->getParent() == BB) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 401 | return P->getIncomingBlock(U); |
Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 402 | } |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 403 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 404 | return nullptr; |
Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 407 | /// emitCallAndSwitchStatement - This method sets up the caller side by adding |
| 408 | /// the call instruction, splitting any PHI nodes in the header block as |
| 409 | /// necessary. |
| 410 | void CodeExtractor:: |
| 411 | emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer, |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 412 | ValueSet &inputs, ValueSet &outputs) { |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 413 | // Emit a call to the new function, passing in: *pointer to struct (if |
| 414 | // aggregating parameters), or plan inputs and allocated memory for outputs |
Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 415 | std::vector<Value*> params, StructValues, ReloadOutputs, Reloads; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 416 | |
| 417 | LLVMContext &Context = newFunction->getContext(); |
Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 418 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 419 | // Add inputs as params, or to be filled into the struct |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 420 | for (Value *input : inputs) |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 421 | if (AggregateArgs) |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 422 | StructValues.push_back(input); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 423 | else |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 424 | params.push_back(input); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 425 | |
| 426 | // Create allocas for the outputs |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 427 | for (Value *output : outputs) { |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 428 | if (AggregateArgs) { |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 429 | StructValues.push_back(output); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 430 | } else { |
| 431 | AllocaInst *alloca = |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 432 | new AllocaInst(output->getType(), nullptr, output->getName() + ".loc", |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 433 | &codeReplacer->getParent()->front().front()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 434 | ReloadOutputs.push_back(alloca); |
| 435 | params.push_back(alloca); |
| 436 | } |
| 437 | } |
| 438 | |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 439 | StructType *StructArgTy = nullptr; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 440 | AllocaInst *Struct = nullptr; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 441 | if (AggregateArgs && (inputs.size() + outputs.size() > 0)) { |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 442 | std::vector<Type*> ArgTypes; |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 443 | for (ValueSet::iterator v = StructValues.begin(), |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 444 | ve = StructValues.end(); v != ve; ++v) |
| 445 | ArgTypes.push_back((*v)->getType()); |
| 446 | |
| 447 | // Allocate a struct at the beginning of this function |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 448 | StructArgTy = StructType::get(newFunction->getContext(), ArgTypes); |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 449 | Struct = new AllocaInst(StructArgTy, nullptr, "structArg", |
| 450 | &codeReplacer->getParent()->front().front()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 451 | params.push_back(Struct); |
| 452 | |
| 453 | for (unsigned i = 0, e = inputs.size(); i != e; ++i) { |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 454 | Value *Idx[2]; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 455 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 456 | Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i); |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 457 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| 458 | StructArgTy, Struct, Idx, "gep_" + StructValues[i]->getName()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 459 | codeReplacer->getInstList().push_back(GEP); |
| 460 | StoreInst *SI = new StoreInst(StructValues[i], GEP); |
| 461 | codeReplacer->getInstList().push_back(SI); |
| 462 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 463 | } |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 464 | |
| 465 | // Emit the call to the function |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 466 | CallInst *call = CallInst::Create(newFunction, params, |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 467 | NumExitBlocks > 1 ? "targetBlock" : ""); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 468 | codeReplacer->getInstList().push_back(call); |
| 469 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 470 | Function::arg_iterator OutputArgBegin = newFunction->arg_begin(); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 471 | unsigned FirstOut = inputs.size(); |
| 472 | if (!AggregateArgs) |
| 473 | std::advance(OutputArgBegin, inputs.size()); |
| 474 | |
| 475 | // Reload the outputs passed in by reference |
| 476 | for (unsigned i = 0, e = outputs.size(); i != e; ++i) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 477 | Value *Output = nullptr; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 478 | if (AggregateArgs) { |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 479 | Value *Idx[2]; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 480 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 481 | Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i); |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 482 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| 483 | StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 484 | codeReplacer->getInstList().push_back(GEP); |
| 485 | Output = GEP; |
| 486 | } else { |
| 487 | Output = ReloadOutputs[i]; |
| 488 | } |
| 489 | LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload"); |
Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 490 | Reloads.push_back(load); |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 491 | codeReplacer->getInstList().push_back(load); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 492 | std::vector<User*> Users(outputs[i]->user_begin(), outputs[i]->user_end()); |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 493 | for (unsigned u = 0, e = Users.size(); u != e; ++u) { |
| 494 | Instruction *inst = cast<Instruction>(Users[u]); |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 495 | if (!Blocks.count(inst->getParent())) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 496 | inst->replaceUsesOfWith(outputs[i], load); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 499 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 500 | // Now we can emit a switch statement using the call as a value. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 501 | SwitchInst *TheSwitch = |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 502 | SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)), |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 503 | codeReplacer, 0, codeReplacer); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 504 | |
| 505 | // Since there may be multiple exits from the original region, make the new |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 506 | // function return an unsigned, switch on that number. This loop iterates |
| 507 | // over all of the blocks in the extracted region, updating any terminator |
| 508 | // instructions in the to-be-extracted region that branch to blocks that are |
| 509 | // not in the region to be extracted. |
| 510 | std::map<BasicBlock*, BasicBlock*> ExitBlockMap; |
| 511 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 512 | unsigned switchVal = 0; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 513 | for (BasicBlock *Block : Blocks) { |
| 514 | TerminatorInst *TI = Block->getTerminator(); |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 515 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 516 | if (!Blocks.count(TI->getSuccessor(i))) { |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 517 | BasicBlock *OldTarget = TI->getSuccessor(i); |
| 518 | // add a new basic block which returns the appropriate value |
| 519 | BasicBlock *&NewTarget = ExitBlockMap[OldTarget]; |
| 520 | if (!NewTarget) { |
| 521 | // If we don't already have an exit stub for this non-extracted |
| 522 | // destination, create one now! |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 523 | NewTarget = BasicBlock::Create(Context, |
| 524 | OldTarget->getName() + ".exitStub", |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 525 | newFunction); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 526 | unsigned SuccNum = switchVal++; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 527 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 528 | Value *brVal = nullptr; |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 529 | switch (NumExitBlocks) { |
| 530 | case 0: |
| 531 | case 1: break; // No value needed. |
| 532 | case 2: // Conditional branch, return a bool |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 533 | brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 534 | break; |
| 535 | default: |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 536 | brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 537 | break; |
| 538 | } |
| 539 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 540 | ReturnInst *NTRet = ReturnInst::Create(Context, brVal, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 541 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 542 | // Update the switch instruction. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 543 | TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context), |
| 544 | SuccNum), |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 545 | OldTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 546 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 547 | // Restore values just before we exit |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 548 | Function::arg_iterator OAI = OutputArgBegin; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 549 | for (unsigned out = 0, e = outputs.size(); out != e; ++out) { |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 550 | // For an invoke, the normal destination is the only one that is |
| 551 | // dominated by the result of the invocation |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 552 | BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent(); |
Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 553 | |
| 554 | bool DominatesDef = true; |
| 555 | |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 556 | BasicBlock *NormalDest = nullptr; |
| 557 | if (auto *Invoke = dyn_cast<InvokeInst>(outputs[out])) |
| 558 | NormalDest = Invoke->getNormalDest(); |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 559 | |
| 560 | if (NormalDest) { |
| 561 | DefBlock = NormalDest; |
Chris Lattner | 5bcca60 | 2004-11-12 23:50:44 +0000 | [diff] [blame] | 562 | |
| 563 | // Make sure we are looking at the original successor block, not |
| 564 | // at a newly inserted exit block, which won't be in the dominator |
| 565 | // info. |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 566 | for (const auto &I : ExitBlockMap) |
| 567 | if (DefBlock == I.second) { |
| 568 | DefBlock = I.first; |
Chris Lattner | 5bcca60 | 2004-11-12 23:50:44 +0000 | [diff] [blame] | 569 | break; |
| 570 | } |
Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 571 | |
| 572 | // In the extract block case, if the block we are extracting ends |
| 573 | // with an invoke instruction, make sure that we don't emit a |
| 574 | // store of the invoke value for the unwind block. |
Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 575 | if (!DT && DefBlock != OldTarget) |
Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 576 | DominatesDef = false; |
Chris Lattner | 5bcca60 | 2004-11-12 23:50:44 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 579 | if (DT) { |
Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 580 | DominatesDef = DT->dominates(DefBlock, OldTarget); |
Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 581 | |
| 582 | // If the output value is used by a phi in the target block, |
| 583 | // then we need to test for dominance of the phi's predecessor |
| 584 | // instead. Unfortunately, this a little complicated since we |
| 585 | // have already rewritten uses of the value to uses of the reload. |
Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 586 | BasicBlock* pred = FindPhiPredForUseInBlock(Reloads[out], |
| 587 | OldTarget); |
| 588 | if (pred && DT && DT->dominates(DefBlock, pred)) |
| 589 | DominatesDef = true; |
Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 590 | } |
Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 591 | |
| 592 | if (DominatesDef) { |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 593 | if (AggregateArgs) { |
David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 594 | Value *Idx[2]; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 595 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 596 | Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), |
| 597 | FirstOut+out); |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 598 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 599 | StructArgTy, &*OAI, Idx, "gep_" + outputs[out]->getName(), |
David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 600 | NTRet); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 601 | new StoreInst(outputs[out], GEP, NTRet); |
Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 602 | } else { |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 603 | new StoreInst(outputs[out], &*OAI, NTRet); |
Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 606 | // Advance output iterator even if we don't emit a store |
| 607 | if (!AggregateArgs) ++OAI; |
| 608 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 609 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 610 | |
| 611 | // rewrite the original branch instruction with this new target |
| 612 | TI->setSuccessor(i, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 613 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 614 | } |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 3d1ca67 | 2004-05-12 03:22:33 +0000 | [diff] [blame] | 616 | // Now that we've done the deed, simplify the switch instruction. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 617 | Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType(); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 618 | switch (NumExitBlocks) { |
| 619 | case 0: |
Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 620 | // There are no successors (the block containing the switch itself), which |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 621 | // means that previously this was the last part of the function, and hence |
| 622 | // this should be rewritten as a `ret' |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 623 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 624 | // Check if the function should return a value |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 625 | if (OldFnRetTy->isVoidTy()) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 626 | ReturnInst::Create(Context, nullptr, TheSwitch); // Return void |
Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 627 | } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) { |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 628 | // return what we have |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 629 | ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch); |
Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 630 | } else { |
| 631 | // Otherwise we must have code extracted an unwind or something, just |
| 632 | // return whatever we want. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 633 | ReturnInst::Create(Context, |
| 634 | Constant::getNullValue(OldFnRetTy), TheSwitch); |
Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 635 | } |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 636 | |
Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 637 | TheSwitch->eraseFromParent(); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 638 | break; |
| 639 | case 1: |
| 640 | // Only a single destination, change the switch into an unconditional |
| 641 | // branch. |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 642 | BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch); |
Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 643 | TheSwitch->eraseFromParent(); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 644 | break; |
| 645 | case 2: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 646 | BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2), |
| 647 | call, TheSwitch); |
Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 648 | TheSwitch->eraseFromParent(); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 649 | break; |
| 650 | default: |
| 651 | // Otherwise, make the default destination of the switch instruction be one |
| 652 | // of the other successors. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 653 | TheSwitch->setCondition(call); |
| 654 | TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks)); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 655 | // Remove redundant case |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 656 | TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1)); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 657 | break; |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 658 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 661 | void CodeExtractor::moveCodeToFunction(Function *newFunction) { |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 662 | Function *oldFunc = (*Blocks.begin())->getParent(); |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 663 | Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList(); |
| 664 | Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList(); |
| 665 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 666 | for (BasicBlock *Block : Blocks) { |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 667 | // Delete the basic block from the old function, and the list of blocks |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 668 | oldBlocks.remove(Block); |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 669 | |
| 670 | // Insert this basic block into the new function |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 671 | newBlocks.push_back(Block); |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 672 | } |
| 673 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 674 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 675 | Function *CodeExtractor::extractCodeRegion() { |
| 676 | if (!isEligible()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 677 | return nullptr; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 678 | |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 679 | ValueSet inputs, outputs; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 680 | |
| 681 | // Assumption: this is a single-entry code region, and the header is the first |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 682 | // block in the region. |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 683 | BasicBlock *header = *Blocks.begin(); |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 685 | // If we have to split PHI nodes or the entry block, do so now. |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 686 | severSplitPHINodes(header); |
| 687 | |
Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 688 | // If we have any return instructions in the region, split those blocks so |
| 689 | // that the return is not in the region. |
| 690 | splitReturnBlocks(); |
| 691 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 692 | Function *oldFunction = header->getParent(); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 693 | |
| 694 | // This takes place of the original loop |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 695 | BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(), |
| 696 | "codeRepl", oldFunction, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 697 | header); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 698 | |
| 699 | // The new function needs a root node because other nodes can branch to the |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 700 | // head of the region, but the entry node of a function cannot have preds. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 701 | BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(), |
| 702 | "newFuncRoot"); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 703 | newFuncRoot->getInstList().push_back(BranchInst::Create(header)); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 705 | // Find inputs to, outputs from the code region. |
| 706 | findInputsOutputs(inputs, outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 707 | |
Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 708 | SmallPtrSet<BasicBlock *, 1> ExitBlocks; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 709 | for (BasicBlock *Block : Blocks) |
| 710 | for (succ_iterator SI = succ_begin(Block), SE = succ_end(Block); SI != SE; |
| 711 | ++SI) |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 712 | if (!Blocks.count(*SI)) |
| 713 | ExitBlocks.insert(*SI); |
Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 714 | NumExitBlocks = ExitBlocks.size(); |
| 715 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 716 | // Construct new function based on inputs/outputs & add allocas for all defs. |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 717 | Function *newFunction = constructFunction(inputs, outputs, header, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 718 | newFuncRoot, |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 719 | codeReplacer, oldFunction, |
| 720 | oldFunction->getParent()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 722 | emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 723 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 724 | moveCodeToFunction(newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 726 | // Loop over all of the PHI nodes in the header block, and change any |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 727 | // references to the old incoming edge to be the new incoming edge. |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 728 | for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) { |
| 729 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 730 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 731 | if (!Blocks.count(PN->getIncomingBlock(i))) |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 732 | PN->setIncomingBlock(i, newFuncRoot); |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 733 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 734 | |
Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 735 | // Look at all successors of the codeReplacer block. If any of these blocks |
| 736 | // had PHI nodes in them, we need to update the "from" block to be the code |
| 737 | // replacer, not the original block in the extracted region. |
| 738 | std::vector<BasicBlock*> Succs(succ_begin(codeReplacer), |
| 739 | succ_end(codeReplacer)); |
| 740 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 741 | for (BasicBlock::iterator I = Succs[i]->begin(); isa<PHINode>(I); ++I) { |
| 742 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 5627382 | 2004-08-13 03:27:07 +0000 | [diff] [blame] | 743 | std::set<BasicBlock*> ProcessedPreds; |
Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 744 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 745 | if (Blocks.count(PN->getIncomingBlock(i))) { |
Chris Lattner | 5627382 | 2004-08-13 03:27:07 +0000 | [diff] [blame] | 746 | if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second) |
| 747 | PN->setIncomingBlock(i, codeReplacer); |
| 748 | else { |
| 749 | // There were multiple entries in the PHI for this block, now there |
| 750 | // is only one, so remove the duplicated entries. |
| 751 | PN->removeIncomingValue(i, false); |
| 752 | --i; --e; |
| 753 | } |
Anton Korobeynikov | 1bfd121 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 754 | } |
Chris Lattner | 5627382 | 2004-08-13 03:27:07 +0000 | [diff] [blame] | 755 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 756 | |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 757 | //cerr << "NEW FUNCTION: " << *newFunction; |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 758 | // verifyFunction(*newFunction); |
| 759 | |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 760 | // cerr << "OLD FUNCTION: " << *oldFunction; |
Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 761 | // verifyFunction(*oldFunction); |
Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 762 | |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 763 | DEBUG(if (verifyFunction(*newFunction)) |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 764 | report_fatal_error("verifyFunction failed!")); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 765 | return newFunction; |
| 766 | } |