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