1 //===- InlineCost.cpp - 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
28 template<class PtrType, unsigned SmallSize>
31 // CodeMetrics - Calculate size and a few similar metrics for a set of
34 /// NeverInline - True if this callee should never be inlined into a
38 /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
39 bool usesDynamicAlloca;
41 /// NumInsts, NumBlocks - Keep track of how large each function is, which
42 /// is used to estimate the code size cost of inlining it.
43 unsigned NumInsts, NumBlocks;
45 /// NumCalls - Keep track of the number of calls to 'big' functions.
48 /// NumVectorInsts - Keep track of how many instructions produce vector
49 /// values. The inliner is being more aggressive with inlining vector
51 unsigned NumVectorInsts;
53 /// NumRets - Keep track of how many Ret instructions the block contains.
56 CodeMetrics() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
57 NumBlocks(0), NumCalls(0), NumVectorInsts(0), NumRets(0) {}
59 /// analyzeBasicBlock - Add information about the specified basic block
60 /// to the current structure.
61 void analyzeBasicBlock(const BasicBlock *BB);
63 /// analyzeFunction - Add information about the specified function
64 /// to the current structure.
65 void analyzeFunction(Function *F);
68 namespace InlineConstants {
69 // Various magic constants used to adjust heuristics.
70 const int InstrCost = 5;
71 const int IndirectCallBonus = 500;
72 const int CallPenalty = 25;
73 const int LastCallToStaticBonus = -15000;
74 const int ColdccPenalty = 2000;
75 const int NoreturnPenalty = 10000;
78 /// InlineCost - Represent the cost of inlining a function. This
79 /// supports special values for functions which should "always" or
80 /// "never" be inlined. Otherwise, the cost represents a unitless
81 /// amount; smaller values increase the likelyhood of the function
90 // This is a do-it-yourself implementation of
93 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
94 enum { TYPE_BITS = 2 };
95 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
96 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
98 Kind getType() const {
99 return Kind(TypedCost >> COST_BITS);
102 int getCost() const {
103 // Sign-extend the bottom COST_BITS bits.
104 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
107 InlineCost(int C, int T) {
108 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
109 assert(getCost() == C && "Cost exceeds InlineCost precision");
112 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
113 static InlineCost getAlways() { return InlineCost(0, Always); }
114 static InlineCost getNever() { return InlineCost(0, Never); }
116 bool isVariable() const { return getType() == Value; }
117 bool isAlways() const { return getType() == Always; }
118 bool isNever() const { return getType() == Never; }
120 /// getValue() - Return a "variable" inline cost's amount. It is
121 /// an error to call this on an "always" or "never" InlineCost.
122 int getValue() const {
123 assert(getType() == Value && "Invalid access of InlineCost");
128 /// InlineCostAnalyzer - Cost analyzer used by inliner.
129 class InlineCostAnalyzer {
132 unsigned ConstantWeight;
133 unsigned AllocaWeight;
135 ArgInfo(unsigned CWeight, unsigned AWeight)
136 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
139 struct FunctionInfo {
142 /// ArgumentWeights - Each formal argument of the function is inspected to
143 /// see if it is used in any contexts where making it a constant or alloca
144 /// would reduce the code size. If so, we add some value to the argument
146 std::vector<ArgInfo> ArgumentWeights;
148 /// CountCodeReductionForConstant - Figure out an approximation for how
149 /// many instructions will be constant folded if the specified value is
151 unsigned CountCodeReductionForConstant(Value *V);
153 /// CountCodeReductionForAlloca - Figure out an approximation of how much
154 /// smaller the function will be if it is inlined into a context where an
155 /// argument becomes an alloca.
157 unsigned CountCodeReductionForAlloca(Value *V);
159 /// analyzeFunction - Add information about the specified function
160 /// to the current structure.
161 void analyzeFunction(Function *F);
164 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
168 /// getInlineCost - The heuristic used to determine if we should inline the
169 /// function call or not.
171 InlineCost getInlineCost(CallSite CS,
172 SmallPtrSet<const Function *, 16> &NeverInline);
174 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
175 /// higher threshold to determine if the function call should be inlined.
176 float getInlineFudgeFactor(CallSite CS);
178 /// resetCachedFunctionInfo - erase any cached cost info for this function.
179 void resetCachedCostInfo(Function* Caller) {
180 CachedFunctionInfo[Caller] = FunctionInfo();