InlineFunction.cpp revision f7703df4968084c18c248c1feea9961c19a32e6a
1//===- InlineFunction.cpp - Code to perform function inlining -------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements inlining of a function into a call site, resolving
11// parameters and the return value as appropriate.
12//
13// FIXME: This pass should transform alloca instructions in the called function
14//        into malloc/free pairs!  Or perhaps it should refuse to inline them!
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Transforms/Utils/Cloning.h"
19#include "llvm/Constant.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
22#include "llvm/Instructions.h"
23#include "llvm/Intrinsics.h"
24#include "llvm/Support/CallSite.h"
25#include "llvm/Transforms/Utils/Local.h"
26using namespace llvm;
27
28bool llvm::InlineFunction(CallInst *CI) { return InlineFunction(CallSite(CI)); }
29bool llvm::InlineFunction(InvokeInst *II) {return InlineFunction(CallSite(II));}
30
31// InlineFunction - This function inlines the called function into the basic
32// block of the caller.  This returns false if it is not possible to inline this
33// call.  The program is still in a well defined state if this occurs though.
34//
35// Note that this only does one level of inlining.  For example, if the
36// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
37// exists in the instruction stream.  Similiarly this will inline a recursive
38// function by one level.
39//
40bool llvm::InlineFunction(CallSite CS) {
41  Instruction *TheCall = CS.getInstruction();
42  assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
43         "Instruction not in function!");
44
45  const Function *CalledFunc = CS.getCalledFunction();
46  if (CalledFunc == 0 ||          // Can't inline external function or indirect
47      CalledFunc->isExternal() || // call, or call to a vararg function!
48      CalledFunc->getFunctionType()->isVarArg()) return false;
49
50  BasicBlock *OrigBB = TheCall->getParent();
51  Function *Caller = OrigBB->getParent();
52
53  // We want to clone the entire callee function into the whole between the
54  // "starter" and "ender" blocks.  How we accomplish this depends on whether
55  // this is an invoke instruction or a call instruction.
56
57  BasicBlock *InvokeDest = 0;     // Exception handling destination
58  std::vector<Value*> InvokeDestPHIValues; // Values for PHI nodes in InvokeDest
59  BasicBlock *AfterCallBB;
60
61  if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
62    InvokeDest = II->getExceptionalDest();
63
64    // If there are PHI nodes in the exceptional destination block, we need to
65    // keep track of which values came into them from this invoke, then remove
66    // the entry for this block.
67    for (BasicBlock::iterator I = InvokeDest->begin();
68         PHINode *PN = dyn_cast<PHINode>(I); ++I) {
69      // Save the value to use for this edge...
70      InvokeDestPHIValues.push_back(PN->getIncomingValueForBlock(OrigBB));
71    }
72
73    // Add an unconditional branch to make this look like the CallInst case...
74    BranchInst *NewBr = new BranchInst(II->getNormalDest(), TheCall);
75
76    // Split the basic block.  This guarantees that no PHI nodes will have to be
77    // updated due to new incoming edges, and make the invoke case more
78    // symmetric to the call case.
79    AfterCallBB = OrigBB->splitBasicBlock(NewBr,
80                                          CalledFunc->getName()+".entry");
81
82    // Remove (unlink) the InvokeInst from the function...
83    OrigBB->getInstList().remove(TheCall);
84
85  } else {  // It's a call
86    // If this is a call instruction, we need to split the basic block that the
87    // call lives in.
88    //
89    AfterCallBB = OrigBB->splitBasicBlock(TheCall,
90                                          CalledFunc->getName()+".entry");
91    // Remove (unlink) the CallInst from the function...
92    AfterCallBB->getInstList().remove(TheCall);
93  }
94
95  // If we have a return value generated by this call, convert it into a PHI
96  // node that gets values from each of the old RET instructions in the original
97  // function.
98  //
99  PHINode *PHI = 0;
100  if (!TheCall->use_empty()) {
101    // The PHI node should go at the front of the new basic block to merge all
102    // possible incoming values.
103    //
104    PHI = new PHINode(CalledFunc->getReturnType(), TheCall->getName(),
105                      AfterCallBB->begin());
106
107    // Anything that used the result of the function call should now use the PHI
108    // node as their operand.
109    //
110    TheCall->replaceAllUsesWith(PHI);
111  }
112
113  // Get an iterator to the last basic block in the function, which will have
114  // the new function inlined after it.
115  //
116  Function::iterator LastBlock = &Caller->back();
117
118  // Calculate the vector of arguments to pass into the function cloner...
119  std::map<const Value*, Value*> ValueMap;
120  assert(std::distance(CalledFunc->abegin(), CalledFunc->aend()) ==
121         std::distance(CS.arg_begin(), CS.arg_end()) &&
122         "No varargs calls can be inlined!");
123
124  CallSite::arg_iterator AI = CS.arg_begin();
125  for (Function::const_aiterator I = CalledFunc->abegin(), E=CalledFunc->aend();
126       I != E; ++I, ++AI)
127    ValueMap[I] = *AI;
128
129  // Since we are now done with the Call/Invoke, we can delete it.
130  delete TheCall;
131
132  // Make a vector to capture the return instructions in the cloned function...
133  std::vector<ReturnInst*> Returns;
134
135  // Do all of the hard part of cloning the callee into the caller...
136  CloneFunctionInto(Caller, CalledFunc, ValueMap, Returns, ".i");
137
138  // Loop over all of the return instructions, turning them into unconditional
139  // branches to the merge point now...
140  for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
141    ReturnInst *RI = Returns[i];
142    BasicBlock *BB = RI->getParent();
143
144    // Add a branch to the merge point where the PHI node lives if it exists.
145    new BranchInst(AfterCallBB, RI);
146
147    if (PHI) {   // The PHI node should include this value!
148      assert(RI->getReturnValue() && "Ret should have value!");
149      assert(RI->getReturnValue()->getType() == PHI->getType() &&
150             "Ret value not consistent in function!");
151      PHI->addIncoming(RI->getReturnValue(), BB);
152    }
153
154    // Delete the return instruction now
155    BB->getInstList().erase(RI);
156  }
157
158  // Check to see if the PHI node only has one argument.  This is a common
159  // case resulting from there only being a single return instruction in the
160  // function call.  Because this is so common, eliminate the PHI node.
161  //
162  if (PHI && PHI->getNumIncomingValues() == 1) {
163    PHI->replaceAllUsesWith(PHI->getIncomingValue(0));
164    PHI->getParent()->getInstList().erase(PHI);
165  }
166
167  // Change the branch that used to go to AfterCallBB to branch to the first
168  // basic block of the inlined function.
169  //
170  TerminatorInst *Br = OrigBB->getTerminator();
171  assert(Br && Br->getOpcode() == Instruction::Br &&
172	 "splitBasicBlock broken!");
173  Br->setOperand(0, ++LastBlock);
174
175  // If there are any alloca instructions in the block that used to be the entry
176  // block for the callee, move them to the entry block of the caller.  First
177  // calculate which instruction they should be inserted before.  We insert the
178  // instructions at the end of the current alloca list.
179  //
180  if (isa<AllocaInst>(LastBlock->begin())) {
181    BasicBlock::iterator InsertPoint = Caller->begin()->begin();
182    while (isa<AllocaInst>(InsertPoint)) ++InsertPoint;
183
184    for (BasicBlock::iterator I = LastBlock->begin(), E = LastBlock->end();
185         I != E; )
186      if (AllocaInst *AI = dyn_cast<AllocaInst>(I++))
187        if (isa<Constant>(AI->getArraySize())) {
188          LastBlock->getInstList().remove(AI);
189          Caller->front().getInstList().insert(InsertPoint, AI);
190        }
191  }
192
193  // If we just inlined a call due to an invoke instruction, scan the inlined
194  // function checking for function calls that should now be made into invoke
195  // instructions, and for unwind's which should be turned into branches.
196  if (InvokeDest) {
197    for (Function::iterator BB = LastBlock, E = Caller->end(); BB != E; ++BB) {
198      for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
199        // We only need to check for function calls: inlined invoke instructions
200        // require no special handling...
201        if (CallInst *CI = dyn_cast<CallInst>(I)) {
202          // Convert this function call into an invoke instruction...
203
204          // First, split the basic block...
205          BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
206
207          // Next, create the new invoke instruction, inserting it at the end
208          // of the old basic block.
209          InvokeInst *II =
210            new InvokeInst(CI->getCalledValue(), Split, InvokeDest,
211                           std::vector<Value*>(CI->op_begin()+1, CI->op_end()),
212                           CI->getName(), BB->getTerminator());
213
214          // Make sure that anything using the call now uses the invoke!
215          CI->replaceAllUsesWith(II);
216
217          // Delete the unconditional branch inserted by splitBasicBlock
218          BB->getInstList().pop_back();
219          Split->getInstList().pop_front();  // Delete the original call
220
221          // Update any PHI nodes in the exceptional block to indicate that
222          // there is now a new entry in them.
223          unsigned i = 0;
224          for (BasicBlock::iterator I = InvokeDest->begin();
225               PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i)
226            PN->addIncoming(InvokeDestPHIValues[i], BB);
227
228          // This basic block is now complete, start scanning the next one.
229          break;
230        } else {
231          ++I;
232        }
233      }
234
235      if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
236        // An UnwindInst requires special handling when it gets inlined into an
237        // invoke site.  Once this happens, we know that the unwind would cause
238        // a control transfer to the invoke exception destination, so we can
239        // transform it into a direct branch to the exception destination.
240        new BranchInst(InvokeDest, UI);
241
242        // Delete the unwind instruction!
243        UI->getParent()->getInstList().pop_back();
244
245        // Update any PHI nodes in the exceptional block to indicate that
246        // there is now a new entry in them.
247        unsigned i = 0;
248        for (BasicBlock::iterator I = InvokeDest->begin();
249             PHINode *PN = dyn_cast<PHINode>(I); ++I, ++i)
250          PN->addIncoming(InvokeDestPHIValues[i], BB);
251      }
252    }
253
254    // Now that everything is happy, we have one final detail.  The PHI nodes in
255    // the exception destination block still have entries due to the original
256    // invoke instruction.  Eliminate these entries (which might even delete the
257    // PHI node) now.
258    for (BasicBlock::iterator I = InvokeDest->begin();
259         PHINode *PN = dyn_cast<PHINode>(I); ++I)
260      PN->removeIncomingValue(AfterCallBB);
261  }
262  // Now that the function is correct, make it a little bit nicer.  In
263  // particular, move the basic blocks inserted from the end of the function
264  // into the space made by splitting the source basic block.
265  //
266  Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
267                                     LastBlock, Caller->end());
268
269  // We should always be able to fold the entry block of the function into the
270  // single predecessor of the block...
271  assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
272  BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
273  SimplifyCFG(CalleeEntry);
274
275  // Okay, continue the CFG cleanup.  It's often the case that there is only a
276  // single return instruction in the callee function.  If this is the case,
277  // then we have an unconditional branch from the return block to the
278  // 'AfterCallBB'.  Check for this case, and eliminate the branch is possible.
279  SimplifyCFG(AfterCallBB);
280  return true;
281}
282