blob: 00ccaf8fcffed8b53f551fbc412a5193ff03ce55 [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
46/// nodes in that block with the values specified in InvokeDestPHIValues. If
47/// CallerCGN is specified, this function updates the call graph.
48///
49static void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
50 BasicBlock *InvokeDest,
51 const SmallVectorImpl<Value*> &InvokeDestPHIValues,
52 CallGraphNode *CallerCGN) {
53 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
54 Instruction *I = BBI++;
55
56 // We only need to check for function calls: inlined invoke
57 // instructions require no special handling.
58 CallInst *CI = dyn_cast<CallInst>(I);
59 if (CI == 0) continue;
60
61 // If this call cannot unwind, don't convert it to an invoke.
62 if (CI->doesNotThrow())
63 continue;
64
65 // Convert this function call into an invoke instruction.
66 // First, split the basic block.
67 BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
68
69 // Next, create the new invoke instruction, inserting it at the end
70 // of the old basic block.
71 SmallVector<Value*, 8> InvokeArgs(CI->op_begin()+1, CI->op_end());
72 InvokeInst *II =
73 InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest,
74 InvokeArgs.begin(), InvokeArgs.end(),
75 CI->getName(), BB->getTerminator());
76 II->setCallingConv(CI->getCallingConv());
77 II->setAttributes(CI->getAttributes());
78
79 // Make sure that anything using the call now uses the invoke!
80 CI->replaceAllUsesWith(II);
81
82 // Update the callgraph if present.
83 if (CallerCGN) {
84 // We should be able to do this:
85 // (*CG)[Caller]->replaceCallSite(CI, II);
86 // but that fails if the old call site isn't in the call graph,
87 // which, because of LLVM bug 3601, it sometimes isn't.
88 for (CallGraphNode::iterator NI = CallerCGN->begin(), NE = CallerCGN->end();
89 NI != NE; ++NI) {
90 if (NI->first == CI) {
91 NI->first = II;
92 break;
93 }
94 }
95 }
96
97 // Delete the unconditional branch inserted by splitBasicBlock
98 BB->getInstList().pop_back();
99 Split->getInstList().pop_front(); // Delete the original call
100
101 // Update any PHI nodes in the exceptional block to indicate that
102 // there is now a new entry in them.
103 unsigned i = 0;
104 for (BasicBlock::iterator I = InvokeDest->begin();
105 isa<PHINode>(I); ++I, ++i)
106 cast<PHINode>(I)->addIncoming(InvokeDestPHIValues[i], BB);
107
108 // This basic block is now complete, the caller will continue scanning the
109 // next one.
110 return;
111 }
112}
113
114
Chris Lattnercd4d3392006-01-13 19:05:59 +0000115/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
116/// in the body of the inlined function into invokes and turn unwind
117/// instructions into branches to the invoke unwind dest.
118///
Nick Lewyckydac5c4b2009-02-03 04:34:40 +0000119/// II is the invoke instruction being inlined. FirstNewBlock is the first
Chris Lattnercd4d3392006-01-13 19:05:59 +0000120/// block of the inlined code (the last block is the end of the function),
121/// and InlineCodeInfo is information about the code that got inlined.
122static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
Devang Patel28c531c2009-03-31 17:36:12 +0000123 ClonedCodeInfo &InlinedCodeInfo,
124 CallGraph *CG) {
Chris Lattnercd4d3392006-01-13 19:05:59 +0000125 BasicBlock *InvokeDest = II->getUnwindDest();
Chris Lattner135755d2009-08-27 03:51:50 +0000126 SmallVector<Value*, 8> InvokeDestPHIValues;
Chris Lattnercd4d3392006-01-13 19:05:59 +0000127
128 // If there are PHI nodes in the unwind destination block, we need to
129 // keep track of which values came into them from this invoke, then remove
130 // the entry for this block.
131 BasicBlock *InvokeBlock = II->getParent();
132 for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) {
133 PHINode *PN = cast<PHINode>(I);
134 // Save the value to use for this edge.
135 InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock));
136 }
137
138 Function *Caller = FirstNewBlock->getParent();
Duncan Sandsa7212e52008-09-05 12:37:12 +0000139
Chris Lattnercd4d3392006-01-13 19:05:59 +0000140 // The inlined code is currently at the end of the function, scan from the
141 // start of the inlined code to its end, checking for stuff we need to
Chris Lattner135755d2009-08-27 03:51:50 +0000142 // rewrite. If the code doesn't have calls or unwinds, we know there is
143 // nothing to rewrite.
144 if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
145 // Now that everything is happy, we have one final detail. The PHI nodes in
146 // the exception destination block still have entries due to the original
147 // invoke instruction. Eliminate these entries (which might even delete the
148 // PHI node) now.
149 InvokeDest->removePredecessor(II->getParent());
150 return;
151 }
152
153 CallGraphNode *CallerCGN = 0;
154 if (CG) CallerCGN = (*CG)[Caller];
155
156 for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
157 if (InlinedCodeInfo.ContainsCalls)
158 HandleCallsInBlockInlinedThroughInvoke(BB, InvokeDest,
159 InvokeDestPHIValues, CallerCGN);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000160
Chris Lattner135755d2009-08-27 03:51:50 +0000161 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
162 // An UnwindInst requires special handling when it gets inlined into an
163 // invoke site. Once this happens, we know that the unwind would cause
164 // a control transfer to the invoke exception destination, so we can
165 // transform it into a direct branch to the exception destination.
166 BranchInst::Create(InvokeDest, UI);
Chris Lattnercd4d3392006-01-13 19:05:59 +0000167
Chris Lattner135755d2009-08-27 03:51:50 +0000168 // Delete the unwind instruction!
169 UI->eraseFromParent();
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000170
Chris Lattner135755d2009-08-27 03:51:50 +0000171 // Update any PHI nodes in the exceptional block to indicate that
172 // there is now a new entry in them.
173 unsigned i = 0;
174 for (BasicBlock::iterator I = InvokeDest->begin();
175 isa<PHINode>(I); ++I, ++i) {
176 PHINode *PN = cast<PHINode>(I);
177 PN->addIncoming(InvokeDestPHIValues[i], BB);
Chris Lattnercd4d3392006-01-13 19:05:59 +0000178 }
179 }
180 }
181
182 // Now that everything is happy, we have one final detail. The PHI nodes in
183 // the exception destination block still have entries due to the original
184 // invoke instruction. Eliminate these entries (which might even delete the
185 // PHI node) now.
186 InvokeDest->removePredecessor(II->getParent());
187}
188
Chris Lattnerd85340f2006-07-12 18:29:36 +0000189/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
190/// into the caller, update the specified callgraph to reflect the changes we
191/// made. Note that it's possible that not all code was copied over, so only
Duncan Sandsd7b98512008-09-08 11:05:51 +0000192/// some edges of the callgraph may remain.
193static void UpdateCallGraphAfterInlining(CallSite CS,
Chris Lattnerd85340f2006-07-12 18:29:36 +0000194 Function::iterator FirstNewBlock,
Chris Lattner5e665f52007-02-03 00:08:31 +0000195 DenseMap<const Value*, Value*> &ValueMap,
Chris Lattner468fb1d2006-01-14 20:07:50 +0000196 CallGraph &CG) {
Duncan Sandsd7b98512008-09-08 11:05:51 +0000197 const Function *Caller = CS.getInstruction()->getParent()->getParent();
198 const Function *Callee = CS.getCalledFunction();
Chris Lattner468fb1d2006-01-14 20:07:50 +0000199 CallGraphNode *CalleeNode = CG[Callee];
200 CallGraphNode *CallerNode = CG[Caller];
Duncan Sandsa7212e52008-09-05 12:37:12 +0000201
Chris Lattnerd85340f2006-07-12 18:29:36 +0000202 // Since we inlined some uninlined call sites in the callee into the caller,
Chris Lattner468fb1d2006-01-14 20:07:50 +0000203 // add edges from the caller to all of the callees of the callee.
Gabor Greifc478e522009-01-15 18:40:09 +0000204 CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
205
206 // Consider the case where CalleeNode == CallerNode.
Gabor Greif12532982009-01-17 00:09:08 +0000207 CallGraphNode::CalledFunctionsVector CallCache;
Gabor Greifc478e522009-01-15 18:40:09 +0000208 if (CalleeNode == CallerNode) {
209 CallCache.assign(I, E);
210 I = CallCache.begin();
211 E = CallCache.end();
212 }
213
214 for (; I != E; ++I) {
Chris Lattnera541b0f2009-09-01 06:31:31 +0000215 const Value *OrigCall = I->first;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000216
Chris Lattner5e665f52007-02-03 00:08:31 +0000217 DenseMap<const Value*, Value*>::iterator VMI = ValueMap.find(OrigCall);
Chris Lattner981418b2006-07-12 21:37:11 +0000218 // Only copy the edge if the call was inlined!
Chris Lattner135755d2009-08-27 03:51:50 +0000219 if (VMI == ValueMap.end() || VMI->second == 0)
220 continue;
221
222 // If the call was inlined, but then constant folded, there is no edge to
223 // add. Check for this case.
224 if (Instruction *NewCall = dyn_cast<Instruction>(VMI->second))
225 CallerNode->addCalledFunction(CallSite::get(NewCall), I->second);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000226 }
Chris Lattner135755d2009-08-27 03:51:50 +0000227
Dale Johannesen39fa3242009-01-13 22:43:37 +0000228 // Update the call graph by deleting the edge from Callee to Caller. We must
229 // do this after the loop above in case Caller and Callee are the same.
230 CallerNode->removeCallEdgeFor(CS);
Chris Lattner468fb1d2006-01-14 20:07:50 +0000231}
232
Devang Patel517576d2009-04-15 00:17:06 +0000233/// findFnRegionEndMarker - This is a utility routine that is used by
234/// InlineFunction. Return llvm.dbg.region.end intrinsic that corresponds
235/// to the llvm.dbg.func.start of the function F. Otherwise return NULL.
Chris Lattner135755d2009-08-27 03:51:50 +0000236///
Devang Patel517576d2009-04-15 00:17:06 +0000237static const DbgRegionEndInst *findFnRegionEndMarker(const Function *F) {
238
Devang Patele4b27562009-08-28 23:24:31 +0000239 MDNode *FnStart = NULL;
Devang Patel517576d2009-04-15 00:17:06 +0000240 const DbgRegionEndInst *FnEnd = NULL;
241 for (Function::const_iterator FI = F->begin(), FE =F->end(); FI != FE; ++FI)
242 for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end(); BI != BE;
243 ++BI) {
244 if (FnStart == NULL) {
245 if (const DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI)) {
Devang Patele4b27562009-08-28 23:24:31 +0000246 DISubprogram SP(FSI->getSubprogram());
Devang Patel517576d2009-04-15 00:17:06 +0000247 assert (SP.isNull() == false && "Invalid llvm.dbg.func.start");
248 if (SP.describes(F))
Devang Patele4b27562009-08-28 23:24:31 +0000249 FnStart = SP.getNode();
Devang Patel517576d2009-04-15 00:17:06 +0000250 }
Chris Lattner135755d2009-08-27 03:51:50 +0000251 continue;
Devang Patel517576d2009-04-15 00:17:06 +0000252 }
Chris Lattner135755d2009-08-27 03:51:50 +0000253
254 if (const DbgRegionEndInst *REI = dyn_cast<DbgRegionEndInst>(BI))
255 if (REI->getContext() == FnStart)
256 FnEnd = REI;
Devang Patel517576d2009-04-15 00:17:06 +0000257 }
258 return FnEnd;
259}
Chris Lattnercd4d3392006-01-13 19:05:59 +0000260
Chris Lattnerca398dc2003-05-29 15:11:31 +0000261// InlineFunction - This function inlines the called function into the basic
262// block of the caller. This returns false if it is not possible to inline this
263// call. The program is still in a well defined state if this occurs though.
264//
Misha Brukmanfd939082005-04-21 23:48:37 +0000265// Note that this only does one level of inlining. For example, if the
266// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
Chris Lattnerca398dc2003-05-29 15:11:31 +0000267// exists in the instruction stream. Similiarly this will inline a recursive
268// function by one level.
269//
Chris Lattner8f2718f2009-08-27 04:20:52 +0000270bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD,
271 SmallVectorImpl<AllocaInst*> *StaticAllocas) {
Chris Lattner80a38d22003-08-24 06:59:16 +0000272 Instruction *TheCall = CS.getInstruction();
Owen Andersone922c022009-07-22 00:24:57 +0000273 LLVMContext &Context = TheCall->getContext();
Chris Lattner80a38d22003-08-24 06:59:16 +0000274 assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
275 "Instruction not in function!");
Chris Lattnerca398dc2003-05-29 15:11:31 +0000276
Chris Lattner80a38d22003-08-24 06:59:16 +0000277 const Function *CalledFunc = CS.getCalledFunction();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000278 if (CalledFunc == 0 || // Can't inline external function or indirect
Reid Spencer5cbf9852007-01-30 20:08:39 +0000279 CalledFunc->isDeclaration() || // call, or call to a vararg function!
Chris Lattnerca398dc2003-05-29 15:11:31 +0000280 CalledFunc->getFunctionType()->isVarArg()) return false;
281
Chris Lattner1b491412005-05-06 06:47:52 +0000282
Chris Lattneraf9985c2009-02-12 07:06:42 +0000283 // If the call to the callee is not a tail call, we must clear the 'tail'
Chris Lattner1b491412005-05-06 06:47:52 +0000284 // flags on any calls that we inline.
285 bool MustClearTailCallFlags =
Chris Lattneraf9985c2009-02-12 07:06:42 +0000286 !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
Chris Lattner1b491412005-05-06 06:47:52 +0000287
Duncan Sandsf0c33542007-12-19 21:13:37 +0000288 // If the call to the callee cannot throw, set the 'nounwind' flag on any
289 // calls that we inline.
290 bool MarkNoUnwind = CS.doesNotThrow();
291
Chris Lattner80a38d22003-08-24 06:59:16 +0000292 BasicBlock *OrigBB = TheCall->getParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000293 Function *Caller = OrigBB->getParent();
294
Gordon Henriksen0e138212007-12-25 03:10:07 +0000295 // GC poses two hazards to inlining, which only occur when the callee has GC:
296 // 1. If the caller has no GC, then the callee's GC must be propagated to the
297 // caller.
298 // 2. If the caller has a differing GC, it is invalid to inline.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000299 if (CalledFunc->hasGC()) {
300 if (!Caller->hasGC())
301 Caller->setGC(CalledFunc->getGC());
302 else if (CalledFunc->getGC() != Caller->getGC())
Gordon Henriksen0e138212007-12-25 03:10:07 +0000303 return false;
304 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000305
Chris Lattner5052c912004-02-04 01:41:09 +0000306 // Get an iterator to the last basic block in the function, which will have
307 // the new function inlined after it.
308 //
309 Function::iterator LastBlock = &Caller->back();
310
Chris Lattner5e923de2004-02-04 02:51:48 +0000311 // Make sure to capture all of the return instructions from the cloned
Chris Lattnerca398dc2003-05-29 15:11:31 +0000312 // function.
Chris Lattnerec1bea02009-08-27 04:02:30 +0000313 SmallVector<ReturnInst*, 8> Returns;
Chris Lattnercd4d3392006-01-13 19:05:59 +0000314 ClonedCodeInfo InlinedFunctionInfo;
Dale Johannesen0744f092009-03-04 02:09:48 +0000315 Function::iterator FirstNewBlock;
Duncan Sandsf0c33542007-12-19 21:13:37 +0000316
Chris Lattner5e923de2004-02-04 02:51:48 +0000317 { // Scope to destroy ValueMap after cloning.
Chris Lattner5e665f52007-02-03 00:08:31 +0000318 DenseMap<const Value*, Value*> ValueMap;
Chris Lattner5b5bc302006-05-27 01:28:04 +0000319
Dan Gohman9614fcc2008-06-20 17:11:32 +0000320 assert(CalledFunc->arg_size() == CS.arg_size() &&
Chris Lattner5e923de2004-02-04 02:51:48 +0000321 "No varargs calls can be inlined!");
Duncan Sandsa7212e52008-09-05 12:37:12 +0000322
Chris Lattnerc93adca2008-01-11 06:09:30 +0000323 // Calculate the vector of arguments to pass into the function cloner, which
324 // matches up the formal to the actual argument values.
Chris Lattner5e923de2004-02-04 02:51:48 +0000325 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattnerc93adca2008-01-11 06:09:30 +0000326 unsigned ArgNo = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +0000327 for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
Chris Lattnerc93adca2008-01-11 06:09:30 +0000328 E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
329 Value *ActualArg = *AI;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000330
Duncan Sandsd82375c2008-01-27 18:12:58 +0000331 // When byval arguments actually inlined, we need to make the copy implied
332 // by them explicit. However, we don't do this if the callee is readonly
333 // or readnone, because the copy would be unneeded: the callee doesn't
334 // modify the struct.
Devang Patel05988662008-09-25 21:00:45 +0000335 if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
Duncan Sandsd82375c2008-01-27 18:12:58 +0000336 !CalledFunc->onlyReadsMemory()) {
Chris Lattnerc93adca2008-01-11 06:09:30 +0000337 const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
Owen Anderson1d0be152009-08-13 21:58:54 +0000338 const Type *VoidPtrTy =
339 PointerType::getUnqual(Type::getInt8Ty(Context));
Duncan Sandsa7212e52008-09-05 12:37:12 +0000340
Chris Lattnerc93adca2008-01-11 06:09:30 +0000341 // Create the alloca. If we have TargetData, use nice alignment.
342 unsigned Align = 1;
343 if (TD) Align = TD->getPrefTypeAlignment(AggTy);
Owen Anderson50dead02009-07-15 23:53:25 +0000344 Value *NewAlloca = new AllocaInst(AggTy, 0, Align,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000345 I->getName(),
346 &*Caller->begin()->begin());
Chris Lattnerc93adca2008-01-11 06:09:30 +0000347 // Emit a memcpy.
Owen Anderson1d0be152009-08-13 21:58:54 +0000348 const Type *Tys[] = { Type::getInt64Ty(Context) };
Chris Lattnerc93adca2008-01-11 06:09:30 +0000349 Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
Chris Lattner824b9582008-11-21 16:42:48 +0000350 Intrinsic::memcpy,
351 Tys, 1);
Chris Lattnerc93adca2008-01-11 06:09:30 +0000352 Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
353 Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000354
Chris Lattnerc93adca2008-01-11 06:09:30 +0000355 Value *Size;
356 if (TD == 0)
Owen Andersonbaf3c402009-07-29 18:55:55 +0000357 Size = ConstantExpr::getSizeOf(AggTy);
Chris Lattnerc93adca2008-01-11 06:09:30 +0000358 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000359 Size = ConstantInt::get(Type::getInt64Ty(Context),
Owen Anderson0a205a42009-07-05 22:41:43 +0000360 TD->getTypeStoreSize(AggTy));
Duncan Sandsa7212e52008-09-05 12:37:12 +0000361
Chris Lattnerc93adca2008-01-11 06:09:30 +0000362 // Always generate a memcpy of alignment 1 here because we don't know
363 // the alignment of the src pointer. Other optimizations can infer
364 // better alignment.
365 Value *CallArgs[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000366 DestCast, SrcCast, Size,
367 ConstantInt::get(Type::getInt32Ty(Context), 1)
Chris Lattnerc93adca2008-01-11 06:09:30 +0000368 };
369 CallInst *TheMemCpy =
Gabor Greif051a9502008-04-06 20:25:17 +0000370 CallInst::Create(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000371
Chris Lattnerc93adca2008-01-11 06:09:30 +0000372 // If we have a call graph, update it.
373 if (CG) {
374 CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn);
375 CallGraphNode *CallerNode = (*CG)[Caller];
376 CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN);
377 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000378
Chris Lattnerc93adca2008-01-11 06:09:30 +0000379 // Uses of the argument in the function should use our new alloca
380 // instead.
381 ActualArg = NewAlloca;
382 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000383
Chris Lattnerc93adca2008-01-11 06:09:30 +0000384 ValueMap[I] = ActualArg;
385 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000386
Devang Patel517576d2009-04-15 00:17:06 +0000387 // Adjust llvm.dbg.region.end. If the CalledFunc has region end
388 // marker then clone that marker after next stop point at the
389 // call site. The function body cloner does not clone original
390 // region end marker from the CalledFunc. This will ensure that
391 // inlined function's scope ends at the right place.
Chris Lattner135755d2009-08-27 03:51:50 +0000392 if (const DbgRegionEndInst *DREI = findFnRegionEndMarker(CalledFunc)) {
393 for (BasicBlock::iterator BI = TheCall, BE = TheCall->getParent()->end();
394 BI != BE; ++BI) {
Devang Patel517576d2009-04-15 00:17:06 +0000395 if (DbgStopPointInst *DSPI = dyn_cast<DbgStopPointInst>(BI)) {
396 if (DbgRegionEndInst *NewDREI =
Chris Lattner135755d2009-08-27 03:51:50 +0000397 dyn_cast<DbgRegionEndInst>(DREI->clone(Context)))
Devang Patel517576d2009-04-15 00:17:06 +0000398 NewDREI->insertAfter(DSPI);
399 break;
400 }
401 }
402 }
403
Chris Lattner5b5bc302006-05-27 01:28:04 +0000404 // We want the inliner to prune the code as it copies. We would LOVE to
405 // have no dead or constant instructions leftover after inlining occurs
406 // (which can happen, e.g., because an argument was constant), but we'll be
407 // happy with whatever the cloner can do.
408 CloneAndPruneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i",
Chris Lattner1dfdf822007-01-30 23:22:39 +0000409 &InlinedFunctionInfo, TD);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000410
Chris Lattnerd85340f2006-07-12 18:29:36 +0000411 // Remember the first block that is newly cloned over.
412 FirstNewBlock = LastBlock; ++FirstNewBlock;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000413
Chris Lattnerd85340f2006-07-12 18:29:36 +0000414 // Update the callgraph if requested.
415 if (CG)
Duncan Sandsd7b98512008-09-08 11:05:51 +0000416 UpdateCallGraphAfterInlining(CS, FirstNewBlock, ValueMap, *CG);
Misha Brukmanfd939082005-04-21 23:48:37 +0000417 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000418
Chris Lattnerca398dc2003-05-29 15:11:31 +0000419 // If there are any alloca instructions in the block that used to be the entry
420 // block for the callee, move them to the entry block of the caller. First
421 // calculate which instruction they should be inserted before. We insert the
422 // instructions at the end of the current alloca list.
423 //
Chris Lattner21f20552006-01-13 18:16:48 +0000424 {
Chris Lattner80a38d22003-08-24 06:59:16 +0000425 BasicBlock::iterator InsertPoint = Caller->begin()->begin();
Chris Lattner5e923de2004-02-04 02:51:48 +0000426 for (BasicBlock::iterator I = FirstNewBlock->begin(),
Chris Lattner135755d2009-08-27 03:51:50 +0000427 E = FirstNewBlock->end(); I != E; ) {
428 AllocaInst *AI = dyn_cast<AllocaInst>(I++);
429 if (AI == 0) continue;
430
431 // If the alloca is now dead, remove it. This often occurs due to code
432 // specialization.
433 if (AI->use_empty()) {
434 AI->eraseFromParent();
435 continue;
Chris Lattner33bb3c82006-09-13 19:23:57 +0000436 }
Chris Lattner135755d2009-08-27 03:51:50 +0000437
438 if (!isa<Constant>(AI->getArraySize()))
439 continue;
440
Chris Lattner8f2718f2009-08-27 04:20:52 +0000441 // Keep track of the static allocas that we inline into the caller if the
442 // StaticAllocas pointer is non-null.
443 if (StaticAllocas) StaticAllocas->push_back(AI);
444
Chris Lattner135755d2009-08-27 03:51:50 +0000445 // Scan for the block of allocas that we can move over, and move them
446 // all at once.
447 while (isa<AllocaInst>(I) &&
Chris Lattner8f2718f2009-08-27 04:20:52 +0000448 isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
449 if (StaticAllocas) StaticAllocas->push_back(cast<AllocaInst>(I));
Chris Lattner135755d2009-08-27 03:51:50 +0000450 ++I;
Chris Lattner8f2718f2009-08-27 04:20:52 +0000451 }
Chris Lattner135755d2009-08-27 03:51:50 +0000452
453 // Transfer all of the allocas over in a block. Using splice means
454 // that the instructions aren't removed from the symbol table, then
455 // reinserted.
456 Caller->getEntryBlock().getInstList().splice(InsertPoint,
457 FirstNewBlock->getInstList(),
458 AI, I);
459 }
Chris Lattner80a38d22003-08-24 06:59:16 +0000460 }
Chris Lattnerca398dc2003-05-29 15:11:31 +0000461
Chris Lattnerbf229f42006-01-13 19:34:14 +0000462 // If the inlined code contained dynamic alloca instructions, wrap the inlined
463 // code with llvm.stacksave/llvm.stackrestore intrinsics.
464 if (InlinedFunctionInfo.ContainsDynamicAllocas) {
465 Module *M = Caller->getParent();
Chris Lattnerbf229f42006-01-13 19:34:14 +0000466 // Get the two intrinsics we care about.
Chris Lattnera121fdd2007-01-07 07:54:34 +0000467 Constant *StackSave, *StackRestore;
Duncan Sands3d292ac2008-04-07 13:43:58 +0000468 StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
469 StackRestore = Intrinsic::getDeclaration(M, Intrinsic::stackrestore);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000470
471 // If we are preserving the callgraph, add edges to the stacksave/restore
472 // functions for the calls we insert.
Chris Lattner21ba23d2006-07-18 21:48:57 +0000473 CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0;
Chris Lattnerd85340f2006-07-12 18:29:36 +0000474 if (CG) {
Chris Lattnera121fdd2007-01-07 07:54:34 +0000475 // We know that StackSave/StackRestore are Function*'s, because they are
476 // intrinsics which must have the right types.
477 StackSaveCGN = CG->getOrInsertFunction(cast<Function>(StackSave));
478 StackRestoreCGN = CG->getOrInsertFunction(cast<Function>(StackRestore));
Chris Lattnerd85340f2006-07-12 18:29:36 +0000479 CallerNode = (*CG)[Caller];
480 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000481
Chris Lattnerbf229f42006-01-13 19:34:14 +0000482 // Insert the llvm.stacksave.
Duncan Sandsa7212e52008-09-05 12:37:12 +0000483 CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack",
Gabor Greif051a9502008-04-06 20:25:17 +0000484 FirstNewBlock->begin());
Chris Lattnerd85340f2006-07-12 18:29:36 +0000485 if (CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000486
Chris Lattnerbf229f42006-01-13 19:34:14 +0000487 // Insert a call to llvm.stackrestore before any return instructions in the
488 // inlined function.
Chris Lattnerd85340f2006-07-12 18:29:36 +0000489 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
Gabor Greif051a9502008-04-06 20:25:17 +0000490 CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000491 if (CG) CallerNode->addCalledFunction(CI, StackRestoreCGN);
492 }
Chris Lattner468fb1d2006-01-14 20:07:50 +0000493
494 // Count the number of StackRestore calls we insert.
495 unsigned NumStackRestores = Returns.size();
Duncan Sandsa7212e52008-09-05 12:37:12 +0000496
Chris Lattnerbf229f42006-01-13 19:34:14 +0000497 // If we are inlining an invoke instruction, insert restores before each
498 // unwind. These unwinds will be rewritten into branches later.
499 if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
500 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
501 BB != E; ++BB)
Chris Lattner468fb1d2006-01-14 20:07:50 +0000502 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
Duncan Sands3d292ac2008-04-07 13:43:58 +0000503 CallInst::Create(StackRestore, SavedPtr, "", UI);
Chris Lattner468fb1d2006-01-14 20:07:50 +0000504 ++NumStackRestores;
505 }
506 }
Chris Lattnerbf229f42006-01-13 19:34:14 +0000507 }
508
Duncan Sandsa7212e52008-09-05 12:37:12 +0000509 // If we are inlining tail call instruction through a call site that isn't
Chris Lattner1fdf4a82006-01-13 19:18:11 +0000510 // marked 'tail', we must remove the tail marker for any calls in the inlined
Duncan Sandsf0c33542007-12-19 21:13:37 +0000511 // code. Also, calls inlined through a 'nounwind' call site should be marked
512 // 'nounwind'.
513 if (InlinedFunctionInfo.ContainsCalls &&
514 (MustClearTailCallFlags || MarkNoUnwind)) {
Chris Lattner1b491412005-05-06 06:47:52 +0000515 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
516 BB != E; ++BB)
517 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Duncan Sandsf0c33542007-12-19 21:13:37 +0000518 if (CallInst *CI = dyn_cast<CallInst>(I)) {
519 if (MustClearTailCallFlags)
520 CI->setTailCall(false);
521 if (MarkNoUnwind)
522 CI->setDoesNotThrow();
523 }
Chris Lattner1b491412005-05-06 06:47:52 +0000524 }
525
Duncan Sandsf0c33542007-12-19 21:13:37 +0000526 // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
527 // instructions are unreachable.
528 if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
529 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
530 BB != E; ++BB) {
531 TerminatorInst *Term = BB->getTerminator();
532 if (isa<UnwindInst>(Term)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000533 new UnreachableInst(Context, Term);
Duncan Sandsf0c33542007-12-19 21:13:37 +0000534 BB->getInstList().erase(Term);
535 }
536 }
537
Chris Lattner5e923de2004-02-04 02:51:48 +0000538 // If we are inlining for an invoke instruction, we must make sure to rewrite
539 // any inlined 'unwind' instructions into branches to the invoke exception
540 // destination, and call instructions into invoke instructions.
Chris Lattnercd4d3392006-01-13 19:05:59 +0000541 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
Devang Patel28c531c2009-03-31 17:36:12 +0000542 HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo, CG);
Chris Lattner5e923de2004-02-04 02:51:48 +0000543
Chris Lattner44a68072004-02-04 04:17:06 +0000544 // If we cloned in _exactly one_ basic block, and if that block ends in a
545 // return instruction, we splice the body of the inlined callee directly into
546 // the calling basic block.
547 if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
548 // Move all of the instructions right before the call.
549 OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
550 FirstNewBlock->begin(), FirstNewBlock->end());
551 // Remove the cloned basic block.
552 Caller->getBasicBlockList().pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000553
Chris Lattner44a68072004-02-04 04:17:06 +0000554 // If the call site was an invoke instruction, add a branch to the normal
555 // destination.
556 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
Gabor Greif051a9502008-04-06 20:25:17 +0000557 BranchInst::Create(II->getNormalDest(), TheCall);
Chris Lattner44a68072004-02-04 04:17:06 +0000558
559 // If the return instruction returned a value, replace uses of the call with
560 // uses of the returned value.
Devang Pateldc00d422008-03-04 21:15:15 +0000561 if (!TheCall->use_empty()) {
562 ReturnInst *R = Returns[0];
Eli Friedman5877ad72009-05-08 00:22:04 +0000563 if (TheCall == R->getReturnValue())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000564 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Eli Friedman5877ad72009-05-08 00:22:04 +0000565 else
566 TheCall->replaceAllUsesWith(R->getReturnValue());
Devang Pateldc00d422008-03-04 21:15:15 +0000567 }
Chris Lattner44a68072004-02-04 04:17:06 +0000568 // Since we are now done with the Call/Invoke, we can delete it.
Dan Gohman1adec832008-06-21 22:08:46 +0000569 TheCall->eraseFromParent();
Chris Lattner44a68072004-02-04 04:17:06 +0000570
571 // Since we are now done with the return instruction, delete it also.
Dan Gohman1adec832008-06-21 22:08:46 +0000572 Returns[0]->eraseFromParent();
Chris Lattner44a68072004-02-04 04:17:06 +0000573
574 // We are now done with the inlining.
575 return true;
576 }
577
578 // Otherwise, we have the normal case, of more than one block to inline or
579 // multiple return sites.
580
Chris Lattner5e923de2004-02-04 02:51:48 +0000581 // We want to clone the entire callee function into the hole between the
582 // "starter" and "ender" blocks. How we accomplish this depends on whether
583 // this is an invoke instruction or a call instruction.
584 BasicBlock *AfterCallBB;
585 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000586
Chris Lattner5e923de2004-02-04 02:51:48 +0000587 // Add an unconditional branch to make this look like the CallInst case...
Gabor Greif051a9502008-04-06 20:25:17 +0000588 BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000589
Chris Lattner5e923de2004-02-04 02:51:48 +0000590 // Split the basic block. This guarantees that no PHI nodes will have to be
591 // updated due to new incoming edges, and make the invoke case more
592 // symmetric to the call case.
593 AfterCallBB = OrigBB->splitBasicBlock(NewBr,
Chris Lattner284d1b82004-12-11 16:59:54 +0000594 CalledFunc->getName()+".exit");
Misha Brukmanfd939082005-04-21 23:48:37 +0000595
Chris Lattner5e923de2004-02-04 02:51:48 +0000596 } else { // It's a call
Chris Lattner44a68072004-02-04 04:17:06 +0000597 // If this is a call instruction, we need to split the basic block that
598 // the call lives in.
Chris Lattner5e923de2004-02-04 02:51:48 +0000599 //
600 AfterCallBB = OrigBB->splitBasicBlock(TheCall,
Chris Lattner284d1b82004-12-11 16:59:54 +0000601 CalledFunc->getName()+".exit");
Chris Lattner5e923de2004-02-04 02:51:48 +0000602 }
603
Chris Lattner44a68072004-02-04 04:17:06 +0000604 // Change the branch that used to go to AfterCallBB to branch to the first
605 // basic block of the inlined function.
606 //
607 TerminatorInst *Br = OrigBB->getTerminator();
Misha Brukmanfd939082005-04-21 23:48:37 +0000608 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner44a68072004-02-04 04:17:06 +0000609 "splitBasicBlock broken!");
610 Br->setOperand(0, FirstNewBlock);
611
612
613 // Now that the function is correct, make it a little bit nicer. In
614 // particular, move the basic blocks inserted from the end of the function
615 // into the space made by splitting the source basic block.
Chris Lattner44a68072004-02-04 04:17:06 +0000616 Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
617 FirstNewBlock, Caller->end());
618
Chris Lattner5e923de2004-02-04 02:51:48 +0000619 // Handle all of the return instructions that we just cloned in, and eliminate
620 // any users of the original call/invoke instruction.
Devang Patelb8f198a2008-03-10 18:34:00 +0000621 const Type *RTy = CalledFunc->getReturnType();
Dan Gohman2c317502008-06-20 01:03:44 +0000622
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000623 if (Returns.size() > 1) {
Chris Lattner5e923de2004-02-04 02:51:48 +0000624 // The PHI node should go at the front of the new basic block to merge all
625 // possible incoming values.
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000626 PHINode *PHI = 0;
Chris Lattner5e923de2004-02-04 02:51:48 +0000627 if (!TheCall->use_empty()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000628 PHI = PHINode::Create(RTy, TheCall->getName(),
629 AfterCallBB->begin());
630 // Anything that used the result of the function call should now use the
631 // PHI node as their operand.
Duncan Sandsa7212e52008-09-05 12:37:12 +0000632 TheCall->replaceAllUsesWith(PHI);
Chris Lattner5e923de2004-02-04 02:51:48 +0000633 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000634
Gabor Greifc478e522009-01-15 18:40:09 +0000635 // Loop over all of the return instructions adding entries to the PHI node
636 // as appropriate.
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000637 if (PHI) {
638 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
639 ReturnInst *RI = Returns[i];
640 assert(RI->getReturnValue()->getType() == PHI->getType() &&
641 "Ret value not consistent in function!");
642 PHI->addIncoming(RI->getReturnValue(), RI->getParent());
Devang Patel12a466b2008-03-07 20:06:16 +0000643 }
644 }
645
Gabor Greifde62aea2009-01-16 23:08:50 +0000646 // Add a branch to the merge points and remove return instructions.
Chris Lattner5e923de2004-02-04 02:51:48 +0000647 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
648 ReturnInst *RI = Returns[i];
Dale Johannesen0744f092009-03-04 02:09:48 +0000649 BranchInst::Create(AfterCallBB, RI);
Devang Patelb8f198a2008-03-10 18:34:00 +0000650 RI->eraseFromParent();
Chris Lattner5e923de2004-02-04 02:51:48 +0000651 }
Devang Patelb8f198a2008-03-10 18:34:00 +0000652 } else if (!Returns.empty()) {
653 // Otherwise, if there is exactly one return value, just replace anything
654 // using the return value of the call with the computed value.
Eli Friedman5877ad72009-05-08 00:22:04 +0000655 if (!TheCall->use_empty()) {
656 if (TheCall == Returns[0]->getReturnValue())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000657 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Eli Friedman5877ad72009-05-08 00:22:04 +0000658 else
659 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
660 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000661
Devang Patelb8f198a2008-03-10 18:34:00 +0000662 // Splice the code from the return block into the block that it will return
663 // to, which contains the code that was after the call.
664 BasicBlock *ReturnBB = Returns[0]->getParent();
665 AfterCallBB->getInstList().splice(AfterCallBB->begin(),
666 ReturnBB->getInstList());
Duncan Sandsa7212e52008-09-05 12:37:12 +0000667
Devang Patelb8f198a2008-03-10 18:34:00 +0000668 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
669 ReturnBB->replaceAllUsesWith(AfterCallBB);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000670
Devang Patelb8f198a2008-03-10 18:34:00 +0000671 // Delete the return instruction now and empty ReturnBB now.
672 Returns[0]->eraseFromParent();
673 ReturnBB->eraseFromParent();
Chris Lattner3787e762004-10-17 23:21:07 +0000674 } else if (!TheCall->use_empty()) {
675 // No returns, but something is using the return value of the call. Just
676 // nuke the result.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000677 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Chris Lattner5e923de2004-02-04 02:51:48 +0000678 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000679
Chris Lattner5e923de2004-02-04 02:51:48 +0000680 // Since we are now done with the Call/Invoke, we can delete it.
Chris Lattner3787e762004-10-17 23:21:07 +0000681 TheCall->eraseFromParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000682
Chris Lattner7152c232003-08-24 04:06:56 +0000683 // We should always be able to fold the entry block of the function into the
684 // single predecessor of the block...
Chris Lattnercd01ae52004-04-16 05:17:59 +0000685 assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
Chris Lattner7152c232003-08-24 04:06:56 +0000686 BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
Chris Lattner44a68072004-02-04 04:17:06 +0000687
Chris Lattnercd01ae52004-04-16 05:17:59 +0000688 // Splice the code entry block into calling block, right before the
689 // unconditional branch.
690 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
691 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
692
693 // Remove the unconditional branch.
694 OrigBB->getInstList().erase(Br);
695
696 // Now we can remove the CalleeEntry block, which is now empty.
697 Caller->getBasicBlockList().erase(CalleeEntry);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000698
Chris Lattnerca398dc2003-05-29 15:11:31 +0000699 return true;
700}