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