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 namespace InlineConstants {
32 // Various magic constants used to adjust heuristics.
33 const int CallPenalty = 5;
34 const int LastCallToStaticBonus = -15000;
35 const int ColdccPenalty = 2000;
36 const int NoreturnPenalty = 10000;
39 /// InlineCost - Represent the cost of inlining a function. This
40 /// supports special values for functions which should "always" or
41 /// "never" be inlined. Otherwise, the cost represents a unitless
42 /// amount; smaller values increase the likelyhood of the function
51 // This is a do-it-yourself implementation of
54 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
55 enum { TYPE_BITS = 2 };
56 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
57 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
59 Kind getType() const {
60 return Kind(TypedCost >> COST_BITS);
64 // Sign-extend the bottom COST_BITS bits.
65 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
68 InlineCost(int C, int T) {
69 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
70 assert(getCost() == C && "Cost exceeds InlineCost precision");
73 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
74 static InlineCost getAlways() { return InlineCost(0, Always); }
75 static InlineCost getNever() { return InlineCost(0, Never); }
77 bool isVariable() const { return getType() == Value; }
78 bool isAlways() const { return getType() == Always; }
79 bool isNever() const { return getType() == Never; }
81 /// getValue() - Return a "variable" inline cost's amount. It is
82 /// an error to call this on an "always" or "never" InlineCost.
83 int getValue() const {
84 assert(getType() == Value && "Invalid access of InlineCost");
89 /// InlineCostAnalyzer - Cost analyzer used by inliner.
90 class InlineCostAnalyzer {
93 unsigned ConstantWeight;
94 unsigned AllocaWeight;
96 ArgInfo(unsigned CWeight, unsigned AWeight)
97 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
100 // RegionInfo - Calculate size and a few related metrics for a set of
103 /// NeverInline - True if this callee should never be inlined into a
107 /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
108 bool usesDynamicAlloca;
110 /// NumInsts, NumBlocks - Keep track of how large each function is, which
111 /// is used to estimate the code size cost of inlining it.
112 unsigned NumInsts, NumBlocks;
114 /// NumVectorInsts - Keep track of how many instructions produce vector
115 /// values. The inliner is being more aggressive with inlining vector
117 unsigned NumVectorInsts;
119 /// NumRets - Keep track of how many Ret instructions the block contains.
122 /// ArgumentWeights - Each formal argument of the function is inspected to
123 /// see if it is used in any contexts where making it a constant or alloca
124 /// would reduce the code size. If so, we add some value to the argument
126 std::vector<ArgInfo> ArgumentWeights;
128 RegionInfo() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
129 NumBlocks(0), NumVectorInsts(0), NumRets(0) {}
131 /// analyzeBasicBlock - Add information about the specified basic block
132 /// to the current structure.
133 void analyzeBasicBlock(const BasicBlock *BB);
135 /// analyzeFunction - Add information about the specified function
136 /// to the current structure.
137 void analyzeFunction(Function *F);
139 /// CountCodeReductionForConstant - Figure out an approximation for how
140 /// many instructions will be constant folded if the specified value is
142 unsigned CountCodeReductionForConstant(Value *V);
144 /// CountCodeReductionForAlloca - Figure out an approximation of how much
145 /// smaller the function will be if it is inlined into a context where an
146 /// argument becomes an alloca.
148 unsigned CountCodeReductionForAlloca(Value *V);
151 std::map<const Function *, RegionInfo> CachedFunctionInfo;
155 /// getInlineCost - The heuristic used to determine if we should inline the
156 /// function call or not.
158 InlineCost getInlineCost(CallSite CS,
159 SmallPtrSet<const Function *, 16> &NeverInline);
161 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
162 /// higher threshold to determine if the function call should be inlined.
163 float getInlineFudgeFactor(CallSite CS);
165 /// resetCachedFunctionInfo - erase any cached cost info for this function.
166 void resetCachedCostInfo(Function* Caller) {
167 CachedFunctionInfo[Caller].NumBlocks = 0;