1 //===- CloneFunction.cpp - Clone a function into another 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 CloneFunctionInto interface, which is used as the 10 // low-level function cloner. This is used by the CloneFunction and function 11 // inliner to do the dirty work of copying the body of a function around. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/ADT/SetVector.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/Analysis/ConstantFolding.h" 18 #include "llvm/Analysis/DomTreeUpdater.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/MDBuilder.h" 31 #include "llvm/IR/Metadata.h" 32 #include "llvm/IR/Module.h" 33 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 34 #include "llvm/Transforms/Utils/Cloning.h" 35 #include "llvm/Transforms/Utils/Local.h" 36 #include "llvm/Transforms/Utils/ValueMapper.h" 37 #include <map> 38 using namespace llvm; 39 40 #define DEBUG_TYPE "clone-function" 41 42 /// See comments in Cloning.h. 43 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, 44 const Twine &NameSuffix, Function *F, 45 ClonedCodeInfo *CodeInfo, 46 DebugInfoFinder *DIFinder) { 47 DenseMap<const MDNode *, MDNode *> Cache; 48 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F); 49 if (BB->hasName()) 50 NewBB->setName(BB->getName() + NameSuffix); 51 52 bool hasCalls = false, hasDynamicAllocas = false; 53 Module *TheModule = F ? F->getParent() : nullptr; 54 55 // Loop over all instructions, and copy them over. 56 for (const Instruction &I : *BB) { 57 if (DIFinder && TheModule) 58 DIFinder->processInstruction(*TheModule, I); 59 60 Instruction *NewInst = I.clone(); 61 if (I.hasName()) 62 NewInst->setName(I.getName() + NameSuffix); 63 NewBB->getInstList().push_back(NewInst); 64 VMap[&I] = NewInst; // Add instruction map to value. 65 66 hasCalls |= (isa<CallInst>(I) && !isa<DbgInfoIntrinsic>(I)); 67 if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 68 if (!AI->isStaticAlloca()) { 69 hasDynamicAllocas = true; 70 } 71 } 72 } 73 74 if (CodeInfo) { 75 CodeInfo->ContainsCalls |= hasCalls; 76 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 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 CloneFunctionChangeType Changes, 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 bool ModuleLevelChanges = Changes > CloneFunctionChangeType::LocalChangesOnly; 99 100 // Copy all attributes other than those stored in the AttributeList. We need 101 // to remap the parameter indices of the AttributeList. 102 AttributeList NewAttrs = NewFunc->getAttributes(); 103 NewFunc->copyAttributesFrom(OldFunc); 104 NewFunc->setAttributes(NewAttrs); 105 106 // Fix up the personality function that got copied over. 107 if (OldFunc->hasPersonalityFn()) 108 NewFunc->setPersonalityFn( 109 MapValue(OldFunc->getPersonalityFn(), VMap, 110 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 111 TypeMapper, Materializer)); 112 113 SmallVector<AttributeSet, 4> NewArgAttrs(NewFunc->arg_size()); 114 AttributeList OldAttrs = OldFunc->getAttributes(); 115 116 // Clone any argument attributes that are present in the VMap. 117 for (const Argument &OldArg : OldFunc->args()) { 118 if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) { 119 NewArgAttrs[NewArg->getArgNo()] = 120 OldAttrs.getParamAttributes(OldArg.getArgNo()); 121 } 122 } 123 124 NewFunc->setAttributes( 125 AttributeList::get(NewFunc->getContext(), OldAttrs.getFnAttributes(), 126 OldAttrs.getRetAttributes(), NewArgAttrs)); 127 128 // When we remap instructions within the same module, we want to avoid 129 // duplicating inlined DISubprograms, so record all subprograms we find as we 130 // duplicate instructions and then freeze them in the MD map. We also record 131 // information about dbg.value and dbg.declare to avoid duplicating the 132 // types. 133 Optional<DebugInfoFinder> DIFinder; 134 135 // Track the subprogram attachment that needs to be cloned to fine-tune the 136 // mapping within the same module. 137 DISubprogram *SPClonedWithinModule = nullptr; 138 if (Changes < CloneFunctionChangeType::DifferentModule) { 139 assert((NewFunc->getParent() == nullptr || 140 NewFunc->getParent() == OldFunc->getParent()) && 141 "Expected NewFunc to have the same parent, or no parent"); 142 143 // Need to find subprograms, types, and compile units. 144 DIFinder.emplace(); 145 146 SPClonedWithinModule = OldFunc->getSubprogram(); 147 if (SPClonedWithinModule) 148 DIFinder->processSubprogram(SPClonedWithinModule); 149 } else { 150 assert((NewFunc->getParent() == nullptr || 151 NewFunc->getParent() != OldFunc->getParent()) && 152 "Set SameModule to true if the new function is in the same module"); 153 154 if (Changes == CloneFunctionChangeType::DifferentModule) { 155 assert(NewFunc->getParent() && 156 "Need parent of new function to maintain debug info invariants"); 157 158 // Need to find all the compile units. 159 DIFinder.emplace(); 160 } 161 } 162 163 // Everything else beyond this point deals with function instructions, 164 // so if we are dealing with a function declaration, we're done. 165 if (OldFunc->isDeclaration()) 166 return; 167 168 // Loop over all of the basic blocks in the function, cloning them as 169 // appropriate. Note that we save BE this way in order to handle cloning of 170 // recursive functions into themselves. 171 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 172 BI != BE; ++BI) { 173 const BasicBlock &BB = *BI; 174 175 // Create a new basic block and copy instructions into it! 176 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo, 177 DIFinder ? &*DIFinder : nullptr); 178 179 // Add basic block mapping. 180 VMap[&BB] = CBB; 181 182 // It is only legal to clone a function if a block address within that 183 // function is never referenced outside of the function. Given that, we 184 // want to map block addresses from the old function to block addresses in 185 // the clone. (This is different from the generic ValueMapper 186 // implementation, which generates an invalid blockaddress when 187 // cloning a function.) 188 if (BB.hasAddressTaken()) { 189 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 190 const_cast<BasicBlock*>(&BB)); 191 VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); 192 } 193 194 // Note return instructions for the caller. 195 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) 196 Returns.push_back(RI); 197 } 198 199 if (Changes < CloneFunctionChangeType::DifferentModule && 200 DIFinder->subprogram_count() > 0) { 201 // Turn on module-level changes, since we need to clone (some of) the 202 // debug info metadata. 203 // 204 // FIXME: Metadata effectively owned by a function should be made 205 // local, and only that local metadata should be cloned. 206 ModuleLevelChanges = true; 207 208 auto mapToSelfIfNew = [&VMap](MDNode *N) { 209 // Avoid clobbering an existing mapping. 210 (void)VMap.MD().try_emplace(N, N); 211 }; 212 213 // Avoid cloning types, compile units, and (other) subprograms. 214 for (DISubprogram *ISP : DIFinder->subprograms()) 215 if (ISP != SPClonedWithinModule) 216 mapToSelfIfNew(ISP); 217 218 for (DICompileUnit *CU : DIFinder->compile_units()) 219 mapToSelfIfNew(CU); 220 221 for (DIType *Type : DIFinder->types()) 222 mapToSelfIfNew(Type); 223 } else { 224 assert(!SPClonedWithinModule && 225 "Subprogram should be in DIFinder->subprogram_count()..."); 226 } 227 228 // Duplicate the metadata that is attached to the cloned function. 229 // Subprograms/CUs/types that were already mapped to themselves won't be 230 // duplicated. 231 SmallVector<std::pair<unsigned, MDNode *>, 1> MDs; 232 OldFunc->getAllMetadata(MDs); 233 for (auto MD : MDs) { 234 NewFunc->addMetadata( 235 MD.first, 236 *MapMetadata(MD.second, VMap, 237 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 238 TypeMapper, Materializer)); 239 } 240 241 // Loop over all of the instructions in the function, fixing up operand 242 // references as we go. This uses VMap to do all the hard work. 243 for (Function::iterator BB = 244 cast<BasicBlock>(VMap[&OldFunc->front()])->getIterator(), 245 BE = NewFunc->end(); 246 BB != BE; ++BB) 247 // Loop over all instructions, fixing each one as we find it... 248 for (Instruction &II : *BB) 249 RemapInstruction(&II, VMap, 250 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 251 TypeMapper, Materializer); 252 253 // Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the 254 // same module, the compile unit will already be listed (or not). When 255 // cloning a module, CloneModule() will handle creating the named metadata. 256 if (Changes != CloneFunctionChangeType::DifferentModule) 257 return; 258 259 // Update !llvm.dbg.cu with compile units added to the new module if this 260 // function is being cloned in isolation. 261 // 262 // FIXME: This is making global / module-level changes, which doesn't seem 263 // like the right encapsulation Consider dropping the requirement to update 264 // !llvm.dbg.cu (either obsoleting the node, or restricting it to 265 // non-discardable compile units) instead of discovering compile units by 266 // visiting the metadata attached to global values, which would allow this 267 // code to be deleted. Alternatively, perhaps give responsibility for this 268 // update to CloneFunctionInto's callers. 269 auto* NewModule = NewFunc->getParent(); 270 auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu"); 271 // Avoid multiple insertions of the same DICompileUnit to NMD. 272 SmallPtrSet<const void *, 8> Visited; 273 for (auto *Operand : NMD->operands()) 274 Visited.insert(Operand); 275 for (auto *Unit : DIFinder->compile_units()) { 276 MDNode *MappedUnit = 277 MapMetadata(Unit, VMap, RF_None, TypeMapper, Materializer); 278 if (Visited.insert(MappedUnit).second) 279 NMD->addOperand(MappedUnit); 280 } 281 } 282 283 /// Return a copy of the specified function and add it to that function's 284 /// module. Also, any references specified in the VMap are changed to refer to 285 /// their mapped value instead of the original one. If any of the arguments to 286 /// the function are in the VMap, the arguments are deleted from the resultant 287 /// function. The VMap is updated to include mappings from all of the 288 /// instructions and basicblocks in the function from their old to new values. 289 /// 290 Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap, 291 ClonedCodeInfo *CodeInfo) { 292 std::vector<Type*> ArgTypes; 293 294 // The user might be deleting arguments to the function by specifying them in 295 // the VMap. If so, we need to not add the arguments to the arg ty vector 296 // 297 for (const Argument &I : F->args()) 298 if (VMap.count(&I) == 0) // Haven't mapped the argument to anything yet? 299 ArgTypes.push_back(I.getType()); 300 301 // Create a new function type... 302 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), 303 ArgTypes, F->getFunctionType()->isVarArg()); 304 305 // Create the new function... 306 Function *NewF = Function::Create(FTy, F->getLinkage(), F->getAddressSpace(), 307 F->getName(), F->getParent()); 308 309 // Loop over the arguments, copying the names of the mapped arguments over... 310 Function::arg_iterator DestI = NewF->arg_begin(); 311 for (const Argument & I : F->args()) 312 if (VMap.count(&I) == 0) { // Is this argument preserved? 313 DestI->setName(I.getName()); // Copy the name over... 314 VMap[&I] = &*DestI++; // Add mapping to VMap 315 } 316 317 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. 318 CloneFunctionInto(NewF, F, VMap, CloneFunctionChangeType::LocalChangesOnly, 319 Returns, "", CodeInfo); 320 321 return NewF; 322 } 323 324 325 326 namespace { 327 /// This is a private class used to implement CloneAndPruneFunctionInto. 328 struct PruningFunctionCloner { 329 Function *NewFunc; 330 const Function *OldFunc; 331 ValueToValueMapTy &VMap; 332 bool ModuleLevelChanges; 333 const char *NameSuffix; 334 ClonedCodeInfo *CodeInfo; 335 336 public: 337 PruningFunctionCloner(Function *newFunc, const Function *oldFunc, 338 ValueToValueMapTy &valueMap, bool moduleLevelChanges, 339 const char *nameSuffix, ClonedCodeInfo *codeInfo) 340 : NewFunc(newFunc), OldFunc(oldFunc), VMap(valueMap), 341 ModuleLevelChanges(moduleLevelChanges), NameSuffix(nameSuffix), 342 CodeInfo(codeInfo) {} 343 344 /// The specified block is found to be reachable, clone it and 345 /// anything that it can reach. 346 void CloneBlock(const BasicBlock *BB, 347 BasicBlock::const_iterator StartingInst, 348 std::vector<const BasicBlock*> &ToClone); 349 }; 350 } 351 352 /// The specified block is found to be reachable, clone it and 353 /// anything that it can reach. 354 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, 355 BasicBlock::const_iterator StartingInst, 356 std::vector<const BasicBlock*> &ToClone){ 357 WeakTrackingVH &BBEntry = VMap[BB]; 358 359 // Have we already cloned this block? 360 if (BBEntry) return; 361 362 // Nope, clone it now. 363 BasicBlock *NewBB; 364 BBEntry = NewBB = BasicBlock::Create(BB->getContext()); 365 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 366 367 // It is only legal to clone a function if a block address within that 368 // function is never referenced outside of the function. Given that, we 369 // want to map block addresses from the old function to block addresses in 370 // the clone. (This is different from the generic ValueMapper 371 // implementation, which generates an invalid blockaddress when 372 // cloning a function.) 373 // 374 // Note that we don't need to fix the mapping for unreachable blocks; 375 // the default mapping there is safe. 376 if (BB->hasAddressTaken()) { 377 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 378 const_cast<BasicBlock*>(BB)); 379 VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB); 380 } 381 382 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 383 384 // Loop over all instructions, and copy them over, DCE'ing as we go. This 385 // loop doesn't include the terminator. 386 for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end(); 387 II != IE; ++II) { 388 389 Instruction *NewInst = II->clone(); 390 391 // Eagerly remap operands to the newly cloned instruction, except for PHI 392 // nodes for which we defer processing until we update the CFG. 393 if (!isa<PHINode>(NewInst)) { 394 RemapInstruction(NewInst, VMap, 395 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 396 397 // If we can simplify this instruction to some other value, simply add 398 // a mapping to that value rather than inserting a new instruction into 399 // the basic block. 400 if (Value *V = 401 SimplifyInstruction(NewInst, BB->getModule()->getDataLayout())) { 402 // On the off-chance that this simplifies to an instruction in the old 403 // function, map it back into the new function. 404 if (NewFunc != OldFunc) 405 if (Value *MappedV = VMap.lookup(V)) 406 V = MappedV; 407 408 if (!NewInst->mayHaveSideEffects()) { 409 VMap[&*II] = V; 410 NewInst->deleteValue(); 411 continue; 412 } 413 } 414 } 415 416 if (II->hasName()) 417 NewInst->setName(II->getName()+NameSuffix); 418 VMap[&*II] = NewInst; // Add instruction map to value. 419 NewBB->getInstList().push_back(NewInst); 420 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 421 422 if (CodeInfo) 423 if (auto *CB = dyn_cast<CallBase>(&*II)) 424 if (CB->hasOperandBundles()) 425 CodeInfo->OperandBundleCallSites.push_back(NewInst); 426 427 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 428 if (isa<ConstantInt>(AI->getArraySize())) 429 hasStaticAllocas = true; 430 else 431 hasDynamicAllocas = true; 432 } 433 } 434 435 // Finally, clone over the terminator. 436 const Instruction *OldTI = BB->getTerminator(); 437 bool TerminatorDone = false; 438 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { 439 if (BI->isConditional()) { 440 // If the condition was a known constant in the callee... 441 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 442 // Or is a known constant in the caller... 443 if (!Cond) { 444 Value *V = VMap.lookup(BI->getCondition()); 445 Cond = dyn_cast_or_null<ConstantInt>(V); 446 } 447 448 // Constant fold to uncond branch! 449 if (Cond) { 450 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); 451 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 452 ToClone.push_back(Dest); 453 TerminatorDone = true; 454 } 455 } 456 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { 457 // If switching on a value known constant in the caller. 458 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); 459 if (!Cond) { // Or known constant after constant prop in the callee... 460 Value *V = VMap.lookup(SI->getCondition()); 461 Cond = dyn_cast_or_null<ConstantInt>(V); 462 } 463 if (Cond) { // Constant fold to uncond branch! 464 SwitchInst::ConstCaseHandle Case = *SI->findCaseValue(Cond); 465 BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor()); 466 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 467 ToClone.push_back(Dest); 468 TerminatorDone = true; 469 } 470 } 471 472 if (!TerminatorDone) { 473 Instruction *NewInst = OldTI->clone(); 474 if (OldTI->hasName()) 475 NewInst->setName(OldTI->getName()+NameSuffix); 476 NewBB->getInstList().push_back(NewInst); 477 VMap[OldTI] = NewInst; // Add instruction map to value. 478 479 if (CodeInfo) 480 if (auto *CB = dyn_cast<CallBase>(OldTI)) 481 if (CB->hasOperandBundles()) 482 CodeInfo->OperandBundleCallSites.push_back(NewInst); 483 484 // Recursively clone any reachable successor blocks. 485 append_range(ToClone, successors(BB->getTerminator())); 486 } 487 488 if (CodeInfo) { 489 CodeInfo->ContainsCalls |= hasCalls; 490 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 491 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 492 BB != &BB->getParent()->front(); 493 } 494 } 495 496 /// This works like CloneAndPruneFunctionInto, except that it does not clone the 497 /// entire function. Instead it starts at an instruction provided by the caller 498 /// and copies (and prunes) only the code reachable from that instruction. 499 void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc, 500 const Instruction *StartingInst, 501 ValueToValueMapTy &VMap, 502 bool ModuleLevelChanges, 503 SmallVectorImpl<ReturnInst *> &Returns, 504 const char *NameSuffix, 505 ClonedCodeInfo *CodeInfo) { 506 assert(NameSuffix && "NameSuffix cannot be null!"); 507 508 ValueMapTypeRemapper *TypeMapper = nullptr; 509 ValueMaterializer *Materializer = nullptr; 510 511 #ifndef NDEBUG 512 // If the cloning starts at the beginning of the function, verify that 513 // the function arguments are mapped. 514 if (!StartingInst) 515 for (const Argument &II : OldFunc->args()) 516 assert(VMap.count(&II) && "No mapping from source argument specified!"); 517 #endif 518 519 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, 520 NameSuffix, CodeInfo); 521 const BasicBlock *StartingBB; 522 if (StartingInst) 523 StartingBB = StartingInst->getParent(); 524 else { 525 StartingBB = &OldFunc->getEntryBlock(); 526 StartingInst = &StartingBB->front(); 527 } 528 529 // Clone the entry block, and anything recursively reachable from it. 530 std::vector<const BasicBlock*> CloneWorklist; 531 PFC.CloneBlock(StartingBB, StartingInst->getIterator(), CloneWorklist); 532 while (!CloneWorklist.empty()) { 533 const BasicBlock *BB = CloneWorklist.back(); 534 CloneWorklist.pop_back(); 535 PFC.CloneBlock(BB, BB->begin(), CloneWorklist); 536 } 537 538 // Loop over all of the basic blocks in the old function. If the block was 539 // reachable, we have cloned it and the old block is now in the value map: 540 // insert it into the new function in the right order. If not, ignore it. 541 // 542 // Defer PHI resolution until rest of function is resolved. 543 SmallVector<const PHINode*, 16> PHIToResolve; 544 for (const BasicBlock &BI : *OldFunc) { 545 Value *V = VMap.lookup(&BI); 546 BasicBlock *NewBB = cast_or_null<BasicBlock>(V); 547 if (!NewBB) continue; // Dead block. 548 549 // Add the new block to the new function. 550 NewFunc->getBasicBlockList().push_back(NewBB); 551 552 // Handle PHI nodes specially, as we have to remove references to dead 553 // blocks. 554 for (const PHINode &PN : BI.phis()) { 555 // PHI nodes may have been remapped to non-PHI nodes by the caller or 556 // during the cloning process. 557 if (isa<PHINode>(VMap[&PN])) 558 PHIToResolve.push_back(&PN); 559 else 560 break; 561 } 562 563 // Finally, remap the terminator instructions, as those can't be remapped 564 // until all BBs are mapped. 565 RemapInstruction(NewBB->getTerminator(), VMap, 566 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 567 TypeMapper, Materializer); 568 } 569 570 // Defer PHI resolution until rest of function is resolved, PHI resolution 571 // requires the CFG to be up-to-date. 572 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { 573 const PHINode *OPN = PHIToResolve[phino]; 574 unsigned NumPreds = OPN->getNumIncomingValues(); 575 const BasicBlock *OldBB = OPN->getParent(); 576 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); 577 578 // Map operands for blocks that are live and remove operands for blocks 579 // that are dead. 580 for (; phino != PHIToResolve.size() && 581 PHIToResolve[phino]->getParent() == OldBB; ++phino) { 582 OPN = PHIToResolve[phino]; 583 PHINode *PN = cast<PHINode>(VMap[OPN]); 584 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { 585 Value *V = VMap.lookup(PN->getIncomingBlock(pred)); 586 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { 587 Value *InVal = MapValue(PN->getIncomingValue(pred), 588 VMap, 589 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 590 assert(InVal && "Unknown input value?"); 591 PN->setIncomingValue(pred, InVal); 592 PN->setIncomingBlock(pred, MappedBlock); 593 } else { 594 PN->removeIncomingValue(pred, false); 595 --pred; // Revisit the next entry. 596 --e; 597 } 598 } 599 } 600 601 // The loop above has removed PHI entries for those blocks that are dead 602 // and has updated others. However, if a block is live (i.e. copied over) 603 // but its terminator has been changed to not go to this block, then our 604 // phi nodes will have invalid entries. Update the PHI nodes in this 605 // case. 606 PHINode *PN = cast<PHINode>(NewBB->begin()); 607 NumPreds = pred_size(NewBB); 608 if (NumPreds != PN->getNumIncomingValues()) { 609 assert(NumPreds < PN->getNumIncomingValues()); 610 // Count how many times each predecessor comes to this block. 611 std::map<BasicBlock*, unsigned> PredCount; 612 for (BasicBlock *Pred : predecessors(NewBB)) 613 --PredCount[Pred]; 614 615 // Figure out how many entries to remove from each PHI. 616 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 617 ++PredCount[PN->getIncomingBlock(i)]; 618 619 // At this point, the excess predecessor entries are positive in the 620 // map. Loop over all of the PHIs and remove excess predecessor 621 // entries. 622 BasicBlock::iterator I = NewBB->begin(); 623 for (; (PN = dyn_cast<PHINode>(I)); ++I) { 624 for (const auto &PCI : PredCount) { 625 BasicBlock *Pred = PCI.first; 626 for (unsigned NumToRemove = PCI.second; NumToRemove; --NumToRemove) 627 PN->removeIncomingValue(Pred, false); 628 } 629 } 630 } 631 632 // If the loops above have made these phi nodes have 0 or 1 operand, 633 // replace them with undef or the input value. We must do this for 634 // correctness, because 0-operand phis are not valid. 635 PN = cast<PHINode>(NewBB->begin()); 636 if (PN->getNumIncomingValues() == 0) { 637 BasicBlock::iterator I = NewBB->begin(); 638 BasicBlock::const_iterator OldI = OldBB->begin(); 639 while ((PN = dyn_cast<PHINode>(I++))) { 640 Value *NV = UndefValue::get(PN->getType()); 641 PN->replaceAllUsesWith(NV); 642 assert(VMap[&*OldI] == PN && "VMap mismatch"); 643 VMap[&*OldI] = NV; 644 PN->eraseFromParent(); 645 ++OldI; 646 } 647 } 648 } 649 650 // Make a second pass over the PHINodes now that all of them have been 651 // remapped into the new function, simplifying the PHINode and performing any 652 // recursive simplifications exposed. This will transparently update the 653 // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce 654 // two PHINodes, the iteration over the old PHIs remains valid, and the 655 // mapping will just map us to the new node (which may not even be a PHI 656 // node). 657 const DataLayout &DL = NewFunc->getParent()->getDataLayout(); 658 SmallSetVector<const Value *, 8> Worklist; 659 for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx) 660 if (isa<PHINode>(VMap[PHIToResolve[Idx]])) 661 Worklist.insert(PHIToResolve[Idx]); 662 663 // Note that we must test the size on each iteration, the worklist can grow. 664 for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) { 665 const Value *OrigV = Worklist[Idx]; 666 auto *I = dyn_cast_or_null<Instruction>(VMap.lookup(OrigV)); 667 if (!I) 668 continue; 669 670 // Skip over non-intrinsic callsites, we don't want to remove any nodes from 671 // the CGSCC. 672 CallBase *CB = dyn_cast<CallBase>(I); 673 if (CB && CB->getCalledFunction() && 674 !CB->getCalledFunction()->isIntrinsic()) 675 continue; 676 677 // See if this instruction simplifies. 678 Value *SimpleV = SimplifyInstruction(I, DL); 679 if (!SimpleV) 680 continue; 681 682 // Stash away all the uses of the old instruction so we can check them for 683 // recursive simplifications after a RAUW. This is cheaper than checking all 684 // uses of To on the recursive step in most cases. 685 for (const User *U : OrigV->users()) 686 Worklist.insert(cast<Instruction>(U)); 687 688 // Replace the instruction with its simplified value. 689 I->replaceAllUsesWith(SimpleV); 690 691 // If the original instruction had no side effects, remove it. 692 if (isInstructionTriviallyDead(I)) 693 I->eraseFromParent(); 694 else 695 VMap[OrigV] = I; 696 } 697 698 // Now that the inlined function body has been fully constructed, go through 699 // and zap unconditional fall-through branches. This happens all the time when 700 // specializing code: code specialization turns conditional branches into 701 // uncond branches, and this code folds them. 702 Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator(); 703 Function::iterator I = Begin; 704 while (I != NewFunc->end()) { 705 // We need to simplify conditional branches and switches with a constant 706 // operand. We try to prune these out when cloning, but if the 707 // simplification required looking through PHI nodes, those are only 708 // available after forming the full basic block. That may leave some here, 709 // and we still want to prune the dead code as early as possible. 710 // 711 // Do the folding before we check if the block is dead since we want code 712 // like 713 // bb: 714 // br i1 undef, label %bb, label %bb 715 // to be simplified to 716 // bb: 717 // br label %bb 718 // before we call I->getSinglePredecessor(). 719 ConstantFoldTerminator(&*I); 720 721 // Check if this block has become dead during inlining or other 722 // simplifications. Note that the first block will appear dead, as it has 723 // not yet been wired up properly. 724 if (I != Begin && (pred_empty(&*I) || I->getSinglePredecessor() == &*I)) { 725 BasicBlock *DeadBB = &*I++; 726 DeleteDeadBlock(DeadBB); 727 continue; 728 } 729 730 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); 731 if (!BI || BI->isConditional()) { ++I; continue; } 732 733 BasicBlock *Dest = BI->getSuccessor(0); 734 if (!Dest->getSinglePredecessor()) { 735 ++I; continue; 736 } 737 738 // We shouldn't be able to get single-entry PHI nodes here, as instsimplify 739 // above should have zapped all of them.. 740 assert(!isa<PHINode>(Dest->begin())); 741 742 // We know all single-entry PHI nodes in the inlined function have been 743 // removed, so we just need to splice the blocks. 744 BI->eraseFromParent(); 745 746 // Make all PHI nodes that referred to Dest now refer to I as their source. 747 Dest->replaceAllUsesWith(&*I); 748 749 // Move all the instructions in the succ to the pred. 750 I->getInstList().splice(I->end(), Dest->getInstList()); 751 752 // Remove the dest block. 753 Dest->eraseFromParent(); 754 755 // Do not increment I, iteratively merge all things this block branches to. 756 } 757 758 // Make a final pass over the basic blocks from the old function to gather 759 // any return instructions which survived folding. We have to do this here 760 // because we can iteratively remove and merge returns above. 761 for (Function::iterator I = cast<BasicBlock>(VMap[StartingBB])->getIterator(), 762 E = NewFunc->end(); 763 I != E; ++I) 764 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) 765 Returns.push_back(RI); 766 } 767 768 769 /// This works exactly like CloneFunctionInto, 770 /// except that it does some simple constant prop and DCE on the fly. The 771 /// effect of this is to copy significantly less code in cases where (for 772 /// example) a function call with constant arguments is inlined, and those 773 /// constant arguments cause a significant amount of code in the callee to be 774 /// dead. Since this doesn't produce an exact copy of the input, it can't be 775 /// used for things like CloneFunction or CloneModule. 776 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 777 ValueToValueMapTy &VMap, 778 bool ModuleLevelChanges, 779 SmallVectorImpl<ReturnInst*> &Returns, 780 const char *NameSuffix, 781 ClonedCodeInfo *CodeInfo, 782 Instruction *TheCall) { 783 CloneAndPruneIntoFromInst(NewFunc, OldFunc, &OldFunc->front().front(), VMap, 784 ModuleLevelChanges, Returns, NameSuffix, CodeInfo); 785 } 786 787 /// Remaps instructions in \p Blocks using the mapping in \p VMap. 788 void llvm::remapInstructionsInBlocks( 789 const SmallVectorImpl<BasicBlock *> &Blocks, ValueToValueMapTy &VMap) { 790 // Rewrite the code to refer to itself. 791 for (auto *BB : Blocks) 792 for (auto &Inst : *BB) 793 RemapInstruction(&Inst, VMap, 794 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); 795 } 796 797 /// Clones a loop \p OrigLoop. Returns the loop and the blocks in \p 798 /// Blocks. 799 /// 800 /// Updates LoopInfo and DominatorTree assuming the loop is dominated by block 801 /// \p LoopDomBB. Insert the new blocks before block specified in \p Before. 802 Loop *llvm::cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, 803 Loop *OrigLoop, ValueToValueMapTy &VMap, 804 const Twine &NameSuffix, LoopInfo *LI, 805 DominatorTree *DT, 806 SmallVectorImpl<BasicBlock *> &Blocks) { 807 Function *F = OrigLoop->getHeader()->getParent(); 808 Loop *ParentLoop = OrigLoop->getParentLoop(); 809 DenseMap<Loop *, Loop *> LMap; 810 811 Loop *NewLoop = LI->AllocateLoop(); 812 LMap[OrigLoop] = NewLoop; 813 if (ParentLoop) 814 ParentLoop->addChildLoop(NewLoop); 815 else 816 LI->addTopLevelLoop(NewLoop); 817 818 BasicBlock *OrigPH = OrigLoop->getLoopPreheader(); 819 assert(OrigPH && "No preheader"); 820 BasicBlock *NewPH = CloneBasicBlock(OrigPH, VMap, NameSuffix, F); 821 // To rename the loop PHIs. 822 VMap[OrigPH] = NewPH; 823 Blocks.push_back(NewPH); 824 825 // Update LoopInfo. 826 if (ParentLoop) 827 ParentLoop->addBasicBlockToLoop(NewPH, *LI); 828 829 // Update DominatorTree. 830 DT->addNewBlock(NewPH, LoopDomBB); 831 832 for (Loop *CurLoop : OrigLoop->getLoopsInPreorder()) { 833 Loop *&NewLoop = LMap[CurLoop]; 834 if (!NewLoop) { 835 NewLoop = LI->AllocateLoop(); 836 837 // Establish the parent/child relationship. 838 Loop *OrigParent = CurLoop->getParentLoop(); 839 assert(OrigParent && "Could not find the original parent loop"); 840 Loop *NewParentLoop = LMap[OrigParent]; 841 assert(NewParentLoop && "Could not find the new parent loop"); 842 843 NewParentLoop->addChildLoop(NewLoop); 844 } 845 } 846 847 for (BasicBlock *BB : OrigLoop->getBlocks()) { 848 Loop *CurLoop = LI->getLoopFor(BB); 849 Loop *&NewLoop = LMap[CurLoop]; 850 assert(NewLoop && "Expecting new loop to be allocated"); 851 852 BasicBlock *NewBB = CloneBasicBlock(BB, VMap, NameSuffix, F); 853 VMap[BB] = NewBB; 854 855 // Update LoopInfo. 856 NewLoop->addBasicBlockToLoop(NewBB, *LI); 857 858 // Add DominatorTree node. After seeing all blocks, update to correct 859 // IDom. 860 DT->addNewBlock(NewBB, NewPH); 861 862 Blocks.push_back(NewBB); 863 } 864 865 for (BasicBlock *BB : OrigLoop->getBlocks()) { 866 // Update loop headers. 867 Loop *CurLoop = LI->getLoopFor(BB); 868 if (BB == CurLoop->getHeader()) 869 LMap[CurLoop]->moveToHeader(cast<BasicBlock>(VMap[BB])); 870 871 // Update DominatorTree. 872 BasicBlock *IDomBB = DT->getNode(BB)->getIDom()->getBlock(); 873 DT->changeImmediateDominator(cast<BasicBlock>(VMap[BB]), 874 cast<BasicBlock>(VMap[IDomBB])); 875 } 876 877 // Move them physically from the end of the block list. 878 F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(), 879 NewPH); 880 F->getBasicBlockList().splice(Before->getIterator(), F->getBasicBlockList(), 881 NewLoop->getHeader()->getIterator(), F->end()); 882 883 return NewLoop; 884 } 885 886 /// Duplicate non-Phi instructions from the beginning of block up to 887 /// StopAt instruction into a split block between BB and its predecessor. 888 BasicBlock *llvm::DuplicateInstructionsInSplitBetween( 889 BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt, 890 ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU) { 891 892 assert(count(successors(PredBB), BB) == 1 && 893 "There must be a single edge between PredBB and BB!"); 894 // We are going to have to map operands from the original BB block to the new 895 // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to 896 // account for entry from PredBB. 897 BasicBlock::iterator BI = BB->begin(); 898 for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) 899 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); 900 901 BasicBlock *NewBB = SplitEdge(PredBB, BB); 902 NewBB->setName(PredBB->getName() + ".split"); 903 Instruction *NewTerm = NewBB->getTerminator(); 904 905 // FIXME: SplitEdge does not yet take a DTU, so we include the split edge 906 // in the update set here. 907 DTU.applyUpdates({{DominatorTree::Delete, PredBB, BB}, 908 {DominatorTree::Insert, PredBB, NewBB}, 909 {DominatorTree::Insert, NewBB, BB}}); 910 911 // Clone the non-phi instructions of BB into NewBB, keeping track of the 912 // mapping and using it to remap operands in the cloned instructions. 913 // Stop once we see the terminator too. This covers the case where BB's 914 // terminator gets replaced and StopAt == BB's terminator. 915 for (; StopAt != &*BI && BB->getTerminator() != &*BI; ++BI) { 916 Instruction *New = BI->clone(); 917 New->setName(BI->getName()); 918 New->insertBefore(NewTerm); 919 ValueMapping[&*BI] = New; 920 921 // Remap operands to patch up intra-block references. 922 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) 923 if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { 924 auto I = ValueMapping.find(Inst); 925 if (I != ValueMapping.end()) 926 New->setOperand(i, I->second); 927 } 928 } 929 930 return NewBB; 931 } 932 933 void llvm::cloneNoAliasScopes( 934 ArrayRef<MDNode *> NoAliasDeclScopes, 935 DenseMap<MDNode *, MDNode *> &ClonedScopes, 936 StringRef Ext, LLVMContext &Context) { 937 MDBuilder MDB(Context); 938 939 for (auto *ScopeList : NoAliasDeclScopes) { 940 for (auto &MDOperand : ScopeList->operands()) { 941 if (MDNode *MD = dyn_cast<MDNode>(MDOperand)) { 942 AliasScopeNode SNANode(MD); 943 944 std::string Name; 945 auto ScopeName = SNANode.getName(); 946 if (!ScopeName.empty()) 947 Name = (Twine(ScopeName) + ":" + Ext).str(); 948 else 949 Name = std::string(Ext); 950 951 MDNode *NewScope = MDB.createAnonymousAliasScope( 952 const_cast<MDNode *>(SNANode.getDomain()), Name); 953 ClonedScopes.insert(std::make_pair(MD, NewScope)); 954 } 955 } 956 } 957 } 958 959 void llvm::adaptNoAliasScopes( 960 Instruction *I, const DenseMap<MDNode *, MDNode *> &ClonedScopes, 961 LLVMContext &Context) { 962 auto CloneScopeList = [&](const MDNode *ScopeList) -> MDNode * { 963 bool NeedsReplacement = false; 964 SmallVector<Metadata *, 8> NewScopeList; 965 for (auto &MDOp : ScopeList->operands()) { 966 if (MDNode *MD = dyn_cast<MDNode>(MDOp)) { 967 if (auto *NewMD = ClonedScopes.lookup(MD)) { 968 NewScopeList.push_back(NewMD); 969 NeedsReplacement = true; 970 continue; 971 } 972 NewScopeList.push_back(MD); 973 } 974 } 975 if (NeedsReplacement) 976 return MDNode::get(Context, NewScopeList); 977 return nullptr; 978 }; 979 980 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I)) 981 if (auto *NewScopeList = CloneScopeList(Decl->getScopeList())) 982 Decl->setScopeList(NewScopeList); 983 984 auto replaceWhenNeeded = [&](unsigned MD_ID) { 985 if (const MDNode *CSNoAlias = I->getMetadata(MD_ID)) 986 if (auto *NewScopeList = CloneScopeList(CSNoAlias)) 987 I->setMetadata(MD_ID, NewScopeList); 988 }; 989 replaceWhenNeeded(LLVMContext::MD_noalias); 990 replaceWhenNeeded(LLVMContext::MD_alias_scope); 991 } 992 993 void llvm::cloneAndAdaptNoAliasScopes( 994 ArrayRef<MDNode *> NoAliasDeclScopes, 995 ArrayRef<BasicBlock *> NewBlocks, LLVMContext &Context, StringRef Ext) { 996 if (NoAliasDeclScopes.empty()) 997 return; 998 999 DenseMap<MDNode *, MDNode *> ClonedScopes; 1000 LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning " 1001 << NoAliasDeclScopes.size() << " node(s)\n"); 1002 1003 cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context); 1004 // Identify instructions using metadata that needs adaptation 1005 for (BasicBlock *NewBlock : NewBlocks) 1006 for (Instruction &I : *NewBlock) 1007 adaptNoAliasScopes(&I, ClonedScopes, Context); 1008 } 1009 1010 void llvm::cloneAndAdaptNoAliasScopes( 1011 ArrayRef<MDNode *> NoAliasDeclScopes, Instruction *IStart, 1012 Instruction *IEnd, LLVMContext &Context, StringRef Ext) { 1013 if (NoAliasDeclScopes.empty()) 1014 return; 1015 1016 DenseMap<MDNode *, MDNode *> ClonedScopes; 1017 LLVM_DEBUG(dbgs() << "cloneAndAdaptNoAliasScopes: cloning " 1018 << NoAliasDeclScopes.size() << " node(s)\n"); 1019 1020 cloneNoAliasScopes(NoAliasDeclScopes, ClonedScopes, Ext, Context); 1021 // Identify instructions using metadata that needs adaptation 1022 assert(IStart->getParent() == IEnd->getParent() && "different basic block ?"); 1023 auto ItStart = IStart->getIterator(); 1024 auto ItEnd = IEnd->getIterator(); 1025 ++ItEnd; // IEnd is included, increment ItEnd to get the end of the range 1026 for (auto &I : llvm::make_range(ItStart, ItEnd)) 1027 adaptNoAliasScopes(&I, ClonedScopes, Context); 1028 } 1029 1030 void llvm::identifyNoAliasScopesToClone( 1031 ArrayRef<BasicBlock *> BBs, SmallVectorImpl<MDNode *> &NoAliasDeclScopes) { 1032 for (BasicBlock *BB : BBs) 1033 for (Instruction &I : *BB) 1034 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I)) 1035 NoAliasDeclScopes.push_back(Decl->getScopeList()); 1036 } 1037 1038 void llvm::identifyNoAliasScopesToClone( 1039 BasicBlock::iterator Start, BasicBlock::iterator End, 1040 SmallVectorImpl<MDNode *> &NoAliasDeclScopes) { 1041 for (Instruction &I : make_range(Start, End)) 1042 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I)) 1043 NoAliasDeclScopes.push_back(Decl->getScopeList()); 1044 } 1045