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