Revert r131155 for now. It makes VMCore depend on Analysis and Transforms
[llvm/stm8.git] / lib / Transforms / Utils / CloneFunction.cpp
blobd967ceb968561ab4ef0ddbb404a6dae350b03b9c
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.
14 //===----------------------------------------------------------------------===//
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;
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);
41 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
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.
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;
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();
68 return NewBB;
71 // Clone OldFunc into NewFunc, transforming the old arguments into references to
72 // VMap values.
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 assert(NameSuffix && "NameSuffix cannot be null!");
81 #ifndef NDEBUG
82 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
83 E = OldFunc->arg_end(); I != E; ++I)
84 assert(VMap.count(I) && "No mapping from source argument specified!");
85 #endif
87 // Clone any attributes.
88 if (NewFunc->arg_size() == OldFunc->arg_size())
89 NewFunc->copyAttributesFrom(OldFunc);
90 else {
91 //Some arguments were deleted with the VMap. Copy arguments one by one
92 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
93 E = OldFunc->arg_end(); I != E; ++I)
94 if (Argument* Anew = dyn_cast<Argument>(VMap[I]))
95 Anew->addAttr( OldFunc->getAttributes()
96 .getParamAttributes(I->getArgNo() + 1));
97 NewFunc->setAttributes(NewFunc->getAttributes()
98 .addAttr(0, OldFunc->getAttributes()
99 .getRetAttributes()));
100 NewFunc->setAttributes(NewFunc->getAttributes()
101 .addAttr(~0, OldFunc->getAttributes()
102 .getFnAttributes()));
106 // Loop over all of the basic blocks in the function, cloning them as
107 // appropriate. Note that we save BE this way in order to handle cloning of
108 // recursive functions into themselves.
110 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
111 BI != BE; ++BI) {
112 const BasicBlock &BB = *BI;
114 // Create a new basic block and copy instructions into it!
115 BasicBlock *CBB = CloneBasicBlock(&BB, VMap, NameSuffix, NewFunc, CodeInfo);
116 VMap[&BB] = CBB; // Add basic block mapping.
118 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
119 Returns.push_back(RI);
122 // Loop over all of the instructions in the function, fixing up operand
123 // references as we go. This uses VMap to do all the hard work.
124 for (Function::iterator BB = cast<BasicBlock>(VMap[OldFunc->begin()]),
125 BE = NewFunc->end(); BB != BE; ++BB)
126 // Loop over all instructions, fixing each one as we find it...
127 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
128 RemapInstruction(II, VMap,
129 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
132 /// CloneFunction - Return a copy of the specified function, but without
133 /// embedding the function into another module. Also, any references specified
134 /// in the VMap are changed to refer to their mapped value instead of the
135 /// original one. If any of the arguments to the function are in the VMap,
136 /// the arguments are deleted from the resultant function. The VMap is
137 /// updated to include mappings from all of the instructions and basicblocks in
138 /// the function from their old to new values.
140 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
141 bool ModuleLevelChanges,
142 ClonedCodeInfo *CodeInfo) {
143 std::vector<const Type*> ArgTypes;
145 // The user might be deleting arguments to the function by specifying them in
146 // the VMap. If so, we need to not add the arguments to the arg ty vector
148 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
149 I != E; ++I)
150 if (VMap.count(I) == 0) // Haven't mapped the argument to anything yet?
151 ArgTypes.push_back(I->getType());
153 // Create a new function type...
154 FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
155 ArgTypes, F->getFunctionType()->isVarArg());
157 // Create the new function...
158 Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
160 // Loop over the arguments, copying the names of the mapped arguments over...
161 Function::arg_iterator DestI = NewF->arg_begin();
162 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
163 I != E; ++I)
164 if (VMap.count(I) == 0) { // Is this argument preserved?
165 DestI->setName(I->getName()); // Copy the name over...
166 VMap[I] = DestI++; // Add mapping to VMap
169 SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned.
170 CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
171 return NewF;
176 namespace {
177 /// PruningFunctionCloner - This class is a private class used to implement
178 /// the CloneAndPruneFunctionInto method.
179 struct PruningFunctionCloner {
180 Function *NewFunc;
181 const Function *OldFunc;
182 ValueToValueMapTy &VMap;
183 bool ModuleLevelChanges;
184 SmallVectorImpl<ReturnInst*> &Returns;
185 const char *NameSuffix;
186 ClonedCodeInfo *CodeInfo;
187 const TargetData *TD;
188 public:
189 PruningFunctionCloner(Function *newFunc, const Function *oldFunc,
190 ValueToValueMapTy &valueMap,
191 bool moduleLevelChanges,
192 SmallVectorImpl<ReturnInst*> &returns,
193 const char *nameSuffix,
194 ClonedCodeInfo *codeInfo,
195 const TargetData *td)
196 : NewFunc(newFunc), OldFunc(oldFunc),
197 VMap(valueMap), ModuleLevelChanges(moduleLevelChanges),
198 Returns(returns), NameSuffix(nameSuffix), CodeInfo(codeInfo), TD(td) {
201 /// CloneBlock - The specified block is found to be reachable, clone it and
202 /// anything that it can reach.
203 void CloneBlock(const BasicBlock *BB,
204 std::vector<const BasicBlock*> &ToClone);
206 public:
207 /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
208 /// mapping its operands through VMap if they are available.
209 Constant *ConstantFoldMappedInstruction(const Instruction *I);
213 /// CloneBlock - The specified block is found to be reachable, clone it and
214 /// anything that it can reach.
215 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
216 std::vector<const BasicBlock*> &ToClone){
217 TrackingVH<Value> &BBEntry = VMap[BB];
219 // Have we already cloned this block?
220 if (BBEntry) return;
222 // Nope, clone it now.
223 BasicBlock *NewBB;
224 BBEntry = NewBB = BasicBlock::Create(BB->getContext());
225 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
227 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
229 // Loop over all instructions, and copy them over, DCE'ing as we go. This
230 // loop doesn't include the terminator.
231 for (BasicBlock::const_iterator II = BB->begin(), IE = --BB->end();
232 II != IE; ++II) {
233 // If this instruction constant folds, don't bother cloning the instruction,
234 // instead, just add the constant to the value map.
235 if (Constant *C = ConstantFoldMappedInstruction(II)) {
236 VMap[II] = C;
237 continue;
240 Instruction *NewInst = II->clone();
241 if (II->hasName())
242 NewInst->setName(II->getName()+NameSuffix);
243 NewBB->getInstList().push_back(NewInst);
244 VMap[II] = NewInst; // Add instruction map to value.
246 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
247 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
248 if (isa<ConstantInt>(AI->getArraySize()))
249 hasStaticAllocas = true;
250 else
251 hasDynamicAllocas = true;
255 // Finally, clone over the terminator.
256 const TerminatorInst *OldTI = BB->getTerminator();
257 bool TerminatorDone = false;
258 if (const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
259 if (BI->isConditional()) {
260 // If the condition was a known constant in the callee...
261 ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
262 // Or is a known constant in the caller...
263 if (Cond == 0) {
264 Value *V = VMap[BI->getCondition()];
265 Cond = dyn_cast_or_null<ConstantInt>(V);
268 // Constant fold to uncond branch!
269 if (Cond) {
270 BasicBlock *Dest = BI->getSuccessor(!Cond->getZExtValue());
271 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
272 ToClone.push_back(Dest);
273 TerminatorDone = true;
276 } else if (const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
277 // If switching on a value known constant in the caller.
278 ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition());
279 if (Cond == 0) { // Or known constant after constant prop in the callee...
280 Value *V = VMap[SI->getCondition()];
281 Cond = dyn_cast_or_null<ConstantInt>(V);
283 if (Cond) { // Constant fold to uncond branch!
284 BasicBlock *Dest = SI->getSuccessor(SI->findCaseValue(Cond));
285 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
286 ToClone.push_back(Dest);
287 TerminatorDone = true;
291 if (!TerminatorDone) {
292 Instruction *NewInst = OldTI->clone();
293 if (OldTI->hasName())
294 NewInst->setName(OldTI->getName()+NameSuffix);
295 NewBB->getInstList().push_back(NewInst);
296 VMap[OldTI] = NewInst; // Add instruction map to value.
298 // Recursively clone any reachable successor blocks.
299 const TerminatorInst *TI = BB->getTerminator();
300 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
301 ToClone.push_back(TI->getSuccessor(i));
304 if (CodeInfo) {
305 CodeInfo->ContainsCalls |= hasCalls;
306 CodeInfo->ContainsUnwinds |= isa<UnwindInst>(OldTI);
307 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
308 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
309 BB != &BB->getParent()->front();
312 if (ReturnInst *RI = dyn_cast<ReturnInst>(NewBB->getTerminator()))
313 Returns.push_back(RI);
316 /// ConstantFoldMappedInstruction - Constant fold the specified instruction,
317 /// mapping its operands through VMap if they are available.
318 Constant *PruningFunctionCloner::
319 ConstantFoldMappedInstruction(const Instruction *I) {
320 SmallVector<Constant*, 8> Ops;
321 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
322 if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i),
323 VMap,
324 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges)))
325 Ops.push_back(Op);
326 else
327 return 0; // All operands not constant!
329 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
330 return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
331 TD);
333 if (const LoadInst *LI = dyn_cast<LoadInst>(I))
334 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))
335 if (!LI->isVolatile() && CE->getOpcode() == Instruction::GetElementPtr)
336 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
337 if (GV->isConstant() && GV->hasDefinitiveInitializer())
338 return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(),
339 CE);
341 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0],
342 Ops.size(), TD);
345 static DebugLoc
346 UpdateInlinedAtInfo(const DebugLoc &InsnDL, const DebugLoc &TheCallDL,
347 LLVMContext &Ctx) {
348 DebugLoc NewLoc = TheCallDL;
349 if (MDNode *IA = InsnDL.getInlinedAt(Ctx))
350 NewLoc = UpdateInlinedAtInfo(DebugLoc::getFromDILocation(IA), TheCallDL,
351 Ctx);
353 return DebugLoc::get(InsnDL.getLine(), InsnDL.getCol(),
354 InsnDL.getScope(Ctx), NewLoc.getAsMDNode(Ctx));
357 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
358 /// except that it does some simple constant prop and DCE on the fly. The
359 /// effect of this is to copy significantly less code in cases where (for
360 /// example) a function call with constant arguments is inlined, and those
361 /// constant arguments cause a significant amount of code in the callee to be
362 /// dead. Since this doesn't produce an exact copy of the input, it can't be
363 /// used for things like CloneFunction or CloneModule.
364 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
365 ValueToValueMapTy &VMap,
366 bool ModuleLevelChanges,
367 SmallVectorImpl<ReturnInst*> &Returns,
368 const char *NameSuffix,
369 ClonedCodeInfo *CodeInfo,
370 const TargetData *TD,
371 Instruction *TheCall) {
372 assert(NameSuffix && "NameSuffix cannot be null!");
374 #ifndef NDEBUG
375 for (Function::const_arg_iterator II = OldFunc->arg_begin(),
376 E = OldFunc->arg_end(); II != E; ++II)
377 assert(VMap.count(II) && "No mapping from source argument specified!");
378 #endif
380 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
381 Returns, NameSuffix, CodeInfo, TD);
383 // Clone the entry block, and anything recursively reachable from it.
384 std::vector<const BasicBlock*> CloneWorklist;
385 CloneWorklist.push_back(&OldFunc->getEntryBlock());
386 while (!CloneWorklist.empty()) {
387 const BasicBlock *BB = CloneWorklist.back();
388 CloneWorklist.pop_back();
389 PFC.CloneBlock(BB, CloneWorklist);
392 // Loop over all of the basic blocks in the old function. If the block was
393 // reachable, we have cloned it and the old block is now in the value map:
394 // insert it into the new function in the right order. If not, ignore it.
396 // Defer PHI resolution until rest of function is resolved.
397 SmallVector<const PHINode*, 16> PHIToResolve;
398 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
399 BI != BE; ++BI) {
400 Value *V = VMap[BI];
401 BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
402 if (NewBB == 0) continue; // Dead block.
404 // Add the new block to the new function.
405 NewFunc->getBasicBlockList().push_back(NewBB);
407 // Loop over all of the instructions in the block, fixing up operand
408 // references as we go. This uses VMap to do all the hard work.
410 BasicBlock::iterator I = NewBB->begin();
412 DebugLoc TheCallDL;
413 if (TheCall)
414 TheCallDL = TheCall->getDebugLoc();
416 // Handle PHI nodes specially, as we have to remove references to dead
417 // blocks.
418 if (PHINode *PN = dyn_cast<PHINode>(I)) {
419 // Skip over all PHI nodes, remembering them for later.
420 BasicBlock::const_iterator OldI = BI->begin();
421 for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI) {
422 if (I->hasMetadata()) {
423 if (!TheCallDL.isUnknown()) {
424 DebugLoc IDL = I->getDebugLoc();
425 if (!IDL.isUnknown()) {
426 DebugLoc NewDL = UpdateInlinedAtInfo(IDL, TheCallDL,
427 I->getContext());
428 I->setDebugLoc(NewDL);
430 } else {
431 // The cloned instruction has dbg info but the call instruction
432 // does not have dbg info. Remove dbg info from cloned instruction.
433 I->setDebugLoc(DebugLoc());
436 PHIToResolve.push_back(cast<PHINode>(OldI));
440 // FIXME:
441 // FIXME:
442 // FIXME: Unclone all this metadata stuff.
443 // FIXME:
444 // FIXME:
446 // Otherwise, remap the rest of the instructions normally.
447 for (; I != NewBB->end(); ++I) {
448 if (I->hasMetadata()) {
449 if (!TheCallDL.isUnknown()) {
450 DebugLoc IDL = I->getDebugLoc();
451 if (!IDL.isUnknown()) {
452 DebugLoc NewDL = UpdateInlinedAtInfo(IDL, TheCallDL,
453 I->getContext());
454 I->setDebugLoc(NewDL);
456 } else {
457 // The cloned instruction has dbg info but the call instruction
458 // does not have dbg info. Remove dbg info from cloned instruction.
459 I->setDebugLoc(DebugLoc());
462 RemapInstruction(I, VMap,
463 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
467 // Defer PHI resolution until rest of function is resolved, PHI resolution
468 // requires the CFG to be up-to-date.
469 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
470 const PHINode *OPN = PHIToResolve[phino];
471 unsigned NumPreds = OPN->getNumIncomingValues();
472 const BasicBlock *OldBB = OPN->getParent();
473 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
475 // Map operands for blocks that are live and remove operands for blocks
476 // that are dead.
477 for (; phino != PHIToResolve.size() &&
478 PHIToResolve[phino]->getParent() == OldBB; ++phino) {
479 OPN = PHIToResolve[phino];
480 PHINode *PN = cast<PHINode>(VMap[OPN]);
481 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
482 Value *V = VMap[PN->getIncomingBlock(pred)];
483 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
484 Value *InVal = MapValue(PN->getIncomingValue(pred),
485 VMap,
486 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
487 assert(InVal && "Unknown input value?");
488 PN->setIncomingValue(pred, InVal);
489 PN->setIncomingBlock(pred, MappedBlock);
490 } else {
491 PN->removeIncomingValue(pred, false);
492 --pred, --e; // Revisit the next entry.
497 // The loop above has removed PHI entries for those blocks that are dead
498 // and has updated others. However, if a block is live (i.e. copied over)
499 // but its terminator has been changed to not go to this block, then our
500 // phi nodes will have invalid entries. Update the PHI nodes in this
501 // case.
502 PHINode *PN = cast<PHINode>(NewBB->begin());
503 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
504 if (NumPreds != PN->getNumIncomingValues()) {
505 assert(NumPreds < PN->getNumIncomingValues());
506 // Count how many times each predecessor comes to this block.
507 std::map<BasicBlock*, unsigned> PredCount;
508 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
509 PI != E; ++PI)
510 --PredCount[*PI];
512 // Figure out how many entries to remove from each PHI.
513 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
514 ++PredCount[PN->getIncomingBlock(i)];
516 // At this point, the excess predecessor entries are positive in the
517 // map. Loop over all of the PHIs and remove excess predecessor
518 // entries.
519 BasicBlock::iterator I = NewBB->begin();
520 for (; (PN = dyn_cast<PHINode>(I)); ++I) {
521 for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(),
522 E = PredCount.end(); PCI != E; ++PCI) {
523 BasicBlock *Pred = PCI->first;
524 for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove)
525 PN->removeIncomingValue(Pred, false);
530 // If the loops above have made these phi nodes have 0 or 1 operand,
531 // replace them with undef or the input value. We must do this for
532 // correctness, because 0-operand phis are not valid.
533 PN = cast<PHINode>(NewBB->begin());
534 if (PN->getNumIncomingValues() == 0) {
535 BasicBlock::iterator I = NewBB->begin();
536 BasicBlock::const_iterator OldI = OldBB->begin();
537 while ((PN = dyn_cast<PHINode>(I++))) {
538 Value *NV = UndefValue::get(PN->getType());
539 PN->replaceAllUsesWith(NV);
540 assert(VMap[OldI] == PN && "VMap mismatch");
541 VMap[OldI] = NV;
542 PN->eraseFromParent();
543 ++OldI;
546 // NOTE: We cannot eliminate single entry phi nodes here, because of
547 // VMap. Single entry phi nodes can have multiple VMap entries
548 // pointing at them. Thus, deleting one would require scanning the VMap
549 // to update any entries in it that would require that. This would be
550 // really slow.
553 // Now that the inlined function body has been fully constructed, go through
554 // and zap unconditional fall-through branches. This happen all the time when
555 // specializing code: code specialization turns conditional branches into
556 // uncond branches, and this code folds them.
557 Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]);
558 while (I != NewFunc->end()) {
559 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
560 if (!BI || BI->isConditional()) { ++I; continue; }
562 // Note that we can't eliminate uncond branches if the destination has
563 // single-entry PHI nodes. Eliminating the single-entry phi nodes would
564 // require scanning the VMap to update any entries that point to the phi
565 // node.
566 BasicBlock *Dest = BI->getSuccessor(0);
567 if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) {
568 ++I; continue;
571 // We know all single-entry PHI nodes in the inlined function have been
572 // removed, so we just need to splice the blocks.
573 BI->eraseFromParent();
575 // Move all the instructions in the succ to the pred.
576 I->getInstList().splice(I->end(), Dest->getInstList());
578 // Make all PHI nodes that referred to Dest now refer to I as their source.
579 Dest->replaceAllUsesWith(I);
581 // Remove the dest block.
582 Dest->eraseFromParent();
584 // Do not increment I, iteratively merge all things this block branches to.