blob: 40b98bd67dea9b7c7170465e739819dc583341a8 [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"
Chris Lattner7061dc52001-12-03 18:02:31 +000026#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000027#include "llvm/iOther.h"
Chris Lattner00950542001-06-06 20:29:01 +000028#include <algorithm>
29#include <map>
30
31#include "llvm/Assembly/Writer.h"
32
Chris Lattner7e02b7e2001-06-30 04:36:40 +000033using namespace opt;
34
Chris Lattner00950542001-06-06 20:29:01 +000035// RemapInstruction - Convert the instruction operands from referencing the
36// current values into those specified by ValueMap.
37//
38static inline void RemapInstruction(Instruction *I,
39 map<const Value *, Value*> &ValueMap) {
40
Chris Lattnerc8b25d42001-07-07 08:36:50 +000041 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
42 const Value *Op = I->getOperand(op);
Chris Lattner00950542001-06-06 20:29:01 +000043 Value *V = ValueMap[Op];
Chris Lattnere9bb2df2001-12-03 22:26:30 +000044 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
Chris Lattner4f685282001-10-31 02:27:26 +000045 continue; // Globals and constants don't get relocated
Chris Lattner00950542001-06-06 20:29:01 +000046
47 if (!V) {
48 cerr << "Val = " << endl << Op << "Addr = " << (void*)Op << endl;
49 cerr << "Inst = " << I;
50 }
51 assert(V && "Referenced value not in value map!");
52 I->setOperand(op, V);
53 }
54}
55
56// InlineMethod - This function forcibly inlines the called method into the
57// basic block of the caller. This returns false if it is not possible to
58// inline this call. The program is still in a well defined state if this
59// occurs though.
60//
61// Note that this only does one level of inlining. For example, if the
62// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
63// exists in the instruction stream. Similiarly this will inline a recursive
64// method by one level.
65//
Chris Lattner7e02b7e2001-06-30 04:36:40 +000066bool opt::InlineMethod(BasicBlock::iterator CIIt) {
Chris Lattnerb00c5822001-10-02 03:41:24 +000067 assert(isa<CallInst>(*CIIt) && "InlineMethod only works on CallInst nodes!");
Chris Lattner00950542001-06-06 20:29:01 +000068 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
69 assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
70
Chris Lattnerb00c5822001-10-02 03:41:24 +000071 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattner00950542001-06-06 20:29:01 +000072 const Method *CalledMeth = CI->getCalledMethod();
Chris Lattner5a0d4172001-10-13 06:52:31 +000073 if (CalledMeth == 0 || // Can't inline external method or indirect call!
74 CalledMeth->isExternal()) return false;
Chris Lattner00950542001-06-06 20:29:01 +000075 Method *CurrentMeth = CI->getParent()->getParent();
76
77 //cerr << "Inlining " << CalledMeth->getName() << " into "
78 // << CurrentMeth->getName() << endl;
79
80 BasicBlock *OrigBB = CI->getParent();
81
82 // Call splitBasicBlock - The original basic block now ends at the instruction
83 // immediately before the call. The original basic block now ends with an
84 // unconditional branch to NewBB, and NewBB starts with the call instruction.
85 //
86 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
87
88 // Remove (unlink) the CallInst from the start of the new basic block.
89 NewBB->getInstList().remove(CI);
90
91 // If we have a return value generated by this call, convert it into a PHI
92 // node that gets values from each of the old RET instructions in the original
93 // method.
94 //
95 PHINode *PHI = 0;
96 if (CalledMeth->getReturnType() != Type::VoidTy) {
97 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
98
99 // The PHI node should go at the front of the new basic block to merge all
100 // possible incoming values.
101 //
102 NewBB->getInstList().push_front(PHI);
103
104 // Anything that used the result of the function call should now use the PHI
105 // node as their operand.
106 //
107 CI->replaceAllUsesWith(PHI);
108 }
109
110 // Keep a mapping between the original method's values and the new duplicated
111 // code's values. This includes all of: Method arguments, instruction values,
112 // constant pool entries, and basic blocks.
113 //
114 map<const Value *, Value*> ValueMap;
115
116 // Add the method arguments to the mapping: (start counting at 1 to skip the
117 // method reference itself)
118 //
119 Method::ArgumentListType::const_iterator PTI =
120 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000121 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
122 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000123
Chris Lattner00950542001-06-06 20:29:01 +0000124 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
125
126 // Loop over all of the basic blocks in the method, inlining them as
127 // appropriate. Keep track of the first basic block of the method...
128 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000129 for (Method::const_iterator BI = CalledMeth->begin();
130 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000131 const BasicBlock *BB = *BI;
132 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
133
134 // Create a new basic block to copy instructions into!
135 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
136
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000137 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000138
139 // Make sure to capture the mapping that a return will use...
140 // TODO: This assumes that the RET is returning a value computed in the same
141 // basic block as the return was issued from!
142 //
143 const TerminatorInst *TI = BB->getTerminator();
144
145 // Loop over all instructions copying them over...
146 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000147 for (BasicBlock::const_iterator II = BB->begin();
148 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000149 IBB->getInstList().push_back((NewInst = (*II)->clone()));
150 ValueMap[*II] = NewInst; // Add instruction map to value.
151 }
152
153 // Copy over the terminator now...
Chris Lattnera41f50d2001-07-07 19:24:15 +0000154 switch (TI->getOpcode()) {
Chris Lattner00950542001-06-06 20:29:01 +0000155 case Instruction::Ret: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000156 const ReturnInst *RI = cast<const ReturnInst>(TI);
Chris Lattner00950542001-06-06 20:29:01 +0000157
158 if (PHI) { // The PHI node should include this value!
159 assert(RI->getReturnValue() && "Ret should have value!");
160 assert(RI->getReturnValue()->getType() == PHI->getType() &&
161 "Ret value not consistent in method!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000162 PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164
165 // Add a branch to the code that was after the original Call.
166 IBB->getInstList().push_back(new BranchInst(NewBB));
167 break;
168 }
169 case Instruction::Br:
170 IBB->getInstList().push_back(TI->clone());
171 break;
172
173 default:
174 cerr << "MethodInlining: Don't know how to handle terminator: " << TI;
175 abort();
176 }
177 }
178
179
Chris Lattner00950542001-06-06 20:29:01 +0000180 // Loop over all of the instructions in the method, fixing up operand
181 // references as we go. This uses ValueMap to do all the hard work.
182 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000183 for (Method::const_iterator BI = CalledMeth->begin();
184 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000185 const BasicBlock *BB = *BI;
186 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
187
188 // Loop over all instructions, fixing each one as we find it...
189 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000190 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000191 RemapInstruction(*II, ValueMap);
192 }
193
194 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
195
196 // Change the branch that used to go to NewBB to branch to the first basic
197 // block of the inlined method.
198 //
199 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnera41f50d2001-07-07 19:24:15 +0000200 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner00950542001-06-06 20:29:01 +0000201 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000202 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000203
204 // Since we are now done with the CallInst, we can finally delete it.
205 delete CI;
206 return true;
207}
208
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000209bool opt::InlineMethod(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000210 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
211 BasicBlock *PBB = CI->getParent();
212
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000213 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
214
215 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000216 "CallInst has parent that doesn't contain CallInst?!?");
217 return InlineMethod(CallIt);
218}
219
220static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) {
221 assert(CI->getParent() && CI->getParent()->getParent() &&
222 "Call not embedded into a method!");
223
224 // Don't inline a recursive call.
225 if (CI->getParent()->getParent() == M) return false;
226
227 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000228 if (M->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000229
230 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000231 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000232
233 // Go ahead and try just about anything else.
234 return true;
235}
236
237
238static inline bool DoMethodInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000239 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000240 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000241 // Check to see if we should inline this method
Chris Lattner00950542001-06-06 20:29:01 +0000242 Method *M = CI->getCalledMethod();
Chris Lattner5a0d4172001-10-13 06:52:31 +0000243 if (M && ShouldInlineMethod(CI, M))
Chris Lattner00950542001-06-06 20:29:01 +0000244 return InlineMethod(I);
245 }
246 }
247 return false;
248}
249
Chris Lattner5680ee62001-10-18 01:32:34 +0000250bool opt::MethodInlining::doMethodInlining(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000251 bool Changed = false;
252
253 // Loop through now and inline instructions a basic block at a time...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000254 for (Method::iterator I = M->begin(); I != M->end(); )
Chris Lattner00950542001-06-06 20:29:01 +0000255 if (DoMethodInlining(*I)) {
256 Changed = true;
257 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000258 I = M->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000259 } else {
260 ++I;
261 }
262
263 return Changed;
264}