RegAllocScore.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/DenseMap.h"
  17. #include "llvm/ADT/SetVector.h"
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/Analysis/ProfileSummaryInfo.h"
  20. #include "llvm/Analysis/Utils/TFUtils.h"
  21. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  22. #include "llvm/CodeGen/MachineFunction.h"
  23. #include "llvm/CodeGen/SelectionDAGNodes.h"
  24. #include "llvm/IR/Module.h"
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <limits>
  28. namespace llvm {
  29. /// Regalloc score.
  30. class RegAllocScore final {
  31. double CopyCounts = 0.0;
  32. double LoadCounts = 0.0;
  33. double StoreCounts = 0.0;
  34. double CheapRematCounts = 0.0;
  35. double LoadStoreCounts = 0.0;
  36. double ExpensiveRematCounts = 0.0;
  37. public:
  38. RegAllocScore() = default;
  39. RegAllocScore(const RegAllocScore &) = default;
  40. double copyCounts() const { return CopyCounts; }
  41. double loadCounts() const { return LoadCounts; }
  42. double storeCounts() const { return StoreCounts; }
  43. double loadStoreCounts() const { return LoadStoreCounts; }
  44. double expensiveRematCounts() const { return ExpensiveRematCounts; }
  45. double cheapRematCounts() const { return CheapRematCounts; }
  46. void onCopy(double Freq) { CopyCounts += Freq; }
  47. void onLoad(double Freq) { LoadCounts += Freq; }
  48. void onStore(double Freq) { StoreCounts += Freq; }
  49. void onLoadStore(double Freq) { LoadStoreCounts += Freq; }
  50. void onExpensiveRemat(double Freq) { ExpensiveRematCounts += Freq; }
  51. void onCheapRemat(double Freq) { CheapRematCounts += Freq; }
  52. RegAllocScore &operator+=(const RegAllocScore &Other);
  53. bool operator==(const RegAllocScore &Other) const;
  54. bool operator!=(const RegAllocScore &Other) const;
  55. double getScore() const;
  56. };
  57. /// Calculate a score. When comparing 2 scores for the same function but
  58. /// different policies, the better policy would have a smaller score.
  59. /// The implementation is the overload below (which is also easily unittestable)
  60. RegAllocScore calculateRegAllocScore(const MachineFunction &MF,
  61. const MachineBlockFrequencyInfo &MBFI,
  62. AAResults &AAResults);
  63. /// Implementation of the above, which is also more easily unittestable.
  64. RegAllocScore calculateRegAllocScore(
  65. const MachineFunction &MF,
  66. llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
  67. llvm::function_ref<bool(const MachineInstr &)> IsTriviallyRematerializable);
  68. } // end namespace llvm
  69. #endif // LLVM_CODEGEN_REGALLOCSCORE_H_