InlineCost.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- InlineCost.h - Cost analysis for inliner -----------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements heuristics for inlining decisions.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_INLINECOST_H
  18. #define LLVM_ANALYSIS_INLINECOST_H
  19. #include "llvm/Analysis/AssumptionCache.h"
  20. #include "llvm/Analysis/CallGraphSCCPass.h"
  21. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  22. #include <cassert>
  23. #include <climits>
  24. namespace llvm {
  25. class AssumptionCacheTracker;
  26. class BlockFrequencyInfo;
  27. class CallBase;
  28. class DataLayout;
  29. class Function;
  30. class ProfileSummaryInfo;
  31. class TargetTransformInfo;
  32. class TargetLibraryInfo;
  33. namespace InlineConstants {
  34. // Various thresholds used by inline cost analysis.
  35. /// Use when optsize (-Os) is specified.
  36. const int OptSizeThreshold = 50;
  37. /// Use when minsize (-Oz) is specified.
  38. const int OptMinSizeThreshold = 5;
  39. /// Use when -O3 is specified.
  40. const int OptAggressiveThreshold = 250;
  41. // Various magic constants used to adjust heuristics.
  42. const int InstrCost = 5;
  43. const int IndirectCallThreshold = 100;
  44. const int CallPenalty = 25;
  45. const int LastCallToStaticBonus = 15000;
  46. const int ColdccPenalty = 2000;
  47. /// Do not inline functions which allocate this many bytes on the stack
  48. /// when the caller is recursive.
  49. const unsigned TotalAllocaSizeRecursiveCaller = 1024;
  50. /// Do not inline dynamic allocas that have been constant propagated to be
  51. /// static allocas above this amount in bytes.
  52. const uint64_t MaxSimplifiedDynamicAllocaToInline = 65536;
  53. } // namespace InlineConstants
  54. /// 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 { AlwaysInlineCost = INT_MIN, NeverInlineCost = INT_MAX };
  66. /// The estimated cost of inlining this callsite.
  67. int Cost = 0;
  68. /// The adjusted threshold against which this cost was computed.
  69. int Threshold = 0;
  70. /// Must be set for Always and Never instances.
  71. const char *Reason = nullptr;
  72. // Trivial constructor, interesting logic in the factory functions below.
  73. InlineCost(int Cost, int Threshold, const char *Reason = nullptr)
  74. : Cost(Cost), Threshold(Threshold), Reason(Reason) {
  75. assert((isVariable() || Reason) &&
  76. "Reason must be provided for Never or Always");
  77. }
  78. public:
  79. static InlineCost get(int Cost, int Threshold) {
  80. assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
  81. assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
  82. return InlineCost(Cost, Threshold);
  83. }
  84. static InlineCost getAlways(const char *Reason) {
  85. return InlineCost(AlwaysInlineCost, 0, Reason);
  86. }
  87. static InlineCost getNever(const char *Reason) {
  88. return InlineCost(NeverInlineCost, 0, Reason);
  89. }
  90. /// Test whether the inline cost is low enough for inlining.
  91. explicit operator bool() const { return Cost < Threshold; }
  92. bool isAlways() const { return Cost == AlwaysInlineCost; }
  93. bool isNever() const { return Cost == NeverInlineCost; }
  94. bool isVariable() const { return !isAlways() && !isNever(); }
  95. /// Get the inline cost estimate.
  96. /// It is an error to call this on an "always" or "never" InlineCost.
  97. int getCost() const {
  98. assert(isVariable() && "Invalid access of InlineCost");
  99. return Cost;
  100. }
  101. /// Get the threshold against which the cost was computed
  102. int getThreshold() const {
  103. assert(isVariable() && "Invalid access of InlineCost");
  104. return Threshold;
  105. }
  106. /// Get the reason of Always or Never.
  107. const char *getReason() const {
  108. assert((Reason || isVariable()) &&
  109. "InlineCost reason must be set for Always or Never");
  110. return Reason;
  111. }
  112. /// Get the cost delta from the threshold for inlining.
  113. /// Only valid if the cost is of the variable kind. Returns a negative
  114. /// value if the cost is too high to inline.
  115. int getCostDelta() const { return Threshold - getCost(); }
  116. };
  117. /// InlineResult is basically true or false. For false results the message
  118. /// describes a reason.
  119. class InlineResult {
  120. const char *Message = nullptr;
  121. InlineResult(const char *Message = nullptr) : Message(Message) {}
  122. public:
  123. static InlineResult success() { return {}; }
  124. static InlineResult failure(const char *Reason) {
  125. return InlineResult(Reason);
  126. }
  127. bool isSuccess() const { return Message == nullptr; }
  128. const char *getFailureReason() const {
  129. assert(!isSuccess() &&
  130. "getFailureReason should only be called in failure cases");
  131. return Message;
  132. }
  133. };
  134. /// Thresholds to tune inline cost analysis. The inline cost analysis decides
  135. /// the condition to apply a threshold and applies it. Otherwise,
  136. /// DefaultThreshold is used. If a threshold is Optional, it is applied only
  137. /// when it has a valid value. Typically, users of inline cost analysis
  138. /// obtain an InlineParams object through one of the \c getInlineParams methods
  139. /// and pass it to \c getInlineCost. Some specialized versions of inliner
  140. /// (such as the pre-inliner) might have custom logic to compute \c InlineParams
  141. /// object.
  142. struct InlineParams {
  143. /// The default threshold to start with for a callee.
  144. int DefaultThreshold = -1;
  145. /// Threshold to use for callees with inline hint.
  146. Optional<int> HintThreshold;
  147. /// Threshold to use for cold callees.
  148. Optional<int> ColdThreshold;
  149. /// Threshold to use when the caller is optimized for size.
  150. Optional<int> OptSizeThreshold;
  151. /// Threshold to use when the caller is optimized for minsize.
  152. Optional<int> OptMinSizeThreshold;
  153. /// Threshold to use when the callsite is considered hot.
  154. Optional<int> HotCallSiteThreshold;
  155. /// Threshold to use when the callsite is considered hot relative to function
  156. /// entry.
  157. Optional<int> LocallyHotCallSiteThreshold;
  158. /// Threshold to use when the callsite is considered cold.
  159. Optional<int> ColdCallSiteThreshold;
  160. /// Compute inline cost even when the cost has exceeded the threshold.
  161. Optional<bool> ComputeFullInlineCost;
  162. /// Indicate whether we should allow inline deferral.
  163. Optional<bool> EnableDeferral = true;
  164. };
  165. /// Generate the parameters to tune the inline cost analysis based only on the
  166. /// commandline options.
  167. InlineParams getInlineParams();
  168. /// Generate the parameters to tune the inline cost analysis based on command
  169. /// line options. If -inline-threshold option is not explicitly passed,
  170. /// \p Threshold is used as the default threshold.
  171. InlineParams getInlineParams(int Threshold);
  172. /// Generate the parameters to tune the inline cost analysis based on command
  173. /// line options. If -inline-threshold option is not explicitly passed,
  174. /// the default threshold is computed from \p OptLevel and \p SizeOptLevel.
  175. /// An \p OptLevel value above 3 is considered an aggressive optimization mode.
  176. /// \p SizeOptLevel of 1 corresponds to the -Os flag and 2 corresponds to
  177. /// the -Oz flag.
  178. InlineParams getInlineParams(unsigned OptLevel, unsigned SizeOptLevel);
  179. /// Return the cost associated with a callsite, including parameter passing
  180. /// and the call/return instruction.
  181. int getCallsiteCost(CallBase &Call, const DataLayout &DL);
  182. /// Get an InlineCost object representing the cost of inlining this
  183. /// callsite.
  184. ///
  185. /// Note that a default threshold is passed into this function. This threshold
  186. /// could be modified based on callsite's properties and only costs below this
  187. /// new threshold are computed with any accuracy. The new threshold can be
  188. /// used to bound the computation necessary to determine whether the cost is
  189. /// sufficiently low to warrant inlining.
  190. ///
  191. /// Also note that calling this function *dynamically* computes the cost of
  192. /// inlining the callsite. It is an expensive, heavyweight call.
  193. InlineCost
  194. getInlineCost(CallBase &Call, const InlineParams &Params,
  195. TargetTransformInfo &CalleeTTI,
  196. function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
  197. function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
  198. function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
  199. ProfileSummaryInfo *PSI = nullptr,
  200. OptimizationRemarkEmitter *ORE = nullptr);
  201. /// Get an InlineCost with the callee explicitly specified.
  202. /// This allows you to calculate the cost of inlining a function via a
  203. /// pointer. This behaves exactly as the version with no explicit callee
  204. /// parameter in all other respects.
  205. //
  206. InlineCost
  207. getInlineCost(CallBase &Call, Function *Callee, const InlineParams &Params,
  208. TargetTransformInfo &CalleeTTI,
  209. function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
  210. function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
  211. function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
  212. ProfileSummaryInfo *PSI = nullptr,
  213. OptimizationRemarkEmitter *ORE = nullptr);
  214. /// Returns InlineResult::success() if the call site should be always inlined
  215. /// because of user directives, and the inlining is viable. Returns
  216. /// InlineResult::failure() if the inlining may never happen because of user
  217. /// directives or incompatibilities detectable without needing callee traversal.
  218. /// Otherwise returns None, meaning that inlining should be decided based on
  219. /// other criteria (e.g. cost modeling).
  220. Optional<InlineResult> getAttributeBasedInliningDecision(
  221. CallBase &Call, Function *Callee, TargetTransformInfo &CalleeTTI,
  222. function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
  223. /// Get the cost estimate ignoring thresholds. This is similar to getInlineCost
  224. /// when passed InlineParams::ComputeFullInlineCost, or a non-null ORE. It
  225. /// uses default InlineParams otherwise.
  226. /// Contrary to getInlineCost, which makes a threshold-based final evaluation of
  227. /// should/shouldn't inline, captured in InlineResult, getInliningCostEstimate
  228. /// returns:
  229. /// - None, if the inlining cannot happen (is illegal)
  230. /// - an integer, representing the cost.
  231. Optional<int> getInliningCostEstimate(
  232. CallBase &Call, TargetTransformInfo &CalleeTTI,
  233. function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
  234. function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
  235. ProfileSummaryInfo *PSI = nullptr,
  236. OptimizationRemarkEmitter *ORE = nullptr);
  237. /// Minimal filter to detect invalid constructs for inlining.
  238. InlineResult isInlineViable(Function &Callee);
  239. // This pass is used to annotate instructions during the inline process for
  240. // debugging and analysis. The main purpose of the pass is to see and test
  241. // inliner's decisions when creating new optimizations to InlineCost.
  242. struct InlineCostAnnotationPrinterPass
  243. : PassInfoMixin<InlineCostAnnotationPrinterPass> {
  244. raw_ostream &OS;
  245. public:
  246. explicit InlineCostAnnotationPrinterPass(raw_ostream &OS) : OS(OS) {}
  247. PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
  248. };
  249. } // namespace llvm
  250. #endif
  251. #ifdef __GNUC__
  252. #pragma GCC diagnostic pop
  253. #endif