[DAGCombiner] Expand combining of FP logical ops to sign-setting FP ops
[llvm-core.git] / include / llvm / Analysis / InlineCost.h
blob529fb75bec9bc45d7751af49ff6ac9d2f1fd3586
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_INLINECOST_H
15 #define LLVM_ANALYSIS_INLINECOST_H
17 #include "llvm/Analysis/AssumptionCache.h"
18 #include "llvm/Analysis/CallGraphSCCPass.h"
19 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
20 #include <cassert>
21 #include <climits>
23 namespace llvm {
24 class AssumptionCacheTracker;
25 class BlockFrequencyInfo;
26 class CallSite;
27 class DataLayout;
28 class Function;
29 class ProfileSummaryInfo;
30 class TargetTransformInfo;
32 namespace InlineConstants {
33 // Various thresholds used by inline cost analysis.
34 /// Use when optsize (-Os) is specified.
35 const int OptSizeThreshold = 50;
37 /// Use when minsize (-Oz) is specified.
38 const int OptMinSizeThreshold = 5;
40 /// Use when -O3 is specified.
41 const int OptAggressiveThreshold = 250;
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;
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
71 /// The estimated cost of inlining this callsite.
72 const int Cost;
74 /// The adjusted threshold against which this cost was computed.
75 const int Threshold;
77 /// Must be set for Always and Never instances.
78 const char *Reason = nullptr;
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");
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);
93 static InlineCost getAlways(const char *Reason) {
94 return InlineCost(AlwaysInlineCost, 0, Reason);
96 static InlineCost getNever(const char *Reason) {
97 return InlineCost(NeverInlineCost, 0, Reason);
100 /// Test whether the inline cost is low enough for inlining.
101 explicit operator bool() const {
102 return Cost < Threshold;
105 bool isAlways() const { return Cost == AlwaysInlineCost; }
106 bool isNever() const { return Cost == NeverInlineCost; }
107 bool isVariable() const { return !isAlways() && !isNever(); }
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;
116 /// Get the threshold against which the cost was computed
117 int getThreshold() const {
118 assert(isVariable() && "Invalid access of InlineCost");
119 return Threshold;
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;
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(); }
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; }
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.
155 struct InlineParams {
156 /// The default threshold to start with for a callee.
157 int DefaultThreshold;
159 /// Threshold to use for callees with inline hint.
160 Optional<int> HintThreshold;
162 /// Threshold to use for cold callees.
163 Optional<int> ColdThreshold;
165 /// Threshold to use when the caller is optimized for size.
166 Optional<int> OptSizeThreshold;
168 /// Threshold to use when the caller is optimized for minsize.
169 Optional<int> OptMinSizeThreshold;
171 /// Threshold to use when the callsite is considered hot.
172 Optional<int> HotCallSiteThreshold;
174 /// Threshold to use when the callsite is considered hot relative to function
175 /// entry.
176 Optional<int> LocallyHotCallSiteThreshold;
178 /// Threshold to use when the callsite is considered cold.
179 Optional<int> ColdCallSiteThreshold;
181 /// Compute inline cost even when the cost has exceeded the threshold.
182 Optional<bool> ComputeFullInlineCost;
185 /// Generate the parameters to tune the inline cost analysis based only on the
186 /// commandline options.
187 InlineParams getInlineParams();
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.
192 InlineParams getInlineParams(int Threshold);
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);
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);
206 /// Get an InlineCost object representing the cost of inlining this
207 /// callsite.
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.
215 /// Also note that calling this function *dynamically* computes the cost of
216 /// inlining the callsite. It is an expensive, heavyweight call.
217 InlineCost getInlineCost(
218 CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
219 std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
220 Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
221 ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
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.
228 InlineCost
229 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
230 TargetTransformInfo &CalleeTTI,
231 std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
232 Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
233 ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);
235 /// Minimal filter to detect invalid constructs for inlining.
236 bool isInlineViable(Function &Callee);
239 #endif