1 //===- InlineCost.h - 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
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/Analysis/AssumptionCache.h"
23 class AssumptionCacheTracker;
24 class BlockFrequencyInfo;
28 class ProfileSummaryInfo;
29 class TargetTransformInfo;
31 namespace InlineConstants {
32 // Various thresholds used by inline cost analysis.
33 /// Use when optsize (-Os) is specified.
34 const int OptSizeThreshold = 50;
36 /// Use when minsize (-Oz) is specified.
37 const int OptMinSizeThreshold = 5;
39 /// Use when -O3 is specified.
40 const int OptAggressiveThreshold = 250;
42 // Various magic constants used to adjust heuristics.
43 const int InstrCost = 5;
44 const int IndirectCallThreshold = 100;
45 const int CallPenalty = 25;
46 const int LastCallToStaticBonus = 15000;
47 const int ColdccPenalty = 2000;
48 const int NoreturnPenalty = 10000;
49 /// Do not inline functions which allocate this many bytes on the stack
50 /// when the caller is recursive.
51 const unsigned TotalAllocaSizeRecursiveCaller = 1024;
54 /// \brief Represents the cost of inlining a function.
56 /// This supports special values for functions which should "always" or
57 /// "never" be inlined. Otherwise, the cost represents a unitless amount;
58 /// smaller values increase the likelihood of the function being inlined.
60 /// Objects of this type also provide the adjusted threshold for inlining
61 /// based on the information available for a particular callsite. They can be
62 /// directly tested to determine if inlining should occur given the cost and
63 /// threshold for this cost metric.
66 AlwaysInlineCost = INT_MIN,
67 NeverInlineCost = INT_MAX
70 /// \brief The estimated cost of inlining this callsite.
73 /// \brief The adjusted threshold against which this cost was computed.
76 // Trivial constructor, interesting logic in the factory functions below.
77 InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
80 static InlineCost get(int Cost, int Threshold) {
81 assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
82 assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
83 return InlineCost(Cost, Threshold);
85 static InlineCost getAlways() {
86 return InlineCost(AlwaysInlineCost, 0);
88 static InlineCost getNever() {
89 return InlineCost(NeverInlineCost, 0);
92 /// \brief Test whether the inline cost is low enough for inlining.
93 explicit operator bool() const {
94 return Cost < Threshold;
97 bool isAlways() const { return Cost == AlwaysInlineCost; }
98 bool isNever() const { return Cost == NeverInlineCost; }
99 bool isVariable() const { return !isAlways() && !isNever(); }
101 /// \brief Get the inline cost estimate.
102 /// It is an error to call this on an "always" or "never" InlineCost.
103 int getCost() const {
104 assert(isVariable() && "Invalid access of InlineCost");
108 /// \brief Get the cost delta from the threshold for inlining.
109 /// Only valid if the cost is of the variable kind. Returns a negative
110 /// value if the cost is too high to inline.
111 int getCostDelta() const { return Threshold - getCost(); }
114 /// Thresholds to tune inline cost analysis. The inline cost analysis decides
115 /// the condition to apply a threshold and applies it. Otherwise,
116 /// DefaultThreshold is used. If a threshold is Optional, it is applied only
117 /// when it has a valid value. Typically, users of inline cost analysis
118 /// obtain an InlineParams object through one of the \c getInlineParams methods
119 /// and pass it to \c getInlineCost. Some specialized versions of inliner
120 /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
123 struct InlineParams {
124 /// The default threshold to start with for a callee.
125 int DefaultThreshold;
127 /// Threshold to use for callees with inline hint.
128 Optional<int> HintThreshold;
130 /// Threshold to use for cold callees.
131 Optional<int> ColdThreshold;
133 /// Threshold to use when the caller is optimized for size.
134 Optional<int> OptSizeThreshold;
136 /// Threshold to use when the caller is optimized for minsize.
137 Optional<int> OptMinSizeThreshold;
139 /// Threshold to use when the callsite is considered hot.
140 Optional<int> HotCallSiteThreshold;
142 /// Threshold to use when the callsite is considered cold.
143 Optional<int> ColdCallSiteThreshold;
146 /// Generate the parameters to tune the inline cost analysis based only on the
147 /// commandline options.
148 InlineParams getInlineParams();
150 /// Generate the parameters to tune the inline cost analysis based on command
151 /// line options. If -inline-threshold option is not explicitly passed,
152 /// \p Threshold is used as the default threshold.
153 InlineParams getInlineParams(int Threshold);
155 /// Generate the parameters to tune the inline cost analysis based on command
156 /// line options. If -inline-threshold option is not explicitly passed,
157 /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
158 /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
159 /// \p SizeOptLevel of 1 corresponds to the the -Os flag and 2 corresponds to
161 InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
163 /// \brief Get an InlineCost object representing the cost of inlining this
166 /// Note that a default threshold is passed into this function. This threshold
167 /// could be modified based on callsite's properties and only costs below this
168 /// new threshold are computed with any accuracy. The new threshold can be
169 /// used to bound the computation necessary to determine whether the cost is
170 /// sufficiently low to warrant inlining.
172 /// Also note that calling this function *dynamically* computes the cost of
173 /// inlining the callsite. It is an expensive, heavyweight call.
175 getInlineCost(CallSite CS, const InlineParams &Params,
176 TargetTransformInfo &CalleeTTI,
177 std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
178 Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
179 ProfileSummaryInfo *PSI);
181 /// \brief Get an InlineCost with the callee explicitly specified.
182 /// This allows you to calculate the cost of inlining a function via a
183 /// pointer. This behaves exactly as the version with no explicit callee
184 /// parameter in all other respects.
187 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
188 TargetTransformInfo &CalleeTTI,
189 std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
190 Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
191 ProfileSummaryInfo *PSI);
193 /// \brief Minimal filter to detect invalid constructs for inlining.
194 bool isInlineViable(Function &Callee);