RegAllocScore.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //==- RegAllocScore.h - evaluate regalloc policy quality ----------*-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. /// Calculate a measure of the register allocation policy quality. This is used
  9. /// to construct a reward for the training of the ML-driven allocation policy.
  10. /// Currently, the score is the sum of the machine basic block frequency-weighed
  11. /// number of loads, stores, copies, and remat instructions, each factored with
  12. /// a relative weight.
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_REGALLOCSCORE_H_
  15. #define LLVM_CODEGEN_REGALLOCSCORE_H_
  16. #include "llvm/ADT/STLFunctionalExtras.h"
  17. namespace llvm {
  18. class MachineBasicBlock;
  19. class MachineBlockFrequencyInfo;
  20. class MachineFunction;
  21. class MachineInstr;
  22. /// Regalloc score.
  23. class RegAllocScore final {
  24. double CopyCounts = 0.0;
  25. double LoadCounts = 0.0;
  26. double StoreCounts = 0.0;
  27. double CheapRematCounts = 0.0;
  28. double LoadStoreCounts = 0.0;
  29. double ExpensiveRematCounts = 0.0;
  30. public:
  31. RegAllocScore() = default;
  32. RegAllocScore(const RegAllocScore &) = default;
  33. double copyCounts() const { return CopyCounts; }
  34. double loadCounts() const { return LoadCounts; }
  35. double storeCounts() const { return StoreCounts; }
  36. double loadStoreCounts() const { return LoadStoreCounts; }
  37. double expensiveRematCounts() const { return ExpensiveRematCounts; }
  38. double cheapRematCounts() const { return CheapRematCounts; }
  39. void onCopy(double Freq) { CopyCounts += Freq; }
  40. void onLoad(double Freq) { LoadCounts += Freq; }
  41. void onStore(double Freq) { StoreCounts += Freq; }
  42. void onLoadStore(double Freq) { LoadStoreCounts += Freq; }
  43. void onExpensiveRemat(double Freq) { ExpensiveRematCounts += Freq; }
  44. void onCheapRemat(double Freq) { CheapRematCounts += Freq; }
  45. RegAllocScore &operator+=(const RegAllocScore &Other);
  46. bool operator==(const RegAllocScore &Other) const;
  47. bool operator!=(const RegAllocScore &Other) const;
  48. double getScore() const;
  49. };
  50. /// Calculate a score. When comparing 2 scores for the same function but
  51. /// different policies, the better policy would have a smaller score.
  52. /// The implementation is the overload below (which is also easily unittestable)
  53. RegAllocScore calculateRegAllocScore(const MachineFunction &MF,
  54. const MachineBlockFrequencyInfo &MBFI);
  55. /// Implementation of the above, which is also more easily unittestable.
  56. RegAllocScore calculateRegAllocScore(
  57. const MachineFunction &MF,
  58. llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
  59. llvm::function_ref<bool(const MachineInstr &)> IsTriviallyRematerializable);
  60. } // end namespace llvm
  61. #endif // LLVM_CODEGEN_REGALLOCSCORE_H_