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_TRANSFORMS_UTILS_INLINECOST_H
15 #define LLVM_TRANSFORMS_UTILS_INLINECOST_H
27 template<class PtrType, unsigned SmallSize>
30 /// InlineCost - Represent the cost of inlining a function. This
31 /// supports special values for functions which should "always" or
32 /// "never" be inlined. Otherwise, the cost represents a unitless
33 /// amount; smaller values increase the likelyhood of the function
42 // This is a do-it-yourself implementation of
45 // We used to use bitfields, but they were sometimes miscompiled (PR3822).
46 enum { TYPE_BITS = 2 };
47 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS };
48 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;
50 Kind getType() const {
51 return Kind(TypedCost >> COST_BITS);
55 // Sign-extend the bottom COST_BITS bits.
56 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;
59 InlineCost(int C, int T) {
60 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS);
61 assert(getCost() == C && "Cost exceeds InlineCost precision");
64 static InlineCost get(int Cost) { return InlineCost(Cost, Value); }
65 static InlineCost getAlways() { return InlineCost(0, Always); }
66 static InlineCost getNever() { return InlineCost(0, Never); }
68 bool isVariable() const { return getType() == Value; }
69 bool isAlways() const { return getType() == Always; }
70 bool isNever() const { return getType() == Never; }
72 /// getValue() - Return a "variable" inline cost's amount. It is
73 /// an error to call this on an "always" or "never" InlineCost.
74 int getValue() const {
75 assert(getType() == Value && "Invalid access of InlineCost");
80 /// InlineCostAnalyzer - Cost analyzer used by inliner.
81 class InlineCostAnalyzer {
84 unsigned ConstantWeight;
85 unsigned AllocaWeight;
87 ArgInfo(unsigned CWeight, unsigned AWeight)
88 : ConstantWeight(CWeight), AllocaWeight(AWeight) {}
91 // FunctionInfo - For each function, calculate the size of it in blocks and
94 /// NeverInline - True if this callee should never be inlined into a
98 /// usesDynamicAlloca - True if this function calls alloca (in the C sense).
99 bool usesDynamicAlloca;
101 /// NumInsts, NumBlocks - Keep track of how large each function is, which
102 /// is used to estimate the code size cost of inlining it.
103 unsigned NumInsts, NumBlocks;
105 /// NumVectorInsts - Keep track of how many instructions produce vector
106 /// values. The inliner is being more aggressive with inlining vector
108 unsigned NumVectorInsts;
110 /// ArgumentWeights - Each formal argument of the function is inspected to
111 /// see if it is used in any contexts where making it a constant or alloca
112 /// would reduce the code size. If so, we add some value to the argument
114 std::vector<ArgInfo> ArgumentWeights;
116 FunctionInfo() : NeverInline(false), usesDynamicAlloca(false), NumInsts(0),
117 NumBlocks(0), NumVectorInsts(0) {}
119 /// analyzeFunction - Fill in the current structure with information
120 /// gleaned from the specified function.
121 void analyzeFunction(Function *F);
123 /// CountCodeReductionForConstant - Figure out an approximation for how
124 /// many instructions will be constant folded if the specified value is
126 unsigned CountCodeReductionForConstant(Value *V);
128 /// CountCodeReductionForAlloca - Figure out an approximation of how much
129 /// smaller the function will be if it is inlined into a context where an
130 /// argument becomes an alloca.
132 unsigned CountCodeReductionForAlloca(Value *V);
135 std::map<const Function *, FunctionInfo> CachedFunctionInfo;
139 /// getInlineCost - The heuristic used to determine if we should inline the
140 /// function call or not.
142 InlineCost getInlineCost(CallSite CS,
143 SmallPtrSet<const Function *, 16> &NeverInline);
145 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a
146 /// higher threshold to determine if the function call should be inlined.
147 float getInlineFudgeFactor(CallSite CS);
149 /// resetCachedFunctionInfo - erase any cached cost info for this function.
150 void resetCachedCostInfo(Function* Caller) {
151 CachedFunctionInfo[Caller].NumBlocks = 0;