CalcSpillWeights.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //===---------------- lib/CodeGen/CalcSpillWeights.h ------------*- 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. #ifndef LLVM_CODEGEN_CALCSPILLWEIGHTS_H
  10. #define LLVM_CODEGEN_CALCSPILLWEIGHTS_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/CodeGen/SlotIndexes.h"
  13. namespace llvm {
  14. class LiveInterval;
  15. class LiveIntervals;
  16. class MachineBlockFrequencyInfo;
  17. class MachineLoopInfo;
  18. /// \brief Normalize the spill weight of a live interval
  19. ///
  20. /// The spill weight of a live interval is computed as:
  21. ///
  22. /// (sum(use freq) + sum(def freq)) / (K + size)
  23. ///
  24. /// @param UseDefFreq Expected number of executed use and def instructions
  25. /// per function call. Derived from block frequencies.
  26. /// @param Size Size of live interval as returnexd by getSize()
  27. /// @param NumInstr Number of instructions using this live interval
  28. ///
  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. /// \brief Calculate auxiliary information for a virtual register such as its
  39. /// spill weight and allocation hint.
  40. class VirtRegAuxInfo {
  41. public:
  42. typedef float (*NormalizingFn)(float, unsigned, unsigned);
  43. private:
  44. MachineFunction &MF;
  45. LiveIntervals &LIS;
  46. const MachineLoopInfo &Loops;
  47. const MachineBlockFrequencyInfo &MBFI;
  48. DenseMap<unsigned, float> Hint;
  49. NormalizingFn normalize;
  50. public:
  51. VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
  52. const MachineLoopInfo &loops,
  53. const MachineBlockFrequencyInfo &mbfi,
  54. NormalizingFn norm = normalizeSpillWeight)
  55. : MF(mf), LIS(lis), Loops(loops), MBFI(mbfi), normalize(norm) {}
  56. /// \brief (re)compute li's spill weight and allocation hint.
  57. void calculateSpillWeightAndHint(LiveInterval &li);
  58. };
  59. /// \brief Compute spill weights and allocation hints for all virtual register
  60. /// live intervals.
  61. void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF,
  62. const MachineLoopInfo &MLI,
  63. const MachineBlockFrequencyInfo &MBFI,
  64. VirtRegAuxInfo::NormalizingFn norm =
  65. normalizeSpillWeight);
  66. }
  67. #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H