RegAllocScore.cpp 4.6 KB

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