add some missing quotes in debug output
[llvm/avr.git] / lib / Transforms / Utils / InlineFunction.cpp
blob4b133fd5e89277f13f167b02469e3b6f09f14a1f
1 //===- InlineFunction.cpp - Code to perform function inlining -------------===//
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 inlining of a function into a call site, resolving
11 // parameters and the return value as appropriate.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/Cloning.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/Intrinsics.h"
23 #include "llvm/Attributes.h"
24 #include "llvm/Analysis/CallGraph.h"
25 #include "llvm/Analysis/DebugInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/CallSite.h"
30 using namespace llvm;
32 bool llvm::InlineFunction(CallInst *CI, CallGraph *CG, const TargetData *TD,
33 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
34 return InlineFunction(CallSite(CI), CG, TD, StaticAllocas);
36 bool llvm::InlineFunction(InvokeInst *II, CallGraph *CG, const TargetData *TD,
37 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
38 return InlineFunction(CallSite(II), CG, TD, StaticAllocas);
42 /// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
43 /// an invoke, we have to turn all of the calls that can throw into
44 /// invokes. This function analyze BB to see if there are any calls, and if so,
45 /// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI
46 /// nodes in that block with the values specified in InvokeDestPHIValues.
47 ///
48 static void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
49 BasicBlock *InvokeDest,
50 const SmallVectorImpl<Value*> &InvokeDestPHIValues) {
51 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
52 Instruction *I = BBI++;
54 // We only need to check for function calls: inlined invoke
55 // instructions require no special handling.
56 CallInst *CI = dyn_cast<CallInst>(I);
57 if (CI == 0) continue;
59 // If this call cannot unwind, don't convert it to an invoke.
60 if (CI->doesNotThrow())
61 continue;
63 // Convert this function call into an invoke instruction.
64 // First, split the basic block.
65 BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
67 // Next, create the new invoke instruction, inserting it at the end
68 // of the old basic block.
69 SmallVector<Value*, 8> InvokeArgs(CI->op_begin()+1, CI->op_end());
70 InvokeInst *II =
71 InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest,
72 InvokeArgs.begin(), InvokeArgs.end(),
73 CI->getName(), BB->getTerminator());
74 II->setCallingConv(CI->getCallingConv());
75 II->setAttributes(CI->getAttributes());
77 // Make sure that anything using the call now uses the invoke! This also
78 // updates the CallGraph if present.
79 CI->replaceAllUsesWith(II);
81 // Delete the unconditional branch inserted by splitBasicBlock
82 BB->getInstList().pop_back();
83 Split->getInstList().pop_front(); // Delete the original call
85 // Update any PHI nodes in the exceptional block to indicate that
86 // there is now a new entry in them.
87 unsigned i = 0;
88 for (BasicBlock::iterator I = InvokeDest->begin();
89 isa<PHINode>(I); ++I, ++i)
90 cast<PHINode>(I)->addIncoming(InvokeDestPHIValues[i], BB);
92 // This basic block is now complete, the caller will continue scanning the
93 // next one.
94 return;
99 /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
100 /// in the body of the inlined function into invokes and turn unwind
101 /// instructions into branches to the invoke unwind dest.
103 /// II is the invoke instruction being inlined. FirstNewBlock is the first
104 /// block of the inlined code (the last block is the end of the function),
105 /// and InlineCodeInfo is information about the code that got inlined.
106 static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
107 ClonedCodeInfo &InlinedCodeInfo) {
108 BasicBlock *InvokeDest = II->getUnwindDest();
109 SmallVector<Value*, 8> InvokeDestPHIValues;
111 // If there are PHI nodes in the unwind destination block, we need to
112 // keep track of which values came into them from this invoke, then remove
113 // the entry for this block.
114 BasicBlock *InvokeBlock = II->getParent();
115 for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) {
116 PHINode *PN = cast<PHINode>(I);
117 // Save the value to use for this edge.
118 InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock));
121 Function *Caller = FirstNewBlock->getParent();
123 // The inlined code is currently at the end of the function, scan from the
124 // start of the inlined code to its end, checking for stuff we need to
125 // rewrite. If the code doesn't have calls or unwinds, we know there is
126 // nothing to rewrite.
127 if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
128 // Now that everything is happy, we have one final detail. The PHI nodes in
129 // the exception destination block still have entries due to the original
130 // invoke instruction. Eliminate these entries (which might even delete the
131 // PHI node) now.
132 InvokeDest->removePredecessor(II->getParent());
133 return;
136 for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
137 if (InlinedCodeInfo.ContainsCalls)
138 HandleCallsInBlockInlinedThroughInvoke(BB, InvokeDest,
139 InvokeDestPHIValues);
141 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
142 // An UnwindInst requires special handling when it gets inlined into an
143 // invoke site. Once this happens, we know that the unwind would cause
144 // a control transfer to the invoke exception destination, so we can
145 // transform it into a direct branch to the exception destination.
146 BranchInst::Create(InvokeDest, UI);
148 // Delete the unwind instruction!
149 UI->eraseFromParent();
151 // Update any PHI nodes in the exceptional block to indicate that
152 // there is now a new entry in them.
153 unsigned i = 0;
154 for (BasicBlock::iterator I = InvokeDest->begin();
155 isa<PHINode>(I); ++I, ++i) {
156 PHINode *PN = cast<PHINode>(I);
157 PN->addIncoming(InvokeDestPHIValues[i], BB);
162 // Now that everything is happy, we have one final detail. The PHI nodes in
163 // the exception destination block still have entries due to the original
164 // invoke instruction. Eliminate these entries (which might even delete the
165 // PHI node) now.
166 InvokeDest->removePredecessor(II->getParent());
169 /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
170 /// into the caller, update the specified callgraph to reflect the changes we
171 /// made. Note that it's possible that not all code was copied over, so only
172 /// some edges of the callgraph may remain.
173 static void UpdateCallGraphAfterInlining(CallSite CS,
174 Function::iterator FirstNewBlock,
175 DenseMap<const Value*, Value*> &ValueMap,
176 CallGraph &CG) {
177 const Function *Caller = CS.getInstruction()->getParent()->getParent();
178 const Function *Callee = CS.getCalledFunction();
179 CallGraphNode *CalleeNode = CG[Callee];
180 CallGraphNode *CallerNode = CG[Caller];
182 // Since we inlined some uninlined call sites in the callee into the caller,
183 // add edges from the caller to all of the callees of the callee.
184 CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
186 // Consider the case where CalleeNode == CallerNode.
187 CallGraphNode::CalledFunctionsVector CallCache;
188 if (CalleeNode == CallerNode) {
189 CallCache.assign(I, E);
190 I = CallCache.begin();
191 E = CallCache.end();
194 for (; I != E; ++I) {
195 const Value *OrigCall = I->first;
197 DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
198 // Only copy the edge if the call was inlined!
199 if (VMI == ValueMap.end() || VMI->second == 0)
200 continue;
202 // If the call was inlined, but then constant folded, there is no edge to
203 // add. Check for this case.
204 if (Instruction *NewCall = dyn_cast<Instruction>(VMI->second))
205 CallerNode->addCalledFunction(CallSite::get(NewCall), I->second);
208 // Update the call graph by deleting the edge from Callee to Caller. We must
209 // do this after the loop above in case Caller and Callee are the same.
210 CallerNode->removeCallEdgeFor(CS);
213 /// findFnRegionEndMarker - This is a utility routine that is used by
214 /// InlineFunction. Return llvm.dbg.region.end intrinsic that corresponds
215 /// to the llvm.dbg.func.start of the function F. Otherwise return NULL.
217 static const DbgRegionEndInst *findFnRegionEndMarker(const Function *F) {
219 MDNode *FnStart = NULL;
220 const DbgRegionEndInst *FnEnd = NULL;
221 for (Function::const_iterator FI = F->begin(), FE =F->end(); FI != FE; ++FI)
222 for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end(); BI != BE;
223 ++BI) {
224 if (FnStart == NULL) {
225 if (const DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI)) {
226 DISubprogram SP(FSI->getSubprogram());
227 assert (SP.isNull() == false && "Invalid llvm.dbg.func.start");
228 if (SP.describes(F))
229 FnStart = SP.getNode();
231 continue;
234 if (const DbgRegionEndInst *REI = dyn_cast<DbgRegionEndInst>(BI))
235 if (REI->getContext() == FnStart)
236 FnEnd = REI;
238 return FnEnd;
241 // InlineFunction - This function inlines the called function into the basic
242 // block of the caller. This returns false if it is not possible to inline this
243 // call. The program is still in a well defined state if this occurs though.
245 // Note that this only does one level of inlining. For example, if the
246 // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
247 // exists in the instruction stream. Similiarly this will inline a recursive
248 // function by one level.
250 bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD,
251 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
252 Instruction *TheCall = CS.getInstruction();
253 LLVMContext &Context = TheCall->getContext();
254 assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
255 "Instruction not in function!");
257 const Function *CalledFunc = CS.getCalledFunction();
258 if (CalledFunc == 0 || // Can't inline external function or indirect
259 CalledFunc->isDeclaration() || // call, or call to a vararg function!
260 CalledFunc->getFunctionType()->isVarArg()) return false;
263 // If the call to the callee is not a tail call, we must clear the 'tail'
264 // flags on any calls that we inline.
265 bool MustClearTailCallFlags =
266 !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
268 // If the call to the callee cannot throw, set the 'nounwind' flag on any
269 // calls that we inline.
270 bool MarkNoUnwind = CS.doesNotThrow();
272 BasicBlock *OrigBB = TheCall->getParent();
273 Function *Caller = OrigBB->getParent();
275 // GC poses two hazards to inlining, which only occur when the callee has GC:
276 // 1. If the caller has no GC, then the callee's GC must be propagated to the
277 // caller.
278 // 2. If the caller has a differing GC, it is invalid to inline.
279 if (CalledFunc->hasGC()) {
280 if (!Caller->hasGC())
281 Caller->setGC(CalledFunc->getGC());
282 else if (CalledFunc->getGC() != Caller->getGC())
283 return false;
286 // Get an iterator to the last basic block in the function, which will have
287 // the new function inlined after it.
289 Function::iterator LastBlock = &Caller->back();
291 // Make sure to capture all of the return instructions from the cloned
292 // function.
293 SmallVector<ReturnInst*, 8> Returns;
294 ClonedCodeInfo InlinedFunctionInfo;
295 Function::iterator FirstNewBlock;
297 { // Scope to destroy ValueMap after cloning.
298 DenseMap<const Value*, Value*> ValueMap;
300 assert(CalledFunc->arg_size() == CS.arg_size() &&
301 "No varargs calls can be inlined!");
303 // Calculate the vector of arguments to pass into the function cloner, which
304 // matches up the formal to the actual argument values.
305 CallSite::arg_iterator AI = CS.arg_begin();
306 unsigned ArgNo = 0;
307 for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
308 E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
309 Value *ActualArg = *AI;
311 // When byval arguments actually inlined, we need to make the copy implied
312 // by them explicit. However, we don't do this if the callee is readonly
313 // or readnone, because the copy would be unneeded: the callee doesn't
314 // modify the struct.
315 if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
316 !CalledFunc->onlyReadsMemory()) {
317 const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
318 const Type *VoidPtrTy =
319 PointerType::getUnqual(Type::getInt8Ty(Context));
321 // Create the alloca. If we have TargetData, use nice alignment.
322 unsigned Align = 1;
323 if (TD) Align = TD->getPrefTypeAlignment(AggTy);
324 Value *NewAlloca = new AllocaInst(AggTy, 0, Align,
325 I->getName(),
326 &*Caller->begin()->begin());
327 // Emit a memcpy.
328 const Type *Tys[] = { Type::getInt64Ty(Context) };
329 Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
330 Intrinsic::memcpy,
331 Tys, 1);
332 Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
333 Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
335 Value *Size;
336 if (TD == 0)
337 Size = ConstantExpr::getSizeOf(AggTy);
338 else
339 Size = ConstantInt::get(Type::getInt64Ty(Context),
340 TD->getTypeStoreSize(AggTy));
342 // Always generate a memcpy of alignment 1 here because we don't know
343 // the alignment of the src pointer. Other optimizations can infer
344 // better alignment.
345 Value *CallArgs[] = {
346 DestCast, SrcCast, Size,
347 ConstantInt::get(Type::getInt32Ty(Context), 1)
349 CallInst *TheMemCpy =
350 CallInst::Create(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
352 // If we have a call graph, update it.
353 if (CG) {
354 CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn);
355 CallGraphNode *CallerNode = (*CG)[Caller];
356 CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN);
359 // Uses of the argument in the function should use our new alloca
360 // instead.
361 ActualArg = NewAlloca;
364 ValueMap[I] = ActualArg;
367 // Adjust llvm.dbg.region.end. If the CalledFunc has region end
368 // marker then clone that marker after next stop point at the
369 // call site. The function body cloner does not clone original
370 // region end marker from the CalledFunc. This will ensure that
371 // inlined function's scope ends at the right place.
372 if (const DbgRegionEndInst *DREI = findFnRegionEndMarker(CalledFunc)) {
373 for (BasicBlock::iterator BI = TheCall, BE = TheCall->getParent()->end();
374 BI != BE; ++BI) {
375 if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BI)) {
376 if (DbgRegionEndInst *NewDREI =
377 dyn_cast<DbgRegionEndInst>(DREI->clone(Context)))
378 NewDREI->insertAfter(DSPI);
379 break;
384 // We want the inliner to prune the code as it copies. We would LOVE to
385 // have no dead or constant instructions leftover after inlining occurs
386 // (which can happen, e.g., because an argument was constant), but we'll be
387 // happy with whatever the cloner can do.
388 CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i",
389 &InlinedFunctionInfo, TD);
391 // Remember the first block that is newly cloned over.
392 FirstNewBlock = LastBlock; ++FirstNewBlock;
394 // Update the callgraph if requested.
395 if (CG)
396 UpdateCallGraphAfterInlining(CS, FirstNewBlock, ValueMap, *CG);
399 // If there are any alloca instructions in the block that used to be the entry
400 // block for the callee, move them to the entry block of the caller. First
401 // calculate which instruction they should be inserted before. We insert the
402 // instructions at the end of the current alloca list.
405 BasicBlock::iterator InsertPoint = Caller->begin()->begin();
406 for (BasicBlock::iterator I = FirstNewBlock->begin(),
407 E = FirstNewBlock->end(); I != E; ) {
408 AllocaInst *AI = dyn_cast<AllocaInst>(I++);
409 if (AI == 0) continue;
411 // If the alloca is now dead, remove it. This often occurs due to code
412 // specialization.
413 if (AI->use_empty()) {
414 AI->eraseFromParent();
415 continue;
418 if (!isa<Constant>(AI->getArraySize()))
419 continue;
421 // Keep track of the static allocas that we inline into the caller if the
422 // StaticAllocas pointer is non-null.
423 if (StaticAllocas) StaticAllocas->push_back(AI);
425 // Scan for the block of allocas that we can move over, and move them
426 // all at once.
427 while (isa<AllocaInst>(I) &&
428 isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
429 if (StaticAllocas) StaticAllocas->push_back(cast<AllocaInst>(I));
430 ++I;
433 // Transfer all of the allocas over in a block. Using splice means
434 // that the instructions aren't removed from the symbol table, then
435 // reinserted.
436 Caller->getEntryBlock().getInstList().splice(InsertPoint,
437 FirstNewBlock->getInstList(),
438 AI, I);
442 // If the inlined code contained dynamic alloca instructions, wrap the inlined
443 // code with llvm.stacksave/llvm.stackrestore intrinsics.
444 if (InlinedFunctionInfo.ContainsDynamicAllocas) {
445 Module *M = Caller->getParent();
446 // Get the two intrinsics we care about.
447 Constant *StackSave, *StackRestore;
448 StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
449 StackRestore = Intrinsic::getDeclaration(M, Intrinsic::stackrestore);
451 // If we are preserving the callgraph, add edges to the stacksave/restore
452 // functions for the calls we insert.
453 CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0;
454 if (CG) {
455 // We know that StackSave/StackRestore are Function*'s, because they are
456 // intrinsics which must have the right types.
457 StackSaveCGN = CG->getOrInsertFunction(cast<Function>(StackSave));
458 StackRestoreCGN = CG->getOrInsertFunction(cast<Function>(StackRestore));
459 CallerNode = (*CG)[Caller];
462 // Insert the llvm.stacksave.
463 CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack",
464 FirstNewBlock->begin());
465 if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN);
467 // Insert a call to llvm.stackrestore before any return instructions in the
468 // inlined function.
469 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
470 CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]);
471 if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN);
474 // Count the number of StackRestore calls we insert.
475 unsigned NumStackRestores = Returns.size();
477 // If we are inlining an invoke instruction, insert restores before each
478 // unwind. These unwinds will be rewritten into branches later.
479 if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
480 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
481 BB != E; ++BB)
482 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
483 CallInst::Create(StackRestore, SavedPtr, "", UI);
484 ++NumStackRestores;
489 // If we are inlining tail call instruction through a call site that isn't
490 // marked 'tail', we must remove the tail marker for any calls in the inlined
491 // code. Also, calls inlined through a 'nounwind' call site should be marked
492 // 'nounwind'.
493 if (InlinedFunctionInfo.ContainsCalls &&
494 (MustClearTailCallFlags || MarkNoUnwind)) {
495 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
496 BB != E; ++BB)
497 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
498 if (CallInst *CI = dyn_cast<CallInst>(I)) {
499 if (MustClearTailCallFlags)
500 CI->setTailCall(false);
501 if (MarkNoUnwind)
502 CI->setDoesNotThrow();
506 // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
507 // instructions are unreachable.
508 if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
509 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
510 BB != E; ++BB) {
511 TerminatorInst *Term = BB->getTerminator();
512 if (isa<UnwindInst>(Term)) {
513 new UnreachableInst(Context, Term);
514 BB->getInstList().erase(Term);
518 // If we are inlining for an invoke instruction, we must make sure to rewrite
519 // any inlined 'unwind' instructions into branches to the invoke exception
520 // destination, and call instructions into invoke instructions.
521 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
522 HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
524 // If we cloned in _exactly one_ basic block, and if that block ends in a
525 // return instruction, we splice the body of the inlined callee directly into
526 // the calling basic block.
527 if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
528 // Move all of the instructions right before the call.
529 OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
530 FirstNewBlock->begin(), FirstNewBlock->end());
531 // Remove the cloned basic block.
532 Caller->getBasicBlockList().pop_back();
534 // If the call site was an invoke instruction, add a branch to the normal
535 // destination.
536 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
537 BranchInst::Create(II->getNormalDest(), TheCall);
539 // If the return instruction returned a value, replace uses of the call with
540 // uses of the returned value.
541 if (!TheCall->use_empty()) {
542 ReturnInst *R = Returns[0];
543 if (TheCall == R->getReturnValue())
544 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
545 else
546 TheCall->replaceAllUsesWith(R->getReturnValue());
548 // Since we are now done with the Call/Invoke, we can delete it.
549 TheCall->eraseFromParent();
551 // Since we are now done with the return instruction, delete it also.
552 Returns[0]->eraseFromParent();
554 // We are now done with the inlining.
555 return true;
558 // Otherwise, we have the normal case, of more than one block to inline or
559 // multiple return sites.
561 // We want to clone the entire callee function into the hole between the
562 // "starter" and "ender" blocks. How we accomplish this depends on whether
563 // this is an invoke instruction or a call instruction.
564 BasicBlock *AfterCallBB;
565 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
567 // Add an unconditional branch to make this look like the CallInst case...
568 BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
570 // Split the basic block. This guarantees that no PHI nodes will have to be
571 // updated due to new incoming edges, and make the invoke case more
572 // symmetric to the call case.
573 AfterCallBB = OrigBB->splitBasicBlock(NewBr,
574 CalledFunc->getName()+".exit");
576 } else { // It's a call
577 // If this is a call instruction, we need to split the basic block that
578 // the call lives in.
580 AfterCallBB = OrigBB->splitBasicBlock(TheCall,
581 CalledFunc->getName()+".exit");
584 // Change the branch that used to go to AfterCallBB to branch to the first
585 // basic block of the inlined function.
587 TerminatorInst *Br = OrigBB->getTerminator();
588 assert(Br && Br->getOpcode() == Instruction::Br &&
589 "splitBasicBlock broken!");
590 Br->setOperand(0, FirstNewBlock);
593 // Now that the function is correct, make it a little bit nicer. In
594 // particular, move the basic blocks inserted from the end of the function
595 // into the space made by splitting the source basic block.
596 Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
597 FirstNewBlock, Caller->end());
599 // Handle all of the return instructions that we just cloned in, and eliminate
600 // any users of the original call/invoke instruction.
601 const Type *RTy = CalledFunc->getReturnType();
603 if (Returns.size() > 1) {
604 // The PHI node should go at the front of the new basic block to merge all
605 // possible incoming values.
606 PHINode *PHI = 0;
607 if (!TheCall->use_empty()) {
608 PHI = PHINode::Create(RTy, TheCall->getName(),
609 AfterCallBB->begin());
610 // Anything that used the result of the function call should now use the
611 // PHI node as their operand.
612 TheCall->replaceAllUsesWith(PHI);
615 // Loop over all of the return instructions adding entries to the PHI node
616 // as appropriate.
617 if (PHI) {
618 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
619 ReturnInst *RI = Returns[i];
620 assert(RI->getReturnValue()->getType() == PHI->getType() &&
621 "Ret value not consistent in function!");
622 PHI->addIncoming(RI->getReturnValue(), RI->getParent());
626 // Add a branch to the merge points and remove return instructions.
627 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
628 ReturnInst *RI = Returns[i];
629 BranchInst::Create(AfterCallBB, RI);
630 RI->eraseFromParent();
632 } else if (!Returns.empty()) {
633 // Otherwise, if there is exactly one return value, just replace anything
634 // using the return value of the call with the computed value.
635 if (!TheCall->use_empty()) {
636 if (TheCall == Returns[0]->getReturnValue())
637 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
638 else
639 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
642 // Splice the code from the return block into the block that it will return
643 // to, which contains the code that was after the call.
644 BasicBlock *ReturnBB = Returns[0]->getParent();
645 AfterCallBB->getInstList().splice(AfterCallBB->begin(),
646 ReturnBB->getInstList());
648 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
649 ReturnBB->replaceAllUsesWith(AfterCallBB);
651 // Delete the return instruction now and empty ReturnBB now.
652 Returns[0]->eraseFromParent();
653 ReturnBB->eraseFromParent();
654 } else if (!TheCall->use_empty()) {
655 // No returns, but something is using the return value of the call. Just
656 // nuke the result.
657 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
660 // Since we are now done with the Call/Invoke, we can delete it.
661 TheCall->eraseFromParent();
663 // We should always be able to fold the entry block of the function into the
664 // single predecessor of the block...
665 assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
666 BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
668 // Splice the code entry block into calling block, right before the
669 // unconditional branch.
670 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
671 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
673 // Remove the unconditional branch.
674 OrigBB->getInstList().erase(Br);
676 // Now we can remove the CalleeEntry block, which is now empty.
677 Caller->getBasicBlockList().erase(CalleeEntry);
679 return true;