blob: e5bc171beb195d39cdd3ef55bbf8643532a8a9e5 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- MethodInlining.cpp - Code to perform method inlining ---------------===//
2//
3// This file implements inlining of methods.
4//
5// Specifically, this:
6// * Exports functionality to inline any method call
7// * Inlines methods that consist of a single basic block
8// * Is able to inline ANY method call
9// . Has a smart heuristic for when to inline a method
10//
11// Notice that:
12// * This pass has a habit of introducing duplicated constant pool entries,
13// and also opens up a lot of opportunities for constant propogation. It is
14// a good idea to to run a constant propogation pass, then a DCE pass
15// sometime after running this pass.
16//
17// TODO: Currently this throws away all of the symbol names in the method being
18// inlined to try to avoid name clashes. Use a name if it's not taken
19//
20//===----------------------------------------------------------------------===//
21
22#include "llvm/Module.h"
23#include "llvm/Method.h"
24#include "llvm/BasicBlock.h"
25#include "llvm/iTerminators.h"
26#include "llvm/iOther.h"
27#include "llvm/Opt/AllOpts.h"
28#include <algorithm>
29#include <map>
30
31#include "llvm/Assembly/Writer.h"
32
33// RemapInstruction - Convert the instruction operands from referencing the
34// current values into those specified by ValueMap.
35//
36static inline void RemapInstruction(Instruction *I,
37 map<const Value *, Value*> &ValueMap) {
38
Chris Lattner7fc9fe32001-06-27 23:41:11 +000039 for (unsigned op = 0; const Value *Op = I->getOperand(op); ++op) {
Chris Lattner00950542001-06-06 20:29:01 +000040 Value *V = ValueMap[Op];
Chris Lattner7fc9fe32001-06-27 23:41:11 +000041 if (!V && Op->isMethod())
Chris Lattner00950542001-06-06 20:29:01 +000042 continue; // Methods don't get relocated
43
44 if (!V) {
45 cerr << "Val = " << endl << Op << "Addr = " << (void*)Op << endl;
46 cerr << "Inst = " << I;
47 }
48 assert(V && "Referenced value not in value map!");
49 I->setOperand(op, V);
50 }
51}
52
53// InlineMethod - This function forcibly inlines the called method into the
54// basic block of the caller. This returns false if it is not possible to
55// inline this call. The program is still in a well defined state if this
56// occurs though.
57//
58// Note that this only does one level of inlining. For example, if the
59// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
60// exists in the instruction stream. Similiarly this will inline a recursive
61// method by one level.
62//
Chris Lattner7fc9fe32001-06-27 23:41:11 +000063bool InlineMethod(BasicBlock::iterator CIIt) {
Chris Lattner00950542001-06-06 20:29:01 +000064 assert((*CIIt)->getInstType() == Instruction::Call &&
65 "InlineMethod only works on CallInst nodes!");
66 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
67 assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
68
69 CallInst *CI = (CallInst*)*CIIt;
70 const Method *CalledMeth = CI->getCalledMethod();
71 Method *CurrentMeth = CI->getParent()->getParent();
72
73 //cerr << "Inlining " << CalledMeth->getName() << " into "
74 // << CurrentMeth->getName() << endl;
75
76 BasicBlock *OrigBB = CI->getParent();
77
78 // Call splitBasicBlock - The original basic block now ends at the instruction
79 // immediately before the call. The original basic block now ends with an
80 // unconditional branch to NewBB, and NewBB starts with the call instruction.
81 //
82 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
83
84 // Remove (unlink) the CallInst from the start of the new basic block.
85 NewBB->getInstList().remove(CI);
86
87 // If we have a return value generated by this call, convert it into a PHI
88 // node that gets values from each of the old RET instructions in the original
89 // method.
90 //
91 PHINode *PHI = 0;
92 if (CalledMeth->getReturnType() != Type::VoidTy) {
93 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
94
95 // The PHI node should go at the front of the new basic block to merge all
96 // possible incoming values.
97 //
98 NewBB->getInstList().push_front(PHI);
99
100 // Anything that used the result of the function call should now use the PHI
101 // node as their operand.
102 //
103 CI->replaceAllUsesWith(PHI);
104 }
105
106 // Keep a mapping between the original method's values and the new duplicated
107 // code's values. This includes all of: Method arguments, instruction values,
108 // constant pool entries, and basic blocks.
109 //
110 map<const Value *, Value*> ValueMap;
111
112 // Add the method arguments to the mapping: (start counting at 1 to skip the
113 // method reference itself)
114 //
115 Method::ArgumentListType::const_iterator PTI =
116 CalledMeth->getArgumentList().begin();
117 for (unsigned a = 1; Value *Operand = CI->getOperand(a); ++a, ++PTI) {
118 ValueMap[*PTI] = Operand;
119 }
120
121
122 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
123
124 // Loop over all of the basic blocks in the method, inlining them as
125 // appropriate. Keep track of the first basic block of the method...
126 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000127 for (Method::const_iterator BI = CalledMeth->begin();
128 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000129 const BasicBlock *BB = *BI;
130 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
131
132 // Create a new basic block to copy instructions into!
133 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
134
135 ValueMap[*BI] = IBB; // Add basic block mapping.
136
137 // Make sure to capture the mapping that a return will use...
138 // TODO: This assumes that the RET is returning a value computed in the same
139 // basic block as the return was issued from!
140 //
141 const TerminatorInst *TI = BB->getTerminator();
142
143 // Loop over all instructions copying them over...
144 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000145 for (BasicBlock::const_iterator II = BB->begin();
146 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000147 IBB->getInstList().push_back((NewInst = (*II)->clone()));
148 ValueMap[*II] = NewInst; // Add instruction map to value.
149 }
150
151 // Copy over the terminator now...
152 switch (TI->getInstType()) {
153 case Instruction::Ret: {
154 const ReturnInst *RI = (const ReturnInst*)TI;
155
156 if (PHI) { // The PHI node should include this value!
157 assert(RI->getReturnValue() && "Ret should have value!");
158 assert(RI->getReturnValue()->getType() == PHI->getType() &&
159 "Ret value not consistent in method!");
Chris Lattneree976f32001-06-11 15:04:40 +0000160 PHI->addIncoming((Value*)RI->getReturnValue(), (BasicBlock*)BB);
Chris Lattner00950542001-06-06 20:29:01 +0000161 }
162
163 // Add a branch to the code that was after the original Call.
164 IBB->getInstList().push_back(new BranchInst(NewBB));
165 break;
166 }
167 case Instruction::Br:
168 IBB->getInstList().push_back(TI->clone());
169 break;
170
171 default:
172 cerr << "MethodInlining: Don't know how to handle terminator: " << TI;
173 abort();
174 }
175 }
176
177
178 // Copy over the constant pool...
179 //
180 const ConstantPool &CP = CalledMeth->getConstantPool();
181 ConstantPool &NewCP = CurrentMeth->getConstantPool();
182 for (ConstantPool::plane_const_iterator PI = CP.begin(); PI != CP.end(); ++PI){
183 ConstantPool::PlaneType &Plane = **PI;
184 for (ConstantPool::PlaneType::const_iterator I = Plane.begin();
185 I != Plane.end(); ++I) {
186 ConstPoolVal *NewVal = (*I)->clone(); // Copy existing constant
187 NewCP.insert(NewVal); // Insert the new copy into local const pool
188 ValueMap[*I] = NewVal; // Keep track of constant value mappings
189 }
190 }
191
192 // Loop over all of the instructions in the method, fixing up operand
193 // references as we go. This uses ValueMap to do all the hard work.
194 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000195 for (Method::const_iterator BI = CalledMeth->begin();
196 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000197 const BasicBlock *BB = *BI;
198 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
199
200 // Loop over all instructions, fixing each one as we find it...
201 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000202 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000203 RemapInstruction(*II, ValueMap);
204 }
205
206 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
207
208 // Change the branch that used to go to NewBB to branch to the first basic
209 // block of the inlined method.
210 //
211 TerminatorInst *Br = OrigBB->getTerminator();
212 assert(Br && Br->getInstType() == Instruction::Br &&
213 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000214 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000215
216 // Since we are now done with the CallInst, we can finally delete it.
217 delete CI;
218 return true;
219}
220
221bool InlineMethod(CallInst *CI) {
222 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
223 BasicBlock *PBB = CI->getParent();
224
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000225 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
226
227 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000228 "CallInst has parent that doesn't contain CallInst?!?");
229 return InlineMethod(CallIt);
230}
231
232static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) {
233 assert(CI->getParent() && CI->getParent()->getParent() &&
234 "Call not embedded into a method!");
235
236 // Don't inline a recursive call.
237 if (CI->getParent()->getParent() == M) return false;
238
239 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000240 if (M->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000241
242 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000243 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000244
245 // Go ahead and try just about anything else.
246 return true;
247}
248
249
250static inline bool DoMethodInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000251 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000252 if ((*I)->getInstType() == Instruction::Call) {
253 // Check to see if we should inline this method
254 CallInst *CI = (CallInst*)*I;
255 Method *M = CI->getCalledMethod();
256 if (ShouldInlineMethod(CI, M))
257 return InlineMethod(I);
258 }
259 }
260 return false;
261}
262
263bool DoMethodInlining(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000264 bool Changed = false;
265
266 // Loop through now and inline instructions a basic block at a time...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000267 for (Method::iterator I = M->begin(); I != M->end(); )
Chris Lattner00950542001-06-06 20:29:01 +0000268 if (DoMethodInlining(*I)) {
269 Changed = true;
270 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000271 I = M->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000272 } else {
273 ++I;
274 }
275
276 return Changed;
277}