blob: 7ec7ae525b22ae374f99d03ce9d86b3076144736 [file] [log] [blame]
Dan Gohmane4aeec02009-10-13 18:30:07 +00001//===- InlineCost.cpp - Cost analysis for inliner -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements inline cost analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/InlineCost.h"
15#include "llvm/Support/CallSite.h"
16#include "llvm/CallingConv.h"
17#include "llvm/IntrinsicInst.h"
18#include "llvm/ADT/SmallPtrSet.h"
Eric Christopher4e8af6d2011-02-05 00:49:15 +000019
Dan Gohmane4aeec02009-10-13 18:30:07 +000020using namespace llvm;
21
Dan Gohman52d55bd2010-04-16 15:14:50 +000022/// callIsSmall - If a call is likely to lower to a single target instruction,
23/// or is otherwise deemed small return true.
24/// TODO: Perhaps calls like memcpy, strcpy, etc?
25bool llvm::callIsSmall(const Function *F) {
Eric Christopher745d8c92010-01-14 21:48:00 +000026 if (!F) return false;
Andrew Trick5c655412011-10-01 01:27:56 +000027
Eric Christopher745d8c92010-01-14 21:48:00 +000028 if (F->hasLocalLinkage()) return false;
Andrew Trick5c655412011-10-01 01:27:56 +000029
Eric Christopher83c20d32010-01-14 23:00:10 +000030 if (!F->hasName()) return false;
Andrew Trick5c655412011-10-01 01:27:56 +000031
Eric Christopher83c20d32010-01-14 23:00:10 +000032 StringRef Name = F->getName();
Andrew Trick5c655412011-10-01 01:27:56 +000033
Eric Christopher83c20d32010-01-14 23:00:10 +000034 // These will all likely lower to a single selection DAG node.
Duncan Sands87cd7ed2010-03-15 14:01:44 +000035 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
Eric Christopher83c20d32010-01-14 23:00:10 +000036 Name == "fabs" || Name == "fabsf" || Name == "fabsl" ||
37 Name == "sin" || Name == "sinf" || Name == "sinl" ||
38 Name == "cos" || Name == "cosf" || Name == "cosl" ||
39 Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl" )
40 return true;
Andrew Trick5c655412011-10-01 01:27:56 +000041
Eric Christopher83c20d32010-01-14 23:00:10 +000042 // These are all likely to be optimized into something smaller.
43 if (Name == "pow" || Name == "powf" || Name == "powl" ||
44 Name == "exp2" || Name == "exp2l" || Name == "exp2f" ||
45 Name == "floor" || Name == "floorf" || Name == "ceil" ||
46 Name == "round" || Name == "ffs" || Name == "ffsl" ||
47 Name == "abs" || Name == "labs" || Name == "llabs")
48 return true;
Andrew Trick5c655412011-10-01 01:27:56 +000049
Eric Christopher2d59ae62010-01-14 20:12:34 +000050 return false;
51}
52
Dan Gohmane4aeec02009-10-13 18:30:07 +000053/// analyzeBasicBlock - Fill in the current structure with information gleaned
54/// from the specified block.
Dan Gohmane7f0ed52009-10-13 19:58:07 +000055void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB) {
Dan Gohmane4aeec02009-10-13 18:30:07 +000056 ++NumBlocks;
Devang Patelafc33fa2010-03-13 01:05:02 +000057 unsigned NumInstsBeforeThisBB = NumInsts;
Dan Gohmane4aeec02009-10-13 18:30:07 +000058 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end();
59 II != E; ++II) {
60 if (isa<PHINode>(II)) continue; // PHI nodes don't count.
61
62 // Special handling for calls.
63 if (isa<CallInst>(II) || isa<InvokeInst>(II)) {
64 if (isa<DbgInfoIntrinsic>(II))
65 continue; // Debug intrinsics don't count as size.
Gabor Greif0de11e02010-07-27 14:15:29 +000066
67 ImmutableCallSite CS(cast<Instruction>(II));
68
Gabor Greif0de11e02010-07-27 14:15:29 +000069 if (const Function *F = CS.getCalledFunction()) {
Andrew Trick5c655412011-10-01 01:27:56 +000070 // If a function is both internal and has a single use, then it is
71 // extremely likely to get inlined in the future (it was probably
Owen Andersonf9a26b82010-09-09 20:32:23 +000072 // exposed by an interleaved devirtualization pass).
73 if (F->hasInternalLinkage() && F->hasOneUse())
74 ++NumInlineCandidates;
Rafael Espindolaac53b0a2011-05-16 15:48:45 +000075
Chris Lattner4b7b42c2010-04-30 22:37:22 +000076 // If this call is to function itself, then the function is recursive.
77 // Inlining it into other functions is a bad idea, because this is
78 // basically just a form of loop peeling, and our metrics aren't useful
79 // for that case.
80 if (F == BB->getParent())
Kenneth Uildriks42c7d232010-06-09 15:11:37 +000081 isRecursive = true;
Chris Lattner4b7b42c2010-04-30 22:37:22 +000082 }
Dan Gohmane4aeec02009-10-13 18:30:07 +000083
Jakob Stoklund Olesenaa034fa2010-02-05 23:21:18 +000084 if (!isa<IntrinsicInst>(II) && !callIsSmall(CS.getCalledFunction())) {
85 // Each argument to a call takes on average one instruction to set up.
86 NumInsts += CS.arg_size();
Jakob Stoklund Olesen8b3ca842010-05-26 22:40:28 +000087
88 // We don't want inline asm to count as a call - that would prevent loop
89 // unrolling. The argument setup cost is still real, though.
90 if (!isa<InlineAsm>(CS.getCalledValue()))
91 ++NumCalls;
Jakob Stoklund Olesenaa034fa2010-02-05 23:21:18 +000092 }
Dan Gohmane4aeec02009-10-13 18:30:07 +000093 }
Andrew Trick5c655412011-10-01 01:27:56 +000094
Dan Gohmane4aeec02009-10-13 18:30:07 +000095 if (const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
96 if (!AI->isStaticAlloca())
97 this->usesDynamicAlloca = true;
98 }
99
Duncan Sands1df98592010-02-16 11:11:14 +0000100 if (isa<ExtractElementInst>(II) || II->getType()->isVectorTy())
Andrew Trick5c655412011-10-01 01:27:56 +0000101 ++NumVectorInsts;
102
Dan Gohmane4aeec02009-10-13 18:30:07 +0000103 if (const CastInst *CI = dyn_cast<CastInst>(II)) {
Evan Cheng1a67dd22010-01-14 21:04:31 +0000104 // Noop casts, including ptr <-> int, don't count.
Andrew Trick5c655412011-10-01 01:27:56 +0000105 if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) ||
Dan Gohmane4aeec02009-10-13 18:30:07 +0000106 isa<PtrToIntInst>(CI))
107 continue;
Evan Cheng1a67dd22010-01-14 21:04:31 +0000108 // Result of a cmp instruction is often extended (to be used by other
109 // cmp instructions, logical or return instructions). These are usually
110 // nop on most sane targets.
111 if (isa<CmpInst>(CI->getOperand(0)))
112 continue;
Chris Lattnerb93a23a2009-11-01 03:07:53 +0000113 } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(II)){
Dan Gohmane4aeec02009-10-13 18:30:07 +0000114 // If a GEP has all constant indices, it will probably be folded with
115 // a load/store.
116 if (GEPI->hasAllConstantIndices())
117 continue;
118 }
119
Dan Gohmane4aeec02009-10-13 18:30:07 +0000120 ++NumInsts;
121 }
Andrew Trick5c655412011-10-01 01:27:56 +0000122
Chris Lattnerb93a23a2009-11-01 03:07:53 +0000123 if (isa<ReturnInst>(BB->getTerminator()))
124 ++NumRets;
Andrew Trick5c655412011-10-01 01:27:56 +0000125
Chris Lattner66588702009-11-01 18:16:30 +0000126 // We never want to inline functions that contain an indirectbr. This is
Duncan Sandsb0469642009-11-01 19:12:43 +0000127 // incorrect because all the blockaddress's (in static global initializers
128 // for example) would be referring to the original function, and this indirect
Chris Lattner66588702009-11-01 18:16:30 +0000129 // jump would jump from the inlined copy of the function into the original
130 // function which is extremely undefined behavior.
Chris Lattnerb93a23a2009-11-01 03:07:53 +0000131 if (isa<IndirectBrInst>(BB->getTerminator()))
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000132 containsIndirectBr = true;
Devang Patelcbd05602010-03-13 00:10:20 +0000133
134 // Remember NumInsts for this BB.
Devang Patelafc33fa2010-03-13 01:05:02 +0000135 NumBBInsts[BB] = NumInsts - NumInstsBeforeThisBB;
Dan Gohmane4aeec02009-10-13 18:30:07 +0000136}
137
Owen Anderson082bf2a2010-09-09 16:56:42 +0000138// CountCodeReductionForConstant - Figure out an approximation for how many
139// instructions will be constant folded if the specified value is constant.
140//
141unsigned CodeMetrics::CountCodeReductionForConstant(Value *V) {
142 unsigned Reduction = 0;
143 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
144 User *U = *UI;
145 if (isa<BranchInst>(U) || isa<SwitchInst>(U)) {
146 // We will be able to eliminate all but one of the successors.
147 const TerminatorInst &TI = cast<TerminatorInst>(*U);
148 const unsigned NumSucc = TI.getNumSuccessors();
149 unsigned Instrs = 0;
150 for (unsigned I = 0; I != NumSucc; ++I)
151 Instrs += NumBBInsts[TI.getSuccessor(I)];
152 // We don't know which blocks will be eliminated, so use the average size.
153 Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1)/NumSucc;
Owen Anderson082bf2a2010-09-09 16:56:42 +0000154 } else {
155 // Figure out if this instruction will be removed due to simple constant
156 // propagation.
157 Instruction &Inst = cast<Instruction>(*U);
158
159 // We can't constant propagate instructions which have effects or
160 // read memory.
161 //
162 // FIXME: It would be nice to capture the fact that a load from a
163 // pointer-to-constant-global is actually a *really* good thing to zap.
164 // Unfortunately, we don't know the pointer that may get propagated here,
165 // so we can't make this decision.
166 if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() ||
167 isa<AllocaInst>(Inst))
168 continue;
169
170 bool AllOperandsConstant = true;
171 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
172 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
173 AllOperandsConstant = false;
174 break;
175 }
176
177 if (AllOperandsConstant) {
178 // We will get to remove this instruction...
179 Reduction += InlineConstants::InstrCost;
180
181 // And any other instructions that use it which become constants
182 // themselves.
183 Reduction += CountCodeReductionForConstant(&Inst);
184 }
185 }
186 }
187 return Reduction;
188}
189
190// CountCodeReductionForAlloca - Figure out an approximation of how much smaller
191// the function will be if it is inlined into a context where an argument
192// becomes an alloca.
193//
194unsigned CodeMetrics::CountCodeReductionForAlloca(Value *V) {
195 if (!V->getType()->isPointerTy()) return 0; // Not a pointer
196 unsigned Reduction = 0;
197 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
198 Instruction *I = cast<Instruction>(*UI);
199 if (isa<LoadInst>(I) || isa<StoreInst>(I))
200 Reduction += InlineConstants::InstrCost;
201 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
202 // If the GEP has variable indices, we won't be able to do much with it.
203 if (GEP->hasAllConstantIndices())
204 Reduction += CountCodeReductionForAlloca(GEP);
205 } else if (BitCastInst *BCI = dyn_cast<BitCastInst>(I)) {
206 // Track pointer through bitcasts.
207 Reduction += CountCodeReductionForAlloca(BCI);
208 } else {
209 // If there is some other strange instruction, we're not going to be able
210 // to do much if we inline this.
211 return 0;
212 }
213 }
214
215 return Reduction;
216}
217
Dan Gohmane4aeec02009-10-13 18:30:07 +0000218/// analyzeFunction - Fill in the current structure with information gleaned
219/// from the specified function.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000220void CodeMetrics::analyzeFunction(Function *F) {
Rafael Espindolaac53b0a2011-05-16 15:48:45 +0000221 // If this function contains a call to setjmp or _setjmp, never inline
222 // it. This is a hack because we depend on the user marking their local
223 // variables as volatile if they are live across a setjmp call, and they
224 // probably won't do this in callers.
225 if (F->callsFunctionThatReturnsTwice())
226 callsSetJmp = true;
227
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000228 // Look at the size of the callee.
Dan Gohmane4aeec02009-10-13 18:30:07 +0000229 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
230 analyzeBasicBlock(&*BB);
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000231}
232
233/// analyzeFunction - Fill in the current structure with information gleaned
234/// from the specified function.
235void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F) {
236 Metrics.analyzeFunction(F);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000237
238 // A function with exactly one return has it removed during the inlining
239 // process (see InlineFunction), so don't count it.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000240 // FIXME: This knowledge should really be encoded outside of FunctionInfo.
241 if (Metrics.NumRets==1)
242 --Metrics.NumInsts;
Dan Gohmane4aeec02009-10-13 18:30:07 +0000243
244 // Check out all of the arguments to the function, figuring out how much
245 // code can be eliminated if one of the arguments is a constant.
Jakob Stoklund Olesene3039b62010-01-26 21:31:24 +0000246 ArgumentWeights.reserve(F->arg_size());
Dan Gohmane4aeec02009-10-13 18:30:07 +0000247 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
Owen Anderson082bf2a2010-09-09 16:56:42 +0000248 ArgumentWeights.push_back(ArgInfo(Metrics.CountCodeReductionForConstant(I),
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000249 Metrics.CountCodeReductionForAlloca(I)));
Dan Gohmane4aeec02009-10-13 18:30:07 +0000250}
251
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000252/// NeverInline - returns true if the function should never be inlined into
253/// any caller
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000254bool InlineCostAnalyzer::FunctionInfo::NeverInline() {
Andrew Trick5c655412011-10-01 01:27:56 +0000255 return (Metrics.callsSetJmp || Metrics.isRecursive ||
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000256 Metrics.containsIndirectBr);
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000257}
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000258// getSpecializationBonus - The heuristic used to determine the per-call
259// performance boost for using a specialization of Callee with argument
260// specializedArgNo replaced by a constant.
261int InlineCostAnalyzer::getSpecializationBonus(Function *Callee,
262 SmallVectorImpl<unsigned> &SpecializedArgNos)
263{
264 if (Callee->mayBeOverridden())
265 return 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000266
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000267 int Bonus = 0;
268 // If this function uses the coldcc calling convention, prefer not to
269 // specialize it.
270 if (Callee->getCallingConv() == CallingConv::Cold)
271 Bonus -= InlineConstants::ColdccPenalty;
Andrew Trick5c655412011-10-01 01:27:56 +0000272
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000273 // Get information about the callee.
274 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000275
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000276 // If we haven't calculated this information yet, do so now.
277 if (CalleeFI->Metrics.NumBlocks == 0)
278 CalleeFI->analyzeFunction(Callee);
279
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000280 unsigned ArgNo = 0;
281 unsigned i = 0;
282 for (Function::arg_iterator I = Callee->arg_begin(), E = Callee->arg_end();
283 I != E; ++I, ++ArgNo)
284 if (ArgNo == SpecializedArgNos[i]) {
285 ++i;
286 Bonus += CountBonusForConstant(I);
287 }
Eric Christopher7d3a16f2011-01-26 01:09:59 +0000288
Andrew Trick5c655412011-10-01 01:27:56 +0000289 // Calls usually take a long time, so they make the specialization gain
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000290 // smaller.
291 Bonus -= CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty;
292
293 return Bonus;
294}
295
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000296// ConstantFunctionBonus - Figure out how much of a bonus we can get for
297// possibly devirtualizing a function. We'll subtract the size of the function
298// we may wish to inline from the indirect call bonus providing a limit on
299// growth. Leave an upper limit of 0 for the bonus - we don't want to penalize
300// inlining because we decide we don't want to give a bonus for
301// devirtualizing.
302int InlineCostAnalyzer::ConstantFunctionBonus(CallSite CS, Constant *C) {
Andrew Trick5c655412011-10-01 01:27:56 +0000303
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000304 // This could just be NULL.
305 if (!C) return 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000306
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000307 Function *F = dyn_cast<Function>(C);
308 if (!F) return 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000309
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000310 int Bonus = InlineConstants::IndirectCallBonus + getInlineSize(CS, F);
311 return (Bonus > 0) ? 0 : Bonus;
312}
313
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000314// CountBonusForConstant - Figure out an approximation for how much per-call
315// performance boost we can expect if the specified value is constant.
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000316int InlineCostAnalyzer::CountBonusForConstant(Value *V, Constant *C) {
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000317 unsigned Bonus = 0;
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000318 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
319 User *U = *UI;
320 if (CallInst *CI = dyn_cast<CallInst>(U)) {
321 // Turning an indirect call into a direct call is a BIG win
322 if (CI->getCalledValue() == V)
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000323 Bonus += ConstantFunctionBonus(CallSite(CI), C);
324 } else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) {
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000325 // Turning an indirect call into a direct call is a BIG win
326 if (II->getCalledValue() == V)
Eric Christophera818d032011-02-05 02:48:47 +0000327 Bonus += ConstantFunctionBonus(CallSite(II), C);
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000328 }
329 // FIXME: Eliminating conditional branches and switches should
330 // also yield a per-call performance boost.
331 else {
332 // Figure out the bonuses that wll accrue due to simple constant
333 // propagation.
334 Instruction &Inst = cast<Instruction>(*U);
335
336 // We can't constant propagate instructions which have effects or
337 // read memory.
338 //
339 // FIXME: It would be nice to capture the fact that a load from a
340 // pointer-to-constant-global is actually a *really* good thing to zap.
341 // Unfortunately, we don't know the pointer that may get propagated here,
342 // so we can't make this decision.
343 if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() ||
344 isa<AllocaInst>(Inst))
345 continue;
346
347 bool AllOperandsConstant = true;
348 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
349 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
350 AllOperandsConstant = false;
351 break;
352 }
353
354 if (AllOperandsConstant)
355 Bonus += CountBonusForConstant(&Inst);
356 }
357 }
Andrew Trick5c655412011-10-01 01:27:56 +0000358
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000359 return Bonus;
360}
361
362int InlineCostAnalyzer::getInlineSize(CallSite CS, Function *Callee) {
363 // Get information about the callee.
364 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000365
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000366 // If we haven't calculated this information yet, do so now.
367 if (CalleeFI->Metrics.NumBlocks == 0)
368 CalleeFI->analyzeFunction(Callee);
Andrew Trick5c655412011-10-01 01:27:56 +0000369
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000370 // InlineCost - This value measures how good of an inline candidate this call
371 // site is to inline. A lower inline cost make is more likely for the call to
372 // be inlined. This value may go negative.
373 //
374 int InlineCost = 0;
375
376 // Compute any size reductions we can expect due to arguments being passed into
377 // the function.
378 //
379 unsigned ArgNo = 0;
380 CallSite::arg_iterator I = CS.arg_begin();
381 for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end();
382 FI != FE; ++I, ++FI, ++ArgNo) {
383
384 // If an alloca is passed in, inlining this function is likely to allow
385 // significant future optimization possibilities (like scalar promotion, and
386 // scalarization), so encourage the inlining of the function.
387 //
388 if (isa<AllocaInst>(I))
389 InlineCost -= CalleeFI->ArgumentWeights[ArgNo].AllocaWeight;
390
391 // If this is a constant being passed into the function, use the argument
392 // weights calculated for the callee to determine how much will be folded
393 // away with this information.
394 else if (isa<Constant>(I))
Andrew Trick5c655412011-10-01 01:27:56 +0000395 InlineCost -= CalleeFI->ArgumentWeights[ArgNo].ConstantWeight;
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000396 }
Andrew Trick5c655412011-10-01 01:27:56 +0000397
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000398 // Each argument passed in has a cost at both the caller and the callee
399 // sides. Measurements show that each argument costs about the same as an
400 // instruction.
401 InlineCost -= (CS.arg_size() * InlineConstants::InstrCost);
402
403 // Now that we have considered all of the factors that make the call site more
404 // likely to be inlined, look at factors that make us not want to inline it.
405
406 // Calls usually take a long time, so they make the inlining gain smaller.
407 InlineCost += CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty;
408
409 // Look at the size of the callee. Each instruction counts as 5.
410 InlineCost += CalleeFI->Metrics.NumInsts*InlineConstants::InstrCost;
Andrew Trick5c655412011-10-01 01:27:56 +0000411
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000412 return InlineCost;
413}
414
415int InlineCostAnalyzer::getInlineBonuses(CallSite CS, Function *Callee) {
416 // Get information about the callee.
417 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000418
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000419 // If we haven't calculated this information yet, do so now.
420 if (CalleeFI->Metrics.NumBlocks == 0)
421 CalleeFI->analyzeFunction(Callee);
Andrew Trick5c655412011-10-01 01:27:56 +0000422
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000423 bool isDirectCall = CS.getCalledFunction() == Callee;
424 Instruction *TheCall = CS.getInstruction();
425 int Bonus = 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000426
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000427 // If there is only one call of the function, and it has internal linkage,
428 // make it almost guaranteed to be inlined.
429 //
430 if (Callee->hasLocalLinkage() && Callee->hasOneUse() && isDirectCall)
431 Bonus += InlineConstants::LastCallToStaticBonus;
Andrew Trick5c655412011-10-01 01:27:56 +0000432
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000433 // If the instruction after the call, or if the normal destination of the
434 // invoke is an unreachable instruction, the function is noreturn. As such,
435 // there is little point in inlining this.
436 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
437 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
438 Bonus += InlineConstants::NoreturnPenalty;
439 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
440 Bonus += InlineConstants::NoreturnPenalty;
Andrew Trick5c655412011-10-01 01:27:56 +0000441
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000442 // If this function uses the coldcc calling convention, prefer not to inline
443 // it.
444 if (Callee->getCallingConv() == CallingConv::Cold)
445 Bonus += InlineConstants::ColdccPenalty;
Andrew Trick5c655412011-10-01 01:27:56 +0000446
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000447 // Add to the inline quality for properties that make the call valuable to
448 // inline. This includes factors that indicate that the result of inlining
449 // the function will be optimizable. Currently this just looks at arguments
450 // passed into the function.
451 //
452 CallSite::arg_iterator I = CS.arg_begin();
453 for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end();
454 FI != FE; ++I, ++FI)
455 // Compute any constant bonus due to inlining we want to give here.
456 if (isa<Constant>(I))
457 Bonus += CountBonusForConstant(FI, cast<Constant>(I));
Andrew Trick5c655412011-10-01 01:27:56 +0000458
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000459 return Bonus;
460}
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000461
Dan Gohmane4aeec02009-10-13 18:30:07 +0000462// getInlineCost - The heuristic used to determine if we should inline the
463// function call or not.
464//
465InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS,
Chris Lattner44b04a52010-04-17 17:55:00 +0000466 SmallPtrSet<const Function*, 16> &NeverInline) {
David Chisnall752e2592010-05-01 15:47:41 +0000467 return getInlineCost(CS, CS.getCalledFunction(), NeverInline);
468}
469
470InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS,
471 Function *Callee,
472 SmallPtrSet<const Function*, 16> &NeverInline) {
Dan Gohmane4aeec02009-10-13 18:30:07 +0000473 Instruction *TheCall = CS.getInstruction();
Dan Gohmane4aeec02009-10-13 18:30:07 +0000474 Function *Caller = TheCall->getParent()->getParent();
475
476 // Don't inline functions which can be redefined at link-time to mean
Eric Christopherf27e6082010-03-25 04:49:10 +0000477 // something else. Don't inline functions marked noinline or call sites
478 // marked noinline.
Dan Gohmane4aeec02009-10-13 18:30:07 +0000479 if (Callee->mayBeOverridden() ||
Eric Christopherf27e6082010-03-25 04:49:10 +0000480 Callee->hasFnAttr(Attribute::NoInline) || NeverInline.count(Callee) ||
481 CS.isNoInline())
Dan Gohmane4aeec02009-10-13 18:30:07 +0000482 return llvm::InlineCost::getNever();
483
Chris Lattner44b04a52010-04-17 17:55:00 +0000484 // Get information about the callee.
485 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000486
Dan Gohmane4aeec02009-10-13 18:30:07 +0000487 // If we haven't calculated this information yet, do so now.
Chris Lattner44b04a52010-04-17 17:55:00 +0000488 if (CalleeFI->Metrics.NumBlocks == 0)
489 CalleeFI->analyzeFunction(Callee);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000490
491 // If we should never inline this, return a huge cost.
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000492 if (CalleeFI->NeverInline())
Dan Gohmane4aeec02009-10-13 18:30:07 +0000493 return InlineCost::getNever();
494
Chris Lattner44b04a52010-04-17 17:55:00 +0000495 // FIXME: It would be nice to kill off CalleeFI->NeverInline. Then we
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000496 // could move this up and avoid computing the FunctionInfo for
Dan Gohmane4aeec02009-10-13 18:30:07 +0000497 // things we are going to just return always inline for. This
498 // requires handling setjmp somewhere else, however.
499 if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline))
500 return InlineCost::getAlways();
Andrew Trick5c655412011-10-01 01:27:56 +0000501
Chris Lattner44b04a52010-04-17 17:55:00 +0000502 if (CalleeFI->Metrics.usesDynamicAlloca) {
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000503 // Get information about the caller.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000504 FunctionInfo &CallerFI = CachedFunctionInfo[Caller];
Dan Gohmane4aeec02009-10-13 18:30:07 +0000505
506 // If we haven't calculated this information yet, do so now.
Chris Lattnerf84755b2010-04-17 17:57:56 +0000507 if (CallerFI.Metrics.NumBlocks == 0) {
Dan Gohmane4aeec02009-10-13 18:30:07 +0000508 CallerFI.analyzeFunction(Caller);
Andrew Trick5c655412011-10-01 01:27:56 +0000509
Chris Lattnerf84755b2010-04-17 17:57:56 +0000510 // Recompute the CalleeFI pointer, getting Caller could have invalidated
511 // it.
512 CalleeFI = &CachedFunctionInfo[Callee];
513 }
Dan Gohmane4aeec02009-10-13 18:30:07 +0000514
515 // Don't inline a callee with dynamic alloca into a caller without them.
516 // Functions containing dynamic alloca's are inefficient in various ways;
517 // don't create more inefficiency.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000518 if (!CallerFI.Metrics.usesDynamicAlloca)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000519 return InlineCost::getNever();
520 }
521
Eric Christopher1bcb4282011-01-25 01:34:31 +0000522 // InlineCost - This value measures how good of an inline candidate this call
523 // site is to inline. A lower inline cost make is more likely for the call to
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000524 // be inlined. This value may go negative due to the fact that bonuses
525 // are negative numbers.
Eric Christopher1bcb4282011-01-25 01:34:31 +0000526 //
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000527 int InlineCost = getInlineSize(CS, Callee) + getInlineBonuses(CS, Callee);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000528 return llvm::InlineCost::get(InlineCost);
529}
530
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000531// getSpecializationCost - The heuristic used to determine the code-size
532// impact of creating a specialized version of Callee with argument
533// SpecializedArgNo replaced by a constant.
534InlineCost InlineCostAnalyzer::getSpecializationCost(Function *Callee,
535 SmallVectorImpl<unsigned> &SpecializedArgNos)
536{
537 // Don't specialize functions which can be redefined at link-time to mean
538 // something else.
539 if (Callee->mayBeOverridden())
540 return llvm::InlineCost::getNever();
Andrew Trick5c655412011-10-01 01:27:56 +0000541
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000542 // Get information about the callee.
543 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000544
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000545 // If we haven't calculated this information yet, do so now.
546 if (CalleeFI->Metrics.NumBlocks == 0)
547 CalleeFI->analyzeFunction(Callee);
548
549 int Cost = 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000550
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000551 // Look at the original size of the callee. Each instruction counts as 5.
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000552 Cost += CalleeFI->Metrics.NumInsts * InlineConstants::InstrCost;
553
554 // Offset that with the amount of code that can be constant-folded
555 // away with the given arguments replaced by constants.
556 for (SmallVectorImpl<unsigned>::iterator an = SpecializedArgNos.begin(),
557 ae = SpecializedArgNos.end(); an != ae; ++an)
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000558 Cost -= CalleeFI->ArgumentWeights[*an].ConstantWeight;
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000559
560 return llvm::InlineCost::get(Cost);
561}
562
Dan Gohmane4aeec02009-10-13 18:30:07 +0000563// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
564// higher threshold to determine if the function call should be inlined.
565float InlineCostAnalyzer::getInlineFudgeFactor(CallSite CS) {
566 Function *Callee = CS.getCalledFunction();
Andrew Trick5c655412011-10-01 01:27:56 +0000567
Chris Lattner44b04a52010-04-17 17:55:00 +0000568 // Get information about the callee.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000569 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000570
Dan Gohmane4aeec02009-10-13 18:30:07 +0000571 // If we haven't calculated this information yet, do so now.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000572 if (CalleeFI.Metrics.NumBlocks == 0)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000573 CalleeFI.analyzeFunction(Callee);
574
575 float Factor = 1.0f;
576 // Single BB functions are often written to be inlined.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000577 if (CalleeFI.Metrics.NumBlocks == 1)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000578 Factor += 0.5f;
579
580 // Be more aggressive if the function contains a good chunk (if it mades up
581 // at least 10% of the instructions) of vector instructions.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000582 if (CalleeFI.Metrics.NumVectorInsts > CalleeFI.Metrics.NumInsts/2)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000583 Factor += 2.0f;
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000584 else if (CalleeFI.Metrics.NumVectorInsts > CalleeFI.Metrics.NumInsts/10)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000585 Factor += 1.5f;
586 return Factor;
587}
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000588
589/// growCachedCostInfo - update the cached cost info for Caller after Callee has
590/// been inlined.
591void
Chris Lattner44b04a52010-04-17 17:55:00 +0000592InlineCostAnalyzer::growCachedCostInfo(Function *Caller, Function *Callee) {
593 CodeMetrics &CallerMetrics = CachedFunctionInfo[Caller].Metrics;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000594
595 // For small functions we prefer to recalculate the cost for better accuracy.
Eli Friedmanb1763992011-05-24 20:22:24 +0000596 if (CallerMetrics.NumBlocks < 10 && CallerMetrics.NumInsts < 1000) {
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000597 resetCachedCostInfo(Caller);
598 return;
599 }
600
601 // For large functions, we can save a lot of computation time by skipping
602 // recalculations.
Chris Lattner44b04a52010-04-17 17:55:00 +0000603 if (CallerMetrics.NumCalls > 0)
604 --CallerMetrics.NumCalls;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000605
Chris Lattner44b04a52010-04-17 17:55:00 +0000606 if (Callee == 0) return;
Andrew Trick5c655412011-10-01 01:27:56 +0000607
Chris Lattner44b04a52010-04-17 17:55:00 +0000608 CodeMetrics &CalleeMetrics = CachedFunctionInfo[Callee].Metrics;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000609
Chris Lattner44b04a52010-04-17 17:55:00 +0000610 // If we don't have metrics for the callee, don't recalculate them just to
611 // update an approximation in the caller. Instead, just recalculate the
612 // caller info from scratch.
613 if (CalleeMetrics.NumBlocks == 0) {
614 resetCachedCostInfo(Caller);
615 return;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000616 }
Andrew Trick5c655412011-10-01 01:27:56 +0000617
Chris Lattnerf84755b2010-04-17 17:57:56 +0000618 // Since CalleeMetrics were already calculated, we know that the CallerMetrics
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000619 // reference isn't invalidated: both were in the DenseMap.
Chris Lattner44b04a52010-04-17 17:55:00 +0000620 CallerMetrics.usesDynamicAlloca |= CalleeMetrics.usesDynamicAlloca;
621
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000622 // FIXME: If any of these three are true for the callee, the callee was
623 // not inlined into the caller, so I think they're redundant here.
624 CallerMetrics.callsSetJmp |= CalleeMetrics.callsSetJmp;
625 CallerMetrics.isRecursive |= CalleeMetrics.isRecursive;
626 CallerMetrics.containsIndirectBr |= CalleeMetrics.containsIndirectBr;
627
Chris Lattner44b04a52010-04-17 17:55:00 +0000628 CallerMetrics.NumInsts += CalleeMetrics.NumInsts;
629 CallerMetrics.NumBlocks += CalleeMetrics.NumBlocks;
630 CallerMetrics.NumCalls += CalleeMetrics.NumCalls;
631 CallerMetrics.NumVectorInsts += CalleeMetrics.NumVectorInsts;
632 CallerMetrics.NumRets += CalleeMetrics.NumRets;
633
634 // analyzeBasicBlock counts each function argument as an inst.
635 if (CallerMetrics.NumInsts >= Callee->arg_size())
636 CallerMetrics.NumInsts -= Callee->arg_size();
637 else
638 CallerMetrics.NumInsts = 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000639
Nick Lewycky9a1581b2010-05-12 21:48:15 +0000640 // We are not updating the argument weights. We have already determined that
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000641 // Caller is a fairly large function, so we accept the loss of precision.
642}
Nick Lewycky9a1581b2010-05-12 21:48:15 +0000643
644/// clear - empty the cache of inline costs
645void InlineCostAnalyzer::clear() {
646 CachedFunctionInfo.clear();
647}