LLVM 22.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
20
21namespace llvm {
22
23class MachineBasicBlock;
24class MachineBlockFrequencyInfo;
25class MachineFunction;
26class MachineInstr;
27
28/// Regalloc score.
29class 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
37public:
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
56 LLVM_ABI_FOR_TEST bool operator==(const RegAllocScore &Other) const;
57 bool operator!=(const RegAllocScore &Other) const;
58 LLVM_ABI_FOR_TEST 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)
64RegAllocScore calculateRegAllocScore(const MachineFunction &MF,
65 const MachineBlockFrequencyInfo &MBFI);
66
67/// Implementation of the above, which is also more easily unittestable.
69 const MachineFunction &MF,
70 llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
71 llvm::function_ref<bool(const MachineInstr &)> IsTriviallyRematerializable);
72} // end namespace llvm
73
74#endif // LLVM_CODEGEN_REGALLOCSCORE_H_
#define LLVM_ABI_FOR_TEST
Definition: Compiler.h:218
Regalloc score.
Definition: RegAllocScore.h:29
double copyCounts() const
Definition: RegAllocScore.h:41
void onLoadStore(double Freq)
Definition: RegAllocScore.h:51
RegAllocScore & operator+=(const RegAllocScore &Other)
void onCheapRemat(double Freq)
Definition: RegAllocScore.h:53
bool operator!=(const RegAllocScore &Other) const
double cheapRematCounts() const
Definition: RegAllocScore.h:46
void onStore(double Freq)
Definition: RegAllocScore.h:50
RegAllocScore()=default
double storeCounts() const
Definition: RegAllocScore.h:43
LLVM_ABI_FOR_TEST double getScore() const
void onExpensiveRemat(double Freq)
Definition: RegAllocScore.h:52
double expensiveRematCounts() const
Definition: RegAllocScore.h:45
void onCopy(double Freq)
Definition: RegAllocScore.h:48
double loadStoreCounts() const
Definition: RegAllocScore.h:44
void onLoad(double Freq)
Definition: RegAllocScore.h:49
RegAllocScore(const RegAllocScore &)=default
double loadCounts() const
Definition: RegAllocScore.h:42
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.