blob: 52d75735ef749a998634c543add96773492c6af2 [file] [log] [blame]
Chris Lattner237ef562003-08-31 19:10:30 +00001//===- InlineSimple.cpp - Code to perform simple function inlining --------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnerca398dc2003-05-29 15:11:31 +000010// This file implements bottom-up inlining of functions into callees.
Chris Lattner01545052002-04-18 18:52:03 +000011//
Chris Lattner00950542001-06-06 20:29:01 +000012//===----------------------------------------------------------------------===//
13
Chris Lattner237ef562003-08-31 19:10:30 +000014#include "Inliner.h"
Chris Lattner49fbff42005-05-18 04:30:33 +000015#include "llvm/CallingConv.h"
Chris Lattner869adc22003-11-21 21:46:09 +000016#include "llvm/Instructions.h"
Chris Lattner8db93f12004-11-22 17:21:44 +000017#include "llvm/IntrinsicInst.h"
Chris Lattner237ef562003-08-31 19:10:30 +000018#include "llvm/Function.h"
Chris Lattner619d3542004-03-13 23:15:45 +000019#include "llvm/Type.h"
Chris Lattnere5445332003-08-24 06:59:28 +000020#include "llvm/Support/CallSite.h"
Chris Lattner237ef562003-08-31 19:10:30 +000021#include "llvm/Transforms/IPO.h"
Chris Lattner869adc22003-11-21 21:46:09 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerbd0ef772002-02-26 21:46:54 +000024namespace {
Chris Lattner619d3542004-03-13 23:15:45 +000025 struct ArgInfo {
26 unsigned ConstantWeight;
27 unsigned AllocaWeight;
28
29 ArgInfo(unsigned CWeight, unsigned AWeight)
30 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
31 };
32
Chris Lattner884d6c42003-10-06 15:52:43 +000033 // FunctionInfo - For each function, calculate the size of it in blocks and
34 // instructions.
35 struct FunctionInfo {
Chris Lattner869adc22003-11-21 21:46:09 +000036 // NumInsts, NumBlocks - Keep track of how large each function is, which is
37 // used to estimate the code size cost of inlining it.
Chris Lattner884d6c42003-10-06 15:52:43 +000038 unsigned NumInsts, NumBlocks;
39
Chris Lattner619d3542004-03-13 23:15:45 +000040 // ArgumentWeights - Each formal argument of the function is inspected to
41 // see if it is used in any contexts where making it a constant or alloca
Chris Lattner869adc22003-11-21 21:46:09 +000042 // would reduce the code size. If so, we add some value to the argument
43 // entry here.
Chris Lattner619d3542004-03-13 23:15:45 +000044 std::vector<ArgInfo> ArgumentWeights;
Chris Lattner869adc22003-11-21 21:46:09 +000045
Chris Lattnerccca3ca2006-01-13 19:35:43 +000046 FunctionInfo() : NumInsts(0), NumBlocks(0) {}
Chris Lattneree45f4c2004-08-12 05:45:09 +000047
48 /// analyzeFunction - Fill in the current structure with information gleaned
49 /// from the specified function.
50 void analyzeFunction(Function *F);
Chris Lattner884d6c42003-10-06 15:52:43 +000051 };
52
53 class SimpleInliner : public Inliner {
54 std::map<const Function*, FunctionInfo> CachedFunctionInfo;
55 public:
Chris Lattner237ef562003-08-31 19:10:30 +000056 int getInlineCost(CallSite CS);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000057 };
Chris Lattner7f8897f2006-08-27 22:42:52 +000058 RegisterPass<SimpleInliner> X("inline", "Function Integration/Inlining");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000059}
60
Chris Lattnerb12914b2004-09-20 04:48:05 +000061ModulePass *llvm::createFunctionInliningPass() { return new SimpleInliner(); }
Chris Lattner869adc22003-11-21 21:46:09 +000062
63// CountCodeReductionForConstant - Figure out an approximation for how many
64// instructions will be constant folded if the specified value is constant.
65//
66static unsigned CountCodeReductionForConstant(Value *V) {
67 unsigned Reduction = 0;
68 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI)
69 if (isa<BranchInst>(*UI))
70 Reduction += 40; // Eliminating a conditional branch is a big win
71 else if (SwitchInst *SI = dyn_cast<SwitchInst>(*UI))
72 // Eliminating a switch is a big win, proportional to the number of edges
73 // deleted.
74 Reduction += (SI->getNumSuccessors()-1) * 40;
75 else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
76 // Turning an indirect call into a direct call is a BIG win
77 Reduction += CI->getCalledValue() == V ? 500 : 0;
78 } else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) {
79 // Turning an indirect call into a direct call is a BIG win
Chris Lattner6d1db012003-11-21 21:57:29 +000080 Reduction += II->getCalledValue() == V ? 500 : 0;
Chris Lattner869adc22003-11-21 21:46:09 +000081 } else {
82 // Figure out if this instruction will be removed due to simple constant
83 // propagation.
84 Instruction &Inst = cast<Instruction>(**UI);
85 bool AllOperandsConstant = true;
86 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
Reid Spencer460f16c2004-07-18 00:32:14 +000087 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
Chris Lattner869adc22003-11-21 21:46:09 +000088 AllOperandsConstant = false;
89 break;
90 }
91
92 if (AllOperandsConstant) {
93 // We will get to remove this instruction...
94 Reduction += 7;
Misha Brukmanfd939082005-04-21 23:48:37 +000095
Chris Lattner869adc22003-11-21 21:46:09 +000096 // And any other instructions that use it which become constants
97 // themselves.
98 Reduction += CountCodeReductionForConstant(&Inst);
99 }
100 }
101
102 return Reduction;
103}
Chris Lattnerca398dc2003-05-29 15:11:31 +0000104
Chris Lattner619d3542004-03-13 23:15:45 +0000105// CountCodeReductionForAlloca - Figure out an approximation of how much smaller
106// the function will be if it is inlined into a context where an argument
107// becomes an alloca.
108//
109static unsigned CountCodeReductionForAlloca(Value *V) {
110 if (!isa<PointerType>(V->getType())) return 0; // Not a pointer
111 unsigned Reduction = 0;
112 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
113 Instruction *I = cast<Instruction>(*UI);
114 if (isa<LoadInst>(I) || isa<StoreInst>(I))
115 Reduction += 10;
116 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
117 // If the GEP has variable indices, we won't be able to do much with it.
118 for (Instruction::op_iterator I = GEP->op_begin()+1, E = GEP->op_end();
119 I != E; ++I)
120 if (!isa<Constant>(*I)) return 0;
121 Reduction += CountCodeReductionForAlloca(GEP)+15;
122 } else {
123 // If there is some other strange instruction, we're not going to be able
124 // to do much if we inline this.
125 return 0;
126 }
127 }
128
129 return Reduction;
130}
131
Chris Lattneree45f4c2004-08-12 05:45:09 +0000132/// analyzeFunction - Fill in the current structure with information gleaned
133/// from the specified function.
134void FunctionInfo::analyzeFunction(Function *F) {
135 unsigned NumInsts = 0, NumBlocks = 0;
136
137 // Look at the size of the callee. Each basic block counts as 20 units, and
138 // each instruction counts as 10.
139 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
140 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
Chris Lattner7b166d92006-09-09 20:40:44 +0000141 II != E; ++II) {
142 if (isa<DbgInfoIntrinsic>(II)) continue; // Debug intrinsics don't count.
143
Reid Spencer3da59db2006-11-27 01:05:10 +0000144 // Noop casts, including ptr <-> int, don't count.
Chris Lattner7b166d92006-09-09 20:40:44 +0000145 if (const CastInst *CI = dyn_cast<CastInst>(II)) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000146 if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) ||
147 isa<PtrToIntInst>(CI))
Chris Lattner7b166d92006-09-09 20:40:44 +0000148 continue;
Chris Lattner7b166d92006-09-09 20:40:44 +0000149 } else if (const GetElementPtrInst *GEPI =
150 dyn_cast<GetElementPtrInst>(II)) {
151 // If a GEP has all constant indices, it will probably be folded with
152 // a load/store.
153 bool AllConstant = true;
154 for (unsigned i = 1, e = GEPI->getNumOperands(); i != e; ++i)
155 if (!isa<ConstantInt>(GEPI->getOperand(i))) {
156 AllConstant = false;
157 break;
158 }
159 if (AllConstant) continue;
160 }
161
162 ++NumInsts;
163 }
Chris Lattneree45f4c2004-08-12 05:45:09 +0000164
165 ++NumBlocks;
166 }
167
168 this->NumBlocks = NumBlocks;
169 this->NumInsts = NumInsts;
170
171 // Check out all of the arguments to the function, figuring out how much
172 // code can be eliminated if one of the arguments is a constant.
Chris Lattnere4d5c442005-03-15 04:54:21 +0000173 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Chris Lattneree45f4c2004-08-12 05:45:09 +0000174 ArgumentWeights.push_back(ArgInfo(CountCodeReductionForConstant(I),
175 CountCodeReductionForAlloca(I)));
176}
177
178
Chris Lattner237ef562003-08-31 19:10:30 +0000179// getInlineCost - The heuristic used to determine if we should inline the
180// function call or not.
Chris Lattnerca398dc2003-05-29 15:11:31 +0000181//
Chris Lattner237ef562003-08-31 19:10:30 +0000182int SimpleInliner::getInlineCost(CallSite CS) {
Chris Lattnere5445332003-08-24 06:59:28 +0000183 Instruction *TheCall = CS.getInstruction();
Chris Lattner869adc22003-11-21 21:46:09 +0000184 Function *Callee = CS.getCalledFunction();
Chris Lattnere5445332003-08-24 06:59:28 +0000185 const Function *Caller = TheCall->getParent()->getParent();
Chris Lattnerca398dc2003-05-29 15:11:31 +0000186
Chris Lattner237ef562003-08-31 19:10:30 +0000187 // Don't inline a directly recursive call.
188 if (Caller == Callee) return 2000000000;
189
190 // InlineCost - This value measures how good of an inline candidate this call
191 // site is to inline. A lower inline cost make is more likely for the call to
192 // be inlined. This value may go negative.
Chris Lattnerca398dc2003-05-29 15:11:31 +0000193 //
Chris Lattner237ef562003-08-31 19:10:30 +0000194 int InlineCost = 0;
Chris Lattnerca398dc2003-05-29 15:11:31 +0000195
196 // If there is only one call of the function, and it has internal linkage,
197 // make it almost guaranteed to be inlined.
198 //
Chris Lattnerbacc7732003-10-20 05:54:26 +0000199 if (Callee->hasInternalLinkage() && Callee->hasOneUse())
Chris Lattnera8e47502004-11-09 08:05:23 +0000200 InlineCost -= 30000;
Chris Lattnerca398dc2003-05-29 15:11:31 +0000201
Chris Lattner49fbff42005-05-18 04:30:33 +0000202 // If this function uses the coldcc calling convention, prefer not to inline
203 // it.
204 if (Callee->getCallingConv() == CallingConv::Cold)
205 InlineCost += 2000;
206
207 // If the instruction after the call, or if the normal destination of the
208 // invoke is an unreachable instruction, the function is noreturn. As such,
209 // there is little point in inlining this.
210 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
211 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
212 InlineCost += 10000;
213 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
214 InlineCost += 10000;
215
Chris Lattner869adc22003-11-21 21:46:09 +0000216 // Get information about the callee...
Chris Lattner884d6c42003-10-06 15:52:43 +0000217 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
Chris Lattnerca398dc2003-05-29 15:11:31 +0000218
Chris Lattneree45f4c2004-08-12 05:45:09 +0000219 // If we haven't calculated this information yet, do so now.
220 if (CalleeFI.NumBlocks == 0)
221 CalleeFI.analyzeFunction(Callee);
Chris Lattner884d6c42003-10-06 15:52:43 +0000222
Chris Lattner869adc22003-11-21 21:46:09 +0000223 // Add to the inline quality for properties that make the call valuable to
224 // inline. This includes factors that indicate that the result of inlining
225 // the function will be optimizable. Currently this just looks at arguments
226 // passed into the function.
227 //
228 unsigned ArgNo = 0;
229 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
230 I != E; ++I, ++ArgNo) {
231 // Each argument passed in has a cost at both the caller and the callee
232 // sides. This favors functions that take many arguments over functions
233 // that take few arguments.
234 InlineCost -= 20;
235
236 // If this is a function being passed in, it is very likely that we will be
237 // able to turn an indirect function call into a direct function call.
238 if (isa<Function>(I))
239 InlineCost -= 100;
240
241 // If an alloca is passed in, inlining this function is likely to allow
242 // significant future optimization possibilities (like scalar promotion, and
243 // scalarization), so encourage the inlining of the function.
244 //
Reid Spencer3ed469c2006-11-02 20:25:50 +0000245 else if (isa<AllocaInst>(I)) {
Chris Lattner619d3542004-03-13 23:15:45 +0000246 if (ArgNo < CalleeFI.ArgumentWeights.size())
247 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].AllocaWeight;
Chris Lattner869adc22003-11-21 21:46:09 +0000248
249 // If this is a constant being passed into the function, use the argument
250 // weights calculated for the callee to determine how much will be folded
251 // away with this information.
Reid Spencer460f16c2004-07-18 00:32:14 +0000252 } else if (isa<Constant>(I)) {
Chris Lattner619d3542004-03-13 23:15:45 +0000253 if (ArgNo < CalleeFI.ArgumentWeights.size())
254 InlineCost -= CalleeFI.ArgumentWeights[ArgNo].ConstantWeight;
Chris Lattner869adc22003-11-21 21:46:09 +0000255 }
256 }
257
258 // Now that we have considered all of the factors that make the call site more
259 // likely to be inlined, look at factors that make us not want to inline it.
260
Chris Lattnerda78b002003-10-07 19:33:31 +0000261 // Don't inline into something too big, which would make it bigger. Here, we
262 // count each basic block as a single unit.
Chris Lattner6dd196f2004-03-15 06:38:14 +0000263 //
Chris Lattner775cbdd2004-04-08 06:34:31 +0000264 InlineCost += Caller->size()/20;
Chris Lattnerda78b002003-10-07 19:33:31 +0000265
266
267 // Look at the size of the callee. Each basic block counts as 20 units, and
Chris Lattner869adc22003-11-21 21:46:09 +0000268 // each instruction counts as 5.
269 InlineCost += CalleeFI.NumInsts*5 + CalleeFI.NumBlocks*20;
Chris Lattner237ef562003-08-31 19:10:30 +0000270 return InlineCost;
Chris Lattnerca398dc2003-05-29 15:11:31 +0000271}
Brian Gaeked0fde302003-11-11 22:41:34 +0000272