1 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements heuristics for inlining decisions.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_ANALYSIS_INLINECOST_H
14 #define LLVM_ANALYSIS_INLINECOST_H
16 #include "llvm/Analysis/AssumptionCache.h"
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/Analysis/OptimizationRemarkEmitter.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 /// Do not inline functions which allocate this many bytes on the stack
49 /// when the caller is recursive.
50 const unsigned TotalAllocaSizeRecursiveCaller
= 1024;
53 /// 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 /// The estimated cost of inlining this callsite.
72 /// The adjusted threshold against which this cost was computed.
75 /// Must be set for Always and Never instances.
76 const char *Reason
= nullptr;
78 // Trivial constructor, interesting logic in the factory functions below.
79 InlineCost(int Cost
, int Threshold
, const char *Reason
= nullptr)
80 : Cost(Cost
), Threshold(Threshold
), Reason(Reason
) {
81 assert((isVariable() || Reason
) &&
82 "Reason must be provided for Never or Always");
86 static InlineCost
get(int Cost
, int Threshold
) {
87 assert(Cost
> AlwaysInlineCost
&& "Cost crosses sentinel value");
88 assert(Cost
< NeverInlineCost
&& "Cost crosses sentinel value");
89 return InlineCost(Cost
, Threshold
);
91 static InlineCost
getAlways(const char *Reason
) {
92 return InlineCost(AlwaysInlineCost
, 0, Reason
);
94 static InlineCost
getNever(const char *Reason
) {
95 return InlineCost(NeverInlineCost
, 0, Reason
);
98 /// Test whether the inline cost is low enough for inlining.
99 explicit operator bool() const {
100 return Cost
< Threshold
;
103 bool isAlways() const { return Cost
== AlwaysInlineCost
; }
104 bool isNever() const { return Cost
== NeverInlineCost
; }
105 bool isVariable() const { return !isAlways() && !isNever(); }
107 /// Get the inline cost estimate.
108 /// It is an error to call this on an "always" or "never" InlineCost.
109 int getCost() const {
110 assert(isVariable() && "Invalid access of InlineCost");
114 /// Get the threshold against which the cost was computed
115 int getThreshold() const {
116 assert(isVariable() && "Invalid access of InlineCost");
120 /// Get the reason of Always or Never.
121 const char *getReason() const {
122 assert((Reason
|| isVariable()) &&
123 "InlineCost reason must be set for Always or Never");
127 /// Get the cost delta from the threshold for inlining.
128 /// Only valid if the cost is of the variable kind. Returns a negative
129 /// value if the cost is too high to inline.
130 int getCostDelta() const { return Threshold
- getCost(); }
133 /// InlineResult is basically true or false. For false results the message
134 /// describes a reason why it is decided not to inline.
135 struct InlineResult
{
136 const char *message
= nullptr;
137 InlineResult(bool result
, const char *message
= nullptr)
138 : message(result
? nullptr : (message
? message
: "cost > threshold")) {}
139 InlineResult(const char *message
= nullptr) : message(message
) {}
140 operator bool() const { return !message
; }
141 operator const char *() const { return message
; }
144 /// Thresholds to tune inline cost analysis. The inline cost analysis decides
145 /// the condition to apply a threshold and applies it. Otherwise,
146 /// DefaultThreshold is used. If a threshold is Optional, it is applied only
147 /// when it has a valid value. Typically, users of inline cost analysis
148 /// obtain an InlineParams object through one of the \c getInlineParams methods
149 /// and pass it to \c getInlineCost. Some specialized versions of inliner
150 /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
153 struct InlineParams
{
154 /// The default threshold to start with for a callee.
155 int DefaultThreshold
;
157 /// Threshold to use for callees with inline hint.
158 Optional
<int> HintThreshold
;
160 /// Threshold to use for cold callees.
161 Optional
<int> ColdThreshold
;
163 /// Threshold to use when the caller is optimized for size.
164 Optional
<int> OptSizeThreshold
;
166 /// Threshold to use when the caller is optimized for minsize.
167 Optional
<int> OptMinSizeThreshold
;
169 /// Threshold to use when the callsite is considered hot.
170 Optional
<int> HotCallSiteThreshold
;
172 /// Threshold to use when the callsite is considered hot relative to function
174 Optional
<int> LocallyHotCallSiteThreshold
;
176 /// Threshold to use when the callsite is considered cold.
177 Optional
<int> ColdCallSiteThreshold
;
179 /// Compute inline cost even when the cost has exceeded the threshold.
180 Optional
<bool> ComputeFullInlineCost
;
183 /// Generate the parameters to tune the inline cost analysis based only on the
184 /// commandline options.
185 InlineParams
getInlineParams();
187 /// Generate the parameters to tune the inline cost analysis based on command
188 /// line options. If -inline-threshold option is not explicitly passed,
189 /// \p Threshold is used as the default threshold.
190 InlineParams
getInlineParams(int Threshold
);
192 /// Generate the parameters to tune the inline cost analysis based on command
193 /// line options. If -inline-threshold option is not explicitly passed,
194 /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
195 /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
196 /// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
198 InlineParams
getInlineParams(unsigned OptLevel
, unsigned SizeOptLevel
);
200 /// Return the cost associated with a callsite, including parameter passing
201 /// and the call/return instruction.
202 int getCallsiteCost(CallBase
&Call
, const DataLayout
&DL
);
204 /// Get an InlineCost object representing the cost of inlining this
207 /// Note that a default threshold is passed into this function. This threshold
208 /// could be modified based on callsite's properties and only costs below this
209 /// new threshold are computed with any accuracy. The new threshold can be
210 /// used to bound the computation necessary to determine whether the cost is
211 /// sufficiently low to warrant inlining.
213 /// Also note that calling this function *dynamically* computes the cost of
214 /// inlining the callsite. It is an expensive, heavyweight call.
215 InlineCost
getInlineCost(
216 CallBase
&Call
, const InlineParams
&Params
, TargetTransformInfo
&CalleeTTI
,
217 std::function
<AssumptionCache
&(Function
&)> &GetAssumptionCache
,
218 Optional
<function_ref
<BlockFrequencyInfo
&(Function
&)>> GetBFI
,
219 ProfileSummaryInfo
*PSI
, OptimizationRemarkEmitter
*ORE
= nullptr);
221 /// Get an InlineCost with the callee explicitly specified.
222 /// This allows you to calculate the cost of inlining a function via a
223 /// pointer. This behaves exactly as the version with no explicit callee
224 /// parameter in all other respects.
227 getInlineCost(CallBase
&Call
, Function
*Callee
, const InlineParams
&Params
,
228 TargetTransformInfo
&CalleeTTI
,
229 std::function
<AssumptionCache
&(Function
&)> &GetAssumptionCache
,
230 Optional
<function_ref
<BlockFrequencyInfo
&(Function
&)>> GetBFI
,
231 ProfileSummaryInfo
*PSI
, OptimizationRemarkEmitter
*ORE
);
233 /// Minimal filter to detect invalid constructs for inlining.
234 InlineResult
isInlineViable(Function
&Callee
);