blob: ef92b2d07cad7d042e8592900670d9b0df20b437 [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 Lattnerc8b25d42001-07-07 08:36:50 +000040 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
41 const Value *Op = I->getOperand(op);
Chris Lattner00950542001-06-06 20:29:01 +000042 Value *V = ValueMap[Op];
Chris Lattner1d87bcf2001-10-01 20:11:19 +000043 if (!V && (isa<Method>(Op) || isa<ConstPoolVal>(Op)))
Chris Lattner78721d52001-09-07 16:42:51 +000044 continue; // Methods and constants don't get relocated
Chris Lattner00950542001-06-06 20:29:01 +000045
46 if (!V) {
47 cerr << "Val = " << endl << Op << "Addr = " << (void*)Op << endl;
48 cerr << "Inst = " << I;
49 }
50 assert(V && "Referenced value not in value map!");
51 I->setOperand(op, V);
52 }
53}
54
55// InlineMethod - This function forcibly inlines the called method into the
56// basic block of the caller. This returns false if it is not possible to
57// inline this call. The program is still in a well defined state if this
58// occurs though.
59//
60// Note that this only does one level of inlining. For example, if the
61// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
62// exists in the instruction stream. Similiarly this will inline a recursive
63// method by one level.
64//
Chris Lattner7e02b7e2001-06-30 04:36:40 +000065bool opt::InlineMethod(BasicBlock::iterator CIIt) {
Chris Lattnerb00c5822001-10-02 03:41:24 +000066 assert(isa<CallInst>(*CIIt) && "InlineMethod only works on CallInst nodes!");
Chris Lattner00950542001-06-06 20:29:01 +000067 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
68 assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
69
Chris Lattnerb00c5822001-10-02 03:41:24 +000070 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattner00950542001-06-06 20:29:01 +000071 const Method *CalledMeth = CI->getCalledMethod();
Chris Lattner5a0d4172001-10-13 06:52:31 +000072 if (CalledMeth == 0 || // Can't inline external method or indirect call!
73 CalledMeth->isExternal()) return false;
Chris Lattner00950542001-06-06 20:29:01 +000074 Method *CurrentMeth = CI->getParent()->getParent();
75
76 //cerr << "Inlining " << CalledMeth->getName() << " into "
77 // << CurrentMeth->getName() << endl;
78
79 BasicBlock *OrigBB = CI->getParent();
80
81 // Call splitBasicBlock - The original basic block now ends at the instruction
82 // immediately before the call. The original basic block now ends with an
83 // unconditional branch to NewBB, and NewBB starts with the call instruction.
84 //
85 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
86
87 // Remove (unlink) the CallInst from the start of the new basic block.
88 NewBB->getInstList().remove(CI);
89
90 // If we have a return value generated by this call, convert it into a PHI
91 // node that gets values from each of the old RET instructions in the original
92 // method.
93 //
94 PHINode *PHI = 0;
95 if (CalledMeth->getReturnType() != Type::VoidTy) {
96 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
97
98 // The PHI node should go at the front of the new basic block to merge all
99 // possible incoming values.
100 //
101 NewBB->getInstList().push_front(PHI);
102
103 // Anything that used the result of the function call should now use the PHI
104 // node as their operand.
105 //
106 CI->replaceAllUsesWith(PHI);
107 }
108
109 // Keep a mapping between the original method's values and the new duplicated
110 // code's values. This includes all of: Method arguments, instruction values,
111 // constant pool entries, and basic blocks.
112 //
113 map<const Value *, Value*> ValueMap;
114
115 // Add the method arguments to the mapping: (start counting at 1 to skip the
116 // method reference itself)
117 //
118 Method::ArgumentListType::const_iterator PTI =
119 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000120 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
121 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000122
Chris Lattner00950542001-06-06 20:29:01 +0000123 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
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000136 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000137
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...
Chris Lattnera41f50d2001-07-07 19:24:15 +0000153 switch (TI->getOpcode()) {
Chris Lattner00950542001-06-06 20:29:01 +0000154 case Instruction::Ret: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000155 const ReturnInst *RI = cast<const ReturnInst>(TI);
Chris Lattner00950542001-06-06 20:29:01 +0000156
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 Lattnerb00c5822001-10-02 03:41:24 +0000161 PHI->addIncoming((Value*)RI->getReturnValue(), cast<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
Chris Lattner00950542001-06-06 20:29:01 +0000179 // Loop over all of the instructions in the method, fixing up operand
180 // references as we go. This uses ValueMap to do all the hard work.
181 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000182 for (Method::const_iterator BI = CalledMeth->begin();
183 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000184 const BasicBlock *BB = *BI;
185 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
186
187 // Loop over all instructions, fixing each one as we find it...
188 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000189 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000190 RemapInstruction(*II, ValueMap);
191 }
192
193 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
194
195 // Change the branch that used to go to NewBB to branch to the first basic
196 // block of the inlined method.
197 //
198 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnera41f50d2001-07-07 19:24:15 +0000199 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner00950542001-06-06 20:29:01 +0000200 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000201 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000202
203 // Since we are now done with the CallInst, we can finally delete it.
204 delete CI;
205 return true;
206}
207
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000208bool opt::InlineMethod(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000209 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
210 BasicBlock *PBB = CI->getParent();
211
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000212 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
213
214 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000215 "CallInst has parent that doesn't contain CallInst?!?");
216 return InlineMethod(CallIt);
217}
218
219static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) {
220 assert(CI->getParent() && CI->getParent()->getParent() &&
221 "Call not embedded into a method!");
222
223 // Don't inline a recursive call.
224 if (CI->getParent()->getParent() == M) return false;
225
226 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000227 if (M->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000228
229 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000230 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000231
232 // Go ahead and try just about anything else.
233 return true;
234}
235
236
237static inline bool DoMethodInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000238 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000239 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000240 // Check to see if we should inline this method
Chris Lattner00950542001-06-06 20:29:01 +0000241 Method *M = CI->getCalledMethod();
Chris Lattner5a0d4172001-10-13 06:52:31 +0000242 if (M && ShouldInlineMethod(CI, M))
Chris Lattner00950542001-06-06 20:29:01 +0000243 return InlineMethod(I);
244 }
245 }
246 return false;
247}
248
Chris Lattner5680ee62001-10-18 01:32:34 +0000249bool opt::MethodInlining::doMethodInlining(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000250 bool Changed = false;
251
252 // Loop through now and inline instructions a basic block at a time...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000253 for (Method::iterator I = M->begin(); I != M->end(); )
Chris Lattner00950542001-06-06 20:29:01 +0000254 if (DoMethodInlining(*I)) {
255 Changed = true;
256 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000257 I = M->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000258 } else {
259 ++I;
260 }
261
262 return Changed;
263}