| 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" |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 21 | #include "llvm/Analysis/BlockFrequencyInfoImpl.h" |
| 22 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
| 24 | #include "llvm/Analysis/RegionInfo.h" |
| 25 | #include "llvm/Analysis/RegionIterator.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DerivedTypes.h" |
| Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Instructions.h" |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IntrinsicInst.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Intrinsics.h" |
| 32 | #include "llvm/IR/LLVMContext.h" |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 33 | #include "llvm/IR/MDBuilder.h" |
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Module.h" |
| Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Verifier.h" |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 36 | #include "llvm/Pass.h" |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 37 | #include "llvm/Support/BlockFrequency.h" |
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
| 39 | #include "llvm/Support/Debug.h" |
| Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 40 | #include "llvm/Support/ErrorHandling.h" |
| Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 43 | #include <algorithm> |
| Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 44 | #include <set> |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
| Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "code-extractor" |
| 48 | |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 49 | // Provide a command-line option to aggregate function arguments into a struct |
| Misha Brukman | 234b44a | 2008-12-13 05:21:37 +0000 | [diff] [blame] | 50 | // for functions produced by the code extractor. This is useful when converting |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 51 | // extracted functions to pthread-based code, as only one argument (void*) can |
| 52 | // be passed in to pthread_create(). |
| 53 | static cl::opt<bool> |
| 54 | AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, |
| 55 | cl::desc("Aggregate arguments to code-extracted functions")); |
| 56 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 57 | /// \brief Test whether a block is valid for extraction. |
| Sean Silva | 285e097 | 2016-07-27 08:02:46 +0000 | [diff] [blame] | 58 | bool CodeExtractor::isBlockValidForExtraction(const BasicBlock &BB) { |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 59 | // 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] | 60 | if (BB.isEHPad()) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 61 | return false; |
| Serge Guelton | 7bc405a | 2017-06-27 18:57:53 +0000 | [diff] [blame] | 62 | // taking the address of a basic block moved to another function is illegal |
| 63 | if (BB.hasAddressTaken()) |
| 64 | return false; |
| 65 | |
| 66 | // don't hoist code that uses another basicblock address, as it's likely to |
| 67 | // lead to unexpected behavior, like cross-function jumps |
| 68 | SmallPtrSet<User const *, 16> Visited; |
| 69 | SmallVector<User const *, 16> ToVisit; |
| 70 | |
| 71 | for (Instruction const &Inst : BB) |
| 72 | ToVisit.push_back(&Inst); |
| 73 | |
| 74 | while (!ToVisit.empty()) { |
| 75 | User const *Curr = ToVisit.pop_back_val(); |
| 76 | if (!Visited.insert(Curr).second) |
| 77 | continue; |
| 78 | if (isa<BlockAddress const>(Curr)) |
| 79 | return false; // even a reference to self is likely to be not compatible |
| 80 | |
| 81 | if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB) |
| 82 | continue; |
| 83 | |
| 84 | for (auto const &U : Curr->operands()) { |
| 85 | if (auto *UU = dyn_cast<User>(U)) |
| 86 | ToVisit.push_back(UU); |
| 87 | } |
| 88 | } |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 89 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 90 | // Don't hoist code containing allocas, invokes, or vastarts. |
| 91 | for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |
| 92 | if (isa<AllocaInst>(I) || isa<InvokeInst>(I)) |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 93 | return false; |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 94 | if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 95 | if (const Function *F = CI->getCalledFunction()) |
| 96 | if (F->getIntrinsicID() == Intrinsic::vastart) |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /// \brief Build a set of blocks to extract if the input blocks are viable. |
| Davide Italiano | 059574c | 2017-04-21 00:21:09 +0000 | [diff] [blame] | 104 | static SetVector<BasicBlock *> |
| Davide Italiano | fa15de3 | 2017-04-21 04:25:00 +0000 | [diff] [blame] | 105 | buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs, DominatorTree *DT) { |
| 106 | assert(!BBs.empty() && "The set of blocks to extract must be non-empty"); |
| Davide Italiano | 059574c | 2017-04-21 00:21:09 +0000 | [diff] [blame] | 107 | SetVector<BasicBlock *> Result; |
| Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 108 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 109 | // Loop over the blocks, adding them to our set-vector, and aborting with an |
| 110 | // empty set if we encounter invalid blocks. |
| Davide Italiano | fa15de3 | 2017-04-21 04:25:00 +0000 | [diff] [blame] | 111 | for (BasicBlock *BB : BBs) { |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 112 | |
| Davide Italiano | fa15de3 | 2017-04-21 04:25:00 +0000 | [diff] [blame] | 113 | // If this block is dead, don't process it. |
| 114 | if (DT && !DT->isReachableFromEntry(BB)) |
| 115 | continue; |
| 116 | |
| 117 | if (!Result.insert(BB)) |
| 118 | llvm_unreachable("Repeated basic blocks in extraction input"); |
| 119 | if (!CodeExtractor::isBlockValidForExtraction(*BB)) { |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 120 | Result.clear(); |
| Chandler Carruth | 0a57055 | 2012-05-04 11:14:19 +0000 | [diff] [blame] | 121 | return Result; |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 122 | } |
| Davide Italiano | fa15de3 | 2017-04-21 04:25:00 +0000 | [diff] [blame] | 123 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 124 | |
| Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 125 | #ifndef NDEBUG |
| Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 126 | for (SetVector<BasicBlock *>::iterator I = std::next(Result.begin()), |
| Chandler Carruth | 6781821 | 2012-05-04 21:33:30 +0000 | [diff] [blame] | 127 | E = Result.end(); |
| Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 128 | I != E; ++I) |
| Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 129 | for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I); |
| 130 | PI != PE; ++PI) |
| 131 | assert(Result.count(*PI) && |
| Chandler Carruth | 2f5d019 | 2012-05-04 10:26:45 +0000 | [diff] [blame] | 132 | "No blocks in this region may have entries from outside the region" |
| 133 | " except for the first block!"); |
| 134 | #endif |
| 135 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 136 | return Result; |
| 137 | } |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 138 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 139 | CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT, |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 140 | bool AggregateArgs, BlockFrequencyInfo *BFI, |
| 141 | BranchProbabilityInfo *BPI) |
| 142 | : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI), |
| Davide Italiano | fa15de3 | 2017-04-21 04:25:00 +0000 | [diff] [blame] | 143 | BPI(BPI), Blocks(buildExtractionBlockSet(BBs, DT)), NumExitBlocks(~0U) {} |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 144 | |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 145 | CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs, |
| 146 | BlockFrequencyInfo *BFI, |
| 147 | BranchProbabilityInfo *BPI) |
| 148 | : DT(&DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI), |
| Davide Italiano | fa15de3 | 2017-04-21 04:25:00 +0000 | [diff] [blame] | 149 | BPI(BPI), Blocks(buildExtractionBlockSet(L.getBlocks(), &DT)), |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 150 | NumExitBlocks(~0U) {} |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 151 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 152 | /// definedInRegion - Return true if the specified value is defined in the |
| 153 | /// extracted region. |
| 154 | static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) { |
| 155 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 156 | if (Blocks.count(I->getParent())) |
| 157 | return true; |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | /// definedInCaller - Return true if the specified value is defined in the |
| 162 | /// function being code extracted, but not in the region being extracted. |
| 163 | /// These values must be passed in as live-ins to the function. |
| 164 | static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) { |
| 165 | if (isa<Argument>(V)) return true; |
| 166 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 167 | if (!Blocks.count(I->getParent())) |
| 168 | return true; |
| 169 | return false; |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 172 | static BasicBlock *getCommonExitBlock(const SetVector<BasicBlock *> &Blocks) { |
| 173 | BasicBlock *CommonExitBlock = nullptr; |
| 174 | auto hasNonCommonExitSucc = [&](BasicBlock *Block) { |
| 175 | for (auto *Succ : successors(Block)) { |
| 176 | // Internal edges, ok. |
| 177 | if (Blocks.count(Succ)) |
| 178 | continue; |
| 179 | if (!CommonExitBlock) { |
| 180 | CommonExitBlock = Succ; |
| 181 | continue; |
| 182 | } |
| 183 | if (CommonExitBlock == Succ) |
| 184 | continue; |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | return false; |
| 189 | }; |
| 190 | |
| 191 | if (any_of(Blocks, hasNonCommonExitSucc)) |
| 192 | return nullptr; |
| 193 | |
| 194 | return CommonExitBlock; |
| 195 | } |
| 196 | |
| 197 | bool CodeExtractor::isLegalToShrinkwrapLifetimeMarkers( |
| 198 | Instruction *Addr) const { |
| 199 | AllocaInst *AI = cast<AllocaInst>(Addr->stripInBoundsConstantOffsets()); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 200 | Function *Func = (*Blocks.begin())->getParent(); |
| 201 | for (BasicBlock &BB : *Func) { |
| 202 | if (Blocks.count(&BB)) |
| 203 | continue; |
| 204 | for (Instruction &II : BB) { |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 205 | |
| 206 | if (isa<DbgInfoIntrinsic>(II)) |
| 207 | continue; |
| 208 | |
| 209 | unsigned Opcode = II.getOpcode(); |
| 210 | Value *MemAddr = nullptr; |
| 211 | switch (Opcode) { |
| 212 | case Instruction::Store: |
| 213 | case Instruction::Load: { |
| 214 | if (Opcode == Instruction::Store) { |
| 215 | StoreInst *SI = cast<StoreInst>(&II); |
| 216 | MemAddr = SI->getPointerOperand(); |
| 217 | } else { |
| 218 | LoadInst *LI = cast<LoadInst>(&II); |
| 219 | MemAddr = LI->getPointerOperand(); |
| 220 | } |
| 221 | // Global variable can not be aliased with locals. |
| 222 | if (dyn_cast<Constant>(MemAddr)) |
| 223 | break; |
| 224 | Value *Base = MemAddr->stripInBoundsConstantOffsets(); |
| 225 | if (!dyn_cast<AllocaInst>(Base) || Base == AI) |
| 226 | return false; |
| 227 | break; |
| 228 | } |
| 229 | default: { |
| 230 | IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(&II); |
| 231 | if (IntrInst) { |
| 232 | if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start || |
| 233 | IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) |
| 234 | break; |
| 235 | return false; |
| 236 | } |
| 237 | // Treat all the other cases conservatively if it has side effects. |
| 238 | if (II.mayHaveSideEffects()) |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | BasicBlock * |
| 249 | CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) { |
| 250 | BasicBlock *SinglePredFromOutlineRegion = nullptr; |
| 251 | assert(!Blocks.count(CommonExitBlock) && |
| 252 | "Expect a block outside the region!"); |
| 253 | for (auto *Pred : predecessors(CommonExitBlock)) { |
| 254 | if (!Blocks.count(Pred)) |
| 255 | continue; |
| 256 | if (!SinglePredFromOutlineRegion) { |
| 257 | SinglePredFromOutlineRegion = Pred; |
| 258 | } else if (SinglePredFromOutlineRegion != Pred) { |
| 259 | SinglePredFromOutlineRegion = nullptr; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (SinglePredFromOutlineRegion) |
| 265 | return SinglePredFromOutlineRegion; |
| 266 | |
| 267 | #ifndef NDEBUG |
| 268 | auto getFirstPHI = [](BasicBlock *BB) { |
| 269 | BasicBlock::iterator I = BB->begin(); |
| 270 | PHINode *FirstPhi = nullptr; |
| 271 | while (I != BB->end()) { |
| 272 | PHINode *Phi = dyn_cast<PHINode>(I); |
| 273 | if (!Phi) |
| 274 | break; |
| 275 | if (!FirstPhi) { |
| 276 | FirstPhi = Phi; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | return FirstPhi; |
| 281 | }; |
| 282 | // If there are any phi nodes, the single pred either exists or has already |
| 283 | // be created before code extraction. |
| 284 | assert(!getFirstPHI(CommonExitBlock) && "Phi not expected"); |
| 285 | #endif |
| 286 | |
| 287 | BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock( |
| 288 | CommonExitBlock->getFirstNonPHI()->getIterator()); |
| 289 | |
| 290 | for (auto *Pred : predecessors(CommonExitBlock)) { |
| 291 | if (Blocks.count(Pred)) |
| 292 | continue; |
| 293 | Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock); |
| 294 | } |
| 295 | // Now add the old exit block to the outline region. |
| 296 | Blocks.insert(CommonExitBlock); |
| 297 | return CommonExitBlock; |
| 298 | } |
| 299 | |
| 300 | void CodeExtractor::findAllocas(ValueSet &SinkCands, ValueSet &HoistCands, |
| 301 | BasicBlock *&ExitBlock) const { |
| 302 | Function *Func = (*Blocks.begin())->getParent(); |
| 303 | ExitBlock = getCommonExitBlock(Blocks); |
| 304 | |
| 305 | for (BasicBlock &BB : *Func) { |
| 306 | if (Blocks.count(&BB)) |
| 307 | continue; |
| 308 | for (Instruction &II : BB) { |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 309 | auto *AI = dyn_cast<AllocaInst>(&II); |
| 310 | if (!AI) |
| 311 | continue; |
| 312 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 313 | // Find the pair of life time markers for address 'Addr' that are either |
| 314 | // defined inside the outline region or can legally be shrinkwrapped into |
| 315 | // the outline region. If there are not other untracked uses of the |
| 316 | // address, return the pair of markers if found; otherwise return a pair |
| 317 | // of nullptr. |
| 318 | auto GetLifeTimeMarkers = |
| 319 | [&](Instruction *Addr, bool &SinkLifeStart, |
| 320 | bool &HoistLifeEnd) -> std::pair<Instruction *, Instruction *> { |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 321 | Instruction *LifeStart = nullptr, *LifeEnd = nullptr; |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 322 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 323 | for (User *U : Addr->users()) { |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 324 | IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(U); |
| 325 | if (IntrInst) { |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 326 | if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) { |
| 327 | // Do not handle the case where AI has multiple start markers. |
| 328 | if (LifeStart) |
| 329 | return std::make_pair<Instruction *>(nullptr, nullptr); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 330 | LifeStart = IntrInst; |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 331 | } |
| 332 | if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) { |
| 333 | if (LifeEnd) |
| 334 | return std::make_pair<Instruction *>(nullptr, nullptr); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 335 | LifeEnd = IntrInst; |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 336 | } |
| 337 | continue; |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 338 | } |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 339 | // Find untracked uses of the address, bail. |
| 340 | if (!definedInRegion(Blocks, U)) |
| 341 | return std::make_pair<Instruction *>(nullptr, nullptr); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 342 | } |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 343 | |
| 344 | if (!LifeStart || !LifeEnd) |
| 345 | return std::make_pair<Instruction *>(nullptr, nullptr); |
| 346 | |
| 347 | SinkLifeStart = !definedInRegion(Blocks, LifeStart); |
| 348 | HoistLifeEnd = !definedInRegion(Blocks, LifeEnd); |
| 349 | // Do legality Check. |
| 350 | if ((SinkLifeStart || HoistLifeEnd) && |
| 351 | !isLegalToShrinkwrapLifetimeMarkers(Addr)) |
| 352 | return std::make_pair<Instruction *>(nullptr, nullptr); |
| 353 | |
| 354 | // Check to see if we have a place to do hoisting, if not, bail. |
| 355 | if (HoistLifeEnd && !ExitBlock) |
| 356 | return std::make_pair<Instruction *>(nullptr, nullptr); |
| 357 | |
| 358 | return std::make_pair(LifeStart, LifeEnd); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 361 | bool SinkLifeStart = false, HoistLifeEnd = false; |
| 362 | auto Markers = GetLifeTimeMarkers(AI, SinkLifeStart, HoistLifeEnd); |
| 363 | |
| 364 | if (Markers.first) { |
| 365 | if (SinkLifeStart) |
| 366 | SinkCands.insert(Markers.first); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 367 | SinkCands.insert(AI); |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 368 | if (HoistLifeEnd) |
| 369 | HoistCands.insert(Markers.second); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 370 | continue; |
| 371 | } |
| 372 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 373 | // Follow the bitcast. |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 374 | Instruction *MarkerAddr = nullptr; |
| 375 | for (User *U : AI->users()) { |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 376 | |
| 377 | if (U->stripInBoundsConstantOffsets() == AI) { |
| 378 | SinkLifeStart = false; |
| 379 | HoistLifeEnd = false; |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 380 | Instruction *Bitcast = cast<Instruction>(U); |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 381 | Markers = GetLifeTimeMarkers(Bitcast, SinkLifeStart, HoistLifeEnd); |
| 382 | if (Markers.first) { |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 383 | MarkerAddr = Bitcast; |
| 384 | continue; |
| 385 | } |
| 386 | } |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 387 | |
| 388 | // Found unknown use of AI. |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 389 | if (!definedInRegion(Blocks, U)) { |
| 390 | MarkerAddr = nullptr; |
| 391 | break; |
| 392 | } |
| 393 | } |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 394 | |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 395 | if (MarkerAddr) { |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 396 | if (SinkLifeStart) |
| 397 | SinkCands.insert(Markers.first); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 398 | if (!definedInRegion(Blocks, MarkerAddr)) |
| 399 | SinkCands.insert(MarkerAddr); |
| 400 | SinkCands.insert(AI); |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 401 | if (HoistLifeEnd) |
| 402 | HoistCands.insert(Markers.second); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, |
| 409 | const ValueSet &SinkCands) const { |
| 410 | |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 411 | for (BasicBlock *BB : Blocks) { |
| Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 412 | // If a used value is defined outside the region, it's an input. If an |
| 413 | // instruction is used outside the region, it's an output. |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 414 | for (Instruction &II : *BB) { |
| 415 | for (User::op_iterator OI = II.op_begin(), OE = II.op_end(); OI != OE; |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 416 | ++OI) { |
| 417 | Value *V = *OI; |
| 418 | if (!SinkCands.count(V) && definedInCaller(Blocks, V)) |
| 419 | Inputs.insert(V); |
| 420 | } |
| Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 421 | |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 422 | for (User *U : II.users()) |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 423 | if (!definedInRegion(Blocks, U)) { |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 424 | Outputs.insert(&II); |
| Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 431 | /// severSplitPHINodes - If a PHI node has multiple inputs from outside of the |
| 432 | /// region, we need to split the entry block of the region so that the PHI node |
| 433 | /// is easier to deal with. |
| 434 | void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) { |
| Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 435 | unsigned NumPredsFromRegion = 0; |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 436 | unsigned NumPredsOutsideRegion = 0; |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 437 | |
| Dan Gohman | dcb291f | 2007-03-22 16:38:57 +0000 | [diff] [blame] | 438 | if (Header != &Header->getParent()->getEntryBlock()) { |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 439 | PHINode *PN = dyn_cast<PHINode>(Header->begin()); |
| 440 | if (!PN) return; // No PHI nodes. |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 441 | |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 442 | // If the header node contains any PHI nodes, check to see if there is more |
| 443 | // than one entry from outside the region. If so, we need to sever the |
| 444 | // header block into two. |
| 445 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 446 | if (Blocks.count(PN->getIncomingBlock(i))) |
| Jay Foad | e0938d8 | 2011-03-30 11:19:20 +0000 | [diff] [blame] | 447 | ++NumPredsFromRegion; |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 448 | else |
| 449 | ++NumPredsOutsideRegion; |
| 450 | |
| 451 | // If there is one (or fewer) predecessor from outside the region, we don't |
| 452 | // need to do anything special. |
| 453 | if (NumPredsOutsideRegion <= 1) return; |
| 454 | } |
| 455 | |
| 456 | // Otherwise, we need to split the header block into two pieces: one |
| 457 | // containing PHI nodes merging values from outside of the region, and a |
| 458 | // second that contains all of the code for the block and merges back any |
| 459 | // incoming values from inside of the region. |
| Xinliang David Li | 99e3ca1 | 2017-04-20 21:40:22 +0000 | [diff] [blame] | 460 | BasicBlock *NewBB = llvm::SplitBlock(Header, Header->getFirstNonPHI(), DT); |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 461 | |
| 462 | // We only want to code extract the second block now, and it becomes the new |
| 463 | // header of the region. |
| 464 | BasicBlock *OldPred = Header; |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 465 | Blocks.remove(OldPred); |
| 466 | Blocks.insert(NewBB); |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 467 | Header = NewBB; |
| 468 | |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 469 | // Okay, now we need to adjust the PHI nodes and any branches from within the |
| 470 | // 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] | 471 | if (NumPredsFromRegion) { |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 472 | PHINode *PN = cast<PHINode>(OldPred->begin()); |
| 473 | // Loop over all of the predecessors of OldPred that are in the region, |
| 474 | // changing them to branch to NewBB instead. |
| 475 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 476 | if (Blocks.count(PN->getIncomingBlock(i))) { |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 477 | TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator(); |
| 478 | TI->replaceUsesOfWith(OldPred, NewBB); |
| 479 | } |
| 480 | |
| Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 481 | // 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] | 482 | // just have to update the PHI nodes now, inserting PHI nodes into NewBB. |
| Xinliang David Li | 99e3ca1 | 2017-04-20 21:40:22 +0000 | [diff] [blame] | 483 | BasicBlock::iterator AfterPHIs; |
| Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 484 | for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) { |
| 485 | PHINode *PN = cast<PHINode>(AfterPHIs); |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 486 | // Create a new PHI node in the new region, which has an incoming value |
| 487 | // from OldPred of PN. |
| Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 488 | PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion, |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 489 | PN->getName() + ".ce", &NewBB->front()); |
| Xinliang David Li | f12a0fa | 2017-04-25 04:51:19 +0000 | [diff] [blame] | 490 | PN->replaceAllUsesWith(NewPN); |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 491 | NewPN->addIncoming(PN, OldPred); |
| 492 | |
| 493 | // Loop over all of the incoming value in PN, moving them to NewPN if they |
| 494 | // are from the extracted region. |
| 495 | for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) { |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 496 | if (Blocks.count(PN->getIncomingBlock(i))) { |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 497 | NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i)); |
| 498 | PN->removeIncomingValue(i); |
| 499 | --i; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 504 | } |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 505 | |
| Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 506 | void CodeExtractor::splitReturnBlocks() { |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 507 | for (BasicBlock *Block : Blocks) |
| 508 | if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) { |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 509 | BasicBlock *New = |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 510 | Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret"); |
| Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 511 | if (DT) { |
| Gabor Greif | 2f5f696 | 2010-09-10 22:25:58 +0000 | [diff] [blame] | 512 | // Old dominates New. New node dominates all other nodes dominated |
| 513 | // by Old. |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 514 | DomTreeNode *OldNode = DT->getNode(Block); |
| 515 | SmallVector<DomTreeNode *, 8> Children(OldNode->begin(), |
| 516 | OldNode->end()); |
| Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 517 | |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 518 | DomTreeNode *NewNode = DT->addNewBlock(New, Block); |
| Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 519 | |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 520 | for (DomTreeNode *I : Children) |
| 521 | DT->changeImmediateDominator(I, NewNode); |
| Owen Anderson | b4aa5b1 | 2009-08-24 23:32:14 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 526 | /// constructFunction - make a function based on inputs and outputs, as follows: |
| 527 | /// f(in0, ..., inN, out0, ..., outN) |
| 528 | /// |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 529 | Function *CodeExtractor::constructFunction(const ValueSet &inputs, |
| 530 | const ValueSet &outputs, |
| Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 531 | BasicBlock *header, |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 532 | BasicBlock *newRootNode, |
| 533 | BasicBlock *newHeader, |
| Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 534 | Function *oldFunction, |
| 535 | Module *M) { |
| David Greene | 0ad6dce | 2010-01-05 01:26:44 +0000 | [diff] [blame] | 536 | DEBUG(dbgs() << "inputs: " << inputs.size() << "\n"); |
| 537 | DEBUG(dbgs() << "outputs: " << outputs.size() << "\n"); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 538 | |
| 539 | // This function returns unsigned, outputs will go back by reference. |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 540 | switch (NumExitBlocks) { |
| 541 | case 0: |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 542 | case 1: RetTy = Type::getVoidTy(header->getContext()); break; |
| 543 | case 2: RetTy = Type::getInt1Ty(header->getContext()); break; |
| 544 | default: RetTy = Type::getInt16Ty(header->getContext()); break; |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 547 | std::vector<Type*> paramTy; |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 548 | |
| 549 | // 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] | 550 | for (Value *value : inputs) { |
| David Greene | 0ad6dce | 2010-01-05 01:26:44 +0000 | [diff] [blame] | 551 | DEBUG(dbgs() << "value used in func: " << *value << "\n"); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 552 | paramTy.push_back(value->getType()); |
| 553 | } |
| 554 | |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 555 | // 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] | 556 | for (Value *output : outputs) { |
| 557 | DEBUG(dbgs() << "instr used in func: " << *output << "\n"); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 558 | if (AggregateArgs) |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 559 | paramTy.push_back(output->getType()); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 560 | else |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 561 | paramTy.push_back(PointerType::getUnqual(output->getType())); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| Benjamin Kramer | 706e4883 | 2016-06-26 13:39:33 +0000 | [diff] [blame] | 564 | DEBUG({ |
| 565 | dbgs() << "Function type: " << *RetTy << " f("; |
| 566 | for (Type *i : paramTy) |
| 567 | dbgs() << *i << ", "; |
| 568 | dbgs() << ")\n"; |
| 569 | }); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 570 | |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 571 | StructType *StructTy; |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 572 | if (AggregateArgs && (inputs.size() + outputs.size() > 0)) { |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 573 | StructTy = StructType::get(M->getContext(), paramTy); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 574 | paramTy.clear(); |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 575 | paramTy.push_back(PointerType::getUnqual(StructTy)); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 576 | } |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 577 | FunctionType *funcType = |
| Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 578 | FunctionType::get(RetTy, paramTy, false); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 579 | |
| 580 | // Create the new function |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 581 | Function *newFunction = Function::Create(funcType, |
| 582 | GlobalValue::InternalLinkage, |
| 583 | oldFunction->getName() + "_" + |
| 584 | header->getName(), M); |
| Chris Lattner | 4caf5eb | 2008-12-18 05:52:56 +0000 | [diff] [blame] | 585 | // If the old function is no-throw, so is the new one. |
| 586 | if (oldFunction->doesNotThrow()) |
| Bill Wendling | f319e99 | 2012-10-10 03:12:49 +0000 | [diff] [blame] | 587 | newFunction->setDoesNotThrow(); |
| Sean Silva | a0a802a | 2016-08-01 03:15:32 +0000 | [diff] [blame] | 588 | |
| 589 | // Inherit the uwtable attribute if we need to. |
| 590 | if (oldFunction->hasUWTable()) |
| 591 | newFunction->setHasUWTable(); |
| 592 | |
| 593 | // Inherit all of the target dependent attributes. |
| 594 | // (e.g. If the extracted region contains a call to an x86.sse |
| 595 | // instruction we need to make sure that the extracted region has the |
| 596 | // "target-features" attribute allowing it to be lowered. |
| 597 | // FIXME: This should be changed to check to see if a specific |
| 598 | // attribute can not be inherited. |
| Reid Kleckner | eb9dd5b | 2017-04-10 23:31:05 +0000 | [diff] [blame] | 599 | AttrBuilder AB(oldFunction->getAttributes().getFnAttributes()); |
| Sean Silva | 9011aca | 2017-02-22 06:34:04 +0000 | [diff] [blame] | 600 | for (const auto &Attr : AB.td_attrs()) |
| Sean Silva | a0a802a | 2016-08-01 03:15:32 +0000 | [diff] [blame] | 601 | newFunction->addFnAttr(Attr.first, Attr.second); |
| 602 | |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 603 | newFunction->getBasicBlockList().push_back(newRootNode); |
| 604 | |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 605 | // Create an iterator to name all of the arguments we inserted. |
| Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 606 | Function::arg_iterator AI = newFunction->arg_begin(); |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 607 | |
| 608 | // 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] | 609 | // arguments (or appropriate addressing into struct) instead. |
| 610 | for (unsigned i = 0, e = inputs.size(); i != e; ++i) { |
| 611 | Value *RewriteVal; |
| 612 | if (AggregateArgs) { |
| David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 613 | Value *Idx[2]; |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 614 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext())); |
| 615 | Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 616 | TerminatorInst *TI = newFunction->begin()->getTerminator(); |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 617 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 618 | StructTy, &*AI, Idx, "gep_" + inputs[i]->getName(), TI); |
| Daniel Dunbar | 12368685 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 619 | RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 620 | } else |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 621 | RewriteVal = &*AI++; |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 622 | |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 623 | std::vector<User*> Users(inputs[i]->user_begin(), inputs[i]->user_end()); |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 624 | for (User *use : Users) |
| 625 | if (Instruction *inst = dyn_cast<Instruction>(use)) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 626 | if (Blocks.count(inst->getParent())) |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 627 | inst->replaceUsesOfWith(inputs[i], RewriteVal); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 630 | // Set names for input and output arguments. |
| 631 | if (!AggregateArgs) { |
| Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 632 | AI = newFunction->arg_begin(); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 633 | for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI) |
| Owen Anderson | 7629b71 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 634 | AI->setName(inputs[i]->getName()); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 635 | for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI) |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 636 | AI->setName(outputs[i]->getName()+".out"); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 637 | } |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 638 | |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 639 | // Rewrite branches to basic blocks outside of the loop to new dummy blocks |
| 640 | // within the new function. This must be done before we lose track of which |
| 641 | // blocks were originally in the code region. |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 642 | std::vector<User*> Users(header->user_begin(), header->user_end()); |
| Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 643 | for (unsigned i = 0, e = Users.size(); i != e; ++i) |
| 644 | // The BasicBlock which contains the branch is not in the region |
| 645 | // modify the branch target to a new block |
| 646 | if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i])) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 647 | if (!Blocks.count(TI->getParent()) && |
| Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 648 | TI->getParent()->getParent() == oldFunction) |
| 649 | TI->replaceUsesOfWith(header, newHeader); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 650 | |
| 651 | return newFunction; |
| 652 | } |
| 653 | |
| Owen Anderson | 4e9ac2a | 2009-08-25 17:42:07 +0000 | [diff] [blame] | 654 | /// FindPhiPredForUseInBlock - Given a value and a basic block, find a PHI |
| 655 | /// that uses the value within the basic block, and return the predecessor |
| 656 | /// block associated with that use, or return 0 if none is found. |
| Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 657 | static BasicBlock* FindPhiPredForUseInBlock(Value* Used, BasicBlock* BB) { |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 658 | for (Use &U : Used->uses()) { |
| 659 | PHINode *P = dyn_cast<PHINode>(U.getUser()); |
| Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 660 | if (P && P->getParent() == BB) |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 661 | return P->getIncomingBlock(U); |
| Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 662 | } |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 663 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 664 | return nullptr; |
| Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 667 | /// emitCallAndSwitchStatement - This method sets up the caller side by adding |
| 668 | /// the call instruction, splitting any PHI nodes in the header block as |
| 669 | /// necessary. |
| 670 | void CodeExtractor:: |
| 671 | emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer, |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 672 | ValueSet &inputs, ValueSet &outputs) { |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 673 | // Emit a call to the new function, passing in: *pointer to struct (if |
| 674 | // aggregating parameters), or plan inputs and allocated memory for outputs |
| Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 675 | std::vector<Value*> params, StructValues, ReloadOutputs, Reloads; |
| Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 676 | |
| 677 | Module *M = newFunction->getParent(); |
| 678 | LLVMContext &Context = M->getContext(); |
| 679 | const DataLayout &DL = M->getDataLayout(); |
| Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 680 | |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 681 | // Add inputs as params, or to be filled into the struct |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 682 | for (Value *input : inputs) |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 683 | if (AggregateArgs) |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 684 | StructValues.push_back(input); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 685 | else |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 686 | params.push_back(input); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 687 | |
| 688 | // Create allocas for the outputs |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 689 | for (Value *output : outputs) { |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 690 | if (AggregateArgs) { |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 691 | StructValues.push_back(output); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 692 | } else { |
| 693 | AllocaInst *alloca = |
| Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 694 | new AllocaInst(output->getType(), DL.getAllocaAddrSpace(), |
| 695 | nullptr, output->getName() + ".loc", |
| 696 | &codeReplacer->getParent()->front().front()); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 697 | ReloadOutputs.push_back(alloca); |
| 698 | params.push_back(alloca); |
| 699 | } |
| 700 | } |
| 701 | |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 702 | StructType *StructArgTy = nullptr; |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 703 | AllocaInst *Struct = nullptr; |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 704 | if (AggregateArgs && (inputs.size() + outputs.size() > 0)) { |
| Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 705 | std::vector<Type*> ArgTypes; |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 706 | for (ValueSet::iterator v = StructValues.begin(), |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 707 | ve = StructValues.end(); v != ve; ++v) |
| 708 | ArgTypes.push_back((*v)->getType()); |
| 709 | |
| 710 | // Allocate a struct at the beginning of this function |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 711 | StructArgTy = StructType::get(newFunction->getContext(), ArgTypes); |
| Matt Arsenault | 3c1fc76 | 2017-04-10 22:27:50 +0000 | [diff] [blame] | 712 | Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr, |
| 713 | "structArg", |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 714 | &codeReplacer->getParent()->front().front()); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 715 | params.push_back(Struct); |
| 716 | |
| 717 | for (unsigned i = 0, e = inputs.size(); i != e; ++i) { |
| David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 718 | Value *Idx[2]; |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 719 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 720 | Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i); |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 721 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| 722 | StructArgTy, Struct, Idx, "gep_" + StructValues[i]->getName()); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 723 | codeReplacer->getInstList().push_back(GEP); |
| 724 | StoreInst *SI = new StoreInst(StructValues[i], GEP); |
| 725 | codeReplacer->getInstList().push_back(SI); |
| 726 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 727 | } |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 728 | |
| 729 | // Emit the call to the function |
| Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 730 | CallInst *call = CallInst::Create(newFunction, params, |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 731 | NumExitBlocks > 1 ? "targetBlock" : ""); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 732 | codeReplacer->getInstList().push_back(call); |
| 733 | |
| Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 734 | Function::arg_iterator OutputArgBegin = newFunction->arg_begin(); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 735 | unsigned FirstOut = inputs.size(); |
| 736 | if (!AggregateArgs) |
| 737 | std::advance(OutputArgBegin, inputs.size()); |
| 738 | |
| 739 | // Reload the outputs passed in by reference |
| 740 | for (unsigned i = 0, e = outputs.size(); i != e; ++i) { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 741 | Value *Output = nullptr; |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 742 | if (AggregateArgs) { |
| David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 743 | Value *Idx[2]; |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 744 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 745 | Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i); |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 746 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| 747 | StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName()); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 748 | codeReplacer->getInstList().push_back(GEP); |
| 749 | Output = GEP; |
| 750 | } else { |
| 751 | Output = ReloadOutputs[i]; |
| 752 | } |
| 753 | LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload"); |
| Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 754 | Reloads.push_back(load); |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 755 | codeReplacer->getInstList().push_back(load); |
| Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 756 | std::vector<User*> Users(outputs[i]->user_begin(), outputs[i]->user_end()); |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 757 | for (unsigned u = 0, e = Users.size(); u != e; ++u) { |
| 758 | Instruction *inst = cast<Instruction>(Users[u]); |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 759 | if (!Blocks.count(inst->getParent())) |
| Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 760 | inst->replaceUsesOfWith(outputs[i], load); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 761 | } |
| 762 | } |
| Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 763 | |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 764 | // 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] | 765 | SwitchInst *TheSwitch = |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 766 | SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)), |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 767 | codeReplacer, 0, codeReplacer); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 768 | |
| 769 | // 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] | 770 | // function return an unsigned, switch on that number. This loop iterates |
| 771 | // over all of the blocks in the extracted region, updating any terminator |
| 772 | // instructions in the to-be-extracted region that branch to blocks that are |
| 773 | // not in the region to be extracted. |
| 774 | std::map<BasicBlock*, BasicBlock*> ExitBlockMap; |
| 775 | |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 776 | unsigned switchVal = 0; |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 777 | for (BasicBlock *Block : Blocks) { |
| 778 | TerminatorInst *TI = Block->getTerminator(); |
| Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 779 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 780 | if (!Blocks.count(TI->getSuccessor(i))) { |
| Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 781 | BasicBlock *OldTarget = TI->getSuccessor(i); |
| 782 | // add a new basic block which returns the appropriate value |
| 783 | BasicBlock *&NewTarget = ExitBlockMap[OldTarget]; |
| 784 | if (!NewTarget) { |
| 785 | // If we don't already have an exit stub for this non-extracted |
| 786 | // destination, create one now! |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 787 | NewTarget = BasicBlock::Create(Context, |
| 788 | OldTarget->getName() + ".exitStub", |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 789 | newFunction); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 790 | unsigned SuccNum = switchVal++; |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 791 | |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 792 | Value *brVal = nullptr; |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 793 | switch (NumExitBlocks) { |
| 794 | case 0: |
| 795 | case 1: break; // No value needed. |
| 796 | case 2: // Conditional branch, return a bool |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 797 | brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 798 | break; |
| 799 | default: |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 800 | brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 801 | break; |
| 802 | } |
| 803 | |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 804 | ReturnInst *NTRet = ReturnInst::Create(Context, brVal, NewTarget); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 805 | |
| Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 806 | // Update the switch instruction. |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 807 | TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context), |
| 808 | SuccNum), |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 809 | OldTarget); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 810 | |
| Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 811 | // Restore values just before we exit |
| Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 812 | Function::arg_iterator OAI = OutputArgBegin; |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 813 | for (unsigned out = 0, e = outputs.size(); out != e; ++out) { |
| David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 814 | // For an invoke, the normal destination is the only one that is |
| 815 | // dominated by the result of the invocation |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 816 | BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent(); |
| Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 817 | |
| 818 | bool DominatesDef = true; |
| 819 | |
| David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 820 | BasicBlock *NormalDest = nullptr; |
| 821 | if (auto *Invoke = dyn_cast<InvokeInst>(outputs[out])) |
| 822 | NormalDest = Invoke->getNormalDest(); |
| David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 823 | |
| 824 | if (NormalDest) { |
| 825 | DefBlock = NormalDest; |
| Chris Lattner | 5bcca60 | 2004-11-12 23:50:44 +0000 | [diff] [blame] | 826 | |
| 827 | // Make sure we are looking at the original successor block, not |
| 828 | // at a newly inserted exit block, which won't be in the dominator |
| 829 | // info. |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 830 | for (const auto &I : ExitBlockMap) |
| 831 | if (DefBlock == I.second) { |
| 832 | DefBlock = I.first; |
| Chris Lattner | 5bcca60 | 2004-11-12 23:50:44 +0000 | [diff] [blame] | 833 | break; |
| 834 | } |
| Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 835 | |
| 836 | // In the extract block case, if the block we are extracting ends |
| 837 | // with an invoke instruction, make sure that we don't emit a |
| 838 | // store of the invoke value for the unwind block. |
| Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 839 | if (!DT && DefBlock != OldTarget) |
| Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 840 | DominatesDef = false; |
| Chris Lattner | 5bcca60 | 2004-11-12 23:50:44 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 843 | if (DT) { |
| Devang Patel | cf470e5 | 2007-06-07 22:17:16 +0000 | [diff] [blame] | 844 | DominatesDef = DT->dominates(DefBlock, OldTarget); |
| Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 845 | |
| 846 | // If the output value is used by a phi in the target block, |
| 847 | // then we need to test for dominance of the phi's predecessor |
| 848 | // instead. Unfortunately, this a little complicated since we |
| 849 | // have already rewritten uses of the value to uses of the reload. |
| Owen Anderson | 5e39d1d | 2009-08-25 17:26:32 +0000 | [diff] [blame] | 850 | BasicBlock* pred = FindPhiPredForUseInBlock(Reloads[out], |
| 851 | OldTarget); |
| 852 | if (pred && DT && DT->dominates(DefBlock, pred)) |
| 853 | DominatesDef = true; |
| Owen Anderson | 34e6148 | 2009-08-25 00:54:39 +0000 | [diff] [blame] | 854 | } |
| Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 855 | |
| 856 | if (DominatesDef) { |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 857 | if (AggregateArgs) { |
| David Greene | c656cbb | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 858 | Value *Idx[2]; |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 859 | Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 860 | Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), |
| 861 | FirstOut+out); |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 862 | GetElementPtrInst *GEP = GetElementPtrInst::Create( |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 863 | StructArgTy, &*OAI, Idx, "gep_" + outputs[out]->getName(), |
| David Blaikie | 741c8f8 | 2015-03-14 01:53:18 +0000 | [diff] [blame] | 864 | NTRet); |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 865 | new StoreInst(outputs[out], GEP, NTRet); |
| Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 866 | } else { |
| Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 867 | new StoreInst(outputs[out], &*OAI, NTRet); |
| Chris Lattner | 9b0291b | 2004-11-13 00:06:45 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 870 | // Advance output iterator even if we don't emit a store |
| 871 | if (!AggregateArgs) ++OAI; |
| 872 | } |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 873 | } |
| Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 874 | |
| 875 | // rewrite the original branch instruction with this new target |
| 876 | TI->setSuccessor(i, NewTarget); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 877 | } |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 878 | } |
| Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 879 | |
| Chris Lattner | 3d1ca67 | 2004-05-12 03:22:33 +0000 | [diff] [blame] | 880 | // Now that we've done the deed, simplify the switch instruction. |
| Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 881 | Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType(); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 882 | switch (NumExitBlocks) { |
| 883 | case 0: |
| Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 884 | // There are no successors (the block containing the switch itself), which |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 885 | // means that previously this was the last part of the function, and hence |
| 886 | // this should be rewritten as a `ret' |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 887 | |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 888 | // Check if the function should return a value |
| Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 889 | if (OldFnRetTy->isVoidTy()) { |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 890 | ReturnInst::Create(Context, nullptr, TheSwitch); // Return void |
| Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 891 | } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) { |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 892 | // return what we have |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 893 | ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch); |
| Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 894 | } else { |
| 895 | // Otherwise we must have code extracted an unwind or something, just |
| 896 | // return whatever we want. |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 897 | ReturnInst::Create(Context, |
| 898 | Constant::getNullValue(OldFnRetTy), TheSwitch); |
| Chris Lattner | 7f1c7ed | 2004-08-12 03:17:02 +0000 | [diff] [blame] | 899 | } |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 900 | |
| Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 901 | TheSwitch->eraseFromParent(); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 902 | break; |
| 903 | case 1: |
| 904 | // Only a single destination, change the switch into an unconditional |
| 905 | // branch. |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 906 | BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch); |
| Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 907 | TheSwitch->eraseFromParent(); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 908 | break; |
| 909 | case 2: |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 910 | BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2), |
| 911 | call, TheSwitch); |
| Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 912 | TheSwitch->eraseFromParent(); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 913 | break; |
| 914 | default: |
| 915 | // Otherwise, make the default destination of the switch instruction be one |
| 916 | // of the other successors. |
| Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 917 | TheSwitch->setCondition(call); |
| 918 | TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks)); |
| Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 919 | // Remove redundant case |
| Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 920 | TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1)); |
| Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 921 | break; |
| Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 922 | } |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 925 | void CodeExtractor::moveCodeToFunction(Function *newFunction) { |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 926 | Function *oldFunc = (*Blocks.begin())->getParent(); |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 927 | Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList(); |
| 928 | Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList(); |
| 929 | |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 930 | for (BasicBlock *Block : Blocks) { |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 931 | // 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] | 932 | oldBlocks.remove(Block); |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 933 | |
| 934 | // Insert this basic block into the new function |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 935 | newBlocks.push_back(Block); |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 938 | |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 939 | void CodeExtractor::calculateNewCallTerminatorWeights( |
| 940 | BasicBlock *CodeReplacer, |
| 941 | DenseMap<BasicBlock *, BlockFrequency> &ExitWeights, |
| 942 | BranchProbabilityInfo *BPI) { |
| 943 | typedef BlockFrequencyInfoImplBase::Distribution Distribution; |
| 944 | typedef BlockFrequencyInfoImplBase::BlockNode BlockNode; |
| 945 | |
| 946 | // Update the branch weights for the exit block. |
| 947 | TerminatorInst *TI = CodeReplacer->getTerminator(); |
| 948 | SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0); |
| 949 | |
| 950 | // Block Frequency distribution with dummy node. |
| 951 | Distribution BranchDist; |
| 952 | |
| 953 | // Add each of the frequencies of the successors. |
| 954 | for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) { |
| 955 | BlockNode ExitNode(i); |
| 956 | uint64_t ExitFreq = ExitWeights[TI->getSuccessor(i)].getFrequency(); |
| 957 | if (ExitFreq != 0) |
| 958 | BranchDist.addExit(ExitNode, ExitFreq); |
| 959 | else |
| 960 | BPI->setEdgeProbability(CodeReplacer, i, BranchProbability::getZero()); |
| 961 | } |
| 962 | |
| 963 | // Check for no total weight. |
| 964 | if (BranchDist.Total == 0) |
| 965 | return; |
| 966 | |
| 967 | // Normalize the distribution so that they can fit in unsigned. |
| 968 | BranchDist.normalize(); |
| 969 | |
| 970 | // Create normalized branch weights and set the metadata. |
| 971 | for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) { |
| 972 | const auto &Weight = BranchDist.Weights[I]; |
| 973 | |
| 974 | // Get the weight and update the current BFI. |
| 975 | BranchWeights[Weight.TargetNode.Index] = Weight.Amount; |
| 976 | BranchProbability BP(Weight.Amount, BranchDist.Total); |
| 977 | BPI->setEdgeProbability(CodeReplacer, Weight.TargetNode.Index, BP); |
| 978 | } |
| 979 | TI->setMetadata( |
| 980 | LLVMContext::MD_prof, |
| 981 | MDBuilder(TI->getContext()).createBranchWeights(BranchWeights)); |
| 982 | } |
| 983 | |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 984 | Function *CodeExtractor::extractCodeRegion() { |
| 985 | if (!isEligible()) |
| Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 986 | return nullptr; |
| Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 987 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 988 | ValueSet inputs, outputs, SinkingCands, HoistingCands; |
| 989 | BasicBlock *CommonExit = nullptr; |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 990 | |
| 991 | // 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] | 992 | // block in the region. |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 993 | BasicBlock *header = *Blocks.begin(); |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 994 | |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 995 | // Calculate the entry frequency of the new function before we change the root |
| 996 | // block. |
| 997 | BlockFrequency EntryFreq; |
| 998 | if (BFI) { |
| 999 | assert(BPI && "Both BPI and BFI are required to preserve profile info"); |
| 1000 | for (BasicBlock *Pred : predecessors(header)) { |
| 1001 | if (Blocks.count(Pred)) |
| 1002 | continue; |
| 1003 | EntryFreq += |
| 1004 | BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 1008 | // 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] | 1009 | severSplitPHINodes(header); |
| 1010 | |
| Chris Lattner | 13d2ddf | 2004-05-12 16:07:41 +0000 | [diff] [blame] | 1011 | // If we have any return instructions in the region, split those blocks so |
| 1012 | // that the return is not in the region. |
| 1013 | splitReturnBlocks(); |
| 1014 | |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1015 | Function *oldFunction = header->getParent(); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1016 | |
| 1017 | // This takes place of the original loop |
| Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1018 | BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(), |
| 1019 | "codeRepl", oldFunction, |
| Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1020 | header); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1021 | |
| 1022 | // 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] | 1023 | // 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] | 1024 | BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(), |
| 1025 | "newFuncRoot"); |
| Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 1026 | newFuncRoot->getInstList().push_back(BranchInst::Create(header)); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1027 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 1028 | findAllocas(SinkingCands, HoistingCands, CommonExit); |
| 1029 | assert(HoistingCands.empty() || CommonExit); |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 1030 | |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 1031 | // Find inputs to, outputs from the code region. |
| Xinliang David Li | 74480ad | 2017-05-30 21:22:18 +0000 | [diff] [blame] | 1032 | findInputsOutputs(inputs, outputs, SinkingCands); |
| 1033 | |
| 1034 | // Now sink all instructions which only have non-phi uses inside the region |
| 1035 | for (auto *II : SinkingCands) |
| 1036 | cast<Instruction>(II)->moveBefore(*newFuncRoot, |
| 1037 | newFuncRoot->getFirstInsertionPt()); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1038 | |
| Xinliang David Li | 7ed6cd3 | 2017-06-11 20:46:05 +0000 | [diff] [blame] | 1039 | if (!HoistingCands.empty()) { |
| 1040 | auto *HoistToBlock = findOrCreateBlockForHoisting(CommonExit); |
| 1041 | Instruction *TI = HoistToBlock->getTerminator(); |
| 1042 | for (auto *II : HoistingCands) |
| 1043 | cast<Instruction>(II)->moveBefore(TI); |
| 1044 | } |
| 1045 | |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 1046 | // Calculate the exit blocks for the extracted region and the total exit |
| 1047 | // weights for each of those blocks. |
| 1048 | DenseMap<BasicBlock *, BlockFrequency> ExitWeights; |
| Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 1049 | SmallPtrSet<BasicBlock *, 1> ExitBlocks; |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 1050 | for (BasicBlock *Block : Blocks) { |
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1051 | for (succ_iterator SI = succ_begin(Block), SE = succ_end(Block); SI != SE; |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 1052 | ++SI) { |
| 1053 | if (!Blocks.count(*SI)) { |
| 1054 | // Update the branch weight for this successor. |
| 1055 | if (BFI) { |
| 1056 | BlockFrequency &BF = ExitWeights[*SI]; |
| 1057 | BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, *SI); |
| 1058 | } |
| Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 1059 | ExitBlocks.insert(*SI); |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | } |
| Chandler Carruth | 14316fc | 2012-05-04 11:20:27 +0000 | [diff] [blame] | 1063 | NumExitBlocks = ExitBlocks.size(); |
| 1064 | |
| Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame] | 1065 | // Construct new function based on inputs/outputs & add allocas for all defs. |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 1066 | Function *newFunction = constructFunction(inputs, outputs, header, |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1067 | newFuncRoot, |
| Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 1068 | codeReplacer, oldFunction, |
| 1069 | oldFunction->getParent()); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1070 | |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 1071 | // Update the entry count of the function. |
| 1072 | if (BFI) { |
| 1073 | Optional<uint64_t> EntryCount = |
| 1074 | BFI->getProfileCountFromFreq(EntryFreq.getFrequency()); |
| 1075 | if (EntryCount.hasValue()) |
| 1076 | newFunction->setEntryCount(EntryCount.getValue()); |
| 1077 | BFI->setBlockFreq(codeReplacer, EntryFreq.getFrequency()); |
| 1078 | } |
| 1079 | |
| Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 1080 | emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1081 | |
| Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 1082 | moveCodeToFunction(newFunction); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1083 | |
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 1084 | // Update the branch weights for the exit block. |
| 1085 | if (BFI && NumExitBlocks > 1) |
| 1086 | calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI); |
| 1087 | |
| Chris Lattner | 795c993 | 2004-05-12 15:29:13 +0000 | [diff] [blame] | 1088 | // 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] | 1089 | // references to the old incoming edge to be the new incoming edge. |
| Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 1090 | for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) { |
| 1091 | PHINode *PN = cast<PHINode>(I); |
| Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 1092 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 1093 | if (!Blocks.count(PN->getIncomingBlock(i))) |
| Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 1094 | PN->setIncomingBlock(i, newFuncRoot); |
| Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 1095 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1096 | |
| Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 1097 | // Look at all successors of the codeReplacer block. If any of these blocks |
| 1098 | // had PHI nodes in them, we need to update the "from" block to be the code |
| 1099 | // replacer, not the original block in the extracted region. |
| 1100 | std::vector<BasicBlock*> Succs(succ_begin(codeReplacer), |
| 1101 | succ_end(codeReplacer)); |
| 1102 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) |
| Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 1103 | for (BasicBlock::iterator I = Succs[i]->begin(); isa<PHINode>(I); ++I) { |
| 1104 | PHINode *PN = cast<PHINode>(I); |
| Chris Lattner | 5627382 | 2004-08-13 03:27:07 +0000 | [diff] [blame] | 1105 | std::set<BasicBlock*> ProcessedPreds; |
| Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 1106 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 1107 | if (Blocks.count(PN->getIncomingBlock(i))) { |
| Chris Lattner | 5627382 | 2004-08-13 03:27:07 +0000 | [diff] [blame] | 1108 | if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second) |
| 1109 | PN->setIncomingBlock(i, codeReplacer); |
| 1110 | else { |
| 1111 | // There were multiple entries in the PHI for this block, now there |
| 1112 | // is only one, so remove the duplicated entries. |
| 1113 | PN->removeIncomingValue(i, false); |
| 1114 | --i; --e; |
| 1115 | } |
| Anton Korobeynikov | 1bfd121 | 2008-02-20 11:26:25 +0000 | [diff] [blame] | 1116 | } |
| Chris Lattner | 5627382 | 2004-08-13 03:27:07 +0000 | [diff] [blame] | 1117 | } |
| Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1118 | |
| Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 1119 | DEBUG(if (verifyFunction(*newFunction)) |
| Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 1120 | report_fatal_error("verifyFunction failed!")); |
| Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1121 | return newFunction; |
| 1122 | } |