LLVM 24.0.0git
SIPreAllocateWWMRegs.cpp
Go to the documentation of this file.
1//===- SIPreAllocateWWMRegs.cpp - WWM Register Pre-allocation -------------===//
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//
9/// \file
10/// Pass to pre-allocated WWM registers
11//
12//===----------------------------------------------------------------------===//
13
15#include "AMDGPU.h"
16#include "GCNSubtarget.h"
27
28using namespace llvm;
29
30#define DEBUG_TYPE "si-pre-allocate-wwm-regs"
31
32static cl::opt<bool>
33 EnablePreallocateSGPRSpillVGPRs("amdgpu-prealloc-sgpr-spill-vgprs",
34 cl::init(false), cl::Hidden);
35
38 MF.getFunction().hasFnAttribute("amdgpu-prealloc-sgpr-spill-vgprs");
39}
40
41namespace {
42
43class SIPreAllocateWWMRegs {
44private:
45 const SIInstrInfo *TII;
46 const SIRegisterInfo *TRI;
48 LiveIntervals *LIS;
50 VirtRegMap *VRM;
52
53 std::vector<unsigned> RegsToRewrite;
54#ifndef NDEBUG
55 void printWWMInfo(const MachineInstr &MI);
56#endif
57 bool processDef(MachineOperand &MO);
58 void rewriteRegs(MachineFunction &MF);
59
60public:
61 SIPreAllocateWWMRegs(LiveIntervals *LIS, LiveRegMatrix *Matrix,
63 : LIS(LIS), Matrix(Matrix), VRM(VRM), RCI(RCI) {}
64 bool run(MachineFunction &MF);
65};
66
67class SIPreAllocateWWMRegsLegacy : public MachineFunctionPass {
68public:
69 static char ID;
70
71 SIPreAllocateWWMRegsLegacy() : MachineFunctionPass(ID) {}
72
73 bool runOnMachineFunction(MachineFunction &MF) override;
74
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
76 AU.addRequired<LiveIntervalsWrapperPass>();
77 AU.addRequired<VirtRegMapWrapperLegacy>();
78 AU.addRequired<LiveRegMatrixWrapperLegacy>();
79 AU.addRequired<MachineRegisterClassInfoWrapperPass>();
80 AU.setPreservesAll();
82 }
83};
84
85} // End anonymous namespace.
86
87INITIALIZE_PASS_BEGIN(SIPreAllocateWWMRegsLegacy, DEBUG_TYPE,
88 "SI Pre-allocate WWM Registers", false, false)
93INITIALIZE_PASS_END(SIPreAllocateWWMRegsLegacy, DEBUG_TYPE,
94 "SI Pre-allocate WWM Registers", false, false)
95
96char SIPreAllocateWWMRegsLegacy::ID = 0;
97
98char &llvm::SIPreAllocateWWMRegsLegacyID = SIPreAllocateWWMRegsLegacy::ID;
99
101 return new SIPreAllocateWWMRegsLegacy();
102}
103
104bool SIPreAllocateWWMRegs::processDef(MachineOperand &MO) {
105 Register Reg = MO.getReg();
106 if (Reg.isPhysical())
107 return false;
108
110 return false;
111
112 if (VRM->hasPhys(Reg))
113 return false;
114
115 LiveInterval &LI = LIS->getInterval(Reg);
116
117 for (MCRegister PhysReg : RCI.getOrder(MRI->getRegClass(Reg))) {
118 if (!MRI->isPhysRegUsed(PhysReg, /*SkipRegMaskTest=*/true) &&
119 Matrix->checkInterference(LI, PhysReg) == LiveRegMatrix::IK_Free) {
120 Matrix->assign(LI, PhysReg);
121 assert(PhysReg != 0);
122 RegsToRewrite.push_back(Reg);
123 return true;
124 }
125 }
126
127 llvm_unreachable("physreg not found for WWM expression");
128}
129
130void SIPreAllocateWWMRegs::rewriteRegs(MachineFunction &MF) {
131 for (MachineBasicBlock &MBB : MF) {
132 for (MachineInstr &MI : MBB) {
133 for (MachineOperand &MO : MI.operands()) {
134 if (!MO.isReg())
135 continue;
136
137 const Register VirtReg = MO.getReg();
138 if (VirtReg.isPhysical())
139 continue;
140
141 if (!VirtReg.isValid())
142 continue;
143
144 if (!VRM->hasPhys(VirtReg))
145 continue;
146
147 Register PhysReg = VRM->getPhys(VirtReg);
148 const unsigned SubReg = MO.getSubReg();
149 if (SubReg != 0) {
150 PhysReg = TRI->getSubReg(PhysReg, SubReg);
151 MO.setSubReg(0);
152 }
153
154 MO.setReg(PhysReg);
155 MO.setIsRenamable(false);
156 }
157 }
158 }
159
160 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
161
162 for (unsigned Reg : RegsToRewrite) {
163 const Register PhysReg = VRM->getPhys(Reg);
164 assert(PhysReg != 0);
165
166 LiveInterval &LI = LIS->getInterval(Reg);
167 Matrix->unassign(LI, /*ClearAllReferencingSegments=*/true);
168 LIS->removeInterval(Reg);
169
170 MFI->reserveWWMRegister(PhysReg);
171 }
172
173 RegsToRewrite.clear();
174
175 // Update the set of reserved registers to include WWM ones
176 // without unnecessarily invalidating RegClassInfo.
177 MRI->freezeReservedRegs();
179}
180
181#ifndef NDEBUG
183SIPreAllocateWWMRegs::printWWMInfo(const MachineInstr &MI) {
184
185 unsigned Opc = MI.getOpcode();
186
187 if (Opc == AMDGPU::ENTER_STRICT_WWM || Opc == AMDGPU::ENTER_STRICT_WQM) {
188 dbgs() << "Entering ";
189 } else {
190 assert(Opc == AMDGPU::EXIT_STRICT_WWM || Opc == AMDGPU::EXIT_STRICT_WQM);
191 dbgs() << "Exiting ";
192 }
193
194 if (Opc == AMDGPU::ENTER_STRICT_WWM || Opc == AMDGPU::EXIT_STRICT_WWM) {
195 dbgs() << "Strict WWM ";
196 } else {
197 assert(Opc == AMDGPU::ENTER_STRICT_WQM || Opc == AMDGPU::EXIT_STRICT_WQM);
198 dbgs() << "Strict WQM ";
199 }
200
201 dbgs() << "region: " << MI;
202}
203
204#endif
205
206bool SIPreAllocateWWMRegsLegacy::runOnMachineFunction(MachineFunction &MF) {
207 auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
208 auto *Matrix = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
209 auto *VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
210 auto &RCI = getAnalysis<MachineRegisterClassInfoWrapperPass>().getRCI();
211 return SIPreAllocateWWMRegs(LIS, Matrix, VRM, RCI).run(MF);
212}
213
214bool SIPreAllocateWWMRegs::run(MachineFunction &MF) {
215 LLVM_DEBUG(dbgs() << "SIPreAllocateWWMRegs: function " << MF.getName() << "\n");
216
217 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
218
219 TII = ST.getInstrInfo();
220 TRI = &TII->getRegisterInfo();
221 MRI = &MF.getRegInfo();
222
223 bool PreallocateSGPRSpillVGPRs = isPreallocateSGPRSpillVGPRsEnabled(MF);
224
225 bool RegsAssigned = false;
226
227 // We use a reverse post-order traversal of the control-flow graph to
228 // guarantee that we visit definitions in dominance order. Since WWM
229 // expressions are guaranteed to never involve phi nodes, and we can only
230 // escape WWM through the special WWM instruction, this means that this is a
231 // perfect elimination order, so we can never do any better.
232 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
233
234 for (MachineBasicBlock *MBB : RPOT) {
235 bool InWWM = false;
236 for (MachineInstr &MI : *MBB) {
237 if (MI.getOpcode() == AMDGPU::SI_SPILL_S32_TO_VGPR) {
238 if (PreallocateSGPRSpillVGPRs)
239 RegsAssigned |= processDef(MI.getOperand(0));
240 continue;
241 }
242
243 if (MI.getOpcode() == AMDGPU::ENTER_STRICT_WWM ||
244 MI.getOpcode() == AMDGPU::ENTER_STRICT_WQM) {
245 LLVM_DEBUG(printWWMInfo(MI));
246 InWWM = true;
247 continue;
248 }
249
250 if (MI.getOpcode() == AMDGPU::EXIT_STRICT_WWM ||
251 MI.getOpcode() == AMDGPU::EXIT_STRICT_WQM) {
252 LLVM_DEBUG(printWWMInfo(MI));
253 InWWM = false;
254 }
255
256 if (!InWWM)
257 continue;
258
259 LLVM_DEBUG(dbgs() << "Processing " << MI);
260
261 for (MachineOperand &DefOpnd : MI.defs()) {
262 RegsAssigned |= processDef(DefOpnd);
263 }
264 }
265 }
266
267 if (!RegsAssigned)
268 return false;
269
270 rewriteRegs(MF);
271 return true;
272}
273
274PreservedAnalyses
277 auto *LIS = &MFAM.getResult<LiveIntervalsAnalysis>(MF);
278 auto *Matrix = &MFAM.getResult<LiveRegMatrixAnalysis>(MF);
279 auto *VRM = &MFAM.getResult<VirtRegMapAnalysis>(MF);
280 auto &RCI = MFAM.getResult<MachineRegisterClassAnalysis>(MF);
281 SIPreAllocateWWMRegs(LIS, Matrix, VRM, RCI).run(MF);
282 return PreservedAnalyses::all();
283}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides AMDGPU specific target descriptions.
MachineBasicBlock & MBB
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Live Register Matrix
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static cl::opt< bool > EnablePreallocateSGPRSpillVGPRs("amdgpu-prealloc-sgpr-spill-vgprs", cl::init(false), cl::Hidden)
#define LLVM_DEBUG(...)
Definition Debug.h:119
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
const HexagonRegisterInfo & getRegisterInfo() const
LiveInterval - This class represents the liveness of a register, or stack slot.
LiveInterval & getInterval(Register Reg)
void removeInterval(Register Reg)
Interval removal.
@ IK_Free
No interference, go ahead and assign.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
LLVM_ABI void setIsRenamable(bool Val=true)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
Result run(MachineFunction &, MachineFunctionAnalysisManager &)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI void freezeReservedRegs()
freezeReservedRegs - Called by the register allocator to freeze the set of reserved registers before ...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
const BitVector & getReservedRegs() const
getReservedRegs - Returns a reference to the frozen set of reserved registers.
LLVM_ABI bool isPhysRegUsed(MCRegister PhysReg, bool SkipRegMaskTest=false) const
Return true if the specified register is modified or read in this function.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
LLVM_ABI void updateReservedRegs(const BitVector &ReservedInput)
Update cached register class information using ReservedInput, MRI's current reserved-register set.
ArrayRef< MCPhysReg > getOrder(const TargetRegisterClass *RC) const
getOrder - Returns the preferred allocation order for RC.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static bool hasVGPRs(const TargetRegisterClass *RC)
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition VirtRegMap.h:87
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
DXILDebugInfoMap run(Module &M)
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createSIPreAllocateWWMRegsLegacyPass()
char & SIPreAllocateWWMRegsLegacyID
bool isPreallocateSGPRSpillVGPRsEnabled(const MachineFunction &MF)