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