1 //===- CloneFunction.cpp - Clone a function into another 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 CloneFunctionInto interface, which is used as the 11 // low-level function cloner. This is used by the CloneFunction and function 12 // inliner to do the dirty work of copying the body of a function around. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/ADT/SetVector.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/Analysis/ConstantFolding.h" 19 #include "llvm/Analysis/InstructionSimplify.h" 20 #include "llvm/Analysis/LoopInfo.h" 21 #include "llvm/IR/CFG.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/DebugInfo.h" 24 #include "llvm/IR/DerivedTypes.h" 25 #include "llvm/IR/Function.h" 26 #include "llvm/IR/GlobalVariable.h" 27 #include "llvm/IR/Instructions.h" 28 #include "llvm/IR/IntrinsicInst.h" 29 #include "llvm/IR/LLVMContext.h" 30 #include "llvm/IR/Metadata.h" 31 #include "llvm/IR/Module.h" 32 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 33 #include "llvm/Transforms/Utils/Cloning.h" 34 #include "llvm/Transforms/Utils/Local.h" 35 #include "llvm/Transforms/Utils/ValueMapper.h" 36 #include <map> 37 using namespace llvm; 38 39 /// See comments in Cloning.h. 40 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, 41 const Twine &NameSuffix, Function *F, 42 ClonedCodeInfo *CodeInfo, 43 DebugInfoFinder *DIFinder) { 44 DenseMap<const MDNode *, MDNode *> Cache; 45 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F); 46 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 47 48 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 49 50 // Loop over all instructions, and copy them over. 51 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); 52 II != IE; ++II) { 53 54 if (DIFinder && F->getParent() && II->getDebugLoc()) 55 DIFinder->processLocation(*F->getParent(), II->getDebugLoc().get()); 56 57 Instruction *NewInst = II->clone(); 58 if (II->hasName()) 59 NewInst->setName(II->getName()+NameSuffix); 60 NewBB->getInstList().push_back(NewInst); 61 VMap[&*II] = NewInst; // Add instruction map to value. 62 63 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 64 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 65 if (isa<ConstantInt>(AI->getArraySize())) 66 hasStaticAllocas = true; 67 else 68 hasDynamicAllocas = true; 69 } 70 } 71 72 if (CodeInfo) { 73 CodeInfo->ContainsCalls |= hasCalls; 74 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 75 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 76 BB != &BB->getParent()->getEntryBlock(); 77 } 78 return NewBB; 79 } 80 81 // Clone OldFunc into NewFunc, transforming the old arguments into references to 82 // VMap values. 83 // 84 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 85 ValueToValueMapTy &VMap, 86 bool ModuleLevelChanges, 87 SmallVectorImpl<ReturnInst*> &Returns, 88 const char *NameSuffix, ClonedCodeInfo *CodeInfo, 89 ValueMapTypeRemapper *TypeMapper, 90 ValueMaterializer *Materializer) { 91 assert(NameSuffix && "NameSuffix cannot be null!"); 92 93 #ifndef NDEBUG 94 for (const Argument &I : OldFunc->args()) 95 assert(VMap.count(&I) && "No mapping from source argument specified!"); 96 #endif 97 98 // Copy all attributes other than those stored in the AttributeList. We need 99 // to remap the parameter indices of the AttributeList. 100 AttributeList NewAttrs = NewFunc->getAttributes(); 101 NewFunc->copyAttributesFrom(OldFunc); 102 NewFunc->setAttributes(NewAttrs); 103 104 // Fix up the personality function that got copied over. 105 if (OldFunc->hasPersonalityFn()) 106 NewFunc->setPersonalityFn( 107 MapValue(OldFunc->getPersonalityFn(), VMap, 108 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 109 TypeMapper, Materializer)); 110 111 SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size()); 112 AttributeList OldAttrs = OldFunc->getAttributes(); 113 114 // Clone any argument attributes that are present in the VMap. 115 for (const Argument &OldArg : OldFunc->args()) { 116 if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) { 117 NewArgAttrs[NewArg->getArgNo()] = 118 OldAttrs.getParamAttributes(OldArg.getArgNo()); 119 } 120 } 121 122 NewFunc->setAttributes( 123 AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(), 124 OldAttrs.getRetAttributes(), NewArgAttrs)); 125 126 bool MustCloneSP = 127 OldFunc->getParent() && OldFunc->getParent() == NewFunc->getParent(); 128 DISubprogram *SP = OldFunc->getSubprogram(); 129 if (SP) { 130 assert(!MustCloneSP || ModuleLevelChanges); 131 // Add mappings for some DebugInfo nodes that we don't want duplicated 132 // even if they're distinct. 133 auto &MD = VMap.MD(); 134 MD[SP->getUnit()].reset(SP->getUnit()); 135 MD[SP->getType()].reset(SP->getType()); 136 MD[SP->getFile()].reset(SP->getFile()); 137 // If we're not cloning into the same module, no need to clone the 138 // subprogram 139 if (!MustCloneSP) 140 MD[SP].reset(SP); 141 } 142 143 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 144 OldFunc->getAllMetadata(MDs); 145 for (auto MD : MDs) { 146 NewFunc->addMetadata( 147 MD.first, 148 *MapMetadata(MD.second, VMap, 149 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 150 TypeMapper, Materializer)); 151 } 152 153 // When we remap instructions, we want to avoid duplicating inlined 154 // DISubprograms, so record all subprograms we find as we duplicate 155 // instructions and then freeze them in the MD map. 156 DebugInfoFinder DIFinder; 157 158 // Loop over all of the basic blocks in the function, cloning them as 159 // appropriate. Note that we save BE this way in order to handle cloning of 160 // recursive functions into themselves. 161 // 162 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 163 BI != BE; ++BI) { 164 const BasicBlock &BB = *BI; 165 166 // Create a new basic block and copy instructions into it! 167 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo, 168 SP ? &DIFinder : nullptr); 169 170 // Add basic block mapping. 171 VMap[&BB] = CBB; 172 173 // It is only legal to clone a function if a block address within that 174 // function is never referenced outside of the function. Given that, we 175 // want to map block addresses from the old function to block addresses in 176 // the clone. (This is different from the generic ValueMapper 177 // implementation, which generates an invalid blockaddress when 178 // cloning a function.) 179 if (BB.hasAddressTaken()) { 180 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 181 const_cast<BasicBlock*>(&BB)); 182 VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); 183 } 184 185 // Note return instructions for the caller. 186 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) 187 Returns.push_back(RI); 188 } 189 190 for (DISubprogram *ISP : DIFinder.subprograms()) { 191 if (ISP != SP) { 192 VMap.MD()[ISP].reset(ISP); 193 } 194 } 195 196 // Loop over all of the instructions in the function, fixing up operand 197 // references as we go. This uses VMap to do all the hard work. 198 for (Function::iterator BB = 199 cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(), 200 BE = NewFunc->end(); 201 BB != BE; ++BB) 202 // Loop over all instructions, fixing each one as we find it... 203 for (Instruction &II : *BB) 204 RemapInstruction(&II, VMap, 205 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 206 TypeMapper, Materializer); 207 } 208 209 /// Return a copy of the specified function and add it to that function's 210 /// module. Also, any references specified in the VMap are changed to refer to 211 /// their mapped value instead of the original one. If any of the arguments to 212 /// the function are in the VMap, the arguments are deleted from the resultant 213 /// function. The VMap is updated to include mappings from all of the 214 /// instructions and basicblocks in the function from their old to new values. 215 /// 216 Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap, 217 ClonedCodeInfo *CodeInfo) { 218 std::vector<Type*> ArgTypes; 219 220 // The user might be deleting arguments to the function by specifying them in 221 // the VMap. If so, we need to not add the arguments to the arg ty vector 222 // 223 for (const Argument &I : F->args()) 224 if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet? 225 ArgTypes.push_back(I.getType()); 226 227 // Create a new function type... 228 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), 229 ArgTypes, F->getFunctionType()->isVarArg()); 230 231 // Create the new function... 232 Function *NewF = 233 Function::Create(FTy, F->getLinkage(), F->getName(), F->getParent()); 234 235 // Loop over the arguments, copying the names of the mapped arguments over... 236 Function::arg_iterator DestI = NewF->arg_begin(); 237 for (const Argument & I : F->args()) 238 if (VMap.count(&I) == 0) { // Is this argument preserved? 239 DestI->setName(I.getName()); // Copy the name over... 240 VMap[&I] = &*DestI++; // Add mapping to VMap 241 } 242 243 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. 244 CloneFunctionInto(NewF, F, VMap, F->getSubprogram() != nullptr, Returns, "", 245 CodeInfo); 246 247 return NewF; 248 } 249 250 251 252 namespace { 253 /// This is a private class used to implement CloneAndPruneFunctionInto. 254 struct PruningFunctionCloner { 255 Function *NewFunc; 256 const Function *OldFunc; 257 ValueToValueMapTy &VMap; 258 bool ModuleLevelChanges; 259 const char *NameSuffix; 260 ClonedCodeInfo *CodeInfo; 261 262 public: 263 PruningFunctionCloner(Function *newFunc, const Function *oldFunc, 264 ValueToValueMapTy &valueMap, bool moduleLevelChanges, 265 const char *nameSuffix, ClonedCodeInfo *codeInfo) 266 : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap), 267 ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix), 268 CodeInfo(codeInfo) {} 269 270 /// The specified block is found to be reachable, clone it and 271 /// anything that it can reach. 272 void CloneBlock(const BasicBlock *BB, 273 BasicBlock::const_iterator StartingInst, 274 std::vector<const BasicBlock*> &ToClone); 275 }; 276 } 277 278 /// The specified block is found to be reachable, clone it and 279 /// anything that it can reach. 280 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, 281 BasicBlock::const_iterator StartingInst, 282 std::vector<const BasicBlock*> &ToClone){ 283 WeakTrackingVH &BBEntry = VMap[BB]; 284 285 // Have we already cloned this block? 286 if (BBEntry) return; 287 288 // Nope, clone it now. 289 BasicBlock *NewBB; 290 BBEntry = NewBB = BasicBlock::Create(BB->getContext()); 291 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 292 293 // It is only legal to clone a function if a block address within that 294 // function is never referenced outside of the function. Given that, we 295 // want to map block addresses from the old function to block addresses in 296 // the clone. (This is different from the generic ValueMapper 297 // implementation, which generates an invalid blockaddress when 298 // cloning a function.) 299 // 300 // Note that we don't need to fix the mapping for unreachable blocks; 301 // the default mapping there is safe. 302 if (BB->hasAddressTaken()) { 303 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 304 const_cast<BasicBlock*>(BB)); 305 VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB); 306 } 307 308 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 309 310 // Loop over all instructions, and copy them over, DCE'ing as we go. This 311 // loop doesn't include the terminator. 312 for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); 313 II != IE; ++II) { 314 315 Instruction *NewInst = II->clone(); 316 317 // Eagerly remap operands to the newly cloned instruction, except for PHI 318 // nodes for which we defer processing until we update the CFG. 319 if (!isa<PHINode>(NewInst)) { 320 RemapInstruction(NewInst, VMap, 321 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 322 323 // If we can simplify this instruction to some other value, simply add 324 // a mapping to that value rather than inserting a new instruction into 325 // the basic block. 326 if (Value *V = 327 SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) { 328 // On the off-chance that this simplifies to an instruction in the old 329 // function, map it back into the new function. 330 if (Value *MappedV = VMap.lookup(V)) 331 V = MappedV; 332 333 if (!NewInst->mayHaveSideEffects()) { 334 VMap[&*II] = V; 335 NewInst->deleteValue(); 336 continue; 337 } 338 } 339 } 340 341 if (II->hasName()) 342 NewInst->setName(II->getName()+NameSuffix); 343 VMap[&*II] = NewInst; // Add instruction map to value. 344 NewBB->getInstList().push_back(NewInst); 345 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 346 347 if (CodeInfo) 348 if (auto CS = ImmutableCallSite(&*II)) 349 if (CS.hasOperandBundles()) 350 CodeInfo->OperandBundleCallSites.push_back(NewInst); 351 352 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 353 if (isa<ConstantInt>(AI->getArraySize())) 354 hasStaticAllocas = true; 355 else 356 hasDynamicAllocas = true; 357 } 358 } 359 360 // Finally, clone over the terminator. 361 const TerminatorInst *OldTI = BB->getTerminator(); 362 bool TerminatorDone = false; 363 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { 364 if (BI->isConditional()) { 365 // If the condition was a known constant in the callee... 366 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 367 // Or is a known constant in the caller... 368 if (!Cond) { 369 Value *V = VMap.lookup(BI->getCondition()); 370 Cond = dyn_cast_or_null<ConstantInt>(V); 371 } 372 373 // Constant fold to uncond branch! 374 if (Cond) { 375 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); 376 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 377 ToClone.push_back(Dest); 378 TerminatorDone = true; 379 } 380 } 381 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { 382 // If switching on a value known constant in the caller. 383 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); 384 if (!Cond) { // Or known constant after constant prop in the callee... 385 Value *V = VMap.lookup(SI->getCondition()); 386 Cond = dyn_cast_or_null<ConstantInt>(V); 387 } 388 if (Cond) { // Constant fold to uncond branch! 389 SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond); 390 BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor()); 391 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 392 ToClone.push_back(Dest); 393 TerminatorDone = true; 394 } 395 } 396 397 if (!TerminatorDone) { 398 Instruction *NewInst = OldTI->clone(); 399 if (OldTI->hasName()) 400 NewInst->setName(OldTI->getName()+NameSuffix); 401 NewBB->getInstList().push_back(NewInst); 402 VMap[OldTI] = NewInst; // Add instruction map to value. 403 404 if (CodeInfo) 405 if (auto CS = ImmutableCallSite(OldTI)) 406 if (CS.hasOperandBundles()) 407 CodeInfo->OperandBundleCallSites.push_back(NewInst); 408 409 // Recursively clone any reachable successor blocks. 410 const TerminatorInst *TI = BB->getTerminator(); 411 for (const BasicBlock *Succ : TI->successors()) 412 ToClone.push_back(Succ); 413 } 414 415 if (CodeInfo) { 416 CodeInfo->ContainsCalls |= hasCalls; 417 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 418 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 419 BB != &BB->getParent()->front(); 420 } 421 } 422 423 /// This works like CloneAndPruneFunctionInto, except that it does not clone the 424 /// entire function. Instead it starts at an instruction provided by the caller 425 /// and copies (and prunes) only the code reachable from that instruction. 426 void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, 427 const Instruction *StartingInst, 428 ValueToValueMapTy &VMap, 429 bool ModuleLevelChanges, 430 SmallVectorImpl<ReturnInst *> &Returns, 431 const char *NameSuffix, 432 ClonedCodeInfo *CodeInfo) { 433 assert(NameSuffix && "NameSuffix cannot be null!"); 434 435 ValueMapTypeRemapper *TypeMapper = nullptr; 436 ValueMaterializer *Materializer = nullptr; 437 438 #ifndef NDEBUG 439 // If the cloning starts at the beginning of the function, verify that 440 // the function arguments are mapped. 441 if (!StartingInst) 442 for (const Argument &II : OldFunc->args()) 443 assert(VMap.count(&II) && "No mapping from source argument specified!"); 444 #endif 445 446 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, 447 NameSuffix, CodeInfo); 448 const BasicBlock *StartingBB; 449 if (StartingInst) 450 StartingBB = StartingInst->getParent(); 451 else { 452 StartingBB = &OldFunc->getEntryBlock(); 453 StartingInst = &StartingBB->front(); 454 } 455 456 // Clone the entry block, and anything recursively reachable from it. 457 std::vector<const BasicBlock*> CloneWorklist; 458 PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist); 459 while (!CloneWorklist.empty()) { 460 const BasicBlock *BB = CloneWorklist.back(); 461 CloneWorklist.pop_back(); 462 PFC.CloneBlock(BB, BB->begin(), CloneWorklist); 463 } 464 465 // Loop over all of the basic blocks in the old function. If the block was 466 // reachable, we have cloned it and the old block is now in the value map: 467 // insert it into the new function in the right order. If not, ignore it. 468 // 469 // Defer PHI resolution until rest of function is resolved. 470 SmallVector<const PHINode*, 16> PHIToResolve; 471 for (const BasicBlock &BI : *OldFunc) { 472 Value *V = VMap.lookup(&BI); 473 BasicBlock *NewBB = cast_or_null<BasicBlock>(V); 474 if (!NewBB) continue; // Dead block. 475 476 // Add the new block to the new function. 477 NewFunc->getBasicBlockList().push_back(NewBB); 478 479 // Handle PHI nodes specially, as we have to remove references to dead 480 // blocks. 481 for (BasicBlock::const_iterator I = BI.begin(), E = BI.end(); I != E; ++I) { 482 // PHI nodes may have been remapped to non-PHI nodes by the caller or 483 // during the cloning process. 484 if (const PHINode *PN = dyn_cast<PHINode>(I)) { 485 if (isa<PHINode>(VMap[PN])) 486 PHIToResolve.push_back(PN); 487 else 488 break; 489 } else { 490 break; 491 } 492 } 493 494 // Finally, remap the terminator instructions, as those can't be remapped 495 // until all BBs are mapped. 496 RemapInstruction(NewBB->getTerminator(), VMap, 497 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 498 TypeMapper, Materializer); 499 } 500 501 // Defer PHI resolution until rest of function is resolved, PHI resolution 502 // requires the CFG to be up-to-date. 503 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { 504 const PHINode *OPN = PHIToResolve[phino]; 505 unsigned NumPreds = OPN->getNumIncomingValues(); 506 const BasicBlock *OldBB = OPN->getParent(); 507 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); 508 509 // Map operands for blocks that are live and remove operands for blocks 510 // that are dead. 511 for (; phino != PHIToResolve.size() && 512 PHIToResolve[phino]->getParent() == OldBB; ++phino) { 513 OPN = PHIToResolve[phino]; 514 PHINode *PN = cast<PHINode>(VMap[OPN]); 515 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { 516 Value *V = VMap.lookup(PN->getIncomingBlock(pred)); 517 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { 518 Value *InVal = MapValue(PN->getIncomingValue(pred), 519 VMap, 520 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 521 assert(InVal && "Unknown input value?"); 522 PN->setIncomingValue(pred, InVal); 523 PN->setIncomingBlock(pred, MappedBlock); 524 } else { 525 PN->removeIncomingValue(pred, false); 526 --pred; // Revisit the next entry. 527 --e; 528 } 529 } 530 } 531 532 // The loop above has removed PHI entries for those blocks that are dead 533 // and has updated others. However, if a block is live (i.e. copied over) 534 // but its terminator has been changed to not go to this block, then our 535 // phi nodes will have invalid entries. Update the PHI nodes in this 536 // case. 537 PHINode *PN = cast<PHINode>(NewBB->begin()); 538 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); 539 if (NumPreds != PN->getNumIncomingValues()) { 540 assert(NumPreds < PN->getNumIncomingValues()); 541 // Count how many times each predecessor comes to this block. 542 std::map<BasicBlock*, unsigned> PredCount; 543 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); 544 PI != E; ++PI) 545 --PredCount[*PI]; 546 547 // Figure out how many entries to remove from each PHI. 548 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 549 ++PredCount[PN->getIncomingBlock(i)]; 550 551 // At this point, the excess predecessor entries are positive in the 552 // map. Loop over all of the PHIs and remove excess predecessor 553 // entries. 554 BasicBlock::iterator I = NewBB->begin(); 555 for (; (PN = dyn_cast<PHINode>(I)); ++I) { 556 for (const auto &PCI : PredCount) { 557 BasicBlock *Pred = PCI.first; 558 for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove) 559 PN->removeIncomingValue(Pred, false); 560 } 561 } 562 } 563 564 // If the loops above have made these phi nodes have 0 or 1 operand, 565 // replace them with undef or the input value. We must do this for 566 // correctness, because 0-operand phis are not valid. 567 PN = cast<PHINode>(NewBB->begin()); 568 if (PN->getNumIncomingValues() == 0) { 569 BasicBlock::iterator I = NewBB->begin(); 570 BasicBlock::const_iterator OldI = OldBB->begin(); 571 while ((PN = dyn_cast<PHINode>(I++))) { 572 Value *NV = UndefValue::get(PN->getType()); 573 PN->replaceAllUsesWith(NV); 574 assert(VMap[&*OldI] == PN && "VMap mismatch"); 575 VMap[&*OldI] = NV; 576 PN->eraseFromParent(); 577 ++OldI; 578 } 579 } 580 } 581 582 // Make a second pass over the PHINodes now that all of them have been 583 // remapped into the new function, simplifying the PHINode and performing any 584 // recursive simplifications exposed. This will transparently update the 585 // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce 586 // two PHINodes, the iteration over the old PHIs remains valid, and the 587 // mapping will just map us to the new node (which may not even be a PHI 588 // node). 589 const DataLayout &DL = NewFunc->getParent()->getDataLayout(); 590 SmallSetVector<const Value *, 8> Worklist; 591 for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx) 592 if (isa<PHINode>(VMap[PHIToResolve[Idx]])) 593 Worklist.insert(PHIToResolve[Idx]); 594 595 // Note that we must test the size on each iteration, the worklist can grow. 596 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { 597 const Value *OrigV = Worklist[Idx]; 598 auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV)); 599 if (!I) 600 continue; 601 602 // Skip over non-intrinsic callsites, we don't want to remove any nodes from 603 // the CGSCC. 604 CallSite CS = CallSite(I); 605 if (CS && CS.getCalledFunction() && !CS.getCalledFunction()->isIntrinsic()) 606 continue; 607 608 // See if this instruction simplifies. 609 Value *SimpleV = SimplifyInstruction(I, DL); 610 if (!SimpleV) 611 continue; 612 613 // Stash away all the uses of the old instruction so we can check them for 614 // recursive simplifications after a RAUW. This is cheaper than checking all 615 // uses of To on the recursive step in most cases. 616 for (const User *U : OrigV->users()) 617 Worklist.insert(cast<Instruction>(U)); 618 619 // Replace the instruction with its simplified value. 620 I->replaceAllUsesWith(SimpleV); 621 622 // If the original instruction had no side effects, remove it. 623 if (isInstructionTriviallyDead(I)) 624 I->eraseFromParent(); 625 else 626 VMap[OrigV] = I; 627 } 628 629 // Now that the inlined function body has been fully constructed, go through 630 // and zap unconditional fall-through branches. This happens all the time when 631 // specializing code: code specialization turns conditional branches into 632 // uncond branches, and this code folds them. 633 Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator(); 634 Function::iterator I = Begin; 635 while (I != NewFunc->end()) { 636 // Check if this block has become dead during inlining or other 637 // simplifications. Note that the first block will appear dead, as it has 638 // not yet been wired up properly. 639 if (I != Begin && (pred_begin(&*I) == pred_end(&*I) || 640 I->getSinglePredecessor() == &*I)) { 641 BasicBlock *DeadBB = &*I++; 642 DeleteDeadBlock(DeadBB); 643 continue; 644 } 645 646 // We need to simplify conditional branches and switches with a constant 647 // operand. We try to prune these out when cloning, but if the 648 // simplification required looking through PHI nodes, those are only 649 // available after forming the full basic block. That may leave some here, 650 // and we still want to prune the dead code as early as possible. 651 ConstantFoldTerminator(&*I); 652 653 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); 654 if (!BI || BI->isConditional()) { ++I; continue; } 655 656 BasicBlock *Dest = BI->getSuccessor(0); 657 if (!Dest->getSinglePredecessor()) { 658 ++I; continue; 659 } 660 661 // We shouldn't be able to get single-entry PHI nodes here, as instsimplify 662 // above should have zapped all of them.. 663 assert(!isa<PHINode>(Dest->begin())); 664 665 // We know all single-entry PHI nodes in the inlined function have been 666 // removed, so we just need to splice the blocks. 667 BI->eraseFromParent(); 668 669 // Make all PHI nodes that referred to Dest now refer to I as their source. 670 Dest->replaceAllUsesWith(&*I); 671 672 // Move all the instructions in the succ to the pred. 673 I->getInstList().splice(I->end(), Dest->getInstList()); 674 675 // Remove the dest block. 676 Dest->eraseFromParent(); 677 678 // Do not increment I, iteratively merge all things this block branches to. 679 } 680 681 // Make a final pass over the basic blocks from the old function to gather 682 // any return instructions which survived folding. We have to do this here 683 // because we can iteratively remove and merge returns above. 684 for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(), 685 E = NewFunc->end(); 686 I != E; ++I) 687 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) 688 Returns.push_back(RI); 689 } 690 691 692 /// This works exactly like CloneFunctionInto, 693 /// except that it does some simple constant prop and DCE on the fly. The 694 /// effect of this is to copy significantly less code in cases where (for 695 /// example) a function call with constant arguments is inlined, and those 696 /// constant arguments cause a significant amount of code in the callee to be 697 /// dead. Since this doesn't produce an exact copy of the input, it can't be 698 /// used for things like CloneFunction or CloneModule. 699 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 700 ValueToValueMapTy &VMap, 701 bool ModuleLevelChanges, 702 SmallVectorImpl<ReturnInst*> &Returns, 703 const char *NameSuffix, 704 ClonedCodeInfo *CodeInfo, 705 Instruction *TheCall) { 706 CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap, 707 ModuleLevelChanges, Returns, NameSuffix, CodeInfo); 708 } 709 710 /// \brief Remaps instructions in \p Blocks using the mapping in \p VMap. 711 void llvm::remapInstructionsInBlocks( 712 const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) { 713 // Rewrite the code to refer to itself. 714 for (auto *BB : Blocks) 715 for (auto &Inst : *BB) 716 RemapInstruction(&Inst, VMap, 717 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); 718 } 719 720 /// \brief Clones a loop \p OrigLoop. Returns the loop and the blocks in \p 721 /// Blocks. 722 /// 723 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block 724 /// \p LoopDomBB. Insert the new blocks before block specified in \p Before. 725 Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, 726 Loop *OrigLoop, ValueToValueMapTy &VMap, 727 const Twine &NameSuffix, LoopInfo *LI, 728 DominatorTree *DT, 729 SmallVectorImpl<BasicBlock *> &Blocks) { 730 assert(OrigLoop->getSubLoops().empty() && 731 "Loop to be cloned cannot have inner loop"); 732 Function *F = OrigLoop->getHeader()->getParent(); 733 Loop *ParentLoop = OrigLoop->getParentLoop(); 734 735 Loop *NewLoop = new Loop(); 736 if (ParentLoop) 737 ParentLoop->addChildLoop(NewLoop); 738 else 739 LI->addTopLevelLoop(NewLoop); 740 741 BasicBlock *OrigPH = OrigLoop->getLoopPreheader(); 742 assert(OrigPH && "No preheader"); 743 BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F); 744 // To rename the loop PHIs. 745 VMap[OrigPH] = NewPH; 746 Blocks.push_back(NewPH); 747 748 // Update LoopInfo. 749 if (ParentLoop) 750 ParentLoop->addBasicBlockToLoop(NewPH, *LI); 751 752 // Update DominatorTree. 753 DT->addNewBlock(NewPH, LoopDomBB); 754 755 for (BasicBlock *BB : OrigLoop->getBlocks()) { 756 BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F); 757 VMap[BB] = NewBB; 758 759 // Update LoopInfo. 760 NewLoop->addBasicBlockToLoop(NewBB, *LI); 761 762 // Add DominatorTree node. After seeing all blocks, update to correct IDom. 763 DT->addNewBlock(NewBB, NewPH); 764 765 Blocks.push_back(NewBB); 766 } 767 768 for (BasicBlock *BB : OrigLoop->getBlocks()) { 769 // Update DominatorTree. 770 BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock(); 771 DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]), 772 cast<BasicBlock>(VMap[IDomBB])); 773 } 774 775 // Move them physically from the end of the block list. 776 F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(), 777 NewPH); 778 F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(), 779 NewLoop->getHeader()->getIterator(), F->end()); 780 781 return NewLoop; 782 } 783 784 /// \brief Duplicate non-Phi instructions from the beginning of block up to 785 /// StopAt instruction into a split block between BB and its predecessor. 786 BasicBlock * 787 llvm::DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB, 788 Instruction *StopAt, 789 ValueToValueMapTy &ValueMapping) { 790 // We are going to have to map operands from the original BB block to the new 791 // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to 792 // account for entry from PredBB. 793 BasicBlock::iterator BI = BB->begin(); 794 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 795 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 796 797 BasicBlock *NewBB = SplitEdge(PredBB, BB); 798 NewBB->setName(PredBB->getName() + ".split"); 799 Instruction *NewTerm = NewBB->getTerminator(); 800 801 // Clone the non-phi instructions of BB into NewBB, keeping track of the 802 // mapping and using it to remap operands in the cloned instructions. 803 for (; StopAt != &*BI; ++BI) { 804 Instruction *New = BI->clone(); 805 New->setName(BI->getName()); 806 New->insertBefore(NewTerm); 807 ValueMapping[&*BI] = New; 808 809 // Remap operands to patch up intra-block references. 810 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 811 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 812 auto I = ValueMapping.find(Inst); 813 if (I != ValueMapping.end()) 814 New->setOperand(i, I->second); 815 } 816 } 817 818 return NewBB; 819 } 820