LLVM  5.0.0
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 
19 #include <cassert>
20 #include <climits>
21 
22 namespace llvm {
23 class AssumptionCacheTracker;
24 class BlockFrequencyInfo;
25 class CallSite;
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 const int NoreturnPenalty = 10000;
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 /// \brief 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  /// \brief The estimated cost of inlining this callsite.
71  const int Cost;
72 
73  /// \brief The adjusted threshold against which this cost was computed.
74  const int Threshold;
75 
76  // Trivial constructor, interesting logic in the factory functions below.
77  InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
78 
79 public:
80  static InlineCost get(int Cost, int Threshold) {
81  assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
82  assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
83  return InlineCost(Cost, Threshold);
84  }
85  static InlineCost getAlways() {
86  return InlineCost(AlwaysInlineCost, 0);
87  }
88  static InlineCost getNever() {
89  return InlineCost(NeverInlineCost, 0);
90  }
91 
92  /// \brief Test whether the inline cost is low enough for inlining.
93  explicit operator bool() const {
94  return Cost < Threshold;
95  }
96 
97  bool isAlways() const { return Cost == AlwaysInlineCost; }
98  bool isNever() const { return Cost == NeverInlineCost; }
99  bool isVariable() const { return !isAlways() && !isNever(); }
100 
101  /// \brief Get the inline cost estimate.
102  /// It is an error to call this on an "always" or "never" InlineCost.
103  int getCost() const {
104  assert(isVariable() && "Invalid access of InlineCost");
105  return Cost;
106  }
107 
108  /// \brief Get the cost delta from the threshold for inlining.
109  /// Only valid if the cost is of the variable kind. Returns a negative
110  /// value if the cost is too high to inline.
111  int getCostDelta() const { return Threshold - getCost(); }
112 };
113 
114 /// Thresholds to tune inline cost analysis. The inline cost analysis decides
115 /// the condition to apply a threshold and applies it. Otherwise,
116 /// DefaultThreshold is used. If a threshold is Optional, it is applied only
117 /// when it has a valid value. Typically, users of inline cost analysis
118 /// obtain an InlineParams object through one of the \c getInlineParams methods
119 /// and pass it to \c getInlineCost. Some specialized versions of inliner
120 /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
121 /// object.
122 
123 struct InlineParams {
124  /// The default threshold to start with for a callee.
126 
127  /// Threshold to use for callees with inline hint.
129 
130  /// Threshold to use for cold callees.
132 
133  /// Threshold to use when the caller is optimized for size.
135 
136  /// Threshold to use when the caller is optimized for minsize.
138 
139  /// Threshold to use when the callsite is considered hot.
141 
142  /// Threshold to use when the callsite is considered cold.
144 };
145 
146 /// Generate the parameters to tune the inline cost analysis based only on the
147 /// commandline options.
149 
150 /// Generate the parameters to tune the inline cost analysis based on command
151 /// line options. If -inline-threshold option is not explicitly passed,
152 /// \p Threshold is used as the default threshold.
154 
155 /// Generate the parameters to tune the inline cost analysis based on command
156 /// line options. If -inline-threshold option is not explicitly passed,
157 /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
158 /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
159 /// \p SizeOptLevel of 1 corresponds to the the -Os flag and 2 corresponds to
160 /// the -Oz flag.
161 InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
162 
163 /// Return the cost associated with a callsite, including parameter passing
164 /// and the call/return instruction.
165 int getCallsiteCost(CallSite CS, const DataLayout &DL);
166 
167 /// \brief Get an InlineCost object representing the cost of inlining this
168 /// callsite.
169 ///
170 /// Note that a default threshold is passed into this function. This threshold
171 /// could be modified based on callsite's properties and only costs below this
172 /// new threshold are computed with any accuracy. The new threshold can be
173 /// used to bound the computation necessary to determine whether the cost is
174 /// sufficiently low to warrant inlining.
175 ///
176 /// Also note that calling this function *dynamically* computes the cost of
177 /// inlining the callsite. It is an expensive, heavyweight call.
179 getInlineCost(CallSite CS, const InlineParams &Params,
180  TargetTransformInfo &CalleeTTI,
181  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
183  ProfileSummaryInfo *PSI);
184 
185 /// \brief Get an InlineCost with the callee explicitly specified.
186 /// This allows you to calculate the cost of inlining a function via a
187 /// pointer. This behaves exactly as the version with no explicit callee
188 /// parameter in all other respects.
189 //
191 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
192  TargetTransformInfo &CalleeTTI,
193  std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
195  ProfileSummaryInfo *PSI);
196 
197 /// \brief Minimal filter to detect invalid constructs for inlining.
198 bool isInlineViable(Function &Callee);
199 }
200 
201 #endif
bool isAlways() const
Definition: InlineCost.h:97
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
Thresholds to tune inline cost analysis.
Definition: InlineCost.h:123
Optional< int > OptSizeThreshold
Threshold to use when the caller is optimized for size.
Definition: InlineCost.h:134
Analysis providing profile information.
DataLayout DL
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:89
A cache of .assume calls within a function.
Represents the cost of inlining a function.
Definition: InlineCost.h:64
int getCost() const
Get the inline cost estimate.
Definition: InlineCost.h:103
Optional< int > HintThreshold
Threshold to use for callees with inline hint.
Definition: InlineCost.h:128
bool isInlineViable(Function &Callee)
Minimal filter to detect invalid constructs for inlining.
const int LastCallToStaticBonus
Definition: InlineCost.h:46
int getCostDelta() const
Get the cost delta from the threshold for inlining.
Definition: InlineCost.h:111
const int CallPenalty
Definition: InlineCost.h:45
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
bool isVariable() const
Definition: InlineCost.h:99
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:137
static InlineCost getNever()
Definition: InlineCost.h:88
InlineCost getInlineCost(CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI, std::function< AssumptionCache &(Function &)> &GetAssumptionCache, Optional< function_ref< BlockFrequencyInfo &(Function &)>> GetBFI, ProfileSummaryInfo *PSI)
Get an InlineCost object representing the cost of inlining this callsite.
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:131
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:51
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< int > ColdCallSiteThreshold
Threshold to use when the callsite is considered cold.
Definition: InlineCost.h:143
print Print MemDeps of function
bool isNever() const
Definition: InlineCost.h:98
const int OptSizeThreshold
Use when optsize (-Os) is specified.
Definition: InlineCost.h:34
static InlineCost getAlways()
Definition: InlineCost.h:85
const int NoreturnPenalty
Definition: InlineCost.h:48
Optional< int > HotCallSiteThreshold
Threshold to use when the callsite is considered hot.
Definition: InlineCost.h:140
int DefaultThreshold
The default threshold to start with for a callee.
Definition: InlineCost.h:125