]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Transforms/Utils/CloneFunction.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / lib / Transforms / Utils / CloneFunction.cpp
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   assert(NameSuffix && "NameSuffix cannot be null!");
80
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
86
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()));
103
104   }
105
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.
109   //
110   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
111        BI != BE; ++BI) {
112     const BasicBlock &BB = *BI;
113
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.
117
118     if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
119       Returns.push_back(RI);
120   }
121
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);
130 }
131
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.
139 ///
140 Function *llvm::CloneFunction(const Function *F, ValueToValueMapTy &VMap,
141                               bool ModuleLevelChanges,
142                               ClonedCodeInfo *CodeInfo) {
143   std::vector<Type*> ArgTypes;
144
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
147   //
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());
152
153   // Create a new function type...
154   FunctionType *FTy = FunctionType::get(F->getFunctionType()->getReturnType(),
155                                     ArgTypes, F->getFunctionType()->isVarArg());
156
157   // Create the new function...
158   Function *NewF = Function::Create(FTy, F->getLinkage(), F->getName());
159
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
167     }
168
169   SmallVector<ReturnInst*, 8> Returns;  // Ignore returns cloned.
170   CloneFunctionInto(NewF, F, VMap, ModuleLevelChanges, Returns, "", CodeInfo);
171   return NewF;
172 }
173
174
175
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) {
199     }
200
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);
205     
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);
210   };
211 }
212
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];
218
219   // Have we already cloned this block?
220   if (BBEntry) return;
221   
222   // Nope, clone it now.
223   BasicBlock *NewBB;
224   BBEntry = NewBB = BasicBlock::Create(BB->getContext());
225   if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
226
227   bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
228   
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;
238     }
239
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.
245     
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;
252     }
253   }
254   
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);
266       }
267
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;
274       }
275     }
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);
282     }
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;
288     }
289   }
290   
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.
297     
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));
302   }
303   
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();
310   }
311   
312   if (ReturnInst *RI = dyn_cast<ReturnInst>(NewBB->getTerminator()))
313     Returns.push_back(RI);
314 }
315
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!
328
329   if (const CmpInst *CI = dyn_cast<CmpInst>(I))
330     return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
331                                            TD);
332
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);
340
341   return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Ops[0],
342                                   Ops.size(), TD);
343 }
344
345 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
346 /// except that it does some simple constant prop and DCE on the fly.  The
347 /// effect of this is to copy significantly less code in cases where (for
348 /// example) a function call with constant arguments is inlined, and those
349 /// constant arguments cause a significant amount of code in the callee to be
350 /// dead.  Since this doesn't produce an exact copy of the input, it can't be
351 /// used for things like CloneFunction or CloneModule.
352 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
353                                      ValueToValueMapTy &VMap,
354                                      bool ModuleLevelChanges,
355                                      SmallVectorImpl<ReturnInst*> &Returns,
356                                      const char *NameSuffix, 
357                                      ClonedCodeInfo *CodeInfo,
358                                      const TargetData *TD,
359                                      Instruction *TheCall) {
360   assert(NameSuffix && "NameSuffix cannot be null!");
361   
362 #ifndef NDEBUG
363   for (Function::const_arg_iterator II = OldFunc->arg_begin(), 
364        E = OldFunc->arg_end(); II != E; ++II)
365     assert(VMap.count(II) && "No mapping from source argument specified!");
366 #endif
367
368   PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
369                             Returns, NameSuffix, CodeInfo, TD);
370
371   // Clone the entry block, and anything recursively reachable from it.
372   std::vector<const BasicBlock*> CloneWorklist;
373   CloneWorklist.push_back(&OldFunc->getEntryBlock());
374   while (!CloneWorklist.empty()) {
375     const BasicBlock *BB = CloneWorklist.back();
376     CloneWorklist.pop_back();
377     PFC.CloneBlock(BB, CloneWorklist);
378   }
379   
380   // Loop over all of the basic blocks in the old function.  If the block was
381   // reachable, we have cloned it and the old block is now in the value map:
382   // insert it into the new function in the right order.  If not, ignore it.
383   //
384   // Defer PHI resolution until rest of function is resolved.
385   SmallVector<const PHINode*, 16> PHIToResolve;
386   for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
387        BI != BE; ++BI) {
388     Value *V = VMap[BI];
389     BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
390     if (NewBB == 0) continue;  // Dead block.
391
392     // Add the new block to the new function.
393     NewFunc->getBasicBlockList().push_back(NewBB);
394     
395     // Loop over all of the instructions in the block, fixing up operand
396     // references as we go.  This uses VMap to do all the hard work.
397     //
398     BasicBlock::iterator I = NewBB->begin();
399
400     DebugLoc TheCallDL;
401     if (TheCall) 
402       TheCallDL = TheCall->getDebugLoc();
403     
404     // Handle PHI nodes specially, as we have to remove references to dead
405     // blocks.
406     if (PHINode *PN = dyn_cast<PHINode>(I)) {
407       // Skip over all PHI nodes, remembering them for later.
408       BasicBlock::const_iterator OldI = BI->begin();
409       for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI)
410         PHIToResolve.push_back(cast<PHINode>(OldI));
411     }
412     
413     // Otherwise, remap the rest of the instructions normally.
414     for (; I != NewBB->end(); ++I)
415       RemapInstruction(I, VMap,
416                        ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
417   }
418   
419   // Defer PHI resolution until rest of function is resolved, PHI resolution
420   // requires the CFG to be up-to-date.
421   for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
422     const PHINode *OPN = PHIToResolve[phino];
423     unsigned NumPreds = OPN->getNumIncomingValues();
424     const BasicBlock *OldBB = OPN->getParent();
425     BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
426
427     // Map operands for blocks that are live and remove operands for blocks
428     // that are dead.
429     for (; phino != PHIToResolve.size() &&
430          PHIToResolve[phino]->getParent() == OldBB; ++phino) {
431       OPN = PHIToResolve[phino];
432       PHINode *PN = cast<PHINode>(VMap[OPN]);
433       for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
434         Value *V = VMap[PN->getIncomingBlock(pred)];
435         if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
436           Value *InVal = MapValue(PN->getIncomingValue(pred),
437                                   VMap, 
438                         ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
439           assert(InVal && "Unknown input value?");
440           PN->setIncomingValue(pred, InVal);
441           PN->setIncomingBlock(pred, MappedBlock);
442         } else {
443           PN->removeIncomingValue(pred, false);
444           --pred, --e;  // Revisit the next entry.
445         }
446       } 
447     }
448     
449     // The loop above has removed PHI entries for those blocks that are dead
450     // and has updated others.  However, if a block is live (i.e. copied over)
451     // but its terminator has been changed to not go to this block, then our
452     // phi nodes will have invalid entries.  Update the PHI nodes in this
453     // case.
454     PHINode *PN = cast<PHINode>(NewBB->begin());
455     NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
456     if (NumPreds != PN->getNumIncomingValues()) {
457       assert(NumPreds < PN->getNumIncomingValues());
458       // Count how many times each predecessor comes to this block.
459       std::map<BasicBlock*, unsigned> PredCount;
460       for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
461            PI != E; ++PI)
462         --PredCount[*PI];
463       
464       // Figure out how many entries to remove from each PHI.
465       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
466         ++PredCount[PN->getIncomingBlock(i)];
467       
468       // At this point, the excess predecessor entries are positive in the
469       // map.  Loop over all of the PHIs and remove excess predecessor
470       // entries.
471       BasicBlock::iterator I = NewBB->begin();
472       for (; (PN = dyn_cast<PHINode>(I)); ++I) {
473         for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(),
474              E = PredCount.end(); PCI != E; ++PCI) {
475           BasicBlock *Pred     = PCI->first;
476           for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove)
477             PN->removeIncomingValue(Pred, false);
478         }
479       }
480     }
481     
482     // If the loops above have made these phi nodes have 0 or 1 operand,
483     // replace them with undef or the input value.  We must do this for
484     // correctness, because 0-operand phis are not valid.
485     PN = cast<PHINode>(NewBB->begin());
486     if (PN->getNumIncomingValues() == 0) {
487       BasicBlock::iterator I = NewBB->begin();
488       BasicBlock::const_iterator OldI = OldBB->begin();
489       while ((PN = dyn_cast<PHINode>(I++))) {
490         Value *NV = UndefValue::get(PN->getType());
491         PN->replaceAllUsesWith(NV);
492         assert(VMap[OldI] == PN && "VMap mismatch");
493         VMap[OldI] = NV;
494         PN->eraseFromParent();
495         ++OldI;
496       }
497     }
498     // NOTE: We cannot eliminate single entry phi nodes here, because of
499     // VMap.  Single entry phi nodes can have multiple VMap entries
500     // pointing at them.  Thus, deleting one would require scanning the VMap
501     // to update any entries in it that would require that.  This would be
502     // really slow.
503   }
504   
505   // Now that the inlined function body has been fully constructed, go through
506   // and zap unconditional fall-through branches.  This happen all the time when
507   // specializing code: code specialization turns conditional branches into
508   // uncond branches, and this code folds them.
509   Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]);
510   while (I != NewFunc->end()) {
511     BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
512     if (!BI || BI->isConditional()) { ++I; continue; }
513     
514     // Note that we can't eliminate uncond branches if the destination has
515     // single-entry PHI nodes.  Eliminating the single-entry phi nodes would
516     // require scanning the VMap to update any entries that point to the phi
517     // node.
518     BasicBlock *Dest = BI->getSuccessor(0);
519     if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) {
520       ++I; continue;
521     }
522     
523     // We know all single-entry PHI nodes in the inlined function have been
524     // removed, so we just need to splice the blocks.
525     BI->eraseFromParent();
526     
527     // Make all PHI nodes that referred to Dest now refer to I as their source.
528     Dest->replaceAllUsesWith(I);
529
530     // Move all the instructions in the succ to the pred.
531     I->getInstList().splice(I->end(), Dest->getInstList());
532     
533     // Remove the dest block.
534     Dest->eraseFromParent();
535     
536     // Do not increment I, iteratively merge all things this block branches to.
537   }
538 }