Add support for printing out statistics information when -stats is added to
[oota-llvm.git] / lib / Transforms / IPO / InlineSimple.cpp
1 //===- FunctionInlining.cpp - Code to perform function inlining -----------===//
2 //
3 // This file implements inlining of functions.
4 //
5 // Specifically, this:
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
10 //
11 // Notice that:
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.
15 //
16 // FIXME: This pass should transform alloca instructions in the called function
17 //        into malloc/free pairs!
18 //
19 //===----------------------------------------------------------------------===//
20
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"
31
32 static Statistic<> NumInlined("inline\t\t- Number of functions inlined");
33 #include <algorithm>
34 #include <iostream>
35 using std::cerr;
36
37 // RemapInstruction - Convert the instruction operands from referencing the 
38 // current values into those specified by ValueMap.
39 //
40 static inline void RemapInstruction(Instruction *I, 
41                                     std::map<const Value *, Value*> &ValueMap) {
42
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
48
49     if (!V) {
50       cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
51       cerr << "\nInst = " << I;
52     }
53     assert(V && "Referenced value not in value map!");
54     I->setOperand(op, V);
55   }
56 }
57
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 
61 // occurs though.
62 //
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.
67 //
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!");
72
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;
77
78   //cerr << "Inlining " << CalledMeth->getName() << " into " 
79   //     << CurrentMeth->getName() << "\n";
80
81   BasicBlock *OrigBB = CI->getParent();
82
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.
86   //
87   BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
88   NewBB->setName("InlinedFunctionReturnNode");
89
90   // Remove (unlink) the CallInst from the start of the new basic block.  
91   NewBB->getInstList().remove(CI);
92
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
95   // function.
96   //
97   PHINode *PHI = 0;
98   if (CalledMeth->getReturnType() != Type::VoidTy) {
99     PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
100
101     // The PHI node should go at the front of the new basic block to merge all 
102     // possible incoming values.
103     //
104     NewBB->getInstList().push_front(PHI);
105
106     // Anything that used the result of the function call should now use the PHI
107     // node as their operand.
108     //
109     CI->replaceAllUsesWith(PHI);
110   }
111
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.
115   //
116   std::map<const Value *, Value*> ValueMap;
117
118   // Add the function arguments to the mapping: (start counting at 1 to skip the
119   // function reference itself)
120   //
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);
125   
126   ValueMap[NewBB] = NewBB;  // Returns get converted to reference NewBB
127
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...
130   //
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!?!?");
135     
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
139
140     ValueMap[BB] = IBB;                       // Add basic block mapping.
141
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!
145     //
146     const TerminatorInst *TI = BB->getTerminator();
147    
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
156     }
157
158     // Copy over the terminator now...
159     switch (TI->getOpcode()) {
160     case Instruction::Ret: {
161       const ReturnInst *RI = cast<const ReturnInst>(TI);
162
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));
168       }
169
170       // Add a branch to the code that was after the original Call.
171       IBB->getInstList().push_back(new BranchInst(NewBB));
172       break;
173     }
174     case Instruction::Br:
175       IBB->getInstList().push_back(TI->clone());
176       break;
177
178     default:
179       cerr << "FunctionInlining: Don't know how to handle terminator: " << TI;
180       abort();
181     }
182   }
183
184
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.
187   //
188   for (Function::const_iterator BI = CalledMeth->begin(); 
189        BI != CalledMeth->end(); ++BI) {
190     const BasicBlock *BB = *BI;
191     BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
192
193     // Loop over all instructions, fixing each one as we find it...
194     //
195     for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
196       RemapInstruction(*II, ValueMap);
197   }
198
199   if (PHI) RemapInstruction(PHI, ValueMap);  // Fix the PHI node also...
200
201   // Change the branch that used to go to NewBB to branch to the first basic 
202   // block of the inlined function.
203   //
204   TerminatorInst *Br = OrigBB->getTerminator();
205   assert(Br && Br->getOpcode() == Instruction::Br && 
206          "splitBasicBlock broken!");
207   Br->setOperand(0, ValueMap[CalledMeth->front()]);
208
209   // Since we are now done with the CallInst, we can finally delete it.
210   delete CI;
211   return true;
212 }
213
214 bool InlineFunction(CallInst *CI) {
215   assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
216   BasicBlock *PBB = CI->getParent();
217
218   BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
219
220   assert(CallIt != PBB->end() && 
221          "CallInst has parent that doesn't contain CallInst?!?");
222   return InlineFunction(CallIt);
223 }
224
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!");
228
229   // Don't inline a recursive call.
230   if (CI->getParent()->getParent() == F) return false;
231
232   // Don't inline something too big.  This is a really crappy heuristic
233   if (F->size() > 3) return false;
234
235   // Don't inline into something too big. This is a **really** crappy heuristic
236   if (CI->getParent()->getParent()->size() > 10) return false;
237
238   // Go ahead and try just about anything else.
239   return true;
240 }
241
242
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);
250     }
251   }
252   return false;
253 }
254
255 // doFunctionInlining - Use a heuristic based approach to inline functions that
256 // seem to look good.
257 //
258 static bool doFunctionInlining(Function *F) {
259   bool Changed = false;
260
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)) {
264       ++NumInlined;
265       Changed = true;
266       // Iterator is now invalidated by new basic blocks inserted
267       I = F->begin();
268     } else {
269       ++I;
270     }
271
272   return Changed;
273 }
274
275 namespace {
276   struct FunctionInlining : public FunctionPass {
277     const char *getPassName() const { return "Function Inlining"; }
278     virtual bool runOnFunction(Function *F) {
279       return doFunctionInlining(F);
280     }
281   };
282 }
283
284 Pass *createFunctionInliningPass() { return new FunctionInlining(); }