/3rd_party/llvm/include/llvm/CodeGen/CalcSpillWeights.h

https://code.google.com/p/softart/ · C Header · 77 lines · 37 code · 12 blank · 28 comment · 0 complexity · dfca80056a8f4f61cd7c0fa81af05d85 MD5 · raw file

  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. ///
  28. static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
  29. // The constant 25 instructions is added to avoid depending too much on
  30. // accidental SlotIndex gaps for small intervals. The effect is that small
  31. // intervals have a spill weight that is mostly proportional to the number
  32. // of uses, while large intervals get a spill weight that is closer to a use
  33. // density.
  34. return UseDefFreq / (Size + 25*SlotIndex::InstrDist);
  35. }
  36. /// \brief Calculate auxiliary information for a virtual register such as its
  37. /// spill weight and allocation hint.
  38. class VirtRegAuxInfo {
  39. public:
  40. typedef float (*NormalizingFn)(float, unsigned);
  41. private:
  42. MachineFunction &MF;
  43. LiveIntervals &LIS;
  44. const MachineLoopInfo &Loops;
  45. const MachineBlockFrequencyInfo &MBFI;
  46. DenseMap<unsigned, float> Hint;
  47. NormalizingFn normalize;
  48. public:
  49. VirtRegAuxInfo(MachineFunction &mf, LiveIntervals &lis,
  50. const MachineLoopInfo &loops,
  51. const MachineBlockFrequencyInfo &mbfi,
  52. NormalizingFn norm = normalizeSpillWeight)
  53. : MF(mf), LIS(lis), Loops(loops), MBFI(mbfi), normalize(norm) {}
  54. /// \brief (re)compute li's spill weight and allocation hint.
  55. void calculateSpillWeightAndHint(LiveInterval &li);
  56. };
  57. /// \brief Compute spill weights and allocation hints for all virtual register
  58. /// live intervals.
  59. void calculateSpillWeightsAndHints(LiveIntervals &LIS, MachineFunction &MF,
  60. const MachineLoopInfo &MLI,
  61. const MachineBlockFrequencyInfo &MBFI,
  62. VirtRegAuxInfo::NormalizingFn norm =
  63. normalizeSpillWeight);
  64. }
  65. #endif // LLVM_CODEGEN_CALCSPILLWEIGHTS_H