blob: d85b33dffd1e56648d5091c8b89b62e0156f84a5 [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
Chris Lattner7e02b7e2001-06-30 04:36:40 +000022#include "llvm/Optimizations/MethodInlining.h"
Chris Lattner00950542001-06-06 20:29:01 +000023#include "llvm/Module.h"
24#include "llvm/Method.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/iTerminators.h"
26#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000027#include <algorithm>
28#include <map>
29
30#include "llvm/Assembly/Writer.h"
31
Chris Lattner7e02b7e2001-06-30 04:36:40 +000032using namespace opt;
33
Chris Lattner00950542001-06-06 20:29:01 +000034// RemapInstruction - Convert the instruction operands from referencing the
35// current values into those specified by ValueMap.
36//
37static inline void RemapInstruction(Instruction *I,
38 map<const Value *, Value*> &ValueMap) {
39
Chris Lattner7fc9fe32001-06-27 23:41:11 +000040 for (unsigned op = 0; const Value *Op = I->getOperand(op); ++op) {
Chris Lattner00950542001-06-06 20:29:01 +000041 Value *V = ValueMap[Op];
Chris Lattner7fc9fe32001-06-27 23:41:11 +000042 if (!V && Op->isMethod())
Chris Lattner00950542001-06-06 20:29:01 +000043 continue; // Methods don't get relocated
44
45 if (!V) {
46 cerr << "Val = " << endl << Op << "Addr = " << (void*)Op << endl;
47 cerr << "Inst = " << I;
48 }
49 assert(V && "Referenced value not in value map!");
50 I->setOperand(op, V);
51 }
52}
53
54// InlineMethod - This function forcibly inlines the called method into the
55// basic block of the caller. This returns false if it is not possible to
56// inline this call. The program is still in a well defined state if this
57// occurs though.
58//
59// Note that this only does one level of inlining. For example, if the
60// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
61// exists in the instruction stream. Similiarly this will inline a recursive
62// method by one level.
63//
Chris Lattner7e02b7e2001-06-30 04:36:40 +000064bool opt::InlineMethod(BasicBlock::iterator CIIt) {
Chris Lattner00950542001-06-06 20:29:01 +000065 assert((*CIIt)->getInstType() == Instruction::Call &&
66 "InlineMethod only works on CallInst nodes!");
67 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
68 assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
69
70 CallInst *CI = (CallInst*)*CIIt;
71 const Method *CalledMeth = CI->getCalledMethod();
72 Method *CurrentMeth = CI->getParent()->getParent();
73
74 //cerr << "Inlining " << CalledMeth->getName() << " into "
75 // << CurrentMeth->getName() << endl;
76
77 BasicBlock *OrigBB = CI->getParent();
78
79 // Call splitBasicBlock - The original basic block now ends at the instruction
80 // immediately before the call. The original basic block now ends with an
81 // unconditional branch to NewBB, and NewBB starts with the call instruction.
82 //
83 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
84
85 // Remove (unlink) the CallInst from the start of the new basic block.
86 NewBB->getInstList().remove(CI);
87
88 // If we have a return value generated by this call, convert it into a PHI
89 // node that gets values from each of the old RET instructions in the original
90 // method.
91 //
92 PHINode *PHI = 0;
93 if (CalledMeth->getReturnType() != Type::VoidTy) {
94 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
95
96 // The PHI node should go at the front of the new basic block to merge all
97 // possible incoming values.
98 //
99 NewBB->getInstList().push_front(PHI);
100
101 // Anything that used the result of the function call should now use the PHI
102 // node as their operand.
103 //
104 CI->replaceAllUsesWith(PHI);
105 }
106
107 // Keep a mapping between the original method's values and the new duplicated
108 // code's values. This includes all of: Method arguments, instruction values,
109 // constant pool entries, and basic blocks.
110 //
111 map<const Value *, Value*> ValueMap;
112
113 // Add the method arguments to the mapping: (start counting at 1 to skip the
114 // method reference itself)
115 //
116 Method::ArgumentListType::const_iterator PTI =
117 CalledMeth->getArgumentList().begin();
118 for (unsigned a = 1; Value *Operand = CI->getOperand(a); ++a, ++PTI) {
119 ValueMap[*PTI] = Operand;
120 }
121
122
123 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
124
125 // Loop over all of the basic blocks in the method, inlining them as
126 // appropriate. Keep track of the first basic block of the method...
127 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000128 for (Method::const_iterator BI = CalledMeth->begin();
129 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000130 const BasicBlock *BB = *BI;
131 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
132
133 // Create a new basic block to copy instructions into!
134 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
135
136 ValueMap[*BI] = IBB; // Add basic block mapping.
137
138 // Make sure to capture the mapping that a return will use...
139 // TODO: This assumes that the RET is returning a value computed in the same
140 // basic block as the return was issued from!
141 //
142 const TerminatorInst *TI = BB->getTerminator();
143
144 // Loop over all instructions copying them over...
145 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000146 for (BasicBlock::const_iterator II = BB->begin();
147 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000148 IBB->getInstList().push_back((NewInst = (*II)->clone()));
149 ValueMap[*II] = NewInst; // Add instruction map to value.
150 }
151
152 // Copy over the terminator now...
153 switch (TI->getInstType()) {
154 case Instruction::Ret: {
155 const ReturnInst *RI = (const ReturnInst*)TI;
156
157 if (PHI) { // The PHI node should include this value!
158 assert(RI->getReturnValue() && "Ret should have value!");
159 assert(RI->getReturnValue()->getType() == PHI->getType() &&
160 "Ret value not consistent in method!");
Chris Lattneree976f32001-06-11 15:04:40 +0000161 PHI->addIncoming((Value*)RI->getReturnValue(), (BasicBlock*)BB);
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
163
164 // Add a branch to the code that was after the original Call.
165 IBB->getInstList().push_back(new BranchInst(NewBB));
166 break;
167 }
168 case Instruction::Br:
169 IBB->getInstList().push_back(TI->clone());
170 break;
171
172 default:
173 cerr << "MethodInlining: Don't know how to handle terminator: " << TI;
174 abort();
175 }
176 }
177
178
179 // Copy over the constant pool...
180 //
181 const ConstantPool &CP = CalledMeth->getConstantPool();
182 ConstantPool &NewCP = CurrentMeth->getConstantPool();
183 for (ConstantPool::plane_const_iterator PI = CP.begin(); PI != CP.end(); ++PI){
184 ConstantPool::PlaneType &Plane = **PI;
185 for (ConstantPool::PlaneType::const_iterator I = Plane.begin();
186 I != Plane.end(); ++I) {
187 ConstPoolVal *NewVal = (*I)->clone(); // Copy existing constant
188 NewCP.insert(NewVal); // Insert the new copy into local const pool
189 ValueMap[*I] = NewVal; // Keep track of constant value mappings
190 }
191 }
192
193 // Loop over all of the instructions in the method, fixing up operand
194 // references as we go. This uses ValueMap to do all the hard work.
195 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000196 for (Method::const_iterator BI = CalledMeth->begin();
197 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000198 const BasicBlock *BB = *BI;
199 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
200
201 // Loop over all instructions, fixing each one as we find it...
202 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000203 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000204 RemapInstruction(*II, ValueMap);
205 }
206
207 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
208
209 // Change the branch that used to go to NewBB to branch to the first basic
210 // block of the inlined method.
211 //
212 TerminatorInst *Br = OrigBB->getTerminator();
213 assert(Br && Br->getInstType() == Instruction::Br &&
214 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000215 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000216
217 // Since we are now done with the CallInst, we can finally delete it.
218 delete CI;
219 return true;
220}
221
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000222bool opt::InlineMethod(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000223 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
224 BasicBlock *PBB = CI->getParent();
225
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000226 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
227
228 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000229 "CallInst has parent that doesn't contain CallInst?!?");
230 return InlineMethod(CallIt);
231}
232
233static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) {
234 assert(CI->getParent() && CI->getParent()->getParent() &&
235 "Call not embedded into a method!");
236
237 // Don't inline a recursive call.
238 if (CI->getParent()->getParent() == M) return false;
239
240 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000241 if (M->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000242
243 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000244 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000245
246 // Go ahead and try just about anything else.
247 return true;
248}
249
250
251static inline bool DoMethodInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000252 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +0000253 if ((*I)->getInstType() == Instruction::Call) {
254 // Check to see if we should inline this method
255 CallInst *CI = (CallInst*)*I;
256 Method *M = CI->getCalledMethod();
257 if (ShouldInlineMethod(CI, M))
258 return InlineMethod(I);
259 }
260 }
261 return false;
262}
263
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000264bool opt::DoMethodInlining(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000265 bool Changed = false;
266
267 // Loop through now and inline instructions a basic block at a time...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000268 for (Method::iterator I = M->begin(); I != M->end(); )
Chris Lattner00950542001-06-06 20:29:01 +0000269 if (DoMethodInlining(*I)) {
270 Changed = true;
271 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000272 I = M->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000273 } else {
274 ++I;
275 }
276
277 return Changed;
278}