blob: 461f5974f4cce505605bb589f15a1e5aa965c394 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- FunctionInlining.cpp - Code to perform function inlining -----------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00003// This file implements inlining of functions.
Chris Lattner00950542001-06-06 20:29:01 +00004//
5// Specifically, this:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00006// * Exports functionality to inline any function call
7// * Inlines functions that consist of a single basic block
8// * Is able to inline ANY function call
9// . Has a smart heuristic for when to inline a function
Chris Lattner00950542001-06-06 20:29:01 +000010//
11// Notice that:
Chris Lattner59b6b8e2002-01-21 23:17:48 +000012// * This pass opens up a lot of opportunities for constant propogation. It
13// is a good idea to to run a constant propogation pass, then a DCE pass
Chris Lattner00950542001-06-06 20:29:01 +000014// sometime after running this pass.
15//
Chris Lattner00950542001-06-06 20:29:01 +000016//===----------------------------------------------------------------------===//
17
Chris Lattner59b6b8e2002-01-21 23:17:48 +000018#include "llvm/Transforms/MethodInlining.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000020#include "llvm/Function.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/iTerminators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000023#include "llvm/iPHINode.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include "llvm/iOther.h"
Chris Lattner237e6d12002-04-08 22:03:00 +000025#include "llvm/Type.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <algorithm>
27#include <map>
Chris Lattner697954c2002-01-20 22:54:45 +000028#include <iostream>
29using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000030
Chris Lattner00950542001-06-06 20:29:01 +000031// RemapInstruction - Convert the instruction operands from referencing the
32// current values into those specified by ValueMap.
33//
34static inline void RemapInstruction(Instruction *I,
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::map<const Value *, Value*> &ValueMap) {
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattnerc8b25d42001-07-07 08:36:50 +000037 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
38 const Value *Op = I->getOperand(op);
Chris Lattner00950542001-06-06 20:29:01 +000039 Value *V = ValueMap[Op];
Chris Lattnere9bb2df2001-12-03 22:26:30 +000040 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
Chris Lattner4f685282001-10-31 02:27:26 +000041 continue; // Globals and constants don't get relocated
Chris Lattner00950542001-06-06 20:29:01 +000042
43 if (!V) {
Chris Lattner697954c2002-01-20 22:54:45 +000044 cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
45 cerr << "\nInst = " << I;
Chris Lattner00950542001-06-06 20:29:01 +000046 }
47 assert(V && "Referenced value not in value map!");
48 I->setOperand(op, V);
49 }
50}
51
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000052// InlineMethod - This function forcibly inlines the called function into the
Chris Lattner00950542001-06-06 20:29:01 +000053// basic block of the caller. This returns false if it is not possible to
54// inline this call. The program is still in a well defined state if this
55// occurs though.
56//
57// Note that this only does one level of inlining. For example, if the
58// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
59// exists in the instruction stream. Similiarly this will inline a recursive
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000060// function by one level.
Chris Lattner00950542001-06-06 20:29:01 +000061//
Chris Lattner59b6b8e2002-01-21 23:17:48 +000062bool InlineMethod(BasicBlock::iterator CIIt) {
Chris Lattnerb00c5822001-10-02 03:41:24 +000063 assert(isa<CallInst>(*CIIt) && "InlineMethod only works on CallInst nodes!");
Chris Lattner00950542001-06-06 20:29:01 +000064 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000065 assert((*CIIt)->getParent()->getParent() && "Instruction not in function!");
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattnerb00c5822001-10-02 03:41:24 +000067 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattnerdc89f872002-03-29 17:08:29 +000068 const Function *CalledMeth = CI->getCalledFunction();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000069 if (CalledMeth == 0 || // Can't inline external function or indirect call!
Chris Lattner5a0d4172001-10-13 06:52:31 +000070 CalledMeth->isExternal()) return false;
Chris Lattner00950542001-06-06 20:29:01 +000071
72 //cerr << "Inlining " << CalledMeth->getName() << " into "
Chris Lattner697954c2002-01-20 22:54:45 +000073 // << CurrentMeth->getName() << "\n";
Chris Lattner00950542001-06-06 20:29:01 +000074
75 BasicBlock *OrigBB = CI->getParent();
76
77 // Call splitBasicBlock - The original basic block now ends at the instruction
78 // immediately before the call. The original basic block now ends with an
79 // unconditional branch to NewBB, and NewBB starts with the call instruction.
80 //
81 BasicBlock *NewBB = OrigBB->splitBasicBlock(CIIt);
Chris Lattner41b66b12002-02-25 00:31:02 +000082 NewBB->setName("InlinedFunctionReturnNode");
Chris Lattner00950542001-06-06 20:29:01 +000083
84 // Remove (unlink) the CallInst from the start of the new basic block.
85 NewBB->getInstList().remove(CI);
86
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000089 // function.
Chris Lattner00950542001-06-06 20:29:01 +000090 //
91 PHINode *PHI = 0;
92 if (CalledMeth->getReturnType() != Type::VoidTy) {
93 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
94
95 // The PHI node should go at the front of the new basic block to merge all
96 // possible incoming values.
97 //
98 NewBB->getInstList().push_front(PHI);
99
100 // Anything that used the result of the function call should now use the PHI
101 // node as their operand.
102 //
103 CI->replaceAllUsesWith(PHI);
104 }
105
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000106 // Keep a mapping between the original function's values and the new
107 // duplicated code's values. This includes all of: Function arguments,
108 // instruction values, constant pool entries, and basic blocks.
Chris Lattner00950542001-06-06 20:29:01 +0000109 //
Chris Lattner697954c2002-01-20 22:54:45 +0000110 std::map<const Value *, Value*> ValueMap;
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000112 // Add the function arguments to the mapping: (start counting at 1 to skip the
113 // function reference itself)
Chris Lattner00950542001-06-06 20:29:01 +0000114 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000115 Function::ArgumentListType::const_iterator PTI =
Chris Lattner00950542001-06-06 20:29:01 +0000116 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000117 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
118 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000119
Chris Lattner00950542001-06-06 20:29:01 +0000120 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
121
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000122 // Loop over all of the basic blocks in the function, inlining them as
123 // appropriate. Keep track of the first basic block of the function...
Chris Lattner00950542001-06-06 20:29:01 +0000124 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000125 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000126 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000127 const BasicBlock *BB = *BI;
128 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
129
130 // Create a new basic block to copy instructions into!
131 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
Chris Lattner41b66b12002-02-25 00:31:02 +0000132 if (BB->hasName()) IBB->setName(BB->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000133
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000134 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000135
136 // Make sure to capture the mapping that a return will use...
137 // TODO: This assumes that the RET is returning a value computed in the same
138 // basic block as the return was issued from!
139 //
140 const TerminatorInst *TI = BB->getTerminator();
141
142 // Loop over all instructions copying them over...
143 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000144 for (BasicBlock::const_iterator II = BB->begin();
145 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000146 IBB->getInstList().push_back((NewInst = (*II)->clone()));
147 ValueMap[*II] = NewInst; // Add instruction map to value.
Chris Lattner41b66b12002-02-25 00:31:02 +0000148 if ((*II)->hasName())
149 NewInst->setName((*II)->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
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() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000160 "Ret value not consistent in function!");
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:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000173 cerr << "FunctionInlining: Don't know how to handle terminator: " << TI;
Chris Lattner00950542001-06-06 20:29:01 +0000174 abort();
175 }
176 }
177
178
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000179 // Loop over all of the instructions in the function, fixing up operand
Chris Lattner00950542001-06-06 20:29:01 +0000180 // references as we go. This uses ValueMap to do all the hard work.
181 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000182 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000183 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000196 // block of the inlined function.
Chris Lattner00950542001-06-06 20:29:01 +0000197 //
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 Lattner59b6b8e2002-01-21 23:17:48 +0000208bool 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000219static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000220 assert(CI->getParent() && CI->getParent()->getParent() &&
221 "Call not embedded into a method!");
222
223 // Don't inline a recursive call.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000224 if (CI->getParent()->getParent() == F) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000225
226 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000227 if (F->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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000237static inline bool DoFunctionInlining(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 Lattner2fbfdcf2002-04-07 20:49:59 +0000240 // Check to see if we should inline this function
241 Function *F = CI->getCalledFunction();
242 if (F && ShouldInlineFunction(CI, F))
Chris Lattner00950542001-06-06 20:29:01 +0000243 return InlineMethod(I);
244 }
245 }
246 return false;
247}
248
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000249// doFunctionInlining - Use a heuristic based approach to inline functions that
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000250// seem to look good.
251//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000252static bool doFunctionInlining(Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000253 bool Changed = false;
254
255 // Loop through now and inline instructions a basic block at a time...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000256 for (Function::iterator I = F->begin(); I != F->end(); )
257 if (DoFunctionInlining(*I)) {
Chris Lattner00950542001-06-06 20:29:01 +0000258 Changed = true;
259 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000260 I = F->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000261 } else {
262 ++I;
263 }
264
265 return Changed;
266}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000267
268namespace {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269 struct FunctionInlining : public MethodPass {
270 virtual bool runOnMethod(Function *F) {
271 return doFunctionInlining(F);
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000272 }
273 };
274}
275
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000276Pass *createMethodInliningPass() { return new FunctionInlining(); }