LLVM  16.0.0git
RegAllocScore.cpp
Go to the documentation of this file.
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"
26 #include "llvm/MC/MCInstrDesc.h"
28 
29 using namespace llvm;
30 cl::opt<double> CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden);
31 cl::opt<double> LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden);
32 cl::opt<double> StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden);
33 cl::opt<double> CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2),
34  cl::Hidden);
35 cl::opt<double> ExpensiveRematWeight("regalloc-expensive-remat-weight",
36  cl::init(1.0), cl::Hidden);
37 #define DEBUG_TYPE "regalloc-score"
38 
40  CopyCounts += Other.copyCounts();
41  LoadCounts += Other.loadCounts();
42  StoreCounts += Other.storeCounts();
43  LoadStoreCounts += Other.loadStoreCounts();
44  CheapRematCounts += Other.cheapRematCounts();
45  ExpensiveRematCounts += Other.expensiveRematCounts();
46  return *this;
47 }
48 
49 bool RegAllocScore::operator==(const RegAllocScore &Other) const {
50  return copyCounts() == Other.copyCounts() &&
51  loadCounts() == Other.loadCounts() &&
52  storeCounts() == Other.storeCounts() &&
53  loadStoreCounts() == Other.loadStoreCounts() &&
54  cheapRematCounts() == Other.cheapRematCounts() &&
55  expensiveRematCounts() == Other.expensiveRematCounts();
56 }
57 
58 bool RegAllocScore::operator!=(const RegAllocScore &Other) const {
59  return !(*this == Other);
60 }
61 
62 double RegAllocScore::getScore() const {
63  double Ret = 0.0;
64  Ret += CopyWeight * copyCounts();
65  Ret += LoadWeight * loadCounts();
70 
71  return Ret;
72 }
73 
76  const MachineBlockFrequencyInfo &MBFI) {
78  MF,
79  [&](const MachineBasicBlock &MBB) {
81  },
82  [&](const MachineInstr &MI) {
84  MI);
85  });
86 }
87 
89  const MachineFunction &MF,
90  llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
91  llvm::function_ref<bool(const MachineInstr &)>
92  IsTriviallyRematerializable) {
93  RegAllocScore Total;
94 
95  for (const MachineBasicBlock &MBB : MF) {
96  double BlockFreqRelativeToEntrypoint = GetBBFreq(MBB);
97  RegAllocScore MBBScore;
98 
99  for (const MachineInstr &MI : MBB) {
100  if (MI.isDebugInstr() || MI.isKill() || MI.isInlineAsm()) {
101  continue;
102  }
103  if (MI.isCopy()) {
104  MBBScore.onCopy(BlockFreqRelativeToEntrypoint);
105  } else if (IsTriviallyRematerializable(MI)) {
106  if (MI.getDesc().isAsCheapAsAMove()) {
107  MBBScore.onCheapRemat(BlockFreqRelativeToEntrypoint);
108  } else {
109  MBBScore.onExpensiveRemat(BlockFreqRelativeToEntrypoint);
110  }
111  } else if (MI.mayLoad() && MI.mayStore()) {
112  MBBScore.onLoadStore(BlockFreqRelativeToEntrypoint);
113  } else if (MI.mayLoad()) {
114  MBBScore.onLoad(BlockFreqRelativeToEntrypoint);
115  } else if (MI.mayStore()) {
116  MBBScore.onStore(BlockFreqRelativeToEntrypoint);
117  }
118  }
119  Total += MBBScore;
120  }
121  return Total;
122 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:108
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCInstrDesc.h
llvm::RegAllocScore::cheapRematCounts
double cheapRematCounts() const
Definition: RegAllocScore.h:45
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:95
llvm::calculateRegAllocScore
RegAllocScore calculateRegAllocScore(const MachineFunction &MF, const MachineBlockFrequencyInfo &MBFI)
Calculate a score.
Definition: RegAllocScore.cpp:75
llvm::MachineBlockFrequencyInfo::getBlockFreqRelativeToEntryBlock
float getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const
Compute the frequency of the block, relative to the entry block.
Definition: MachineBlockFrequencyInfo.h:68
MachineBasicBlock.h
LoadWeight
cl::opt< double > LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden)
llvm::RegAllocScore::storeCounts
double storeCounts() const
Definition: RegAllocScore.h:42
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::TargetInstrInfo::isTriviallyReMaterializable
bool isTriviallyReMaterializable(const MachineInstr &MI) const
Return true if the instruction is trivially rematerializable, meaning it has no side effects and requ...
Definition: TargetInstrInfo.h:130
TargetInstrInfo.h
llvm::RegAllocScore::onCheapRemat
void onCheapRemat(double Freq)
Definition: RegAllocScore.h:52
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::RegAllocScore
Regalloc score.
Definition: RegAllocScore.h:28
llvm::RegAllocScore::onExpensiveRemat
void onExpensiveRemat(double Freq)
Definition: RegAllocScore.h:51
ExpensiveRematWeight
cl::opt< double > ExpensiveRematWeight("regalloc-expensive-remat-weight", cl::init(1.0), cl::Hidden)
CommandLine.h
llvm::MachineBlockFrequencyInfo
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
Definition: MachineBlockFrequencyInfo.h:33
RegAllocScore.h
CheapRematWeight
cl::opt< double > CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2), cl::Hidden)
llvm::RegAllocScore::operator+=
RegAllocScore & operator+=(const RegAllocScore &Other)
Definition: RegAllocScore.cpp:39
llvm::RegAllocScore::loadCounts
double loadCounts() const
Definition: RegAllocScore.h:41
STLForwardCompat.h
llvm::RegAllocScore::operator!=
bool operator!=(const RegAllocScore &Other) const
Definition: RegAllocScore.cpp:58
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
CopyWeight
cl::opt< double > CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden)
llvm::RegAllocScore::onStore
void onStore(double Freq)
Definition: RegAllocScore.h:49
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:657
llvm::cl::opt
Definition: CommandLine.h:1412
StoreWeight
cl::opt< double > StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden)
llvm::RegAllocScore::onCopy
void onCopy(double Freq)
Definition: RegAllocScore.h:47
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::RegAllocScore::onLoad
void onLoad(double Freq)
Definition: RegAllocScore.h:48
llvm::RegAllocScore::onLoadStore
void onLoadStore(double Freq)
Definition: RegAllocScore.h:50
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:447
llvm::MachineFunction
Definition: MachineFunction.h:257
MachineInstrBundleIterator.h
TargetSubtargetInfo.h
llvm::RegAllocScore::copyCounts
double copyCounts() const
Definition: RegAllocScore.h:40
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::RegAllocScore::getScore
double getScore() const
Definition: RegAllocScore.cpp:62
llvm::RegAllocScore::expensiveRematCounts
double expensiveRematCounts() const
Definition: RegAllocScore.h:44
DenseMapInfo.h
MachineFunction.h
MachineBlockFrequencyInfo.h
llvm::RegAllocScore::loadStoreCounts
double loadStoreCounts() const
Definition: RegAllocScore.h:43
ilist_iterator.h
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1251
llvm::RegAllocScore::operator==
bool operator==(const RegAllocScore &Other) const
Definition: RegAllocScore.cpp:49