blob: e386093351695226a1a460dcbb522e65fbdc9ef4 [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;
25class CallSite;
26class DataLayout;
27class Function;
28class ProfileSummaryInfo;
29class TargetTransformInfo;
30
31namespace InlineConstants {
32// Various thresholds used by inline cost analysis.
33/// Use when optsize (-Os) is specified.
34const int OptSizeThreshold = 50;
35
36/// Use when minsize (-Oz) is specified.
37const int OptMinSizeThreshold = 5;
38
39/// Use when -O3 is specified.
40const int OptAggressiveThreshold = 250;
41
42// Various magic constants used to adjust heuristics.
43const int InstrCost = 5;
44const int IndirectCallThreshold = 100;
45const int CallPenalty = 25;
46const int LastCallToStaticBonus = 15000;
47const int ColdccPenalty = 2000;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048/// Do not inline functions which allocate this many bytes on the stack
49/// when the caller is recursive.
50const unsigned TotalAllocaSizeRecursiveCaller = 1024;
51}
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053/// Represents the cost of inlining a function.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054///
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.
63class InlineCost {
64 enum SentinelValues {
65 AlwaysInlineCost = INT_MIN,
66 NeverInlineCost = INT_MAX
67 };
68
Andrew Scullcdfcccc2018-10-05 20:58:37 +010069 /// The estimated cost of inlining this callsite.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070 const int Cost;
71
Andrew Scullcdfcccc2018-10-05 20:58:37 +010072 /// The adjusted threshold against which this cost was computed.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073 const int Threshold;
74
Andrew Scullcdfcccc2018-10-05 20:58:37 +010075 /// Must be set for Always and Never instances.
76 const char *Reason = nullptr;
77
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078 // Trivial constructor, interesting logic in the factory functions below.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010079 InlineCost(int Cost, int Threshold, const char *Reason = nullptr)
80 : Cost(Cost), Threshold(Threshold), Reason(Reason) {
81 assert((isVariable() || Reason) &&
82 "Reason must be provided for Never or Always");
83 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010084
85public:
86 static InlineCost get(int Cost, int Threshold) {
87 assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
88 assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
89 return InlineCost(Cost, Threshold);
90 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010091 static InlineCost getAlways(const char *Reason) {
92 return InlineCost(AlwaysInlineCost, 0, Reason);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +010094 static InlineCost getNever(const char *Reason) {
95 return InlineCost(NeverInlineCost, 0, Reason);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 }
97
Andrew Scullcdfcccc2018-10-05 20:58:37 +010098 /// Test whether the inline cost is low enough for inlining.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099 explicit operator bool() const {
100 return Cost < Threshold;
101 }
102
103 bool isAlways() const { return Cost == AlwaysInlineCost; }
104 bool isNever() const { return Cost == NeverInlineCost; }
105 bool isVariable() const { return !isAlways() && !isNever(); }
106
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107 /// Get the inline cost estimate.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108 /// It is an error to call this on an "always" or "never" InlineCost.
109 int getCost() const {
110 assert(isVariable() && "Invalid access of InlineCost");
111 return Cost;
112 }
113
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100114 /// Get the threshold against which the cost was computed
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100115 int getThreshold() const {
116 assert(isVariable() && "Invalid access of InlineCost");
117 return Threshold;
118 }
119
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100120 /// Get the reason of Always or Never.
121 const char *getReason() const {
122 assert((Reason || isVariable()) &&
123 "InlineCost reason must be set for Always or Never");
124 return Reason;
125 }
126
127 /// Get the cost delta from the threshold for inlining.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 /// Only valid if the cost is of the variable kind. Returns a negative
129 /// value if the cost is too high to inline.
130 int getCostDelta() const { return Threshold - getCost(); }
131};
132
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100133/// InlineResult is basically true or false. For false results the message
134/// describes a reason why it is decided not to inline.
135struct InlineResult {
136 const char *message = nullptr;
137 InlineResult(bool result, const char *message = nullptr)
138 : message(result ? nullptr : (message ? message : "cost > threshold")) {}
139 InlineResult(const char *message = nullptr) : message(message) {}
140 operator bool() const { return !message; }
141 operator const char *() const { return message; }
142};
143
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100144/// Thresholds to tune inline cost analysis. The inline cost analysis decides
145/// the condition to apply a threshold and applies it. Otherwise,
146/// DefaultThreshold is used. If a threshold is Optional, it is applied only
147/// when it has a valid value. Typically, users of inline cost analysis
148/// obtain an InlineParams object through one of the \c getInlineParams methods
149/// and pass it to \c getInlineCost. Some specialized versions of inliner
150/// (such as the pre-inliner) might have custom logic to compute \c InlineParams
151/// object.
152
153struct InlineParams {
154 /// The default threshold to start with for a callee.
155 int DefaultThreshold;
156
157 /// Threshold to use for callees with inline hint.
158 Optional<int> HintThreshold;
159
160 /// Threshold to use for cold callees.
161 Optional<int> ColdThreshold;
162
163 /// Threshold to use when the caller is optimized for size.
164 Optional<int> OptSizeThreshold;
165
166 /// Threshold to use when the caller is optimized for minsize.
167 Optional<int> OptMinSizeThreshold;
168
169 /// Threshold to use when the callsite is considered hot.
170 Optional<int> HotCallSiteThreshold;
171
172 /// Threshold to use when the callsite is considered hot relative to function
173 /// entry.
174 Optional<int> LocallyHotCallSiteThreshold;
175
176 /// Threshold to use when the callsite is considered cold.
177 Optional<int> ColdCallSiteThreshold;
178
179 /// Compute inline cost even when the cost has exceeded the threshold.
180 Optional<bool> ComputeFullInlineCost;
181};
182
183/// Generate the parameters to tune the inline cost analysis based only on the
184/// commandline options.
185InlineParams getInlineParams();
186
187/// Generate the parameters to tune the inline cost analysis based on command
188/// line options. If -inline-threshold option is not explicitly passed,
189/// \p Threshold is used as the default threshold.
190InlineParams getInlineParams(int Threshold);
191
192/// Generate the parameters to tune the inline cost analysis based on command
193/// line options. If -inline-threshold option is not explicitly passed,
194/// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
195/// An \p OptLevel value above 3 is considered an aggressive optimization mode.
196/// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
197/// the -Oz flag.
198InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
199
200/// Return the cost associated with a callsite, including parameter passing
201/// and the call/return instruction.
202int getCallsiteCost(CallSite CS, const DataLayout &DL);
203
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100204/// Get an InlineCost object representing the cost of inlining this
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100205/// callsite.
206///
207/// Note that a default threshold is passed into this function. This threshold
208/// could be modified based on callsite's properties and only costs below this
209/// new threshold are computed with any accuracy. The new threshold can be
210/// used to bound the computation necessary to determine whether the cost is
211/// sufficiently low to warrant inlining.
212///
213/// Also note that calling this function *dynamically* computes the cost of
214/// inlining the callsite. It is an expensive, heavyweight call.
215InlineCost getInlineCost(
216 CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
217 std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
218 Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
219 ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE = nullptr);
220
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100221/// Get an InlineCost with the callee explicitly specified.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100222/// This allows you to calculate the cost of inlining a function via a
223/// pointer. This behaves exactly as the version with no explicit callee
224/// parameter in all other respects.
225//
226InlineCost
227getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
228 TargetTransformInfo &CalleeTTI,
229 std::function<AssumptionCache &(Function &)> &GetAssumptionCache,
230 Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI,
231 ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE);
232
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100233/// Minimal filter to detect invalid constructs for inlining.
Andrew Walbran16937d02019-10-22 13:54:20 +0100234InlineResult isInlineViable(Function &Callee);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100235}
236
237#endif