LLVM API Documentation
00001 //===- CloneFunction.cpp - Clone a function into another function ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the CloneFunctionInto interface, which is used as the 00011 // low-level function cloner. This is used by the CloneFunction and function 00012 // inliner to do the dirty work of copying the body of a function around. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "llvm/Transforms/Utils/Cloning.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/DerivedTypes.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/GlobalVariable.h" 00021 #include "llvm/Function.h" 00022 #include "llvm/Support/CFG.h" 00023 #include "llvm/Support/Compiler.h" 00024 #include "llvm/Transforms/Utils/ValueMapper.h" 00025 #include "llvm/Analysis/ConstantFolding.h" 00026 #include "llvm/ADT/SmallVector.h" 00027 #include <map> 00028 using namespace llvm; 00029 00030 // CloneBasicBlock - See comments in Cloning.h 00031 BasicBlock *llvm::CloneBasicBlock(const BasicBlock *BB, 00032 DenseMap<const Value*, Value*> &ValueMap, 00033 const char *NameSuffix, Function *F, 00034 ClonedCodeInfo *CodeInfo) { 00035 BasicBlock *NewBB = BasicBlock::Create("", F); 00036 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 00037 00038 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 00039 00040 // Loop over all instructions, and copy them over. 00041 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); 00042 II != IE; ++II) { 00043 Instruction *NewInst = II->clone(); 00044 if (II->hasName()) 00045 NewInst->setName(II->getName()+NameSuffix); 00046 NewBB->getInstList().push_back(NewInst); 00047 ValueMap[II] = NewInst; // Add instruction map to value. 00048 00049 hasCalls |= isa<CallInst>(II); 00050 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 00051 if (isa<ConstantInt>(AI->getArraySize())) 00052 hasStaticAllocas = true; 00053 else 00054 hasDynamicAllocas = true; 00055 } 00056 } 00057 00058 if (CodeInfo) { 00059 CodeInfo->ContainsCalls |= hasCalls; 00060 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(BB->getTerminator()); 00061 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 00062 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 00063 BB != &BB->getParent()->getEntryBlock(); 00064 } 00065 return NewBB; 00066 } 00067 00068 // Clone OldFunc into NewFunc, transforming the old arguments into references to 00069 // ArgMap values. 00070 // 00071 void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc, 00072 DenseMap<const Value*, Value*> &ValueMap, 00073 std::vector<ReturnInst*> &Returns, 00074 const char *NameSuffix, ClonedCodeInfo *CodeInfo) { 00075 assert(NameSuffix && "NameSuffix cannot be null!"); 00076 00077 #ifndef NDEBUG 00078 for (Function::const_arg_iterator I = OldFunc->arg_begin(), 00079 E = OldFunc->arg_end(); I != E; ++I) 00080 assert(ValueMap.count(I) && "No mapping from source argument specified!"); 00081 #endif 00082 00083 // Clone any attributes. 00084 if (NewFunc->arg_size() == OldFunc->arg_size()) 00085 NewFunc->copyAttributesFrom(OldFunc); 00086 else { 00087 //Some arguments were deleted with the ValueMap. Copy arguments one by one 00088 for (Function::const_arg_iterator I = OldFunc->arg_begin(), 00089 E = OldFunc->arg_end(); I != E; ++I) 00090 if (Argument* Anew = dyn_cast<Argument>(ValueMap[I])) 00091 Anew->addAttr( OldFunc->getAttributes() 00092 .getParamAttributes(I->getArgNo() + 1)); 00093 NewFunc->setAttributes(NewFunc->getAttributes() 00094 .addAttr(0, OldFunc->getAttributes() 00095 .getRetAttributes())); 00096 NewFunc->setAttributes(NewFunc->getAttributes() 00097 .addAttr(~0, OldFunc->getAttributes() 00098 .getFnAttributes())); 00099 00100 } 00101 00102 // Loop over all of the basic blocks in the function, cloning them as 00103 // appropriate. Note that we save BE this way in order to handle cloning of 00104 // recursive functions into themselves. 00105 // 00106 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 00107 BI != BE; ++BI) { 00108 const BasicBlock &BB = *BI; 00109 00110 // Create a new basic block and copy instructions into it! 00111 BasicBlock *CBB = CloneBasicBlock(&BB, ValueMap, NameSuffix, NewFunc, 00112 CodeInfo); 00113 ValueMap[&BB] = CBB; // Add basic block mapping. 00114 00115 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator())) 00116 Returns.push_back(RI); 00117 } 00118 00119 // Loop over all of the instructions in the function, fixing up operand 00120 // references as we go. This uses ValueMap to do all the hard work. 00121 // 00122 for (Function::iterator BB = cast<BasicBlock>(ValueMap[OldFunc->begin()]), 00123 BE = NewFunc->end(); BB != BE; ++BB) 00124 // Loop over all instructions, fixing each one as we find it... 00125 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) 00126 RemapInstruction(II, ValueMap); 00127 } 00128 00129 /// CloneFunction - Return a copy of the specified function, but without 00130 /// embedding the function into another module. Also, any references specified 00131 /// in the ValueMap are changed to refer to their mapped value instead of the 00132 /// original one. If any of the arguments to the function are in the ValueMap, 00133 /// the arguments are deleted from the resultant function. The ValueMap is 00134 /// updated to include mappings from all of the instructions and basicblocks in 00135 /// the function from their old to new values. 00136 /// 00137 Function *llvm::CloneFunction(const Function *F, 00138 DenseMap<const Value*, Value*> &ValueMap, 00139 ClonedCodeInfo *CodeInfo) { 00140 std::vector<const Type*> ArgTypes; 00141 00142 // The user might be deleting arguments to the function by specifying them in 00143 // the ValueMap. If so, we need to not add the arguments to the arg ty vector 00144 // 00145 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 00146 I != E; ++I) 00147 if (ValueMap.count(I) == 0) // Haven't mapped the argument to anything yet? 00148 ArgTypes.push_back(I->getType()); 00149 00150 // Create a new function type... 00151 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(), 00152 ArgTypes, F->getFunctionType()->isVarArg()); 00153 00154 // Create the new function... 00155 Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName()); 00156 00157 // Loop over the arguments, copying the names of the mapped arguments over... 00158 Function::arg_iterator DestI = NewF->arg_begin(); 00159 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); 00160 I != E; ++I) 00161 if (ValueMap.count(I) == 0) { // Is this argument preserved? 00162 DestI->setName(I->getName()); // Copy the name over... 00163 ValueMap[I] = DestI++; // Add mapping to ValueMap 00164 } 00165 00166 std::vector<ReturnInst*> Returns; // Ignore returns cloned... 00167 CloneFunctionInto(NewF, F, ValueMap, Returns, "", CodeInfo); 00168 return NewF; 00169 } 00170 00171 00172 00173 namespace { 00174 /// PruningFunctionCloner - This class is a private class used to implement 00175 /// the CloneAndPruneFunctionInto method. 00176 struct VISIBILITY_HIDDEN PruningFunctionCloner { 00177 Function *NewFunc; 00178 const Function *OldFunc; 00179 DenseMap<const Value*, Value*> &ValueMap; 00180 std::vector<ReturnInst*> &Returns; 00181 const char *NameSuffix; 00182 ClonedCodeInfo *CodeInfo; 00183 const TargetData *TD; 00184 00185 public: 00186 PruningFunctionCloner(Function *newFunc, const Function *oldFunc, 00187 DenseMap<const Value*, Value*> &valueMap, 00188 std::vector<ReturnInst*> &returns, 00189 const char *nameSuffix, 00190 ClonedCodeInfo *codeInfo, 00191 const TargetData *td) 00192 : NewFunc(newFunc), OldFunc(oldFunc), ValueMap(valueMap), Returns(returns), 00193 NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) { 00194 } 00195 00196 /// CloneBlock - The specified block is found to be reachable, clone it and 00197 /// anything that it can reach. 00198 void CloneBlock(const BasicBlock *BB, 00199 std::vector<const BasicBlock*> &ToClone); 00200 00201 public: 00202 /// ConstantFoldMappedInstruction - Constant fold the specified instruction, 00203 /// mapping its operands through ValueMap if they are available. 00204 Constant *ConstantFoldMappedInstruction(const Instruction *I); 00205 }; 00206 } 00207 00208 /// CloneBlock - The specified block is found to be reachable, clone it and 00209 /// anything that it can reach. 00210 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB, 00211 std::vector<const BasicBlock*> &ToClone){ 00212 Value *&BBEntry = ValueMap[BB]; 00213 00214 // Have we already cloned this block? 00215 if (BBEntry) return; 00216 00217 // Nope, clone it now. 00218 BasicBlock *NewBB; 00219 BBEntry = NewBB = BasicBlock::Create(); 00220 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix); 00221 00222 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false; 00223 00224 // Loop over all instructions, and copy them over, DCE'ing as we go. This 00225 // loop doesn't include the terminator. 00226 for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end(); 00227 II != IE; ++II) { 00228 // If this instruction constant folds, don't bother cloning the instruction, 00229 // instead, just add the constant to the value map. 00230 if (Constant *C = ConstantFoldMappedInstruction(II)) { 00231 ValueMap[II] = C; 00232 continue; 00233 } 00234 00235 Instruction *NewInst = II->clone(); 00236 if (II->hasName()) 00237 NewInst->setName(II->getName()+NameSuffix); 00238 NewBB->getInstList().push_back(NewInst); 00239 ValueMap[II] = NewInst; // Add instruction map to value. 00240 00241 hasCalls |= isa<CallInst>(II); 00242 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) { 00243 if (isa<ConstantInt>(AI->getArraySize())) 00244 hasStaticAllocas = true; 00245 else 00246 hasDynamicAllocas = true; 00247 } 00248 } 00249 00250 // Finally, clone over the terminator. 00251 const TerminatorInst *OldTI = BB->getTerminator(); 00252 bool TerminatorDone = false; 00253 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) { 00254 if (BI->isConditional()) { 00255 // If the condition was a known constant in the callee... 00256 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition()); 00257 // Or is a known constant in the caller... 00258 if (Cond == 0) 00259 Cond = dyn_cast_or_null<ConstantInt>(ValueMap[BI->getCondition()]); 00260 00261 // Constant fold to uncond branch! 00262 if (Cond) { 00263 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue()); 00264 ValueMap[OldTI] = BranchInst::Create(Dest, NewBB); 00265 ToClone.push_back(Dest); 00266 TerminatorDone = true; 00267 } 00268 } 00269 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) { 00270 // If switching on a value known constant in the caller. 00271 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition()); 00272 if (Cond == 0) // Or known constant after constant prop in the callee... 00273 Cond = dyn_cast_or_null<ConstantInt>(ValueMap[SI->getCondition()]); 00274 if (Cond) { // Constant fold to uncond branch! 00275 BasicBlock *Dest = SI->getSuccessor(SI->findCaseValue(Cond)); 00276 ValueMap[OldTI] = BranchInst::Create(Dest, NewBB); 00277 ToClone.push_back(Dest); 00278 TerminatorDone = true; 00279 } 00280 } 00281 00282 if (!TerminatorDone) { 00283 Instruction *NewInst = OldTI->clone(); 00284 if (OldTI->hasName()) 00285 NewInst->setName(OldTI->getName()+NameSuffix); 00286 NewBB->getInstList().push_back(NewInst); 00287 ValueMap[OldTI] = NewInst; // Add instruction map to value. 00288 00289 // Recursively clone any reachable successor blocks. 00290 const TerminatorInst *TI = BB->getTerminator(); 00291 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) 00292 ToClone.push_back(TI->getSuccessor(i)); 00293 } 00294 00295 if (CodeInfo) { 00296 CodeInfo->ContainsCalls |= hasCalls; 00297 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(OldTI); 00298 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas; 00299 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas && 00300 BB != &BB->getParent()->front(); 00301 } 00302 00303 if (ReturnInst *RI = dyn_cast<ReturnInst>(NewBB->getTerminator())) 00304 Returns.push_back(RI); 00305 } 00306 00307 /// ConstantFoldMappedInstruction - Constant fold the specified instruction, 00308 /// mapping its operands through ValueMap if they are available. 00309 Constant *PruningFunctionCloner:: 00310 ConstantFoldMappedInstruction(const Instruction *I) { 00311 SmallVector<Constant*, 8> Ops; 00312 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 00313 if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i), 00314 ValueMap))) 00315 Ops.push_back(Op); 00316 else 00317 return 0; // All operands not constant! 00318 00319 if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 00320 return ConstantFoldCompareInstOperands(CI->getPredicate(), 00321 &Ops[0], Ops.size(), TD); 00322 00323 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) 00324 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) 00325 if (!LI->isVolatile() && CE->getOpcode() == Instruction::GetElementPtr) 00326 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) 00327 if (GV->isConstant() && !GV->isDeclaration()) 00328 return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), 00329 CE); 00330 00331 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0], 00332 Ops.size(), TD); 00333 } 00334 00335 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto, 00336 /// except that it does some simple constant prop and DCE on the fly. The 00337 /// effect of this is to copy significantly less code in cases where (for 00338 /// example) a function call with constant arguments is inlined, and those 00339 /// constant arguments cause a significant amount of code in the callee to be 00340 /// dead. Since this doesn't produce an exact copy of the input, it can't be 00341 /// used for things like CloneFunction or CloneModule. 00342 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, 00343 DenseMap<const Value*, Value*> &ValueMap, 00344 std::vector<ReturnInst*> &Returns, 00345 const char *NameSuffix, 00346 ClonedCodeInfo *CodeInfo, 00347 const TargetData *TD) { 00348 assert(NameSuffix && "NameSuffix cannot be null!"); 00349 00350 #ifndef NDEBUG 00351 for (Function::const_arg_iterator II = OldFunc->arg_begin(), 00352 E = OldFunc->arg_end(); II != E; ++II) 00353 assert(ValueMap.count(II) && "No mapping from source argument specified!"); 00354 #endif 00355 00356 PruningFunctionCloner PFC(NewFunc, OldFunc, ValueMap, Returns, 00357 NameSuffix, CodeInfo, TD); 00358 00359 // Clone the entry block, and anything recursively reachable from it. 00360 std::vector<const BasicBlock*> CloneWorklist; 00361 CloneWorklist.push_back(&OldFunc->getEntryBlock()); 00362 while (!CloneWorklist.empty()) { 00363 const BasicBlock *BB = CloneWorklist.back(); 00364 CloneWorklist.pop_back(); 00365 PFC.CloneBlock(BB, CloneWorklist); 00366 } 00367 00368 // Loop over all of the basic blocks in the old function. If the block was 00369 // reachable, we have cloned it and the old block is now in the value map: 00370 // insert it into the new function in the right order. If not, ignore it. 00371 // 00372 // Defer PHI resolution until rest of function is resolved. 00373 std::vector<const PHINode*> PHIToResolve; 00374 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end(); 00375 BI != BE; ++BI) { 00376 BasicBlock *NewBB = cast_or_null<BasicBlock>(ValueMap[BI]); 00377 if (NewBB == 0) continue; // Dead block. 00378 00379 // Add the new block to the new function. 00380 NewFunc->getBasicBlockList().push_back(NewBB); 00381 00382 // Loop over all of the instructions in the block, fixing up operand 00383 // references as we go. This uses ValueMap to do all the hard work. 00384 // 00385 BasicBlock::iterator I = NewBB->begin(); 00386 00387 // Handle PHI nodes specially, as we have to remove references to dead 00388 // blocks. 00389 if (PHINode *PN = dyn_cast<PHINode>(I)) { 00390 // Skip over all PHI nodes, remembering them for later. 00391 BasicBlock::const_iterator OldI = BI->begin(); 00392 for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI) 00393 PHIToResolve.push_back(cast<PHINode>(OldI)); 00394 } 00395 00396 // Otherwise, remap the rest of the instructions normally. 00397 for (; I != NewBB->end(); ++I) 00398 RemapInstruction(I, ValueMap); 00399 } 00400 00401 // Defer PHI resolution until rest of function is resolved, PHI resolution 00402 // requires the CFG to be up-to-date. 00403 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) { 00404 const PHINode *OPN = PHIToResolve[phino]; 00405 unsigned NumPreds = OPN->getNumIncomingValues(); 00406 const BasicBlock *OldBB = OPN->getParent(); 00407 BasicBlock *NewBB = cast<BasicBlock>(ValueMap[OldBB]); 00408 00409 // Map operands for blocks that are live and remove operands for blocks 00410 // that are dead. 00411 for (; phino != PHIToResolve.size() && 00412 PHIToResolve[phino]->getParent() == OldBB; ++phino) { 00413 OPN = PHIToResolve[phino]; 00414 PHINode *PN = cast<PHINode>(ValueMap[OPN]); 00415 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) { 00416 if (BasicBlock *MappedBlock = 00417 cast_or_null<BasicBlock>(ValueMap[PN->getIncomingBlock(pred)])) { 00418 Value *InVal = MapValue(PN->getIncomingValue(pred), ValueMap); 00419 assert(InVal && "Unknown input value?"); 00420 PN->setIncomingValue(pred, InVal); 00421 PN->setIncomingBlock(pred, MappedBlock); 00422 } else { 00423 PN->removeIncomingValue(pred, false); 00424 --pred, --e; // Revisit the next entry. 00425 } 00426 } 00427 } 00428 00429 // The loop above has removed PHI entries for those blocks that are dead 00430 // and has updated others. However, if a block is live (i.e. copied over) 00431 // but its terminator has been changed to not go to this block, then our 00432 // phi nodes will have invalid entries. Update the PHI nodes in this 00433 // case. 00434 PHINode *PN = cast<PHINode>(NewBB->begin()); 00435 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB)); 00436 if (NumPreds != PN->getNumIncomingValues()) { 00437 assert(NumPreds < PN->getNumIncomingValues()); 00438 // Count how many times each predecessor comes to this block. 00439 std::map<BasicBlock*, unsigned> PredCount; 00440 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); 00441 PI != E; ++PI) 00442 --PredCount[*PI]; 00443 00444 // Figure out how many entries to remove from each PHI. 00445 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 00446 ++PredCount[PN->getIncomingBlock(i)]; 00447 00448 // At this point, the excess predecessor entries are positive in the 00449 // map. Loop over all of the PHIs and remove excess predecessor 00450 // entries. 00451 BasicBlock::iterator I = NewBB->begin(); 00452 for (; (PN = dyn_cast<PHINode>(I)); ++I) { 00453 for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(), 00454 E = PredCount.end(); PCI != E; ++PCI) { 00455 BasicBlock *Pred = PCI->first; 00456 for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove) 00457 PN->removeIncomingValue(Pred, false); 00458 } 00459 } 00460 } 00461 00462 // If the loops above have made these phi nodes have 0 or 1 operand, 00463 // replace them with undef or the input value. We must do this for 00464 // correctness, because 0-operand phis are not valid. 00465 PN = cast<PHINode>(NewBB->begin()); 00466 if (PN->getNumIncomingValues() == 0) { 00467 BasicBlock::iterator I = NewBB->begin(); 00468 BasicBlock::const_iterator OldI = OldBB->begin(); 00469 while ((PN = dyn_cast<PHINode>(I++))) { 00470 Value *NV = UndefValue::get(PN->getType()); 00471 PN->replaceAllUsesWith(NV); 00472 assert(ValueMap[OldI] == PN && "ValueMap mismatch"); 00473 ValueMap[OldI] = NV; 00474 PN->eraseFromParent(); 00475 ++OldI; 00476 } 00477 } 00478 // NOTE: We cannot eliminate single entry phi nodes here, because of 00479 // ValueMap. Single entry phi nodes can have multiple ValueMap entries 00480 // pointing at them. Thus, deleting one would require scanning the ValueMap 00481 // to update any entries in it that would require that. This would be 00482 // really slow. 00483 } 00484 00485 // Now that the inlined function body has been fully constructed, go through 00486 // and zap unconditional fall-through branches. This happen all the time when 00487 // specializing code: code specialization turns conditional branches into 00488 // uncond branches, and this code folds them. 00489 Function::iterator I = cast<BasicBlock>(ValueMap[&OldFunc->getEntryBlock()]); 00490 while (I != NewFunc->end()) { 00491 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator()); 00492 if (!BI || BI->isConditional()) { ++I; continue; } 00493 00494 // Note that we can't eliminate uncond branches if the destination has 00495 // single-entry PHI nodes. Eliminating the single-entry phi nodes would 00496 // require scanning the ValueMap to update any entries that point to the phi 00497 // node. 00498 BasicBlock *Dest = BI->getSuccessor(0); 00499 if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) { 00500 ++I; continue; 00501 } 00502 00503 // We know all single-entry PHI nodes in the inlined function have been 00504 // removed, so we just need to splice the blocks. 00505 BI->eraseFromParent(); 00506 00507 // Move all the instructions in the succ to the pred. 00508 I->getInstList().splice(I->end(), Dest->getInstList()); 00509 00510 // Make all PHI nodes that referred to Dest now refer to I as their source. 00511 Dest->replaceAllUsesWith(I); 00512 00513 // Remove the dest block. 00514 Dest->eraseFromParent(); 00515 00516 // Do not increment I, iteratively merge all things this block branches to. 00517 } 00518 }