1 //===- InlineFunction.cpp - Code to perform function inlining -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements inlining of a function into a call site, resolving
11 // parameters and the return value as appropriate.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/Utils/Cloning.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Intrinsics.h"
21 #include "llvm/Support/CallSite.h"
24 bool llvm::InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); }
25 bool llvm::InlineFunction(InvokeInst *II) {return InlineFunction(CallSite(II));}
27 /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
28 /// in the body of the inlined function into invokes and turn unwind
29 /// instructions into branches to the invoke unwind dest.
31 /// II is the invoke instruction begin inlined. FirstNewBlock is the first
32 /// block of the inlined code (the last block is the end of the function),
33 /// and InlineCodeInfo is information about the code that got inlined.
34 static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
35 ClonedCodeInfo &InlinedCodeInfo) {
36 BasicBlock *InvokeDest = II->getUnwindDest();
37 std::vector<Value*> InvokeDestPHIValues;
39 // If there are PHI nodes in the unwind destination block, we need to
40 // keep track of which values came into them from this invoke, then remove
41 // the entry for this block.
42 BasicBlock *InvokeBlock = II->getParent();
43 for (BasicBlock::iterator I = InvokeDest->begin(); isa<PHINode>(I); ++I) {
44 PHINode *PN = cast<PHINode>(I);
45 // Save the value to use for this edge.
46 InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(InvokeBlock));
49 Function *Caller = FirstNewBlock->getParent();
51 // The inlined code is currently at the end of the function, scan from the
52 // start of the inlined code to its end, checking for stuff we need to
54 if (InlinedCodeInfo.ContainsCalls || InlinedCodeInfo.ContainsUnwinds) {
55 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
57 if (InlinedCodeInfo.ContainsCalls) {
58 for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ){
59 Instruction *I = BBI++;
61 // We only need to check for function calls: inlined invoke
62 // instructions require no special handling.
63 if (!isa<CallInst>(I)) continue;
64 CallInst *CI = cast<CallInst>(I);
66 // If this is an intrinsic function call, don't convert it to an
68 if (CI->getCalledFunction() &&
69 CI->getCalledFunction()->getIntrinsicID())
72 // Convert this function call into an invoke instruction.
73 // First, split the basic block.
74 BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
76 // Next, create the new invoke instruction, inserting it at the end
77 // of the old basic block.
79 new InvokeInst(CI->getCalledValue(), Split, InvokeDest,
80 std::vector<Value*>(CI->op_begin()+1, CI->op_end()),
81 CI->getName(), BB->getTerminator());
82 II->setCallingConv(CI->getCallingConv());
84 // Make sure that anything using the call now uses the invoke!
85 CI->replaceAllUsesWith(II);
87 // Delete the unconditional branch inserted by splitBasicBlock
88 BB->getInstList().pop_back();
89 Split->getInstList().pop_front(); // Delete the original call
91 // Update any PHI nodes in the exceptional block to indicate that
92 // there is now a new entry in them.
94 for (BasicBlock::iterator I = InvokeDest->begin();
95 isa<PHINode>(I); ++I, ++i) {
96 PHINode *PN = cast<PHINode>(I);
97 PN->addIncoming(InvokeDestPHIValues[i], BB);
100 // This basic block is now complete, start scanning the next one.
105 if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
106 // An UnwindInst requires special handling when it gets inlined into an
107 // invoke site. Once this happens, we know that the unwind would cause
108 // a control transfer to the invoke exception destination, so we can
109 // transform it into a direct branch to the exception destination.
110 new BranchInst(InvokeDest, UI);
112 // Delete the unwind instruction!
113 UI->getParent()->getInstList().pop_back();
115 // Update any PHI nodes in the exceptional block to indicate that
116 // there is now a new entry in them.
118 for (BasicBlock::iterator I = InvokeDest->begin();
119 isa<PHINode>(I); ++I, ++i) {
120 PHINode *PN = cast<PHINode>(I);
121 PN->addIncoming(InvokeDestPHIValues[i], BB);
127 // Now that everything is happy, we have one final detail. The PHI nodes in
128 // the exception destination block still have entries due to the original
129 // invoke instruction. Eliminate these entries (which might even delete the
131 InvokeDest->removePredecessor(II->getParent());
135 // InlineFunction - This function inlines the called function into the basic
136 // block of the caller. This returns false if it is not possible to inline this
137 // call. The program is still in a well defined state if this occurs though.
139 // Note that this only does one level of inlining. For example, if the
140 // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
141 // exists in the instruction stream. Similiarly this will inline a recursive
142 // function by one level.
144 bool llvm::InlineFunction(CallSite CS) {
145 Instruction *TheCall = CS.getInstruction();
146 assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
147 "Instruction not in function!");
149 const Function *CalledFunc = CS.getCalledFunction();
150 if (CalledFunc == 0 || // Can't inline external function or indirect
151 CalledFunc->isExternal() || // call, or call to a vararg function!
152 CalledFunc->getFunctionType()->isVarArg()) return false;
155 // If the call to the callee is a non-tail call, we must clear the 'tail'
156 // flags on any calls that we inline.
157 bool MustClearTailCallFlags =
158 isa<CallInst>(TheCall) && !cast<CallInst>(TheCall)->isTailCall();
160 BasicBlock *OrigBB = TheCall->getParent();
161 Function *Caller = OrigBB->getParent();
163 // Get an iterator to the last basic block in the function, which will have
164 // the new function inlined after it.
166 Function::iterator LastBlock = &Caller->back();
168 // Make sure to capture all of the return instructions from the cloned
170 std::vector<ReturnInst*> Returns;
171 ClonedCodeInfo InlinedFunctionInfo;
172 { // Scope to destroy ValueMap after cloning.
173 // Calculate the vector of arguments to pass into the function cloner...
174 std::map<const Value*, Value*> ValueMap;
175 assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) ==
176 std::distance(CS.arg_begin(), CS.arg_end()) &&
177 "No varargs calls can be inlined!");
179 CallSite::arg_iterator AI = CS.arg_begin();
180 for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
181 E = CalledFunc->arg_end(); I != E; ++I, ++AI)
184 // Clone the entire body of the callee into the caller.
185 CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i",
186 &InlinedFunctionInfo);
189 // Remember the first block that is newly cloned over.
190 Function::iterator FirstNewBlock = LastBlock; ++FirstNewBlock;
192 // If there are any alloca instructions in the block that used to be the entry
193 // block for the callee, move them to the entry block of the caller. First
194 // calculate which instruction they should be inserted before. We insert the
195 // instructions at the end of the current alloca list.
198 BasicBlock::iterator InsertPoint = Caller->begin()->begin();
199 for (BasicBlock::iterator I = FirstNewBlock->begin(),
200 E = FirstNewBlock->end(); I != E; )
201 if (AllocaInst *AI = dyn_cast<AllocaInst>(I++))
202 if (isa<Constant>(AI->getArraySize())) {
203 // Scan for the block of allocas that we can move over, and move them
205 while (isa<AllocaInst>(I) &&
206 isa<Constant>(cast<AllocaInst>(I)->getArraySize()))
209 // Transfer all of the allocas over in a block. Using splice means
210 // that they instructions aren't removed from the symbol table, then
212 Caller->front().getInstList().splice(InsertPoint,
213 FirstNewBlock->getInstList(),
218 // If we are inlining tail call instruction through an invoke or
219 if (MustClearTailCallFlags) {
220 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
222 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
223 if (CallInst *CI = dyn_cast<CallInst>(I))
224 CI->setTailCall(false);
227 // If we are inlining for an invoke instruction, we must make sure to rewrite
228 // any inlined 'unwind' instructions into branches to the invoke exception
229 // destination, and call instructions into invoke instructions.
230 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
231 HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
233 // If we cloned in _exactly one_ basic block, and if that block ends in a
234 // return instruction, we splice the body of the inlined callee directly into
235 // the calling basic block.
236 if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
237 // Move all of the instructions right before the call.
238 OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
239 FirstNewBlock->begin(), FirstNewBlock->end());
240 // Remove the cloned basic block.
241 Caller->getBasicBlockList().pop_back();
243 // If the call site was an invoke instruction, add a branch to the normal
245 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
246 new BranchInst(II->getNormalDest(), TheCall);
248 // If the return instruction returned a value, replace uses of the call with
249 // uses of the returned value.
250 if (!TheCall->use_empty())
251 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
253 // Since we are now done with the Call/Invoke, we can delete it.
254 TheCall->getParent()->getInstList().erase(TheCall);
256 // Since we are now done with the return instruction, delete it also.
257 Returns[0]->getParent()->getInstList().erase(Returns[0]);
259 // We are now done with the inlining.
263 // Otherwise, we have the normal case, of more than one block to inline or
264 // multiple return sites.
266 // We want to clone the entire callee function into the hole between the
267 // "starter" and "ender" blocks. How we accomplish this depends on whether
268 // this is an invoke instruction or a call instruction.
269 BasicBlock *AfterCallBB;
270 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
272 // Add an unconditional branch to make this look like the CallInst case...
273 BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall);
275 // Split the basic block. This guarantees that no PHI nodes will have to be
276 // updated due to new incoming edges, and make the invoke case more
277 // symmetric to the call case.
278 AfterCallBB = OrigBB->splitBasicBlock(NewBr,
279 CalledFunc->getName()+".exit");
281 } else { // It's a call
282 // If this is a call instruction, we need to split the basic block that
283 // the call lives in.
285 AfterCallBB = OrigBB->splitBasicBlock(TheCall,
286 CalledFunc->getName()+".exit");
289 // Change the branch that used to go to AfterCallBB to branch to the first
290 // basic block of the inlined function.
292 TerminatorInst *Br = OrigBB->getTerminator();
293 assert(Br && Br->getOpcode() == Instruction::Br &&
294 "splitBasicBlock broken!");
295 Br->setOperand(0, FirstNewBlock);
298 // Now that the function is correct, make it a little bit nicer. In
299 // particular, move the basic blocks inserted from the end of the function
300 // into the space made by splitting the source basic block.
302 Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
303 FirstNewBlock, Caller->end());
305 // Handle all of the return instructions that we just cloned in, and eliminate
306 // any users of the original call/invoke instruction.
307 if (Returns.size() > 1) {
308 // The PHI node should go at the front of the new basic block to merge all
309 // possible incoming values.
312 if (!TheCall->use_empty()) {
313 PHI = new PHINode(CalledFunc->getReturnType(),
314 TheCall->getName(), AfterCallBB->begin());
316 // Anything that used the result of the function call should now use the
317 // PHI node as their operand.
319 TheCall->replaceAllUsesWith(PHI);
322 // Loop over all of the return instructions, turning them into unconditional
323 // branches to the merge point now, and adding entries to the PHI node as
325 for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
326 ReturnInst *RI = Returns[i];
329 assert(RI->getReturnValue() && "Ret should have value!");
330 assert(RI->getReturnValue()->getType() == PHI->getType() &&
331 "Ret value not consistent in function!");
332 PHI->addIncoming(RI->getReturnValue(), RI->getParent());
335 // Add a branch to the merge point where the PHI node lives if it exists.
336 new BranchInst(AfterCallBB, RI);
338 // Delete the return instruction now
339 RI->getParent()->getInstList().erase(RI);
342 } else if (!Returns.empty()) {
343 // Otherwise, if there is exactly one return value, just replace anything
344 // using the return value of the call with the computed value.
345 if (!TheCall->use_empty())
346 TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
348 // Splice the code from the return block into the block that it will return
349 // to, which contains the code that was after the call.
350 BasicBlock *ReturnBB = Returns[0]->getParent();
351 AfterCallBB->getInstList().splice(AfterCallBB->begin(),
352 ReturnBB->getInstList());
354 // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
355 ReturnBB->replaceAllUsesWith(AfterCallBB);
357 // Delete the return instruction now and empty ReturnBB now.
358 Returns[0]->eraseFromParent();
359 ReturnBB->eraseFromParent();
360 } else if (!TheCall->use_empty()) {
361 // No returns, but something is using the return value of the call. Just
363 TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
366 // Since we are now done with the Call/Invoke, we can delete it.
367 TheCall->eraseFromParent();
369 // We should always be able to fold the entry block of the function into the
370 // single predecessor of the block...
371 assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
372 BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
374 // Splice the code entry block into calling block, right before the
375 // unconditional branch.
376 OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
377 CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
379 // Remove the unconditional branch.
380 OrigBB->getInstList().erase(Br);
382 // Now we can remove the CalleeEntry block, which is now empty.
383 Caller->getBasicBlockList().erase(CalleeEntry);