]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AMDGPU/GCNHazardRecognizer.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / AMDGPU / GCNHazardRecognizer.cpp
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 "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"
28 #include <algorithm>
29 #include <cassert>
30 #include <limits>
31 #include <set>
32 #include <vector>
33
34 using namespace llvm;
35
36 //===----------------------------------------------------------------------===//
37 // Hazard Recoginizer Implementation
38 //===----------------------------------------------------------------------===//
39
40 GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) :
41   CurrCycleInstr(nullptr),
42   MF(MF),
43   ST(MF.getSubtarget<GCNSubtarget>()),
44   TII(*ST.getInstrInfo()),
45   TRI(TII.getRegisterInfo()),
46   ClauseUses(TRI.getNumRegUnits()),
47   ClauseDefs(TRI.getNumRegUnits()) {
48   MaxLookAhead = 5;
49 }
50
51 void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
52   EmitInstruction(SU->getInstr());
53 }
54
55 void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
56   CurrCycleInstr = MI;
57 }
58
59 static bool isDivFMas(unsigned Opcode) {
60   return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
61 }
62
63 static bool isSGetReg(unsigned Opcode) {
64   return Opcode == AMDGPU::S_GETREG_B32;
65 }
66
67 static bool isSSetReg(unsigned Opcode) {
68   return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
69 }
70
71 static bool isRWLane(unsigned Opcode) {
72   return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
73 }
74
75 static bool isRFE(unsigned Opcode) {
76   return Opcode == AMDGPU::S_RFE_B64;
77 }
78
79 static bool isSMovRel(unsigned Opcode) {
80   switch (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:
85     return true;
86   default:
87     return false;
88   }
89 }
90
91 static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII,
92                                     const MachineInstr &MI) {
93   if (TII.isAlwaysGDS(MI.getOpcode()))
94     return true;
95
96   switch (MI.getOpcode()) {
97   case AMDGPU::S_SENDMSG:
98   case AMDGPU::S_SENDMSGHALT:
99   case AMDGPU::S_TTRACEDATA:
100     return true;
101   // These DS opcodes don't support GDS.
102   case AMDGPU::DS_NOP:
103   case AMDGPU::DS_PERMUTE_B32:
104   case AMDGPU::DS_BPERMUTE_B32:
105     return false;
106   default:
107     if (TII.isDS(MI.getOpcode())) {
108       int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
109                                            AMDGPU::OpName::gds);
110       if (MI.getOperand(GDS).getImm())
111         return true;
112     }
113     return false;
114   }
115 }
116
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_;
121 }
122
123 ScheduleHazardRecognizer::HazardType
124 GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
125   MachineInstr *MI = SU->getInstr();
126
127   if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
128     return NoopHazard;
129
130   // FIXME: Should flat be considered vmem?
131   if ((SIInstrInfo::isVMEM(*MI) ||
132        SIInstrInfo::isFLAT(*MI))
133       && checkVMEMHazards(MI) > 0)
134     return NoopHazard;
135
136   if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
137     return NoopHazard;
138
139   if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
140     return NoopHazard;
141
142   if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
143     return NoopHazard;
144
145   if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
146     return NoopHazard;
147
148   if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
149     return NoopHazard;
150
151   if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
152     return NoopHazard;
153
154   if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
155     return NoopHazard;
156
157   if (ST.hasReadM0MovRelInterpHazard() &&
158       (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
159       checkReadM0Hazards(MI) > 0)
160     return NoopHazard;
161
162   if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) &&
163       checkReadM0Hazards(MI) > 0)
164     return NoopHazard;
165
166   if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0)
167     return NoopHazard;
168
169   if (checkAnyInstHazards(MI) > 0)
170     return NoopHazard;
171
172   return NoHazard;
173 }
174
175 unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
176   return PreEmitNoops(SU->getInstr());
177 }
178
179 unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
180   int WaitStates = std::max(0, checkAnyInstHazards(MI));
181
182   if (SIInstrInfo::isSMRD(*MI))
183     return std::max(WaitStates, checkSMRDHazards(MI));
184
185   if (SIInstrInfo::isVALU(*MI))
186     WaitStates = std::max(WaitStates, checkVALUHazards(MI));
187
188   if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
189     WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
190
191   if (SIInstrInfo::isDPP(*MI))
192     WaitStates = std::max(WaitStates, checkDPPHazards(MI));
193
194   if (isDivFMas(MI->getOpcode()))
195     WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
196
197   if (isRWLane(MI->getOpcode()))
198     WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
199
200   if (MI->isInlineAsm())
201     return std::max(WaitStates, checkInlineAsmHazards(MI));
202
203   if (isSGetReg(MI->getOpcode()))
204     return std::max(WaitStates, checkGetRegHazards(MI));
205
206   if (isSSetReg(MI->getOpcode()))
207     return std::max(WaitStates, checkSetRegHazards(MI));
208
209   if (isRFE(MI->getOpcode()))
210     return std::max(WaitStates, checkRFEHazards(MI));
211
212   if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
213                                            isSMovRel(MI->getOpcode())))
214     return std::max(WaitStates, checkReadM0Hazards(MI));
215
216   if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI))
217     return std::max(WaitStates, checkReadM0Hazards(MI));
218
219   return WaitStates;
220 }
221
222 void GCNHazardRecognizer::EmitNoop() {
223   EmittedInstrs.push_front(nullptr);
224 }
225
226 void GCNHazardRecognizer::AdvanceCycle() {
227   // When the scheduler detects a stall, it will call AdvanceCycle() without
228   // emitting any instructions.
229   if (!CurrCycleInstr)
230     return;
231
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)
236     return;
237   else if (CurrCycleInstr->isDebugInstr())
238     return;
239
240   unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
241
242   // Keep track of emitted instructions
243   EmittedInstrs.push_front(CurrCycleInstr);
244
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());
249        i < e; ++i) {
250     EmittedInstrs.push_front(nullptr);
251   }
252
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
255   // wait states.
256   EmittedInstrs.resize(getMaxLookAhead());
257
258   CurrCycleInstr = nullptr;
259 }
260
261 void GCNHazardRecognizer::RecedeCycle() {
262   llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
263 }
264
265 //===----------------------------------------------------------------------===//
266 // Helper Functions
267 //===----------------------------------------------------------------------===//
268
269 int GCNHazardRecognizer::getWaitStatesSince(
270     function_ref<bool(MachineInstr *)> IsHazard) {
271   int WaitStates = 0;
272   for (MachineInstr *MI : EmittedInstrs) {
273     if (MI) {
274       if (IsHazard(MI))
275         return WaitStates;
276
277       unsigned Opcode = MI->getOpcode();
278       if (Opcode == AMDGPU::INLINEASM)
279         continue;
280     }
281     ++WaitStates;
282   }
283   return std::numeric_limits<int>::max();
284 }
285
286 int GCNHazardRecognizer::getWaitStatesSinceDef(
287     unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
288   const SIRegisterInfo *TRI = ST.getRegisterInfo();
289
290   auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
291     return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
292   };
293
294   return getWaitStatesSince(IsHazardFn);
295 }
296
297 int GCNHazardRecognizer::getWaitStatesSinceSetReg(
298     function_ref<bool(MachineInstr *)> IsHazard) {
299   auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
300     return isSSetReg(MI->getOpcode()) && IsHazard(MI);
301   };
302
303   return getWaitStatesSince(IsHazardFn);
304 }
305
306 //===----------------------------------------------------------------------===//
307 // No-op Hazard Detection
308 //===----------------------------------------------------------------------===//
309
310 static void addRegUnits(const SIRegisterInfo &TRI,
311                         BitVector &BV, unsigned Reg) {
312   for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
313     BV.set(*RUI);
314 }
315
316 static void addRegsToSet(const SIRegisterInfo &TRI,
317                          iterator_range<MachineInstr::const_mop_iterator> Ops,
318                          BitVector &Set) {
319   for (const MachineOperand &Op : Ops) {
320     if (Op.isReg())
321       addRegUnits(TRI, Set, Op.getReg());
322   }
323 }
324
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);
329 }
330
331 int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
332   // SMEM soft clause are only present on VI+, and only matter if xnack is
333   // enabled.
334   if (!ST.isXNACKEnabled())
335     return 0;
336
337   bool IsSMRD = TII.isSMRD(*MEM);
338
339   resetClause();
340
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).
344   //
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.
350
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.
354     if (!MI)
355       break;
356
357     if (IsSMRD != SIInstrInfo::isSMRD(*MI))
358       break;
359
360     addClauseInst(*MI);
361   }
362
363   if (ClauseDefs.none())
364     return 0;
365
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
368   // store.
369   if (MEM->mayStore())
370     return 1;
371
372   addClauseInst(*MEM);
373
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;
377 }
378
379 int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
380   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
381   int WaitStatesNeeded = 0;
382
383   WaitStatesNeeded = checkSoftClauseHazards(SMRD);
384
385   // This SMRD hazard only affects SI.
386   if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS)
387     return WaitStatesNeeded;
388
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); };
394
395   bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
396
397   for (const MachineOperand &Use : SMRD->uses()) {
398     if (!Use.isReg())
399       continue;
400     int WaitStatesNeededForUse =
401         SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
402     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
403
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.
411     if (IsBufferSMRD) {
412       int WaitStatesNeededForUse =
413         SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
414                                                    IsBufferHazardDefFn);
415       WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
416     }
417   }
418
419   return WaitStatesNeeded;
420 }
421
422 int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
423   if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
424     return 0;
425
426   int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
427
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); };
432
433   for (const MachineOperand &Use : VMEM->uses()) {
434     if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
435       continue;
436
437     int WaitStatesNeededForUse =
438         VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
439     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
440   }
441   return WaitStatesNeeded;
442 }
443
444 int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
445   const SIRegisterInfo *TRI = ST.getRegisterInfo();
446   const SIInstrInfo *TII = ST.getInstrInfo();
447
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); };
453
454   for (const MachineOperand &Use : DPP->uses()) {
455     if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
456       continue;
457     int WaitStatesNeededForUse =
458         DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
459     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
460   }
461
462   WaitStatesNeeded = std::max(
463       WaitStatesNeeded,
464       DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
465
466   return WaitStatesNeeded;
467 }
468
469 int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
470   const SIInstrInfo *TII = ST.getInstrInfo();
471
472   // v_div_fmas requires 4 wait states after a write to vcc from a VALU
473   // instruction.
474   const int DivFMasWaitStates = 4;
475   auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
476   int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
477
478   return DivFMasWaitStates - WaitStatesNeeded;
479 }
480
481 int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
482   const SIInstrInfo *TII = ST.getInstrInfo();
483   unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
484
485   const int GetRegWaitStates = 2;
486   auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
487     return GetRegHWReg == getHWReg(TII, *MI);
488   };
489   int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
490
491   return GetRegWaitStates - WaitStatesNeeded;
492 }
493
494 int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
495   const SIInstrInfo *TII = ST.getInstrInfo();
496   unsigned HWReg = getHWReg(TII, *SetRegInstr);
497
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);
502   };
503   int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
504   return SetRegWaitStates - WaitStatesNeeded;
505 }
506
507 int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
508   if (!MI.mayStore())
509     return -1;
510
511   const SIInstrInfo *TII = ST.getInstrInfo();
512   unsigned Opcode = MI.getOpcode();
513   const MCInstrDesc &Desc = MI.getDesc();
514
515   int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
516   int VDataRCID = -1;
517   if (VDataIdx != -1)
518     VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
519
520   if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
521     // There is no hazard if the instruction does not use vector regs
522     // (like wbinvl1)
523     if (VDataIdx == -1)
524       return -1;
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()))
533       return VDataIdx;
534   }
535
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);
544     (void)SRsrcIdx;
545   }
546
547   if (TII->isFLAT(MI)) {
548     int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
549     if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
550       return DataIdx;
551   }
552
553   return -1;
554 }
555
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();
561
562   const int VALUWaitStates = 1;
563   int WaitStatesNeeded = 0;
564
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);
572   };
573   int WaitStatesNeededForDef =
574     VALUWaitStates - getWaitStatesSince(IsHazardFn);
575   WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
576
577   return WaitStatesNeeded;
578 }
579
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())
584     return 0;
585
586   const MachineRegisterInfo &MRI = MF.getRegInfo();
587   int WaitStatesNeeded = 0;
588
589   for (const MachineOperand &Def : VALU->defs()) {
590     WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
591   }
592
593   return WaitStatesNeeded;
594 }
595
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.
603
604   // see checkVALUHazards()
605   if (!ST.has12DWordStoreHazard())
606     return 0;
607
608   const MachineRegisterInfo &MRI = MF.getRegInfo();
609   int WaitStatesNeeded = 0;
610
611   for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands();
612        I != E; ++I) {
613     const MachineOperand &Op = IA->getOperand(I);
614     if (Op.isReg() && Op.isDef()) {
615       WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI));
616     }
617   }
618
619   return WaitStatesNeeded;
620 }
621
622 int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
623   const SIInstrInfo *TII = ST.getInstrInfo();
624   const SIRegisterInfo *TRI = ST.getRegisterInfo();
625   const MachineRegisterInfo &MRI = MF.getRegInfo();
626
627   const MachineOperand *LaneSelectOp =
628       TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
629
630   if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
631     return 0;
632
633   unsigned LaneSelectReg = LaneSelectOp->getReg();
634   auto IsHazardFn = [TII] (MachineInstr *MI) {
635     return TII->isVALU(*MI);
636   };
637
638   const int RWLaneWaitStates = 4;
639   int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
640   return RWLaneWaitStates - WaitStatesSince;
641 }
642
643 int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
644   if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
645     return 0;
646
647   const SIInstrInfo *TII = ST.getInstrInfo();
648
649   const int RFEWaitStates = 1;
650
651   auto IsHazardFn = [TII] (MachineInstr *MI) {
652     return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
653   };
654   int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
655   return RFEWaitStates - WaitStatesNeeded;
656 }
657
658 int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
659   if (MI->isDebugInstr())
660     return 0;
661
662   const SIRegisterInfo *TRI = ST.getRegisterInfo();
663   if (!ST.hasSMovFedHazard())
664     return 0;
665
666   // Check for any instruction reading an SGPR after a write from
667   // s_mov_fed_b32.
668   int MovFedWaitStates = 1;
669   int WaitStatesNeeded = 0;
670
671   for (const MachineOperand &Use : MI->uses()) {
672     if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
673       continue;
674     auto IsHazardFn = [] (MachineInstr *MI) {
675       return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
676     };
677     int WaitStatesNeededForUse =
678         MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
679     WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
680   }
681
682   return WaitStatesNeeded;
683 }
684
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);
690   };
691   return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
692 }