blob: dedbfebea70c4a720e99f5170e827380ac9b69d2 [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"
Andrew Trickb2ab2fa2011-10-01 01:39:05 +000018#include "llvm/Target/TargetData.h"
Dan Gohmane4aeec02009-10-13 18:30:07 +000019#include "llvm/ADT/SmallPtrSet.h"
Eric Christopher4e8af6d2011-02-05 00:49:15 +000020
Dan Gohmane4aeec02009-10-13 18:30:07 +000021using namespace llvm;
22
Chandler Carruth6f130bf2012-03-08 02:04:19 +000023unsigned InlineCostAnalyzer::FunctionInfo::countCodeReductionForConstant(
24 const CodeMetrics &Metrics, Value *V) {
Owen Anderson082bf2a2010-09-09 16:56:42 +000025 unsigned Reduction = 0;
Chandler Carruth3d1d8952012-03-14 07:32:53 +000026 SmallVector<Value *, 4> Worklist;
27 Worklist.push_back(V);
28 do {
29 Value *V = Worklist.pop_back_val();
30 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
31 User *U = *UI;
32 if (isa<BranchInst>(U) || isa<SwitchInst>(U)) {
33 // We will be able to eliminate all but one of the successors.
34 const TerminatorInst &TI = cast<TerminatorInst>(*U);
35 const unsigned NumSucc = TI.getNumSuccessors();
36 unsigned Instrs = 0;
37 for (unsigned I = 0; I != NumSucc; ++I)
38 Instrs += Metrics.NumBBInsts.lookup(TI.getSuccessor(I));
39 // We don't know which blocks will be eliminated, so use the average size.
40 Reduction += InlineConstants::InstrCost*Instrs*(NumSucc-1)/NumSucc;
41 continue;
42 }
43
Owen Anderson082bf2a2010-09-09 16:56:42 +000044 // Figure out if this instruction will be removed due to simple constant
45 // propagation.
46 Instruction &Inst = cast<Instruction>(*U);
47
48 // We can't constant propagate instructions which have effects or
49 // read memory.
50 //
51 // FIXME: It would be nice to capture the fact that a load from a
52 // pointer-to-constant-global is actually a *really* good thing to zap.
53 // Unfortunately, we don't know the pointer that may get propagated here,
54 // so we can't make this decision.
55 if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() ||
56 isa<AllocaInst>(Inst))
57 continue;
58
59 bool AllOperandsConstant = true;
60 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
61 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
62 AllOperandsConstant = false;
63 break;
64 }
Chandler Carruth3d1d8952012-03-14 07:32:53 +000065 if (!AllOperandsConstant)
66 continue;
Owen Anderson082bf2a2010-09-09 16:56:42 +000067
Chandler Carruth3d1d8952012-03-14 07:32:53 +000068 // We will get to remove this instruction...
69 Reduction += InlineConstants::InstrCost;
Owen Anderson082bf2a2010-09-09 16:56:42 +000070
Chandler Carruth3d1d8952012-03-14 07:32:53 +000071 // And any other instructions that use it which become constants
72 // themselves.
73 Worklist.push_back(&Inst);
Owen Anderson082bf2a2010-09-09 16:56:42 +000074 }
Chandler Carruth3d1d8952012-03-14 07:32:53 +000075 } while (!Worklist.empty());
Owen Anderson082bf2a2010-09-09 16:56:42 +000076 return Reduction;
77}
78
Chandler Carruthe8187e02012-03-09 02:49:36 +000079static unsigned countCodeReductionForAllocaICmp(const CodeMetrics &Metrics,
80 ICmpInst *ICI) {
81 unsigned Reduction = 0;
82
83 // Bail if this is comparing against a non-constant; there is nothing we can
84 // do there.
85 if (!isa<Constant>(ICI->getOperand(1)))
86 return Reduction;
87
88 // An icmp pred (alloca, C) becomes true if the predicate is true when
89 // equal and false otherwise.
90 bool Result = ICI->isTrueWhenEqual();
91
92 SmallVector<Instruction *, 4> Worklist;
93 Worklist.push_back(ICI);
94 do {
95 Instruction *U = Worklist.pop_back_val();
96 Reduction += InlineConstants::InstrCost;
97 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
98 UI != UE; ++UI) {
99 Instruction *I = dyn_cast<Instruction>(*UI);
100 if (!I || I->mayHaveSideEffects()) continue;
101 if (I->getNumOperands() == 1)
102 Worklist.push_back(I);
103 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
104 // If BO produces the same value as U, then the other operand is
105 // irrelevant and we can put it into the Worklist to continue
106 // deleting dead instructions. If BO produces the same value as the
107 // other operand, we can delete BO but that's it.
108 if (Result == true) {
109 if (BO->getOpcode() == Instruction::Or)
110 Worklist.push_back(I);
111 if (BO->getOpcode() == Instruction::And)
112 Reduction += InlineConstants::InstrCost;
113 } else {
114 if (BO->getOpcode() == Instruction::Or ||
115 BO->getOpcode() == Instruction::Xor)
116 Reduction += InlineConstants::InstrCost;
117 if (BO->getOpcode() == Instruction::And)
118 Worklist.push_back(I);
119 }
120 }
121 if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
122 BasicBlock *BB = BI->getSuccessor(Result ? 0 : 1);
123 if (BB->getSinglePredecessor())
124 Reduction
125 += InlineConstants::InstrCost * Metrics.NumBBInsts.lookup(BB);
126 }
127 }
128 } while (!Worklist.empty());
129
130 return Reduction;
131}
132
133/// \brief Compute the reduction possible for a given instruction if we are able
134/// to SROA an alloca.
135///
136/// The reduction for this instruction is added to the SROAReduction output
137/// parameter. Returns false if this instruction is expected to defeat SROA in
138/// general.
Benjamin Kramer4210b342012-03-10 22:41:06 +0000139static bool countCodeReductionForSROAInst(Instruction *I,
140 SmallVectorImpl<Value *> &Worklist,
141 unsigned &SROAReduction) {
Chandler Carruthe8187e02012-03-09 02:49:36 +0000142 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
143 if (!LI->isSimple())
144 return false;
145 SROAReduction += InlineConstants::InstrCost;
146 return true;
147 }
148
149 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
150 if (!SI->isSimple())
151 return false;
152 SROAReduction += InlineConstants::InstrCost;
153 return true;
154 }
155
156 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
157 // If the GEP has variable indices, we won't be able to do much with it.
158 if (!GEP->hasAllConstantIndices())
159 return false;
160 // A non-zero GEP will likely become a mask operation after SROA.
161 if (GEP->hasAllZeroIndices())
162 SROAReduction += InlineConstants::InstrCost;
163 Worklist.push_back(GEP);
164 return true;
165 }
166
167 if (BitCastInst *BCI = dyn_cast<BitCastInst>(I)) {
168 // Track pointer through bitcasts.
169 Worklist.push_back(BCI);
170 SROAReduction += InlineConstants::InstrCost;
171 return true;
172 }
173
174 // We just look for non-constant operands to ICmp instructions as those will
175 // defeat SROA. The actual reduction for these happens even without SROA.
176 if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
177 return isa<Constant>(ICI->getOperand(1));
178
179 if (SelectInst *SI = dyn_cast<SelectInst>(I)) {
180 // SROA can handle a select of alloca iff all uses of the alloca are
181 // loads, and dereferenceable. We assume it's dereferenceable since
182 // we're told the input is an alloca.
183 for (Value::use_iterator UI = SI->use_begin(), UE = SI->use_end();
184 UI != UE; ++UI) {
185 LoadInst *LI = dyn_cast<LoadInst>(*UI);
186 if (LI == 0 || !LI->isSimple())
187 return false;
188 }
189 // We don't know whether we'll be deleting the rest of the chain of
190 // instructions from the SelectInst on, because we don't know whether
191 // the other side of the select is also an alloca or not.
192 return true;
193 }
194
195 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
196 switch (II->getIntrinsicID()) {
197 default:
198 return false;
199 case Intrinsic::memset:
200 case Intrinsic::memcpy:
201 case Intrinsic::memmove:
202 case Intrinsic::lifetime_start:
203 case Intrinsic::lifetime_end:
204 // SROA can usually chew through these intrinsics.
205 SROAReduction += InlineConstants::InstrCost;
206 return true;
207 }
208 }
209
210 // If there is some other strange instruction, we're not going to be
211 // able to do much if we inline this.
212 return false;
213}
214
Chandler Carruth6f130bf2012-03-08 02:04:19 +0000215unsigned InlineCostAnalyzer::FunctionInfo::countCodeReductionForAlloca(
216 const CodeMetrics &Metrics, Value *V) {
Owen Anderson082bf2a2010-09-09 16:56:42 +0000217 if (!V->getType()->isPointerTy()) return 0; // Not a pointer
218 unsigned Reduction = 0;
Chandler Carruthe8187e02012-03-09 02:49:36 +0000219 unsigned SROAReduction = 0;
220 bool CanSROAAlloca = true;
Nick Lewycky6977e792012-01-25 08:27:40 +0000221
Nick Lewycky38b6d9d2012-01-20 08:35:20 +0000222 SmallVector<Value *, 4> Worklist;
223 Worklist.push_back(V);
224 do {
225 Value *V = Worklist.pop_back_val();
226 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
227 UI != E; ++UI){
228 Instruction *I = cast<Instruction>(*UI);
Chandler Carruthe8187e02012-03-09 02:49:36 +0000229
230 if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
231 Reduction += countCodeReductionForAllocaICmp(Metrics, ICI);
232
233 if (CanSROAAlloca)
234 CanSROAAlloca = countCodeReductionForSROAInst(I, Worklist,
235 SROAReduction);
Owen Anderson082bf2a2010-09-09 16:56:42 +0000236 }
Nick Lewycky38b6d9d2012-01-20 08:35:20 +0000237 } while (!Worklist.empty());
Owen Anderson082bf2a2010-09-09 16:56:42 +0000238
Chandler Carruthe8187e02012-03-09 02:49:36 +0000239 return Reduction + (CanSROAAlloca ? SROAReduction : 0);
Owen Anderson082bf2a2010-09-09 16:56:42 +0000240}
241
Chandler Carruth274d3772012-03-14 23:19:53 +0000242void InlineCostAnalyzer::FunctionInfo::countCodeReductionForPointerPair(
243 const CodeMetrics &Metrics, DenseMap<Value *, unsigned> &PointerArgs,
244 Value *V, unsigned ArgIdx) {
245 SmallVector<Value *, 4> Worklist;
246 Worklist.push_back(V);
247 do {
248 Value *V = Worklist.pop_back_val();
249 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
250 UI != E; ++UI){
251 Instruction *I = cast<Instruction>(*UI);
252
253 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
254 // If the GEP has variable indices, we won't be able to do much with it.
255 if (!GEP->hasAllConstantIndices())
256 continue;
257 // Unless the GEP is in-bounds, some comparisons will be non-constant.
258 // Fortunately, the real-world cases where this occurs uses in-bounds
259 // GEPs, and so we restrict the optimization to them here.
260 if (!GEP->isInBounds())
261 continue;
262
263 // Constant indices just change the constant offset. Add the resulting
264 // value both to our worklist for this argument, and to the set of
265 // viable paired values with future arguments.
266 PointerArgs[GEP] = ArgIdx;
267 Worklist.push_back(GEP);
268 continue;
269 }
270
271 // Track pointer through casts. Even when the result is not a pointer, it
272 // remains a constant relative to constants derived from other constant
273 // pointers.
274 if (CastInst *CI = dyn_cast<CastInst>(I)) {
275 PointerArgs[CI] = ArgIdx;
276 Worklist.push_back(CI);
277 continue;
278 }
279
280 // There are two instructions which produce a strict constant value when
281 // applied to two related pointer values. Ignore everything else.
282 if (!isa<ICmpInst>(I) && I->getOpcode() != Instruction::Sub)
283 continue;
284 assert(I->getNumOperands() == 2);
285
286 // Ensure that the two operands are in our set of potentially paired
287 // pointers (or are derived from them).
288 Value *OtherArg = I->getOperand(0);
289 if (OtherArg == V)
290 OtherArg = I->getOperand(1);
291 DenseMap<Value *, unsigned>::const_iterator ArgIt
292 = PointerArgs.find(OtherArg);
293 if (ArgIt == PointerArgs.end())
294 continue;
Chandler Carruth0e33d9f2012-03-15 00:50:21 +0000295 std::pair<unsigned, unsigned> ArgPair(ArgIt->second, ArgIdx);
Chandler Carruth377c7f02012-03-15 00:55:51 +0000296 if (ArgPair.first > ArgPair.second)
Chandler Carruth0e33d9f2012-03-15 00:50:21 +0000297 std::swap(ArgPair.first, ArgPair.second);
Chandler Carruth274d3772012-03-14 23:19:53 +0000298
Chandler Carruth0e33d9f2012-03-15 00:50:21 +0000299 PointerArgPairWeights[ArgPair]
Chandler Carruth274d3772012-03-14 23:19:53 +0000300 += countCodeReductionForConstant(Metrics, I);
301 }
302 } while (!Worklist.empty());
303}
304
Dan Gohmane4aeec02009-10-13 18:30:07 +0000305/// analyzeFunction - Fill in the current structure with information gleaned
306/// from the specified function.
Andrew Trickb2ab2fa2011-10-01 01:39:05 +0000307void InlineCostAnalyzer::FunctionInfo::analyzeFunction(Function *F,
308 const TargetData *TD) {
309 Metrics.analyzeFunction(F, TD);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000310
311 // A function with exactly one return has it removed during the inlining
312 // process (see InlineFunction), so don't count it.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000313 // FIXME: This knowledge should really be encoded outside of FunctionInfo.
314 if (Metrics.NumRets==1)
315 --Metrics.NumInsts;
Dan Gohmane4aeec02009-10-13 18:30:07 +0000316
Jakob Stoklund Olesene3039b62010-01-26 21:31:24 +0000317 ArgumentWeights.reserve(F->arg_size());
Chandler Carruth274d3772012-03-14 23:19:53 +0000318 DenseMap<Value *, unsigned> PointerArgs;
319 unsigned ArgIdx = 0;
320 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
321 ++I, ++ArgIdx) {
322 // Count how much code can be eliminated if one of the arguments is
323 // a constant or an alloca.
Chandler Carruth6f130bf2012-03-08 02:04:19 +0000324 ArgumentWeights.push_back(ArgInfo(countCodeReductionForConstant(Metrics, I),
325 countCodeReductionForAlloca(Metrics, I)));
Chandler Carruth274d3772012-03-14 23:19:53 +0000326
327 // If the argument is a pointer, also check for pairs of pointers where
328 // knowing a fixed offset between them allows simplification. This pattern
329 // arises mostly due to STL algorithm patterns where pointers are used as
330 // random access iterators.
331 if (!I->getType()->isPointerTy())
332 continue;
333 PointerArgs[I] = ArgIdx;
334 countCodeReductionForPointerPair(Metrics, PointerArgs, I, ArgIdx);
335 }
Dan Gohmane4aeec02009-10-13 18:30:07 +0000336}
337
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000338/// NeverInline - returns true if the function should never be inlined into
339/// any caller
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000340bool InlineCostAnalyzer::FunctionInfo::NeverInline() {
Joerg Sonnenberger34706932011-12-18 20:35:43 +0000341 return (Metrics.exposesReturnsTwice || Metrics.isRecursive ||
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000342 Metrics.containsIndirectBr);
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000343}
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000344
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000345// ConstantFunctionBonus - Figure out how much of a bonus we can get for
346// possibly devirtualizing a function. We'll subtract the size of the function
347// we may wish to inline from the indirect call bonus providing a limit on
348// growth. Leave an upper limit of 0 for the bonus - we don't want to penalize
349// inlining because we decide we don't want to give a bonus for
350// devirtualizing.
351int InlineCostAnalyzer::ConstantFunctionBonus(CallSite CS, Constant *C) {
Andrew Trick5c655412011-10-01 01:27:56 +0000352
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000353 // This could just be NULL.
354 if (!C) return 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000355
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000356 Function *F = dyn_cast<Function>(C);
357 if (!F) return 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000358
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000359 int Bonus = InlineConstants::IndirectCallBonus + getInlineSize(CS, F);
360 return (Bonus > 0) ? 0 : Bonus;
361}
362
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000363// CountBonusForConstant - Figure out an approximation for how much per-call
364// performance boost we can expect if the specified value is constant.
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000365int InlineCostAnalyzer::CountBonusForConstant(Value *V, Constant *C) {
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000366 unsigned Bonus = 0;
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000367 for (Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E;++UI){
368 User *U = *UI;
369 if (CallInst *CI = dyn_cast<CallInst>(U)) {
370 // Turning an indirect call into a direct call is a BIG win
371 if (CI->getCalledValue() == V)
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000372 Bonus += ConstantFunctionBonus(CallSite(CI), C);
373 } else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) {
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000374 // Turning an indirect call into a direct call is a BIG win
375 if (II->getCalledValue() == V)
Eric Christophera818d032011-02-05 02:48:47 +0000376 Bonus += ConstantFunctionBonus(CallSite(II), C);
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000377 }
378 // FIXME: Eliminating conditional branches and switches should
379 // also yield a per-call performance boost.
380 else {
381 // Figure out the bonuses that wll accrue due to simple constant
382 // propagation.
383 Instruction &Inst = cast<Instruction>(*U);
384
385 // We can't constant propagate instructions which have effects or
386 // read memory.
387 //
388 // FIXME: It would be nice to capture the fact that a load from a
389 // pointer-to-constant-global is actually a *really* good thing to zap.
390 // Unfortunately, we don't know the pointer that may get propagated here,
391 // so we can't make this decision.
392 if (Inst.mayReadFromMemory() || Inst.mayHaveSideEffects() ||
393 isa<AllocaInst>(Inst))
394 continue;
395
396 bool AllOperandsConstant = true;
397 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i)
398 if (!isa<Constant>(Inst.getOperand(i)) && Inst.getOperand(i) != V) {
399 AllOperandsConstant = false;
400 break;
401 }
402
403 if (AllOperandsConstant)
404 Bonus += CountBonusForConstant(&Inst);
405 }
406 }
Andrew Trick5c655412011-10-01 01:27:56 +0000407
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000408 return Bonus;
409}
410
411int InlineCostAnalyzer::getInlineSize(CallSite CS, Function *Callee) {
412 // Get information about the callee.
413 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000414
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000415 // If we haven't calculated this information yet, do so now.
416 if (CalleeFI->Metrics.NumBlocks == 0)
Andrew Trickb2ab2fa2011-10-01 01:39:05 +0000417 CalleeFI->analyzeFunction(Callee, TD);
Andrew Trick5c655412011-10-01 01:27:56 +0000418
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000419 // InlineCost - This value measures how good of an inline candidate this call
420 // site is to inline. A lower inline cost make is more likely for the call to
421 // be inlined. This value may go negative.
422 //
423 int InlineCost = 0;
424
425 // Compute any size reductions we can expect due to arguments being passed into
426 // the function.
427 //
428 unsigned ArgNo = 0;
429 CallSite::arg_iterator I = CS.arg_begin();
430 for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end();
431 FI != FE; ++I, ++FI, ++ArgNo) {
432
433 // If an alloca is passed in, inlining this function is likely to allow
434 // significant future optimization possibilities (like scalar promotion, and
435 // scalarization), so encourage the inlining of the function.
436 //
437 if (isa<AllocaInst>(I))
438 InlineCost -= CalleeFI->ArgumentWeights[ArgNo].AllocaWeight;
439
440 // If this is a constant being passed into the function, use the argument
441 // weights calculated for the callee to determine how much will be folded
442 // away with this information.
443 else if (isa<Constant>(I))
Andrew Trick5c655412011-10-01 01:27:56 +0000444 InlineCost -= CalleeFI->ArgumentWeights[ArgNo].ConstantWeight;
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000445 }
Andrew Trick5c655412011-10-01 01:27:56 +0000446
Chandler Carruth274d3772012-03-14 23:19:53 +0000447 const DenseMap<std::pair<unsigned, unsigned>, unsigned> &ArgPairWeights
448 = CalleeFI->PointerArgPairWeights;
449 for (DenseMap<std::pair<unsigned, unsigned>, unsigned>::const_iterator I
450 = ArgPairWeights.begin(), E = ArgPairWeights.end();
451 I != E; ++I)
452 if (CS.getArgument(I->first.first)->stripInBoundsConstantOffsets() ==
453 CS.getArgument(I->first.second)->stripInBoundsConstantOffsets())
454 InlineCost -= I->second;
455
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000456 // Each argument passed in has a cost at both the caller and the callee
457 // sides. Measurements show that each argument costs about the same as an
458 // instruction.
459 InlineCost -= (CS.arg_size() * InlineConstants::InstrCost);
460
461 // Now that we have considered all of the factors that make the call site more
462 // likely to be inlined, look at factors that make us not want to inline it.
463
464 // Calls usually take a long time, so they make the inlining gain smaller.
465 InlineCost += CalleeFI->Metrics.NumCalls * InlineConstants::CallPenalty;
466
467 // Look at the size of the callee. Each instruction counts as 5.
Nick Lewycky144bef42011-12-21 20:21:55 +0000468 InlineCost += CalleeFI->Metrics.NumInsts * InlineConstants::InstrCost;
Andrew Trick5c655412011-10-01 01:27:56 +0000469
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000470 return InlineCost;
471}
472
473int InlineCostAnalyzer::getInlineBonuses(CallSite CS, Function *Callee) {
474 // Get information about the callee.
475 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000476
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000477 // If we haven't calculated this information yet, do so now.
478 if (CalleeFI->Metrics.NumBlocks == 0)
Andrew Trickb2ab2fa2011-10-01 01:39:05 +0000479 CalleeFI->analyzeFunction(Callee, TD);
Andrew Trick5c655412011-10-01 01:27:56 +0000480
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000481 bool isDirectCall = CS.getCalledFunction() == Callee;
482 Instruction *TheCall = CS.getInstruction();
483 int Bonus = 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000484
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000485 // If there is only one call of the function, and it has internal linkage,
486 // make it almost guaranteed to be inlined.
487 //
488 if (Callee->hasLocalLinkage() && Callee->hasOneUse() && isDirectCall)
489 Bonus += InlineConstants::LastCallToStaticBonus;
Andrew Trick5c655412011-10-01 01:27:56 +0000490
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000491 // If the instruction after the call, or if the normal destination of the
492 // invoke is an unreachable instruction, the function is noreturn. As such,
493 // there is little point in inlining this.
494 if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
495 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
496 Bonus += InlineConstants::NoreturnPenalty;
497 } else if (isa<UnreachableInst>(++BasicBlock::iterator(TheCall)))
498 Bonus += InlineConstants::NoreturnPenalty;
Andrew Trick5c655412011-10-01 01:27:56 +0000499
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000500 // If this function uses the coldcc calling convention, prefer not to inline
501 // it.
502 if (Callee->getCallingConv() == CallingConv::Cold)
503 Bonus += InlineConstants::ColdccPenalty;
Andrew Trick5c655412011-10-01 01:27:56 +0000504
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000505 // Add to the inline quality for properties that make the call valuable to
506 // inline. This includes factors that indicate that the result of inlining
507 // the function will be optimizable. Currently this just looks at arguments
508 // passed into the function.
509 //
510 CallSite::arg_iterator I = CS.arg_begin();
511 for (Function::arg_iterator FI = Callee->arg_begin(), FE = Callee->arg_end();
512 FI != FE; ++I, ++FI)
513 // Compute any constant bonus due to inlining we want to give here.
514 if (isa<Constant>(I))
515 Bonus += CountBonusForConstant(FI, cast<Constant>(I));
Andrew Trick5c655412011-10-01 01:27:56 +0000516
Eric Christopher8e2da0c2011-02-01 01:16:32 +0000517 return Bonus;
518}
Kenneth Uildriks74fa7322010-10-09 22:06:36 +0000519
Dan Gohmane4aeec02009-10-13 18:30:07 +0000520// getInlineCost - The heuristic used to determine if we should inline the
521// function call or not.
522//
Chandler Carruthf91f5af2012-03-16 06:10:13 +0000523InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS) {
524 return getInlineCost(CS, CS.getCalledFunction());
David Chisnall752e2592010-05-01 15:47:41 +0000525}
526
Chandler Carruthf91f5af2012-03-16 06:10:13 +0000527InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, Function *Callee) {
Dan Gohmane4aeec02009-10-13 18:30:07 +0000528 Instruction *TheCall = CS.getInstruction();
Dan Gohmane4aeec02009-10-13 18:30:07 +0000529 Function *Caller = TheCall->getParent()->getParent();
530
531 // Don't inline functions which can be redefined at link-time to mean
Eric Christopherf27e6082010-03-25 04:49:10 +0000532 // something else. Don't inline functions marked noinline or call sites
533 // marked noinline.
Chandler Carruthf91f5af2012-03-16 06:10:13 +0000534 if (Callee->mayBeOverridden() || Callee->hasFnAttr(Attribute::NoInline) ||
Eric Christopherf27e6082010-03-25 04:49:10 +0000535 CS.isNoInline())
Dan Gohmane4aeec02009-10-13 18:30:07 +0000536 return llvm::InlineCost::getNever();
537
Chris Lattner44b04a52010-04-17 17:55:00 +0000538 // Get information about the callee.
539 FunctionInfo *CalleeFI = &CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000540
Dan Gohmane4aeec02009-10-13 18:30:07 +0000541 // If we haven't calculated this information yet, do so now.
Chris Lattner44b04a52010-04-17 17:55:00 +0000542 if (CalleeFI->Metrics.NumBlocks == 0)
Andrew Trickb2ab2fa2011-10-01 01:39:05 +0000543 CalleeFI->analyzeFunction(Callee, TD);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000544
545 // If we should never inline this, return a huge cost.
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000546 if (CalleeFI->NeverInline())
Dan Gohmane4aeec02009-10-13 18:30:07 +0000547 return InlineCost::getNever();
548
Chris Lattner44b04a52010-04-17 17:55:00 +0000549 // FIXME: It would be nice to kill off CalleeFI->NeverInline. Then we
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000550 // could move this up and avoid computing the FunctionInfo for
Dan Gohmane4aeec02009-10-13 18:30:07 +0000551 // things we are going to just return always inline for. This
552 // requires handling setjmp somewhere else, however.
553 if (!Callee->isDeclaration() && Callee->hasFnAttr(Attribute::AlwaysInline))
554 return InlineCost::getAlways();
Andrew Trick5c655412011-10-01 01:27:56 +0000555
Chris Lattner44b04a52010-04-17 17:55:00 +0000556 if (CalleeFI->Metrics.usesDynamicAlloca) {
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000557 // Get information about the caller.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000558 FunctionInfo &CallerFI = CachedFunctionInfo[Caller];
Dan Gohmane4aeec02009-10-13 18:30:07 +0000559
560 // If we haven't calculated this information yet, do so now.
Chris Lattnerf84755b2010-04-17 17:57:56 +0000561 if (CallerFI.Metrics.NumBlocks == 0) {
Andrew Trickb2ab2fa2011-10-01 01:39:05 +0000562 CallerFI.analyzeFunction(Caller, TD);
Andrew Trick5c655412011-10-01 01:27:56 +0000563
Chris Lattnerf84755b2010-04-17 17:57:56 +0000564 // Recompute the CalleeFI pointer, getting Caller could have invalidated
565 // it.
566 CalleeFI = &CachedFunctionInfo[Callee];
567 }
Dan Gohmane4aeec02009-10-13 18:30:07 +0000568
569 // Don't inline a callee with dynamic alloca into a caller without them.
570 // Functions containing dynamic alloca's are inefficient in various ways;
571 // don't create more inefficiency.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000572 if (!CallerFI.Metrics.usesDynamicAlloca)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000573 return InlineCost::getNever();
574 }
575
Eric Christopher1bcb4282011-01-25 01:34:31 +0000576 // InlineCost - This value measures how good of an inline candidate this call
577 // site is to inline. A lower inline cost make is more likely for the call to
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000578 // be inlined. This value may go negative due to the fact that bonuses
579 // are negative numbers.
Eric Christopher1bcb4282011-01-25 01:34:31 +0000580 //
Eric Christopher4e8af6d2011-02-05 00:49:15 +0000581 int InlineCost = getInlineSize(CS, Callee) + getInlineBonuses(CS, Callee);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000582 return llvm::InlineCost::get(InlineCost);
583}
584
585// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
586// higher threshold to determine if the function call should be inlined.
587float InlineCostAnalyzer::getInlineFudgeFactor(CallSite CS) {
588 Function *Callee = CS.getCalledFunction();
Andrew Trick5c655412011-10-01 01:27:56 +0000589
Chris Lattner44b04a52010-04-17 17:55:00 +0000590 // Get information about the callee.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000591 FunctionInfo &CalleeFI = CachedFunctionInfo[Callee];
Andrew Trick5c655412011-10-01 01:27:56 +0000592
Dan Gohmane4aeec02009-10-13 18:30:07 +0000593 // If we haven't calculated this information yet, do so now.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000594 if (CalleeFI.Metrics.NumBlocks == 0)
Andrew Trickb2ab2fa2011-10-01 01:39:05 +0000595 CalleeFI.analyzeFunction(Callee, TD);
Dan Gohmane4aeec02009-10-13 18:30:07 +0000596
597 float Factor = 1.0f;
598 // Single BB functions are often written to be inlined.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000599 if (CalleeFI.Metrics.NumBlocks == 1)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000600 Factor += 0.5f;
601
602 // Be more aggressive if the function contains a good chunk (if it mades up
603 // at least 10% of the instructions) of vector instructions.
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000604 if (CalleeFI.Metrics.NumVectorInsts > CalleeFI.Metrics.NumInsts/2)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000605 Factor += 2.0f;
Dan Gohmane7f0ed52009-10-13 19:58:07 +0000606 else if (CalleeFI.Metrics.NumVectorInsts > CalleeFI.Metrics.NumInsts/10)
Dan Gohmane4aeec02009-10-13 18:30:07 +0000607 Factor += 1.5f;
608 return Factor;
609}
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000610
611/// growCachedCostInfo - update the cached cost info for Caller after Callee has
612/// been inlined.
613void
Chris Lattner44b04a52010-04-17 17:55:00 +0000614InlineCostAnalyzer::growCachedCostInfo(Function *Caller, Function *Callee) {
615 CodeMetrics &CallerMetrics = CachedFunctionInfo[Caller].Metrics;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000616
617 // For small functions we prefer to recalculate the cost for better accuracy.
Eli Friedmanb1763992011-05-24 20:22:24 +0000618 if (CallerMetrics.NumBlocks < 10 && CallerMetrics.NumInsts < 1000) {
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000619 resetCachedCostInfo(Caller);
620 return;
621 }
622
623 // For large functions, we can save a lot of computation time by skipping
624 // recalculations.
Chris Lattner44b04a52010-04-17 17:55:00 +0000625 if (CallerMetrics.NumCalls > 0)
626 --CallerMetrics.NumCalls;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000627
Chris Lattner44b04a52010-04-17 17:55:00 +0000628 if (Callee == 0) return;
Andrew Trick5c655412011-10-01 01:27:56 +0000629
Chris Lattner44b04a52010-04-17 17:55:00 +0000630 CodeMetrics &CalleeMetrics = CachedFunctionInfo[Callee].Metrics;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000631
Chris Lattner44b04a52010-04-17 17:55:00 +0000632 // If we don't have metrics for the callee, don't recalculate them just to
633 // update an approximation in the caller. Instead, just recalculate the
634 // caller info from scratch.
635 if (CalleeMetrics.NumBlocks == 0) {
636 resetCachedCostInfo(Caller);
637 return;
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000638 }
Andrew Trick5c655412011-10-01 01:27:56 +0000639
Chris Lattnerf84755b2010-04-17 17:57:56 +0000640 // Since CalleeMetrics were already calculated, we know that the CallerMetrics
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000641 // reference isn't invalidated: both were in the DenseMap.
Chris Lattner44b04a52010-04-17 17:55:00 +0000642 CallerMetrics.usesDynamicAlloca |= CalleeMetrics.usesDynamicAlloca;
643
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000644 // FIXME: If any of these three are true for the callee, the callee was
645 // not inlined into the caller, so I think they're redundant here.
Joerg Sonnenberger34706932011-12-18 20:35:43 +0000646 CallerMetrics.exposesReturnsTwice |= CalleeMetrics.exposesReturnsTwice;
Kenneth Uildriks42c7d232010-06-09 15:11:37 +0000647 CallerMetrics.isRecursive |= CalleeMetrics.isRecursive;
648 CallerMetrics.containsIndirectBr |= CalleeMetrics.containsIndirectBr;
649
Chris Lattner44b04a52010-04-17 17:55:00 +0000650 CallerMetrics.NumInsts += CalleeMetrics.NumInsts;
651 CallerMetrics.NumBlocks += CalleeMetrics.NumBlocks;
652 CallerMetrics.NumCalls += CalleeMetrics.NumCalls;
653 CallerMetrics.NumVectorInsts += CalleeMetrics.NumVectorInsts;
654 CallerMetrics.NumRets += CalleeMetrics.NumRets;
655
656 // analyzeBasicBlock counts each function argument as an inst.
657 if (CallerMetrics.NumInsts >= Callee->arg_size())
658 CallerMetrics.NumInsts -= Callee->arg_size();
659 else
660 CallerMetrics.NumInsts = 0;
Andrew Trick5c655412011-10-01 01:27:56 +0000661
Nick Lewycky9a1581b2010-05-12 21:48:15 +0000662 // We are not updating the argument weights. We have already determined that
Jakob Stoklund Olesenf7477472010-03-09 23:02:17 +0000663 // Caller is a fairly large function, so we accept the loss of precision.
664}
Nick Lewycky9a1581b2010-05-12 21:48:15 +0000665
666/// clear - empty the cache of inline costs
667void InlineCostAnalyzer::clear() {
668 CachedFunctionInfo.clear();
669}