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/Constants.h" 18 #include "llvm/DerivedTypes.h" 19 #include "llvm/Instructions.h" 20 #include "llvm/IntrinsicInst.h" 21 #include "llvm/GlobalVariable.h" 22 #include "llvm/Function.h" 23 #include "llvm/LLVMContext.h" 24 #include "llvm/Metadata.h" 25 #include "llvm/Support/CFG.h" 26 #include "llvm/Transforms/Utils/ValueMapper.h" 27 #include "llvm/Analysis/ConstantFolding.h" 28 #include "llvm/Analysis/DebugInfo.h" 29 #include "llvm/ADT/SmallVector.h" 30 #include <map> 31 using namespace llvm; 32 33 // CloneBasicBlock - See comments in Cloning.h 34 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, 35 ValueToValueMapTy &VMap, 36 const Twine &NameSuffix, Function *F, 37 ClonedCodeInfo *CodeInfo) { 38 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "", F); 39 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 40 41 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 42 43 // Loop over all instructions, and copy them over. 44 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); 45 II != IE; ++II) { 46 Instruction *NewInst = II->clone(); 47 if (II->hasName()) 48 NewInst->setName(II->getName()+NameSuffix); 49 NewBB->getInstList().push_back(NewInst); 50 VMap[II] = NewInst; // Add instruction map to value. 51 52 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 53 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 54 if (isa<ConstantInt>(AI->getArraySize())) 55 hasStaticAllocas = true; 56 else 57 hasDynamicAllocas = true; 58 } 59 } 60 61 if (CodeInfo) { 62 CodeInfo->ContainsCalls |= hasCalls; 63 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator()); 64 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 65 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 66 BB != &BB->getParent()->getEntryBlock(); 67 } 68 return NewBB; 69 } 70 71 // Clone OldFunc into NewFunc, transforming the old arguments into references to 72 // VMap values. 73 // 74 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 75 ValueToValueMapTy &VMap, 76 bool ModuleLevelChanges, 77 SmallVectorImpl<ReturnInst*> &Returns, 78 const char *NameSuffix, ClonedCodeInfo *CodeInfo, 79 ValueMapTypeRemapper *TypeMapper) { 80 assert(NameSuffix && "NameSuffix cannot be null!"); 81 82 #ifndef NDEBUG 83 for (Function::const_arg_iterator I = OldFunc->arg_begin(), 84 E = OldFunc->arg_end(); I != E; ++I) 85 assert(VMap.count(I) && "No mapping from source argument specified!"); 86 #endif 87 88 // Clone any attributes. 89 if (NewFunc->arg_size() == OldFunc->arg_size()) 90 NewFunc->copyAttributesFrom(OldFunc); 91 else { 92 //Some arguments were deleted with the VMap. Copy arguments one by one 93 for (Function::const_arg_iterator I = OldFunc->arg_begin(), 94 E = OldFunc->arg_end(); I != E; ++I) 95 if (Argument* Anew = dyn_cast<Argument>(VMap[I])) 96 Anew->addAttr( OldFunc->getAttributes() 97 .getParamAttributes(I->getArgNo() + 1)); 98 NewFunc->setAttributes(NewFunc->getAttributes() 99 .addAttr(0, OldFunc->getAttributes() 100 .getRetAttributes())); 101 NewFunc->setAttributes(NewFunc->getAttributes() 102 .addAttr(~0, OldFunc->getAttributes() 103 .getFnAttributes())); 104 105 } 106 107 // Loop over all of the basic blocks in the function, cloning them as 108 // appropriate. Note that we save BE this way in order to handle cloning of 109 // recursive functions into themselves. 110 // 111 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 112 BI != BE; ++BI) { 113 const BasicBlock &BB = *BI; 114 115 // Create a new basic block and copy instructions into it! 116 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo); 117 118 // Add basic block mapping. 119 VMap[&BB] = CBB; 120 121 // It is only legal to clone a function if a block address within that 122 // function is never referenced outside of the function. Given that, we 123 // want to map block addresses from the old function to block addresses in 124 // the clone. (This is different from the generic ValueMapper 125 // implementation, which generates an invalid blockaddress when 126 // cloning a function.) 127 if (BB.hasAddressTaken()) { 128 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 129 const_cast<BasicBlock*>(&BB)); 130 VMap[OldBBAddr] = BlockAddress::get(NewFunc, CBB); 131 } 132 133 // Note return instructions for the caller. 134 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) 135 Returns.push_back(RI); 136 } 137 138 // Loop over all of the instructions in the function, fixing up operand 139 // references as we go. This uses VMap to do all the hard work. 140 for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]), 141 BE = NewFunc->end(); BB != BE; ++BB) 142 // Loop over all instructions, fixing each one as we find it... 143 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) 144 RemapInstruction(II, VMap, 145 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges, 146 TypeMapper); 147 } 148 149 /// CloneFunction - Return a copy of the specified function, but without 150 /// embedding the function into another module. Also, any references specified 151 /// in the VMap are changed to refer to their mapped value instead of the 152 /// original one. If any of the arguments to the function are in the VMap, 153 /// the arguments are deleted from the resultant function. The VMap is 154 /// updated to include mappings from all of the instructions and basicblocks in 155 /// the function from their old to new values. 156 /// 157 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap, 158 bool ModuleLevelChanges, 159 ClonedCodeInfo *CodeInfo) { 160 std::vector<Type*> ArgTypes; 161 162 // The user might be deleting arguments to the function by specifying them in 163 // the VMap. If so, we need to not add the arguments to the arg ty vector 164 // 165 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 166 I != E; ++I) 167 if (VMap.count(I) == 0) // Haven't mapped the argument to anything yet? 168 ArgTypes.push_back(I->getType()); 169 170 // Create a new function type... 171 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), 172 ArgTypes, F->getFunctionType()->isVarArg()); 173 174 // Create the new function... 175 Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName()); 176 177 // Loop over the arguments, copying the names of the mapped arguments over... 178 Function::arg_iterator DestI = NewF->arg_begin(); 179 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 180 I != E; ++I) 181 if (VMap.count(I) == 0) { // Is this argument preserved? 182 DestI->setName(I->getName()); // Copy the name over... 183 VMap[I] = DestI++; // Add mapping to VMap 184 } 185 186 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. 187 CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo); 188 return NewF; 189 } 190 191 192 193 namespace { 194 /// PruningFunctionCloner - This class is a private class used to implement 195 /// the CloneAndPruneFunctionInto method. 196 struct PruningFunctionCloner { 197 Function *NewFunc; 198 const Function *OldFunc; 199 ValueToValueMapTy &VMap; 200 bool ModuleLevelChanges; 201 SmallVectorImpl<ReturnInst*> &Returns; 202 const char *NameSuffix; 203 ClonedCodeInfo *CodeInfo; 204 const TargetData *TD; 205 public: 206 PruningFunctionCloner(Function *newFunc, const Function *oldFunc, 207 ValueToValueMapTy &valueMap, 208 bool moduleLevelChanges, 209 SmallVectorImpl<ReturnInst*> &returns, 210 const char *nameSuffix, 211 ClonedCodeInfo *codeInfo, 212 const TargetData *td) 213 : NewFunc(newFunc), OldFunc(oldFunc), 214 VMap(valueMap), ModuleLevelChanges(moduleLevelChanges), 215 Returns(returns), NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) { 216 } 217 218 /// CloneBlock - The specified block is found to be reachable, clone it and 219 /// anything that it can reach. 220 void CloneBlock(const BasicBlock *BB, 221 std::vector<const BasicBlock*> &ToClone); 222 223 public: 224 /// ConstantFoldMappedInstruction - Constant fold the specified instruction, 225 /// mapping its operands through VMap if they are available. 226 Constant *ConstantFoldMappedInstruction(const Instruction *I); 227 }; 228 } 229 230 /// CloneBlock - The specified block is found to be reachable, clone it and 231 /// anything that it can reach. 232 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, 233 std::vector<const BasicBlock*> &ToClone){ 234 TrackingVH<Value> &BBEntry = VMap[BB]; 235 236 // Have we already cloned this block? 237 if (BBEntry) return; 238 239 // Nope, clone it now. 240 BasicBlock *NewBB; 241 BBEntry = NewBB = BasicBlock::Create(BB->getContext()); 242 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 243 244 // It is only legal to clone a function if a block address within that 245 // function is never referenced outside of the function. Given that, we 246 // want to map block addresses from the old function to block addresses in 247 // the clone. (This is different from the generic ValueMapper 248 // implementation, which generates an invalid blockaddress when 249 // cloning a function.) 250 // 251 // Note that we don't need to fix the mapping for unreachable blocks; 252 // the default mapping there is safe. 253 if (BB->hasAddressTaken()) { 254 Constant *OldBBAddr = BlockAddress::get(const_cast<Function*>(OldFunc), 255 const_cast<BasicBlock*>(BB)); 256 VMap[OldBBAddr] = BlockAddress::get(NewFunc, NewBB); 257 } 258 259 260 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 261 262 // Loop over all instructions, and copy them over, DCE'ing as we go. This 263 // loop doesn't include the terminator. 264 for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end(); 265 II != IE; ++II) { 266 // If this instruction constant folds, don't bother cloning the instruction, 267 // instead, just add the constant to the value map. 268 if (Constant *C = ConstantFoldMappedInstruction(II)) { 269 VMap[II] = C; 270 continue; 271 } 272 273 Instruction *NewInst = II->clone(); 274 if (II->hasName()) 275 NewInst->setName(II->getName()+NameSuffix); 276 NewBB->getInstList().push_back(NewInst); 277 VMap[II] = NewInst; // Add instruction map to value. 278 279 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II)); 280 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 281 if (isa<ConstantInt>(AI->getArraySize())) 282 hasStaticAllocas = true; 283 else 284 hasDynamicAllocas = true; 285 } 286 } 287 288 // Finally, clone over the terminator. 289 const TerminatorInst *OldTI = BB->getTerminator(); 290 bool TerminatorDone = false; 291 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { 292 if (BI->isConditional()) { 293 // If the condition was a known constant in the callee... 294 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 295 // Or is a known constant in the caller... 296 if (Cond == 0) { 297 Value *V = VMap[BI->getCondition()]; 298 Cond = dyn_cast_or_null<ConstantInt>(V); 299 } 300 301 // Constant fold to uncond branch! 302 if (Cond) { 303 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); 304 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 305 ToClone.push_back(Dest); 306 TerminatorDone = true; 307 } 308 } 309 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { 310 // If switching on a value known constant in the caller. 311 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); 312 if (Cond == 0) { // Or known constant after constant prop in the callee... 313 Value *V = VMap[SI->getCondition()]; 314 Cond = dyn_cast_or_null<ConstantInt>(V); 315 } 316 if (Cond) { // Constant fold to uncond branch! 317 unsigned CaseIndex = SI->findCaseValue(Cond); 318 BasicBlock *Dest = SI->getSuccessor(SI->resolveSuccessorIndex(CaseIndex)); 319 VMap[OldTI] = BranchInst::Create(Dest, NewBB); 320 ToClone.push_back(Dest); 321 TerminatorDone = true; 322 } 323 } 324 325 if (!TerminatorDone) { 326 Instruction *NewInst = OldTI->clone(); 327 if (OldTI->hasName()) 328 NewInst->setName(OldTI->getName()+NameSuffix); 329 NewBB->getInstList().push_back(NewInst); 330 VMap[OldTI] = NewInst; // Add instruction map to value. 331 332 // Recursively clone any reachable successor blocks. 333 const TerminatorInst *TI = BB->getTerminator(); 334 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 335 ToClone.push_back(TI->getSuccessor(i)); 336 } 337 338 if (CodeInfo) { 339 CodeInfo->ContainsCalls |= hasCalls; 340 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(OldTI); 341 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 342 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 343 BB != &BB->getParent()->front(); 344 } 345 346 if (ReturnInst *RI = dyn_cast<ReturnInst>(NewBB->getTerminator())) 347 Returns.push_back(RI); 348 } 349 350 /// ConstantFoldMappedInstruction - Constant fold the specified instruction, 351 /// mapping its operands through VMap if they are available. 352 Constant *PruningFunctionCloner:: 353 ConstantFoldMappedInstruction(const Instruction *I) { 354 SmallVector<Constant*, 8> Ops; 355 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 356 if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i), 357 VMap, 358 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges))) 359 Ops.push_back(Op); 360 else 361 return 0; // All operands not constant! 362 363 if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 364 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], 365 TD); 366 367 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) 368 if (!LI->isVolatile()) 369 return ConstantFoldLoadFromConstPtr(Ops[0], TD); 370 371 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Ops, TD); 372 } 373 374 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, 375 /// except that it does some simple constant prop and DCE on the fly. The 376 /// effect of this is to copy significantly less code in cases where (for 377 /// example) a function call with constant arguments is inlined, and those 378 /// constant arguments cause a significant amount of code in the callee to be 379 /// dead. Since this doesn't produce an exact copy of the input, it can't be 380 /// used for things like CloneFunction or CloneModule. 381 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 382 ValueToValueMapTy &VMap, 383 bool ModuleLevelChanges, 384 SmallVectorImpl<ReturnInst*> &Returns, 385 const char *NameSuffix, 386 ClonedCodeInfo *CodeInfo, 387 const TargetData *TD, 388 Instruction *TheCall) { 389 assert(NameSuffix && "NameSuffix cannot be null!"); 390 391 #ifndef NDEBUG 392 for (Function::const_arg_iterator II = OldFunc->arg_begin(), 393 E = OldFunc->arg_end(); II != E; ++II) 394 assert(VMap.count(II) && "No mapping from source argument specified!"); 395 #endif 396 397 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges, 398 Returns, NameSuffix, CodeInfo, TD); 399 400 // Clone the entry block, and anything recursively reachable from it. 401 std::vector<const BasicBlock*> CloneWorklist; 402 CloneWorklist.push_back(&OldFunc->getEntryBlock()); 403 while (!CloneWorklist.empty()) { 404 const BasicBlock *BB = CloneWorklist.back(); 405 CloneWorklist.pop_back(); 406 PFC.CloneBlock(BB, CloneWorklist); 407 } 408 409 // Loop over all of the basic blocks in the old function. If the block was 410 // reachable, we have cloned it and the old block is now in the value map: 411 // insert it into the new function in the right order. If not, ignore it. 412 // 413 // Defer PHI resolution until rest of function is resolved. 414 SmallVector<const PHINode*, 16> PHIToResolve; 415 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 416 BI != BE; ++BI) { 417 Value *V = VMap[BI]; 418 BasicBlock *NewBB = cast_or_null<BasicBlock>(V); 419 if (NewBB == 0) continue; // Dead block. 420 421 // Add the new block to the new function. 422 NewFunc->getBasicBlockList().push_back(NewBB); 423 424 // Loop over all of the instructions in the block, fixing up operand 425 // references as we go. This uses VMap to do all the hard work. 426 // 427 BasicBlock::iterator I = NewBB->begin(); 428 429 DebugLoc TheCallDL; 430 if (TheCall) 431 TheCallDL = TheCall->getDebugLoc(); 432 433 // Handle PHI nodes specially, as we have to remove references to dead 434 // blocks. 435 if (PHINode *PN = dyn_cast<PHINode>(I)) { 436 // Skip over all PHI nodes, remembering them for later. 437 BasicBlock::const_iterator OldI = BI->begin(); 438 for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI) 439 PHIToResolve.push_back(cast<PHINode>(OldI)); 440 } 441 442 // Otherwise, remap the rest of the instructions normally. 443 for (; I != NewBB->end(); ++I) 444 RemapInstruction(I, VMap, 445 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 446 } 447 448 // Defer PHI resolution until rest of function is resolved, PHI resolution 449 // requires the CFG to be up-to-date. 450 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { 451 const PHINode *OPN = PHIToResolve[phino]; 452 unsigned NumPreds = OPN->getNumIncomingValues(); 453 const BasicBlock *OldBB = OPN->getParent(); 454 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]); 455 456 // Map operands for blocks that are live and remove operands for blocks 457 // that are dead. 458 for (; phino != PHIToResolve.size() && 459 PHIToResolve[phino]->getParent() == OldBB; ++phino) { 460 OPN = PHIToResolve[phino]; 461 PHINode *PN = cast<PHINode>(VMap[OPN]); 462 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { 463 Value *V = VMap[PN->getIncomingBlock(pred)]; 464 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) { 465 Value *InVal = MapValue(PN->getIncomingValue(pred), 466 VMap, 467 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges); 468 assert(InVal && "Unknown input value?"); 469 PN->setIncomingValue(pred, InVal); 470 PN->setIncomingBlock(pred, MappedBlock); 471 } else { 472 PN->removeIncomingValue(pred, false); 473 --pred, --e; // Revisit the next entry. 474 } 475 } 476 } 477 478 // The loop above has removed PHI entries for those blocks that are dead 479 // and has updated others. However, if a block is live (i.e. copied over) 480 // but its terminator has been changed to not go to this block, then our 481 // phi nodes will have invalid entries. Update the PHI nodes in this 482 // case. 483 PHINode *PN = cast<PHINode>(NewBB->begin()); 484 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); 485 if (NumPreds != PN->getNumIncomingValues()) { 486 assert(NumPreds < PN->getNumIncomingValues()); 487 // Count how many times each predecessor comes to this block. 488 std::map<BasicBlock*, unsigned> PredCount; 489 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); 490 PI != E; ++PI) 491 --PredCount[*PI]; 492 493 // Figure out how many entries to remove from each PHI. 494 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 495 ++PredCount[PN->getIncomingBlock(i)]; 496 497 // At this point, the excess predecessor entries are positive in the 498 // map. Loop over all of the PHIs and remove excess predecessor 499 // entries. 500 BasicBlock::iterator I = NewBB->begin(); 501 for (; (PN = dyn_cast<PHINode>(I)); ++I) { 502 for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), 503 E = PredCount.end(); PCI != E; ++PCI) { 504 BasicBlock *Pred = PCI->first; 505 for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) 506 PN->removeIncomingValue(Pred, false); 507 } 508 } 509 } 510 511 // If the loops above have made these phi nodes have 0 or 1 operand, 512 // replace them with undef or the input value. We must do this for 513 // correctness, because 0-operand phis are not valid. 514 PN = cast<PHINode>(NewBB->begin()); 515 if (PN->getNumIncomingValues() == 0) { 516 BasicBlock::iterator I = NewBB->begin(); 517 BasicBlock::const_iterator OldI = OldBB->begin(); 518 while ((PN = dyn_cast<PHINode>(I++))) { 519 Value *NV = UndefValue::get(PN->getType()); 520 PN->replaceAllUsesWith(NV); 521 assert(VMap[OldI] == PN && "VMap mismatch"); 522 VMap[OldI] = NV; 523 PN->eraseFromParent(); 524 ++OldI; 525 } 526 } 527 // NOTE: We cannot eliminate single entry phi nodes here, because of 528 // VMap. Single entry phi nodes can have multiple VMap entries 529 // pointing at them. Thus, deleting one would require scanning the VMap 530 // to update any entries in it that would require that. This would be 531 // really slow. 532 } 533 534 // Now that the inlined function body has been fully constructed, go through 535 // and zap unconditional fall-through branches. This happen all the time when 536 // specializing code: code specialization turns conditional branches into 537 // uncond branches, and this code folds them. 538 Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]); 539 while (I != NewFunc->end()) { 540 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); 541 if (!BI || BI->isConditional()) { ++I; continue; } 542 543 // Note that we can't eliminate uncond branches if the destination has 544 // single-entry PHI nodes. Eliminating the single-entry phi nodes would 545 // require scanning the VMap to update any entries that point to the phi 546 // node. 547 BasicBlock *Dest = BI->getSuccessor(0); 548 if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) { 549 ++I; continue; 550 } 551 552 // We know all single-entry PHI nodes in the inlined function have been 553 // removed, so we just need to splice the blocks. 554 BI->eraseFromParent(); 555 556 // Make all PHI nodes that referred to Dest now refer to I as their source. 557 Dest->replaceAllUsesWith(I); 558 559 // Move all the instructions in the succ to the pred. 560 I->getInstList().splice(I->end(), Dest->getInstList()); 561 562 // Remove the dest block. 563 Dest->eraseFromParent(); 564 565 // Do not increment I, iteratively merge all things this block branches to. 566 } 567 } 568