LLVM  15.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"
18 #include "llvm/ADT/SetVector.h"
27 #include "llvm/MC/MCInstrDesc.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 
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();
71 
72  return Ret;
73 }
74 
77  const MachineBlockFrequencyInfo &MBFI,
80  MF,
81  [&](const MachineBasicBlock &MBB) {
83  },
84  [&](const MachineInstr &MI) {
86  MI, &AAResults);
87  });
88 }
89 
91  const MachineFunction &MF,
92  llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
93  llvm::function_ref<bool(const MachineInstr &)>
94  IsTriviallyRematerializable) {
95  RegAllocScore Total;
96 
97  for (const MachineBasicBlock &MBB : MF) {
98  double BlockFreqRelativeToEntrypoint = GetBBFreq(MBB);
99  RegAllocScore MBBScore;
100 
101  for (const MachineInstr &MI : MBB) {
102  if (MI.isDebugInstr() || MI.isKill() || MI.isInlineAsm()) {
103  continue;
104  }
105  if (MI.isCopy()) {
106  MBBScore.onCopy(BlockFreqRelativeToEntrypoint);
107  } else if (IsTriviallyRematerializable(MI)) {
108  if (MI.getDesc().isAsCheapAsAMove()) {
109  MBBScore.onCheapRemat(BlockFreqRelativeToEntrypoint);
110  } else {
111  MBBScore.onExpensiveRemat(BlockFreqRelativeToEntrypoint);
112  }
113  } else if (MI.mayLoad() && MI.mayStore()) {
114  MBBScore.onLoadStore(BlockFreqRelativeToEntrypoint);
115  } else if (MI.mayLoad()) {
116  MBBScore.onLoad(BlockFreqRelativeToEntrypoint);
117  } else if (MI.mayStore()) {
118  MBBScore.onStore(BlockFreqRelativeToEntrypoint);
119  }
120  }
121  Total += MBBScore;
122  }
123  return Total;
124 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:104
MachineInstr.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
MCInstrDesc.h
llvm::RegAllocScore::cheapRematCounts
double cheapRematCounts() const
Definition: RegAllocScore.h:46
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:93
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:43
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:139
TargetInstrInfo.h
llvm::RegAllocScore::onCheapRemat
void onCheapRemat(double Freq)
Definition: RegAllocScore.h:53
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
llvm::RegAllocScore
Regalloc score.
Definition: RegAllocScore.h:29
llvm::RegAllocScore::onExpensiveRemat
void onExpensiveRemat(double Freq)
Definition: RegAllocScore.h:52
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
llvm::AAResults
Definition: AliasAnalysis.h:511
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:40
llvm::RegAllocScore::loadCounts
double loadCounts() const
Definition: RegAllocScore.h:42
STLForwardCompat.h
llvm::RegAllocScore::operator!=
bool operator!=(const RegAllocScore &Other) const
Definition: RegAllocScore.cpp:59
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:50
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:640
llvm::cl::opt
Definition: CommandLine.h:1392
StoreWeight
cl::opt< double > StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden)
llvm::RegAllocScore::onCopy
void onCopy(double Freq)
Definition: RegAllocScore.h:48
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
llvm::RegAllocScore::onLoad
void onLoad(double Freq)
Definition: RegAllocScore.h:49
llvm::RegAllocScore::onLoadStore
void onLoadStore(double Freq)
Definition: RegAllocScore.h:51
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:432
llvm::MachineFunction
Definition: MachineFunction.h:241
MachineInstrBundleIterator.h
TargetSubtargetInfo.h
llvm::RegAllocScore::copyCounts
double copyCounts() const
Definition: RegAllocScore.h:41
llvm::TargetInstrInfo::isTriviallyReMaterializable
bool isTriviallyReMaterializable(const MachineInstr &MI, AAResults *AA=nullptr) const
Return true if the instruction is trivially rematerializable, meaning it has no side effects and requ...
Definition: TargetInstrInfo.h:124
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
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
DenseMapInfo.h
MachineFunction.h
MachineBlockFrequencyInfo.h
llvm::RegAllocScore::loadStoreCounts
double loadStoreCounts() const
Definition: RegAllocScore.h:44
ilist_iterator.h
SetVector.h
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1225
llvm::RegAllocScore::operator==
bool operator==(const RegAllocScore &Other) const
Definition: RegAllocScore.cpp:50