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 /// NumVectorInsts - Keep track of how many instructions produce vector
46 /// values. The inliner is being more aggressive with inlining vector
48 unsigned NumVectorInsts;
50 /// NumRets - Keep track of how many Ret instructions the block contains.
53 CodeMetrics() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
54 NumBlocks(0), NumVectorInsts(0), NumRets(0) {}
56 /// analyzeBasicBlock - Add information about the specified basic block
57 /// to the current structure.
58 void analyzeBasicBlock(const BasicBlock *BB);
60 /// analyzeFunction - Add information about the specified function
61 /// to the current structure.
62 void analyzeFunction(Function *F);
65 namespace InlineConstants {
66 // Various magic constants used to adjust heuristics.
67 const int CallPenalty = 5;
68 const int LastCallToStaticBonus = -15000;
69 const int ColdccPenalty = 2000;
70 const int NoreturnPenalty = 10000;
73 /// InlineCost - Represent the cost of inlining a function. This
74 /// supports special values for functions which should "always" or
75 /// "never" be inlined. Otherwise, the cost represents a unitless
76 /// amount; smaller values increase the likelyhood of the function
85 // This is a do-it-yourself implementation of
88 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
89 enum { TYPE_BITS = 2 };
90 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
91 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
93 Kind getType() const {
94 return Kind(TypedCost >> COST_BITS);
98 // Sign-extend the bottom COST_BITS bits.
99 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
102 InlineCost(int C, int T) {
103 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
104 assert(getCost() == C && "Cost exceeds InlineCost precision");
107 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
108 static InlineCost getAlways() { return InlineCost(0, Always); }
109 static InlineCost getNever() { return InlineCost(0, Never); }
111 bool isVariable() const { return getType() == Value; }
112 bool isAlways() const { return getType() == Always; }
113 bool isNever() const { return getType() == Never; }
115 /// getValue() - Return a "variable" inline cost's amount. It is
116 /// an error to call this on an "always" or "never" InlineCost.
117 int getValue() const {
118 assert(getType() == Value && "Invalid access of InlineCost");
123 /// InlineCostAnalyzer - Cost analyzer used by inliner.
124 class InlineCostAnalyzer {
127 unsigned ConstantWeight;
128 unsigned AllocaWeight;
130 ArgInfo(unsigned CWeight, unsigned AWeight)
131 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
134 struct FunctionInfo {
137 /// ArgumentWeights - Each formal argument of the function is inspected to
138 /// see if it is used in any contexts where making it a constant or alloca
139 /// would reduce the code size. If so, we add some value to the argument
141 std::vector<ArgInfo> ArgumentWeights;
143 /// CountCodeReductionForConstant - Figure out an approximation for how
144 /// many instructions will be constant folded if the specified value is
146 unsigned CountCodeReductionForConstant(Value *V);
148 /// CountCodeReductionForAlloca - Figure out an approximation of how much
149 /// smaller the function will be if it is inlined into a context where an
150 /// argument becomes an alloca.
152 unsigned CountCodeReductionForAlloca(Value *V);
154 /// analyzeFunction - Add information about the specified function
155 /// to the current structure.
156 void analyzeFunction(Function *F);
159 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
163 /// getInlineCost - The heuristic used to determine if we should inline the
164 /// function call or not.
166 InlineCost getInlineCost(CallSite CS,
167 SmallPtrSet<const Function *, 16> &NeverInline);
169 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
170 /// higher threshold to determine if the function call should be inlined.
171 float getInlineFudgeFactor(CallSite CS);
173 /// resetCachedFunctionInfo - erase any cached cost info for this function.
174 void resetCachedCostInfo(Function* Caller) {
175 CachedFunctionInfo[Caller] = FunctionInfo();