LLVM  8.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 const int NoreturnPenalty = 10000;
50 /// Do not inline functions which allocate this many bytes on the stack
51 /// when the caller is recursive.
52 const unsigned TotalAllocaSizeRecursiveCaller = 1024;
53 }
54 
55 /// Represents the cost of inlining a function.
56 ///
57 /// This supports special values for functions which should "always" or
58 /// "never" be inlined. Otherwise, the cost represents a unitless amount;
59 /// smaller values increase the likelihood of the function being inlined.
60 ///
61 /// Objects of this type also provide the adjusted threshold for inlining
62 /// based on the information available for a particular callsite. They can be
63 /// directly tested to determine if inlining should occur given the cost and
64 /// threshold for this cost metric.
65 class InlineCost {
66  enum SentinelValues {
67  AlwaysInlineCost = INT_MIN,
68  NeverInlineCost = INT_MAX
69  };
70 
71  /// The estimated cost of inlining this callsite.
72  const int Cost;
73 
74  /// The adjusted threshold against which this cost was computed.
75  const int Threshold;
76 
77  /// Must be set for Always and Never instances.
78  const char *Reason = nullptr;
79 
80  // Trivial constructor, interesting logic in the factory functions below.
81  InlineCost(int Cost, int Threshold, const char *Reason = nullptr)
82  : Cost(Cost), Threshold(Threshold), Reason(Reason) {
83  assert((isVariable() || Reason) &&
84  "Reason must be provided for Never or Always");
85  }
86 
87 public:
88  static InlineCost get(int Cost, int Threshold) {
89  assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
90  assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
91  return InlineCost(Cost, Threshold);
92  }
93  static InlineCost getAlways(const char *Reason) {
94  return InlineCost(AlwaysInlineCost, 0, Reason);
95  }
96  static InlineCost getNever(const char *Reason) {
97  return InlineCost(NeverInlineCost, 0, Reason);
98  }
99 
100  /// Test whether the inline cost is low enough for inlining.
101  explicit operator bool() const {
102  return Cost < Threshold;
103  }
104 
105  bool isAlways() const { return Cost == AlwaysInlineCost; }
106  bool isNever() const { return Cost == NeverInlineCost; }
107  bool isVariable() const { return !isAlways() && !isNever(); }
108 
109  /// Get the inline cost estimate.
110  /// It is an error to call this on an "always" or "never" InlineCost.
111  int getCost() const {
112  assert(isVariable() && "Invalid access of InlineCost");
113  return Cost;
114  }
115 
116  /// Get the threshold against which the cost was computed
117  int getThreshold() const {
118  assert(isVariable() && "Invalid access of InlineCost");
119  return Threshold;
120  }
121 
122  /// Get the reason of Always or Never.
123  const char *getReason() const {
124  assert((Reason || isVariable()) &&
125  "InlineCost reason must be set for Always or Never");
126  return Reason;
127  }
128 
129  /// Get the cost delta from the threshold for inlining.
130  /// Only valid if the cost is of the variable kind. Returns a negative
131  /// value if the cost is too high to inline.
132  int getCostDelta() const { return Threshold - getCost(); }
133 };
134 
135 /// InlineResult is basically true or false. For false results the message
136 /// describes a reason why it is decided not to inline.
137 struct InlineResult {
138  const char *message = nullptr;
139  InlineResult(bool result, const char *message = nullptr)
140  : message(result ? nullptr : (message ? message : "cost > threshold")) {}
141  InlineResult(const char *message = nullptr) : message(message) {}
142  operator bool() const { return !message; }
143  operator const char *() const { return message; }
144 };
145 
146 /// Thresholds to tune inline cost analysis. The inline cost analysis decides
147 /// the condition to apply a threshold and applies it. Otherwise,
148 /// DefaultThreshold is used. If a threshold is Optional, it is applied only
149 /// when it has a valid value. Typically, users of inline cost analysis
150 /// obtain an InlineParams object through one of the \c getInlineParams methods
151 /// and pass it to \c getInlineCost. Some specialized versions of inliner
152 /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
153 /// object.
154 
155 struct InlineParams {
156  /// The default threshold to start with for a callee.
158 
159  /// Threshold to use for callees with inline hint.
161 
162  /// Threshold to use for cold callees.
164 
165  /// Threshold to use when the caller is optimized for size.
167 
168  /// Threshold to use when the caller is optimized for minsize.
170 
171  /// Threshold to use when the callsite is considered hot.
173 
174  /// Threshold to use when the callsite is considered hot relative to function
175  /// entry.
177 
178  /// Threshold to use when the callsite is considered cold.
180 
181  /// Compute inline cost even when the cost has exceeded the threshold.
183 };
184 
185 /// Generate the parameters to tune the inline cost analysis based only on the
186 /// commandline options.
188 
189 /// Generate the parameters to tune the inline cost analysis based on command
190 /// line options. If -inline-threshold option is not explicitly passed,
191 /// \p Threshold is used as the default threshold.
193 
194 /// Generate the parameters to tune the inline cost analysis based on command
195 /// line options. If -inline-threshold option is not explicitly passed,
196 /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
197 /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
198 /// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
199 /// the -Oz flag.
200 InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
201 
202 /// Return the cost associated with a callsite, including parameter passing
203 /// and the call/return instruction.
204 int getCallsiteCost(CallSite CS, const DataLayout &DL);
205 
206 /// Get an InlineCost object representing the cost of inlining this
207 /// callsite.
208 ///
209 /// Note that a default threshold is passed into this function. This threshold
210 /// could be modified based on callsite's properties and only costs below this
211 /// new threshold are computed with any accuracy. The new threshold can be
212 /// used to bound the computation necessary to determine whether the cost is
213 /// sufficiently low to warrant inlining.
214 ///
215 /// Also note that calling this function *dynamically* computes the cost of
216 /// inlining the callsite. It is an expensive, heavyweight call.
218  CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
219  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
221  ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
222 
223 /// Get an InlineCost with the callee explicitly specified.
224 /// This allows you to calculate the cost of inlining a function via a
225 /// pointer. This behaves exactly as the version with no explicit callee
226 /// parameter in all other respects.
227 //
229 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
230  TargetTransformInfo &CalleeTTI,
231  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
234 
235 /// Minimal filter to detect invalid constructs for inlining.
236 bool isInlineViable(Function &Callee);
237 }
238 
239 #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:155
bool isNever() const
Definition: InlineCost.h:106
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Optional< int > OptSizeThreshold
Threshold to use when the caller is optimized for size.
Definition: InlineCost.h:166
bool isVariable() const
Definition: InlineCost.h:107
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:107
A cache of @llvm.assume calls within a function.
Represents the cost of inlining a function.
Definition: InlineCost.h:65
Optional< int > HintThreshold
Threshold to use for callees with inline hint.
Definition: InlineCost.h:160
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:93
const int CallPenalty
Definition: InlineCost.h:46
InlineResult is basically true or false.
Definition: InlineCost.h:137
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool isAlways() const
Definition: InlineCost.h:105
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:169
InlineResult(bool result, const char *message=nullptr)
Definition: InlineCost.h:139
Optional< int > LocallyHotCallSiteThreshold
Threshold to use when the callsite is considered hot relative to function entry.
Definition: InlineCost.h:176
int getThreshold() const
Get the threshold against which the cost was computed.
Definition: InlineCost.h:117
const char * getReason() const
Get the reason of Always or Never.
Definition: InlineCost.h:123
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:182
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:163
int getCost() const
Get the inline cost estimate.
Definition: InlineCost.h:111
int getCostDelta() const
Get the cost delta from the threshold for inlining.
Definition: InlineCost.h:132
static InlineCost getNever(const char *Reason)
Definition: InlineCost.h:96
int getCallsiteCost(CallSite CS, const DataLayout &DL)
Return the cost associated with a callsite, including parameter passing and the call/return instructi...
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
const unsigned TotalAllocaSizeRecursiveCaller
Do not inline functions which allocate this many bytes on the stack when the caller is recursive...
Definition: InlineCost.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< int > ColdCallSiteThreshold
Threshold to use when the callsite is considered cold.
Definition: InlineCost.h:179
print Print MemDeps of function
InlineResult(const char *message=nullptr)
Definition: InlineCost.h:141
const int OptSizeThreshold
Use when optsize (-Os) is specified.
Definition: InlineCost.h:35
The optimization diagnostic interface.
const int NoreturnPenalty
Definition: InlineCost.h:49
Optional< int > HotCallSiteThreshold
Threshold to use when the callsite is considered hot.
Definition: InlineCost.h:172
int DefaultThreshold
The default threshold to start with for a callee.
Definition: InlineCost.h:157