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