blob: 7f04a8ce8f5fa735e64b3537f2cae5b97a10f2bb [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements heuristics for inlining decisions.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_INLINECOST_H
14#define LLVM_ANALYSIS_INLINECOST_H
15
16#include "llvm/Analysis/AssumptionCache.h"
17#include "llvm/Analysis/CallGraphSCCPass.h"
18#include "llvm/Analysis/OptimizationRemarkEmitter.h"
19#include <cassert>
20#include <climits>
21
22namespace llvm {
23class AssumptionCacheTracker;
24class BlockFrequencyInfo;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010025class CallBase;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010026class DataLayout;
27class Function;
28class ProfileSummaryInfo;
29class TargetTransformInfo;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020030class TargetLibraryInfo;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031
32namespace InlineConstants {
33// Various thresholds used by inline cost analysis.
34/// Use when optsize (-Os) is specified.
35const int OptSizeThreshold = 50;
36
37/// Use when minsize (-Oz) is specified.
38const int OptMinSizeThreshold = 5;
39
40/// Use when -O3 is specified.
41const int OptAggressiveThreshold = 250;
42
43// Various magic constants used to adjust heuristics.
44const int InstrCost = 5;
45const int IndirectCallThreshold = 100;
46const int CallPenalty = 25;
47const int LastCallToStaticBonus = 15000;
48const int ColdccPenalty = 2000;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010049/// Do not inline functions which allocate this many bytes on the stack
50/// when the caller is recursive.
51const unsigned TotalAllocaSizeRecursiveCaller = 1024;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020052/// Do not inline dynamic allocas that have been constant propagated to be
53/// static allocas above this amount in bytes.
54const uint64_t MaxSimplifiedDynamicAllocaToInline = 65536;
55} // namespace InlineConstants
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056
Andrew Scullcdfcccc2018-10-05 20:58:37 +010057/// Represents the cost of inlining a function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058///
59/// This supports special values for functions which should "always" or
60/// "never" be inlined. Otherwise, the cost represents a unitless amount;
61/// smaller values increase the likelihood of the function being inlined.
62///
63/// Objects of this type also provide the adjusted threshold for inlining
64/// based on the information available for a particular callsite. They can be
65/// directly tested to determine if inlining should occur given the cost and
66/// threshold for this cost metric.
67class InlineCost {
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020068 enum SentinelValues { AlwaysInlineCost = INT_MIN, NeverInlineCost = INT_MAX };
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069
Andrew Scullcdfcccc2018-10-05 20:58:37 +010070 /// The estimated cost of inlining this callsite.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020071 int Cost = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010072
Andrew Scullcdfcccc2018-10-05 20:58:37 +010073 /// The adjusted threshold against which this cost was computed.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020074 int Threshold = 0;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075
Andrew Scullcdfcccc2018-10-05 20:58:37 +010076 /// Must be set for Always and Never instances.
77 const char *Reason = nullptr;
78
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079 // Trivial constructor, interesting logic in the factory functions below.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080 InlineCost(int Cost, int Threshold, const char *Reason = nullptr)
81 : Cost(Cost), Threshold(Threshold), Reason(Reason) {
82 assert((isVariable() || Reason) &&
83 "Reason must be provided for Never or Always");
84 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085
86public:
87 static InlineCost get(int Cost, int Threshold) {
88 assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
89 assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
90 return InlineCost(Cost, Threshold);
91 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092 static InlineCost getAlways(const char *Reason) {
93 return InlineCost(AlwaysInlineCost, 0, Reason);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010094 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 static InlineCost getNever(const char *Reason) {
96 return InlineCost(NeverInlineCost, 0, Reason);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 }
98
Andrew Scullcdfcccc2018-10-05 20:58:37 +010099 /// Test whether the inline cost is low enough for inlining.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200100 explicit operator bool() const { return Cost < Threshold; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100101
102 bool isAlways() const { return Cost == AlwaysInlineCost; }
103 bool isNever() const { return Cost == NeverInlineCost; }
104 bool isVariable() const { return !isAlways() && !isNever(); }
105
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100106 /// Get the inline cost estimate.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107 /// It is an error to call this on an "always" or "never" InlineCost.
108 int getCost() const {
109 assert(isVariable() && "Invalid access of InlineCost");
110 return Cost;
111 }
112
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100113 /// Get the threshold against which the cost was computed
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100114 int getThreshold() const {
115 assert(isVariable() && "Invalid access of InlineCost");
116 return Threshold;
117 }
118
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100119 /// Get the reason of Always or Never.
120 const char *getReason() const {
121 assert((Reason || isVariable()) &&
122 "InlineCost reason must be set for Always or Never");
123 return Reason;
124 }
125
126 /// Get the cost delta from the threshold for inlining.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100127 /// Only valid if the cost is of the variable kind. Returns a negative
128 /// value if the cost is too high to inline.
129 int getCostDelta() const { return Threshold - getCost(); }
130};
131
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100132/// InlineResult is basically true or false. For false results the message
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200133/// describes a reason.
134class InlineResult {
135 const char *Message = nullptr;
136 InlineResult(const char *Message = nullptr) : Message(Message) {}
137
138public:
139 static InlineResult success() { return {}; }
140 static InlineResult failure(const char *Reason) {
141 return InlineResult(Reason);
142 }
143 bool isSuccess() const { return Message == nullptr; }
144 const char *getFailureReason() const {
145 assert(!isSuccess() &&
146 "getFailureReason should only be called in failure cases");
147 return Message;
148 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100149};
150
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151/// Thresholds to tune inline cost analysis. The inline cost analysis decides
152/// the condition to apply a threshold and applies it. Otherwise,
153/// DefaultThreshold is used. If a threshold is Optional, it is applied only
154/// when it has a valid value. Typically, users of inline cost analysis
155/// obtain an InlineParams object through one of the \c getInlineParams methods
156/// and pass it to \c getInlineCost. Some specialized versions of inliner
157/// (such as the pre-inliner) might have custom logic to compute \c InlineParams
158/// object.
159
160struct InlineParams {
161 /// The default threshold to start with for a callee.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200162 int DefaultThreshold = -1;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100163
164 /// Threshold to use for callees with inline hint.
165 Optional<int> HintThreshold;
166
167 /// Threshold to use for cold callees.
168 Optional<int> ColdThreshold;
169
170 /// Threshold to use when the caller is optimized for size.
171 Optional<int> OptSizeThreshold;
172
173 /// Threshold to use when the caller is optimized for minsize.
174 Optional<int> OptMinSizeThreshold;
175
176 /// Threshold to use when the callsite is considered hot.
177 Optional<int> HotCallSiteThreshold;
178
179 /// Threshold to use when the callsite is considered hot relative to function
180 /// entry.
181 Optional<int> LocallyHotCallSiteThreshold;
182
183 /// Threshold to use when the callsite is considered cold.
184 Optional<int> ColdCallSiteThreshold;
185
186 /// Compute inline cost even when the cost has exceeded the threshold.
187 Optional<bool> ComputeFullInlineCost;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200188
189 /// Indicate whether we should allow inline deferral.
190 Optional<bool> EnableDeferral = true;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100191};
192
193/// Generate the parameters to tune the inline cost analysis based only on the
194/// commandline options.
195InlineParams getInlineParams();
196
197/// Generate the parameters to tune the inline cost analysis based on command
198/// line options. If -inline-threshold option is not explicitly passed,
199/// \p Threshold is used as the default threshold.
200InlineParams getInlineParams(int Threshold);
201
202/// Generate the parameters to tune the inline cost analysis based on command
203/// line options. If -inline-threshold option is not explicitly passed,
204/// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
205/// An \p OptLevel value above 3 is considered an aggressive optimization mode.
206/// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
207/// the -Oz flag.
208InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
209
210/// Return the cost associated with a callsite, including parameter passing
211/// and the call/return instruction.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100212int getCallsiteCost(CallBase &Call, const DataLayout &DL);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100213
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100214/// Get an InlineCost object representing the cost of inlining this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100215/// callsite.
216///
217/// Note that a default threshold is passed into this function. This threshold
218/// could be modified based on callsite's properties and only costs below this
219/// new threshold are computed with any accuracy. The new threshold can be
220/// used to bound the computation necessary to determine whether the cost is
221/// sufficiently low to warrant inlining.
222///
223/// Also note that calling this function *dynamically* computes the cost of
224/// inlining the callsite. It is an expensive, heavyweight call.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200225InlineCost
226getInlineCost(CallBase &Call, const InlineParams &Params,
227 TargetTransformInfo &CalleeTTI,
228 function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
229 function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
230 function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
231 ProfileSummaryInfo *PSI = nullptr,
232 OptimizationRemarkEmitter *ORE = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100233
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100234/// Get an InlineCost with the callee explicitly specified.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100235/// This allows you to calculate the cost of inlining a function via a
236/// pointer. This behaves exactly as the version with no explicit callee
237/// parameter in all other respects.
238//
239InlineCost
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100240getInlineCost(CallBase &Call, Function *Callee, const InlineParams &Params,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100241 TargetTransformInfo &CalleeTTI,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200242 function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
243 function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
244 function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
245 ProfileSummaryInfo *PSI = nullptr,
246 OptimizationRemarkEmitter *ORE = nullptr);
247
248/// Returns InlineResult::success() if the call site should be always inlined
249/// because of user directives, and the inlining is viable. Returns
250/// InlineResult::failure() if the inlining may never happen because of user
251/// directives or incompatibilities detectable without needing callee traversal.
252/// Otherwise returns None, meaning that inlining should be decided based on
253/// other criteria (e.g. cost modeling).
254Optional<InlineResult> getAttributeBasedInliningDecision(
255 CallBase &Call, Function *Callee, TargetTransformInfo &CalleeTTI,
256 function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
257
258/// Get the cost estimate ignoring thresholds. This is similar to getInlineCost
259/// when passed InlineParams::ComputeFullInlineCost, or a non-null ORE. It
260/// uses default InlineParams otherwise.
261/// Contrary to getInlineCost, which makes a threshold-based final evaluation of
262/// should/shouldn't inline, captured in InlineResult, getInliningCostEstimate
263/// returns:
264/// - None, if the inlining cannot happen (is illegal)
265/// - an integer, representing the cost.
266Optional<int> getInliningCostEstimate(
267 CallBase &Call, TargetTransformInfo &CalleeTTI,
268 function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
269 function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
270 ProfileSummaryInfo *PSI = nullptr,
271 OptimizationRemarkEmitter *ORE = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100272
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100273/// Minimal filter to detect invalid constructs for inlining.
Andrew Walbran16937d02019-10-22 13:54:20 +0100274InlineResult isInlineViable(Function &Callee);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200275
276// This pass is used to annotate instructions during the inline process for
277// debugging and analysis. The main purpose of the pass is to see and test
278// inliner's decisions when creating new optimizations to InlineCost.
279struct InlineCostAnnotationPrinterPass
280 : PassInfoMixin<InlineCostAnnotationPrinterPass> {
281 raw_ostream &OS;
282
283public:
284 explicit InlineCostAnnotationPrinterPass(raw_ostream &OS) : OS(OS) {}
285 PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
286};
287} // namespace llvm
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100288
289#endif