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