CalcSpillWeights.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- lib/CodeGen/CalcSpillWeights.h ---------------------------*- 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. #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
  14. #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/CodeGen/SlotIndexes.h"
  17. namespace llvm {
  18. class LiveInterval;
  19. class LiveIntervals;
  20. class MachineBlockFrequencyInfo;
  21. class MachineFunction;
  22. class MachineLoopInfo;
  23. class VirtRegMap;
  24. /// Normalize the spill weight of a live interval
  25. ///
  26. /// The spill weight of a live interval is computed as:
  27. ///
  28. /// (sum(use freq) + sum(def freq)) / (K + size)
  29. ///
  30. /// @param UseDefFreq Expected number of executed use and def instructions
  31. /// per function call. Derived from block frequencies.
  32. /// @param Size Size of live interval as returnexd by getSize()
  33. /// @param NumInstr Number of instructions using this live interval
  34. static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size,
  35. unsigned NumInstr) {
  36. // The constant 25 instructions is added to avoid depending too much on
  37. // accidental SlotIndex gaps for small intervals. The effect is that small
  38. // intervals have a spill weight that is mostly proportional to the number
  39. // of uses, while large intervals get a spill weight that is closer to a use
  40. // density.
  41. return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
  42. }
  43. /// Calculate auxiliary information for a virtual register such as its
  44. /// spill weight and allocation hint.
  45. class VirtRegAuxInfo {
  46. MachineFunction &MF;
  47. LiveIntervals &LIS;
  48. const VirtRegMap &VRM;
  49. const MachineLoopInfo &Loops;
  50. const MachineBlockFrequencyInfo &MBFI;
  51. /// Returns true if Reg of live interval LI is used in instruction with many
  52. /// operands like STATEPOINT.
  53. bool isLiveAtStatepointVarArg(LiveInterval &LI);
  54. public:
  55. VirtRegAuxInfo(MachineFunction &MF, LiveIntervals &LIS,
  56. const VirtRegMap &VRM, const MachineLoopInfo &Loops,
  57. const MachineBlockFrequencyInfo &MBFI)
  58. : MF(MF), LIS(LIS), VRM(VRM), Loops(Loops), MBFI(MBFI) {}
  59. virtual ~VirtRegAuxInfo() = default;
  60. /// (re)compute li's spill weight and allocation hint.
  61. void calculateSpillWeightAndHint(LiveInterval &LI);
  62. /// Compute future expected spill weight of a split artifact of LI
  63. /// that will span between start and end slot indexes.
  64. /// \param LI The live interval to be split.
  65. /// \param Start The expected beginning of the split artifact. Instructions
  66. /// before start will not affect the weight.
  67. /// \param End The expected end of the split artifact. Instructions
  68. /// after end will not affect the weight.
  69. /// \return The expected spill weight of the split artifact. Returns
  70. /// negative weight for unspillable LI.
  71. float futureWeight(LiveInterval &LI, SlotIndex Start, SlotIndex End);
  72. /// Compute spill weights and allocation hints for all virtual register
  73. /// live intervals.
  74. void calculateSpillWeightsAndHints();
  75. /// Return the preferred allocation register for reg, given a COPY
  76. /// instruction.
  77. static Register copyHint(const MachineInstr *MI, unsigned Reg,
  78. const TargetRegisterInfo &TRI,
  79. const MachineRegisterInfo &MRI);
  80. /// Determine if all values in LI are rematerializable.
  81. static bool isRematerializable(const LiveInterval &LI,
  82. const LiveIntervals &LIS,
  83. const VirtRegMap &VRM,
  84. const TargetInstrInfo &TII);
  85. protected:
  86. /// Helper function for weight calculations.
  87. /// (Re)compute LI's spill weight and allocation hint, or, for non null
  88. /// start and end - compute future expected spill weight of a split
  89. /// artifact of LI that will span between start and end slot indexes.
  90. /// \param LI The live interval for which to compute the weight.
  91. /// \param Start The expected beginning of the split artifact. Instructions
  92. /// before start will not affect the weight. Relevant for
  93. /// weight calculation of future split artifact.
  94. /// \param End The expected end of the split artifact. Instructions
  95. /// after end will not affect the weight. Relevant for
  96. /// weight calculation of future split artifact.
  97. /// \return The spill weight. Returns negative weight for unspillable LI.
  98. float weightCalcHelper(LiveInterval &LI, SlotIndex *Start = nullptr,
  99. SlotIndex *End = nullptr);
  100. /// Weight normalization function.
  101. virtual float normalize(float UseDefFreq, unsigned Size,
  102. unsigned NumInstr) {
  103. return normalizeSpillWeight(UseDefFreq, Size, NumInstr);
  104. }
  105. };
  106. } // end namespace llvm
  107. #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H
  108. #ifdef __GNUC__
  109. #pragma GCC diagnostic pop
  110. #endif