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
CloneBasicBlock(const BasicBlock * BB,ValueToValueMapTy & VMap,const Twine & NameSuffix,Function * F,ClonedCodeInfo * CodeInfo)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 //
CloneFunctionInto(Function * NewFunc,const Function * OldFunc,ValueToValueMapTy & VMap,bool ModuleLevelChanges,SmallVectorImpl<ReturnInst * > & Returns,const char * NameSuffix,ClonedCodeInfo * CodeInfo)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 ///
CloneFunction(const Function * F,ValueToValueMapTy & VMap,bool ModuleLevelChanges,ClonedCodeInfo * CodeInfo)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:
PruningFunctionCloner__anona233b00a0111::PruningFunctionCloner189 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.
CloneBlock(const BasicBlock * BB,std::vector<const BasicBlock * > & ToClone)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::
ConstantFoldMappedInstruction(const Instruction * I)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, TD);
342 }
343
344 /// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
345 /// except that it does some simple constant prop and DCE on the fly. The
346 /// effect of this is to copy significantly less code in cases where (for
347 /// example) a function call with constant arguments is inlined, and those
348 /// constant arguments cause a significant amount of code in the callee to be
349 /// dead. Since this doesn't produce an exact copy of the input, it can't be
350 /// used for things like CloneFunction or CloneModule.
CloneAndPruneFunctionInto(Function * NewFunc,const Function * OldFunc,ValueToValueMapTy & VMap,bool ModuleLevelChanges,SmallVectorImpl<ReturnInst * > & Returns,const char * NameSuffix,ClonedCodeInfo * CodeInfo,const TargetData * TD,Instruction * TheCall)351 void llvm::CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc,
352 ValueToValueMapTy &VMap,
353 bool ModuleLevelChanges,
354 SmallVectorImpl<ReturnInst*> &Returns,
355 const char *NameSuffix,
356 ClonedCodeInfo *CodeInfo,
357 const TargetData *TD,
358 Instruction *TheCall) {
359 assert(NameSuffix && "NameSuffix cannot be null!");
360
361 #ifndef NDEBUG
362 for (Function::const_arg_iterator II = OldFunc->arg_begin(),
363 E = OldFunc->arg_end(); II != E; ++II)
364 assert(VMap.count(II) && "No mapping from source argument specified!");
365 #endif
366
367 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
368 Returns, NameSuffix, CodeInfo, TD);
369
370 // Clone the entry block, and anything recursively reachable from it.
371 std::vector<const BasicBlock*> CloneWorklist;
372 CloneWorklist.push_back(&OldFunc->getEntryBlock());
373 while (!CloneWorklist.empty()) {
374 const BasicBlock *BB = CloneWorklist.back();
375 CloneWorklist.pop_back();
376 PFC.CloneBlock(BB, CloneWorklist);
377 }
378
379 // Loop over all of the basic blocks in the old function. If the block was
380 // reachable, we have cloned it and the old block is now in the value map:
381 // insert it into the new function in the right order. If not, ignore it.
382 //
383 // Defer PHI resolution until rest of function is resolved.
384 SmallVector<const PHINode*, 16> PHIToResolve;
385 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
386 BI != BE; ++BI) {
387 Value *V = VMap[BI];
388 BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
389 if (NewBB == 0) continue; // Dead block.
390
391 // Add the new block to the new function.
392 NewFunc->getBasicBlockList().push_back(NewBB);
393
394 // Loop over all of the instructions in the block, fixing up operand
395 // references as we go. This uses VMap to do all the hard work.
396 //
397 BasicBlock::iterator I = NewBB->begin();
398
399 DebugLoc TheCallDL;
400 if (TheCall)
401 TheCallDL = TheCall->getDebugLoc();
402
403 // Handle PHI nodes specially, as we have to remove references to dead
404 // blocks.
405 if (PHINode *PN = dyn_cast<PHINode>(I)) {
406 // Skip over all PHI nodes, remembering them for later.
407 BasicBlock::const_iterator OldI = BI->begin();
408 for (; (PN = dyn_cast<PHINode>(I)); ++I, ++OldI)
409 PHIToResolve.push_back(cast<PHINode>(OldI));
410 }
411
412 // Otherwise, remap the rest of the instructions normally.
413 for (; I != NewBB->end(); ++I)
414 RemapInstruction(I, VMap,
415 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
416 }
417
418 // Defer PHI resolution until rest of function is resolved, PHI resolution
419 // requires the CFG to be up-to-date.
420 for (unsigned phino = 0, e = PHIToResolve.size(); phino != e; ) {
421 const PHINode *OPN = PHIToResolve[phino];
422 unsigned NumPreds = OPN->getNumIncomingValues();
423 const BasicBlock *OldBB = OPN->getParent();
424 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
425
426 // Map operands for blocks that are live and remove operands for blocks
427 // that are dead.
428 for (; phino != PHIToResolve.size() &&
429 PHIToResolve[phino]->getParent() == OldBB; ++phino) {
430 OPN = PHIToResolve[phino];
431 PHINode *PN = cast<PHINode>(VMap[OPN]);
432 for (unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
433 Value *V = VMap[PN->getIncomingBlock(pred)];
434 if (BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
435 Value *InVal = MapValue(PN->getIncomingValue(pred),
436 VMap,
437 ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
438 assert(InVal && "Unknown input value?");
439 PN->setIncomingValue(pred, InVal);
440 PN->setIncomingBlock(pred, MappedBlock);
441 } else {
442 PN->removeIncomingValue(pred, false);
443 --pred, --e; // Revisit the next entry.
444 }
445 }
446 }
447
448 // The loop above has removed PHI entries for those blocks that are dead
449 // and has updated others. However, if a block is live (i.e. copied over)
450 // but its terminator has been changed to not go to this block, then our
451 // phi nodes will have invalid entries. Update the PHI nodes in this
452 // case.
453 PHINode *PN = cast<PHINode>(NewBB->begin());
454 NumPreds = std::distance(pred_begin(NewBB), pred_end(NewBB));
455 if (NumPreds != PN->getNumIncomingValues()) {
456 assert(NumPreds < PN->getNumIncomingValues());
457 // Count how many times each predecessor comes to this block.
458 std::map<BasicBlock*, unsigned> PredCount;
459 for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB);
460 PI != E; ++PI)
461 --PredCount[*PI];
462
463 // Figure out how many entries to remove from each PHI.
464 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
465 ++PredCount[PN->getIncomingBlock(i)];
466
467 // At this point, the excess predecessor entries are positive in the
468 // map. Loop over all of the PHIs and remove excess predecessor
469 // entries.
470 BasicBlock::iterator I = NewBB->begin();
471 for (; (PN = dyn_cast<PHINode>(I)); ++I) {
472 for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(),
473 E = PredCount.end(); PCI != E; ++PCI) {
474 BasicBlock *Pred = PCI->first;
475 for (unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove)
476 PN->removeIncomingValue(Pred, false);
477 }
478 }
479 }
480
481 // If the loops above have made these phi nodes have 0 or 1 operand,
482 // replace them with undef or the input value. We must do this for
483 // correctness, because 0-operand phis are not valid.
484 PN = cast<PHINode>(NewBB->begin());
485 if (PN->getNumIncomingValues() == 0) {
486 BasicBlock::iterator I = NewBB->begin();
487 BasicBlock::const_iterator OldI = OldBB->begin();
488 while ((PN = dyn_cast<PHINode>(I++))) {
489 Value *NV = UndefValue::get(PN->getType());
490 PN->replaceAllUsesWith(NV);
491 assert(VMap[OldI] == PN && "VMap mismatch");
492 VMap[OldI] = NV;
493 PN->eraseFromParent();
494 ++OldI;
495 }
496 }
497 // NOTE: We cannot eliminate single entry phi nodes here, because of
498 // VMap. Single entry phi nodes can have multiple VMap entries
499 // pointing at them. Thus, deleting one would require scanning the VMap
500 // to update any entries in it that would require that. This would be
501 // really slow.
502 }
503
504 // Now that the inlined function body has been fully constructed, go through
505 // and zap unconditional fall-through branches. This happen all the time when
506 // specializing code: code specialization turns conditional branches into
507 // uncond branches, and this code folds them.
508 Function::iterator I = cast<BasicBlock>(VMap[&OldFunc->getEntryBlock()]);
509 while (I != NewFunc->end()) {
510 BranchInst *BI = dyn_cast<BranchInst>(I->getTerminator());
511 if (!BI || BI->isConditional()) { ++I; continue; }
512
513 // Note that we can't eliminate uncond branches if the destination has
514 // single-entry PHI nodes. Eliminating the single-entry phi nodes would
515 // require scanning the VMap to update any entries that point to the phi
516 // node.
517 BasicBlock *Dest = BI->getSuccessor(0);
518 if (!Dest->getSinglePredecessor() || isa<PHINode>(Dest->begin())) {
519 ++I; continue;
520 }
521
522 // We know all single-entry PHI nodes in the inlined function have been
523 // removed, so we just need to splice the blocks.
524 BI->eraseFromParent();
525
526 // Make all PHI nodes that referred to Dest now refer to I as their source.
527 Dest->replaceAllUsesWith(I);
528
529 // Move all the instructions in the succ to the pred.
530 I->getInstList().splice(I->end(), Dest->getInstList());
531
532 // Remove the dest block.
533 Dest->eraseFromParent();
534
535 // Do not increment I, iteratively merge all things this block branches to.
536 }
537 }
538