LLVM 20.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
20namespace llvm {
21
22class MachineBasicBlock;
23class MachineBlockFrequencyInfo;
24class MachineFunction;
25class MachineInstr;
26
27/// Regalloc score.
28class 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
36public:
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
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)
63RegAllocScore calculateRegAllocScore(const MachineFunction &MF,
64 const MachineBlockFrequencyInfo &MBFI);
65
66/// Implementation of the above, which is also more easily unittestable.
67RegAllocScore 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_
Regalloc score.
Definition: RegAllocScore.h:28
double copyCounts() const
Definition: RegAllocScore.h:40
void onLoadStore(double Freq)
Definition: RegAllocScore.h:50
RegAllocScore & operator+=(const RegAllocScore &Other)
void onCheapRemat(double Freq)
Definition: RegAllocScore.h:52
bool operator!=(const RegAllocScore &Other) const
double cheapRematCounts() const
Definition: RegAllocScore.h:45
void onStore(double Freq)
Definition: RegAllocScore.h:49
RegAllocScore()=default
double storeCounts() const
Definition: RegAllocScore.h:42
double getScore() const
void onExpensiveRemat(double Freq)
Definition: RegAllocScore.h:51
double expensiveRematCounts() const
Definition: RegAllocScore.h:44
void onCopy(double Freq)
Definition: RegAllocScore.h:47
double loadStoreCounts() const
Definition: RegAllocScore.h:43
void onLoad(double Freq)
Definition: RegAllocScore.h:48
RegAllocScore(const RegAllocScore &)=default
double loadCounts() const
Definition: RegAllocScore.h:41
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
RegAllocScore calculateRegAllocScore(const MachineFunction &MF, const MachineBlockFrequencyInfo &MBFI)
Calculate a score.
@ Other
Any other memory.