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