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