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