blob: e09666a417b43fd8badabdee024b24828097a43f [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"
Chris Lattnerca398dc2003-05-29 15:11:31 +000018#include "llvm/Module.h"
Chris Lattner80a38d22003-08-24 06:59:16 +000019#include "llvm/Instructions.h"
Devang Patel517576d2009-04-15 00:17:06 +000020#include "llvm/IntrinsicInst.h"
Chris Lattner80a38d22003-08-24 06:59:16 +000021#include "llvm/Intrinsics.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000022#include "llvm/Attributes.h"
Chris Lattner468fb1d2006-01-14 20:07:50 +000023#include "llvm/Analysis/CallGraph.h"
Devang Patel517576d2009-04-15 00:17:06 +000024#include "llvm/Analysis/DebugInfo.h"
Chris Lattnerc93adca2008-01-11 06:09:30 +000025#include "llvm/Target/TargetData.h"
Chris Lattner93e985f2007-02-13 02:10:56 +000026#include "llvm/ADT/SmallVector.h"
Devang Patel641ca932008-03-10 18:22:16 +000027#include "llvm/ADT/StringExtras.h"
Chris Lattner80a38d22003-08-24 06:59:16 +000028#include "llvm/Support/CallSite.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000029using namespace llvm;
Chris Lattnerca398dc2003-05-29 15:11:31 +000030
Chris Lattner60915142010-04-22 23:07:58 +000031bool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI) {
32 return InlineFunction(CallSite(CI), IFI);
Chris Lattner468fb1d2006-01-14 20:07:50 +000033}
Chris Lattner60915142010-04-22 23:07:58 +000034bool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) {
35 return InlineFunction(CallSite(II), IFI);
Chris Lattner468fb1d2006-01-14 20:07:50 +000036}
Chris Lattner80a38d22003-08-24 06:59:16 +000037
Chris Lattner135755d2009-08-27 03:51:50 +000038
39/// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
Eric Christopherf61f89a2009-09-06 22:20:54 +000040/// an invoke, we have to turn all of the calls that can throw into
Chris Lattner135755d2009-08-27 03:51:50 +000041/// invokes. This function analyze BB to see if there are any calls, and if so,
42/// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI
Chris Lattner81dfb382009-09-01 18:44:06 +000043/// nodes in that block with the values specified in InvokeDestPHIValues.
Chris Lattner135755d2009-08-27 03:51:50 +000044///
45static void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
46 BasicBlock *InvokeDest,
Chris Lattner81dfb382009-09-01 18:44:06 +000047 const SmallVectorImpl<Value*> &InvokeDestPHIValues) {
Chris Lattner135755d2009-08-27 03:51:50 +000048 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
49 Instruction *I = BBI++;
50
51 // We only need to check for function calls: inlined invoke
52 // instructions require no special handling.
53 CallInst *CI = dyn_cast<CallInst>(I);
54 if (CI == 0) continue;
55
56 // If this call cannot unwind, don't convert it to an invoke.
57 if (CI->doesNotThrow())
58 continue;
59
60 // Convert this function call into an invoke instruction.
61 // First, split the basic block.
62 BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
63
64 // Next, create the new invoke instruction, inserting it at the end
65 // of the old basic block.
Gabor Greifaeff3852010-06-24 09:56:43 +000066 ImmutableCallSite CS(CI);
67 SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end());
Chris Lattner135755d2009-08-27 03:51:50 +000068 InvokeInst *II =
69 InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest,
70 InvokeArgs.begin(), InvokeArgs.end(),
71 CI->getName(), BB->getTerminator());
72 II->setCallingConv(CI->getCallingConv());
73 II->setAttributes(CI->getAttributes());
74
Chris Lattner81dfb382009-09-01 18:44:06 +000075 // Make sure that anything using the call now uses the invoke! This also
Chris Lattner07686322010-04-23 18:37:01 +000076 // updates the CallGraph if present, because it uses a WeakVH.
Chris Lattner135755d2009-08-27 03:51:50 +000077 CI->replaceAllUsesWith(II);
78
Chris Lattner135755d2009-08-27 03:51:50 +000079 // Delete the unconditional branch inserted by splitBasicBlock
80 BB->getInstList().pop_back();
81 Split->getInstList().pop_front(); // Delete the original call
82
83 // Update any PHI nodes in the exceptional block to indicate that
84 // there is now a new entry in them.
85 unsigned i = 0;
86 for (BasicBlock::iterator I = InvokeDest->begin();
87 isa<PHINode>(I); ++I, ++i)
88 cast<PHINode>(I)->addIncoming(InvokeDestPHIValues[i], BB);
89
90 // This basic block is now complete, the caller will continue scanning the
91 // next one.
92 return;
93 }
94}
95
96
Chris Lattnercd4d3392006-01-13 19:05:59 +000097/// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
98/// in the body of the inlined function into invokes and turn unwind
99/// instructions into branches to the invoke unwind dest.
100///
Nick Lewyckydac5c4b2009-02-03 04:34:40 +0000101/// II is the invoke instruction being inlined. FirstNewBlock is the first
Chris Lattnercd4d3392006-01-13 19:05:59 +0000102/// block of the inlined code (the last block is the end of the function),
103/// and InlineCodeInfo is information about the code that got inlined.
104static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
Chris Lattner81dfb382009-09-01 18:44:06 +0000105 ClonedCodeInfo &InlinedCodeInfo) {
Chris Lattnercd4d3392006-01-13 19:05:59 +0000106 BasicBlock *InvokeDest = II->getUnwindDest();
Chris Lattner135755d2009-08-27 03:51:50 +0000107 SmallVector<Value*, 8> InvokeDestPHIValues;
Chris Lattnercd4d3392006-01-13 19:05:59 +0000108
109 // If there are PHI nodes in the unwind destination block, we need to
110 // keep track of which values came into them from this invoke, then remove
111 // the entry for this block.
112 BasicBlock *InvokeBlock = II->getParent();
113 for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) {
114 PHINode *PN = cast<PHINode>(I);
115 // Save the value to use for this edge.
116 InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock));
117 }
118
119 Function *Caller = FirstNewBlock->getParent();
Duncan Sandsa7212e52008-09-05 12:37:12 +0000120
Chris Lattnercd4d3392006-01-13 19:05:59 +0000121 // The inlined code is currently at the end of the function, scan from the
122 // start of the inlined code to its end, checking for stuff we need to
Chris Lattner135755d2009-08-27 03:51:50 +0000123 // rewrite. If the code doesn't have calls or unwinds, we know there is
124 // nothing to rewrite.
125 if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
126 // Now that everything is happy, we have one final detail. The PHI nodes in
127 // the exception destination block still have entries due to the original
128 // invoke instruction. Eliminate these entries (which might even delete the
129 // PHI node) now.
130 InvokeDest->removePredecessor(II->getParent());
131 return;
132 }
133
Chris Lattner135755d2009-08-27 03:51:50 +0000134 for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
135 if (InlinedCodeInfo.ContainsCalls)
136 HandleCallsInBlockInlinedThroughInvoke(BB, InvokeDest,
Chris Lattner81dfb382009-09-01 18:44:06 +0000137 InvokeDestPHIValues);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000138
Chris Lattner135755d2009-08-27 03:51:50 +0000139 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
140 // An UnwindInst requires special handling when it gets inlined into an
141 // invoke site. Once this happens, we know that the unwind would cause
142 // a control transfer to the invoke exception destination, so we can
143 // transform it into a direct branch to the exception destination.
144 BranchInst::Create(InvokeDest, UI);
Chris Lattnercd4d3392006-01-13 19:05:59 +0000145
Chris Lattner135755d2009-08-27 03:51:50 +0000146 // Delete the unwind instruction!
147 UI->eraseFromParent();
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000148
Chris Lattner135755d2009-08-27 03:51:50 +0000149 // Update any PHI nodes in the exceptional block to indicate that
150 // there is now a new entry in them.
151 unsigned i = 0;
152 for (BasicBlock::iterator I = InvokeDest->begin();
153 isa<PHINode>(I); ++I, ++i) {
154 PHINode *PN = cast<PHINode>(I);
155 PN->addIncoming(InvokeDestPHIValues[i], BB);
Chris Lattnercd4d3392006-01-13 19:05:59 +0000156 }
157 }
158 }
159
160 // Now that everything is happy, we have one final detail. The PHI nodes in
161 // the exception destination block still have entries due to the original
162 // invoke instruction. Eliminate these entries (which might even delete the
163 // PHI node) now.
164 InvokeDest->removePredecessor(II->getParent());
165}
166
Chris Lattnerd85340f2006-07-12 18:29:36 +0000167/// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
168/// into the caller, update the specified callgraph to reflect the changes we
169/// made. Note that it's possible that not all code was copied over, so only
Duncan Sandsd7b98512008-09-08 11:05:51 +0000170/// some edges of the callgraph may remain.
171static void UpdateCallGraphAfterInlining(CallSite CS,
Chris Lattnerd85340f2006-07-12 18:29:36 +0000172 Function::iterator FirstNewBlock,
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000173 ValueToValueMapTy &VMap,
Chris Lattnerfe9af3b2010-04-22 23:37:35 +0000174 InlineFunctionInfo &IFI) {
175 CallGraph &CG = *IFI.CG;
Duncan Sandsd7b98512008-09-08 11:05:51 +0000176 const Function *Caller = CS.getInstruction()->getParent()->getParent();
177 const Function *Callee = CS.getCalledFunction();
Chris Lattner468fb1d2006-01-14 20:07:50 +0000178 CallGraphNode *CalleeNode = CG[Callee];
179 CallGraphNode *CallerNode = CG[Caller];
Duncan Sandsa7212e52008-09-05 12:37:12 +0000180
Chris Lattnerd85340f2006-07-12 18:29:36 +0000181 // Since we inlined some uninlined call sites in the callee into the caller,
Chris Lattner468fb1d2006-01-14 20:07:50 +0000182 // add edges from the caller to all of the callees of the callee.
Gabor Greifc478e522009-01-15 18:40:09 +0000183 CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
184
185 // Consider the case where CalleeNode == CallerNode.
Gabor Greif12532982009-01-17 00:09:08 +0000186 CallGraphNode::CalledFunctionsVector CallCache;
Gabor Greifc478e522009-01-15 18:40:09 +0000187 if (CalleeNode == CallerNode) {
188 CallCache.assign(I, E);
189 I = CallCache.begin();
190 E = CallCache.end();
191 }
192
193 for (; I != E; ++I) {
Chris Lattnera541b0f2009-09-01 06:31:31 +0000194 const Value *OrigCall = I->first;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000195
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000196 ValueToValueMapTy::iterator VMI = VMap.find(OrigCall);
Chris Lattner981418b2006-07-12 21:37:11 +0000197 // Only copy the edge if the call was inlined!
Devang Patel29d3dd82010-06-23 23:55:51 +0000198 if (VMI == VMap.end() || VMI->second == 0)
Chris Lattner135755d2009-08-27 03:51:50 +0000199 continue;
200
201 // If the call was inlined, but then constant folded, there is no edge to
202 // add. Check for this case.
Chris Lattnerb957a5e2010-04-22 21:31:00 +0000203 Instruction *NewCall = dyn_cast<Instruction>(VMI->second);
204 if (NewCall == 0) continue;
Chris Lattner0ca2f282010-05-01 01:26:13 +0000205
206 // Remember that this call site got inlined for the client of
207 // InlineFunction.
208 IFI.InlinedCalls.push_back(NewCall);
209
Chris Lattnerb957a5e2010-04-22 21:31:00 +0000210 // It's possible that inlining the callsite will cause it to go from an
211 // indirect to a direct call by resolving a function pointer. If this
212 // happens, set the callee of the new call site to a more precise
213 // destination. This can also happen if the call graph node of the caller
214 // was just unnecessarily imprecise.
215 if (I->second->getFunction() == 0)
216 if (Function *F = CallSite(NewCall).getCalledFunction()) {
217 // Indirect call site resolved to direct call.
Gabor Greif86099342010-07-27 15:02:37 +0000218 CallerNode->addCalledFunction(CallSite(NewCall), CG[F]);
219
Chris Lattnerb957a5e2010-04-22 21:31:00 +0000220 continue;
221 }
Gabor Greif86099342010-07-27 15:02:37 +0000222
223 CallerNode->addCalledFunction(CallSite(NewCall), I->second);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000224 }
Chris Lattner135755d2009-08-27 03:51:50 +0000225
Dale Johannesen39fa3242009-01-13 22:43:37 +0000226 // Update the call graph by deleting the edge from Callee to Caller. We must
227 // do this after the loop above in case Caller and Callee are the same.
228 CallerNode->removeCallEdgeFor(CS);
Chris Lattner468fb1d2006-01-14 20:07:50 +0000229}
230
Chris Lattnerca398dc2003-05-29 15:11:31 +0000231// InlineFunction - This function inlines the called function into the basic
232// block of the caller. This returns false if it is not possible to inline this
233// call. The program is still in a well defined state if this occurs though.
234//
Misha Brukmanfd939082005-04-21 23:48:37 +0000235// Note that this only does one level of inlining. For example, if the
236// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
Chris Lattnerca398dc2003-05-29 15:11:31 +0000237// exists in the instruction stream. Similiarly this will inline a recursive
238// function by one level.
239//
Chris Lattner60915142010-04-22 23:07:58 +0000240bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
Chris Lattner80a38d22003-08-24 06:59:16 +0000241 Instruction *TheCall = CS.getInstruction();
Owen Andersone922c022009-07-22 00:24:57 +0000242 LLVMContext &Context = TheCall->getContext();
Chris Lattner80a38d22003-08-24 06:59:16 +0000243 assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
244 "Instruction not in function!");
Chris Lattnerca398dc2003-05-29 15:11:31 +0000245
Chris Lattner60915142010-04-22 23:07:58 +0000246 // If IFI has any state in it, zap it before we fill it in.
247 IFI.reset();
248
Chris Lattner80a38d22003-08-24 06:59:16 +0000249 const Function *CalledFunc = CS.getCalledFunction();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000250 if (CalledFunc == 0 || // Can't inline external function or indirect
Reid Spencer5cbf9852007-01-30 20:08:39 +0000251 CalledFunc->isDeclaration() || // call, or call to a vararg function!
Eric Christopher0623e902010-03-24 23:35:21 +0000252 CalledFunc->getFunctionType()->isVarArg()) return false;
Chris Lattnerca398dc2003-05-29 15:11:31 +0000253
Chris Lattner1b491412005-05-06 06:47:52 +0000254
Chris Lattneraf9985c2009-02-12 07:06:42 +0000255 // If the call to the callee is not a tail call, we must clear the 'tail'
Chris Lattner1b491412005-05-06 06:47:52 +0000256 // flags on any calls that we inline.
257 bool MustClearTailCallFlags =
Chris Lattneraf9985c2009-02-12 07:06:42 +0000258 !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
Chris Lattner1b491412005-05-06 06:47:52 +0000259
Duncan Sandsf0c33542007-12-19 21:13:37 +0000260 // If the call to the callee cannot throw, set the 'nounwind' flag on any
261 // calls that we inline.
262 bool MarkNoUnwind = CS.doesNotThrow();
263
Chris Lattner80a38d22003-08-24 06:59:16 +0000264 BasicBlock *OrigBB = TheCall->getParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000265 Function *Caller = OrigBB->getParent();
266
Gordon Henriksen0e138212007-12-25 03:10:07 +0000267 // GC poses two hazards to inlining, which only occur when the callee has GC:
268 // 1. If the caller has no GC, then the callee's GC must be propagated to the
269 // caller.
270 // 2. If the caller has a differing GC, it is invalid to inline.
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000271 if (CalledFunc->hasGC()) {
272 if (!Caller->hasGC())
273 Caller->setGC(CalledFunc->getGC());
274 else if (CalledFunc->getGC() != Caller->getGC())
Gordon Henriksen0e138212007-12-25 03:10:07 +0000275 return false;
276 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000277
Chris Lattner5052c912004-02-04 01:41:09 +0000278 // Get an iterator to the last basic block in the function, which will have
279 // the new function inlined after it.
280 //
281 Function::iterator LastBlock = &Caller->back();
282
Chris Lattner5e923de2004-02-04 02:51:48 +0000283 // Make sure to capture all of the return instructions from the cloned
Chris Lattnerca398dc2003-05-29 15:11:31 +0000284 // function.
Chris Lattnerec1bea02009-08-27 04:02:30 +0000285 SmallVector<ReturnInst*, 8> Returns;
Chris Lattnercd4d3392006-01-13 19:05:59 +0000286 ClonedCodeInfo InlinedFunctionInfo;
Dale Johannesen0744f092009-03-04 02:09:48 +0000287 Function::iterator FirstNewBlock;
Duncan Sandsf0c33542007-12-19 21:13:37 +0000288
Devang Patel29d3dd82010-06-23 23:55:51 +0000289 { // Scope to destroy VMap after cloning.
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000290 ValueToValueMapTy VMap;
Chris Lattner5b5bc302006-05-27 01:28:04 +0000291
Dan Gohman9614fcc2008-06-20 17:11:32 +0000292 assert(CalledFunc->arg_size() == CS.arg_size() &&
Chris Lattner5e923de2004-02-04 02:51:48 +0000293 "No varargs calls can be inlined!");
Duncan Sandsa7212e52008-09-05 12:37:12 +0000294
Chris Lattnerc93adca2008-01-11 06:09:30 +0000295 // Calculate the vector of arguments to pass into the function cloner, which
296 // matches up the formal to the actual argument values.
Chris Lattner5e923de2004-02-04 02:51:48 +0000297 CallSite::arg_iterator AI = CS.arg_begin();
Chris Lattnerc93adca2008-01-11 06:09:30 +0000298 unsigned ArgNo = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +0000299 for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
Chris Lattnerc93adca2008-01-11 06:09:30 +0000300 E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
301 Value *ActualArg = *AI;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000302
Duncan Sandsd82375c2008-01-27 18:12:58 +0000303 // When byval arguments actually inlined, we need to make the copy implied
304 // by them explicit. However, we don't do this if the callee is readonly
305 // or readnone, because the copy would be unneeded: the callee doesn't
306 // modify the struct.
Devang Patel05988662008-09-25 21:00:45 +0000307 if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal) &&
Duncan Sandsd82375c2008-01-27 18:12:58 +0000308 !CalledFunc->onlyReadsMemory()) {
Chris Lattnerc93adca2008-01-11 06:09:30 +0000309 const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
Owen Anderson1d0be152009-08-13 21:58:54 +0000310 const Type *VoidPtrTy =
Duncan Sandsac53a0b2009-10-06 15:40:36 +0000311 Type::getInt8PtrTy(Context);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000312
Chris Lattnerc93adca2008-01-11 06:09:30 +0000313 // Create the alloca. If we have TargetData, use nice alignment.
314 unsigned Align = 1;
Chris Lattner60915142010-04-22 23:07:58 +0000315 if (IFI.TD) Align = IFI.TD->getPrefTypeAlignment(AggTy);
Owen Anderson50dead02009-07-15 23:53:25 +0000316 Value *NewAlloca = new AllocaInst(AggTy, 0, Align,
Owen Anderson9adc0ab2009-07-14 23:09:55 +0000317 I->getName(),
318 &*Caller->begin()->begin());
Chris Lattnerc93adca2008-01-11 06:09:30 +0000319 // Emit a memcpy.
Mon P Wang20adc9d2010-04-04 03:10:48 +0000320 const Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)};
Chris Lattnerc93adca2008-01-11 06:09:30 +0000321 Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
Chris Lattner824b9582008-11-21 16:42:48 +0000322 Intrinsic::memcpy,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000323 Tys, 3);
Chris Lattnerc93adca2008-01-11 06:09:30 +0000324 Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
325 Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000326
Chris Lattnerc93adca2008-01-11 06:09:30 +0000327 Value *Size;
Chris Lattner60915142010-04-22 23:07:58 +0000328 if (IFI.TD == 0)
Owen Andersonbaf3c402009-07-29 18:55:55 +0000329 Size = ConstantExpr::getSizeOf(AggTy);
Chris Lattnerc93adca2008-01-11 06:09:30 +0000330 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000331 Size = ConstantInt::get(Type::getInt64Ty(Context),
Chris Lattner60915142010-04-22 23:07:58 +0000332 IFI.TD->getTypeStoreSize(AggTy));
Duncan Sandsa7212e52008-09-05 12:37:12 +0000333
Chris Lattnerc93adca2008-01-11 06:09:30 +0000334 // Always generate a memcpy of alignment 1 here because we don't know
335 // the alignment of the src pointer. Other optimizations can infer
336 // better alignment.
337 Value *CallArgs[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000338 DestCast, SrcCast, Size,
Mon P Wang20adc9d2010-04-04 03:10:48 +0000339 ConstantInt::get(Type::getInt32Ty(Context), 1),
340 ConstantInt::get(Type::getInt1Ty(Context), 0)
Chris Lattnerc93adca2008-01-11 06:09:30 +0000341 };
342 CallInst *TheMemCpy =
Mon P Wang20adc9d2010-04-04 03:10:48 +0000343 CallInst::Create(MemCpyFn, CallArgs, CallArgs+5, "", TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000344
Chris Lattnerc93adca2008-01-11 06:09:30 +0000345 // If we have a call graph, update it.
Chris Lattner60915142010-04-22 23:07:58 +0000346 if (CallGraph *CG = IFI.CG) {
Chris Lattnerc93adca2008-01-11 06:09:30 +0000347 CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn);
348 CallGraphNode *CallerNode = (*CG)[Caller];
349 CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN);
350 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000351
Chris Lattnerc93adca2008-01-11 06:09:30 +0000352 // Uses of the argument in the function should use our new alloca
353 // instead.
354 ActualArg = NewAlloca;
Duncan Sands2914ba62010-05-31 21:00:26 +0000355
356 // Calls that we inline may use the new alloca, so we need to clear
357 // their 'tail' flags.
358 MustClearTailCallFlags = true;
Chris Lattnerc93adca2008-01-11 06:09:30 +0000359 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000360
Devang Patel29d3dd82010-06-23 23:55:51 +0000361 VMap[I] = ActualArg;
Chris Lattnerc93adca2008-01-11 06:09:30 +0000362 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000363
Chris Lattner5b5bc302006-05-27 01:28:04 +0000364 // We want the inliner to prune the code as it copies. We would LOVE to
365 // have no dead or constant instructions leftover after inlining occurs
366 // (which can happen, e.g., because an argument was constant), but we'll be
367 // happy with whatever the cloner can do.
Dan Gohman6cb8c232010-08-26 15:41:53 +0000368 CloneAndPruneFunctionInto(Caller, CalledFunc, VMap,
369 /*ModuleLevelChanges=*/false, Returns, ".i",
Chris Lattner60915142010-04-22 23:07:58 +0000370 &InlinedFunctionInfo, IFI.TD, TheCall);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000371
Chris Lattnerd85340f2006-07-12 18:29:36 +0000372 // Remember the first block that is newly cloned over.
373 FirstNewBlock = LastBlock; ++FirstNewBlock;
Duncan Sandsa7212e52008-09-05 12:37:12 +0000374
Chris Lattnerd85340f2006-07-12 18:29:36 +0000375 // Update the callgraph if requested.
Chris Lattner60915142010-04-22 23:07:58 +0000376 if (IFI.CG)
Devang Patel29d3dd82010-06-23 23:55:51 +0000377 UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI);
Misha Brukmanfd939082005-04-21 23:48:37 +0000378 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000379
Chris Lattnerca398dc2003-05-29 15:11:31 +0000380 // If there are any alloca instructions in the block that used to be the entry
381 // block for the callee, move them to the entry block of the caller. First
382 // calculate which instruction they should be inserted before. We insert the
383 // instructions at the end of the current alloca list.
384 //
Chris Lattner21f20552006-01-13 18:16:48 +0000385 {
Chris Lattner80a38d22003-08-24 06:59:16 +0000386 BasicBlock::iterator InsertPoint = Caller->begin()->begin();
Chris Lattner5e923de2004-02-04 02:51:48 +0000387 for (BasicBlock::iterator I = FirstNewBlock->begin(),
Chris Lattner135755d2009-08-27 03:51:50 +0000388 E = FirstNewBlock->end(); I != E; ) {
389 AllocaInst *AI = dyn_cast<AllocaInst>(I++);
390 if (AI == 0) continue;
391
392 // If the alloca is now dead, remove it. This often occurs due to code
393 // specialization.
394 if (AI->use_empty()) {
395 AI->eraseFromParent();
396 continue;
Chris Lattner33bb3c82006-09-13 19:23:57 +0000397 }
Chris Lattner135755d2009-08-27 03:51:50 +0000398
399 if (!isa<Constant>(AI->getArraySize()))
400 continue;
401
Chris Lattner8f2718f2009-08-27 04:20:52 +0000402 // Keep track of the static allocas that we inline into the caller if the
403 // StaticAllocas pointer is non-null.
Chris Lattner60915142010-04-22 23:07:58 +0000404 IFI.StaticAllocas.push_back(AI);
Chris Lattner8f2718f2009-08-27 04:20:52 +0000405
Chris Lattner135755d2009-08-27 03:51:50 +0000406 // Scan for the block of allocas that we can move over, and move them
407 // all at once.
408 while (isa<AllocaInst>(I) &&
Chris Lattner8f2718f2009-08-27 04:20:52 +0000409 isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
Chris Lattner60915142010-04-22 23:07:58 +0000410 IFI.StaticAllocas.push_back(cast<AllocaInst>(I));
Chris Lattner135755d2009-08-27 03:51:50 +0000411 ++I;
Chris Lattner8f2718f2009-08-27 04:20:52 +0000412 }
Chris Lattner135755d2009-08-27 03:51:50 +0000413
414 // Transfer all of the allocas over in a block. Using splice means
415 // that the instructions aren't removed from the symbol table, then
416 // reinserted.
417 Caller->getEntryBlock().getInstList().splice(InsertPoint,
418 FirstNewBlock->getInstList(),
419 AI, I);
420 }
Chris Lattner80a38d22003-08-24 06:59:16 +0000421 }
Chris Lattnerca398dc2003-05-29 15:11:31 +0000422
Chris Lattnerbf229f42006-01-13 19:34:14 +0000423 // If the inlined code contained dynamic alloca instructions, wrap the inlined
424 // code with llvm.stacksave/llvm.stackrestore intrinsics.
425 if (InlinedFunctionInfo.ContainsDynamicAllocas) {
426 Module *M = Caller->getParent();
Chris Lattnerbf229f42006-01-13 19:34:14 +0000427 // Get the two intrinsics we care about.
Chris Lattner6128df52009-10-17 05:39:39 +0000428 Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
429 Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000430
431 // If we are preserving the callgraph, add edges to the stacksave/restore
432 // functions for the calls we insert.
Chris Lattner21ba23d2006-07-18 21:48:57 +0000433 CallGraphNode *StackSaveCGN = 0, *StackRestoreCGN = 0, *CallerNode = 0;
Chris Lattner60915142010-04-22 23:07:58 +0000434 if (CallGraph *CG = IFI.CG) {
Chris Lattner6128df52009-10-17 05:39:39 +0000435 StackSaveCGN = CG->getOrInsertFunction(StackSave);
436 StackRestoreCGN = CG->getOrInsertFunction(StackRestore);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000437 CallerNode = (*CG)[Caller];
438 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000439
Chris Lattnerbf229f42006-01-13 19:34:14 +0000440 // Insert the llvm.stacksave.
Duncan Sandsa7212e52008-09-05 12:37:12 +0000441 CallInst *SavedPtr = CallInst::Create(StackSave, "savedstack",
Gabor Greif051a9502008-04-06 20:25:17 +0000442 FirstNewBlock->begin());
Chris Lattner60915142010-04-22 23:07:58 +0000443 if (IFI.CG) CallerNode->addCalledFunction(SavedPtr, StackSaveCGN);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000444
Chris Lattnerbf229f42006-01-13 19:34:14 +0000445 // Insert a call to llvm.stackrestore before any return instructions in the
446 // inlined function.
Chris Lattnerd85340f2006-07-12 18:29:36 +0000447 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
Gabor Greif051a9502008-04-06 20:25:17 +0000448 CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", Returns[i]);
Chris Lattner60915142010-04-22 23:07:58 +0000449 if (IFI.CG) CallerNode->addCalledFunction(CI, StackRestoreCGN);
Chris Lattnerd85340f2006-07-12 18:29:36 +0000450 }
Chris Lattner468fb1d2006-01-14 20:07:50 +0000451
452 // Count the number of StackRestore calls we insert.
453 unsigned NumStackRestores = Returns.size();
Duncan Sandsa7212e52008-09-05 12:37:12 +0000454
Chris Lattnerbf229f42006-01-13 19:34:14 +0000455 // If we are inlining an invoke instruction, insert restores before each
456 // unwind. These unwinds will be rewritten into branches later.
457 if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
458 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
459 BB != E; ++BB)
Chris Lattner468fb1d2006-01-14 20:07:50 +0000460 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
Chris Lattner6128df52009-10-17 05:39:39 +0000461 CallInst *CI = CallInst::Create(StackRestore, SavedPtr, "", UI);
Chris Lattner60915142010-04-22 23:07:58 +0000462 if (IFI.CG) CallerNode->addCalledFunction(CI, StackRestoreCGN);
Chris Lattner468fb1d2006-01-14 20:07:50 +0000463 ++NumStackRestores;
464 }
465 }
Chris Lattnerbf229f42006-01-13 19:34:14 +0000466 }
467
Duncan Sandsa7212e52008-09-05 12:37:12 +0000468 // If we are inlining tail call instruction through a call site that isn't
Chris Lattner1fdf4a82006-01-13 19:18:11 +0000469 // marked 'tail', we must remove the tail marker for any calls in the inlined
Duncan Sandsf0c33542007-12-19 21:13:37 +0000470 // code. Also, calls inlined through a 'nounwind' call site should be marked
471 // 'nounwind'.
472 if (InlinedFunctionInfo.ContainsCalls &&
473 (MustClearTailCallFlags || MarkNoUnwind)) {
Chris Lattner1b491412005-05-06 06:47:52 +0000474 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
475 BB != E; ++BB)
476 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Duncan Sandsf0c33542007-12-19 21:13:37 +0000477 if (CallInst *CI = dyn_cast<CallInst>(I)) {
478 if (MustClearTailCallFlags)
479 CI->setTailCall(false);
480 if (MarkNoUnwind)
481 CI->setDoesNotThrow();
482 }
Chris Lattner1b491412005-05-06 06:47:52 +0000483 }
484
Duncan Sandsf0c33542007-12-19 21:13:37 +0000485 // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
486 // instructions are unreachable.
487 if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
488 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
489 BB != E; ++BB) {
490 TerminatorInst *Term = BB->getTerminator();
491 if (isa<UnwindInst>(Term)) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000492 new UnreachableInst(Context, Term);
Duncan Sandsf0c33542007-12-19 21:13:37 +0000493 BB->getInstList().erase(Term);
494 }
495 }
496
Chris Lattner5e923de2004-02-04 02:51:48 +0000497 // If we are inlining for an invoke instruction, we must make sure to rewrite
498 // any inlined 'unwind' instructions into branches to the invoke exception
499 // destination, and call instructions into invoke instructions.
Chris Lattnercd4d3392006-01-13 19:05:59 +0000500 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
Chris Lattner81dfb382009-09-01 18:44:06 +0000501 HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
Chris Lattner5e923de2004-02-04 02:51:48 +0000502
Chris Lattner44a68072004-02-04 04:17:06 +0000503 // If we cloned in _exactly one_ basic block, and if that block ends in a
504 // return instruction, we splice the body of the inlined callee directly into
505 // the calling basic block.
506 if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
507 // Move all of the instructions right before the call.
508 OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
509 FirstNewBlock->begin(), FirstNewBlock->end());
510 // Remove the cloned basic block.
511 Caller->getBasicBlockList().pop_back();
Misha Brukmanfd939082005-04-21 23:48:37 +0000512
Chris Lattner44a68072004-02-04 04:17:06 +0000513 // If the call site was an invoke instruction, add a branch to the normal
514 // destination.
515 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
Gabor Greif051a9502008-04-06 20:25:17 +0000516 BranchInst::Create(II->getNormalDest(), TheCall);
Chris Lattner44a68072004-02-04 04:17:06 +0000517
518 // If the return instruction returned a value, replace uses of the call with
519 // uses of the returned value.
Devang Pateldc00d422008-03-04 21:15:15 +0000520 if (!TheCall->use_empty()) {
521 ReturnInst *R = Returns[0];
Eli Friedman5877ad72009-05-08 00:22:04 +0000522 if (TheCall == R->getReturnValue())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000523 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Eli Friedman5877ad72009-05-08 00:22:04 +0000524 else
525 TheCall->replaceAllUsesWith(R->getReturnValue());
Devang Pateldc00d422008-03-04 21:15:15 +0000526 }
Chris Lattner44a68072004-02-04 04:17:06 +0000527 // Since we are now done with the Call/Invoke, we can delete it.
Dan Gohman1adec832008-06-21 22:08:46 +0000528 TheCall->eraseFromParent();
Chris Lattner44a68072004-02-04 04:17:06 +0000529
530 // Since we are now done with the return instruction, delete it also.
Dan Gohman1adec832008-06-21 22:08:46 +0000531 Returns[0]->eraseFromParent();
Chris Lattner44a68072004-02-04 04:17:06 +0000532
533 // We are now done with the inlining.
534 return true;
535 }
536
537 // Otherwise, we have the normal case, of more than one block to inline or
538 // multiple return sites.
539
Chris Lattner5e923de2004-02-04 02:51:48 +0000540 // We want to clone the entire callee function into the hole between the
541 // "starter" and "ender" blocks. How we accomplish this depends on whether
542 // this is an invoke instruction or a call instruction.
543 BasicBlock *AfterCallBB;
544 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000545
Chris Lattner5e923de2004-02-04 02:51:48 +0000546 // Add an unconditional branch to make this look like the CallInst case...
Gabor Greif051a9502008-04-06 20:25:17 +0000547 BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
Misha Brukmanfd939082005-04-21 23:48:37 +0000548
Chris Lattner5e923de2004-02-04 02:51:48 +0000549 // Split the basic block. This guarantees that no PHI nodes will have to be
550 // updated due to new incoming edges, and make the invoke case more
551 // symmetric to the call case.
552 AfterCallBB = OrigBB->splitBasicBlock(NewBr,
Chris Lattner284d1b82004-12-11 16:59:54 +0000553 CalledFunc->getName()+".exit");
Misha Brukmanfd939082005-04-21 23:48:37 +0000554
Chris Lattner5e923de2004-02-04 02:51:48 +0000555 } else { // It's a call
Chris Lattner44a68072004-02-04 04:17:06 +0000556 // If this is a call instruction, we need to split the basic block that
557 // the call lives in.
Chris Lattner5e923de2004-02-04 02:51:48 +0000558 //
559 AfterCallBB = OrigBB->splitBasicBlock(TheCall,
Chris Lattner284d1b82004-12-11 16:59:54 +0000560 CalledFunc->getName()+".exit");
Chris Lattner5e923de2004-02-04 02:51:48 +0000561 }
562
Chris Lattner44a68072004-02-04 04:17:06 +0000563 // Change the branch that used to go to AfterCallBB to branch to the first
564 // basic block of the inlined function.
565 //
566 TerminatorInst *Br = OrigBB->getTerminator();
Misha Brukmanfd939082005-04-21 23:48:37 +0000567 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner44a68072004-02-04 04:17:06 +0000568 "splitBasicBlock broken!");
569 Br->setOperand(0, FirstNewBlock);
570
571
572 // Now that the function is correct, make it a little bit nicer. In
573 // particular, move the basic blocks inserted from the end of the function
574 // into the space made by splitting the source basic block.
Chris Lattner44a68072004-02-04 04:17:06 +0000575 Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
576 FirstNewBlock, Caller->end());
577
Chris Lattner5e923de2004-02-04 02:51:48 +0000578 // Handle all of the return instructions that we just cloned in, and eliminate
579 // any users of the original call/invoke instruction.
Devang Patelb8f198a2008-03-10 18:34:00 +0000580 const Type *RTy = CalledFunc->getReturnType();
Dan Gohman2c317502008-06-20 01:03:44 +0000581
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000582 if (Returns.size() > 1) {
Chris Lattner5e923de2004-02-04 02:51:48 +0000583 // The PHI node should go at the front of the new basic block to merge all
584 // possible incoming values.
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000585 PHINode *PHI = 0;
Chris Lattner5e923de2004-02-04 02:51:48 +0000586 if (!TheCall->use_empty()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000587 PHI = PHINode::Create(RTy, TheCall->getName(),
588 AfterCallBB->begin());
589 // Anything that used the result of the function call should now use the
590 // PHI node as their operand.
Duncan Sandsa7212e52008-09-05 12:37:12 +0000591 TheCall->replaceAllUsesWith(PHI);
Chris Lattner5e923de2004-02-04 02:51:48 +0000592 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000593
Gabor Greifc478e522009-01-15 18:40:09 +0000594 // Loop over all of the return instructions adding entries to the PHI node
595 // as appropriate.
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000596 if (PHI) {
597 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
598 ReturnInst *RI = Returns[i];
599 assert(RI->getReturnValue()->getType() == PHI->getType() &&
600 "Ret value not consistent in function!");
601 PHI->addIncoming(RI->getReturnValue(), RI->getParent());
Devang Patel12a466b2008-03-07 20:06:16 +0000602 }
Chris Lattnerc581acb2009-10-27 05:39:41 +0000603
604 // Now that we inserted the PHI, check to see if it has a single value
605 // (e.g. all the entries are the same or undef). If so, remove the PHI so
606 // it doesn't block other optimizations.
607 if (Value *V = PHI->hasConstantValue()) {
608 PHI->replaceAllUsesWith(V);
609 PHI->eraseFromParent();
610 }
Devang Patel12a466b2008-03-07 20:06:16 +0000611 }
612
Chris Lattnerc581acb2009-10-27 05:39:41 +0000613
Gabor Greifde62aea2009-01-16 23:08:50 +0000614 // Add a branch to the merge points and remove return instructions.
Chris Lattner5e923de2004-02-04 02:51:48 +0000615 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
616 ReturnInst *RI = Returns[i];
Dale Johannesen0744f092009-03-04 02:09:48 +0000617 BranchInst::Create(AfterCallBB, RI);
Devang Patelb8f198a2008-03-10 18:34:00 +0000618 RI->eraseFromParent();
Chris Lattner5e923de2004-02-04 02:51:48 +0000619 }
Devang Patelb8f198a2008-03-10 18:34:00 +0000620 } else if (!Returns.empty()) {
621 // Otherwise, if there is exactly one return value, just replace anything
622 // using the return value of the call with the computed value.
Eli Friedman5877ad72009-05-08 00:22:04 +0000623 if (!TheCall->use_empty()) {
624 if (TheCall == Returns[0]->getReturnValue())
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000625 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Eli Friedman5877ad72009-05-08 00:22:04 +0000626 else
627 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
628 }
Duncan Sandsa7212e52008-09-05 12:37:12 +0000629
Devang Patelb8f198a2008-03-10 18:34:00 +0000630 // Splice the code from the return block into the block that it will return
631 // to, which contains the code that was after the call.
632 BasicBlock *ReturnBB = Returns[0]->getParent();
633 AfterCallBB->getInstList().splice(AfterCallBB->begin(),
634 ReturnBB->getInstList());
Duncan Sandsa7212e52008-09-05 12:37:12 +0000635
Devang Patelb8f198a2008-03-10 18:34:00 +0000636 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
637 ReturnBB->replaceAllUsesWith(AfterCallBB);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000638
Devang Patelb8f198a2008-03-10 18:34:00 +0000639 // Delete the return instruction now and empty ReturnBB now.
640 Returns[0]->eraseFromParent();
641 ReturnBB->eraseFromParent();
Chris Lattner3787e762004-10-17 23:21:07 +0000642 } else if (!TheCall->use_empty()) {
643 // No returns, but something is using the return value of the call. Just
644 // nuke the result.
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000645 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
Chris Lattner5e923de2004-02-04 02:51:48 +0000646 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000647
Chris Lattner5e923de2004-02-04 02:51:48 +0000648 // Since we are now done with the Call/Invoke, we can delete it.
Chris Lattner3787e762004-10-17 23:21:07 +0000649 TheCall->eraseFromParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000650
Chris Lattner7152c232003-08-24 04:06:56 +0000651 // We should always be able to fold the entry block of the function into the
652 // single predecessor of the block...
Chris Lattnercd01ae52004-04-16 05:17:59 +0000653 assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
Chris Lattner7152c232003-08-24 04:06:56 +0000654 BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
Chris Lattner44a68072004-02-04 04:17:06 +0000655
Chris Lattnercd01ae52004-04-16 05:17:59 +0000656 // Splice the code entry block into calling block, right before the
657 // unconditional branch.
658 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
659 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
660
661 // Remove the unconditional branch.
662 OrigBB->getInstList().erase(Br);
663
664 // Now we can remove the CalleeEntry block, which is now empty.
665 Caller->getBasicBlockList().erase(CalleeEntry);
Duncan Sandsa7212e52008-09-05 12:37:12 +0000666
Chris Lattnerca398dc2003-05-29 15:11:31 +0000667 return true;
668}