Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 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 | |
| 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; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 53 | /// Represents the cost of inlining a function. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 69 | /// The estimated cost of inlining this callsite. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 70 | const int Cost; |
| 71 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 72 | /// The adjusted threshold against which this cost was computed. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 73 | const int Threshold; |
| 74 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 75 | /// Must be set for Always and Never instances. |
| 76 | const char *Reason = nullptr; |
| 77 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 78 | // Trivial constructor, interesting logic in the factory functions below. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 79 | 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 84 | |
| 85 | public: |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 91 | static InlineCost getAlways(const char *Reason) { |
| 92 | return InlineCost(AlwaysInlineCost, 0, Reason); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 93 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 94 | static InlineCost getNever(const char *Reason) { |
| 95 | return InlineCost(NeverInlineCost, 0, Reason); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 98 | /// Test whether the inline cost is low enough for inlining. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 99 | 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 107 | /// Get the inline cost estimate. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 108 | /// 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 114 | /// Get the threshold against which the cost was computed |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 115 | int getThreshold() const { |
| 116 | assert(isVariable() && "Invalid access of InlineCost"); |
| 117 | return Threshold; |
| 118 | } |
| 119 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 120 | /// 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 128 | /// 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 133 | /// InlineResult is basically true or false. For false results the message |
| 134 | /// describes a reason why it is decided not to inline. |
| 135 | struct 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 Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 144 | /// 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 | |
| 153 | struct 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. |
| 185 | InlineParams 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. |
| 190 | InlineParams 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. |
| 198 | InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel); |
| 199 | |
| 200 | /// Return the cost associated with a callsite, including parameter passing |
| 201 | /// and the call/return instruction. |
| 202 | int getCallsiteCost(CallSite CS, const DataLayout &DL); |
| 203 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 204 | /// Get an InlineCost object representing the cost of inlining this |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 205 | /// 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. |
| 215 | InlineCost 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 221 | /// Get an InlineCost with the callee explicitly specified. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 222 | /// 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 | // |
| 226 | InlineCost |
| 227 | getInlineCost(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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 233 | /// Minimal filter to detect invalid constructs for inlining. |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 234 | InlineResult isInlineViable(Function &Callee); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | #endif |