blob: 7004248f772f0873cfba80b1d8d660dfed3f252f [file] [log] [blame]
Chris Lattnerca398dc2003-05-29 15:11:31 +00001//===- InlineFunction.cpp - Code to perform function inlining -------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerca398dc2003-05-29 15:11:31 +00009//
10// This file implements inlining of a function into a call site, resolving
11// parameters and the return value as appropriate.
12//
Chris Lattnerca398dc2003-05-29 15:11:31 +000013//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner3787e762004-10-17 23:21:07 +000016#include "llvm/Constants.h"
Chris Lattner7152c232003-08-24 04:06:56 +000017#include "llvm/DerivedTypes.h"
Owen Anderson0a205a42009-07-05 22:41:43 +000018#include "llvm/LLVMContext.h"
Chris Lattnerca398dc2003-05-29 15:11:31 +000019#include "llvm/Module.h"
Chris Lattner80a38d22003-08-24 06:59:16 +000020#include "llvm/Instructions.h"
Devang Patel517576d2009-04-15 00:17:06 +000021#include "llvm/IntrinsicInst.h"
Chris Lattner80a38d22003-08-24 06:59:16 +000022#include "llvm/Intrinsics.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000023#include "llvm/Attributes.h"
Chris Lattner468fb1d2006-01-14 20:07:50 +000024#include "llvm/Analysis/CallGraph.h"
Devang Patel517576d2009-04-15 00:17:06 +000025#include "llvm/Analysis/DebugInfo.h"
Chris Lattnerc93adca2008-01-11 06:09:30 +000026#include "llvm/Target/TargetData.h"
Chris Lattner93e985f2007-02-13 02:10:56 +000027#include "llvm/ADT/SmallVector.h"
Devang Patel641ca932008-03-10 18:22:16 +000028#include "llvm/ADT/StringExtras.h"
Chris Lattner80a38d22003-08-24 06:59:16 +000029#include "llvm/Support/CallSite.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000030using namespace llvm;
Chris Lattnerca398dc2003-05-29 15:11:31 +000031
Chris Lattner8f2718f2009-08-27 04:20:52 +000032bool llvm::InlineFunction(CallInst *CI, CallGraph *CG, const TargetData *TD,
33 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
34 return InlineFunction(CallSite(CI), CG, TD, StaticAllocas);
Chris Lattner468fb1d2006-01-14 20:07:50 +000035}
Chris Lattner8f2718f2009-08-27 04:20:52 +000036bool llvm::InlineFunction(InvokeInst *II, CallGraph *CG, const TargetData *TD,
37 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
38 return InlineFunction(CallSite(II), CG, TD, StaticAllocas);
Chris Lattner468fb1d2006-01-14 20:07:50 +000039}
Chris Lattner80a38d22003-08-24 06:59:16 +000040
Chris Lattner135755d2009-08-27 03:51:50 +000041
42/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
43/// an invoke, we have to check all of 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
Chris Lattner81dfb382009-09-01 18:44:06 +000046/// nodes in that block with the values specified in InvokeDestPHIValues.
Chris Lattner135755d2009-08-27 03:51:50 +000047///
48static void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
49 BasicBlock *InvokeDest,
Chris Lattner81dfb382009-09-01 18:44:06 +000050 const SmallVectorImpl<Value*> &InvokeDestPHIValues) {
Chris Lattner135755d2009-08-27 03:51:50 +000051 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
52 Instruction *I = BBI++;
53
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;
58
59 // If this call cannot unwind, don't convert it to an invoke.
60 if (CI->doesNotThrow())
61 continue;
62
63 // Convert this function call into an invoke instruction.
64 // First, split the basic block.
65 BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
66
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());
76
Chris Lattner81dfb382009-09-01 18:44:06 +000077 // Make sure that anything using the call now uses the invoke! This also
78 // updates the CallGraph if present.
Chris Lattner135755d2009-08-27 03:51:50 +000079 CI->replaceAllUsesWith(II);
80
Chris Lattner135755d2009-08-27 03:51:50 +000081 // Delete the unconditional branch inserted by splitBasicBlock
82 BB->getInstList().pop_back();
83 Split->getInstList().pop_front(); // Delete the original call
84
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);
91
92 // This basic block is now complete, the caller will continue scanning the
93 // next one.
94 return;
95 }
96}
97
98
Chris Lattnercd4d3392006-01-13 19:05:59 +000099/// 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.
102///
Nick Lewyckydac5c4b2009-02-03 04:34:40 +0000103/// II is the invoke instruction being inlined. FirstNewBlock is the first
Chris Lattnercd4d3392006-01-13 19:05:59 +0000104/// 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.
106static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
Chris Lattner81dfb382009-09-01 18:44:06 +0000107 ClonedCodeInfo &InlinedCodeInfo) {
Chris Lattnercd4d3392006-01-13 19:05:59 +0000108 BasicBlock *InvokeDest = II->getUnwindDest();
Chris Lattner135755d2009-08-27 03:51:50 +0000109 SmallVector<Value*, 8> InvokeDestPHIValues;
Chris Lattnercd4d3392006-01-13 19:05:59 +0000110
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));
119 }
120
121 Function *Caller = FirstNewBlock->getParent();
Duncan Sandsa7212e52008-09-05 12:37:12 +0000122
Chris Lattnercd4d3392006-01-13 19:05:59 +0000123 // 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
Chris Lattner135755d2009-08-27 03:51:50 +0000125 // 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;
134 }
135
Chris Lattner135755d2009-08-27 03:51:50 +0000136 for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
137 if (InlinedCodeInfo.ContainsCalls)
138 HandleCallsInBlockInlinedThroughInvoke(BB, InvokeDest,
Chris Lattner81dfb382009-09-01 18:44:06 +0000139 InvokeDestPHIValues);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000140
Chris Lattner135755d2009-08-27 03:51:50 +0000141 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);
Chris Lattnercd4d3392006-01-13 19:05:59 +0000147
Chris Lattner135755d2009-08-27 03:51:50 +0000148 // Delete the unwind instruction!
149 UI->eraseFromParent();
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000150
Chris Lattner135755d2009-08-27 03:51:50 +0000151 // 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);
Chris Lattnercd4d3392006-01-13 19:05:59 +0000158 }
159 }
160 }
161
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());
167}
168
Chris Lattnerd85340f2006-07-12 18:29:36 +0000169/// 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
Duncan Sandsd7b98512008-09-08 11:05:51 +0000172/// some edges of the callgraph may remain.
173static void UpdateCallGraphAfterInlining(CallSite CS,
Chris Lattnerd85340f2006-07-12 18:29:36 +0000174 Function::iterator FirstNewBlock,
Chris Lattner5e665f52007-02-03 00:08:31 +0000175 DenseMap<const Value*, Value*> &ValueMap,
Chris Lattner468fb1d2006-01-14 20:07:50 +0000176 CallGraph &CG) {
Duncan Sandsd7b98512008-09-08 11:05:51 +0000177 const Function *Caller = CS.getInstruction()->getParent()->getParent();
178 const Function *Callee = CS.getCalledFunction();
Chris Lattner468fb1d2006-01-14 20:07:50 +0000179 CallGraphNode *CalleeNode = CG[Callee];
180 CallGraphNode *CallerNode = CG[Caller];
Duncan Sandsa7212e52008-09-05 12:37:12 +0000181
Chris Lattnerd85340f2006-07-12 18:29:36 +0000182 // Since we inlined some uninlined call sites in the callee into the caller,
Chris Lattner468fb1d2006-01-14 20:07:50 +0000183 // add edges from the caller to all of the callees of the callee.
Gabor Greifc478e522009-01-15 18:40:09 +0000184 CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
185
186 // Consider the case where CalleeNode == CallerNode.
Gabor Greif12532982009-01-17 00:09:08 +0000187 CallGraphNode::CalledFunctionsVector CallCache;
Gabor Greifc478e522009-01-15 18:40:09 +0000188 if (CalleeNode == CallerNode) {
189 CallCache.assign(I, E);
190 I = CallCache.begin();
191 E = CallCache.end();
192 }
193
194 for (; I != E; ++I) {
Chris Lattnera541b0f2009-09-01 06:31:31 +0000195 const Value *OrigCall = I->first;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000196
Chris Lattner5e665f52007-02-03 00:08:31 +0000197 DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
Chris Lattner981418b2006-07-12 21:37:11 +0000198 // Only copy the edge if the call was inlined!
Chris Lattner135755d2009-08-27 03:51:50 +0000199 if (VMI == ValueMap.end() || VMI->second == 0)
200 continue;
201
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);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000206 }
Chris Lattner135755d2009-08-27 03:51:50 +0000207
Dale Johannesen39fa3242009-01-13 22:43:37 +0000208 // 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);
Chris Lattner468fb1d2006-01-14 20:07:50 +0000211}
212
Devang Patel517576d2009-04-15 00:17:06 +0000213/// 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.
Chris Lattner135755d2009-08-27 03:51:50 +0000216///
Devang Patel517576d2009-04-15 00:17:06 +0000217static const DbgRegionEndInst *findFnRegionEndMarker(const Function *F) {
218
Devang Patele4b27562009-08-28 23:24:31 +0000219 MDNode *FnStart = NULL;
Devang Patel517576d2009-04-15 00:17:06 +0000220 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)) {
Devang Patele4b27562009-08-28 23:24:31 +0000226 DISubprogram SP(FSI->getSubprogram());
Devang Patel517576d2009-04-15 00:17:06 +0000227 assert (SP.isNull() == false && "Invalid llvm.dbg.func.start");
228 if (SP.describes(F))
Devang Patele4b27562009-08-28 23:24:31 +0000229 FnStart = SP.getNode();
Devang Patel517576d2009-04-15 00:17:06 +0000230 }
Chris Lattner135755d2009-08-27 03:51:50 +0000231 continue;
Devang Patel517576d2009-04-15 00:17:06 +0000232 }
Chris Lattner135755d2009-08-27 03:51:50 +0000233
234 if (const DbgRegionEndInst *REI = dyn_cast<DbgRegionEndInst>(BI))
235 if (REI->getContext() == FnStart)
236 FnEnd = REI;
Devang Patel517576d2009-04-15 00:17:06 +0000237 }
238 return FnEnd;
239}
Chris Lattnercd4d3392006-01-13 19:05:59 +0000240
Chris Lattnerca398dc2003-05-29 15:11:31 +0000241// 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.
244//
Misha Brukmanfd939082005-04-21 23:48:37 +0000245// 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
Chris Lattnerca398dc2003-05-29 15:11:31 +0000247// exists in the instruction stream. Similiarly this will inline a recursive
248// function by one level.
249//
Chris Lattner8f2718f2009-08-27 04:20:52 +0000250bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD,
251 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
Chris Lattner80a38d22003-08-24 06:59:16 +0000252 Instruction *TheCall = CS.getInstruction();
Owen Andersone922c022009-07-22 00:24:57 +0000253 LLVMContext &Context = TheCall->getContext();
Chris Lattner80a38d22003-08-24 06:59:16 +0000254 assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
255 "Instruction not in function!");
Chris Lattnerca398dc2003-05-29 15:11:31 +0000256
Chris Lattner80a38d22003-08-24 06:59:16 +0000257 const Function *CalledFunc = CS.getCalledFunction();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000258 if (CalledFunc == 0 || // Can't inline external function or indirect
Reid Spencer5cbf9852007-01-30 20:08:39 +0000259 CalledFunc->isDeclaration() || // call, or call to a vararg function!
Chris Lattnerca398dc2003-05-29 15:11:31 +0000260 CalledFunc->getFunctionType()->isVarArg()) return false;
261
Chris Lattner1b491412005-05-06 06:47:52 +0000262
Chris Lattneraf9985c2009-02-12 07:06:42 +0000263 // If the call to the callee is not a tail call, we must clear the 'tail'
Chris Lattner1b491412005-05-06 06:47:52 +0000264 // flags on any calls that we inline.
265 bool MustClearTailCallFlags =
Chris Lattneraf9985c2009-02-12 07:06:42 +0000266 !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
Chris Lattner1b491412005-05-06 06:47:52 +0000267
Duncan Sandsf0c33542007-12-19 21:13:37 +0000268 // If the call to the callee cannot throw, set the 'nounwind' flag on any
269 // calls that we inline.
270 bool MarkNoUnwind = CS.doesNotThrow();
271
Chris Lattner80a38d22003-08-24 06:59:16 +0000272 BasicBlock *OrigBB = TheCall->getParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000273 Function *Caller = OrigBB->getParent();
274
Gordon Henriksen0e138212007-12-25 03:10:07 +0000275 // 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.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000279 if (CalledFunc->hasGC()) {
280 if (!Caller->hasGC())
281 Caller->setGC(CalledFunc->getGC());
282 else if (CalledFunc->getGC() != Caller->getGC())
Gordon Henriksen0e138212007-12-25 03:10:07 +0000283 return false;
284 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000285
Chris Lattner5052c912004-02-04 01:41:09 +0000286 // Get an iterator to the last basic block in the function, which will have
287 // the new function inlined after it.
288 //
289 Function::iterator LastBlock = &Caller->back();
290
Chris Lattner5e923de2004-02-04 02:51:48 +0000291 // Make sure to capture all of the return instructions from the cloned
Chris Lattnerca398dc2003-05-29 15:11:31 +0000292 // function.
Chris Lattnerec1bea02009-08-27 04:02:30 +0000293 SmallVector<ReturnInst*, 8> Returns;
Chris Lattnercd4d3392006-01-13 19:05:59 +0000294 ClonedCodeInfo InlinedFunctionInfo;
Dale Johannesen0744f092009-03-04 02:09:48 +0000295 Function::iterator FirstNewBlock;
Duncan Sandsf0c33542007-12-19 21:13:37 +0000296
Chris Lattner5e923de2004-02-04 02:51:48 +0000297 { // Scope to destroy ValueMap after cloning.
Chris Lattner5e665f52007-02-03 00:08:31 +0000298 DenseMap<const Value*, Value*> ValueMap;
Chris Lattner5b5bc302006-05-27 01:28:04 +0000299
Dan Gohman9614fcc2008-06-20 17:11:32 +0000300 assert(CalledFunc->arg_size() == CS.arg_size() &&
Chris Lattner5e923de2004-02-04 02:51:48 +0000301 "No varargs calls can be inlined!");
Duncan Sandsa7212e52008-09-05 12:37:12 +0000302
Chris Lattnerc93adca2008-01-11 06:09:30 +0000303 // Calculate the vector of arguments to pass into the function cloner, which
304 // matches up the formal to the actual argument values.
Chris Lattner5e923de2004-02-04 02:51:48 +0000305 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattnerc93adca2008-01-11 06:09:30 +0000306 unsigned ArgNo = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +0000307 for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
Chris Lattnerc93adca2008-01-11 06:09:30 +0000308 E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
309 Value *ActualArg = *AI;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000310
Duncan Sandsd82375c2008-01-27 18:12:58 +0000311 // 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.
Devang Patel05988662008-09-25 21:00:45 +0000315 if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
Duncan Sandsd82375c2008-01-27 18:12:58 +0000316 !CalledFunc->onlyReadsMemory()) {
Chris Lattnerc93adca2008-01-11 06:09:30 +0000317 const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
Owen Anderson1d0be152009-08-13 21:58:54 +0000318 const Type *VoidPtrTy =
319 PointerType::getUnqual(Type::getInt8Ty(Context));
Duncan Sandsa7212e52008-09-05 12:37:12 +0000320
Chris Lattnerc93adca2008-01-11 06:09:30 +0000321 // Create the alloca. If we have TargetData, use nice alignment.
322 unsigned Align = 1;
323 if (TD) Align = TD->getPrefTypeAlignment(AggTy);
Owen Anderson50dead02009-07-15 23:53:25 +0000324 Value *NewAlloca = new AllocaInst(AggTy, 0, Align,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000325 I->getName(),
326 &*Caller->begin()->begin());
Chris Lattnerc93adca2008-01-11 06:09:30 +0000327 // Emit a memcpy.
Owen Anderson1d0be152009-08-13 21:58:54 +0000328 const Type *Tys[] = { Type::getInt64Ty(Context) };
Chris Lattnerc93adca2008-01-11 06:09:30 +0000329 Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
Chris Lattner824b9582008-11-21 16:42:48 +0000330 Intrinsic::memcpy,
331 Tys, 1);
Chris Lattnerc93adca2008-01-11 06:09:30 +0000332 Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
333 Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000334
Chris Lattnerc93adca2008-01-11 06:09:30 +0000335 Value *Size;
336 if (TD == 0)
Owen Andersonbaf3c402009-07-29 18:55:55 +0000337 Size = ConstantExpr::getSizeOf(AggTy);
Chris Lattnerc93adca2008-01-11 06:09:30 +0000338 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000339 Size = ConstantInt::get(Type::getInt64Ty(Context),
Owen Anderson0a205a42009-07-05 22:41:43 +0000340 TD->getTypeStoreSize(AggTy));
Duncan Sandsa7212e52008-09-05 12:37:12 +0000341
Chris Lattnerc93adca2008-01-11 06:09:30 +0000342 // 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[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000346 DestCast, SrcCast, Size,
347 ConstantInt::get(Type::getInt32Ty(Context), 1)
Chris Lattnerc93adca2008-01-11 06:09:30 +0000348 };
349 CallInst *TheMemCpy =
Gabor Greif051a9502008-04-06 20:25:17 +0000350 CallInst::Create(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000351
Chris Lattnerc93adca2008-01-11 06:09:30 +0000352 // 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);
357 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000358
Chris Lattnerc93adca2008-01-11 06:09:30 +0000359 // Uses of the argument in the function should use our new alloca
360 // instead.
361 ActualArg = NewAlloca;
362 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000363
Chris Lattnerc93adca2008-01-11 06:09:30 +0000364 ValueMap[I] = ActualArg;
365 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000366
Devang Patel517576d2009-04-15 00:17:06 +0000367 // 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.
Chris Lattner135755d2009-08-27 03:51:50 +0000372 if (const DbgRegionEndInst *DREI = findFnRegionEndMarker(CalledFunc)) {
373 for (BasicBlock::iterator BI = TheCall, BE = TheCall->getParent()->end();
374 BI != BE; ++BI) {
Devang Patel517576d2009-04-15 00:17:06 +0000375 if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BI)) {
376 if (DbgRegionEndInst *NewDREI =
Chris Lattner135755d2009-08-27 03:51:50 +0000377 dyn_cast<DbgRegionEndInst>(DREI->clone(Context)))
Devang Patel517576d2009-04-15 00:17:06 +0000378 NewDREI->insertAfter(DSPI);
379 break;
380 }
381 }
382 }
383
Chris Lattner5b5bc302006-05-27 01:28:04 +0000384 // 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",
Chris Lattner1dfdf822007-01-30 23:22:39 +0000389 &InlinedFunctionInfo, TD);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000390
Chris Lattnerd85340f2006-07-12 18:29:36 +0000391 // Remember the first block that is newly cloned over.
392 FirstNewBlock = LastBlock; ++FirstNewBlock;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000393
Chris Lattnerd85340f2006-07-12 18:29:36 +0000394 // Update the callgraph if requested.
395 if (CG)
Duncan Sandsd7b98512008-09-08 11:05:51 +0000396 UpdateCallGraphAfterInlining(CS, FirstNewBlock, ValueMap, *CG);
Misha Brukmanfd939082005-04-21 23:48:37 +0000397 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000398
Chris Lattnerca398dc2003-05-29 15:11:31 +0000399 // 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.
403 //
Chris Lattner21f20552006-01-13 18:16:48 +0000404 {
Chris Lattner80a38d22003-08-24 06:59:16 +0000405 BasicBlock::iterator InsertPoint = Caller->begin()->begin();
Chris Lattner5e923de2004-02-04 02:51:48 +0000406 for (BasicBlock::iterator I = FirstNewBlock->begin(),
Chris Lattner135755d2009-08-27 03:51:50 +0000407 E = FirstNewBlock->end(); I != E; ) {
408 AllocaInst *AI = dyn_cast<AllocaInst>(I++);
409 if (AI == 0) continue;
410
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;
Chris Lattner33bb3c82006-09-13 19:23:57 +0000416 }
Chris Lattner135755d2009-08-27 03:51:50 +0000417
418 if (!isa<Constant>(AI->getArraySize()))
419 continue;
420
Chris Lattner8f2718f2009-08-27 04:20:52 +0000421 // 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);
424
Chris Lattner135755d2009-08-27 03:51:50 +0000425 // Scan for the block of allocas that we can move over, and move them
426 // all at once.
427 while (isa<AllocaInst>(I) &&
Chris Lattner8f2718f2009-08-27 04:20:52 +0000428 isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
429 if (StaticAllocas) StaticAllocas->push_back(cast<AllocaInst>(I));
Chris Lattner135755d2009-08-27 03:51:50 +0000430 ++I;
Chris Lattner8f2718f2009-08-27 04:20:52 +0000431 }
Chris Lattner135755d2009-08-27 03:51:50 +0000432
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);
439 }
Chris Lattner80a38d22003-08-24 06:59:16 +0000440 }
Chris Lattnerca398dc2003-05-29 15:11:31 +0000441
Chris Lattnerbf229f42006-01-13 19:34:14 +0000442 // 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();
Chris Lattnerbf229f42006-01-13 19:34:14 +0000446 // Get the two intrinsics we care about.
Chris Lattnera121fdd2007-01-07 07:54:34 +0000447 Constant *StackSave, *StackRestore;
Duncan Sands3d292ac2008-04-07 13:43:58 +0000448 StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
449 StackRestore = Intrinsic::getDeclaration(M, Intrinsic::stackrestore);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000450
451 // If we are preserving the callgraph, add edges to the stacksave/restore
452 // functions for the calls we insert.
Chris Lattner21ba23d2006-07-18 21:48:57 +0000453 CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0;
Chris Lattnerd85340f2006-07-12 18:29:36 +0000454 if (CG) {
Chris Lattnera121fdd2007-01-07 07:54:34 +0000455 // 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));
Chris Lattnerd85340f2006-07-12 18:29:36 +0000459 CallerNode = (*CG)[Caller];
460 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000461
Chris Lattnerbf229f42006-01-13 19:34:14 +0000462 // Insert the llvm.stacksave.
Duncan Sandsa7212e52008-09-05 12:37:12 +0000463 CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack",
Gabor Greif051a9502008-04-06 20:25:17 +0000464 FirstNewBlock->begin());
Chris Lattnerd85340f2006-07-12 18:29:36 +0000465 if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000466
Chris Lattnerbf229f42006-01-13 19:34:14 +0000467 // Insert a call to llvm.stackrestore before any return instructions in the
468 // inlined function.
Chris Lattnerd85340f2006-07-12 18:29:36 +0000469 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
Gabor Greif051a9502008-04-06 20:25:17 +0000470 CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000471 if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN);
472 }
Chris Lattner468fb1d2006-01-14 20:07:50 +0000473
474 // Count the number of StackRestore calls we insert.
475 unsigned NumStackRestores = Returns.size();
Duncan Sandsa7212e52008-09-05 12:37:12 +0000476
Chris Lattnerbf229f42006-01-13 19:34:14 +0000477 // 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)
Chris Lattner468fb1d2006-01-14 20:07:50 +0000482 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
Duncan Sands3d292ac2008-04-07 13:43:58 +0000483 CallInst::Create(StackRestore, SavedPtr, "", UI);
Chris Lattner468fb1d2006-01-14 20:07:50 +0000484 ++NumStackRestores;
485 }
486 }
Chris Lattnerbf229f42006-01-13 19:34:14 +0000487 }
488
Duncan Sandsa7212e52008-09-05 12:37:12 +0000489 // If we are inlining tail call instruction through a call site that isn't
Chris Lattner1fdf4a82006-01-13 19:18:11 +0000490 // marked 'tail', we must remove the tail marker for any calls in the inlined
Duncan Sandsf0c33542007-12-19 21:13:37 +0000491 // code. Also, calls inlined through a 'nounwind' call site should be marked
492 // 'nounwind'.
493 if (InlinedFunctionInfo.ContainsCalls &&
494 (MustClearTailCallFlags || MarkNoUnwind)) {
Chris Lattner1b491412005-05-06 06:47:52 +0000495 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)
Duncan Sandsf0c33542007-12-19 21:13:37 +0000498 if (CallInst *CI = dyn_cast<CallInst>(I)) {
499 if (MustClearTailCallFlags)
500 CI->setTailCall(false);
501 if (MarkNoUnwind)
502 CI->setDoesNotThrow();
503 }
Chris Lattner1b491412005-05-06 06:47:52 +0000504 }
505
Duncan Sandsf0c33542007-12-19 21:13:37 +0000506 // 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)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000513 new UnreachableInst(Context, Term);
Duncan Sandsf0c33542007-12-19 21:13:37 +0000514 BB->getInstList().erase(Term);
515 }
516 }
517
Chris Lattner5e923de2004-02-04 02:51:48 +0000518 // 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.
Chris Lattnercd4d3392006-01-13 19:05:59 +0000521 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
Chris Lattner81dfb382009-09-01 18:44:06 +0000522 HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
Chris Lattner5e923de2004-02-04 02:51:48 +0000523
Chris Lattner44a68072004-02-04 04:17:06 +0000524 // 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();
Misha Brukmanfd939082005-04-21 23:48:37 +0000533
Chris Lattner44a68072004-02-04 04:17:06 +0000534 // If the call site was an invoke instruction, add a branch to the normal
535 // destination.
536 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
Gabor Greif051a9502008-04-06 20:25:17 +0000537 BranchInst::Create(II->getNormalDest(), TheCall);
Chris Lattner44a68072004-02-04 04:17:06 +0000538
539 // If the return instruction returned a value, replace uses of the call with
540 // uses of the returned value.
Devang Pateldc00d422008-03-04 21:15:15 +0000541 if (!TheCall->use_empty()) {
542 ReturnInst *R = Returns[0];
Eli Friedman5877ad72009-05-08 00:22:04 +0000543 if (TheCall == R->getReturnValue())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000544 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Eli Friedman5877ad72009-05-08 00:22:04 +0000545 else
546 TheCall->replaceAllUsesWith(R->getReturnValue());
Devang Pateldc00d422008-03-04 21:15:15 +0000547 }
Chris Lattner44a68072004-02-04 04:17:06 +0000548 // Since we are now done with the Call/Invoke, we can delete it.
Dan Gohman1adec832008-06-21 22:08:46 +0000549 TheCall->eraseFromParent();
Chris Lattner44a68072004-02-04 04:17:06 +0000550
551 // Since we are now done with the return instruction, delete it also.
Dan Gohman1adec832008-06-21 22:08:46 +0000552 Returns[0]->eraseFromParent();
Chris Lattner44a68072004-02-04 04:17:06 +0000553
554 // We are now done with the inlining.
555 return true;
556 }
557
558 // Otherwise, we have the normal case, of more than one block to inline or
559 // multiple return sites.
560
Chris Lattner5e923de2004-02-04 02:51:48 +0000561 // 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)) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000566
Chris Lattner5e923de2004-02-04 02:51:48 +0000567 // Add an unconditional branch to make this look like the CallInst case...
Gabor Greif051a9502008-04-06 20:25:17 +0000568 BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000569
Chris Lattner5e923de2004-02-04 02:51:48 +0000570 // 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,
Chris Lattner284d1b82004-12-11 16:59:54 +0000574 CalledFunc->getName()+".exit");
Misha Brukmanfd939082005-04-21 23:48:37 +0000575
Chris Lattner5e923de2004-02-04 02:51:48 +0000576 } else { // It's a call
Chris Lattner44a68072004-02-04 04:17:06 +0000577 // If this is a call instruction, we need to split the basic block that
578 // the call lives in.
Chris Lattner5e923de2004-02-04 02:51:48 +0000579 //
580 AfterCallBB = OrigBB->splitBasicBlock(TheCall,
Chris Lattner284d1b82004-12-11 16:59:54 +0000581 CalledFunc->getName()+".exit");
Chris Lattner5e923de2004-02-04 02:51:48 +0000582 }
583
Chris Lattner44a68072004-02-04 04:17:06 +0000584 // Change the branch that used to go to AfterCallBB to branch to the first
585 // basic block of the inlined function.
586 //
587 TerminatorInst *Br = OrigBB->getTerminator();
Misha Brukmanfd939082005-04-21 23:48:37 +0000588 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner44a68072004-02-04 04:17:06 +0000589 "splitBasicBlock broken!");
590 Br->setOperand(0, FirstNewBlock);
591
592
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.
Chris Lattner44a68072004-02-04 04:17:06 +0000596 Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
597 FirstNewBlock, Caller->end());
598
Chris Lattner5e923de2004-02-04 02:51:48 +0000599 // Handle all of the return instructions that we just cloned in, and eliminate
600 // any users of the original call/invoke instruction.
Devang Patelb8f198a2008-03-10 18:34:00 +0000601 const Type *RTy = CalledFunc->getReturnType();
Dan Gohman2c317502008-06-20 01:03:44 +0000602
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000603 if (Returns.size() > 1) {
Chris Lattner5e923de2004-02-04 02:51:48 +0000604 // The PHI node should go at the front of the new basic block to merge all
605 // possible incoming values.
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000606 PHINode *PHI = 0;
Chris Lattner5e923de2004-02-04 02:51:48 +0000607 if (!TheCall->use_empty()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000608 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.
Duncan Sandsa7212e52008-09-05 12:37:12 +0000612 TheCall->replaceAllUsesWith(PHI);
Chris Lattner5e923de2004-02-04 02:51:48 +0000613 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000614
Gabor Greifc478e522009-01-15 18:40:09 +0000615 // Loop over all of the return instructions adding entries to the PHI node
616 // as appropriate.
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000617 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());
Devang Patel12a466b2008-03-07 20:06:16 +0000623 }
624 }
625
Gabor Greifde62aea2009-01-16 23:08:50 +0000626 // Add a branch to the merge points and remove return instructions.
Chris Lattner5e923de2004-02-04 02:51:48 +0000627 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
628 ReturnInst *RI = Returns[i];
Dale Johannesen0744f092009-03-04 02:09:48 +0000629 BranchInst::Create(AfterCallBB, RI);
Devang Patelb8f198a2008-03-10 18:34:00 +0000630 RI->eraseFromParent();
Chris Lattner5e923de2004-02-04 02:51:48 +0000631 }
Devang Patelb8f198a2008-03-10 18:34:00 +0000632 } 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.
Eli Friedman5877ad72009-05-08 00:22:04 +0000635 if (!TheCall->use_empty()) {
636 if (TheCall == Returns[0]->getReturnValue())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000637 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Eli Friedman5877ad72009-05-08 00:22:04 +0000638 else
639 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
640 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000641
Devang Patelb8f198a2008-03-10 18:34:00 +0000642 // 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());
Duncan Sandsa7212e52008-09-05 12:37:12 +0000647
Devang Patelb8f198a2008-03-10 18:34:00 +0000648 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
649 ReturnBB->replaceAllUsesWith(AfterCallBB);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000650
Devang Patelb8f198a2008-03-10 18:34:00 +0000651 // Delete the return instruction now and empty ReturnBB now.
652 Returns[0]->eraseFromParent();
653 ReturnBB->eraseFromParent();
Chris Lattner3787e762004-10-17 23:21:07 +0000654 } else if (!TheCall->use_empty()) {
655 // No returns, but something is using the return value of the call. Just
656 // nuke the result.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000657 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Chris Lattner5e923de2004-02-04 02:51:48 +0000658 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000659
Chris Lattner5e923de2004-02-04 02:51:48 +0000660 // Since we are now done with the Call/Invoke, we can delete it.
Chris Lattner3787e762004-10-17 23:21:07 +0000661 TheCall->eraseFromParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000662
Chris Lattner7152c232003-08-24 04:06:56 +0000663 // We should always be able to fold the entry block of the function into the
664 // single predecessor of the block...
Chris Lattnercd01ae52004-04-16 05:17:59 +0000665 assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
Chris Lattner7152c232003-08-24 04:06:56 +0000666 BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
Chris Lattner44a68072004-02-04 04:17:06 +0000667
Chris Lattnercd01ae52004-04-16 05:17:59 +0000668 // 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
672
673 // Remove the unconditional branch.
674 OrigBB->getInstList().erase(Br);
675
676 // Now we can remove the CalleeEntry block, which is now empty.
677 Caller->getBasicBlockList().erase(CalleeEntry);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000678
Chris Lattnerca398dc2003-05-29 15:11:31 +0000679 return true;
680}