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