1 //===- FunctionInlining.cpp - Code to perform function inlining -----------===//
3 // This file implements inlining of functions.
6 // * Exports functionality to inline any function call
7 // * Inlines functions that consist of a single basic block
8 // * Is able to inline ANY function call
9 // . Has a smart heuristic for when to inline a function
12 // * This pass opens up a lot of opportunities for constant propogation. It
13 // is a good idea to to run a constant propogation pass, then a DCE pass
14 // sometime after running this pass.
16 // FIXME: This pass should transform alloca instructions in the called function
17 // into malloc/free pairs!
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Transforms/FunctionInlining.h"
22 #include "llvm/Module.h"
23 #include "llvm/Function.h"
24 #include "llvm/Pass.h"
25 #include "llvm/iTerminators.h"
26 #include "llvm/iPHINode.h"
27 #include "llvm/iOther.h"
28 #include "llvm/Type.h"
29 #include "llvm/Argument.h"
30 #include "Support/StatisticReporter.h"
32 static Statistic<> NumInlined("inline\t\t- Number of functions inlined");
37 // RemapInstruction - Convert the instruction operands from referencing the
38 // current values into those specified by ValueMap.
40 static inline void RemapInstruction(Instruction *I,
41 std::map<const Value *, Value*> &ValueMap) {
43 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
44 const Value *Op = I->getOperand(op);
45 Value *V = ValueMap[Op];
46 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
47 continue; // Globals and constants don't get relocated
50 cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
51 cerr << "\nInst = " << I;
53 assert(V && "Referenced value not in value map!");
58 // InlineFunction - This function forcibly inlines the called function into the
59 // basic block of the caller. This returns false if it is not possible to
60 // inline this call. The program is still in a well defined state if this
63 // Note that this only does one level of inlining. For example, if the
64 // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
65 // exists in the instruction stream. Similiarly this will inline a recursive
66 // function by one level.
68 bool InlineFunction(BasicBlock::iterator CIIt) {
69 assert(isa<CallInst>(*CIIt) && "InlineFunction only works on CallInst nodes");
70 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
71 assert((*CIIt)->getParent()->getParent() && "Instruction not in function!");
73 CallInst *CI = cast<CallInst>(*CIIt);
74 const Function *CalledMeth = CI->getCalledFunction();
75 if (CalledMeth == 0 || // Can't inline external function or indirect call!
76 CalledMeth->isExternal()) return false;
78 //cerr << "Inlining " << CalledMeth->getName() << " into "
79 // << CurrentMeth->getName() << "\n";
81 BasicBlock *OrigBB = CI->getParent();
83 // Call splitBasicBlock - The original basic block now ends at the instruction
84 // immediately before the call. The original basic block now ends with an
85 // unconditional branch to NewBB, and NewBB starts with the call instruction.
87 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
88 NewBB->setName("InlinedFunctionReturnNode");
90 // Remove (unlink) the CallInst from the start of the new basic block.
91 NewBB->getInstList().remove(CI);
93 // If we have a return value generated by this call, convert it into a PHI
94 // node that gets values from each of the old RET instructions in the original
98 if (CalledMeth->getReturnType() != Type::VoidTy) {
99 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
101 // The PHI node should go at the front of the new basic block to merge all
102 // possible incoming values.
104 NewBB->getInstList().push_front(PHI);
106 // Anything that used the result of the function call should now use the PHI
107 // node as their operand.
109 CI->replaceAllUsesWith(PHI);
112 // Keep a mapping between the original function's values and the new
113 // duplicated code's values. This includes all of: Function arguments,
114 // instruction values, constant pool entries, and basic blocks.
116 std::map<const Value *, Value*> ValueMap;
118 // Add the function arguments to the mapping: (start counting at 1 to skip the
119 // function reference itself)
121 Function::ArgumentListType::const_iterator PTI =
122 CalledMeth->getArgumentList().begin();
123 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
124 ValueMap[*PTI] = CI->getOperand(a);
126 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
128 // Loop over all of the basic blocks in the function, inlining them as
129 // appropriate. Keep track of the first basic block of the function...
131 for (Function::const_iterator BI = CalledMeth->begin();
132 BI != CalledMeth->end(); ++BI) {
133 const BasicBlock *BB = *BI;
134 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
136 // Create a new basic block to copy instructions into!
137 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
138 if (BB->hasName()) IBB->setName(BB->getName()+".i"); // .i = inlined once
140 ValueMap[BB] = IBB; // Add basic block mapping.
142 // Make sure to capture the mapping that a return will use...
143 // TODO: This assumes that the RET is returning a value computed in the same
144 // basic block as the return was issued from!
146 const TerminatorInst *TI = BB->getTerminator();
148 // Loop over all instructions copying them over...
149 Instruction *NewInst;
150 for (BasicBlock::const_iterator II = BB->begin();
151 II != (BB->end()-1); ++II) {
152 IBB->getInstList().push_back((NewInst = (*II)->clone()));
153 ValueMap[*II] = NewInst; // Add instruction map to value.
154 if ((*II)->hasName())
155 NewInst->setName((*II)->getName()+".i"); // .i = inlined once
158 // Copy over the terminator now...
159 switch (TI->getOpcode()) {
160 case Instruction::Ret: {
161 const ReturnInst *RI = cast<const ReturnInst>(TI);
163 if (PHI) { // The PHI node should include this value!
164 assert(RI->getReturnValue() && "Ret should have value!");
165 assert(RI->getReturnValue()->getType() == PHI->getType() &&
166 "Ret value not consistent in function!");
167 PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
170 // Add a branch to the code that was after the original Call.
171 IBB->getInstList().push_back(new BranchInst(NewBB));
174 case Instruction::Br:
175 IBB->getInstList().push_back(TI->clone());
179 cerr << "FunctionInlining: Don't know how to handle terminator: " << TI;
185 // Loop over all of the instructions in the function, fixing up operand
186 // references as we go. This uses ValueMap to do all the hard work.
188 for (Function::const_iterator BI = CalledMeth->begin();
189 BI != CalledMeth->end(); ++BI) {
190 const BasicBlock *BB = *BI;
191 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
193 // Loop over all instructions, fixing each one as we find it...
195 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
196 RemapInstruction(*II, ValueMap);
199 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
201 // Change the branch that used to go to NewBB to branch to the first basic
202 // block of the inlined function.
204 TerminatorInst *Br = OrigBB->getTerminator();
205 assert(Br && Br->getOpcode() == Instruction::Br &&
206 "splitBasicBlock broken!");
207 Br->setOperand(0, ValueMap[CalledMeth->front()]);
209 // Since we are now done with the CallInst, we can finally delete it.
214 bool InlineFunction(CallInst *CI) {
215 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
216 BasicBlock *PBB = CI->getParent();
218 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
220 assert(CallIt != PBB->end() &&
221 "CallInst has parent that doesn't contain CallInst?!?");
222 return InlineFunction(CallIt);
225 static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
226 assert(CI->getParent() && CI->getParent()->getParent() &&
227 "Call not embedded into a function!");
229 // Don't inline a recursive call.
230 if (CI->getParent()->getParent() == F) return false;
232 // Don't inline something too big. This is a really crappy heuristic
233 if (F->size() > 3) return false;
235 // Don't inline into something too big. This is a **really** crappy heuristic
236 if (CI->getParent()->getParent()->size() > 10) return false;
238 // Go ahead and try just about anything else.
243 static inline bool DoFunctionInlining(BasicBlock *BB) {
244 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
245 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
246 // Check to see if we should inline this function
247 Function *F = CI->getCalledFunction();
248 if (F && ShouldInlineFunction(CI, F))
249 return InlineFunction(I);
255 // doFunctionInlining - Use a heuristic based approach to inline functions that
256 // seem to look good.
258 static bool doFunctionInlining(Function *F) {
259 bool Changed = false;
261 // Loop through now and inline instructions a basic block at a time...
262 for (Function::iterator I = F->begin(); I != F->end(); )
263 if (DoFunctionInlining(*I)) {
266 // Iterator is now invalidated by new basic blocks inserted
276 struct FunctionInlining : public FunctionPass {
277 const char *getPassName() const { return "Function Inlining"; }
278 virtual bool runOnFunction(Function *F) {
279 return doFunctionInlining(F);
284 Pass *createFunctionInliningPass() { return new FunctionInlining(); }