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
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/ValueMap.h"
22 #include "llvm/Analysis/CodeMetrics.h"
30 template<class PtrType, unsigned SmallSize>
33 namespace InlineConstants {
34 // Various magic constants used to adjust heuristics.
35 const int InstrCost = 5;
36 const int IndirectCallBonus = 500;
37 const int CallPenalty = 25;
38 const int LastCallToStaticBonus = -15000;
39 const int ColdccPenalty = 2000;
40 const int NoreturnPenalty = 10000;
43 /// InlineCost - Represent the cost of inlining a function. This
44 /// supports special values for functions which should "always" or
45 /// "never" be inlined. Otherwise, the cost represents a unitless
46 /// amount; smaller values increase the likelyhood of the function
55 // This is a do-it-yourself implementation of
58 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
59 enum { TYPE_BITS = 2 };
60 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
61 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
63 Kind getType() const {
64 return Kind(TypedCost >> COST_BITS);
68 // Sign-extend the bottom COST_BITS bits.
69 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
72 InlineCost(int C, int T) {
73 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
74 assert(getCost() == C && "Cost exceeds InlineCost precision");
77 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
78 static InlineCost getAlways() { return InlineCost(0, Always); }
79 static InlineCost getNever() { return InlineCost(0, Never); }
81 bool isVariable() const { return getType() == Value; }
82 bool isAlways() const { return getType() == Always; }
83 bool isNever() const { return getType() == Never; }
85 /// getValue() - Return a "variable" inline cost's amount. It is
86 /// an error to call this on an "always" or "never" InlineCost.
87 int getValue() const {
88 assert(getType() == Value && "Invalid access of InlineCost");
93 /// InlineCostAnalyzer - Cost analyzer used by inliner.
94 class InlineCostAnalyzer {
97 unsigned ConstantWeight;
98 unsigned AllocaWeight;
99 unsigned ConstantBonus;
101 ArgInfo(unsigned CWeight, unsigned AWeight, unsigned CBonus)
102 : ConstantWeight(CWeight), AllocaWeight(AWeight), ConstantBonus(CBonus)
106 struct FunctionInfo {
109 /// ArgumentWeights - Each formal argument of the function is inspected to
110 /// see if it is used in any contexts where making it a constant or alloca
111 /// would reduce the code size. If so, we add some value to the argument
113 std::vector<ArgInfo> ArgumentWeights;
115 /// analyzeFunction - Add information about the specified function
116 /// to the current structure.
117 void analyzeFunction(Function *F);
119 /// NeverInline - Returns true if the function should never be
120 /// inlined into any caller.
124 // The Function* for a function can be changed (by ArgumentPromotion);
125 // the ValueMap will update itself when this happens.
126 ValueMap<const Function *, FunctionInfo> CachedFunctionInfo;
130 /// getInlineCost - The heuristic used to determine if we should inline the
131 /// function call or not.
133 InlineCost getInlineCost(CallSite CS,
134 SmallPtrSet<const Function *, 16> &NeverInline);
135 /// getCalledFunction - The heuristic used to determine if we should inline
136 /// the function call or not. The callee is explicitly specified, to allow
137 /// you to calculate the cost of inlining a function via a pointer. The
138 /// result assumes that the inlined version will always be used. You should
139 /// weight it yourself in cases where this callee will not always be called.
140 InlineCost getInlineCost(CallSite CS,
142 SmallPtrSet<const Function *, 16> &NeverInline);
144 /// getSpecializationBonus - The heuristic used to determine the per-call
145 /// performance boost for using a specialization of Callee with argument
146 /// SpecializedArgNos replaced by a constant.
147 int getSpecializationBonus(Function *Callee,
148 SmallVectorImpl<unsigned> &SpecializedArgNo);
150 /// getSpecializationCost - The heuristic used to determine the code-size
151 /// impact of creating a specialized version of Callee with argument
152 /// SpecializedArgNo replaced by a constant.
153 InlineCost getSpecializationCost(Function *Callee,
154 SmallVectorImpl<unsigned> &SpecializedArgNo);
156 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
157 /// higher threshold to determine if the function call should be inlined.
158 float getInlineFudgeFactor(CallSite CS);
160 /// resetCachedFunctionInfo - erase any cached cost info for this function.
161 void resetCachedCostInfo(Function* Caller) {
162 CachedFunctionInfo[Caller] = FunctionInfo();
165 /// growCachedCostInfo - update the cached cost info for Caller after Callee
166 /// has been inlined. If Callee is NULL it means a dead call has been
168 void growCachedCostInfo(Function* Caller, Function* Callee);
170 /// clear - empty the cache of inline costs
174 /// callIsSmall - If a call is likely to lower to a single target instruction,
175 /// or is otherwise deemed small return true.
176 bool callIsSmall(const Function *Callee);