CalcSpillWeights.h 4.6 KB

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