LLVM  15.0.0git
RegAllocScore.h
Go to the documentation of this file.
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 
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_
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::RegAllocScore::cheapRematCounts
double cheapRematCounts() const
Definition: RegAllocScore.h:46
llvm::RegAllocScore::RegAllocScore
RegAllocScore()=default
llvm::RegAllocScore::storeCounts
double storeCounts() const
Definition: RegAllocScore.h:43
llvm::RegAllocScore::onCheapRemat
void onCheapRemat(double Freq)
Definition: RegAllocScore.h:53
llvm::RegAllocScore
Regalloc score.
Definition: RegAllocScore.h:29
llvm::RegAllocScore::onExpensiveRemat
void onExpensiveRemat(double Freq)
Definition: RegAllocScore.h:52
llvm::RegAllocScore::operator+=
RegAllocScore & operator+=(const RegAllocScore &Other)
Definition: RegAllocScore.cpp:40
llvm::RegAllocScore::loadCounts
double loadCounts() const
Definition: RegAllocScore.h:42
STLFunctionalExtras.h
llvm::RegAllocScore::operator!=
bool operator!=(const RegAllocScore &Other) const
Definition: RegAllocScore.cpp:59
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::RegAllocScore::onStore
void onStore(double Freq)
Definition: RegAllocScore.h:50
llvm::RegAllocScore::onCopy
void onCopy(double Freq)
Definition: RegAllocScore.h:48
llvm::RegAllocScore::onLoad
void onLoad(double Freq)
Definition: RegAllocScore.h:49
llvm::RegAllocScore::onLoadStore
void onLoadStore(double Freq)
Definition: RegAllocScore.h:51
llvm::RegAllocScore::copyCounts
double copyCounts() const
Definition: RegAllocScore.h:41
llvm::calculateRegAllocScore
RegAllocScore calculateRegAllocScore(const MachineFunction &MF, const MachineBlockFrequencyInfo &MBFI, AAResults &AAResults)
Calculate a score.
Definition: RegAllocScore.cpp:76
llvm::RegAllocScore::getScore
double getScore() const
Definition: RegAllocScore.cpp:63
llvm::RegAllocScore::expensiveRematCounts
double expensiveRematCounts() const
Definition: RegAllocScore.h:45
llvm::RegAllocScore::loadStoreCounts
double loadStoreCounts() const
Definition: RegAllocScore.h:44
llvm::RegAllocScore::operator==
bool operator==(const RegAllocScore &Other) const
Definition: RegAllocScore.cpp:50