CalcSpillWeights.h 4.6 KB

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