LLVM  9.0.0svn
InlineCost.h
Go to the documentation of this file.
1 //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements heuristics for inlining decisions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_INLINECOST_H
15 #define LLVM_ANALYSIS_INLINECOST_H
16 
20 #include <cassert>
21 #include <climits>
22 
23 namespace llvm {
24 class AssumptionCacheTracker;
25 class BlockFrequencyInfo;
26 class CallSite;
27 class DataLayout;
28 class Function;
29 class ProfileSummaryInfo;
30 class TargetTransformInfo;
31 
32 namespace InlineConstants {
33 // Various thresholds used by inline cost analysis.
34 /// Use when optsize (-Os) is specified.
35 const int OptSizeThreshold = 50;
36 
37 /// Use when minsize (-Oz) is specified.
38 const int OptMinSizeThreshold = 5;
39 
40 /// Use when -O3 is specified.
41 const int OptAggressiveThreshold = 250;
42 
43 // Various magic constants used to adjust heuristics.
44 const int InstrCost = 5;
45 const int IndirectCallThreshold = 100;
46 const int CallPenalty = 25;
47 const int LastCallToStaticBonus = 15000;
48 const int ColdccPenalty = 2000;
49 /// Do not inline functions which allocate this many bytes on the stack
50 /// when the caller is recursive.
51 const unsigned TotalAllocaSizeRecursiveCaller = 1024;
52 }
53 
54 /// Represents the cost of inlining a function.
55 ///
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.
59 ///
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.
64 class InlineCost {
65  enum SentinelValues {
66  AlwaysInlineCost = INT_MIN,
67  NeverInlineCost = INT_MAX
68  };
69 
70  /// The estimated cost of inlining this callsite.
71  const int Cost;
72 
73  /// The adjusted threshold against which this cost was computed.
74  const int Threshold;
75 
76  /// Must be set for Always and Never instances.
77  const char *Reason = nullptr;
78 
79  // Trivial constructor, interesting logic in the factory functions below.
80  InlineCost(int Cost, int Threshold, const char *Reason = nullptr)
81  : Cost(Cost), Threshold(Threshold), Reason(Reason) {
82  assert((isVariable() || Reason) &&
83  "Reason must be provided for Never or Always");
84  }
85 
86 public:
87  static InlineCost get(int Cost, int Threshold) {
88  assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
89  assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
90  return InlineCost(Cost, Threshold);
91  }
92  static InlineCost getAlways(const char *Reason) {
93  return InlineCost(AlwaysInlineCost, 0, Reason);
94  }
95  static InlineCost getNever(const char *Reason) {
96  return InlineCost(NeverInlineCost, 0, Reason);
97  }
98 
99  /// Test whether the inline cost is low enough for inlining.
100  explicit operator bool() const {
101  return Cost < Threshold;
102  }
103 
104  bool isAlways() const { return Cost == AlwaysInlineCost; }
105  bool isNever() const { return Cost == NeverInlineCost; }
106  bool isVariable() const { return !isAlways() && !isNever(); }
107 
108  /// Get the inline cost estimate.
109  /// It is an error to call this on an "always" or "never" InlineCost.
110  int getCost() const {
111  assert(isVariable() && "Invalid access of InlineCost");
112  return Cost;
113  }
114 
115  /// Get the threshold against which the cost was computed
116  int getThreshold() const {
117  assert(isVariable() && "Invalid access of InlineCost");
118  return Threshold;
119  }
120 
121  /// Get the reason of Always or Never.
122  const char *getReason() const {
123  assert((Reason || isVariable()) &&
124  "InlineCost reason must be set for Always or Never");
125  return Reason;
126  }
127 
128  /// Get the cost delta from the threshold for inlining.
129  /// Only valid if the cost is of the variable kind. Returns a negative
130  /// value if the cost is too high to inline.
131  int getCostDelta() const { return Threshold - getCost(); }
132 };
133 
134 /// InlineResult is basically true or false. For false results the message
135 /// describes a reason why it is decided not to inline.
136 struct InlineResult {
137  const char *message = nullptr;
138  InlineResult(bool result, const char *message = nullptr)
139  : message(result ? nullptr : (message ? message : "cost > threshold")) {}
140  InlineResult(const char *message = nullptr) : message(message) {}
141  operator bool() const { return !message; }
142  operator const char *() const { return message; }
143 };
144 
145 /// Thresholds to tune inline cost analysis. The inline cost analysis decides
146 /// the condition to apply a threshold and applies it. Otherwise,
147 /// DefaultThreshold is used. If a threshold is Optional, it is applied only
148 /// when it has a valid value. Typically, users of inline cost analysis
149 /// obtain an InlineParams object through one of the \c getInlineParams methods
150 /// and pass it to \c getInlineCost. Some specialized versions of inliner
151 /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
152 /// object.
153 
154 struct InlineParams {
155  /// The default threshold to start with for a callee.
157 
158  /// Threshold to use for callees with inline hint.
160 
161  /// Threshold to use for cold callees.
163 
164  /// Threshold to use when the caller is optimized for size.
166 
167  /// Threshold to use when the caller is optimized for minsize.
169 
170  /// Threshold to use when the callsite is considered hot.
172 
173  /// Threshold to use when the callsite is considered hot relative to function
174  /// entry.
176 
177  /// Threshold to use when the callsite is considered cold.
179 
180  /// Compute inline cost even when the cost has exceeded the threshold.
182 };
183 
184 /// Generate the parameters to tune the inline cost analysis based only on the
185 /// commandline options.
187 
188 /// Generate the parameters to tune the inline cost analysis based on command
189 /// line options. If -inline-threshold option is not explicitly passed,
190 /// \p Threshold is used as the default threshold.
192 
193 /// Generate the parameters to tune the inline cost analysis based on command
194 /// line options. If -inline-threshold option is not explicitly passed,
195 /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
196 /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
197 /// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
198 /// the -Oz flag.
199 InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
200 
201 /// Return the cost associated with a callsite, including parameter passing
202 /// and the call/return instruction.
203 int getCallsiteCost(CallSite CS, const DataLayout &DL);
204 
205 /// Get an InlineCost object representing the cost of inlining this
206 /// callsite.
207 ///
208 /// Note that a default threshold is passed into this function. This threshold
209 /// could be modified based on callsite's properties and only costs below this
210 /// new threshold are computed with any accuracy. The new threshold can be
211 /// used to bound the computation necessary to determine whether the cost is
212 /// sufficiently low to warrant inlining.
213 ///
214 /// Also note that calling this function *dynamically* computes the cost of
215 /// inlining the callsite. It is an expensive, heavyweight call.
217  CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
218  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
220  ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
221 
222 /// Get an InlineCost with the callee explicitly specified.
223 /// This allows you to calculate the cost of inlining a function via a
224 /// pointer. This behaves exactly as the version with no explicit callee
225 /// parameter in all other respects.
226 //
228 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
229  TargetTransformInfo &CalleeTTI,
230  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
233 
234 /// Minimal filter to detect invalid constructs for inlining.
235 bool isInlineViable(Function &Callee);
236 }
237 
238 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:154
bool isNever() const
Definition: InlineCost.h:105
This class represents lattice values for constants.
Definition: AllocatorList.h:24
Optional< int > OptSizeThreshold
Threshold to use when the caller is optimized for size.
Definition: InlineCost.h:165
bool isVariable() const
Definition: InlineCost.h:106
Analysis providing profile information.
const int OptMinSizeThreshold
Use when minsize (-Oz) is specified.
Definition: InlineCost.h:38
const int ColdccPenalty
Definition: InlineCost.h:48
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:117
A cache of @llvm.assume calls within a function.
Represents the cost of inlining a function.
Definition: InlineCost.h:64
Optional< int > HintThreshold
Threshold to use for callees with inline hint.
Definition: InlineCost.h:159
bool isInlineViable(Function &Callee)
Minimal filter to detect invalid constructs for inlining.
const int LastCallToStaticBonus
Definition: InlineCost.h:47
static InlineCost getAlways(const char *Reason)
Definition: InlineCost.h:92
const int CallPenalty
Definition: InlineCost.h:46
InlineResult is basically true or false.
Definition: InlineCost.h:136
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool isAlways() const
Definition: InlineCost.h:104
const int IndirectCallThreshold
Definition: InlineCost.h:45
const int OptAggressiveThreshold
Use when -O3 is specified.
Definition: InlineCost.h:41
Optional< int > OptMinSizeThreshold
Threshold to use when the caller is optimized for minsize.
Definition: InlineCost.h:168
InlineResult(bool result, const char *message=nullptr)
Definition: InlineCost.h:138
Optional< int > LocallyHotCallSiteThreshold
Threshold to use when the callsite is considered hot relative to function entry.
Definition: InlineCost.h:175
int getThreshold() const
Get the threshold against which the cost was computed.
Definition: InlineCost.h:116
const char * getReason() const
Get the reason of Always or Never.
Definition: InlineCost.h:122
InlineCost getInlineCost(CallSite CS, 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.
Optional< bool > ComputeFullInlineCost
Compute inline cost even when the cost has exceeded the threshold.
Definition: InlineCost.h:181
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:162
int getCost() const
Get the inline cost estimate.
Definition: InlineCost.h:110
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:131
static InlineCost getNever(const char *Reason)
Definition: InlineCost.h:95
int getCallsiteCost(CallSite CS, const DataLayout &DL)
Return the cost associated with a callsite, including parameter passing and the call/return instructi...
const unsigned TotalAllocaSizeRecursiveCaller
Do not inline functions which allocate this many bytes on the stack when the caller is recursive...
Definition: InlineCost.h:51
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< int > ColdCallSiteThreshold
Threshold to use when the callsite is considered cold.
Definition: InlineCost.h:178
print Print MemDeps of function
InlineResult(const char *message=nullptr)
Definition: InlineCost.h:140
const int OptSizeThreshold
Use when optsize (-Os) is specified.
Definition: InlineCost.h:35
The optimization diagnostic interface.
Optional< int > HotCallSiteThreshold
Threshold to use when the callsite is considered hot.
Definition: InlineCost.h:171
int DefaultThreshold
The default threshold to start with for a callee.
Definition: InlineCost.h:156