1 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements heuristics for inlining decisions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_INLINECOST_H
15 #define LLVM_ANALYSIS_INLINECOST_H
17 #include "llvm/Function.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/ValueMap.h"
20 #include "llvm/Analysis/CodeMetrics.h"
28 template<class PtrType, unsigned SmallSize>
32 namespace InlineConstants {
33 // Various magic constants used to adjust heuristics.
34 const int InstrCost = 5;
35 const int IndirectCallBonus = -100;
36 const int CallPenalty = 25;
37 const int LastCallToStaticBonus = -15000;
38 const int ColdccPenalty = 2000;
39 const int NoreturnPenalty = 10000;
42 /// InlineCost - Represent the cost of inlining a function. This
43 /// supports special values for functions which should "always" or
44 /// "never" be inlined. Otherwise, the cost represents a unitless
45 /// amount; smaller values increase the likelihood of the function
54 // This is a do-it-yourself implementation of
57 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
58 enum { TYPE_BITS = 2 };
59 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
60 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
62 Kind getType() const {
63 return Kind(TypedCost >> COST_BITS);
67 // Sign-extend the bottom COST_BITS bits.
68 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
71 InlineCost(int C, int T) {
72 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
73 assert(getCost() == C && "Cost exceeds InlineCost precision");
76 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
77 static InlineCost getAlways() { return InlineCost(0, Always); }
78 static InlineCost getNever() { return InlineCost(0, Never); }
80 bool isVariable() const { return getType() == Value; }
81 bool isAlways() const { return getType() == Always; }
82 bool isNever() const { return getType() == Never; }
84 /// getValue() - Return a "variable" inline cost's amount. It is
85 /// an error to call this on an "always" or "never" InlineCost.
86 int getValue() const {
87 assert(getType() == Value && "Invalid access of InlineCost");
92 /// InlineCostAnalyzer - Cost analyzer used by inliner.
93 class InlineCostAnalyzer {
96 unsigned ConstantWeight;
97 unsigned AllocaWeight;
99 ArgInfo(unsigned CWeight, unsigned AWeight)
100 : ConstantWeight(CWeight), AllocaWeight(AWeight)
104 struct FunctionInfo {
107 /// ArgumentWeights - Each formal argument of the function is inspected to
108 /// see if it is used in any contexts where making it a constant or alloca
109 /// would reduce the code size. If so, we add some value to the argument
111 std::vector<ArgInfo> ArgumentWeights;
113 /// PointerArgPairWeights - Weights to use when giving an inline bonus to
114 /// a call site due to correlated pairs of pointers.
115 DenseMap<std::pair<unsigned, unsigned>, unsigned> PointerArgPairWeights;
117 /// countCodeReductionForConstant - Figure out an approximation for how
118 /// many instructions will be constant folded if the specified value is
120 unsigned countCodeReductionForConstant(const CodeMetrics &Metrics,
123 /// countCodeReductionForAlloca - Figure out an approximation of how much
124 /// smaller the function will be if it is inlined into a context where an
125 /// argument becomes an alloca.
126 unsigned countCodeReductionForAlloca(const CodeMetrics &Metrics,
129 /// countCodeReductionForPointerPair - Count the bonus to apply to an
130 /// inline call site where a pair of arguments are pointers and one
131 /// argument is a constant offset from the other. The idea is to
132 /// recognize a common C++ idiom where a begin and end iterator are
133 /// actually pointers, and many operations on the pair of them will be
134 /// constants if the function is called with arguments that have
135 /// a constant offset.
136 void countCodeReductionForPointerPair(
137 const CodeMetrics &Metrics,
138 DenseMap<Value *, unsigned> &PointerArgs,
139 Value *V, unsigned ArgIdx);
141 /// analyzeFunction - Add information about the specified function
142 /// to the current structure.
143 void analyzeFunction(Function *F, const TargetData *TD);
145 /// NeverInline - Returns true if the function should never be
146 /// inlined into any caller.
150 // The Function* for a function can be changed (by ArgumentPromotion);
151 // the ValueMap will update itself when this happens.
152 ValueMap<const Function *, FunctionInfo> CachedFunctionInfo;
154 // TargetData if available, or null.
155 const TargetData *TD;
157 int CountBonusForConstant(Value *V, Constant *C = NULL);
158 int ConstantFunctionBonus(CallSite CS, Constant *C);
159 int getInlineSize(CallSite CS, Function *Callee);
160 int getInlineBonuses(CallSite CS, Function *Callee);
162 InlineCostAnalyzer(): TD(0) {}
164 void setTargetData(const TargetData *TData) { TD = TData; }
166 /// getInlineCost - The heuristic used to determine if we should inline the
167 /// function call or not.
169 InlineCost getInlineCost(CallSite CS,
170 SmallPtrSet<const Function *, 16> &NeverInline);
171 /// getCalledFunction - The heuristic used to determine if we should inline
172 /// the function call or not. The callee is explicitly specified, to allow
173 /// you to calculate the cost of inlining a function via a pointer. The
174 /// result assumes that the inlined version will always be used. You should
175 /// weight it yourself in cases where this callee will not always be called.
176 InlineCost getInlineCost(CallSite CS,
178 SmallPtrSet<const Function *, 16> &NeverInline);
180 /// getSpecializationBonus - The heuristic used to determine the per-call
181 /// performance boost for using a specialization of Callee with argument
182 /// SpecializedArgNos replaced by a constant.
183 int getSpecializationBonus(Function *Callee,
184 SmallVectorImpl<unsigned> &SpecializedArgNo);
186 /// getSpecializationCost - The heuristic used to determine the code-size
187 /// impact of creating a specialized version of Callee with argument
188 /// SpecializedArgNo replaced by a constant.
189 InlineCost getSpecializationCost(Function *Callee,
190 SmallVectorImpl<unsigned> &SpecializedArgNo);
192 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
193 /// higher threshold to determine if the function call should be inlined.
194 float getInlineFudgeFactor(CallSite CS);
196 /// resetCachedFunctionInfo - erase any cached cost info for this function.
197 void resetCachedCostInfo(Function* Caller) {
198 CachedFunctionInfo[Caller] = FunctionInfo();
201 /// growCachedCostInfo - update the cached cost info for Caller after Callee
202 /// has been inlined. If Callee is NULL it means a dead call has been
204 void growCachedCostInfo(Function* Caller, Function* Callee);
206 /// clear - empty the cache of inline costs
210 /// callIsSmall - If a call is likely to lower to a single target instruction,
211 /// or is otherwise deemed small return true.
212 bool callIsSmall(const Function *Callee);