]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/InlineCost.h
Merge llvm trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / InlineCost.h
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
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/Analysis/AssumptionCache.h"
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.
125   int DefaultThreshold;
126
127   /// Threshold to use for callees with inline hint.
128   Optional<int> HintThreshold;
129
130   /// Threshold to use for cold callees.
131   Optional<int> ColdThreshold;
132
133   /// Threshold to use when the caller is optimized for size.
134   Optional<int> OptSizeThreshold;
135
136   /// Threshold to use when the caller is optimized for minsize.
137   Optional<int> OptMinSizeThreshold;
138
139   /// Threshold to use when the callsite is considered hot.
140   Optional<int> HotCallSiteThreshold;
141
142   /// Threshold to use when the callsite is considered cold.
143   Optional<int> ColdCallSiteThreshold;
144 };
145
146 /// Generate the parameters to tune the inline cost analysis based only on the
147 /// commandline options.
148 InlineParams getInlineParams();
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.
153 InlineParams getInlineParams(int 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 /// \brief Get an InlineCost object representing the cost of inlining this
164 /// callsite.
165 ///
166 /// Note that a default threshold is passed into this function. This threshold
167 /// could be modified based on callsite's properties and only costs below this
168 /// new threshold are computed with any accuracy. The new threshold can be
169 /// used to bound the computation necessary to determine whether the cost is
170 /// sufficiently low to warrant inlining.
171 ///
172 /// Also note that calling this function *dynamically* computes the cost of
173 /// inlining the callsite. It is an expensive, heavyweight call.
174 InlineCost
175 getInlineCost(CallSite CS, const InlineParams &Params,
176               TargetTransformInfo &CalleeTTI,
177               std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
178               Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
179               ProfileSummaryInfo *PSI);
180
181 /// \brief Get an InlineCost with the callee explicitly specified.
182 /// This allows you to calculate the cost of inlining a function via a
183 /// pointer. This behaves exactly as the version with no explicit callee
184 /// parameter in all other respects.
185 //
186 InlineCost
187 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
188               TargetTransformInfo &CalleeTTI,
189               std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
190               Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
191               ProfileSummaryInfo *PSI);
192
193 /// \brief Minimal filter to detect invalid constructs for inlining.
194 bool isInlineViable(Function &Callee);
195 }
196
197 #endif