xref: /llvm-project/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp (revision 75c98c365b8b4c42e9ba84351b0aef8f73f69019)
1 //===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements hazard recognizers for scheduling on GCN processors.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "GCNHazardRecognizer.h"
15 #include "AMDGPUSubtarget.h"
16 #include "SIDefines.h"
17 #include "SIInstrInfo.h"
18 #include "SIRegisterInfo.h"
19 #include "Utils/AMDGPUBaseInfo.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/ScheduleDAG.h"
25 #include "llvm/MC/MCInstrDesc.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <limits>
30 #include <set>
31 #include <vector>
32 
33 using namespace llvm;
34 
35 //===----------------------------------------------------------------------===//
36 // Hazard Recoginizer Implementation
37 //===----------------------------------------------------------------------===//
38 
39 GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) :
40   CurrCycleInstr(nullptr),
41   MF(MF),
42   ST(MF.getSubtarget<SISubtarget>()),
43   TII(*ST.getInstrInfo()) {
44   MaxLookAhead = 5;
45 }
46 
47 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
48   EmitInstruction(SU->getInstr());
49 }
50 
51 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
52   CurrCycleInstr = MI;
53 }
54 
55 static bool isDivFMas(unsigned Opcode) {
56   return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
57 }
58 
59 static bool isSGetReg(unsigned Opcode) {
60   return Opcode == AMDGPU::S_GETREG_B32;
61 }
62 
63 static bool isSSetReg(unsigned Opcode) {
64   return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
65 }
66 
67 static bool isRWLane(unsigned Opcode) {
68   return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
69 }
70 
71 static bool isRFE(unsigned Opcode) {
72   return Opcode == AMDGPU::S_RFE_B64;
73 }
74 
75 static bool isSMovRel(unsigned Opcode) {
76   switch (Opcode) {
77   case AMDGPU::S_MOVRELS_B32:
78   case AMDGPU::S_MOVRELS_B64:
79   case AMDGPU::S_MOVRELD_B32:
80   case AMDGPU::S_MOVRELD_B64:
81     return true;
82   default:
83     return false;
84   }
85 }
86 
87 static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
88   const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
89                                                      AMDGPU::OpName::simm16);
90   return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
91 }
92 
93 ScheduleHazardRecognizer::HazardType
94 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
95   MachineInstr *MI = SU->getInstr();
96 
97   if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
98     return NoopHazard;
99 
100   if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0)
101     return NoopHazard;
102 
103   if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
104     return NoopHazard;
105 
106   if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
107     return NoopHazard;
108 
109   if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
110     return NoopHazard;
111 
112   if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
113     return NoopHazard;
114 
115   if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
116     return NoopHazard;
117 
118   if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
119     return NoopHazard;
120 
121   if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
122     return NoopHazard;
123 
124   if ((TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
125       checkReadM0Hazards(MI) > 0)
126     return NoopHazard;
127 
128   if (checkAnyInstHazards(MI) > 0)
129     return NoopHazard;
130 
131   return NoHazard;
132 }
133 
134 unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
135   return PreEmitNoops(SU->getInstr());
136 }
137 
138 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
139   int WaitStates = std::max(0, checkAnyInstHazards(MI));
140 
141   if (SIInstrInfo::isSMRD(*MI))
142     return std::max(WaitStates, checkSMRDHazards(MI));
143 
144   if (SIInstrInfo::isVALU(*MI)) {
145       WaitStates = std::max(WaitStates, checkVALUHazards(MI));
146 
147     if (SIInstrInfo::isVMEM(*MI))
148       WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
149 
150     if (SIInstrInfo::isDPP(*MI))
151       WaitStates = std::max(WaitStates, checkDPPHazards(MI));
152 
153     if (isDivFMas(MI->getOpcode()))
154       WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
155 
156     if (isRWLane(MI->getOpcode()))
157       WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
158 
159     if (TII.isVINTRP(*MI))
160       WaitStates = std::max(WaitStates, checkReadM0Hazards(MI));
161 
162     return WaitStates;
163   }
164 
165   if (isSGetReg(MI->getOpcode()))
166     return std::max(WaitStates, checkGetRegHazards(MI));
167 
168   if (isSSetReg(MI->getOpcode()))
169     return std::max(WaitStates, checkSetRegHazards(MI));
170 
171   if (isRFE(MI->getOpcode()))
172     return std::max(WaitStates, checkRFEHazards(MI));
173 
174   if (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()))
175     return std::max(WaitStates, checkReadM0Hazards(MI));
176 
177   return WaitStates;
178 }
179 
180 void GCNHazardRecognizer::EmitNoop() {
181   EmittedInstrs.push_front(nullptr);
182 }
183 
184 void GCNHazardRecognizer::AdvanceCycle() {
185   // When the scheduler detects a stall, it will call AdvanceCycle() without
186   // emitting any instructions.
187   if (!CurrCycleInstr)
188     return;
189 
190   unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
191 
192   // Keep track of emitted instructions
193   EmittedInstrs.push_front(CurrCycleInstr);
194 
195   // Add a nullptr for each additional wait state after the first.  Make sure
196   // not to add more than getMaxLookAhead() items to the list, since we
197   // truncate the list to that size right after this loop.
198   for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
199        i < e; ++i) {
200     EmittedInstrs.push_front(nullptr);
201   }
202 
203   // getMaxLookahead() is the largest number of wait states we will ever need
204   // to insert, so there is no point in keeping track of more than that many
205   // wait states.
206   EmittedInstrs.resize(getMaxLookAhead());
207 
208   CurrCycleInstr = nullptr;
209 }
210 
211 void GCNHazardRecognizer::RecedeCycle() {
212   llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
213 }
214 
215 //===----------------------------------------------------------------------===//
216 // Helper Functions
217 //===----------------------------------------------------------------------===//
218 
219 int GCNHazardRecognizer::getWaitStatesSince(
220     function_ref<bool(MachineInstr *)> IsHazard) {
221   int WaitStates = 0;
222   for (MachineInstr *MI : EmittedInstrs) {
223     if (MI) {
224       if (IsHazard(MI))
225         return WaitStates;
226 
227       unsigned Opcode = MI->getOpcode();
228       if (Opcode == AMDGPU::DBG_VALUE || Opcode == AMDGPU::IMPLICIT_DEF)
229         continue;
230     }
231     ++WaitStates;
232   }
233   return std::numeric_limits<int>::max();
234 }
235 
236 int GCNHazardRecognizer::getWaitStatesSinceDef(
237     unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
238   const SIRegisterInfo *TRI = ST.getRegisterInfo();
239 
240   auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
241     return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
242   };
243 
244   return getWaitStatesSince(IsHazardFn);
245 }
246 
247 int GCNHazardRecognizer::getWaitStatesSinceSetReg(
248     function_ref<bool(MachineInstr *)> IsHazard) {
249   auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
250     return isSSetReg(MI->getOpcode()) && IsHazard(MI);
251   };
252 
253   return getWaitStatesSince(IsHazardFn);
254 }
255 
256 //===----------------------------------------------------------------------===//
257 // No-op Hazard Detection
258 //===----------------------------------------------------------------------===//
259 
260 static void addRegsToSet(iterator_range<MachineInstr::const_mop_iterator> Ops,
261                          std::set<unsigned> &Set) {
262   for (const MachineOperand &Op : Ops) {
263     if (Op.isReg())
264       Set.insert(Op.getReg());
265   }
266 }
267 
268 int GCNHazardRecognizer::checkSMEMSoftClauseHazards(MachineInstr *SMEM) {
269   // SMEM soft clause are only present on VI+
270   if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
271     return 0;
272 
273   // A soft-clause is any group of consecutive SMEM instructions.  The
274   // instructions in this group may return out of order and/or may be
275   // replayed (i.e. the same instruction issued more than once).
276   //
277   // In order to handle these situations correctly we need to make sure
278   // that when a clause has more than one instruction, no instruction in the
279   // clause writes to a register that is read another instruction in the clause
280   // (including itself). If we encounter this situaion, we need to break the
281   // clause by inserting a non SMEM instruction.
282 
283   std::set<unsigned> ClauseDefs;
284   std::set<unsigned> ClauseUses;
285 
286   for (MachineInstr *MI : EmittedInstrs) {
287 
288     // When we hit a non-SMEM instruction then we have passed the start of the
289     // clause and we can stop.
290     if (!MI || !SIInstrInfo::isSMRD(*MI))
291       break;
292 
293     addRegsToSet(MI->defs(), ClauseDefs);
294     addRegsToSet(MI->uses(), ClauseUses);
295   }
296 
297   if (ClauseDefs.empty())
298     return 0;
299 
300   // FIXME: When we support stores, we need to make sure not to put loads and
301   // stores in the same clause if they use the same address.  For now, just
302   // start a new clause whenever we see a store.
303   if (SMEM->mayStore())
304     return 1;
305 
306   addRegsToSet(SMEM->defs(), ClauseDefs);
307   addRegsToSet(SMEM->uses(), ClauseUses);
308 
309   std::vector<unsigned> Result(std::max(ClauseDefs.size(), ClauseUses.size()));
310   std::vector<unsigned>::iterator End;
311 
312   End = std::set_intersection(ClauseDefs.begin(), ClauseDefs.end(),
313                               ClauseUses.begin(), ClauseUses.end(), Result.begin());
314 
315   // If the set of defs and uses intersect then we cannot add this instruction
316   // to the clause, so we have a hazard.
317   if (End != Result.begin())
318     return 1;
319 
320   return 0;
321 }
322 
323 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
324   const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
325   int WaitStatesNeeded = 0;
326 
327   WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD);
328 
329   // This SMRD hazard only affects SI.
330   if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS)
331     return WaitStatesNeeded;
332 
333   // A read of an SGPR by SMRD instruction requires 4 wait states when the
334   // SGPR was written by a VALU instruction.
335   int SmrdSgprWaitStates = 4;
336   auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
337 
338   for (const MachineOperand &Use : SMRD->uses()) {
339     if (!Use.isReg())
340       continue;
341     int WaitStatesNeededForUse =
342         SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
343     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
344   }
345   return WaitStatesNeeded;
346 }
347 
348 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
349   const SIInstrInfo *TII = ST.getInstrInfo();
350 
351   if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
352     return 0;
353 
354   const SIRegisterInfo &TRI = TII->getRegisterInfo();
355 
356   // A read of an SGPR by a VMEM instruction requires 5 wait states when the
357   // SGPR was written by a VALU Instruction.
358   int VmemSgprWaitStates = 5;
359   int WaitStatesNeeded = 0;
360   auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
361 
362   for (const MachineOperand &Use : VMEM->uses()) {
363     if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
364       continue;
365 
366     int WaitStatesNeededForUse =
367         VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
368     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
369   }
370   return WaitStatesNeeded;
371 }
372 
373 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
374   const SIRegisterInfo *TRI = ST.getRegisterInfo();
375   const SIInstrInfo *TII = ST.getInstrInfo();
376 
377   // Check for DPP VGPR read after VALU VGPR write and EXEC write.
378   int DppVgprWaitStates = 2;
379   int DppExecWaitStates = 5;
380   int WaitStatesNeeded = 0;
381   auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
382 
383   for (const MachineOperand &Use : DPP->uses()) {
384     if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
385       continue;
386     int WaitStatesNeededForUse =
387         DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
388     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
389   }
390 
391   WaitStatesNeeded = std::max(
392       WaitStatesNeeded,
393       DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
394 
395   return WaitStatesNeeded;
396 }
397 
398 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
399   const SIInstrInfo *TII = ST.getInstrInfo();
400 
401   // v_div_fmas requires 4 wait states after a write to vcc from a VALU
402   // instruction.
403   const int DivFMasWaitStates = 4;
404   auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
405   int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
406 
407   return DivFMasWaitStates - WaitStatesNeeded;
408 }
409 
410 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
411   const SIInstrInfo *TII = ST.getInstrInfo();
412   unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
413 
414   const int GetRegWaitStates = 2;
415   auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
416     return GetRegHWReg == getHWReg(TII, *MI);
417   };
418   int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
419 
420   return GetRegWaitStates - WaitStatesNeeded;
421 }
422 
423 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
424   const SIInstrInfo *TII = ST.getInstrInfo();
425   unsigned HWReg = getHWReg(TII, *SetRegInstr);
426 
427   const int SetRegWaitStates =
428       ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
429   auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
430     return HWReg == getHWReg(TII, *MI);
431   };
432   int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
433   return SetRegWaitStates - WaitStatesNeeded;
434 }
435 
436 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
437   if (!MI.mayStore())
438     return -1;
439 
440   const SIInstrInfo *TII = ST.getInstrInfo();
441   unsigned Opcode = MI.getOpcode();
442   const MCInstrDesc &Desc = MI.getDesc();
443 
444   int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
445   int VDataRCID = -1;
446   if (VDataIdx != -1)
447     VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
448 
449   if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
450     // There is no hazard if the instruction does not use vector regs
451     // (like wbinvl1)
452     if (VDataIdx == -1)
453       return -1;
454     // For MUBUF/MTBUF instructions this hazard only exists if the
455     // instruction is not using a register in the soffset field.
456     const MachineOperand *SOffset =
457         TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
458     // If we have no soffset operand, then assume this field has been
459     // hardcoded to zero.
460     if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
461         (!SOffset || !SOffset->isReg()))
462       return VDataIdx;
463   }
464 
465   // MIMG instructions create a hazard if they don't use a 256-bit T# and
466   // the store size is greater than 8 bytes and they have more than two bits
467   // of their dmask set.
468   // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
469   if (TII->isMIMG(MI)) {
470     int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
471     assert(SRsrcIdx != -1 &&
472            AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
473     (void)SRsrcIdx;
474   }
475 
476   if (TII->isFLAT(MI)) {
477     int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
478     if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
479       return DataIdx;
480   }
481 
482   return -1;
483 }
484 
485 int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
486   // This checks for the hazard where VMEM instructions that store more than
487   // 8 bytes can have there store data over written by the next instruction.
488   if (!ST.has12DWordStoreHazard())
489     return 0;
490 
491   const SIRegisterInfo *TRI = ST.getRegisterInfo();
492   const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo();
493 
494   const int VALUWaitStates = 1;
495   int WaitStatesNeeded = 0;
496 
497   for (const MachineOperand &Def : VALU->defs()) {
498     if (!TRI->isVGPR(MRI, Def.getReg()))
499       continue;
500     unsigned Reg = Def.getReg();
501     auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
502       int DataIdx = createsVALUHazard(*MI);
503       return DataIdx >= 0 &&
504              TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
505     };
506     int WaitStatesNeededForDef =
507         VALUWaitStates - getWaitStatesSince(IsHazardFn);
508     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
509   }
510   return WaitStatesNeeded;
511 }
512 
513 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
514   const SIInstrInfo *TII = ST.getInstrInfo();
515   const SIRegisterInfo *TRI = ST.getRegisterInfo();
516   const MachineRegisterInfo &MRI =
517       RWLane->getParent()->getParent()->getRegInfo();
518 
519   const MachineOperand *LaneSelectOp =
520       TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
521 
522   if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
523     return 0;
524 
525   unsigned LaneSelectReg = LaneSelectOp->getReg();
526   auto IsHazardFn = [TII] (MachineInstr *MI) {
527     return TII->isVALU(*MI);
528   };
529 
530   const int RWLaneWaitStates = 4;
531   int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
532   return RWLaneWaitStates - WaitStatesSince;
533 }
534 
535 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
536   if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
537     return 0;
538 
539   const SIInstrInfo *TII = ST.getInstrInfo();
540 
541   const int RFEWaitStates = 1;
542 
543   auto IsHazardFn = [TII] (MachineInstr *MI) {
544     return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
545   };
546   int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
547   return RFEWaitStates - WaitStatesNeeded;
548 }
549 
550 int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
551   if (MI->isDebugValue())
552     return 0;
553 
554   const SIRegisterInfo *TRI = ST.getRegisterInfo();
555   if (!ST.hasSMovFedHazard())
556     return 0;
557 
558   // Check for any instruction reading an SGPR after a write from
559   // s_mov_fed_b32.
560   int MovFedWaitStates = 1;
561   int WaitStatesNeeded = 0;
562 
563   for (const MachineOperand &Use : MI->uses()) {
564     if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
565       continue;
566     auto IsHazardFn = [] (MachineInstr *MI) {
567       return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
568     };
569     int WaitStatesNeededForUse =
570         MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
571     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
572   }
573 
574   return WaitStatesNeeded;
575 }
576 
577 int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
578   if (!ST.hasReadM0Hazard())
579     return 0;
580 
581   const SIInstrInfo *TII = ST.getInstrInfo();
582   int SMovRelWaitStates = 1;
583   auto IsHazardFn = [TII] (MachineInstr *MI) {
584     return TII->isSALU(*MI);
585   };
586   return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
587 }
588