Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.8 KiB

  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.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_INLINECOST_H
  14. #define LLVM_ANALYSIS_INLINECOST_H
  15. #include "llvm/Analysis/CodeMetrics.h"
  16. #include "llvm/Analysis/CallGraphSCCPass.h"
  17. #include <cassert>
  18. #include <climits>
  19. namespace llvm {
  20. class CallSite;
  21. class DataLayout;
  22. class Function;
  23. class TargetTransformInfo;
  24. namespace InlineConstants {
  25. // Various magic constants used to adjust heuristics.
  26. const int InstrCost = 5;
  27. const int IndirectCallThreshold = 100;
  28. const int CallPenalty = 25;
  29. const int LastCallToStaticBonus = -15000;
  30. const int ColdccPenalty = 2000;
  31. const int NoreturnPenalty = 10000;
  32. /// Do not inline functions which allocate this many bytes on the stack
  33. /// when the caller is recursive.
  34. const unsigned TotalAllocaSizeRecursiveCaller = 1024;
  35. }
  36. /// \brief Represents the cost of inlining a function.
  37. ///
  38. /// This supports special values for functions which should "always" or
  39. /// "never" be inlined. Otherwise, the cost represents a unitless amount;
  40. /// smaller values increase the likelihood of the function being inlined.
  41. ///
  42. /// Objects of this type also provide the adjusted threshold for inlining
  43. /// based on the information available for a particular callsite. They can be
  44. /// directly tested to determine if inlining should occur given the cost and
  45. /// threshold for this cost metric.
  46. class InlineCost {
  47. enum SentinelValues {
  48. AlwaysInlineCost = INT_MIN,
  49. NeverInlineCost = INT_MAX
  50. };
  51. /// \brief The estimated cost of inlining this callsite.
  52. const int Cost;
  53. /// \brief The adjusted threshold against which this cost was computed.
  54. const int Threshold;
  55. // Trivial constructor, interesting logic in the factory functions below.
  56. InlineCost(int Cost, int Threshold) : Cost(Cost), Threshold(Threshold) {}
  57. public:
  58. static InlineCost get(int Cost, int Threshold) {
  59. assert(Cost > AlwaysInlineCost && "Cost crosses sentinel value");
  60. assert(Cost < NeverInlineCost && "Cost crosses sentinel value");
  61. return InlineCost(Cost, Threshold);
  62. }
  63. static InlineCost getAlways() {
  64. return InlineCost(AlwaysInlineCost, 0);
  65. }
  66. static InlineCost getNever() {
  67. return InlineCost(NeverInlineCost, 0);
  68. }
  69. /// \brief Test whether the inline cost is low enough for inlining.
  70. operator bool() const {
  71. return Cost < Threshold;
  72. }
  73. bool isAlways() const { return Cost == AlwaysInlineCost; }
  74. bool isNever() const { return Cost == NeverInlineCost; }
  75. bool isVariable() const { return !isAlways() && !isNever(); }
  76. /// \brief Get the inline cost estimate.
  77. /// It is an error to call this on an "always" or "never" InlineCost.
  78. int getCost() const {
  79. assert(isVariable() && "Invalid access of InlineCost");
  80. return Cost;
  81. }
  82. /// \brief Get the cost delta from the threshold for inlining.
  83. /// Only valid if the cost is of the variable kind. Returns a negative
  84. /// value if the cost is too high to inline.
  85. int getCostDelta() const { return Threshold - getCost(); }
  86. };
  87. /// \brief Cost analyzer used by inliner.
  88. class InlineCostAnalysis : public CallGraphSCCPass {
  89. const DataLayout *TD;
  90. const TargetTransformInfo *TTI;
  91. public:
  92. static char ID;
  93. InlineCostAnalysis();
  94. ~InlineCostAnalysis();
  95. // Pass interface implementation.
  96. void getAnalysisUsage(AnalysisUsage &AU) const;
  97. bool runOnSCC(CallGraphSCC &SCC);
  98. /// \brief Get an InlineCost object representing the cost of inlining this
  99. /// callsite.
  100. ///
  101. /// Note that threshold is passed into this function. Only costs below the
  102. /// threshold are computed with any accuracy. The threshold can be used to
  103. /// bound the computation necessary to determine whether the cost is
  104. /// sufficiently low to warrant inlining.
  105. ///
  106. /// Also note that calling this function *dynamically* computes the cost of
  107. /// inlining the callsite. It is an expensive, heavyweight call.
  108. InlineCost getInlineCost(CallSite CS, int Threshold);
  109. /// \brief Get an InlineCost with the callee explicitly specified.
  110. /// This allows you to calculate the cost of inlining a function via a
  111. /// pointer. This behaves exactly as the version with no explicit callee
  112. /// parameter in all other respects.
  113. //
  114. // Note: This is used by out-of-tree passes, please do not remove without
  115. // adding a replacement API.
  116. InlineCost getInlineCost(CallSite CS, Function *Callee, int Threshold);
  117. /// \brief Minimal filter to detect invalid constructs for inlining.
  118. bool isInlineViable(Function &Callee);
  119. };
  120. }
  121. #endif