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
21 #include "llvm/ADT/DenseMap.h"
29 template<class PtrType, unsigned SmallSize>
32 // CodeMetrics - Calculate size and a few similar metrics for a set of
35 /// NeverInline - True if this callee should never be inlined into a
39 /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
40 bool usesDynamicAlloca;
42 /// NumInsts, NumBlocks - Keep track of how large each function is, which
43 /// is used to estimate the code size cost of inlining it.
44 unsigned NumInsts, NumBlocks;
46 /// NumBBInsts - Keeps track of basic block code size estimates.
47 DenseMap<const BasicBlock *, unsigned> NumBBInsts;
49 /// NumCalls - Keep track of the number of calls to 'big' functions.
52 /// NumVectorInsts - Keep track of how many instructions produce vector
53 /// values. The inliner is being more aggressive with inlining vector
55 unsigned NumVectorInsts;
57 /// NumRets - Keep track of how many Ret instructions the block contains.
60 CodeMetrics() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
61 NumBlocks(0), NumCalls(0), NumVectorInsts(0), NumRets(0) {}
63 /// analyzeBasicBlock - Add information about the specified basic block
64 /// to the current structure.
65 void analyzeBasicBlock(const BasicBlock *BB);
67 /// analyzeFunction - Add information about the specified function
68 /// to the current structure.
69 void analyzeFunction(Function *F);
72 namespace InlineConstants {
73 // Various magic constants used to adjust heuristics.
74 const int InstrCost = 5;
75 const int IndirectCallBonus = 500;
76 const int CallPenalty = 25;
77 const int LastCallToStaticBonus = -15000;
78 const int ColdccPenalty = 2000;
79 const int NoreturnPenalty = 10000;
82 /// InlineCost - Represent the cost of inlining a function. This
83 /// supports special values for functions which should "always" or
84 /// "never" be inlined. Otherwise, the cost represents a unitless
85 /// amount; smaller values increase the likelyhood of the function
94 // This is a do-it-yourself implementation of
97 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
98 enum { TYPE_BITS = 2 };
99 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
100 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
102 Kind getType() const {
103 return Kind(TypedCost >> COST_BITS);
106 int getCost() const {
107 // Sign-extend the bottom COST_BITS bits.
108 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
111 InlineCost(int C, int T) {
112 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
113 assert(getCost() == C && "Cost exceeds InlineCost precision");
116 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
117 static InlineCost getAlways() { return InlineCost(0, Always); }
118 static InlineCost getNever() { return InlineCost(0, Never); }
120 bool isVariable() const { return getType() == Value; }
121 bool isAlways() const { return getType() == Always; }
122 bool isNever() const { return getType() == Never; }
124 /// getValue() - Return a "variable" inline cost's amount. It is
125 /// an error to call this on an "always" or "never" InlineCost.
126 int getValue() const {
127 assert(getType() == Value && "Invalid access of InlineCost");
132 /// InlineCostAnalyzer - Cost analyzer used by inliner.
133 class InlineCostAnalyzer {
136 unsigned ConstantWeight;
137 unsigned AllocaWeight;
139 ArgInfo(unsigned CWeight, unsigned AWeight)
140 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
143 struct FunctionInfo {
146 /// ArgumentWeights - Each formal argument of the function is inspected to
147 /// see if it is used in any contexts where making it a constant or alloca
148 /// would reduce the code size. If so, we add some value to the argument
150 std::vector<ArgInfo> ArgumentWeights;
152 /// CountCodeReductionForConstant - Figure out an approximation for how
153 /// many instructions will be constant folded if the specified value is
155 unsigned CountCodeReductionForConstant(Value *V);
157 /// CountCodeReductionForAlloca - Figure out an approximation of how much
158 /// smaller the function will be if it is inlined into a context where an
159 /// argument becomes an alloca.
161 unsigned CountCodeReductionForAlloca(Value *V);
163 /// analyzeFunction - Add information about the specified function
164 /// to the current structure.
165 void analyzeFunction(Function *F);
168 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
172 /// getInlineCost - The heuristic used to determine if we should inline the
173 /// function call or not.
175 InlineCost getInlineCost(CallSite CS,
176 SmallPtrSet<const Function *, 16> &NeverInline);
178 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
179 /// higher threshold to determine if the function call should be inlined.
180 float getInlineFudgeFactor(CallSite CS);
182 /// resetCachedFunctionInfo - erase any cached cost info for this function.
183 void resetCachedCostInfo(Function* Caller) {
184 CachedFunctionInfo[Caller] = FunctionInfo();
187 /// growCachedCostInfo - update the cached cost info for Caller after Callee
188 /// has been inlined. If Callee is NULL it means a dead call has been
190 void growCachedCostInfo(Function* Caller, Function* Callee);