1 //===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements hazard recognizers for scheduling on GCN processors.
12 //===----------------------------------------------------------------------===//
14 #include "GCNHazardRecognizer.h"
15 #include "AMDGPUSubtarget.h"
16 #include "SIDefines.h"
17 #include "SIInstrInfo.h"
18 #include "SIRegisterInfo.h"
19 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
20 #include "Utils/AMDGPUBaseInfo.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/CodeGen/ScheduleDAG.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/Support/ErrorHandling.h"
36 //===----------------------------------------------------------------------===//
37 // Hazard Recoginizer Implementation
38 //===----------------------------------------------------------------------===//
40 GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) :
41 CurrCycleInstr(nullptr),
43 ST(MF.getSubtarget<GCNSubtarget>()),
44 TII(*ST.getInstrInfo()),
45 TRI(TII.getRegisterInfo()),
46 ClauseUses(TRI.getNumRegUnits()),
47 ClauseDefs(TRI.getNumRegUnits()) {
51 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
52 EmitInstruction(SU->getInstr());
55 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
59 static bool isDivFMas(unsigned Opcode) {
60 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
63 static bool isSGetReg(unsigned Opcode) {
64 return Opcode == AMDGPU::S_GETREG_B32;
67 static bool isSSetReg(unsigned Opcode) {
68 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
71 static bool isRWLane(unsigned Opcode) {
72 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
75 static bool isRFE(unsigned Opcode) {
76 return Opcode == AMDGPU::S_RFE_B64;
79 static bool isSMovRel(unsigned Opcode) {
81 case AMDGPU::S_MOVRELS_B32:
82 case AMDGPU::S_MOVRELS_B64:
83 case AMDGPU::S_MOVRELD_B32:
84 case AMDGPU::S_MOVRELD_B64:
91 static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII,
92 const MachineInstr &MI) {
93 if (TII.isAlwaysGDS(MI.getOpcode()))
96 switch (MI.getOpcode()) {
97 case AMDGPU::S_SENDMSG:
98 case AMDGPU::S_SENDMSGHALT:
99 case AMDGPU::S_TTRACEDATA:
101 // These DS opcodes don't support GDS.
103 case AMDGPU::DS_PERMUTE_B32:
104 case AMDGPU::DS_BPERMUTE_B32:
107 if (TII.isDS(MI.getOpcode())) {
108 int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
109 AMDGPU::OpName::gds);
110 if (MI.getOperand(GDS).getImm())
117 static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
118 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
119 AMDGPU::OpName::simm16);
120 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
123 ScheduleHazardRecognizer::HazardType
124 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
125 MachineInstr *MI = SU->getInstr();
127 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
130 // FIXME: Should flat be considered vmem?
131 if ((SIInstrInfo::isVMEM(*MI) ||
132 SIInstrInfo::isFLAT(*MI))
133 && checkVMEMHazards(MI) > 0)
136 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
139 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
142 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
145 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
148 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
151 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
154 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
157 if (ST.hasReadM0MovRelInterpHazard() &&
158 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
159 checkReadM0Hazards(MI) > 0)
162 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) &&
163 checkReadM0Hazards(MI) > 0)
166 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0)
169 if (checkAnyInstHazards(MI) > 0)
175 unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
176 return PreEmitNoops(SU->getInstr());
179 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
180 int WaitStates = std::max(0, checkAnyInstHazards(MI));
182 if (SIInstrInfo::isSMRD(*MI))
183 return std::max(WaitStates, checkSMRDHazards(MI));
185 if (SIInstrInfo::isVALU(*MI))
186 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
188 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
189 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
191 if (SIInstrInfo::isDPP(*MI))
192 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
194 if (isDivFMas(MI->getOpcode()))
195 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
197 if (isRWLane(MI->getOpcode()))
198 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
200 if (MI->isInlineAsm())
201 return std::max(WaitStates, checkInlineAsmHazards(MI));
203 if (isSGetReg(MI->getOpcode()))
204 return std::max(WaitStates, checkGetRegHazards(MI));
206 if (isSSetReg(MI->getOpcode()))
207 return std::max(WaitStates, checkSetRegHazards(MI));
209 if (isRFE(MI->getOpcode()))
210 return std::max(WaitStates, checkRFEHazards(MI));
212 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
213 isSMovRel(MI->getOpcode())))
214 return std::max(WaitStates, checkReadM0Hazards(MI));
216 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI))
217 return std::max(WaitStates, checkReadM0Hazards(MI));
222 void GCNHazardRecognizer::EmitNoop() {
223 EmittedInstrs.push_front(nullptr);
226 void GCNHazardRecognizer::AdvanceCycle() {
227 // When the scheduler detects a stall, it will call AdvanceCycle() without
228 // emitting any instructions.
232 // Do not track non-instructions which do not affect the wait states.
233 // If included, these instructions can lead to buffer overflow such that
234 // detectable hazards are missed.
235 if (CurrCycleInstr->getOpcode() == AMDGPU::IMPLICIT_DEF)
237 else if (CurrCycleInstr->isDebugInstr())
240 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
242 // Keep track of emitted instructions
243 EmittedInstrs.push_front(CurrCycleInstr);
245 // Add a nullptr for each additional wait state after the first. Make sure
246 // not to add more than getMaxLookAhead() items to the list, since we
247 // truncate the list to that size right after this loop.
248 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
250 EmittedInstrs.push_front(nullptr);
253 // getMaxLookahead() is the largest number of wait states we will ever need
254 // to insert, so there is no point in keeping track of more than that many
256 EmittedInstrs.resize(getMaxLookAhead());
258 CurrCycleInstr = nullptr;
261 void GCNHazardRecognizer::RecedeCycle() {
262 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
265 //===----------------------------------------------------------------------===//
267 //===----------------------------------------------------------------------===//
269 int GCNHazardRecognizer::getWaitStatesSince(
270 function_ref<bool(MachineInstr *)> IsHazard) {
272 for (MachineInstr *MI : EmittedInstrs) {
277 unsigned Opcode = MI->getOpcode();
278 if (Opcode == AMDGPU::INLINEASM)
283 return std::numeric_limits<int>::max();
286 int GCNHazardRecognizer::getWaitStatesSinceDef(
287 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
288 const SIRegisterInfo *TRI = ST.getRegisterInfo();
290 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
291 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
294 return getWaitStatesSince(IsHazardFn);
297 int GCNHazardRecognizer::getWaitStatesSinceSetReg(
298 function_ref<bool(MachineInstr *)> IsHazard) {
299 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
300 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
303 return getWaitStatesSince(IsHazardFn);
306 //===----------------------------------------------------------------------===//
307 // No-op Hazard Detection
308 //===----------------------------------------------------------------------===//
310 static void addRegUnits(const SIRegisterInfo &TRI,
311 BitVector &BV, unsigned Reg) {
312 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
316 static void addRegsToSet(const SIRegisterInfo &TRI,
317 iterator_range<MachineInstr::const_mop_iterator> Ops,
319 for (const MachineOperand &Op : Ops) {
321 addRegUnits(TRI, Set, Op.getReg());
325 void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) {
326 // XXX: Do we need to worry about implicit operands
327 addRegsToSet(TRI, MI.defs(), ClauseDefs);
328 addRegsToSet(TRI, MI.uses(), ClauseUses);
331 int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
332 // SMEM soft clause are only present on VI+, and only matter if xnack is
334 if (!ST.isXNACKEnabled())
337 bool IsSMRD = TII.isSMRD(*MEM);
341 // A soft-clause is any group of consecutive SMEM instructions. The
342 // instructions in this group may return out of order and/or may be
343 // replayed (i.e. the same instruction issued more than once).
345 // In order to handle these situations correctly we need to make sure
346 // that when a clause has more than one instruction, no instruction in the
347 // clause writes to a register that is read another instruction in the clause
348 // (including itself). If we encounter this situaion, we need to break the
349 // clause by inserting a non SMEM instruction.
351 for (MachineInstr *MI : EmittedInstrs) {
352 // When we hit a non-SMEM instruction then we have passed the start of the
353 // clause and we can stop.
357 if (IsSMRD != SIInstrInfo::isSMRD(*MI))
363 if (ClauseDefs.none())
366 // We need to make sure not to put loads and stores in the same clause if they
367 // use the same address. For now, just start a new clause whenever we see a
374 // If the set of defs and uses intersect then we cannot add this instruction
375 // to the clause, so we have a hazard.
376 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
379 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
380 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
381 int WaitStatesNeeded = 0;
383 WaitStatesNeeded = checkSoftClauseHazards(SMRD);
385 // This SMRD hazard only affects SI.
386 if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS)
387 return WaitStatesNeeded;
389 // A read of an SGPR by SMRD instruction requires 4 wait states when the
390 // SGPR was written by a VALU instruction.
391 int SmrdSgprWaitStates = 4;
392 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
393 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
395 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
397 for (const MachineOperand &Use : SMRD->uses()) {
400 int WaitStatesNeededForUse =
401 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
402 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
404 // This fixes what appears to be undocumented hardware behavior in SI where
405 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
406 // needs some number of nops in between. We don't know how many we need, but
407 // let's use 4. This wasn't discovered before probably because the only
408 // case when this happens is when we expand a 64-bit pointer into a full
409 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
410 // probably never encountered in the closed-source land.
412 int WaitStatesNeededForUse =
413 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
414 IsBufferHazardDefFn);
415 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
419 return WaitStatesNeeded;
422 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
423 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
426 int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
428 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
429 // SGPR was written by a VALU Instruction.
430 const int VmemSgprWaitStates = 5;
431 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
433 for (const MachineOperand &Use : VMEM->uses()) {
434 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
437 int WaitStatesNeededForUse =
438 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
439 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
441 return WaitStatesNeeded;
444 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
445 const SIRegisterInfo *TRI = ST.getRegisterInfo();
446 const SIInstrInfo *TII = ST.getInstrInfo();
448 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
449 int DppVgprWaitStates = 2;
450 int DppExecWaitStates = 5;
451 int WaitStatesNeeded = 0;
452 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
454 for (const MachineOperand &Use : DPP->uses()) {
455 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
457 int WaitStatesNeededForUse =
458 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
459 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
462 WaitStatesNeeded = std::max(
464 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
466 return WaitStatesNeeded;
469 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
470 const SIInstrInfo *TII = ST.getInstrInfo();
472 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
474 const int DivFMasWaitStates = 4;
475 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
476 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
478 return DivFMasWaitStates - WaitStatesNeeded;
481 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
482 const SIInstrInfo *TII = ST.getInstrInfo();
483 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
485 const int GetRegWaitStates = 2;
486 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
487 return GetRegHWReg == getHWReg(TII, *MI);
489 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
491 return GetRegWaitStates - WaitStatesNeeded;
494 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
495 const SIInstrInfo *TII = ST.getInstrInfo();
496 unsigned HWReg = getHWReg(TII, *SetRegInstr);
498 const int SetRegWaitStates =
499 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
500 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
501 return HWReg == getHWReg(TII, *MI);
503 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
504 return SetRegWaitStates - WaitStatesNeeded;
507 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
511 const SIInstrInfo *TII = ST.getInstrInfo();
512 unsigned Opcode = MI.getOpcode();
513 const MCInstrDesc &Desc = MI.getDesc();
515 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
518 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
520 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
521 // There is no hazard if the instruction does not use vector regs
525 // For MUBUF/MTBUF instructions this hazard only exists if the
526 // instruction is not using a register in the soffset field.
527 const MachineOperand *SOffset =
528 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
529 // If we have no soffset operand, then assume this field has been
530 // hardcoded to zero.
531 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
532 (!SOffset || !SOffset->isReg()))
536 // MIMG instructions create a hazard if they don't use a 256-bit T# and
537 // the store size is greater than 8 bytes and they have more than two bits
538 // of their dmask set.
539 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
540 if (TII->isMIMG(MI)) {
541 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
542 assert(SRsrcIdx != -1 &&
543 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
547 if (TII->isFLAT(MI)) {
548 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
549 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
556 int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def,
557 const MachineRegisterInfo &MRI) {
558 // Helper to check for the hazard where VMEM instructions that store more than
559 // 8 bytes can have there store data over written by the next instruction.
560 const SIRegisterInfo *TRI = ST.getRegisterInfo();
562 const int VALUWaitStates = 1;
563 int WaitStatesNeeded = 0;
565 if (!TRI->isVGPR(MRI, Def.getReg()))
566 return WaitStatesNeeded;
567 unsigned Reg = Def.getReg();
568 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
569 int DataIdx = createsVALUHazard(*MI);
570 return DataIdx >= 0 &&
571 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
573 int WaitStatesNeededForDef =
574 VALUWaitStates - getWaitStatesSince(IsHazardFn);
575 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
577 return WaitStatesNeeded;
580 int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
581 // This checks for the hazard where VMEM instructions that store more than
582 // 8 bytes can have there store data over written by the next instruction.
583 if (!ST.has12DWordStoreHazard())
586 const MachineRegisterInfo &MRI = MF.getRegInfo();
587 int WaitStatesNeeded = 0;
589 for (const MachineOperand &Def : VALU->defs()) {
590 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
593 return WaitStatesNeeded;
596 int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) {
597 // This checks for hazards associated with inline asm statements.
598 // Since inline asms can contain just about anything, we use this
599 // to call/leverage other check*Hazard routines. Note that
600 // this function doesn't attempt to address all possible inline asm
601 // hazards (good luck), but is a collection of what has been
602 // problematic thus far.
604 // see checkVALUHazards()
605 if (!ST.has12DWordStoreHazard())
608 const MachineRegisterInfo &MRI = MF.getRegInfo();
609 int WaitStatesNeeded = 0;
611 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands();
613 const MachineOperand &Op = IA->getOperand(I);
614 if (Op.isReg() && Op.isDef()) {
615 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI));
619 return WaitStatesNeeded;
622 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
623 const SIInstrInfo *TII = ST.getInstrInfo();
624 const SIRegisterInfo *TRI = ST.getRegisterInfo();
625 const MachineRegisterInfo &MRI = MF.getRegInfo();
627 const MachineOperand *LaneSelectOp =
628 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
630 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
633 unsigned LaneSelectReg = LaneSelectOp->getReg();
634 auto IsHazardFn = [TII] (MachineInstr *MI) {
635 return TII->isVALU(*MI);
638 const int RWLaneWaitStates = 4;
639 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
640 return RWLaneWaitStates - WaitStatesSince;
643 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
644 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
647 const SIInstrInfo *TII = ST.getInstrInfo();
649 const int RFEWaitStates = 1;
651 auto IsHazardFn = [TII] (MachineInstr *MI) {
652 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
654 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
655 return RFEWaitStates - WaitStatesNeeded;
658 int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
659 if (MI->isDebugInstr())
662 const SIRegisterInfo *TRI = ST.getRegisterInfo();
663 if (!ST.hasSMovFedHazard())
666 // Check for any instruction reading an SGPR after a write from
668 int MovFedWaitStates = 1;
669 int WaitStatesNeeded = 0;
671 for (const MachineOperand &Use : MI->uses()) {
672 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
674 auto IsHazardFn = [] (MachineInstr *MI) {
675 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
677 int WaitStatesNeededForUse =
678 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
679 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
682 return WaitStatesNeeded;
685 int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
686 const SIInstrInfo *TII = ST.getInstrInfo();
687 const int SMovRelWaitStates = 1;
688 auto IsHazardFn = [TII] (MachineInstr *MI) {
689 return TII->isSALU(*MI);
691 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);