RegAllocScore.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //===- RegAllocScore.cpp - evaluate regalloc policy quality ---------------===//
  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. #include "RegAllocScore.h"
  15. #include "llvm/ADT/DenseMapInfo.h"
  16. #include "llvm/ADT/ilist_iterator.h"
  17. #include "llvm/CodeGen/MachineBasicBlock.h"
  18. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  19. #include "llvm/CodeGen/MachineFunction.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineInstrBundleIterator.h"
  22. #include "llvm/CodeGen/TargetInstrInfo.h"
  23. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  24. #include "llvm/MC/MCInstrDesc.h"
  25. #include "llvm/Support/CommandLine.h"
  26. using namespace llvm;
  27. cl::opt<double> CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden);
  28. cl::opt<double> LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden);
  29. cl::opt<double> StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden);
  30. cl::opt<double> CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2),
  31. cl::Hidden);
  32. cl::opt<double> ExpensiveRematWeight("regalloc-expensive-remat-weight",
  33. cl::init(1.0), cl::Hidden);
  34. #define DEBUG_TYPE "regalloc-score"
  35. RegAllocScore &RegAllocScore::operator+=(const RegAllocScore &Other) {
  36. CopyCounts += Other.copyCounts();
  37. LoadCounts += Other.loadCounts();
  38. StoreCounts += Other.storeCounts();
  39. LoadStoreCounts += Other.loadStoreCounts();
  40. CheapRematCounts += Other.cheapRematCounts();
  41. ExpensiveRematCounts += Other.expensiveRematCounts();
  42. return *this;
  43. }
  44. bool RegAllocScore::operator==(const RegAllocScore &Other) const {
  45. return copyCounts() == Other.copyCounts() &&
  46. loadCounts() == Other.loadCounts() &&
  47. storeCounts() == Other.storeCounts() &&
  48. loadStoreCounts() == Other.loadStoreCounts() &&
  49. cheapRematCounts() == Other.cheapRematCounts() &&
  50. expensiveRematCounts() == Other.expensiveRematCounts();
  51. }
  52. bool RegAllocScore::operator!=(const RegAllocScore &Other) const {
  53. return !(*this == Other);
  54. }
  55. double RegAllocScore::getScore() const {
  56. double Ret = 0.0;
  57. Ret += CopyWeight * copyCounts();
  58. Ret += LoadWeight * loadCounts();
  59. Ret += StoreWeight * storeCounts();
  60. Ret += (LoadWeight + StoreWeight) * loadStoreCounts();
  61. Ret += CheapRematWeight * cheapRematCounts();
  62. Ret += ExpensiveRematWeight * expensiveRematCounts();
  63. return Ret;
  64. }
  65. RegAllocScore
  66. llvm::calculateRegAllocScore(const MachineFunction &MF,
  67. const MachineBlockFrequencyInfo &MBFI) {
  68. return calculateRegAllocScore(
  69. MF,
  70. [&](const MachineBasicBlock &MBB) {
  71. return MBFI.getBlockFreqRelativeToEntryBlock(&MBB);
  72. },
  73. [&](const MachineInstr &MI) {
  74. return MF.getSubtarget().getInstrInfo()->isTriviallyReMaterializable(
  75. MI);
  76. });
  77. }
  78. RegAllocScore llvm::calculateRegAllocScore(
  79. const MachineFunction &MF,
  80. llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
  81. llvm::function_ref<bool(const MachineInstr &)>
  82. IsTriviallyRematerializable) {
  83. RegAllocScore Total;
  84. for (const MachineBasicBlock &MBB : MF) {
  85. double BlockFreqRelativeToEntrypoint = GetBBFreq(MBB);
  86. RegAllocScore MBBScore;
  87. for (const MachineInstr &MI : MBB) {
  88. if (MI.isDebugInstr() || MI.isKill() || MI.isInlineAsm()) {
  89. continue;
  90. }
  91. if (MI.isCopy()) {
  92. MBBScore.onCopy(BlockFreqRelativeToEntrypoint);
  93. } else if (IsTriviallyRematerializable(MI)) {
  94. if (MI.getDesc().isAsCheapAsAMove()) {
  95. MBBScore.onCheapRemat(BlockFreqRelativeToEntrypoint);
  96. } else {
  97. MBBScore.onExpensiveRemat(BlockFreqRelativeToEntrypoint);
  98. }
  99. } else if (MI.mayLoad() && MI.mayStore()) {
  100. MBBScore.onLoadStore(BlockFreqRelativeToEntrypoint);
  101. } else if (MI.mayLoad()) {
  102. MBBScore.onLoad(BlockFreqRelativeToEntrypoint);
  103. } else if (MI.mayStore()) {
  104. MBBScore.onStore(BlockFreqRelativeToEntrypoint);
  105. }
  106. }
  107. Total += MBBScore;
  108. }
  109. return Total;
  110. }