1 //===- CodeExtractor.cpp - Pull code region into a new function -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the interface to tear out a code region, such as an 10 // individual loop or a parallel section, into a new function, replacing it with 11 // a call to the new function. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Utils/CodeExtractor.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SetVector.h" 20 #include "llvm/ADT/SmallPtrSet.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/Analysis/AssumptionCache.h" 23 #include "llvm/Analysis/BlockFrequencyInfo.h" 24 #include "llvm/Analysis/BlockFrequencyInfoImpl.h" 25 #include "llvm/Analysis/BranchProbabilityInfo.h" 26 #include "llvm/IR/Argument.h" 27 #include "llvm/IR/Attributes.h" 28 #include "llvm/IR/BasicBlock.h" 29 #include "llvm/IR/CFG.h" 30 #include "llvm/IR/Constant.h" 31 #include "llvm/IR/Constants.h" 32 #include "llvm/IR/DIBuilder.h" 33 #include "llvm/IR/DataLayout.h" 34 #include "llvm/IR/DebugInfo.h" 35 #include "llvm/IR/DebugInfoMetadata.h" 36 #include "llvm/IR/DerivedTypes.h" 37 #include "llvm/IR/Dominators.h" 38 #include "llvm/IR/Function.h" 39 #include "llvm/IR/GlobalValue.h" 40 #include "llvm/IR/InstIterator.h" 41 #include "llvm/IR/InstrTypes.h" 42 #include "llvm/IR/Instruction.h" 43 #include "llvm/IR/Instructions.h" 44 #include "llvm/IR/IntrinsicInst.h" 45 #include "llvm/IR/Intrinsics.h" 46 #include "llvm/IR/LLVMContext.h" 47 #include "llvm/IR/MDBuilder.h" 48 #include "llvm/IR/Module.h" 49 #include "llvm/IR/PatternMatch.h" 50 #include "llvm/IR/Type.h" 51 #include "llvm/IR/User.h" 52 #include "llvm/IR/Value.h" 53 #include "llvm/IR/Verifier.h" 54 #include "llvm/Support/BlockFrequency.h" 55 #include "llvm/Support/BranchProbability.h" 56 #include "llvm/Support/Casting.h" 57 #include "llvm/Support/CommandLine.h" 58 #include "llvm/Support/Debug.h" 59 #include "llvm/Support/ErrorHandling.h" 60 #include "llvm/Support/raw_ostream.h" 61 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 62 #include <cassert> 63 #include <cstdint> 64 #include <iterator> 65 #include <map> 66 #include <utility> 67 #include <vector> 68 69 using namespace llvm; 70 using namespace llvm::PatternMatch; 71 using ProfileCount = Function::ProfileCount; 72 73 #define DEBUG_TYPE "code-extractor" 74 75 // Provide a command-line option to aggregate function arguments into a struct 76 // for functions produced by the code extractor. This is useful when converting 77 // extracted functions to pthread-based code, as only one argument (void*) can 78 // be passed in to pthread_create(). 79 static cl::opt<bool> 80 AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, 81 cl::desc("Aggregate arguments to code-extracted functions")); 82 83 /// Test whether a block is valid for extraction. 84 static bool isBlockValidForExtraction(const BasicBlock &BB, 85 const SetVector<BasicBlock *> &Result, 86 bool AllowVarArgs, bool AllowAlloca) { 87 // taking the address of a basic block moved to another function is illegal 88 if (BB.hasAddressTaken()) 89 return false; 90 91 // don't hoist code that uses another basicblock address, as it's likely to 92 // lead to unexpected behavior, like cross-function jumps 93 SmallPtrSet<User const *, 16> Visited; 94 SmallVector<User const *, 16> ToVisit; 95 96 for (Instruction const &Inst : BB) 97 ToVisit.push_back(&Inst); 98 99 while (!ToVisit.empty()) { 100 User const *Curr = ToVisit.pop_back_val(); 101 if (!Visited.insert(Curr).second) 102 continue; 103 if (isa<BlockAddress const>(Curr)) 104 return false; // even a reference to self is likely to be not compatible 105 106 if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB) 107 continue; 108 109 for (auto const &U : Curr->operands()) { 110 if (auto *UU = dyn_cast<User>(U)) 111 ToVisit.push_back(UU); 112 } 113 } 114 115 // If explicitly requested, allow vastart and alloca. For invoke instructions 116 // verify that extraction is valid. 117 for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) { 118 if (isa<AllocaInst>(I)) { 119 if (!AllowAlloca) 120 return false; 121 continue; 122 } 123 124 if (const auto *II = dyn_cast<InvokeInst>(I)) { 125 // Unwind destination (either a landingpad, catchswitch, or cleanuppad) 126 // must be a part of the subgraph which is being extracted. 127 if (auto *UBB = II->getUnwindDest()) 128 if (!Result.count(UBB)) 129 return false; 130 continue; 131 } 132 133 // All catch handlers of a catchswitch instruction as well as the unwind 134 // destination must be in the subgraph. 135 if (const auto *CSI = dyn_cast<CatchSwitchInst>(I)) { 136 if (auto *UBB = CSI->getUnwindDest()) 137 if (!Result.count(UBB)) 138 return false; 139 for (const auto *HBB : CSI->handlers()) 140 if (!Result.count(const_cast<BasicBlock*>(HBB))) 141 return false; 142 continue; 143 } 144 145 // Make sure that entire catch handler is within subgraph. It is sufficient 146 // to check that catch return's block is in the list. 147 if (const auto *CPI = dyn_cast<CatchPadInst>(I)) { 148 for (const auto *U : CPI->users()) 149 if (const auto *CRI = dyn_cast<CatchReturnInst>(U)) 150 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent()))) 151 return false; 152 continue; 153 } 154 155 // And do similar checks for cleanup handler - the entire handler must be 156 // in subgraph which is going to be extracted. For cleanup return should 157 // additionally check that the unwind destination is also in the subgraph. 158 if (const auto *CPI = dyn_cast<CleanupPadInst>(I)) { 159 for (const auto *U : CPI->users()) 160 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U)) 161 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent()))) 162 return false; 163 continue; 164 } 165 if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) { 166 if (auto *UBB = CRI->getUnwindDest()) 167 if (!Result.count(UBB)) 168 return false; 169 continue; 170 } 171 172 if (const CallInst *CI = dyn_cast<CallInst>(I)) { 173 if (const Function *F = CI->getCalledFunction()) { 174 auto IID = F->getIntrinsicID(); 175 if (IID == Intrinsic::vastart) { 176 if (AllowVarArgs) 177 continue; 178 else 179 return false; 180 } 181 182 // Currently, we miscompile outlined copies of eh_typid_for. There are 183 // proposals for fixing this in llvm.org/PR39545. 184 if (IID == Intrinsic::eh_typeid_for) 185 return false; 186 } 187 } 188 } 189 190 return true; 191 } 192 193 /// Build a set of blocks to extract if the input blocks are viable. 194 static SetVector<BasicBlock *> 195 buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs, DominatorTree *DT, 196 bool AllowVarArgs, bool AllowAlloca) { 197 assert(!BBs.empty() && "The set of blocks to extract must be non-empty"); 198 SetVector<BasicBlock *> Result; 199 200 // Loop over the blocks, adding them to our set-vector, and aborting with an 201 // empty set if we encounter invalid blocks. 202 for (BasicBlock *BB : BBs) { 203 // If this block is dead, don't process it. 204 if (DT && !DT->isReachableFromEntry(BB)) 205 continue; 206 207 if (!Result.insert(BB)) 208 llvm_unreachable("Repeated basic blocks in extraction input"); 209 } 210 211 LLVM_DEBUG(dbgs() << "Region front block: " << Result.front()->getName() 212 << '\n'); 213 214 for (auto *BB : Result) { 215 if (!isBlockValidForExtraction(*BB, Result, AllowVarArgs, AllowAlloca)) 216 return {}; 217 218 // Make sure that the first block is not a landing pad. 219 if (BB == Result.front()) { 220 if (BB->isEHPad()) { 221 LLVM_DEBUG(dbgs() << "The first block cannot be an unwind block\n"); 222 return {}; 223 } 224 continue; 225 } 226 227 // All blocks other than the first must not have predecessors outside of 228 // the subgraph which is being extracted. 229 for (auto *PBB : predecessors(BB)) 230 if (!Result.count(PBB)) { 231 LLVM_DEBUG(dbgs() << "No blocks in this region may have entries from " 232 "outside the region except for the first block!\n" 233 << "Problematic source BB: " << BB->getName() << "\n" 234 << "Problematic destination BB: " << PBB->getName() 235 << "\n"); 236 return {}; 237 } 238 } 239 240 return Result; 241 } 242 243 CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT, 244 bool AggregateArgs, BlockFrequencyInfo *BFI, 245 BranchProbabilityInfo *BPI, AssumptionCache *AC, 246 bool AllowVarArgs, bool AllowAlloca, 247 BasicBlock *AllocationBlock, std::string Suffix, 248 bool ArgsInZeroAddressSpace) 249 : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI), 250 BPI(BPI), AC(AC), AllocationBlock(AllocationBlock), 251 AllowVarArgs(AllowVarArgs), 252 Blocks(buildExtractionBlockSet(BBs, DT, AllowVarArgs, AllowAlloca)), 253 Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace) {} 254 255 /// definedInRegion - Return true if the specified value is defined in the 256 /// extracted region. 257 static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) { 258 if (Instruction *I = dyn_cast<Instruction>(V)) 259 if (Blocks.count(I->getParent())) 260 return true; 261 return false; 262 } 263 264 /// definedInCaller - Return true if the specified value is defined in the 265 /// function being code extracted, but not in the region being extracted. 266 /// These values must be passed in as live-ins to the function. 267 static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) { 268 if (isa<Argument>(V)) return true; 269 if (Instruction *I = dyn_cast<Instruction>(V)) 270 if (!Blocks.count(I->getParent())) 271 return true; 272 return false; 273 } 274 275 static BasicBlock *getCommonExitBlock(const SetVector<BasicBlock *> &Blocks) { 276 BasicBlock *CommonExitBlock = nullptr; 277 auto hasNonCommonExitSucc = [&](BasicBlock *Block) { 278 for (auto *Succ : successors(Block)) { 279 // Internal edges, ok. 280 if (Blocks.count(Succ)) 281 continue; 282 if (!CommonExitBlock) { 283 CommonExitBlock = Succ; 284 continue; 285 } 286 if (CommonExitBlock != Succ) 287 return true; 288 } 289 return false; 290 }; 291 292 if (any_of(Blocks, hasNonCommonExitSucc)) 293 return nullptr; 294 295 return CommonExitBlock; 296 } 297 298 CodeExtractorAnalysisCache::CodeExtractorAnalysisCache(Function &F) { 299 for (BasicBlock &BB : F) { 300 for (Instruction &II : BB.instructionsWithoutDebug()) 301 if (auto *AI = dyn_cast<AllocaInst>(&II)) 302 Allocas.push_back(AI); 303 304 findSideEffectInfoForBlock(BB); 305 } 306 } 307 308 void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(BasicBlock &BB) { 309 for (Instruction &II : BB.instructionsWithoutDebug()) { 310 unsigned Opcode = II.getOpcode(); 311 Value *MemAddr = nullptr; 312 switch (Opcode) { 313 case Instruction::Store: 314 case Instruction::Load: { 315 if (Opcode == Instruction::Store) { 316 StoreInst *SI = cast<StoreInst>(&II); 317 MemAddr = SI->getPointerOperand(); 318 } else { 319 LoadInst *LI = cast<LoadInst>(&II); 320 MemAddr = LI->getPointerOperand(); 321 } 322 // Global variable can not be aliased with locals. 323 if (isa<Constant>(MemAddr)) 324 break; 325 Value *Base = MemAddr->stripInBoundsConstantOffsets(); 326 if (!isa<AllocaInst>(Base)) { 327 SideEffectingBlocks.insert(&BB); 328 return; 329 } 330 BaseMemAddrs[&BB].insert(Base); 331 break; 332 } 333 default: { 334 IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(&II); 335 if (IntrInst) { 336 if (IntrInst->isLifetimeStartOrEnd()) 337 break; 338 SideEffectingBlocks.insert(&BB); 339 return; 340 } 341 // Treat all the other cases conservatively if it has side effects. 342 if (II.mayHaveSideEffects()) { 343 SideEffectingBlocks.insert(&BB); 344 return; 345 } 346 } 347 } 348 } 349 } 350 351 bool CodeExtractorAnalysisCache::doesBlockContainClobberOfAddr( 352 BasicBlock &BB, AllocaInst *Addr) const { 353 if (SideEffectingBlocks.count(&BB)) 354 return true; 355 auto It = BaseMemAddrs.find(&BB); 356 if (It != BaseMemAddrs.end()) 357 return It->second.count(Addr); 358 return false; 359 } 360 361 bool CodeExtractor::isLegalToShrinkwrapLifetimeMarkers( 362 const CodeExtractorAnalysisCache &CEAC, Instruction *Addr) const { 363 AllocaInst *AI = cast<AllocaInst>(Addr->stripInBoundsConstantOffsets()); 364 Function *Func = (*Blocks.begin())->getParent(); 365 for (BasicBlock &BB : *Func) { 366 if (Blocks.count(&BB)) 367 continue; 368 if (CEAC.doesBlockContainClobberOfAddr(BB, AI)) 369 return false; 370 } 371 return true; 372 } 373 374 BasicBlock * 375 CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) { 376 BasicBlock *SinglePredFromOutlineRegion = nullptr; 377 assert(!Blocks.count(CommonExitBlock) && 378 "Expect a block outside the region!"); 379 for (auto *Pred : predecessors(CommonExitBlock)) { 380 if (!Blocks.count(Pred)) 381 continue; 382 if (!SinglePredFromOutlineRegion) { 383 SinglePredFromOutlineRegion = Pred; 384 } else if (SinglePredFromOutlineRegion != Pred) { 385 SinglePredFromOutlineRegion = nullptr; 386 break; 387 } 388 } 389 390 if (SinglePredFromOutlineRegion) 391 return SinglePredFromOutlineRegion; 392 393 #ifndef NDEBUG 394 auto getFirstPHI = [](BasicBlock *BB) { 395 BasicBlock::iterator I = BB->begin(); 396 PHINode *FirstPhi = nullptr; 397 while (I != BB->end()) { 398 PHINode *Phi = dyn_cast<PHINode>(I); 399 if (!Phi) 400 break; 401 if (!FirstPhi) { 402 FirstPhi = Phi; 403 break; 404 } 405 } 406 return FirstPhi; 407 }; 408 // If there are any phi nodes, the single pred either exists or has already 409 // be created before code extraction. 410 assert(!getFirstPHI(CommonExitBlock) && "Phi not expected"); 411 #endif 412 413 BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock( 414 CommonExitBlock->getFirstNonPHI()->getIterator()); 415 416 for (BasicBlock *Pred : 417 llvm::make_early_inc_range(predecessors(CommonExitBlock))) { 418 if (Blocks.count(Pred)) 419 continue; 420 Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock); 421 } 422 // Now add the old exit block to the outline region. 423 Blocks.insert(CommonExitBlock); 424 return CommonExitBlock; 425 } 426 427 // Find the pair of life time markers for address 'Addr' that are either 428 // defined inside the outline region or can legally be shrinkwrapped into the 429 // outline region. If there are not other untracked uses of the address, return 430 // the pair of markers if found; otherwise return a pair of nullptr. 431 CodeExtractor::LifetimeMarkerInfo 432 CodeExtractor::getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, 433 Instruction *Addr, 434 BasicBlock *ExitBlock) const { 435 LifetimeMarkerInfo Info; 436 437 for (User *U : Addr->users()) { 438 IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(U); 439 if (IntrInst) { 440 // We don't model addresses with multiple start/end markers, but the 441 // markers do not need to be in the region. 442 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) { 443 if (Info.LifeStart) 444 return {}; 445 Info.LifeStart = IntrInst; 446 continue; 447 } 448 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) { 449 if (Info.LifeEnd) 450 return {}; 451 Info.LifeEnd = IntrInst; 452 continue; 453 } 454 // At this point, permit debug uses outside of the region. 455 // This is fixed in a later call to fixupDebugInfoPostExtraction(). 456 if (isa<DbgInfoIntrinsic>(IntrInst)) 457 continue; 458 } 459 // Find untracked uses of the address, bail. 460 if (!definedInRegion(Blocks, U)) 461 return {}; 462 } 463 464 if (!Info.LifeStart || !Info.LifeEnd) 465 return {}; 466 467 Info.SinkLifeStart = !definedInRegion(Blocks, Info.LifeStart); 468 Info.HoistLifeEnd = !definedInRegion(Blocks, Info.LifeEnd); 469 // Do legality check. 470 if ((Info.SinkLifeStart || Info.HoistLifeEnd) && 471 !isLegalToShrinkwrapLifetimeMarkers(CEAC, Addr)) 472 return {}; 473 474 // Check to see if we have a place to do hoisting, if not, bail. 475 if (Info.HoistLifeEnd && !ExitBlock) 476 return {}; 477 478 return Info; 479 } 480 481 void CodeExtractor::findAllocas(const CodeExtractorAnalysisCache &CEAC, 482 ValueSet &SinkCands, ValueSet &HoistCands, 483 BasicBlock *&ExitBlock) const { 484 Function *Func = (*Blocks.begin())->getParent(); 485 ExitBlock = getCommonExitBlock(Blocks); 486 487 auto moveOrIgnoreLifetimeMarkers = 488 [&](const LifetimeMarkerInfo &LMI) -> bool { 489 if (!LMI.LifeStart) 490 return false; 491 if (LMI.SinkLifeStart) { 492 LLVM_DEBUG(dbgs() << "Sinking lifetime.start: " << *LMI.LifeStart 493 << "\n"); 494 SinkCands.insert(LMI.LifeStart); 495 } 496 if (LMI.HoistLifeEnd) { 497 LLVM_DEBUG(dbgs() << "Hoisting lifetime.end: " << *LMI.LifeEnd << "\n"); 498 HoistCands.insert(LMI.LifeEnd); 499 } 500 return true; 501 }; 502 503 // Look up allocas in the original function in CodeExtractorAnalysisCache, as 504 // this is much faster than walking all the instructions. 505 for (AllocaInst *AI : CEAC.getAllocas()) { 506 BasicBlock *BB = AI->getParent(); 507 if (Blocks.count(BB)) 508 continue; 509 510 // As a prior call to extractCodeRegion() may have shrinkwrapped the alloca, 511 // check whether it is actually still in the original function. 512 Function *AIFunc = BB->getParent(); 513 if (AIFunc != Func) 514 continue; 515 516 LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock); 517 bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo); 518 if (Moved) { 519 LLVM_DEBUG(dbgs() << "Sinking alloca: " << *AI << "\n"); 520 SinkCands.insert(AI); 521 continue; 522 } 523 524 // Find bitcasts in the outlined region that have lifetime marker users 525 // outside that region. Replace the lifetime marker use with an 526 // outside region bitcast to avoid unnecessary alloca/reload instructions 527 // and extra lifetime markers. 528 SmallVector<Instruction *, 2> LifetimeBitcastUsers; 529 for (User *U : AI->users()) { 530 if (!definedInRegion(Blocks, U)) 531 continue; 532 533 if (U->stripInBoundsConstantOffsets() != AI) 534 continue; 535 536 Instruction *Bitcast = cast<Instruction>(U); 537 for (User *BU : Bitcast->users()) { 538 IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(BU); 539 if (!IntrInst) 540 continue; 541 542 if (!IntrInst->isLifetimeStartOrEnd()) 543 continue; 544 545 if (definedInRegion(Blocks, IntrInst)) 546 continue; 547 548 LLVM_DEBUG(dbgs() << "Replace use of extracted region bitcast" 549 << *Bitcast << " in out-of-region lifetime marker " 550 << *IntrInst << "\n"); 551 LifetimeBitcastUsers.push_back(IntrInst); 552 } 553 } 554 555 for (Instruction *I : LifetimeBitcastUsers) { 556 Module *M = AIFunc->getParent(); 557 LLVMContext &Ctx = M->getContext(); 558 auto *Int8PtrTy = PointerType::getUnqual(Ctx); 559 CastInst *CastI = 560 CastInst::CreatePointerCast(AI, Int8PtrTy, "lt.cast", I->getIterator()); 561 I->replaceUsesOfWith(I->getOperand(1), CastI); 562 } 563 564 // Follow any bitcasts. 565 SmallVector<Instruction *, 2> Bitcasts; 566 SmallVector<LifetimeMarkerInfo, 2> BitcastLifetimeInfo; 567 for (User *U : AI->users()) { 568 if (U->stripInBoundsConstantOffsets() == AI) { 569 Instruction *Bitcast = cast<Instruction>(U); 570 LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock); 571 if (LMI.LifeStart) { 572 Bitcasts.push_back(Bitcast); 573 BitcastLifetimeInfo.push_back(LMI); 574 continue; 575 } 576 } 577 578 // Found unknown use of AI. 579 if (!definedInRegion(Blocks, U)) { 580 Bitcasts.clear(); 581 break; 582 } 583 } 584 585 // Either no bitcasts reference the alloca or there are unknown uses. 586 if (Bitcasts.empty()) 587 continue; 588 589 LLVM_DEBUG(dbgs() << "Sinking alloca (via bitcast): " << *AI << "\n"); 590 SinkCands.insert(AI); 591 for (unsigned I = 0, E = Bitcasts.size(); I != E; ++I) { 592 Instruction *BitcastAddr = Bitcasts[I]; 593 const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[I]; 594 assert(LMI.LifeStart && 595 "Unsafe to sink bitcast without lifetime markers"); 596 moveOrIgnoreLifetimeMarkers(LMI); 597 if (!definedInRegion(Blocks, BitcastAddr)) { 598 LLVM_DEBUG(dbgs() << "Sinking bitcast-of-alloca: " << *BitcastAddr 599 << "\n"); 600 SinkCands.insert(BitcastAddr); 601 } 602 } 603 } 604 } 605 606 bool CodeExtractor::isEligible() const { 607 if (Blocks.empty()) 608 return false; 609 BasicBlock *Header = *Blocks.begin(); 610 Function *F = Header->getParent(); 611 612 // For functions with varargs, check that varargs handling is only done in the 613 // outlined function, i.e vastart and vaend are only used in outlined blocks. 614 if (AllowVarArgs && F->getFunctionType()->isVarArg()) { 615 auto containsVarArgIntrinsic = [](const Instruction &I) { 616 if (const CallInst *CI = dyn_cast<CallInst>(&I)) 617 if (const Function *Callee = CI->getCalledFunction()) 618 return Callee->getIntrinsicID() == Intrinsic::vastart || 619 Callee->getIntrinsicID() == Intrinsic::vaend; 620 return false; 621 }; 622 623 for (auto &BB : *F) { 624 if (Blocks.count(&BB)) 625 continue; 626 if (llvm::any_of(BB, containsVarArgIntrinsic)) 627 return false; 628 } 629 } 630 // stacksave as input implies stackrestore in the outlined function. 631 // This can confuse prolog epilog insertion phase. 632 // stacksave's uses must not cross outlined function. 633 for (BasicBlock *BB : Blocks) { 634 for (Instruction &I : *BB) { 635 IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); 636 if (!II) 637 continue; 638 bool IsSave = II->getIntrinsicID() == Intrinsic::stacksave; 639 bool IsRestore = II->getIntrinsicID() == Intrinsic::stackrestore; 640 if (IsSave && any_of(II->users(), [&Blks = this->Blocks](User *U) { 641 return !definedInRegion(Blks, U); 642 })) 643 return false; 644 if (IsRestore && !definedInRegion(Blocks, II->getArgOperand(0))) 645 return false; 646 } 647 } 648 return true; 649 } 650 651 void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, 652 const ValueSet &SinkCands, 653 bool CollectGlobalInputs) const { 654 for (BasicBlock *BB : Blocks) { 655 // If a used value is defined outside the region, it's an input. If an 656 // instruction is used outside the region, it's an output. 657 for (Instruction &II : *BB) { 658 for (auto &OI : II.operands()) { 659 Value *V = OI; 660 if (!SinkCands.count(V) && 661 (definedInCaller(Blocks, V) || 662 (CollectGlobalInputs && llvm::isa<llvm::GlobalVariable>(V)))) 663 Inputs.insert(V); 664 } 665 666 for (User *U : II.users()) 667 if (!definedInRegion(Blocks, U)) { 668 Outputs.insert(&II); 669 break; 670 } 671 } 672 } 673 } 674 675 /// severSplitPHINodesOfEntry - If a PHI node has multiple inputs from outside 676 /// of the region, we need to split the entry block of the region so that the 677 /// PHI node is easier to deal with. 678 void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) { 679 unsigned NumPredsFromRegion = 0; 680 unsigned NumPredsOutsideRegion = 0; 681 682 if (Header != &Header->getParent()->getEntryBlock()) { 683 PHINode *PN = dyn_cast<PHINode>(Header->begin()); 684 if (!PN) return; // No PHI nodes. 685 686 // If the header node contains any PHI nodes, check to see if there is more 687 // than one entry from outside the region. If so, we need to sever the 688 // header block into two. 689 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 690 if (Blocks.count(PN->getIncomingBlock(i))) 691 ++NumPredsFromRegion; 692 else 693 ++NumPredsOutsideRegion; 694 695 // If there is one (or fewer) predecessor from outside the region, we don't 696 // need to do anything special. 697 if (NumPredsOutsideRegion <= 1) return; 698 } 699 700 // Otherwise, we need to split the header block into two pieces: one 701 // containing PHI nodes merging values from outside of the region, and a 702 // second that contains all of the code for the block and merges back any 703 // incoming values from inside of the region. 704 BasicBlock *NewBB = SplitBlock(Header, Header->getFirstNonPHI(), DT); 705 706 // We only want to code extract the second block now, and it becomes the new 707 // header of the region. 708 BasicBlock *OldPred = Header; 709 Blocks.remove(OldPred); 710 Blocks.insert(NewBB); 711 Header = NewBB; 712 713 // Okay, now we need to adjust the PHI nodes and any branches from within the 714 // region to go to the new header block instead of the old header block. 715 if (NumPredsFromRegion) { 716 PHINode *PN = cast<PHINode>(OldPred->begin()); 717 // Loop over all of the predecessors of OldPred that are in the region, 718 // changing them to branch to NewBB instead. 719 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 720 if (Blocks.count(PN->getIncomingBlock(i))) { 721 Instruction *TI = PN->getIncomingBlock(i)->getTerminator(); 722 TI->replaceUsesOfWith(OldPred, NewBB); 723 } 724 725 // Okay, everything within the region is now branching to the right block, we 726 // just have to update the PHI nodes now, inserting PHI nodes into NewBB. 727 BasicBlock::iterator AfterPHIs; 728 for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) { 729 PHINode *PN = cast<PHINode>(AfterPHIs); 730 // Create a new PHI node in the new region, which has an incoming value 731 // from OldPred of PN. 732 PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion, 733 PN->getName() + ".ce"); 734 NewPN->insertBefore(NewBB->begin()); 735 PN->replaceAllUsesWith(NewPN); 736 NewPN->addIncoming(PN, OldPred); 737 738 // Loop over all of the incoming value in PN, moving them to NewPN if they 739 // are from the extracted region. 740 for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) { 741 if (Blocks.count(PN->getIncomingBlock(i))) { 742 NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i)); 743 PN->removeIncomingValue(i); 744 --i; 745 } 746 } 747 } 748 } 749 } 750 751 /// severSplitPHINodesOfExits - if PHI nodes in exit blocks have inputs from 752 /// outlined region, we split these PHIs on two: one with inputs from region 753 /// and other with remaining incoming blocks; then first PHIs are placed in 754 /// outlined region. 755 void CodeExtractor::severSplitPHINodesOfExits() { 756 for (BasicBlock *ExitBB : ExtractedFuncRetVals) { 757 BasicBlock *NewBB = nullptr; 758 759 for (PHINode &PN : ExitBB->phis()) { 760 // Find all incoming values from the outlining region. 761 SmallVector<unsigned, 2> IncomingVals; 762 for (unsigned i = 0; i < PN.getNumIncomingValues(); ++i) 763 if (Blocks.count(PN.getIncomingBlock(i))) 764 IncomingVals.push_back(i); 765 766 // Do not process PHI if there is one (or fewer) predecessor from region. 767 // If PHI has exactly one predecessor from region, only this one incoming 768 // will be replaced on codeRepl block, so it should be safe to skip PHI. 769 if (IncomingVals.size() <= 1) 770 continue; 771 772 // Create block for new PHIs and add it to the list of outlined if it 773 // wasn't done before. 774 if (!NewBB) { 775 NewBB = BasicBlock::Create(ExitBB->getContext(), 776 ExitBB->getName() + ".split", 777 ExitBB->getParent(), ExitBB); 778 NewBB->IsNewDbgInfoFormat = ExitBB->IsNewDbgInfoFormat; 779 SmallVector<BasicBlock *, 4> Preds(predecessors(ExitBB)); 780 for (BasicBlock *PredBB : Preds) 781 if (Blocks.count(PredBB)) 782 PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB); 783 BranchInst::Create(ExitBB, NewBB); 784 Blocks.insert(NewBB); 785 } 786 787 // Split this PHI. 788 PHINode *NewPN = PHINode::Create(PN.getType(), IncomingVals.size(), 789 PN.getName() + ".ce"); 790 NewPN->insertBefore(NewBB->getFirstNonPHIIt()); 791 for (unsigned i : IncomingVals) 792 NewPN->addIncoming(PN.getIncomingValue(i), PN.getIncomingBlock(i)); 793 for (unsigned i : reverse(IncomingVals)) 794 PN.removeIncomingValue(i, false); 795 PN.addIncoming(NewPN, NewBB); 796 } 797 } 798 } 799 800 void CodeExtractor::splitReturnBlocks() { 801 for (BasicBlock *Block : Blocks) 802 if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) { 803 BasicBlock *New = 804 Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret"); 805 if (DT) { 806 // Old dominates New. New node dominates all other nodes dominated 807 // by Old. 808 DomTreeNode *OldNode = DT->getNode(Block); 809 SmallVector<DomTreeNode *, 8> Children(OldNode->begin(), 810 OldNode->end()); 811 812 DomTreeNode *NewNode = DT->addNewBlock(New, Block); 813 814 for (DomTreeNode *I : Children) 815 DT->changeImmediateDominator(I, NewNode); 816 } 817 } 818 } 819 820 Function *CodeExtractor::constructFunctionDeclaration( 821 const ValueSet &inputs, const ValueSet &outputs, BlockFrequency EntryFreq, 822 const Twine &Name, ValueSet &StructValues, StructType *&StructTy) { 823 LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n"); 824 LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n"); 825 826 Function *oldFunction = Blocks.front()->getParent(); 827 Module *M = Blocks.front()->getModule(); 828 829 // Assemble the function's parameter lists. 830 std::vector<Type *> ParamTy; 831 std::vector<Type *> AggParamTy; 832 const DataLayout &DL = M->getDataLayout(); 833 834 // Add the types of the input values to the function's argument list 835 for (Value *value : inputs) { 836 LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n"); 837 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) { 838 AggParamTy.push_back(value->getType()); 839 StructValues.insert(value); 840 } else 841 ParamTy.push_back(value->getType()); 842 } 843 844 // Add the types of the output values to the function's argument list. 845 for (Value *output : outputs) { 846 LLVM_DEBUG(dbgs() << "instr used in func: " << *output << "\n"); 847 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) { 848 AggParamTy.push_back(output->getType()); 849 StructValues.insert(output); 850 } else 851 ParamTy.push_back( 852 PointerType::get(output->getContext(), DL.getAllocaAddrSpace())); 853 } 854 855 assert( 856 (ParamTy.size() + AggParamTy.size()) == 857 (inputs.size() + outputs.size()) && 858 "Number of scalar and aggregate params does not match inputs, outputs"); 859 assert((StructValues.empty() || AggregateArgs) && 860 "Expeced StructValues only with AggregateArgs set"); 861 862 // Concatenate scalar and aggregate params in ParamTy. 863 if (!AggParamTy.empty()) { 864 StructTy = StructType::get(M->getContext(), AggParamTy); 865 ParamTy.push_back(PointerType::get( 866 M->getContext(), ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace())); 867 } 868 869 Type *RetTy = getSwitchType(); 870 LLVM_DEBUG({ 871 dbgs() << "Function type: " << *RetTy << " f("; 872 for (Type *i : ParamTy) 873 dbgs() << *i << ", "; 874 dbgs() << ")\n"; 875 }); 876 877 FunctionType *funcType = FunctionType::get( 878 RetTy, ParamTy, AllowVarArgs && oldFunction->isVarArg()); 879 880 // Create the new function 881 Function *newFunction = 882 Function::Create(funcType, GlobalValue::InternalLinkage, 883 oldFunction->getAddressSpace(), Name, M); 884 885 // Propagate personality info to the new function if there is one. 886 if (oldFunction->hasPersonalityFn()) 887 newFunction->setPersonalityFn(oldFunction->getPersonalityFn()); 888 889 // Inherit all of the target dependent attributes and white-listed 890 // target independent attributes. 891 // (e.g. If the extracted region contains a call to an x86.sse 892 // instruction we need to make sure that the extracted region has the 893 // "target-features" attribute allowing it to be lowered. 894 // FIXME: This should be changed to check to see if a specific 895 // attribute can not be inherited. 896 for (const auto &Attr : oldFunction->getAttributes().getFnAttrs()) { 897 if (Attr.isStringAttribute()) { 898 if (Attr.getKindAsString() == "thunk") 899 continue; 900 } else 901 switch (Attr.getKindAsEnum()) { 902 // Those attributes cannot be propagated safely. Explicitly list them 903 // here so we get a warning if new attributes are added. 904 case Attribute::AllocSize: 905 case Attribute::Builtin: 906 case Attribute::Convergent: 907 case Attribute::JumpTable: 908 case Attribute::Naked: 909 case Attribute::NoBuiltin: 910 case Attribute::NoMerge: 911 case Attribute::NoReturn: 912 case Attribute::NoSync: 913 case Attribute::ReturnsTwice: 914 case Attribute::Speculatable: 915 case Attribute::StackAlignment: 916 case Attribute::WillReturn: 917 case Attribute::AllocKind: 918 case Attribute::PresplitCoroutine: 919 case Attribute::Memory: 920 case Attribute::NoFPClass: 921 case Attribute::CoroDestroyOnlyWhenComplete: 922 case Attribute::CoroElideSafe: 923 case Attribute::NoDivergenceSource: 924 continue; 925 // Those attributes should be safe to propagate to the extracted function. 926 case Attribute::AlwaysInline: 927 case Attribute::Cold: 928 case Attribute::DisableSanitizerInstrumentation: 929 case Attribute::FnRetThunkExtern: 930 case Attribute::Hot: 931 case Attribute::HybridPatchable: 932 case Attribute::NoRecurse: 933 case Attribute::InlineHint: 934 case Attribute::MinSize: 935 case Attribute::NoCallback: 936 case Attribute::NoDuplicate: 937 case Attribute::NoFree: 938 case Attribute::NoImplicitFloat: 939 case Attribute::NoInline: 940 case Attribute::NonLazyBind: 941 case Attribute::NoRedZone: 942 case Attribute::NoUnwind: 943 case Attribute::NoSanitizeBounds: 944 case Attribute::NoSanitizeCoverage: 945 case Attribute::NullPointerIsValid: 946 case Attribute::OptimizeForDebugging: 947 case Attribute::OptForFuzzing: 948 case Attribute::OptimizeNone: 949 case Attribute::OptimizeForSize: 950 case Attribute::SafeStack: 951 case Attribute::ShadowCallStack: 952 case Attribute::SanitizeAddress: 953 case Attribute::SanitizeMemory: 954 case Attribute::SanitizeNumericalStability: 955 case Attribute::SanitizeThread: 956 case Attribute::SanitizeType: 957 case Attribute::SanitizeHWAddress: 958 case Attribute::SanitizeMemTag: 959 case Attribute::SanitizeRealtime: 960 case Attribute::SanitizeRealtimeBlocking: 961 case Attribute::SpeculativeLoadHardening: 962 case Attribute::StackProtect: 963 case Attribute::StackProtectReq: 964 case Attribute::StackProtectStrong: 965 case Attribute::StrictFP: 966 case Attribute::UWTable: 967 case Attribute::VScaleRange: 968 case Attribute::NoCfCheck: 969 case Attribute::MustProgress: 970 case Attribute::NoProfile: 971 case Attribute::SkipProfile: 972 break; 973 // These attributes cannot be applied to functions. 974 case Attribute::Alignment: 975 case Attribute::AllocatedPointer: 976 case Attribute::AllocAlign: 977 case Attribute::ByVal: 978 case Attribute::Captures: 979 case Attribute::Dereferenceable: 980 case Attribute::DereferenceableOrNull: 981 case Attribute::ElementType: 982 case Attribute::InAlloca: 983 case Attribute::InReg: 984 case Attribute::Nest: 985 case Attribute::NoAlias: 986 case Attribute::NoCapture: 987 case Attribute::NoUndef: 988 case Attribute::NonNull: 989 case Attribute::Preallocated: 990 case Attribute::ReadNone: 991 case Attribute::ReadOnly: 992 case Attribute::Returned: 993 case Attribute::SExt: 994 case Attribute::StructRet: 995 case Attribute::SwiftError: 996 case Attribute::SwiftSelf: 997 case Attribute::SwiftAsync: 998 case Attribute::ZExt: 999 case Attribute::ImmArg: 1000 case Attribute::ByRef: 1001 case Attribute::WriteOnly: 1002 case Attribute::Writable: 1003 case Attribute::DeadOnUnwind: 1004 case Attribute::Range: 1005 case Attribute::Initializes: 1006 case Attribute::NoExt: 1007 // These are not really attributes. 1008 case Attribute::None: 1009 case Attribute::EndAttrKinds: 1010 case Attribute::EmptyKey: 1011 case Attribute::TombstoneKey: 1012 llvm_unreachable("Not a function attribute"); 1013 } 1014 1015 newFunction->addFnAttr(Attr); 1016 } 1017 1018 // Create scalar and aggregate iterators to name all of the arguments we 1019 // inserted. 1020 Function::arg_iterator ScalarAI = newFunction->arg_begin(); 1021 1022 // Set names and attributes for input and output arguments. 1023 ScalarAI = newFunction->arg_begin(); 1024 for (Value *input : inputs) { 1025 if (StructValues.contains(input)) 1026 continue; 1027 1028 ScalarAI->setName(input->getName()); 1029 if (input->isSwiftError()) 1030 newFunction->addParamAttr(ScalarAI - newFunction->arg_begin(), 1031 Attribute::SwiftError); 1032 ++ScalarAI; 1033 } 1034 for (Value *output : outputs) { 1035 if (StructValues.contains(output)) 1036 continue; 1037 1038 ScalarAI->setName(output->getName() + ".out"); 1039 ++ScalarAI; 1040 } 1041 1042 // Update the entry count of the function. 1043 if (BFI) { 1044 auto Count = BFI->getProfileCountFromFreq(EntryFreq); 1045 if (Count.has_value()) 1046 newFunction->setEntryCount( 1047 ProfileCount(*Count, Function::PCT_Real)); // FIXME 1048 } 1049 1050 return newFunction; 1051 } 1052 1053 /// If the original function has debug info, we have to add a debug location 1054 /// to the new branch instruction from the artificial entry block. 1055 /// We use the debug location of the first instruction in the extracted 1056 /// blocks, as there is no other equivalent line in the source code. 1057 static void applyFirstDebugLoc(Function *oldFunction, 1058 ArrayRef<BasicBlock *> Blocks, 1059 Instruction *BranchI) { 1060 if (oldFunction->getSubprogram()) { 1061 any_of(Blocks, [&BranchI](const BasicBlock *BB) { 1062 return any_of(*BB, [&BranchI](const Instruction &I) { 1063 if (!I.getDebugLoc()) 1064 return false; 1065 // Don't use source locations attached to debug-intrinsics: they could 1066 // be from completely unrelated scopes. 1067 if (isa<DbgInfoIntrinsic>(I)) 1068 return false; 1069 BranchI->setDebugLoc(I.getDebugLoc()); 1070 return true; 1071 }); 1072 }); 1073 } 1074 } 1075 1076 /// Erase lifetime.start markers which reference inputs to the extraction 1077 /// region, and insert the referenced memory into \p LifetimesStart. 1078 /// 1079 /// The extraction region is defined by a set of blocks (\p Blocks), and a set 1080 /// of allocas which will be moved from the caller function into the extracted 1081 /// function (\p SunkAllocas). 1082 static void eraseLifetimeMarkersOnInputs(const SetVector<BasicBlock *> &Blocks, 1083 const SetVector<Value *> &SunkAllocas, 1084 SetVector<Value *> &LifetimesStart) { 1085 for (BasicBlock *BB : Blocks) { 1086 for (Instruction &I : llvm::make_early_inc_range(*BB)) { 1087 auto *II = dyn_cast<IntrinsicInst>(&I); 1088 if (!II || !II->isLifetimeStartOrEnd()) 1089 continue; 1090 1091 // Get the memory operand of the lifetime marker. If the underlying 1092 // object is a sunk alloca, or is otherwise defined in the extraction 1093 // region, the lifetime marker must not be erased. 1094 Value *Mem = II->getOperand(1)->stripInBoundsOffsets(); 1095 if (SunkAllocas.count(Mem) || definedInRegion(Blocks, Mem)) 1096 continue; 1097 1098 if (II->getIntrinsicID() == Intrinsic::lifetime_start) 1099 LifetimesStart.insert(Mem); 1100 II->eraseFromParent(); 1101 } 1102 } 1103 } 1104 1105 /// Insert lifetime start/end markers surrounding the call to the new function 1106 /// for objects defined in the caller. 1107 static void insertLifetimeMarkersSurroundingCall( 1108 Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd, 1109 CallInst *TheCall) { 1110 LLVMContext &Ctx = M->getContext(); 1111 auto NegativeOne = ConstantInt::getSigned(Type::getInt64Ty(Ctx), -1); 1112 Instruction *Term = TheCall->getParent()->getTerminator(); 1113 1114 // Emit lifetime markers for the pointers given in \p Objects. Insert the 1115 // markers before the call if \p InsertBefore, and after the call otherwise. 1116 auto insertMarkers = [&](Intrinsic::ID MarkerFunc, ArrayRef<Value *> Objects, 1117 bool InsertBefore) { 1118 for (Value *Mem : Objects) { 1119 assert((!isa<Instruction>(Mem) || cast<Instruction>(Mem)->getFunction() == 1120 TheCall->getFunction()) && 1121 "Input memory not defined in original function"); 1122 1123 Function *Func = 1124 Intrinsic::getOrInsertDeclaration(M, MarkerFunc, Mem->getType()); 1125 auto Marker = CallInst::Create(Func, {NegativeOne, Mem}); 1126 if (InsertBefore) 1127 Marker->insertBefore(TheCall->getIterator()); 1128 else 1129 Marker->insertBefore(Term->getIterator()); 1130 } 1131 }; 1132 1133 if (!LifetimesStart.empty()) { 1134 insertMarkers(Intrinsic::lifetime_start, LifetimesStart, 1135 /*InsertBefore=*/true); 1136 } 1137 1138 if (!LifetimesEnd.empty()) { 1139 insertMarkers(Intrinsic::lifetime_end, LifetimesEnd, 1140 /*InsertBefore=*/false); 1141 } 1142 } 1143 1144 void CodeExtractor::moveCodeToFunction(Function *newFunction) { 1145 auto newFuncIt = newFunction->begin(); 1146 for (BasicBlock *Block : Blocks) { 1147 // Delete the basic block from the old function, and the list of blocks 1148 Block->removeFromParent(); 1149 1150 // Insert this basic block into the new function 1151 // Insert the original blocks after the entry block created 1152 // for the new function. The entry block may be followed 1153 // by a set of exit blocks at this point, but these exit 1154 // blocks better be placed at the end of the new function. 1155 newFuncIt = newFunction->insert(std::next(newFuncIt), Block); 1156 } 1157 } 1158 1159 void CodeExtractor::calculateNewCallTerminatorWeights( 1160 BasicBlock *CodeReplacer, 1161 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights, 1162 BranchProbabilityInfo *BPI) { 1163 using Distribution = BlockFrequencyInfoImplBase::Distribution; 1164 using BlockNode = BlockFrequencyInfoImplBase::BlockNode; 1165 1166 // Update the branch weights for the exit block. 1167 Instruction *TI = CodeReplacer->getTerminator(); 1168 SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0); 1169 1170 // Block Frequency distribution with dummy node. 1171 Distribution BranchDist; 1172 1173 SmallVector<BranchProbability, 4> EdgeProbabilities( 1174 TI->getNumSuccessors(), BranchProbability::getUnknown()); 1175 1176 // Add each of the frequencies of the successors. 1177 for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) { 1178 BlockNode ExitNode(i); 1179 uint64_t ExitFreq = ExitWeights.lookup(TI->getSuccessor(i)).getFrequency(); 1180 if (ExitFreq != 0) 1181 BranchDist.addExit(ExitNode, ExitFreq); 1182 else 1183 EdgeProbabilities[i] = BranchProbability::getZero(); 1184 } 1185 1186 // Check for no total weight. 1187 if (BranchDist.Total == 0) { 1188 BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities); 1189 return; 1190 } 1191 1192 // Normalize the distribution so that they can fit in unsigned. 1193 BranchDist.normalize(); 1194 1195 // Create normalized branch weights and set the metadata. 1196 for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) { 1197 const auto &Weight = BranchDist.Weights[I]; 1198 1199 // Get the weight and update the current BFI. 1200 BranchWeights[Weight.TargetNode.Index] = Weight.Amount; 1201 BranchProbability BP(Weight.Amount, BranchDist.Total); 1202 EdgeProbabilities[Weight.TargetNode.Index] = BP; 1203 } 1204 BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities); 1205 TI->setMetadata( 1206 LLVMContext::MD_prof, 1207 MDBuilder(TI->getContext()).createBranchWeights(BranchWeights)); 1208 } 1209 1210 /// Erase debug info intrinsics which refer to values in \p F but aren't in 1211 /// \p F. 1212 static void eraseDebugIntrinsicsWithNonLocalRefs(Function &F) { 1213 for (Instruction &I : instructions(F)) { 1214 SmallVector<DbgVariableIntrinsic *, 4> DbgUsers; 1215 SmallVector<DbgVariableRecord *, 4> DbgVariableRecords; 1216 findDbgUsers(DbgUsers, &I, &DbgVariableRecords); 1217 for (DbgVariableIntrinsic *DVI : DbgUsers) 1218 if (DVI->getFunction() != &F) 1219 DVI->eraseFromParent(); 1220 for (DbgVariableRecord *DVR : DbgVariableRecords) 1221 if (DVR->getFunction() != &F) 1222 DVR->eraseFromParent(); 1223 } 1224 } 1225 1226 /// Fix up the debug info in the old and new functions by pointing line 1227 /// locations and debug intrinsics to the new subprogram scope, and by deleting 1228 /// intrinsics which point to values outside of the new function. 1229 static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc, 1230 CallInst &TheCall) { 1231 DISubprogram *OldSP = OldFunc.getSubprogram(); 1232 LLVMContext &Ctx = OldFunc.getContext(); 1233 1234 if (!OldSP) { 1235 // Erase any debug info the new function contains. 1236 stripDebugInfo(NewFunc); 1237 // Make sure the old function doesn't contain any non-local metadata refs. 1238 eraseDebugIntrinsicsWithNonLocalRefs(NewFunc); 1239 return; 1240 } 1241 1242 // Create a subprogram for the new function. Leave out a description of the 1243 // function arguments, as the parameters don't correspond to anything at the 1244 // source level. 1245 assert(OldSP->getUnit() && "Missing compile unit for subprogram"); 1246 DIBuilder DIB(*OldFunc.getParent(), /*AllowUnresolved=*/false, 1247 OldSP->getUnit()); 1248 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray({})); 1249 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagDefinition | 1250 DISubprogram::SPFlagOptimized | 1251 DISubprogram::SPFlagLocalToUnit; 1252 auto NewSP = DIB.createFunction( 1253 OldSP->getUnit(), NewFunc.getName(), NewFunc.getName(), OldSP->getFile(), 1254 /*LineNo=*/0, SPType, /*ScopeLine=*/0, DINode::FlagZero, SPFlags); 1255 NewFunc.setSubprogram(NewSP); 1256 1257 auto IsInvalidLocation = [&NewFunc](Value *Location) { 1258 // Location is invalid if it isn't a constant or an instruction, or is an 1259 // instruction but isn't in the new function. 1260 if (!Location || 1261 (!isa<Constant>(Location) && !isa<Instruction>(Location))) 1262 return true; 1263 Instruction *LocationInst = dyn_cast<Instruction>(Location); 1264 return LocationInst && LocationInst->getFunction() != &NewFunc; 1265 }; 1266 1267 // Debug intrinsics in the new function need to be updated in one of two 1268 // ways: 1269 // 1) They need to be deleted, because they describe a value in the old 1270 // function. 1271 // 2) They need to point to fresh metadata, e.g. because they currently 1272 // point to a variable in the wrong scope. 1273 SmallDenseMap<DINode *, DINode *> RemappedMetadata; 1274 SmallVector<Instruction *, 4> DebugIntrinsicsToDelete; 1275 SmallVector<DbgVariableRecord *, 4> DVRsToDelete; 1276 DenseMap<const MDNode *, MDNode *> Cache; 1277 1278 auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar) { 1279 DINode *&NewVar = RemappedMetadata[OldVar]; 1280 if (!NewVar) { 1281 DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram( 1282 *OldVar->getScope(), *NewSP, Ctx, Cache); 1283 NewVar = DIB.createAutoVariable( 1284 NewScope, OldVar->getName(), OldVar->getFile(), OldVar->getLine(), 1285 OldVar->getType(), /*AlwaysPreserve=*/false, DINode::FlagZero, 1286 OldVar->getAlignInBits()); 1287 } 1288 return cast<DILocalVariable>(NewVar); 1289 }; 1290 1291 auto UpdateDbgLabel = [&](auto *LabelRecord) { 1292 // Point the label record to a fresh label within the new function if 1293 // the record was not inlined from some other function. 1294 if (LabelRecord->getDebugLoc().getInlinedAt()) 1295 return; 1296 DILabel *OldLabel = LabelRecord->getLabel(); 1297 DINode *&NewLabel = RemappedMetadata[OldLabel]; 1298 if (!NewLabel) { 1299 DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram( 1300 *OldLabel->getScope(), *NewSP, Ctx, Cache); 1301 NewLabel = DILabel::get(Ctx, NewScope, OldLabel->getName(), 1302 OldLabel->getFile(), OldLabel->getLine()); 1303 } 1304 LabelRecord->setLabel(cast<DILabel>(NewLabel)); 1305 }; 1306 1307 auto UpdateDbgRecordsOnInst = [&](Instruction &I) -> void { 1308 for (DbgRecord &DR : I.getDbgRecordRange()) { 1309 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) { 1310 UpdateDbgLabel(DLR); 1311 continue; 1312 } 1313 1314 DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR); 1315 // Apply the two updates that dbg.values get: invalid operands, and 1316 // variable metadata fixup. 1317 if (any_of(DVR.location_ops(), IsInvalidLocation)) { 1318 DVRsToDelete.push_back(&DVR); 1319 continue; 1320 } 1321 if (DVR.isDbgAssign() && IsInvalidLocation(DVR.getAddress())) { 1322 DVRsToDelete.push_back(&DVR); 1323 continue; 1324 } 1325 if (!DVR.getDebugLoc().getInlinedAt()) 1326 DVR.setVariable(GetUpdatedDIVariable(DVR.getVariable())); 1327 } 1328 }; 1329 1330 for (Instruction &I : instructions(NewFunc)) { 1331 UpdateDbgRecordsOnInst(I); 1332 1333 auto *DII = dyn_cast<DbgInfoIntrinsic>(&I); 1334 if (!DII) 1335 continue; 1336 1337 // Point the intrinsic to a fresh label within the new function if the 1338 // intrinsic was not inlined from some other function. 1339 if (auto *DLI = dyn_cast<DbgLabelInst>(&I)) { 1340 UpdateDbgLabel(DLI); 1341 continue; 1342 } 1343 1344 auto *DVI = cast<DbgVariableIntrinsic>(DII); 1345 // If any of the used locations are invalid, delete the intrinsic. 1346 if (any_of(DVI->location_ops(), IsInvalidLocation)) { 1347 DebugIntrinsicsToDelete.push_back(DVI); 1348 continue; 1349 } 1350 // DbgAssign intrinsics have an extra Value argument: 1351 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI); 1352 DAI && IsInvalidLocation(DAI->getAddress())) { 1353 DebugIntrinsicsToDelete.push_back(DVI); 1354 continue; 1355 } 1356 // If the variable was in the scope of the old function, i.e. it was not 1357 // inlined, point the intrinsic to a fresh variable within the new function. 1358 if (!DVI->getDebugLoc().getInlinedAt()) 1359 DVI->setVariable(GetUpdatedDIVariable(DVI->getVariable())); 1360 } 1361 1362 for (auto *DII : DebugIntrinsicsToDelete) 1363 DII->eraseFromParent(); 1364 for (auto *DVR : DVRsToDelete) 1365 DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR); 1366 DIB.finalizeSubprogram(NewSP); 1367 1368 // Fix up the scope information attached to the line locations and the 1369 // debug assignment metadata in the new function. 1370 DenseMap<DIAssignID *, DIAssignID *> AssignmentIDMap; 1371 for (Instruction &I : instructions(NewFunc)) { 1372 if (const DebugLoc &DL = I.getDebugLoc()) 1373 I.setDebugLoc( 1374 DebugLoc::replaceInlinedAtSubprogram(DL, *NewSP, Ctx, Cache)); 1375 for (DbgRecord &DR : I.getDbgRecordRange()) 1376 DR.setDebugLoc(DebugLoc::replaceInlinedAtSubprogram(DR.getDebugLoc(), 1377 *NewSP, Ctx, Cache)); 1378 1379 // Loop info metadata may contain line locations. Fix them up. 1380 auto updateLoopInfoLoc = [&Ctx, &Cache, NewSP](Metadata *MD) -> Metadata * { 1381 if (auto *Loc = dyn_cast_or_null<DILocation>(MD)) 1382 return DebugLoc::replaceInlinedAtSubprogram(Loc, *NewSP, Ctx, Cache); 1383 return MD; 1384 }; 1385 updateLoopMetadataDebugLocations(I, updateLoopInfoLoc); 1386 at::remapAssignID(AssignmentIDMap, I); 1387 } 1388 if (!TheCall.getDebugLoc()) 1389 TheCall.setDebugLoc(DILocation::get(Ctx, 0, 0, OldSP)); 1390 1391 eraseDebugIntrinsicsWithNonLocalRefs(NewFunc); 1392 } 1393 1394 Function * 1395 CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC) { 1396 ValueSet Inputs, Outputs; 1397 return extractCodeRegion(CEAC, Inputs, Outputs); 1398 } 1399 1400 Function * 1401 CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC, 1402 ValueSet &inputs, ValueSet &outputs) { 1403 if (!isEligible()) 1404 return nullptr; 1405 1406 // Assumption: this is a single-entry code region, and the header is the first 1407 // block in the region. 1408 BasicBlock *header = *Blocks.begin(); 1409 Function *oldFunction = header->getParent(); 1410 1411 normalizeCFGForExtraction(header); 1412 1413 // Remove @llvm.assume calls that will be moved to the new function from the 1414 // old function's assumption cache. 1415 for (BasicBlock *Block : Blocks) { 1416 for (Instruction &I : llvm::make_early_inc_range(*Block)) { 1417 if (auto *AI = dyn_cast<AssumeInst>(&I)) { 1418 if (AC) 1419 AC->unregisterAssumption(AI); 1420 AI->eraseFromParent(); 1421 } 1422 } 1423 } 1424 1425 ValueSet SinkingCands, HoistingCands; 1426 BasicBlock *CommonExit = nullptr; 1427 findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit); 1428 assert(HoistingCands.empty() || CommonExit); 1429 1430 // Find inputs to, outputs from the code region. 1431 findInputsOutputs(inputs, outputs, SinkingCands); 1432 1433 // Collect objects which are inputs to the extraction region and also 1434 // referenced by lifetime start markers within it. The effects of these 1435 // markers must be replicated in the calling function to prevent the stack 1436 // coloring pass from merging slots which store input objects. 1437 ValueSet LifetimesStart; 1438 eraseLifetimeMarkersOnInputs(Blocks, SinkingCands, LifetimesStart); 1439 1440 if (!HoistingCands.empty()) { 1441 auto *HoistToBlock = findOrCreateBlockForHoisting(CommonExit); 1442 Instruction *TI = HoistToBlock->getTerminator(); 1443 for (auto *II : HoistingCands) 1444 cast<Instruction>(II)->moveBefore(TI->getIterator()); 1445 computeExtractedFuncRetVals(); 1446 } 1447 1448 // CFG/ExitBlocks must not change hereafter 1449 1450 // Calculate the entry frequency of the new function before we change the root 1451 // block. 1452 BlockFrequency EntryFreq; 1453 DenseMap<BasicBlock *, BlockFrequency> ExitWeights; 1454 if (BFI) { 1455 assert(BPI && "Both BPI and BFI are required to preserve profile info"); 1456 for (BasicBlock *Pred : predecessors(header)) { 1457 if (Blocks.count(Pred)) 1458 continue; 1459 EntryFreq += 1460 BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header); 1461 } 1462 1463 for (BasicBlock *Succ : ExtractedFuncRetVals) { 1464 for (BasicBlock *Block : predecessors(Succ)) { 1465 if (!Blocks.count(Block)) 1466 continue; 1467 1468 // Update the branch weight for this successor. 1469 BlockFrequency &BF = ExitWeights[Succ]; 1470 BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, Succ); 1471 } 1472 } 1473 } 1474 1475 // Determine position for the replacement code. Do so before header is moved 1476 // to the new function. 1477 BasicBlock *ReplIP = header; 1478 while (ReplIP && Blocks.count(ReplIP)) 1479 ReplIP = ReplIP->getNextNode(); 1480 1481 // Construct new function based on inputs/outputs & add allocas for all defs. 1482 std::string SuffixToUse = 1483 Suffix.empty() 1484 ? (header->getName().empty() ? "extracted" : header->getName().str()) 1485 : Suffix; 1486 1487 ValueSet StructValues; 1488 StructType *StructTy = nullptr; 1489 Function *newFunction = constructFunctionDeclaration( 1490 inputs, outputs, EntryFreq, oldFunction->getName() + "." + SuffixToUse, 1491 StructValues, StructTy); 1492 newFunction->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat; 1493 1494 emitFunctionBody(inputs, outputs, StructValues, newFunction, StructTy, header, 1495 SinkingCands); 1496 1497 std::vector<Value *> Reloads; 1498 CallInst *TheCall = emitReplacerCall( 1499 inputs, outputs, StructValues, newFunction, StructTy, oldFunction, ReplIP, 1500 EntryFreq, LifetimesStart.getArrayRef(), Reloads); 1501 1502 insertReplacerCall(oldFunction, header, TheCall->getParent(), outputs, 1503 Reloads, ExitWeights); 1504 1505 fixupDebugInfoPostExtraction(*oldFunction, *newFunction, *TheCall); 1506 1507 LLVM_DEBUG(if (verifyFunction(*newFunction, &errs())) { 1508 newFunction->dump(); 1509 report_fatal_error("verification of newFunction failed!"); 1510 }); 1511 LLVM_DEBUG(if (verifyFunction(*oldFunction)) 1512 report_fatal_error("verification of oldFunction failed!")); 1513 LLVM_DEBUG(if (AC && verifyAssumptionCache(*oldFunction, *newFunction, AC)) 1514 report_fatal_error("Stale Asumption cache for old Function!")); 1515 return newFunction; 1516 } 1517 1518 void CodeExtractor::normalizeCFGForExtraction(BasicBlock *&header) { 1519 // If we have any return instructions in the region, split those blocks so 1520 // that the return is not in the region. 1521 splitReturnBlocks(); 1522 1523 // If we have to split PHI nodes of the entry or exit blocks, do so now. 1524 severSplitPHINodesOfEntry(header); 1525 1526 // If a PHI in an exit block has multiple incoming values from the outlined 1527 // region, create a new PHI for those values within the region such that only 1528 // PHI itself becomes an output value, not each of its incoming values 1529 // individually. 1530 computeExtractedFuncRetVals(); 1531 severSplitPHINodesOfExits(); 1532 } 1533 1534 void CodeExtractor::computeExtractedFuncRetVals() { 1535 ExtractedFuncRetVals.clear(); 1536 1537 SmallPtrSet<BasicBlock *, 2> ExitBlocks; 1538 for (BasicBlock *Block : Blocks) { 1539 for (BasicBlock *Succ : successors(Block)) { 1540 if (Blocks.count(Succ)) 1541 continue; 1542 1543 bool IsNew = ExitBlocks.insert(Succ).second; 1544 if (IsNew) 1545 ExtractedFuncRetVals.push_back(Succ); 1546 } 1547 } 1548 } 1549 1550 Type *CodeExtractor::getSwitchType() { 1551 LLVMContext &Context = Blocks.front()->getContext(); 1552 1553 assert(ExtractedFuncRetVals.size() < 0xffff && 1554 "too many exit blocks for switch"); 1555 switch (ExtractedFuncRetVals.size()) { 1556 case 0: 1557 case 1: 1558 return Type::getVoidTy(Context); 1559 case 2: 1560 // Conditional branch, return a bool 1561 return Type::getInt1Ty(Context); 1562 default: 1563 return Type::getInt16Ty(Context); 1564 } 1565 } 1566 1567 void CodeExtractor::emitFunctionBody( 1568 const ValueSet &inputs, const ValueSet &outputs, 1569 const ValueSet &StructValues, Function *newFunction, 1570 StructType *StructArgTy, BasicBlock *header, const ValueSet &SinkingCands) { 1571 Function *oldFunction = header->getParent(); 1572 LLVMContext &Context = oldFunction->getContext(); 1573 1574 // The new function needs a root node because other nodes can branch to the 1575 // head of the region, but the entry node of a function cannot have preds. 1576 BasicBlock *newFuncRoot = 1577 BasicBlock::Create(Context, "newFuncRoot", newFunction); 1578 newFuncRoot->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat; 1579 1580 // Now sink all instructions which only have non-phi uses inside the region. 1581 // Group the allocas at the start of the block, so that any bitcast uses of 1582 // the allocas are well-defined. 1583 for (auto *II : SinkingCands) { 1584 if (!isa<AllocaInst>(II)) { 1585 cast<Instruction>(II)->moveBefore(*newFuncRoot, 1586 newFuncRoot->getFirstInsertionPt()); 1587 } 1588 } 1589 for (auto *II : SinkingCands) { 1590 if (auto *AI = dyn_cast<AllocaInst>(II)) { 1591 AI->moveBefore(*newFuncRoot, newFuncRoot->getFirstInsertionPt()); 1592 } 1593 } 1594 1595 Function::arg_iterator ScalarAI = newFunction->arg_begin(); 1596 Argument *AggArg = StructValues.empty() 1597 ? nullptr 1598 : newFunction->getArg(newFunction->arg_size() - 1); 1599 1600 // Rewrite all users of the inputs in the extracted region to use the 1601 // arguments (or appropriate addressing into struct) instead. 1602 SmallVector<Value *> NewValues; 1603 for (unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) { 1604 Value *RewriteVal; 1605 if (StructValues.contains(inputs[i])) { 1606 Value *Idx[2]; 1607 Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext())); 1608 Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), aggIdx); 1609 GetElementPtrInst *GEP = GetElementPtrInst::Create( 1610 StructArgTy, AggArg, Idx, "gep_" + inputs[i]->getName(), newFuncRoot); 1611 RewriteVal = new LoadInst(StructArgTy->getElementType(aggIdx), GEP, 1612 "loadgep_" + inputs[i]->getName(), newFuncRoot); 1613 ++aggIdx; 1614 } else 1615 RewriteVal = &*ScalarAI++; 1616 1617 NewValues.push_back(RewriteVal); 1618 } 1619 1620 moveCodeToFunction(newFunction); 1621 1622 for (unsigned i = 0, e = inputs.size(); i != e; ++i) { 1623 Value *RewriteVal = NewValues[i]; 1624 1625 std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end()); 1626 for (User *use : Users) 1627 if (Instruction *inst = dyn_cast<Instruction>(use)) 1628 if (Blocks.count(inst->getParent())) 1629 inst->replaceUsesOfWith(inputs[i], RewriteVal); 1630 } 1631 1632 // Since there may be multiple exits from the original region, make the new 1633 // function return an unsigned, switch on that number. This loop iterates 1634 // over all of the blocks in the extracted region, updating any terminator 1635 // instructions in the to-be-extracted region that branch to blocks that are 1636 // not in the region to be extracted. 1637 std::map<BasicBlock *, BasicBlock *> ExitBlockMap; 1638 1639 // Iterate over the previously collected targets, and create new blocks inside 1640 // the function to branch to. 1641 for (auto P : enumerate(ExtractedFuncRetVals)) { 1642 BasicBlock *OldTarget = P.value(); 1643 size_t SuccNum = P.index(); 1644 1645 BasicBlock *NewTarget = BasicBlock::Create( 1646 Context, OldTarget->getName() + ".exitStub", newFunction); 1647 ExitBlockMap[OldTarget] = NewTarget; 1648 1649 Value *brVal = nullptr; 1650 Type *RetTy = getSwitchType(); 1651 assert(ExtractedFuncRetVals.size() < 0xffff && 1652 "too many exit blocks for switch"); 1653 switch (ExtractedFuncRetVals.size()) { 1654 case 0: 1655 case 1: 1656 // No value needed. 1657 break; 1658 case 2: // Conditional branch, return a bool 1659 brVal = ConstantInt::get(RetTy, !SuccNum); 1660 break; 1661 default: 1662 brVal = ConstantInt::get(RetTy, SuccNum); 1663 break; 1664 } 1665 1666 ReturnInst::Create(Context, brVal, NewTarget); 1667 } 1668 1669 for (BasicBlock *Block : Blocks) { 1670 Instruction *TI = Block->getTerminator(); 1671 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) { 1672 if (Blocks.count(TI->getSuccessor(i))) 1673 continue; 1674 BasicBlock *OldTarget = TI->getSuccessor(i); 1675 // add a new basic block which returns the appropriate value 1676 BasicBlock *NewTarget = ExitBlockMap[OldTarget]; 1677 assert(NewTarget && "Unknown target block!"); 1678 1679 // rewrite the original branch instruction with this new target 1680 TI->setSuccessor(i, NewTarget); 1681 } 1682 } 1683 1684 // Loop over all of the PHI nodes in the header and exit blocks, and change 1685 // any references to the old incoming edge to be the new incoming edge. 1686 for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) { 1687 PHINode *PN = cast<PHINode>(I); 1688 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 1689 if (!Blocks.count(PN->getIncomingBlock(i))) 1690 PN->setIncomingBlock(i, newFuncRoot); 1691 } 1692 1693 // Connect newFunction entry block to new header. 1694 BranchInst *BranchI = BranchInst::Create(header, newFuncRoot); 1695 applyFirstDebugLoc(oldFunction, Blocks.getArrayRef(), BranchI); 1696 1697 // Store the arguments right after the definition of output value. 1698 // This should be proceeded after creating exit stubs to be ensure that invoke 1699 // result restore will be placed in the outlined function. 1700 ScalarAI = newFunction->arg_begin(); 1701 unsigned AggIdx = 0; 1702 1703 for (Value *Input : inputs) { 1704 if (StructValues.contains(Input)) 1705 ++AggIdx; 1706 else 1707 ++ScalarAI; 1708 } 1709 1710 for (Value *Output : outputs) { 1711 // Find proper insertion point. 1712 // In case Output is an invoke, we insert the store at the beginning in the 1713 // 'normal destination' BB. Otherwise we insert the store right after 1714 // Output. 1715 BasicBlock::iterator InsertPt; 1716 if (auto *InvokeI = dyn_cast<InvokeInst>(Output)) 1717 InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt(); 1718 else if (auto *Phi = dyn_cast<PHINode>(Output)) 1719 InsertPt = Phi->getParent()->getFirstInsertionPt(); 1720 else if (auto *OutI = dyn_cast<Instruction>(Output)) 1721 InsertPt = std::next(OutI->getIterator()); 1722 else { 1723 // Globals don't need to be updated, just advance to the next argument. 1724 if (StructValues.contains(Output)) 1725 ++AggIdx; 1726 else 1727 ++ScalarAI; 1728 continue; 1729 } 1730 1731 assert((InsertPt->getFunction() == newFunction || 1732 Blocks.count(InsertPt->getParent())) && 1733 "InsertPt should be in new function"); 1734 1735 if (StructValues.contains(Output)) { 1736 assert(AggArg && "Number of aggregate output arguments should match " 1737 "the number of defined values"); 1738 Value *Idx[2]; 1739 Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); 1740 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), AggIdx); 1741 GetElementPtrInst *GEP = GetElementPtrInst::Create( 1742 StructArgTy, AggArg, Idx, "gep_" + Output->getName(), InsertPt); 1743 new StoreInst(Output, GEP, InsertPt); 1744 ++AggIdx; 1745 } else { 1746 assert(ScalarAI != newFunction->arg_end() && 1747 "Number of scalar output arguments should match " 1748 "the number of defined values"); 1749 new StoreInst(Output, &*ScalarAI, InsertPt); 1750 ++ScalarAI; 1751 } 1752 } 1753 1754 if (ExtractedFuncRetVals.empty()) { 1755 // Mark the new function `noreturn` if applicable. Terminators which resume 1756 // exception propagation are treated as returning instructions. This is to 1757 // avoid inserting traps after calls to outlined functions which unwind. 1758 if (none_of(Blocks, [](const BasicBlock *BB) { 1759 const Instruction *Term = BB->getTerminator(); 1760 return isa<ReturnInst>(Term) || isa<ResumeInst>(Term); 1761 })) 1762 newFunction->setDoesNotReturn(); 1763 } 1764 } 1765 1766 CallInst *CodeExtractor::emitReplacerCall( 1767 const ValueSet &inputs, const ValueSet &outputs, 1768 const ValueSet &StructValues, Function *newFunction, 1769 StructType *StructArgTy, Function *oldFunction, BasicBlock *ReplIP, 1770 BlockFrequency EntryFreq, ArrayRef<Value *> LifetimesStart, 1771 std::vector<Value *> &Reloads) { 1772 LLVMContext &Context = oldFunction->getContext(); 1773 Module *M = oldFunction->getParent(); 1774 const DataLayout &DL = M->getDataLayout(); 1775 1776 // This takes place of the original loop 1777 BasicBlock *codeReplacer = 1778 BasicBlock::Create(Context, "codeRepl", oldFunction, ReplIP); 1779 codeReplacer->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat; 1780 BasicBlock *AllocaBlock = 1781 AllocationBlock ? AllocationBlock : &oldFunction->getEntryBlock(); 1782 AllocaBlock->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat; 1783 1784 // Update the entry count of the function. 1785 if (BFI) 1786 BFI->setBlockFreq(codeReplacer, EntryFreq); 1787 1788 std::vector<Value *> params; 1789 1790 // Add inputs as params, or to be filled into the struct 1791 for (Value *input : inputs) { 1792 if (StructValues.contains(input)) 1793 continue; 1794 1795 params.push_back(input); 1796 } 1797 1798 // Create allocas for the outputs 1799 std::vector<Value *> ReloadOutputs; 1800 for (Value *output : outputs) { 1801 if (StructValues.contains(output)) 1802 continue; 1803 1804 AllocaInst *alloca = new AllocaInst( 1805 output->getType(), DL.getAllocaAddrSpace(), nullptr, 1806 output->getName() + ".loc", AllocaBlock->getFirstInsertionPt()); 1807 params.push_back(alloca); 1808 ReloadOutputs.push_back(alloca); 1809 } 1810 1811 AllocaInst *Struct = nullptr; 1812 if (!StructValues.empty()) { 1813 Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr, 1814 "structArg", AllocaBlock->getFirstInsertionPt()); 1815 if (ArgsInZeroAddressSpace && DL.getAllocaAddrSpace() != 0) { 1816 auto *StructSpaceCast = new AddrSpaceCastInst( 1817 Struct, PointerType ::get(Context, 0), "structArg.ascast"); 1818 StructSpaceCast->insertAfter(Struct->getIterator()); 1819 params.push_back(StructSpaceCast); 1820 } else { 1821 params.push_back(Struct); 1822 } 1823 1824 unsigned AggIdx = 0; 1825 for (Value *input : inputs) { 1826 if (!StructValues.contains(input)) 1827 continue; 1828 1829 Value *Idx[2]; 1830 Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); 1831 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), AggIdx); 1832 GetElementPtrInst *GEP = GetElementPtrInst::Create( 1833 StructArgTy, Struct, Idx, "gep_" + input->getName()); 1834 GEP->insertInto(codeReplacer, codeReplacer->end()); 1835 new StoreInst(input, GEP, codeReplacer); 1836 1837 ++AggIdx; 1838 } 1839 } 1840 1841 // Emit the call to the function 1842 CallInst *call = CallInst::Create( 1843 newFunction, params, ExtractedFuncRetVals.size() > 1 ? "targetBlock" : "", 1844 codeReplacer); 1845 1846 // Set swifterror parameter attributes. 1847 unsigned ParamIdx = 0; 1848 unsigned AggIdx = 0; 1849 for (auto input : inputs) { 1850 if (StructValues.contains(input)) { 1851 ++AggIdx; 1852 } else { 1853 if (input->isSwiftError()) 1854 call->addParamAttr(ParamIdx, Attribute::SwiftError); 1855 ++ParamIdx; 1856 } 1857 } 1858 1859 // Add debug location to the new call, if the original function has debug 1860 // info. In that case, the terminator of the entry block of the extracted 1861 // function contains the first debug location of the extracted function, 1862 // set in extractCodeRegion. 1863 if (codeReplacer->getParent()->getSubprogram()) { 1864 if (auto DL = newFunction->getEntryBlock().getTerminator()->getDebugLoc()) 1865 call->setDebugLoc(DL); 1866 } 1867 1868 // Reload the outputs passed in by reference, use the struct if output is in 1869 // the aggregate or reload from the scalar argument. 1870 for (unsigned i = 0, e = outputs.size(), scalarIdx = 0; i != e; ++i) { 1871 Value *Output = nullptr; 1872 if (StructValues.contains(outputs[i])) { 1873 Value *Idx[2]; 1874 Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context)); 1875 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), AggIdx); 1876 GetElementPtrInst *GEP = GetElementPtrInst::Create( 1877 StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName()); 1878 GEP->insertInto(codeReplacer, codeReplacer->end()); 1879 Output = GEP; 1880 ++AggIdx; 1881 } else { 1882 Output = ReloadOutputs[scalarIdx]; 1883 ++scalarIdx; 1884 } 1885 LoadInst *load = 1886 new LoadInst(outputs[i]->getType(), Output, 1887 outputs[i]->getName() + ".reload", codeReplacer); 1888 Reloads.push_back(load); 1889 } 1890 1891 // Now we can emit a switch statement using the call as a value. 1892 SwitchInst *TheSwitch = 1893 SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)), 1894 codeReplacer, 0, codeReplacer); 1895 for (auto P : enumerate(ExtractedFuncRetVals)) { 1896 BasicBlock *OldTarget = P.value(); 1897 size_t SuccNum = P.index(); 1898 1899 TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context), SuccNum), 1900 OldTarget); 1901 } 1902 1903 // Now that we've done the deed, simplify the switch instruction. 1904 Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType(); 1905 switch (ExtractedFuncRetVals.size()) { 1906 case 0: 1907 // There are no successors (the block containing the switch itself), which 1908 // means that previously this was the last part of the function, and hence 1909 // this should be rewritten as a `ret` or `unreachable`. 1910 if (newFunction->doesNotReturn()) { 1911 // If fn is no return, end with an unreachable terminator. 1912 (void)new UnreachableInst(Context, TheSwitch->getIterator()); 1913 } else if (OldFnRetTy->isVoidTy()) { 1914 // We have no return value. 1915 ReturnInst::Create(Context, nullptr, 1916 TheSwitch->getIterator()); // Return void 1917 } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) { 1918 // return what we have 1919 ReturnInst::Create(Context, TheSwitch->getCondition(), 1920 TheSwitch->getIterator()); 1921 } else { 1922 // Otherwise we must have code extracted an unwind or something, just 1923 // return whatever we want. 1924 ReturnInst::Create(Context, Constant::getNullValue(OldFnRetTy), 1925 TheSwitch->getIterator()); 1926 } 1927 1928 TheSwitch->eraseFromParent(); 1929 break; 1930 case 1: 1931 // Only a single destination, change the switch into an unconditional 1932 // branch. 1933 BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getIterator()); 1934 TheSwitch->eraseFromParent(); 1935 break; 1936 case 2: 1937 // Only two destinations, convert to a condition branch. 1938 // Remark: This also swaps the target branches: 1939 // 0 -> false -> getSuccessor(2); 1 -> true -> getSuccessor(1) 1940 BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2), 1941 call, TheSwitch->getIterator()); 1942 TheSwitch->eraseFromParent(); 1943 break; 1944 default: 1945 // Otherwise, make the default destination of the switch instruction be one 1946 // of the other successors. 1947 TheSwitch->setCondition(call); 1948 TheSwitch->setDefaultDest( 1949 TheSwitch->getSuccessor(ExtractedFuncRetVals.size())); 1950 // Remove redundant case 1951 TheSwitch->removeCase( 1952 SwitchInst::CaseIt(TheSwitch, ExtractedFuncRetVals.size() - 1)); 1953 break; 1954 } 1955 1956 // Insert lifetime markers around the reloads of any output values. The 1957 // allocas output values are stored in are only in-use in the codeRepl block. 1958 insertLifetimeMarkersSurroundingCall(M, ReloadOutputs, ReloadOutputs, call); 1959 1960 // Replicate the effects of any lifetime start/end markers which referenced 1961 // input objects in the extraction region by placing markers around the call. 1962 insertLifetimeMarkersSurroundingCall(oldFunction->getParent(), LifetimesStart, 1963 {}, call); 1964 1965 return call; 1966 } 1967 1968 void CodeExtractor::insertReplacerCall( 1969 Function *oldFunction, BasicBlock *header, BasicBlock *codeReplacer, 1970 const ValueSet &outputs, ArrayRef<Value *> Reloads, 1971 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights) { 1972 1973 // Rewrite branches to basic blocks outside of the loop to new dummy blocks 1974 // within the new function. This must be done before we lose track of which 1975 // blocks were originally in the code region. 1976 std::vector<User *> Users(header->user_begin(), header->user_end()); 1977 for (auto &U : Users) 1978 // The BasicBlock which contains the branch is not in the region 1979 // modify the branch target to a new block 1980 if (Instruction *I = dyn_cast<Instruction>(U)) 1981 if (I->isTerminator() && I->getFunction() == oldFunction && 1982 !Blocks.count(I->getParent())) 1983 I->replaceUsesOfWith(header, codeReplacer); 1984 1985 // When moving the code region it is sufficient to replace all uses to the 1986 // extracted function values. Since the original definition's block 1987 // dominated its use, it will also be dominated by codeReplacer's switch 1988 // which joined multiple exit blocks. 1989 for (BasicBlock *ExitBB : ExtractedFuncRetVals) 1990 for (PHINode &PN : ExitBB->phis()) { 1991 Value *IncomingCodeReplacerVal = nullptr; 1992 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { 1993 // Ignore incoming values from outside of the extracted region. 1994 if (!Blocks.count(PN.getIncomingBlock(i))) 1995 continue; 1996 1997 // Ensure that there is only one incoming value from codeReplacer. 1998 if (!IncomingCodeReplacerVal) { 1999 PN.setIncomingBlock(i, codeReplacer); 2000 IncomingCodeReplacerVal = PN.getIncomingValue(i); 2001 } else 2002 assert(IncomingCodeReplacerVal == PN.getIncomingValue(i) && 2003 "PHI has two incompatbile incoming values from codeRepl"); 2004 } 2005 } 2006 2007 for (unsigned i = 0, e = outputs.size(); i != e; ++i) { 2008 Value *load = Reloads[i]; 2009 std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end()); 2010 for (User *U : Users) { 2011 Instruction *inst = cast<Instruction>(U); 2012 if (inst->getParent()->getParent() == oldFunction) 2013 inst->replaceUsesOfWith(outputs[i], load); 2014 } 2015 } 2016 2017 // Update the branch weights for the exit block. 2018 if (BFI && ExtractedFuncRetVals.size() > 1) 2019 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI); 2020 } 2021 2022 bool CodeExtractor::verifyAssumptionCache(const Function &OldFunc, 2023 const Function &NewFunc, 2024 AssumptionCache *AC) { 2025 for (auto AssumeVH : AC->assumptions()) { 2026 auto *I = dyn_cast_or_null<CallInst>(AssumeVH); 2027 if (!I) 2028 continue; 2029 2030 // There shouldn't be any llvm.assume intrinsics in the new function. 2031 if (I->getFunction() != &OldFunc) 2032 return true; 2033 2034 // There shouldn't be any stale affected values in the assumption cache 2035 // that were previously in the old function, but that have now been moved 2036 // to the new function. 2037 for (auto AffectedValVH : AC->assumptionsFor(I->getOperand(0))) { 2038 auto *AffectedCI = dyn_cast_or_null<CallInst>(AffectedValVH); 2039 if (!AffectedCI) 2040 continue; 2041 if (AffectedCI->getFunction() != &OldFunc) 2042 return true; 2043 auto *AssumedInst = cast<Instruction>(AffectedCI->getOperand(0)); 2044 if (AssumedInst->getFunction() != &OldFunc) 2045 return true; 2046 } 2047 } 2048 return false; 2049 } 2050 2051 void CodeExtractor::excludeArgFromAggregate(Value *Arg) { 2052 ExcludeArgsFromAggregate.insert(Arg); 2053 } 2054