1 //===- MethodInlining.cpp - Code to perform method inlining ---------------===//
3 // This file implements inlining of methods.
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
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.
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
20 //===----------------------------------------------------------------------===//
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"
31 #include "llvm/Assembly/Writer.h"
33 // RemapInstruction - Convert the instruction operands from referencing the
34 // current values into those specified by ValueMap.
36 static inline void RemapInstruction(Instruction *I,
37 map<const Value *, Value*> &ValueMap) {
39 for (unsigned op = 0; const Value *Op = I->getOperand(op); op++) {
40 Value *V = ValueMap[Op];
41 if (!V && Op->getValueType() == Value::MethodVal)
42 continue; // Methods don't get relocated
45 cerr << "Val = " << endl << Op << "Addr = " << (void*)Op << endl;
46 cerr << "Inst = " << I;
48 assert(V && "Referenced value not in value map!");
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
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.
63 bool InlineMethod(BasicBlock::InstListType::iterator CIIt) {
64 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!");
69 CallInst *CI = (CallInst*)*CIIt;
70 const Method *CalledMeth = CI->getCalledMethod();
71 Method *CurrentMeth = CI->getParent()->getParent();
73 //cerr << "Inlining " << CalledMeth->getName() << " into "
74 // << CurrentMeth->getName() << endl;
76 BasicBlock *OrigBB = CI->getParent();
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.
82 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
84 // Remove (unlink) the CallInst from the start of the new basic block.
85 NewBB->getInstList().remove(CI);
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
92 if (CalledMeth->getReturnType() != Type::VoidTy) {
93 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
95 // The PHI node should go at the front of the new basic block to merge all
96 // possible incoming values.
98 NewBB->getInstList().push_front(PHI);
100 // Anything that used the result of the function call should now use the PHI
101 // node as their operand.
103 CI->replaceAllUsesWith(PHI);
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.
110 map<const Value *, Value*> ValueMap;
112 // Add the method arguments to the mapping: (start counting at 1 to skip the
113 // method reference itself)
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;
122 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
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...
127 for (Method::BasicBlocksType::const_iterator BI =
128 CalledMeth->getBasicBlocks().begin();
129 BI != CalledMeth->getBasicBlocks().end(); BI++) {
130 const BasicBlock *BB = *BI;
131 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
133 // Create a new basic block to copy instructions into!
134 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
136 ValueMap[*BI] = IBB; // Add basic block mapping.
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!
142 const TerminatorInst *TI = BB->getTerminator();
144 // Loop over all instructions copying them over...
145 Instruction *NewInst;
146 for (BasicBlock::InstListType::const_iterator II = BB->getInstList().begin();
147 II != (BB->getInstList().end()-1); II++) {
148 IBB->getInstList().push_back((NewInst = (*II)->clone()));
149 ValueMap[*II] = NewInst; // Add instruction map to value.
152 // Copy over the terminator now...
153 switch (TI->getInstType()) {
154 case Instruction::Ret: {
155 const ReturnInst *RI = (const ReturnInst*)TI;
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!");
161 PHI->addIncoming((Value*)RI->getReturnValue());
164 // Add a branch to the code that was after the original Call.
165 IBB->getInstList().push_back(new BranchInst(NewBB));
168 case Instruction::Br:
169 IBB->getInstList().push_back(TI->clone());
173 cerr << "MethodInlining: Don't know how to handle terminator: " << TI;
179 // Copy over the constant pool...
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
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.
196 for (Method::BasicBlocksType::const_iterator BI =
197 CalledMeth->getBasicBlocks().begin();
198 BI != CalledMeth->getBasicBlocks().end(); BI++) {
199 const BasicBlock *BB = *BI;
200 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
202 // Loop over all instructions, fixing each one as we find it...
204 for (BasicBlock::InstListType::iterator II = NBB->getInstList().begin();
205 II != NBB->getInstList().end(); II++)
206 RemapInstruction(*II, ValueMap);
209 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
211 // Change the branch that used to go to NewBB to branch to the first basic
212 // block of the inlined method.
214 TerminatorInst *Br = OrigBB->getTerminator();
215 assert(Br && Br->getInstType() == Instruction::Br &&
216 "splitBasicBlock broken!");
217 Br->setOperand(0, ValueMap[CalledMeth->getBasicBlocks().front()]);
219 // Since we are now done with the CallInst, we can finally delete it.
224 bool InlineMethod(CallInst *CI) {
225 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
226 BasicBlock *PBB = CI->getParent();
228 BasicBlock::InstListType::iterator CallIt = find(PBB->getInstList().begin(),
229 PBB->getInstList().end(),
231 assert(CallIt != PBB->getInstList().end() &&
232 "CallInst has parent that doesn't contain CallInst?!?");
233 return InlineMethod(CallIt);
236 static inline bool ShouldInlineMethod(const CallInst *CI, const Method *M) {
237 assert(CI->getParent() && CI->getParent()->getParent() &&
238 "Call not embedded into a method!");
240 // Don't inline a recursive call.
241 if (CI->getParent()->getParent() == M) return false;
243 // Don't inline something too big. This is a really crappy heuristic
244 if (M->getBasicBlocks().size() > 3) return false;
246 // Don't inline into something too big. This is a **really** crappy heuristic
247 if (CI->getParent()->getParent()->getBasicBlocks().size() > 10) return false;
249 // Go ahead and try just about anything else.
254 static inline bool DoMethodInlining(BasicBlock *BB) {
255 for (BasicBlock::InstListType::iterator I = BB->getInstList().begin();
256 I != BB->getInstList().end(); I++) {
257 if ((*I)->getInstType() == Instruction::Call) {
258 // Check to see if we should inline this method
259 CallInst *CI = (CallInst*)*I;
260 Method *M = CI->getCalledMethod();
261 if (ShouldInlineMethod(CI, M))
262 return InlineMethod(I);
268 bool DoMethodInlining(Method *M) {
269 Method::BasicBlocksType &BBs = M->getBasicBlocks();
270 bool Changed = false;
272 // Loop through now and inline instructions a basic block at a time...
273 for (Method::BasicBlocksType::iterator I = BBs.begin(); I != BBs.end(); )
274 if (DoMethodInlining(*I)) {
276 // Iterator is now invalidated by new basic blocks inserted