blob: 9d86c8689539cc37cb2e28abdb6fee1818d8a0fb [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>
Chris Lattner697954c2002-01-20 22:54:45 +000030#include <iostream>
31using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000032
33#include "llvm/Assembly/Writer.h"
34
Chris Lattner7e02b7e2001-06-30 04:36:40 +000035using namespace opt;
36
Chris Lattner00950542001-06-06 20:29:01 +000037// RemapInstruction - Convert the instruction operands from referencing the
38// current values into those specified by ValueMap.
39//
40static inline void RemapInstruction(Instruction *I,
Chris Lattner697954c2002-01-20 22:54:45 +000041 std::map<const Value *, Value*> &ValueMap) {
Chris Lattner00950542001-06-06 20:29:01 +000042
Chris Lattnerc8b25d42001-07-07 08:36:50 +000043 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
44 const Value *Op = I->getOperand(op);
Chris Lattner00950542001-06-06 20:29:01 +000045 Value *V = ValueMap[Op];
Chris Lattnere9bb2df2001-12-03 22:26:30 +000046 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
Chris Lattner4f685282001-10-31 02:27:26 +000047 continue; // Globals and constants don't get relocated
Chris Lattner00950542001-06-06 20:29:01 +000048
49 if (!V) {
Chris Lattner697954c2002-01-20 22:54:45 +000050 cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
51 cerr << "\nInst = " << I;
Chris Lattner00950542001-06-06 20:29:01 +000052 }
53 assert(V && "Referenced value not in value map!");
54 I->setOperand(op, V);
55 }
56}
57
58// InlineMethod - This function forcibly inlines the called method 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// method by one level.
67//
Chris Lattner7e02b7e2001-06-30 04:36:40 +000068bool opt::InlineMethod(BasicBlock::iterator CIIt) {
Chris Lattnerb00c5822001-10-02 03:41:24 +000069 assert(isa<CallInst>(*CIIt) && "InlineMethod only works on CallInst nodes!");
Chris Lattner00950542001-06-06 20:29:01 +000070 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
71 assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
72
Chris Lattnerb00c5822001-10-02 03:41:24 +000073 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattner00950542001-06-06 20:29:01 +000074 const Method *CalledMeth = CI->getCalledMethod();
Chris Lattner5a0d4172001-10-13 06:52:31 +000075 if (CalledMeth == 0 || // Can't inline external method or indirect call!
76 CalledMeth->isExternal()) return false;
Chris Lattner00950542001-06-06 20:29:01 +000077
78 //cerr << "Inlining " << CalledMeth->getName() << " into "
Chris Lattner697954c2002-01-20 22:54:45 +000079 // << CurrentMeth->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000080
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
89 // Remove (unlink) the CallInst from the start of the new basic block.
90 NewBB->getInstList().remove(CI);
91
92 // If we have a return value generated by this call, convert it into a PHI
93 // node that gets values from each of the old RET instructions in the original
94 // method.
95 //
96 PHINode *PHI = 0;
97 if (CalledMeth->getReturnType() != Type::VoidTy) {
98 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
99
100 // The PHI node should go at the front of the new basic block to merge all
101 // possible incoming values.
102 //
103 NewBB->getInstList().push_front(PHI);
104
105 // Anything that used the result of the function call should now use the PHI
106 // node as their operand.
107 //
108 CI->replaceAllUsesWith(PHI);
109 }
110
111 // Keep a mapping between the original method's values and the new duplicated
112 // code's values. This includes all of: Method arguments, instruction values,
113 // constant pool entries, and basic blocks.
114 //
Chris Lattner697954c2002-01-20 22:54:45 +0000115 std::map<const Value *, Value*> ValueMap;
Chris Lattner00950542001-06-06 20:29:01 +0000116
117 // Add the method arguments to the mapping: (start counting at 1 to skip the
118 // method reference itself)
119 //
120 Method::ArgumentListType::const_iterator PTI =
121 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000122 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
123 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000124
Chris Lattner00950542001-06-06 20:29:01 +0000125 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
126
127 // Loop over all of the basic blocks in the method, inlining them as
128 // appropriate. Keep track of the first basic block of the method...
129 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000130 for (Method::const_iterator BI = CalledMeth->begin();
131 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000132 const BasicBlock *BB = *BI;
133 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
134
135 // Create a new basic block to copy instructions into!
136 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
137
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000138 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000139
140 // Make sure to capture the mapping that a return will use...
141 // TODO: This assumes that the RET is returning a value computed in the same
142 // basic block as the return was issued from!
143 //
144 const TerminatorInst *TI = BB->getTerminator();
145
146 // Loop over all instructions copying them over...
147 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000148 for (BasicBlock::const_iterator II = BB->begin();
149 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000150 IBB->getInstList().push_back((NewInst = (*II)->clone()));
151 ValueMap[*II] = NewInst; // Add instruction map to value.
152 }
153
154 // Copy over the terminator now...
Chris Lattnera41f50d2001-07-07 19:24:15 +0000155 switch (TI->getOpcode()) {
Chris Lattner00950542001-06-06 20:29:01 +0000156 case Instruction::Ret: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000157 const ReturnInst *RI = cast<const ReturnInst>(TI);
Chris Lattner00950542001-06-06 20:29:01 +0000158
159 if (PHI) { // The PHI node should include this value!
160 assert(RI->getReturnValue() && "Ret should have value!");
161 assert(RI->getReturnValue()->getType() == PHI->getType() &&
162 "Ret value not consistent in method!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000163 PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
Chris Lattner00950542001-06-06 20:29:01 +0000164 }
165
166 // Add a branch to the code that was after the original Call.
167 IBB->getInstList().push_back(new BranchInst(NewBB));
168 break;
169 }
170 case Instruction::Br:
171 IBB->getInstList().push_back(TI->clone());
172 break;
173
174 default:
175 cerr << "MethodInlining: Don't know how to handle terminator: " << TI;
176 abort();
177 }
178 }
179
180
Chris Lattner00950542001-06-06 20:29:01 +0000181 // Loop over all of the instructions in the method, fixing up operand
182 // references as we go. This uses ValueMap to do all the hard work.
183 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000184 for (Method::const_iterator BI = CalledMeth->begin();
185 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000186 const BasicBlock *BB = *BI;
187 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
188
189 // Loop over all instructions, fixing each one as we find it...
190 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000191 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000192 RemapInstruction(*II, ValueMap);
193 }
194
195 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
196
197 // Change the branch that used to go to NewBB to branch to the first basic
198 // block of the inlined method.
199 //
200 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnera41f50d2001-07-07 19:24:15 +0000201 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner00950542001-06-06 20:29:01 +0000202 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000203 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000204
205 // Since we are now done with the CallInst, we can finally delete it.
206 delete CI;
207 return true;
208}
209
Chris Lattner7e02b7e2001-06-30 04:36:40 +0000210bool opt::InlineMethod(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000211 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
212 BasicBlock *PBB = CI->getParent();
213
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000214 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
215
216 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000217 "CallInst has parent that doesn't contain CallInst?!?");
218 return InlineMethod(CallIt);
219}
220
221static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) {
222 assert(CI->getParent() && CI->getParent()->getParent() &&
223 "Call not embedded into a method!");
224
225 // Don't inline a recursive call.
226 if (CI->getParent()->getParent() == M) return false;
227
228 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000229 if (M->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000230
231 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000232 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000233
234 // Go ahead and try just about anything else.
235 return true;
236}
237
238
239static inline bool DoMethodInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000240 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000241 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000242 // Check to see if we should inline this method
Chris Lattner00950542001-06-06 20:29:01 +0000243 Method *M = CI->getCalledMethod();
Chris Lattner5a0d4172001-10-13 06:52:31 +0000244 if (M && ShouldInlineMethod(CI, M))
Chris Lattner00950542001-06-06 20:29:01 +0000245 return InlineMethod(I);
246 }
247 }
248 return false;
249}
250
Chris Lattner5680ee62001-10-18 01:32:34 +0000251bool opt::MethodInlining::doMethodInlining(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +0000252 bool Changed = false;
253
254 // Loop through now and inline instructions a basic block at a time...
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000255 for (Method::iterator I = M->begin(); I != M->end(); )
Chris Lattner00950542001-06-06 20:29:01 +0000256 if (DoMethodInlining(*I)) {
257 Changed = true;
258 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000259 I = M->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000260 } else {
261 ++I;
262 }
263
264 return Changed;
265}