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 
14 #include "AMDGPU.h"
15 #include "GCNSubtarget.h"
16 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
17 #include "SIMachineFunctionInfo.h"
18 #include "llvm/ADT/PostOrderIterator.h"
19 #include "llvm/CodeGen/LiveIntervals.h"
20 #include "llvm/CodeGen/LiveRegMatrix.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/InitializePasses.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "si-pre-allocate-wwm-regs"
27 
28 namespace {
29 
30 class SIPreAllocateWWMRegs : public MachineFunctionPass {
31 private:
32   const SIInstrInfo *TII;
33   const SIRegisterInfo *TRI;
34   MachineRegisterInfo *MRI;
35   LiveIntervals *LIS;
36   LiveRegMatrix *Matrix;
37   VirtRegMap *VRM;
38   RegisterClassInfo RegClassInfo;
39 
40   std::vector<unsigned> RegsToRewrite;
41 #ifndef NDEBUG
42   void printWWMInfo(const MachineInstr &MI);
43 #endif
44 
45 public:
46   static char ID;
47 
48   SIPreAllocateWWMRegs() : MachineFunctionPass(ID) {
49     initializeSIPreAllocateWWMRegsPass(*PassRegistry::getPassRegistry());
50   }
51 
52   bool runOnMachineFunction(MachineFunction &MF) override;
53 
54   void getAnalysisUsage(AnalysisUsage &AU) const override {
55     AU.addRequired<LiveIntervals>();
56     AU.addPreserved<LiveIntervals>();
57     AU.addRequired<VirtRegMap>();
58     AU.addRequired<LiveRegMatrix>();
59     AU.addPreserved<SlotIndexes>();
60     AU.setPreservesCFG();
61     MachineFunctionPass::getAnalysisUsage(AU);
62   }
63 
64 private:
65   bool processDef(MachineOperand &MO);
66   void rewriteRegs(MachineFunction &MF);
67 };
68 
69 } // End anonymous namespace.
70 
71 INITIALIZE_PASS_BEGIN(SIPreAllocateWWMRegs, DEBUG_TYPE,
72                 "SI Pre-allocate WWM Registers", false, false)
73 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
74 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
75 INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
76 INITIALIZE_PASS_END(SIPreAllocateWWMRegs, DEBUG_TYPE,
77                 "SI Pre-allocate WWM Registers", false, false)
78 
79 char SIPreAllocateWWMRegs::ID = 0;
80 
81 char &llvm::SIPreAllocateWWMRegsID = SIPreAllocateWWMRegs::ID;
82 
83 FunctionPass *llvm::createSIPreAllocateWWMRegsPass() {
84   return new SIPreAllocateWWMRegs();
85 }
86 
87 bool SIPreAllocateWWMRegs::processDef(MachineOperand &MO) {
88   if (!MO.isReg())
89     return false;
90 
91   Register Reg = MO.getReg();
92   if (Reg.isPhysical())
93     return false;
94 
95   if (!TRI->isVGPR(*MRI, Reg))
96     return false;
97 
98   if (VRM->hasPhys(Reg))
99     return false;
100 
101   LiveInterval &LI = LIS->getInterval(Reg);
102 
103   for (MCRegister PhysReg : RegClassInfo.getOrder(MRI->getRegClass(Reg))) {
104     if (!MRI->isPhysRegUsed(PhysReg) &&
105         Matrix->checkInterference(LI, PhysReg) == LiveRegMatrix::IK_Free) {
106       Matrix->assign(LI, PhysReg);
107       assert(PhysReg != 0);
108       RegsToRewrite.push_back(Reg);
109       return true;
110     }
111   }
112 
113   llvm_unreachable("physreg not found for WWM expression");
114   return false;
115 }
116 
117 void SIPreAllocateWWMRegs::rewriteRegs(MachineFunction &MF) {
118   for (MachineBasicBlock &MBB : MF) {
119     for (MachineInstr &MI : MBB) {
120       for (MachineOperand &MO : MI.operands()) {
121         if (!MO.isReg())
122           continue;
123 
124         const Register VirtReg = MO.getReg();
125         if (VirtReg.isPhysical())
126           continue;
127 
128         if (!VRM->hasPhys(VirtReg))
129           continue;
130 
131         Register PhysReg = VRM->getPhys(VirtReg);
132         const unsigned SubReg = MO.getSubReg();
133         if (SubReg != 0) {
134           PhysReg = TRI->getSubReg(PhysReg, SubReg);
135           MO.setSubReg(0);
136         }
137 
138         MO.setReg(PhysReg);
139         MO.setIsRenamable(false);
140       }
141     }
142   }
143 
144   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
145 
146   for (unsigned Reg : RegsToRewrite) {
147     LIS->removeInterval(Reg);
148 
149     const Register PhysReg = VRM->getPhys(Reg);
150     assert(PhysReg != 0);
151     MFI->ReserveWWMRegister(PhysReg);
152   }
153 
154   RegsToRewrite.clear();
155 
156   // Update the set of reserved registers to include WWM ones.
157   MRI->freezeReservedRegs(MF);
158 }
159 
160 #ifndef NDEBUG
161 LLVM_DUMP_METHOD void
162 SIPreAllocateWWMRegs::printWWMInfo(const MachineInstr &MI) {
163 
164   unsigned Opc = MI.getOpcode();
165 
166   if (Opc == AMDGPU::ENTER_STRICT_WWM || Opc == AMDGPU::ENTER_STRICT_WQM) {
167     dbgs() << "Entering ";
168   } else {
169     assert(Opc == AMDGPU::EXIT_STRICT_WWM || Opc == AMDGPU::EXIT_STRICT_WQM);
170     dbgs() << "Exiting ";
171   }
172 
173   if (Opc == AMDGPU::ENTER_STRICT_WWM || Opc == AMDGPU::EXIT_STRICT_WWM) {
174     dbgs() << "Strict WWM ";
175   } else {
176     assert(Opc == AMDGPU::ENTER_STRICT_WQM || Opc == AMDGPU::EXIT_STRICT_WQM);
177     dbgs() << "Strict WQM ";
178   }
179 
180   dbgs() << "region: " << MI;
181 }
182 
183 #endif
184 
185 bool SIPreAllocateWWMRegs::runOnMachineFunction(MachineFunction &MF) {
186   LLVM_DEBUG(dbgs() << "SIPreAllocateWWMRegs: function " << MF.getName() << "\n");
187 
188   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
189 
190   TII = ST.getInstrInfo();
191   TRI = &TII->getRegisterInfo();
192   MRI = &MF.getRegInfo();
193 
194   LIS = &getAnalysis<LiveIntervals>();
195   Matrix = &getAnalysis<LiveRegMatrix>();
196   VRM = &getAnalysis<VirtRegMap>();
197 
198   RegClassInfo.runOnMachineFunction(MF);
199 
200   bool RegsAssigned = false;
201 
202   // We use a reverse post-order traversal of the control-flow graph to
203   // guarantee that we visit definitions in dominance order. Since WWM
204   // expressions are guaranteed to never involve phi nodes, and we can only
205   // escape WWM through the special WWM instruction, this means that this is a
206   // perfect elimination order, so we can never do any better.
207   ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
208 
209   for (MachineBasicBlock *MBB : RPOT) {
210     bool InWWM = false;
211     for (MachineInstr &MI : *MBB) {
212       if (MI.getOpcode() == AMDGPU::V_SET_INACTIVE_B32 ||
213           MI.getOpcode() == AMDGPU::V_SET_INACTIVE_B64)
214         RegsAssigned |= processDef(MI.getOperand(0));
215 
216       if (MI.getOpcode() == AMDGPU::ENTER_STRICT_WWM ||
217           MI.getOpcode() == AMDGPU::ENTER_STRICT_WQM) {
218         LLVM_DEBUG(printWWMInfo(MI));
219         InWWM = true;
220         continue;
221       }
222 
223       if (MI.getOpcode() == AMDGPU::EXIT_STRICT_WWM ||
224           MI.getOpcode() == AMDGPU::EXIT_STRICT_WQM) {
225         LLVM_DEBUG(printWWMInfo(MI));
226         InWWM = false;
227       }
228 
229       if (!InWWM)
230         continue;
231 
232       LLVM_DEBUG(dbgs() << "Processing " << MI);
233 
234       for (MachineOperand &DefOpnd : MI.defs()) {
235         RegsAssigned |= processDef(DefOpnd);
236       }
237     }
238   }
239 
240   if (!RegsAssigned)
241     return false;
242 
243   rewriteRegs(MF);
244   return true;
245 }
246