blob: 69ddbfb53958337233959f742c9b0eee39e3a54c [file] [log] [blame]
Tom Stellardcb6ba622016-04-30 00:23:06 +00001//===-- 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
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000014#include "GCNHazardRecognizer.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "AMDGPUSubtarget.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000016#include "SIDefines.h"
Tom Stellardcb6ba622016-04-30 00:23:06 +000017#include "SIInstrInfo.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000018#include "SIRegisterInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000019#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000020#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"
Tom Stellardcb6ba622016-04-30 00:23:06 +000025#include "llvm/CodeGen/ScheduleDAG.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000026#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>
Tom Stellardcb6ba622016-04-30 00:23:06 +000033
34using namespace llvm;
35
36//===----------------------------------------------------------------------===//
37// Hazard Recoginizer Implementation
38//===----------------------------------------------------------------------===//
39
40GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) :
41 CurrCycleInstr(nullptr),
Matt Arsenault43e92fe2016-06-24 06:30:11 +000042 MF(MF),
Tom Stellard5bfbae52018-07-11 20:59:01 +000043 ST(MF.getSubtarget<GCNSubtarget>()),
Matt Arsenault03c67d12017-11-17 04:18:24 +000044 TII(*ST.getInstrInfo()),
45 TRI(TII.getRegisterInfo()),
46 ClauseUses(TRI.getNumRegUnits()),
47 ClauseDefs(TRI.getNumRegUnits()) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000048 MaxLookAhead = 5;
49}
50
51void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
52 EmitInstruction(SU->getInstr());
53}
54
55void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
56 CurrCycleInstr = MI;
57}
58
Tom Stellard5ab61542016-10-07 23:42:48 +000059static bool isDivFMas(unsigned Opcode) {
60 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
61}
62
Tom Stellard961811c2016-10-15 00:58:14 +000063static bool isSGetReg(unsigned Opcode) {
64 return Opcode == AMDGPU::S_GETREG_B32;
65}
66
67static bool isSSetReg(unsigned Opcode) {
68 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
69}
70
Tom Stellard04051b52016-10-27 23:42:29 +000071static bool isRWLane(unsigned Opcode) {
72 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
73}
74
Tom Stellardaea899e2016-10-27 23:50:21 +000075static bool isRFE(unsigned Opcode) {
76 return Opcode == AMDGPU::S_RFE_B64;
77}
78
Matt Arsenaulte823d922017-02-18 18:29:53 +000079static bool isSMovRel(unsigned Opcode) {
Matt Arsenault59ece952017-03-17 21:36:28 +000080 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 }
Matt Arsenaulte823d922017-02-18 18:29:53 +000089}
90
Marek Olsakc5cec5e2019-01-16 15:43:53 +000091static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII,
92 const MachineInstr &MI) {
93 if (TII.isAlwaysGDS(MI.getOpcode()))
94 return true;
95
Matt Arsenaulta41351e2017-11-17 21:35:32 +000096 switch (MI.getOpcode()) {
97 case AMDGPU::S_SENDMSG:
98 case AMDGPU::S_SENDMSGHALT:
99 case AMDGPU::S_TTRACEDATA:
100 return true;
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000101 // 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;
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000106 default:
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000107 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 }
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000113 return false;
114 }
115}
116
Tom Stellardaea899e2016-10-27 23:50:21 +0000117static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +0000118 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
119 AMDGPU::OpName::simm16);
120 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
121}
122
Tom Stellardcb6ba622016-04-30 00:23:06 +0000123ScheduleHazardRecognizer::HazardType
124GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000125 MachineInstr *MI = SU->getInstr();
126
Aaron Ballman5c190d02016-05-02 14:48:03 +0000127 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000128 return NoopHazard;
129
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000130 // FIXME: Should flat be considered vmem?
131 if ((SIInstrInfo::isVMEM(*MI) ||
132 SIInstrInfo::isFLAT(*MI))
133 && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000134 return NoopHazard;
135
Tom Stellardb133fbb2016-10-27 23:05:31 +0000136 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
137 return NoopHazard;
138
Tom Stellarda27007e2016-05-02 16:23:09 +0000139 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
140 return NoopHazard;
141
Tom Stellard5ab61542016-10-07 23:42:48 +0000142 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
143 return NoopHazard;
144
Tom Stellard04051b52016-10-27 23:42:29 +0000145 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
146 return NoopHazard;
147
Tom Stellard961811c2016-10-15 00:58:14 +0000148 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
149 return NoopHazard;
150
Tom Stellard30d30822016-10-27 20:39:09 +0000151 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
152 return NoopHazard;
153
Tom Stellardaea899e2016-10-27 23:50:21 +0000154 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
155 return NoopHazard;
156
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000157 if (ST.hasReadM0MovRelInterpHazard() &&
158 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
159 checkReadM0Hazards(MI) > 0)
160 return NoopHazard;
161
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000162 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) &&
Matt Arsenaulte823d922017-02-18 18:29:53 +0000163 checkReadM0Hazards(MI) > 0)
164 return NoopHazard;
165
Mark Searlesd29f24a2017-12-07 20:34:25 +0000166 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0)
167 return NoopHazard;
168
Matt Arsenaulte823d922017-02-18 18:29:53 +0000169 if (checkAnyInstHazards(MI) > 0)
170 return NoopHazard;
171
Tom Stellardcb6ba622016-04-30 00:23:06 +0000172 return NoHazard;
173}
174
175unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
176 return PreEmitNoops(SU->getInstr());
177}
178
179unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000180 int WaitStates = std::max(0, checkAnyInstHazards(MI));
181
Aaron Ballman5c190d02016-05-02 14:48:03 +0000182 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000183 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000184
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000185 if (SIInstrInfo::isVALU(*MI))
186 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000187
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000188 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
189 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000190
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000191 if (SIInstrInfo::isDPP(*MI))
192 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000193
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000194 if (isDivFMas(MI->getOpcode()))
195 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000196
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000197 if (isRWLane(MI->getOpcode()))
198 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
Tom Stellard5ab61542016-10-07 23:42:48 +0000199
Mark Searlesd29f24a2017-12-07 20:34:25 +0000200 if (MI->isInlineAsm())
201 return std::max(WaitStates, checkInlineAsmHazards(MI));
202
Tom Stellard961811c2016-10-15 00:58:14 +0000203 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000204 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000205
Tom Stellard30d30822016-10-27 20:39:09 +0000206 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000207 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000208
Tom Stellardaea899e2016-10-27 23:50:21 +0000209 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000210 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000211
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000212 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
213 isSMovRel(MI->getOpcode())))
214 return std::max(WaitStates, checkReadM0Hazards(MI));
215
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000216 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000217 return std::max(WaitStates, checkReadM0Hazards(MI));
218
219 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000220}
221
222void GCNHazardRecognizer::EmitNoop() {
223 EmittedInstrs.push_front(nullptr);
224}
225
226void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000227 // When the scheduler detects a stall, it will call AdvanceCycle() without
228 // emitting any instructions.
229 if (!CurrCycleInstr)
230 return;
231
Carl Ritsonf898edd2018-09-10 10:14:48 +0000232 // 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
Matt Arsenault59ece952017-03-17 21:36:28 +0000240 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000241
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
261void GCNHazardRecognizer::RecedeCycle() {
262 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
263}
264
265//===----------------------------------------------------------------------===//
266// Helper Functions
267//===----------------------------------------------------------------------===//
268
Tom Stellardb133fbb2016-10-27 23:05:31 +0000269int GCNHazardRecognizer::getWaitStatesSince(
Tom Stellard961811c2016-10-15 00:58:14 +0000270 function_ref<bool(MachineInstr *)> IsHazard) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000271 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000272 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000273 if (MI) {
274 if (IsHazard(MI))
275 return WaitStates;
276
277 unsigned Opcode = MI->getOpcode();
Carl Ritsonf898edd2018-09-10 10:14:48 +0000278 if (Opcode == AMDGPU::INLINEASM)
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000279 continue;
280 }
Tom Stellard961811c2016-10-15 00:58:14 +0000281 ++WaitStates;
Tom Stellard961811c2016-10-15 00:58:14 +0000282 }
283 return std::numeric_limits<int>::max();
284}
285
Tom Stellardb133fbb2016-10-27 23:05:31 +0000286int 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
297int GCNHazardRecognizer::getWaitStatesSinceSetReg(
298 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000299 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
300 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
301 };
302
303 return getWaitStatesSince(IsHazardFn);
304}
305
Tom Stellardcb6ba622016-04-30 00:23:06 +0000306//===----------------------------------------------------------------------===//
307// No-op Hazard Detection
308//===----------------------------------------------------------------------===//
309
Matt Arsenault03c67d12017-11-17 04:18:24 +0000310static 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
316static void addRegsToSet(const SIRegisterInfo &TRI,
317 iterator_range<MachineInstr::const_mop_iterator> Ops,
318 BitVector &Set) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000319 for (const MachineOperand &Op : Ops) {
320 if (Op.isReg())
Matt Arsenault03c67d12017-11-17 04:18:24 +0000321 addRegUnits(TRI, Set, Op.getReg());
Tom Stellard1f520e52016-05-02 17:39:06 +0000322 }
323}
324
Matt Arsenault03c67d12017-11-17 04:18:24 +0000325void 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
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000331int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
Matt Arsenault03c67d12017-11-17 04:18:24 +0000332 // SMEM soft clause are only present on VI+, and only matter if xnack is
333 // enabled.
334 if (!ST.isXNACKEnabled())
Tom Stellard1f520e52016-05-02 17:39:06 +0000335 return 0;
336
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000337 bool IsSMRD = TII.isSMRD(*MEM);
338
Matt Arsenault03c67d12017-11-17 04:18:24 +0000339 resetClause();
340
Tom Stellard1f520e52016-05-02 17:39:06 +0000341 // 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
Tom Stellard1f520e52016-05-02 17:39:06 +0000351 for (MachineInstr *MI : EmittedInstrs) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000352 // When we hit a non-SMEM instruction then we have passed the start of the
353 // clause and we can stop.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000354 if (!MI)
355 break;
356
357 if (IsSMRD != SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000358 break;
359
Matt Arsenault03c67d12017-11-17 04:18:24 +0000360 addClauseInst(*MI);
Tom Stellard1f520e52016-05-02 17:39:06 +0000361 }
362
Matt Arsenault03c67d12017-11-17 04:18:24 +0000363 if (ClauseDefs.none())
Tom Stellard1f520e52016-05-02 17:39:06 +0000364 return 0;
365
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000366 // 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())
Tom Stellard1f520e52016-05-02 17:39:06 +0000370 return 1;
371
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000372 addClauseInst(*MEM);
Tom Stellard1f520e52016-05-02 17:39:06 +0000373
374 // If the set of defs and uses intersect then we cannot add this instruction
375 // to the clause, so we have a hazard.
Matt Arsenault03c67d12017-11-17 04:18:24 +0000376 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
Tom Stellard1f520e52016-05-02 17:39:06 +0000377}
378
Tom Stellardcb6ba622016-04-30 00:23:06 +0000379int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000380 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Tom Stellard1f520e52016-05-02 17:39:06 +0000381 int WaitStatesNeeded = 0;
382
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000383 WaitStatesNeeded = checkSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000384
385 // This SMRD hazard only affects SI.
Tom Stellard5bfbae52018-07-11 20:59:01 +0000386 if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000387 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000388
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;
Matt Arsenault59ece952017-03-17 21:36:28 +0000392 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000393 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
394
Matt Arsenault4512d0a2017-11-17 04:18:26 +0000395 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000396
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);
Marek Olsak22322432017-10-26 14:43:02 +0000403
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 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000417 }
Marek Olsak22322432017-10-26 14:43:02 +0000418
Tom Stellardcb6ba622016-04-30 00:23:06 +0000419 return WaitStatesNeeded;
420}
421
422int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000423 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000424 return 0;
425
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000426 int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000427
428 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
429 // SGPR was written by a VALU Instruction.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000430 const int VmemSgprWaitStates = 5;
431 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Tom Stellardcb6ba622016-04-30 00:23:06 +0000432
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}
Tom Stellarda27007e2016-05-02 16:23:09 +0000443
444int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000445 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000446 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000447
Connor Abbott00755362017-08-04 01:09:43 +0000448 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000449 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000450 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000451 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000452 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000453
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
Connor Abbott00755362017-08-04 01:09:43 +0000462 WaitStatesNeeded = std::max(
463 WaitStatesNeeded,
464 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
465
Tom Stellarda27007e2016-05-02 16:23:09 +0000466 return WaitStatesNeeded;
467}
Tom Stellard5ab61542016-10-07 23:42:48 +0000468
469int 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}
Tom Stellard961811c2016-10-15 00:58:14 +0000480
481int 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}
Tom Stellard30d30822016-10-27 20:39:09 +0000493
494int 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}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000506
507int 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)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000521 // There is no hazard if the instruction does not use vector regs
522 // (like wbinvl1)
523 if (VDataIdx == -1)
524 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000525 // 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);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000544 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000545 }
546
547 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000548 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000549 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
550 return DataIdx;
551 }
552
553 return -1;
554}
555
Mark Searlesd29f24a2017-12-07 20:34:25 +0000556int 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
Tom Stellardb133fbb2016-10-27 23:05:31 +0000580int 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
Mark Searlesd29f24a2017-12-07 20:34:25 +0000586 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellardb133fbb2016-10-27 23:05:31 +0000587 int WaitStatesNeeded = 0;
588
589 for (const MachineOperand &Def : VALU->defs()) {
Mark Searlesd29f24a2017-12-07 20:34:25 +0000590 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000591 }
Mark Searlesd29f24a2017-12-07 20:34:25 +0000592
593 return WaitStatesNeeded;
594}
595
596int 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
Tom Stellardb133fbb2016-10-27 23:05:31 +0000619 return WaitStatesNeeded;
620}
Tom Stellard04051b52016-10-27 23:42:29 +0000621
622int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
623 const SIInstrInfo *TII = ST.getInstrInfo();
624 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Mark Searlesd29f24a2017-12-07 20:34:25 +0000625 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard04051b52016-10-27 23:42:29 +0000626
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}
Tom Stellardaea899e2016-10-27 23:50:21 +0000642
643int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000644 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}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000657
658int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000659 if (MI->isDebugInstr())
Matt Arsenaulte823d922017-02-18 18:29:53 +0000660 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
685int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000686 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000687 const int SMovRelWaitStates = 1;
Matt Arsenaulte823d922017-02-18 18:29:53 +0000688 auto IsHazardFn = [TII] (MachineInstr *MI) {
689 return TII->isSALU(*MI);
690 };
691 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
692}