LLVM  9.0.0svn
InlineCost.h
Go to the documentation of this file.
1 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements heuristics for inlining decisions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_INLINECOST_H
14 #define LLVM_ANALYSIS_INLINECOST_H
15 
19 #include <cassert>
20 #include <climits>
21 
22 namespace llvm {
23 class AssumptionCacheTracker;
24 class BlockFrequencyInfo;
25 class CallBase;
26 class DataLayout;
27 class Function;
28 class ProfileSummaryInfo;
29 class TargetTransformInfo;
30 
31 namespace InlineConstants {
32 // Various thresholds used by inline cost analysis.
33 /// Use when optsize (-Os) is specified.
34 const int OptSizeThreshold = 50;
35 
36 /// Use when minsize (-Oz) is specified.
37 const int OptMinSizeThreshold = 5;
38 
39 /// Use when -O3 is specified.
40 const int OptAggressiveThreshold = 250;
41 
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;
51 }
52 
53 /// Represents the cost of inlining a function.
54 ///
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.
58 ///
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.
63 class InlineCost {
64  enum SentinelValues {
65  AlwaysInlineCost = INT_MIN,
66  NeverInlineCost = INT_MAX
67  };
68 
69  /// The estimated cost of inlining this callsite.
70  const int Cost;
71 
72  /// The adjusted threshold against which this cost was computed.
73  const int Threshold;
74 
75  /// Must be set for Always and Never instances.
76  const char *Reason = nullptr;
77 
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");
83  }
84 
85 public:
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);
90  }
91  static InlineCost getAlways(const char *Reason) {
92  return InlineCost(AlwaysInlineCost, 0, Reason);
93  }
94  static InlineCost getNever(const char *Reason) {
95  return InlineCost(NeverInlineCost, 0, Reason);
96  }
97 
98  /// Test whether the inline cost is low enough for inlining.
99  explicit operator bool() const {
100  return Cost < Threshold;
101  }
102 
103  bool isAlways() const { return Cost == AlwaysInlineCost; }
104  bool isNever() const { return Cost == NeverInlineCost; }
105  bool isVariable() const { return !isAlways() && !isNever(); }
106 
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");
111  return Cost;
112  }
113 
114  /// Get the threshold against which the cost was computed
115  int getThreshold() const {
116  assert(isVariable() && "Invalid access of InlineCost");
117  return Threshold;
118  }
119 
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");
124  return Reason;
125  }
126 
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(); }
131 };
132 
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; }
142 };
143 
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
151 /// object.
152 
153 struct InlineParams {
154  /// The default threshold to start with for a callee.
156 
157  /// Threshold to use for callees with inline hint.
159 
160  /// Threshold to use for cold callees.
162 
163  /// Threshold to use when the caller is optimized for size.
165 
166  /// Threshold to use when the caller is optimized for minsize.
168 
169  /// Threshold to use when the callsite is considered hot.
171 
172  /// Threshold to use when the callsite is considered hot relative to function
173  /// entry.
175 
176  /// Threshold to use when the callsite is considered cold.
178 
179  /// Compute inline cost even when the cost has exceeded the threshold.
181 };
182 
183 /// Generate the parameters to tune the inline cost analysis based only on the
184 /// commandline options.
186 
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.
191 
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
197 /// the -Oz flag.
198 InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
199 
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);
203 
204 /// Get an InlineCost object representing the cost of inlining this
205 /// callsite.
206 ///
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.
212 ///
213 /// Also note that calling this function *dynamically* computes the cost of
214 /// inlining the callsite. It is an expensive, heavyweight call.
216  CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
217  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
219  ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
220 
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.
225 //
227 getInlineCost(CallBase &Call, Function *Callee, const InlineParams &Params,
228  TargetTransformInfo &CalleeTTI,
229  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
232 
233 /// Minimal filter to detect invalid constructs for inlining.
235 }
236 
237 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:153
InlineCost getInlineCost(CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI, std::function< AssumptionCache &(Function &)> &GetAssumptionCache, Optional< function_ref< BlockFrequencyInfo &(Function &)>> GetBFI, ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE=nullptr)
Get an InlineCost object representing the cost of inlining this callsite.
bool isNever() const
Definition: InlineCost.h:104
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Optional< int > OptSizeThreshold
Threshold to use when the caller is optimized for size.
Definition: InlineCost.h:164
bool isVariable() const
Definition: InlineCost.h:105
Analysis providing profile information.
const int OptMinSizeThreshold
Use when minsize (-Oz) is specified.
Definition: InlineCost.h:37
const int ColdccPenalty
Definition: InlineCost.h:47
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:116
A cache of @llvm.assume calls within a function.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1100
Represents the cost of inlining a function.
Definition: InlineCost.h:63
Optional< int > HintThreshold
Threshold to use for callees with inline hint.
Definition: InlineCost.h:158
const int LastCallToStaticBonus
Definition: InlineCost.h:46
static InlineCost getAlways(const char *Reason)
Definition: InlineCost.h:91
const int CallPenalty
Definition: InlineCost.h:45
InlineResult is basically true or false.
Definition: InlineCost.h:135
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool isAlways() const
Definition: InlineCost.h:103
const int IndirectCallThreshold
Definition: InlineCost.h:44
const int OptAggressiveThreshold
Use when -O3 is specified.
Definition: InlineCost.h:40
Optional< int > OptMinSizeThreshold
Threshold to use when the caller is optimized for minsize.
Definition: InlineCost.h:167
InlineResult(bool result, const char *message=nullptr)
Definition: InlineCost.h:137
InlineResult isInlineViable(Function &Callee)
Minimal filter to detect invalid constructs for inlining.
Optional< int > LocallyHotCallSiteThreshold
Threshold to use when the callsite is considered hot relative to function entry.
Definition: InlineCost.h:174
int getThreshold() const
Get the threshold against which the cost was computed.
Definition: InlineCost.h:115
const char * getReason() const
Get the reason of Always or Never.
Definition: InlineCost.h:121
Optional< bool > ComputeFullInlineCost
Compute inline cost even when the cost has exceeded the threshold.
Definition: InlineCost.h:180
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
InlineParams getInlineParams()
Generate the parameters to tune the inline cost analysis based only on the commandline options...
Optional< int > ColdThreshold
Threshold to use for cold callees.
Definition: InlineCost.h:161
int getCallsiteCost(CallBase &Call, const DataLayout &DL)
Return the cost associated with a callsite, including parameter passing and the call/return instructi...
int getCost() const
Get the inline cost estimate.
Definition: InlineCost.h:109
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
int getCostDelta() const
Get the cost delta from the threshold for inlining.
Definition: InlineCost.h:130
static InlineCost getNever(const char *Reason)
Definition: InlineCost.h:94
const unsigned TotalAllocaSizeRecursiveCaller
Do not inline functions which allocate this many bytes on the stack when the caller is recursive...
Definition: InlineCost.h:50
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< int > ColdCallSiteThreshold
Threshold to use when the callsite is considered cold.
Definition: InlineCost.h:177
print Print MemDeps of function
InlineResult(const char *message=nullptr)
Definition: InlineCost.h:139
const int OptSizeThreshold
Use when optsize (-Os) is specified.
Definition: InlineCost.h:34
The optimization diagnostic interface.
Optional< int > HotCallSiteThreshold
Threshold to use when the callsite is considered hot.
Definition: InlineCost.h:170
int DefaultThreshold
The default threshold to start with for a callee.
Definition: InlineCost.h:155