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