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 /// analyzeFunction - Add information about the specified function
114 /// to the current structure.
115 void analyzeFunction(Function *F, const TargetData *TD);
117 /// NeverInline - Returns true if the function should never be
118 /// inlined into any caller.
122 // The Function* for a function can be changed (by ArgumentPromotion);
123 // the ValueMap will update itself when this happens.
124 ValueMap<const Function *, FunctionInfo> CachedFunctionInfo;
126 // TargetData if available, or null.
127 const TargetData *TD;
129 int CountBonusForConstant(Value *V, Constant *C = NULL);
130 int ConstantFunctionBonus(CallSite CS, Constant *C);
131 int getInlineSize(CallSite CS, Function *Callee);
132 int getInlineBonuses(CallSite CS, Function *Callee);
134 InlineCostAnalyzer(): TD(0) {}
136 void setTargetData(const TargetData *TData) { TD = TData; }
138 /// getInlineCost - The heuristic used to determine if we should inline the
139 /// function call or not.
141 InlineCost getInlineCost(CallSite CS,
142 SmallPtrSet<const Function *, 16> &NeverInline);
143 /// getCalledFunction - The heuristic used to determine if we should inline
144 /// the function call or not. The callee is explicitly specified, to allow
145 /// you to calculate the cost of inlining a function via a pointer. The
146 /// result assumes that the inlined version will always be used. You should
147 /// weight it yourself in cases where this callee will not always be called.
148 InlineCost getInlineCost(CallSite CS,
150 SmallPtrSet<const Function *, 16> &NeverInline);
152 /// getSpecializationBonus - The heuristic used to determine the per-call
153 /// performance boost for using a specialization of Callee with argument
154 /// SpecializedArgNos replaced by a constant.
155 int getSpecializationBonus(Function *Callee,
156 SmallVectorImpl<unsigned> &SpecializedArgNo);
158 /// getSpecializationCost - The heuristic used to determine the code-size
159 /// impact of creating a specialized version of Callee with argument
160 /// SpecializedArgNo replaced by a constant.
161 InlineCost getSpecializationCost(Function *Callee,
162 SmallVectorImpl<unsigned> &SpecializedArgNo);
164 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
165 /// higher threshold to determine if the function call should be inlined.
166 float getInlineFudgeFactor(CallSite CS);
168 /// resetCachedFunctionInfo - erase any cached cost info for this function.
169 void resetCachedCostInfo(Function* Caller) {
170 CachedFunctionInfo[Caller] = FunctionInfo();
173 /// growCachedCostInfo - update the cached cost info for Caller after Callee
174 /// has been inlined. If Callee is NULL it means a dead call has been
176 void growCachedCostInfo(Function* Caller, Function* Callee);
178 /// clear - empty the cache of inline costs
182 /// callIsSmall - If a call is likely to lower to a single target instruction,
183 /// or is otherwise deemed small return true.
184 bool callIsSmall(const Function *Callee);