blob: 12430e12702f43d9a9ca152ebc3102645d8e9d41 [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 Lattner01545052002-04-18 18:52:03 +000016// FIXME: This pass should transform alloca instructions in the called function
17// into malloc/free pairs!
18//
Chris Lattner00950542001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner483e14e2002-04-27 07:27:19 +000021#include "llvm/Transforms/FunctionInlining.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/Module.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023#include "llvm/Function.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000024#include "llvm/Pass.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 Lattner237e6d12002-04-08 22:03:00 +000028#include "llvm/Type.h"
Chris Lattner73e21422002-04-09 19:48:49 +000029#include "llvm/Argument.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000030#include "Support/StatisticReporter.h"
31
32static Statistic<> NumInlined("inline\t\t- Number of functions inlined");
Chris Lattner00950542001-06-06 20:29:01 +000033#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000034#include <iostream>
35using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000036
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
Chris Lattnerf57b8452002-04-27 06:56:12 +000058// InlineFunction - This function forcibly inlines the called function into the
Chris Lattner00950542001-06-06 20:29:01 +000059// 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000066// function by one level.
Chris Lattner00950542001-06-06 20:29:01 +000067//
Chris Lattnerf57b8452002-04-27 06:56:12 +000068bool InlineFunction(BasicBlock::iterator CIIt) {
69 assert(isa<CallInst>(*CIIt) && "InlineFunction only works on CallInst nodes");
Chris Lattner00950542001-06-06 20:29:01 +000070 assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000071 assert((*CIIt)->getParent()->getParent() && "Instruction not in function!");
Chris Lattner00950542001-06-06 20:29:01 +000072
Chris Lattnerb00c5822001-10-02 03:41:24 +000073 CallInst *CI = cast<CallInst>(*CIIt);
Chris Lattnerdc89f872002-03-29 17:08:29 +000074 const Function *CalledMeth = CI->getCalledFunction();
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000075 if (CalledMeth == 0 || // Can't inline external function or indirect call!
Chris Lattner5a0d4172001-10-13 06:52:31 +000076 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);
Chris Lattner41b66b12002-02-25 00:31:02 +000088 NewBB->setName("InlinedFunctionReturnNode");
Chris Lattner00950542001-06-06 20:29:01 +000089
90 // Remove (unlink) the CallInst from the start of the new basic block.
91 NewBB->getInstList().remove(CI);
92
93 // If we have a return value generated by this call, convert it into a PHI
94 // node that gets values from each of the old RET instructions in the original
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000095 // function.
Chris Lattner00950542001-06-06 20:29:01 +000096 //
97 PHINode *PHI = 0;
98 if (CalledMeth->getReturnType() != Type::VoidTy) {
99 PHI = new PHINode(CalledMeth->getReturnType(), CI->getName());
100
101 // The PHI node should go at the front of the new basic block to merge all
102 // possible incoming values.
103 //
104 NewBB->getInstList().push_front(PHI);
105
106 // Anything that used the result of the function call should now use the PHI
107 // node as their operand.
108 //
109 CI->replaceAllUsesWith(PHI);
110 }
111
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000112 // Keep a mapping between the original function's values and the new
113 // duplicated code's values. This includes all of: Function arguments,
114 // instruction values, constant pool entries, and basic blocks.
Chris Lattner00950542001-06-06 20:29:01 +0000115 //
Chris Lattner697954c2002-01-20 22:54:45 +0000116 std::map<const Value *, Value*> ValueMap;
Chris Lattner00950542001-06-06 20:29:01 +0000117
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000118 // Add the function arguments to the mapping: (start counting at 1 to skip the
119 // function reference itself)
Chris Lattner00950542001-06-06 20:29:01 +0000120 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000121 Function::ArgumentListType::const_iterator PTI =
Chris Lattner00950542001-06-06 20:29:01 +0000122 CalledMeth->getArgumentList().begin();
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000123 for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
124 ValueMap[*PTI] = CI->getOperand(a);
Chris Lattner00950542001-06-06 20:29:01 +0000125
Chris Lattner00950542001-06-06 20:29:01 +0000126 ValueMap[NewBB] = NewBB; // Returns get converted to reference NewBB
127
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 // Loop over all of the basic blocks in the function, inlining them as
129 // appropriate. Keep track of the first basic block of the function...
Chris Lattner00950542001-06-06 20:29:01 +0000130 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000131 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000132 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000133 const BasicBlock *BB = *BI;
134 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
135
136 // Create a new basic block to copy instructions into!
137 BasicBlock *IBB = new BasicBlock("", NewBB->getParent());
Chris Lattner41b66b12002-02-25 00:31:02 +0000138 if (BB->hasName()) IBB->setName(BB->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000139
Chris Lattner5fdc4c92001-10-14 23:29:30 +0000140 ValueMap[BB] = IBB; // Add basic block mapping.
Chris Lattner00950542001-06-06 20:29:01 +0000141
142 // Make sure to capture the mapping that a return will use...
143 // TODO: This assumes that the RET is returning a value computed in the same
144 // basic block as the return was issued from!
145 //
146 const TerminatorInst *TI = BB->getTerminator();
147
148 // Loop over all instructions copying them over...
149 Instruction *NewInst;
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000150 for (BasicBlock::const_iterator II = BB->begin();
151 II != (BB->end()-1); ++II) {
Chris Lattner00950542001-06-06 20:29:01 +0000152 IBB->getInstList().push_back((NewInst = (*II)->clone()));
153 ValueMap[*II] = NewInst; // Add instruction map to value.
Chris Lattner41b66b12002-02-25 00:31:02 +0000154 if ((*II)->hasName())
155 NewInst->setName((*II)->getName()+".i"); // .i = inlined once
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
158 // Copy over the terminator now...
Chris Lattnera41f50d2001-07-07 19:24:15 +0000159 switch (TI->getOpcode()) {
Chris Lattner00950542001-06-06 20:29:01 +0000160 case Instruction::Ret: {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000161 const ReturnInst *RI = cast<const ReturnInst>(TI);
Chris Lattner00950542001-06-06 20:29:01 +0000162
163 if (PHI) { // The PHI node should include this value!
164 assert(RI->getReturnValue() && "Ret should have value!");
165 assert(RI->getReturnValue()->getType() == PHI->getType() &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000166 "Ret value not consistent in function!");
Chris Lattnerb00c5822001-10-02 03:41:24 +0000167 PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169
170 // Add a branch to the code that was after the original Call.
171 IBB->getInstList().push_back(new BranchInst(NewBB));
172 break;
173 }
174 case Instruction::Br:
175 IBB->getInstList().push_back(TI->clone());
176 break;
177
178 default:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000179 cerr << "FunctionInlining: Don't know how to handle terminator: " << TI;
Chris Lattner00950542001-06-06 20:29:01 +0000180 abort();
181 }
182 }
183
184
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000185 // Loop over all of the instructions in the function, fixing up operand
Chris Lattner00950542001-06-06 20:29:01 +0000186 // references as we go. This uses ValueMap to do all the hard work.
187 //
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000188 for (Function::const_iterator BI = CalledMeth->begin();
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000189 BI != CalledMeth->end(); ++BI) {
Chris Lattner00950542001-06-06 20:29:01 +0000190 const BasicBlock *BB = *BI;
191 BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
192
193 // Loop over all instructions, fixing each one as we find it...
194 //
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000195 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
Chris Lattner00950542001-06-06 20:29:01 +0000196 RemapInstruction(*II, ValueMap);
197 }
198
199 if (PHI) RemapInstruction(PHI, ValueMap); // Fix the PHI node also...
200
201 // Change the branch that used to go to NewBB to branch to the first basic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000202 // block of the inlined function.
Chris Lattner00950542001-06-06 20:29:01 +0000203 //
204 TerminatorInst *Br = OrigBB->getTerminator();
Chris Lattnera41f50d2001-07-07 19:24:15 +0000205 assert(Br && Br->getOpcode() == Instruction::Br &&
Chris Lattner00950542001-06-06 20:29:01 +0000206 "splitBasicBlock broken!");
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000207 Br->setOperand(0, ValueMap[CalledMeth->front()]);
Chris Lattner00950542001-06-06 20:29:01 +0000208
209 // Since we are now done with the CallInst, we can finally delete it.
210 delete CI;
211 return true;
212}
213
Chris Lattnerf57b8452002-04-27 06:56:12 +0000214bool InlineFunction(CallInst *CI) {
Chris Lattner00950542001-06-06 20:29:01 +0000215 assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
216 BasicBlock *PBB = CI->getParent();
217
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000218 BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
219
220 assert(CallIt != PBB->end() &&
Chris Lattner00950542001-06-06 20:29:01 +0000221 "CallInst has parent that doesn't contain CallInst?!?");
Chris Lattnerf57b8452002-04-27 06:56:12 +0000222 return InlineFunction(CallIt);
Chris Lattner00950542001-06-06 20:29:01 +0000223}
224
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000225static inline bool ShouldInlineFunction(const CallInst *CI, const Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000226 assert(CI->getParent() && CI->getParent()->getParent() &&
Chris Lattnerf57b8452002-04-27 06:56:12 +0000227 "Call not embedded into a function!");
Chris Lattner00950542001-06-06 20:29:01 +0000228
229 // Don't inline a recursive call.
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000230 if (CI->getParent()->getParent() == F) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000231
232 // Don't inline something too big. This is a really crappy heuristic
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000233 if (F->size() > 3) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000234
235 // Don't inline into something too big. This is a **really** crappy heuristic
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000236 if (CI->getParent()->getParent()->size() > 10) return false;
Chris Lattner00950542001-06-06 20:29:01 +0000237
238 // Go ahead and try just about anything else.
239 return true;
240}
241
242
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000243static inline bool DoFunctionInlining(BasicBlock *BB) {
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000244 for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000245 if (CallInst *CI = dyn_cast<CallInst>(*I)) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000246 // Check to see if we should inline this function
247 Function *F = CI->getCalledFunction();
248 if (F && ShouldInlineFunction(CI, F))
Chris Lattnerf57b8452002-04-27 06:56:12 +0000249 return InlineFunction(I);
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
251 }
252 return false;
253}
254
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000255// doFunctionInlining - Use a heuristic based approach to inline functions that
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000256// seem to look good.
257//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000258static bool doFunctionInlining(Function *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000259 bool Changed = false;
260
261 // Loop through now and inline instructions a basic block at a time...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000262 for (Function::iterator I = F->begin(); I != F->end(); )
263 if (DoFunctionInlining(*I)) {
Chris Lattner3dec1f22002-05-10 15:38:35 +0000264 ++NumInlined;
Chris Lattner00950542001-06-06 20:29:01 +0000265 Changed = true;
266 // Iterator is now invalidated by new basic blocks inserted
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267 I = F->begin();
Chris Lattner00950542001-06-06 20:29:01 +0000268 } else {
269 ++I;
270 }
271
272 return Changed;
273}
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000274
275namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000276 struct FunctionInlining : public FunctionPass {
Chris Lattner96c466b2002-04-29 14:57:45 +0000277 const char *getPassName() const { return "Function Inlining"; }
Chris Lattnerf57b8452002-04-27 06:56:12 +0000278 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000279 return doFunctionInlining(F);
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000280 }
281 };
282}
283
Chris Lattnerf57b8452002-04-27 06:56:12 +0000284Pass *createFunctionInliningPass() { return new FunctionInlining(); }