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/Transforms/Utils/Cloning.h" 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/Analysis/ConstantFolding.h" 19 #include "llvm/Analysis/InstructionSimplify.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/GlobalVariable.h" 26 #include "llvm/IR/Instructions.h" 27 #include "llvm/IR/IntrinsicInst.h" 28 #include "llvm/IR/LLVMContext.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/Local.h" 33 #include "llvm/Transforms/Utils/ValueMapper.h" 34 #include <map> 35 #include <set> 36 using namespace llvm; 37 38 // CloneBasicBlock - See comments in Cloning.h 39 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, 40 ValueToValueMapTy &VMap, 41 const Twine &NameSuffix, Function *F, 42 ClonedCodeInfo *CodeInfo) { 43 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F); 44 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 45 46 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 47 48 // Loop over all instructions, and copy them over. 49 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); 50 II != IE; ++II) { 51 Instruction *NewInst = II->clone(); 52 if (II->hasName()) 53 NewInst->setName(II->getName()+NameSuffix); 54 NewBB->getInstList().push_back(NewInst); 55 VMap[II] = NewInst; // Add instruction map to value. 56 57 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 58 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 59 if (isa<ConstantInt>(AI->getArraySize())) 60 hasStaticAllocas = true; 61 else 62 hasDynamicAllocas = true; 63 } 64 } 65 66 if (CodeInfo) { 67 CodeInfo->ContainsCalls |= hasCalls; 68 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 69 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 70 BB != &BB->getParent()->getEntryBlock(); 71 } 72 return NewBB; 73 } 74 75 // Clone OldFunc into NewFunc, transforming the old arguments into references to 76 // VMap values. 77 // 78 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 79 ValueToValueMapTy &VMap, 80 bool ModuleLevelChanges, 81 SmallVectorImpl<ReturnInst*> &Returns, 82 const char *NameSuffix, ClonedCodeInfo *CodeInfo, 83 ValueMapTypeRemapper *TypeMapper, 84 ValueMaterializer *Materializer) { 85 assert(NameSuffix && "NameSuffix cannot be null!"); 86 87 #ifndef NDEBUG 88 for (Function::const_arg_iterator I = OldFunc->arg_begin(), 89 E = OldFunc->arg_end(); I != E; ++I) 90 assert(VMap.count(I) && "No mapping from source argument specified!"); 91 #endif 92 93 // Copy all attributes other than those stored in the AttributeSet. We need 94 // to remap the parameter indices of the AttributeSet. 95 AttributeSet NewAttrs = NewFunc->getAttributes(); 96 NewFunc->copyAttributesFrom(OldFunc); 97 NewFunc->setAttributes(NewAttrs); 98 99 AttributeSet OldAttrs = OldFunc->getAttributes(); 100 // Clone any argument attributes that are present in the VMap. 101 for (const Argument &OldArg : OldFunc->args()) 102 if (Argument *NewArg = dyn_cast<Argument>(VMap[&OldArg])) { 103 AttributeSet attrs = 104 OldAttrs.getParamAttributes(OldArg.getArgNo() + 1); 105 if (attrs.getNumSlots() > 0) 106 NewArg->addAttr(attrs); 107 } 108 109 NewFunc->setAttributes( 110 NewFunc->getAttributes() 111 .addAttributes(NewFunc->getContext(), AttributeSet::ReturnIndex, 112 OldAttrs.getRetAttributes()) 113 .addAttributes(NewFunc->getContext(), AttributeSet::FunctionIndex, 114 OldAttrs.getFnAttributes())); 115 116 // Loop over all of the basic blocks in the function, cloning them as 117 // appropriate. Note that we save BE this way in order to handle cloning of 118 // recursive functions into themselves. 119 // 120 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 121 BI != BE; ++BI) { 122 const BasicBlock &BB = *BI; 123 124 // Create a new basic block and copy instructions into it! 125 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); 126 127 // Add basic block mapping. 128 VMap[&BB] = CBB; 129 130 // It is only legal to clone a function if a block address within that 131 // function is never referenced outside of the function. Given that, we 132 // want to map block addresses from the old function to block addresses in 133 // the clone. (This is different from the generic ValueMapper 134 // implementation, which generates an invalid blockaddress when 135 // cloning a function.) 136 if (BB.hasAddressTaken()) { 137 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 138 const_cast<BasicBlock*>(&BB)); 139 VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); 140 } 141 142 // Note return instructions for the caller. 143 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) 144 Returns.push_back(RI); 145 } 146 147 // Loop over all of the instructions in the function, fixing up operand 148 // references as we go. This uses VMap to do all the hard work. 149 for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]), 150 BE = NewFunc->end(); BB != BE; ++BB) 151 // Loop over all instructions, fixing each one as we find it... 152 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) 153 RemapInstruction(II, VMap, 154 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 155 TypeMapper, Materializer); 156 } 157 158 // Find the MDNode which corresponds to the DISubprogram data that described F. 159 static MDNode* FindSubprogram(const Function *F, DebugInfoFinder &Finder) { 160 for (DISubprogram Subprogram : Finder.subprograms()) { 161 if (Subprogram.describes(F)) return Subprogram; 162 } 163 return nullptr; 164 } 165 166 // Add an operand to an existing MDNode. The new operand will be added at the 167 // back of the operand list. 168 static void AddOperand(MDNode *Node, Value *Operand) { 169 SmallVector<Value*, 16> Operands; 170 for (unsigned i = 0; i < Node->getNumOperands(); i++) { 171 Operands.push_back(Node->getOperand(i)); 172 } 173 Operands.push_back(Operand); 174 MDNode *NewNode = MDNode::get(Node->getContext(), Operands); 175 Node->replaceAllUsesWith(NewNode); 176 } 177 178 // Clone the module-level debug info associated with OldFunc. The cloned data 179 // will point to NewFunc instead. 180 static void CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc, 181 ValueToValueMapTy &VMap) { 182 DebugInfoFinder Finder; 183 Finder.processModule(*OldFunc->getParent()); 184 185 const MDNode *OldSubprogramMDNode = FindSubprogram(OldFunc, Finder); 186 if (!OldSubprogramMDNode) return; 187 188 // Ensure that OldFunc appears in the map. 189 // (if it's already there it must point to NewFunc anyway) 190 VMap[OldFunc] = NewFunc; 191 DISubprogram NewSubprogram(MapValue(OldSubprogramMDNode, VMap)); 192 193 for (DICompileUnit CU : Finder.compile_units()) { 194 DIArray Subprograms(CU.getSubprograms()); 195 196 // If the compile unit's function list contains the old function, it should 197 // also contain the new one. 198 for (unsigned i = 0; i < Subprograms.getNumElements(); i++) { 199 if ((MDNode*)Subprograms.getElement(i) == OldSubprogramMDNode) { 200 AddOperand(Subprograms, NewSubprogram); 201 } 202 } 203 } 204 } 205 206 /// CloneFunction - Return a copy of the specified function, but without 207 /// embedding the function into another module. Also, any references specified 208 /// in the VMap are changed to refer to their mapped value instead of the 209 /// original one. If any of the arguments to the function are in the VMap, 210 /// the arguments are deleted from the resultant function. The VMap is 211 /// updated to include mappings from all of the instructions and basicblocks in 212 /// the function from their old to new values. 213 /// 214 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap, 215 bool ModuleLevelChanges, 216 ClonedCodeInfo *CodeInfo) { 217 std::vector<Type*> ArgTypes; 218 219 // The user might be deleting arguments to the function by specifying them in 220 // the VMap. If so, we need to not add the arguments to the arg ty vector 221 // 222 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 223 I != E; ++I) 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 = Function::Create(FTy, F->getLinkage(), F->getName()); 233 234 // Loop over the arguments, copying the names of the mapped arguments over... 235 Function::arg_iterator DestI = NewF->arg_begin(); 236 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 237 I != E; ++I) 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 if (ModuleLevelChanges) 244 CloneDebugInfoMetadata(NewF, F, VMap); 245 246 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. 247 CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo); 248 return NewF; 249 } 250 251 252 253 namespace { 254 /// PruningFunctionCloner - This class is a private class used to implement 255 /// the CloneAndPruneFunctionInto method. 256 struct PruningFunctionCloner { 257 Function *NewFunc; 258 const Function *OldFunc; 259 ValueToValueMapTy &VMap; 260 bool ModuleLevelChanges; 261 const char *NameSuffix; 262 ClonedCodeInfo *CodeInfo; 263 const DataLayout *DL; 264 public: 265 PruningFunctionCloner(Function *newFunc, const Function *oldFunc, 266 ValueToValueMapTy &valueMap, 267 bool moduleLevelChanges, 268 const char *nameSuffix, 269 ClonedCodeInfo *codeInfo, 270 const DataLayout *DL) 271 : NewFunc(newFunc), OldFunc(oldFunc), 272 VMap(valueMap), ModuleLevelChanges(moduleLevelChanges), 273 NameSuffix(nameSuffix), CodeInfo(codeInfo), DL(DL) { 274 } 275 276 /// CloneBlock - The specified block is found to be reachable, so clone it 277 /// into NewBB. 278 /// ToClone is the vector of actually cloned blocks. 279 /// OrigBBs is the set of all blocks reacheable from the entry block. 280 /// It contains the block candidates and makes sure each block 281 /// is cloned at most once. 282 void CloneBlock(const BasicBlock *BB, 283 BasicBlock *NewBB, 284 std::vector<const BasicBlock *> &ToClone, 285 std::set<const BasicBlock *> &OrigBBs); 286 }; 287 } 288 289 /// CloneBlock - The specified block is found to be reachable, so clone it 290 /// into NewBB. 291 /// ToClone is the vector of actually cloned blocks. 292 /// OrigBBs is the set of all blocks reacheable from the entry block. 293 /// It contains the block candidates and makes sure each block 294 /// is cloned at most once. 295 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, 296 BasicBlock *NewBB, 297 std::vector<const BasicBlock *> &ToClone, 298 std::set<const BasicBlock *> &OrigBBs) { 299 300 // Remove BB from list of blocks to clone. 301 // When it was not in the list, it has been cloned already, so 302 // don't clone again. 303 if (!OrigBBs.erase(BB)) return; 304 305 // Nope, clone it now. 306 307 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 308 309 // Loop over all instructions, and copy them over, DCE'ing as we go. This 310 // loop doesn't include the terminator. 311 for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end(); 312 II != IE; ++II) { 313 Instruction *NewInst = II->clone(); 314 315 // Eagerly remap operands to the newly cloned instruction, except for PHI 316 // nodes for which we defer processing until we update the CFG. 317 if (!isa<PHINode>(NewInst)) { 318 RemapInstruction(NewInst, VMap, 319 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 320 321 // If we can simplify this instruction to some other value, simply add 322 // a mapping to that value rather than inserting a new instruction into 323 // the basic block. 324 if (Value *V = SimplifyInstruction(NewInst, DL)) { 325 // On the off-chance that this simplifies to an instruction in the old 326 // function, map it back into the new function. 327 if (Value *MappedV = VMap.lookup(V)) 328 V = MappedV; 329 330 VMap[II] = V; 331 delete NewInst; 332 continue; 333 } 334 } 335 336 if (II->hasName()) 337 NewInst->setName(II->getName()+NameSuffix); 338 VMap[II] = NewInst; // Add instruction map to value. 339 NewBB->getInstList().push_back(NewInst); 340 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 341 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 342 if (isa<ConstantInt>(AI->getArraySize())) 343 hasStaticAllocas = true; 344 else 345 hasDynamicAllocas = true; 346 } 347 } 348 349 // Finally, clone over the terminator. 350 const TerminatorInst *OldTI = BB->getTerminator(); 351 bool TerminatorDone = false; 352 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { 353 if (BI->isConditional()) { 354 // If the condition was a known constant in the callee... 355 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 356 // Or is a known constant in the caller... 357 if (!Cond) { 358 Value *V = VMap[BI->getCondition()]; 359 Cond = dyn_cast_or_null<ConstantInt>(V); 360 } 361 362 // Constant fold to uncond branch! 363 if (Cond) { 364 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); 365 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 366 ToClone.push_back(Dest); 367 TerminatorDone = true; 368 } 369 } 370 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { 371 // If switching on a value known constant in the caller. 372 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); 373 if (!Cond) { // Or known constant after constant prop in the callee... 374 Value *V = VMap[SI->getCondition()]; 375 Cond = dyn_cast_or_null<ConstantInt>(V); 376 } 377 if (Cond) { // Constant fold to uncond branch! 378 SwitchInst::ConstCaseIt Case = SI->findCaseValue(Cond); 379 BasicBlock *Dest = const_cast<BasicBlock*>(Case.getCaseSuccessor()); 380 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 381 ToClone.push_back(Dest); 382 TerminatorDone = true; 383 } 384 } 385 386 if (!TerminatorDone) { 387 Instruction *NewInst = OldTI->clone(); 388 if (OldTI->hasName()) 389 NewInst->setName(OldTI->getName()+NameSuffix); 390 NewBB->getInstList().push_back(NewInst); 391 VMap[OldTI] = NewInst; // Add instruction map to value. 392 393 // Recursively clone any reachable successor blocks. 394 const TerminatorInst *TI = BB->getTerminator(); 395 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 396 ToClone.push_back(TI->getSuccessor(i)); 397 } 398 399 if (CodeInfo) { 400 CodeInfo->ContainsCalls |= hasCalls; 401 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 402 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 403 BB != &BB->getParent()->front(); 404 } 405 } 406 407 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, 408 /// except that it does some simple constant prop and DCE on the fly. The 409 /// effect of this is to copy significantly less code in cases where (for 410 /// example) a function call with constant arguments is inlined, and those 411 /// constant arguments cause a significant amount of code in the callee to be 412 /// dead. Since this doesn't produce an exact copy of the input, it can't be 413 /// used for things like CloneFunction or CloneModule. 414 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 415 ValueToValueMapTy &VMap, 416 bool ModuleLevelChanges, 417 SmallVectorImpl<ReturnInst*> &Returns, 418 const char *NameSuffix, 419 ClonedCodeInfo *CodeInfo, 420 const DataLayout *DL, 421 Instruction *TheCall) { 422 assert(NameSuffix && "NameSuffix cannot be null!"); 423 424 #ifndef NDEBUG 425 for (Function::const_arg_iterator II = OldFunc->arg_begin(), 426 E = OldFunc->arg_end(); II != E; ++II) 427 assert(VMap.count(II) && "No mapping from source argument specified!"); 428 #endif 429 430 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, 431 NameSuffix, CodeInfo, DL); 432 433 // Since all BB address references need to be known before block-by-block 434 // processing, we need to create all reachable blocks before processing 435 // them for instruction cloning and pruning. Some of these blocks may 436 // be removed due to later pruning. 437 std::vector<const BasicBlock*> CloneWorklist; 438 // 439 // OrigBBs consists of all blocks reachable from the entry 440 // block. 441 // This list will be pruned down by the CloneFunction() due to two 442 // two optimizations: 443 // First, when a conditional branch target is known at compile-time, 444 // only the actual branch destination block needs to be cloned. 445 // Second, when a switch statement target is known at compile-time, 446 // only the actual case statement needs to be cloned. 447 std::set<const BasicBlock *> OrigBBs; 448 449 CloneWorklist.push_back(&OldFunc->getEntryBlock()); 450 while (!CloneWorklist.empty()) { 451 const BasicBlock *BB = CloneWorklist.back(); 452 CloneWorklist.pop_back(); 453 454 // Don't revisit blocks. 455 if (VMap.count(BB)) 456 continue; 457 458 BasicBlock *NewBB = BasicBlock::Create(BB->getContext()); 459 if (BB->hasName()) 460 NewBB->setName(BB->getName() + NameSuffix); 461 462 // It is legal to clone a function when a block address within that 463 // function is never escapes outside of the function. Given that, we 464 // want to map block addresses from the old function to block addresses in 465 // the clone. (This is different from the generic ValueMapper 466 // implementation, which generates an invalid block address when 467 // cloning a function.) 468 // Note the current escape address does not catch all legal cases: even 469 // when all block addresses taken are local and the function has the 470 // always_inline attribute due to the indirect branch inlining is 471 // suppressed. 472 // Note that we don't need to fix the mapping for unreachable blocks; 473 // the default mapping there is safe. 474 if (BB->hasAddressTaken()) { 475 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 476 const_cast<BasicBlock*>(BB)); 477 VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB); 478 } 479 480 OrigBBs.insert(BB); 481 VMap[BB] = NewBB; 482 // Iterate over all possible successors and add them to the CloneWorklist. 483 const TerminatorInst *Term = BB->getTerminator(); 484 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) { 485 BasicBlock *Succ = Term->getSuccessor(i); 486 CloneWorklist.push_back(Succ); 487 } 488 } 489 490 // Now, fill only the reachable blocks with the cloned contents 491 // of the originals. 492 assert(CloneWorklist.empty() && "Dirty worklist before re-use\n"); 493 CloneWorklist.push_back(&OldFunc->getEntryBlock()); 494 while (!CloneWorklist.empty()) { 495 const BasicBlock *BB = CloneWorklist.back(); 496 CloneWorklist.pop_back(); 497 PFC.CloneBlock(BB, cast<BasicBlock>(VMap[BB]), CloneWorklist, 498 OrigBBs); 499 } 500 501 // FIXME: Delete BB's that were created but have been pruned. 502 // Actual cloning may have found pruning opportunities since 503 // branch or switch statement target may have been known at compile-time. 504 // Alternatively we could write a routine CloneFunction and add a) a 505 // parameter to actually do the cloning and b) a return parameter that 506 // gives a list of blocks that need to be cloned also. Then we could 507 // call CloneFunction when we collect the blocks to clone, but suppress 508 // cloning. And then actually *do* the cloning in the while loop above. Then 509 // the cleanup here would become redundant, and so would be the OrigBBs. 510 for (std::set<const BasicBlock *>::iterator Oi = OrigBBs.begin(), 511 Oe = OrigBBs.end(); Oi != Oe; ++Oi) { 512 const BasicBlock *Orig = *Oi; 513 BasicBlock *NewBB = cast<BasicBlock>(VMap[Orig]); 514 delete NewBB; 515 VMap[Orig] = 0; 516 } 517 518 // Loop over all of the basic blocks in the old function. If the block was 519 // reachable, we have cloned it and the old block is now in the value map: 520 // insert it into the new function in the right order. If not, ignore it. 521 // 522 // Defer PHI resolution until rest of function is resolved. 523 SmallVector<const PHINode*, 16> PHIToResolve; 524 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 525 BI != BE; ++BI) { 526 Value *V = VMap[BI]; 527 BasicBlock *NewBB = cast_or_null<BasicBlock>(V); 528 if (!NewBB) 529 continue; // Dead block. 530 531 // Add the new block to the new function. 532 NewFunc->getBasicBlockList().push_back(NewBB); 533 534 // Handle PHI nodes specially, as we have to remove references to dead 535 // blocks. 536 for (BasicBlock::const_iterator I = BI->begin(), E = BI->end(); I != E; ++I) 537 if (const PHINode *PN = dyn_cast<PHINode>(I)) 538 PHIToResolve.push_back(PN); 539 else 540 break; 541 542 // Finally, remap the terminator instructions, as those can't be remapped 543 // until all BBs are mapped. 544 RemapInstruction(NewBB->getTerminator(), VMap, 545 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 546 } 547 548 // Defer PHI resolution until rest of function is resolved, PHI resolution 549 // requires the CFG to be up-to-date. 550 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { 551 const PHINode *OPN = PHIToResolve[phino]; 552 unsigned NumPreds = OPN->getNumIncomingValues(); 553 const BasicBlock *OldBB = OPN->getParent(); 554 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); 555 556 // Map operands for blocks that are live and remove operands for blocks 557 // that are dead. 558 for (; phino != PHIToResolve.size() && 559 PHIToResolve[phino]->getParent() == OldBB; ++phino) { 560 OPN = PHIToResolve[phino]; 561 PHINode *PN = cast<PHINode>(VMap[OPN]); 562 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { 563 Value *V = VMap[PN->getIncomingBlock(pred)]; 564 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { 565 Value *InVal = MapValue(PN->getIncomingValue(pred), 566 VMap, 567 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 568 assert(InVal && "Unknown input value?"); 569 PN->setIncomingValue(pred, InVal); 570 PN->setIncomingBlock(pred, MappedBlock); 571 } else { 572 PN->removeIncomingValue(pred, false); 573 --pred, --e; // Revisit the next entry. 574 } 575 } 576 } 577 578 // The loop above has removed PHI entries for those blocks that are dead 579 // and has updated others. However, if a block is live (i.e. copied over) 580 // but its terminator has been changed to not go to this block, then our 581 // phi nodes will have invalid entries. Update the PHI nodes in this 582 // case. 583 PHINode *PN = cast<PHINode>(NewBB->begin()); 584 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); 585 if (NumPreds != PN->getNumIncomingValues()) { 586 assert(NumPreds < PN->getNumIncomingValues()); 587 // Count how many times each predecessor comes to this block. 588 std::map<BasicBlock*, unsigned> PredCount; 589 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); 590 PI != E; ++PI) 591 --PredCount[*PI]; 592 593 // Figure out how many entries to remove from each PHI. 594 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 595 ++PredCount[PN->getIncomingBlock(i)]; 596 597 // At this point, the excess predecessor entries are positive in the 598 // map. Loop over all of the PHIs and remove excess predecessor 599 // entries. 600 BasicBlock::iterator I = NewBB->begin(); 601 for (; (PN = dyn_cast<PHINode>(I)); ++I) { 602 for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), 603 E = PredCount.end(); PCI != E; ++PCI) { 604 BasicBlock *Pred = PCI->first; 605 for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) 606 PN->removeIncomingValue(Pred, false); 607 } 608 } 609 } 610 611 // If the loops above have made these phi nodes have 0 or 1 operand, 612 // replace them with undef or the input value. We must do this for 613 // correctness, because 0-operand phis are not valid. 614 PN = cast<PHINode>(NewBB->begin()); 615 if (PN->getNumIncomingValues() == 0) { 616 BasicBlock::iterator I = NewBB->begin(); 617 BasicBlock::const_iterator OldI = OldBB->begin(); 618 while ((PN = dyn_cast<PHINode>(I++))) { 619 Value *NV = UndefValue::get(PN->getType()); 620 PN->replaceAllUsesWith(NV); 621 assert(VMap[OldI] == PN && "VMap mismatch"); 622 VMap[OldI] = NV; 623 PN->eraseFromParent(); 624 ++OldI; 625 } 626 } 627 } 628 629 // Make a second pass over the PHINodes now that all of them have been 630 // remapped into the new function, simplifying the PHINode and performing any 631 // recursive simplifications exposed. This will transparently update the 632 // WeakVH in the VMap. Notably, we rely on that so that if we coalesce 633 // two PHINodes, the iteration over the old PHIs remains valid, and the 634 // mapping will just map us to the new node (which may not even be a PHI 635 // node). 636 for (unsigned Idx = 0, Size = PHIToResolve.size(); Idx != Size; ++Idx) 637 if (PHINode *PN = dyn_cast<PHINode>(VMap[PHIToResolve[Idx]])) 638 recursivelySimplifyInstruction(PN, DL); 639 640 // Now that the inlined function body has been fully constructed, go through 641 // and zap unconditional fall-through branches. This happen all the time when 642 // specializing code: code specialization turns conditional branches into 643 // uncond branches, and this code folds them. 644 Function::iterator Begin = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]); 645 Function::iterator I = Begin; 646 while (I != NewFunc->end()) { 647 // Check if this block has become dead during inlining or other 648 // simplifications. Note that the first block will appear dead, as it has 649 // not yet been wired up properly. 650 if (I != Begin && (pred_begin(I) == pred_end(I) || 651 I->getSinglePredecessor() == I)) { 652 BasicBlock *DeadBB = I++; 653 DeleteDeadBlock(DeadBB); 654 continue; 655 } 656 657 // We need to simplify conditional branches and switches with a constant 658 // operand. We try to prune these out when cloning, but if the 659 // simplification required looking through PHI nodes, those are only 660 // available after forming the full basic block. That may leave some here, 661 // and we still want to prune the dead code as early as possible. 662 ConstantFoldTerminator(I); 663 664 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); 665 if (!BI || BI->isConditional()) { ++I; continue; } 666 667 BasicBlock *Dest = BI->getSuccessor(0); 668 if (!Dest->getSinglePredecessor()) { 669 ++I; continue; 670 } 671 672 // We shouldn't be able to get single-entry PHI nodes here, as instsimplify 673 // above should have zapped all of them.. 674 assert(!isa<PHINode>(Dest->begin())); 675 676 // We know all single-entry PHI nodes in the inlined function have been 677 // removed, so we just need to splice the blocks. 678 BI->eraseFromParent(); 679 680 // Make all PHI nodes that referred to Dest now refer to I as their source. 681 Dest->replaceAllUsesWith(I); 682 683 // Move all the instructions in the succ to the pred. 684 I->getInstList().splice(I->end(), Dest->getInstList()); 685 686 // Remove the dest block. 687 Dest->eraseFromParent(); 688 689 // Do not increment I, iteratively merge all things this block branches to. 690 } 691 692 // Make a final pass over the basic blocks from theh old function to gather 693 // any return instructions which survived folding. We have to do this here 694 // because we can iteratively remove and merge returns above. 695 for (Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]), 696 E = NewFunc->end(); 697 I != E; ++I) 698 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) 699 Returns.push_back(RI); 700 } 701