blob: f19fa6846293954b9629df51ab4f5d98cc7aeedd [file] [log] [blame]
Tom Stellardcb6ba622016-04-30 00:23:06 +00001//===-- GCNHazardRecognizers.cpp - GCN Hazard Recognizer Impls ------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellardcb6ba622016-04-30 00:23:06 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements hazard recognizers for scheduling on GCN processors.
10//
11//===----------------------------------------------------------------------===//
12
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000013#include "GCNHazardRecognizer.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "AMDGPUSubtarget.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000015#include "SIDefines.h"
Tom Stellardcb6ba622016-04-30 00:23:06 +000016#include "SIInstrInfo.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000017#include "SIRegisterInfo.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000018#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000019#include "Utils/AMDGPUBaseInfo.h"
20#include "llvm/ADT/iterator_range.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +000023#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000024#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) :
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +000041 IsHazardRecognizerMode(false),
Tom Stellardcb6ba622016-04-30 00:23:06 +000042 CurrCycleInstr(nullptr),
Matt Arsenault43e92fe2016-06-24 06:30:11 +000043 MF(MF),
Tom Stellard5bfbae52018-07-11 20:59:01 +000044 ST(MF.getSubtarget<GCNSubtarget>()),
Matt Arsenault03c67d12017-11-17 04:18:24 +000045 TII(*ST.getInstrInfo()),
46 TRI(TII.getRegisterInfo()),
47 ClauseUses(TRI.getNumRegUnits()),
48 ClauseDefs(TRI.getNumRegUnits()) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000049 MaxLookAhead = 5;
50}
51
52void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
53 EmitInstruction(SU->getInstr());
54}
55
56void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
57 CurrCycleInstr = MI;
58}
59
Tom Stellard5ab61542016-10-07 23:42:48 +000060static bool isDivFMas(unsigned Opcode) {
61 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
62}
63
Tom Stellard961811c2016-10-15 00:58:14 +000064static bool isSGetReg(unsigned Opcode) {
65 return Opcode == AMDGPU::S_GETREG_B32;
66}
67
68static bool isSSetReg(unsigned Opcode) {
69 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
70}
71
Tom Stellard04051b52016-10-27 23:42:29 +000072static bool isRWLane(unsigned Opcode) {
73 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
74}
75
Tom Stellardaea899e2016-10-27 23:50:21 +000076static bool isRFE(unsigned Opcode) {
77 return Opcode == AMDGPU::S_RFE_B64;
78}
79
Matt Arsenaulte823d922017-02-18 18:29:53 +000080static bool isSMovRel(unsigned Opcode) {
Matt Arsenault59ece952017-03-17 21:36:28 +000081 switch (Opcode) {
82 case AMDGPU::S_MOVRELS_B32:
83 case AMDGPU::S_MOVRELS_B64:
84 case AMDGPU::S_MOVRELD_B32:
85 case AMDGPU::S_MOVRELD_B64:
86 return true;
87 default:
88 return false;
89 }
Matt Arsenaulte823d922017-02-18 18:29:53 +000090}
91
Marek Olsakc5cec5e2019-01-16 15:43:53 +000092static bool isSendMsgTraceDataOrGDS(const SIInstrInfo &TII,
93 const MachineInstr &MI) {
94 if (TII.isAlwaysGDS(MI.getOpcode()))
95 return true;
96
Matt Arsenaulta41351e2017-11-17 21:35:32 +000097 switch (MI.getOpcode()) {
98 case AMDGPU::S_SENDMSG:
99 case AMDGPU::S_SENDMSGHALT:
100 case AMDGPU::S_TTRACEDATA:
101 return true;
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000102 // These DS opcodes don't support GDS.
103 case AMDGPU::DS_NOP:
104 case AMDGPU::DS_PERMUTE_B32:
105 case AMDGPU::DS_BPERMUTE_B32:
106 return false;
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000107 default:
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000108 if (TII.isDS(MI.getOpcode())) {
109 int GDS = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
110 AMDGPU::OpName::gds);
111 if (MI.getOperand(GDS).getImm())
112 return true;
113 }
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000114 return false;
115 }
116}
117
Stanislav Mekhanoshin5f581c92019-06-12 17:52:51 +0000118static bool isPermlane(const MachineInstr &MI) {
119 unsigned Opcode = MI.getOpcode();
120 return Opcode == AMDGPU::V_PERMLANE16_B32 ||
121 Opcode == AMDGPU::V_PERMLANEX16_B32;
122}
123
Tom Stellardaea899e2016-10-27 23:50:21 +0000124static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +0000125 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
126 AMDGPU::OpName::simm16);
127 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
128}
129
Tom Stellardcb6ba622016-04-30 00:23:06 +0000130ScheduleHazardRecognizer::HazardType
131GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000132 MachineInstr *MI = SU->getInstr();
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000133 if (MI->isBundle())
134 return NoHazard;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000135
Aaron Ballman5c190d02016-05-02 14:48:03 +0000136 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000137 return NoopHazard;
138
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000139 // FIXME: Should flat be considered vmem?
140 if ((SIInstrInfo::isVMEM(*MI) ||
141 SIInstrInfo::isFLAT(*MI))
142 && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000143 return NoopHazard;
144
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000145 if (ST.hasNSAtoVMEMBug() && checkNSAtoVMEMHazard(MI) > 0)
146 return NoopHazard;
147
148 if (ST.hasNoDataDepHazard())
149 return NoHazard;
150
Tom Stellardb133fbb2016-10-27 23:05:31 +0000151 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
152 return NoopHazard;
153
Tom Stellarda27007e2016-05-02 16:23:09 +0000154 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
155 return NoopHazard;
156
Tom Stellard5ab61542016-10-07 23:42:48 +0000157 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
158 return NoopHazard;
159
Tom Stellard04051b52016-10-27 23:42:29 +0000160 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
161 return NoopHazard;
162
Tom Stellard961811c2016-10-15 00:58:14 +0000163 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
164 return NoopHazard;
165
Tom Stellard30d30822016-10-27 20:39:09 +0000166 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
167 return NoopHazard;
168
Tom Stellardaea899e2016-10-27 23:50:21 +0000169 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
170 return NoopHazard;
171
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000172 if (ST.hasReadM0MovRelInterpHazard() &&
173 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
174 checkReadM0Hazards(MI) > 0)
175 return NoopHazard;
176
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000177 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI) &&
Matt Arsenaulte823d922017-02-18 18:29:53 +0000178 checkReadM0Hazards(MI) > 0)
179 return NoopHazard;
180
Mark Searlesd29f24a2017-12-07 20:34:25 +0000181 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0)
182 return NoopHazard;
183
Matt Arsenaulte823d922017-02-18 18:29:53 +0000184 if (checkAnyInstHazards(MI) > 0)
185 return NoopHazard;
186
Tom Stellardcb6ba622016-04-30 00:23:06 +0000187 return NoHazard;
188}
189
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000190static void insertNoopInBundle(MachineInstr *MI, const SIInstrInfo &TII) {
191 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII.get(AMDGPU::S_NOP))
192 .addImm(0);
193}
194
195void GCNHazardRecognizer::processBundle() {
196 MachineBasicBlock::instr_iterator MI = std::next(CurrCycleInstr->getIterator());
197 MachineBasicBlock::instr_iterator E = CurrCycleInstr->getParent()->instr_end();
198 // Check bundled MachineInstr's for hazards.
199 for (; MI != E && MI->isInsideBundle(); ++MI) {
200 CurrCycleInstr = &*MI;
201 unsigned WaitStates = PreEmitNoopsCommon(CurrCycleInstr);
202
203 if (IsHazardRecognizerMode)
204 fixHazards(CurrCycleInstr);
205
206 for (unsigned i = 0; i < WaitStates; ++i)
207 insertNoopInBundle(CurrCycleInstr, TII);
208
209 // It’s unnecessary to track more than MaxLookAhead instructions. Since we
210 // include the bundled MI directly after, only add a maximum of
211 // (MaxLookAhead - 1) noops to EmittedInstrs.
212 for (unsigned i = 0, e = std::min(WaitStates, MaxLookAhead - 1); i < e; ++i)
213 EmittedInstrs.push_front(nullptr);
214
215 EmittedInstrs.push_front(CurrCycleInstr);
216 EmittedInstrs.resize(MaxLookAhead);
217 }
218 CurrCycleInstr = nullptr;
219}
220
Tom Stellardcb6ba622016-04-30 00:23:06 +0000221unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000222 IsHazardRecognizerMode = false;
223 return PreEmitNoopsCommon(SU->getInstr());
Tom Stellardcb6ba622016-04-30 00:23:06 +0000224}
225
226unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000227 IsHazardRecognizerMode = true;
228 CurrCycleInstr = MI;
229 unsigned W = PreEmitNoopsCommon(MI);
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000230 fixHazards(MI);
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000231 CurrCycleInstr = nullptr;
232 return W;
233}
234
235unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) {
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000236 if (MI->isBundle())
237 return 0;
238
Matt Arsenaulte823d922017-02-18 18:29:53 +0000239 int WaitStates = std::max(0, checkAnyInstHazards(MI));
240
Aaron Ballman5c190d02016-05-02 14:48:03 +0000241 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000242 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000243
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000244 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
245 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000246
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000247 if (ST.hasNSAtoVMEMBug())
248 WaitStates = std::max(WaitStates, checkNSAtoVMEMHazard(MI));
249
250 if (ST.hasNoDataDepHazard())
251 return WaitStates;
252
253 if (SIInstrInfo::isVALU(*MI))
254 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
255
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000256 if (SIInstrInfo::isDPP(*MI))
257 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000258
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000259 if (isDivFMas(MI->getOpcode()))
260 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000261
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000262 if (isRWLane(MI->getOpcode()))
263 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
Tom Stellard5ab61542016-10-07 23:42:48 +0000264
Mark Searlesd29f24a2017-12-07 20:34:25 +0000265 if (MI->isInlineAsm())
266 return std::max(WaitStates, checkInlineAsmHazards(MI));
267
Tom Stellard961811c2016-10-15 00:58:14 +0000268 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000269 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000270
Tom Stellard30d30822016-10-27 20:39:09 +0000271 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000272 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000273
Tom Stellardaea899e2016-10-27 23:50:21 +0000274 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000275 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000276
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000277 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
278 isSMovRel(MI->getOpcode())))
279 return std::max(WaitStates, checkReadM0Hazards(MI));
280
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000281 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000282 return std::max(WaitStates, checkReadM0Hazards(MI));
283
284 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000285}
286
287void GCNHazardRecognizer::EmitNoop() {
288 EmittedInstrs.push_front(nullptr);
289}
290
291void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000292 // When the scheduler detects a stall, it will call AdvanceCycle() without
293 // emitting any instructions.
294 if (!CurrCycleInstr)
295 return;
296
Carl Ritsonf898edd2018-09-10 10:14:48 +0000297 // Do not track non-instructions which do not affect the wait states.
298 // If included, these instructions can lead to buffer overflow such that
299 // detectable hazards are missed.
David Stuttard81eec582019-03-05 10:25:16 +0000300 if (CurrCycleInstr->isImplicitDef() || CurrCycleInstr->isDebugInstr() ||
301 CurrCycleInstr->isKill())
Carl Ritsonf898edd2018-09-10 10:14:48 +0000302 return;
303
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000304 if (CurrCycleInstr->isBundle()) {
305 processBundle();
306 return;
307 }
308
Matt Arsenault59ece952017-03-17 21:36:28 +0000309 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000310
311 // Keep track of emitted instructions
312 EmittedInstrs.push_front(CurrCycleInstr);
313
314 // Add a nullptr for each additional wait state after the first. Make sure
315 // not to add more than getMaxLookAhead() items to the list, since we
316 // truncate the list to that size right after this loop.
317 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
318 i < e; ++i) {
319 EmittedInstrs.push_front(nullptr);
320 }
321
322 // getMaxLookahead() is the largest number of wait states we will ever need
323 // to insert, so there is no point in keeping track of more than that many
324 // wait states.
325 EmittedInstrs.resize(getMaxLookAhead());
326
327 CurrCycleInstr = nullptr;
328}
329
330void GCNHazardRecognizer::RecedeCycle() {
331 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
332}
333
334//===----------------------------------------------------------------------===//
335// Helper Functions
336//===----------------------------------------------------------------------===//
337
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000338typedef function_ref<bool(MachineInstr *, int WaitStates)> IsExpiredFn;
339
340// Returns a minimum wait states since \p I walking all predecessors.
341// Only scans until \p IsExpired does not return true.
342// Can only be run in a hazard recognizer mode.
343static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard,
344 MachineBasicBlock *MBB,
345 MachineBasicBlock::reverse_instr_iterator I,
346 int WaitStates,
347 IsExpiredFn IsExpired,
348 DenseSet<const MachineBasicBlock *> &Visited) {
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000349 for (auto E = MBB->instr_rend(); I != E; ++I) {
350 // Don't add WaitStates for parent BUNDLE instructions.
351 if (I->isBundle())
352 continue;
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000353
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000354 if (IsHazard(&*I))
355 return WaitStates;
356
357 if (I->isInlineAsm() || I->isImplicitDef() || I->isDebugInstr())
358 continue;
359
360 WaitStates += SIInstrInfo::getNumWaitStates(*I);
361
362 if (IsExpired(&*I, WaitStates))
363 return std::numeric_limits<int>::max();
364 }
365
366 int MinWaitStates = WaitStates;
367 bool Found = false;
368 for (MachineBasicBlock *Pred : MBB->predecessors()) {
369 if (!Visited.insert(Pred).second)
370 continue;
371
372 int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(),
373 WaitStates, IsExpired, Visited);
374
375 if (W == std::numeric_limits<int>::max())
376 continue;
377
378 MinWaitStates = Found ? std::min(MinWaitStates, W) : W;
379 if (IsExpired(nullptr, MinWaitStates))
380 return MinWaitStates;
381
382 Found = true;
383 }
384
385 if (Found)
386 return MinWaitStates;
387
388 return std::numeric_limits<int>::max();
389}
390
391static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard,
392 MachineInstr *MI,
393 IsExpiredFn IsExpired) {
394 DenseSet<const MachineBasicBlock *> Visited;
395 return getWaitStatesSince(IsHazard, MI->getParent(),
396 std::next(MI->getReverseIterator()),
397 0, IsExpired, Visited);
398}
399
400int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) {
401 if (IsHazardRecognizerMode) {
402 auto IsExpiredFn = [Limit] (MachineInstr *, int WaitStates) {
403 return WaitStates >= Limit;
404 };
405 return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn);
406 }
407
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000408 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000409 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000410 if (MI) {
411 if (IsHazard(MI))
412 return WaitStates;
413
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000414 if (MI->isInlineAsm())
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000415 continue;
416 }
Tom Stellard961811c2016-10-15 00:58:14 +0000417 ++WaitStates;
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000418
419 if (WaitStates >= Limit)
420 break;
Tom Stellard961811c2016-10-15 00:58:14 +0000421 }
422 return std::numeric_limits<int>::max();
423}
424
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000425int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg,
426 IsHazardFn IsHazardDef,
427 int Limit) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000428 const SIRegisterInfo *TRI = ST.getRegisterInfo();
429
430 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
431 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
432 };
433
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000434 return getWaitStatesSince(IsHazardFn, Limit);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000435}
436
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000437int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard,
438 int Limit) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000439 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
440 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
441 };
442
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000443 return getWaitStatesSince(IsHazardFn, Limit);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000444}
445
Tom Stellardcb6ba622016-04-30 00:23:06 +0000446//===----------------------------------------------------------------------===//
447// No-op Hazard Detection
448//===----------------------------------------------------------------------===//
449
Matt Arsenault03c67d12017-11-17 04:18:24 +0000450static void addRegUnits(const SIRegisterInfo &TRI,
451 BitVector &BV, unsigned Reg) {
452 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
453 BV.set(*RUI);
454}
455
456static void addRegsToSet(const SIRegisterInfo &TRI,
457 iterator_range<MachineInstr::const_mop_iterator> Ops,
458 BitVector &Set) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000459 for (const MachineOperand &Op : Ops) {
460 if (Op.isReg())
Matt Arsenault03c67d12017-11-17 04:18:24 +0000461 addRegUnits(TRI, Set, Op.getReg());
Tom Stellard1f520e52016-05-02 17:39:06 +0000462 }
463}
464
Matt Arsenault03c67d12017-11-17 04:18:24 +0000465void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) {
466 // XXX: Do we need to worry about implicit operands
467 addRegsToSet(TRI, MI.defs(), ClauseDefs);
468 addRegsToSet(TRI, MI.uses(), ClauseUses);
469}
470
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000471int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
Matt Arsenault03c67d12017-11-17 04:18:24 +0000472 // SMEM soft clause are only present on VI+, and only matter if xnack is
473 // enabled.
474 if (!ST.isXNACKEnabled())
Tom Stellard1f520e52016-05-02 17:39:06 +0000475 return 0;
476
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000477 bool IsSMRD = TII.isSMRD(*MEM);
478
Matt Arsenault03c67d12017-11-17 04:18:24 +0000479 resetClause();
480
Tom Stellard1f520e52016-05-02 17:39:06 +0000481 // A soft-clause is any group of consecutive SMEM instructions. The
482 // instructions in this group may return out of order and/or may be
483 // replayed (i.e. the same instruction issued more than once).
484 //
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000485 // In order to handle these situations correctly we need to make sure that
486 // when a clause has more than one instruction, no instruction in the clause
487 // writes to a register that is read by another instruction in the clause
Tom Stellard1f520e52016-05-02 17:39:06 +0000488 // (including itself). If we encounter this situaion, we need to break the
489 // clause by inserting a non SMEM instruction.
490
Tom Stellard1f520e52016-05-02 17:39:06 +0000491 for (MachineInstr *MI : EmittedInstrs) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000492 // When we hit a non-SMEM instruction then we have passed the start of the
493 // clause and we can stop.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000494 if (!MI)
495 break;
496
497 if (IsSMRD != SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000498 break;
499
Matt Arsenault03c67d12017-11-17 04:18:24 +0000500 addClauseInst(*MI);
Tom Stellard1f520e52016-05-02 17:39:06 +0000501 }
502
Matt Arsenault03c67d12017-11-17 04:18:24 +0000503 if (ClauseDefs.none())
Tom Stellard1f520e52016-05-02 17:39:06 +0000504 return 0;
505
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000506 // We need to make sure not to put loads and stores in the same clause if they
507 // use the same address. For now, just start a new clause whenever we see a
508 // store.
509 if (MEM->mayStore())
Tom Stellard1f520e52016-05-02 17:39:06 +0000510 return 1;
511
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000512 addClauseInst(*MEM);
Tom Stellard1f520e52016-05-02 17:39:06 +0000513
514 // If the set of defs and uses intersect then we cannot add this instruction
515 // to the clause, so we have a hazard.
Matt Arsenault03c67d12017-11-17 04:18:24 +0000516 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
Tom Stellard1f520e52016-05-02 17:39:06 +0000517}
518
Tom Stellardcb6ba622016-04-30 00:23:06 +0000519int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000520 int WaitStatesNeeded = 0;
521
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000522 WaitStatesNeeded = checkSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000523
524 // This SMRD hazard only affects SI.
Tom Stellard5bfbae52018-07-11 20:59:01 +0000525 if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000526 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000527
528 // A read of an SGPR by SMRD instruction requires 4 wait states when the
529 // SGPR was written by a VALU instruction.
530 int SmrdSgprWaitStates = 4;
Matt Arsenault59ece952017-03-17 21:36:28 +0000531 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000532 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
533
Matt Arsenault4512d0a2017-11-17 04:18:26 +0000534 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000535
536 for (const MachineOperand &Use : SMRD->uses()) {
537 if (!Use.isReg())
538 continue;
539 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000540 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn,
541 SmrdSgprWaitStates);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000542 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
Marek Olsak22322432017-10-26 14:43:02 +0000543
544 // This fixes what appears to be undocumented hardware behavior in SI where
545 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
546 // needs some number of nops in between. We don't know how many we need, but
547 // let's use 4. This wasn't discovered before probably because the only
548 // case when this happens is when we expand a 64-bit pointer into a full
549 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
550 // probably never encountered in the closed-source land.
551 if (IsBufferSMRD) {
552 int WaitStatesNeededForUse =
553 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000554 IsBufferHazardDefFn,
555 SmrdSgprWaitStates);
Marek Olsak22322432017-10-26 14:43:02 +0000556 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
557 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000558 }
Marek Olsak22322432017-10-26 14:43:02 +0000559
Tom Stellardcb6ba622016-04-30 00:23:06 +0000560 return WaitStatesNeeded;
561}
562
563int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000564 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000565 return 0;
566
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000567 int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000568
569 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
570 // SGPR was written by a VALU Instruction.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000571 const int VmemSgprWaitStates = 5;
572 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Tom Stellardcb6ba622016-04-30 00:23:06 +0000573 for (const MachineOperand &Use : VMEM->uses()) {
574 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
575 continue;
576
577 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000578 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn,
579 VmemSgprWaitStates);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000580 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
581 }
582 return WaitStatesNeeded;
583}
Tom Stellarda27007e2016-05-02 16:23:09 +0000584
585int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000586 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000587 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000588
Connor Abbott00755362017-08-04 01:09:43 +0000589 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000590 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000591 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000592 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000593 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000594
595 for (const MachineOperand &Use : DPP->uses()) {
596 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
597 continue;
598 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000599 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
600 [](MachineInstr *) { return true; },
601 DppVgprWaitStates);
Tom Stellarda27007e2016-05-02 16:23:09 +0000602 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
603 }
604
Connor Abbott00755362017-08-04 01:09:43 +0000605 WaitStatesNeeded = std::max(
606 WaitStatesNeeded,
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000607 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn,
608 DppExecWaitStates));
Connor Abbott00755362017-08-04 01:09:43 +0000609
Tom Stellarda27007e2016-05-02 16:23:09 +0000610 return WaitStatesNeeded;
611}
Tom Stellard5ab61542016-10-07 23:42:48 +0000612
613int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
614 const SIInstrInfo *TII = ST.getInstrInfo();
615
616 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
617 // instruction.
618 const int DivFMasWaitStates = 4;
619 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000620 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn,
621 DivFMasWaitStates);
Tom Stellard5ab61542016-10-07 23:42:48 +0000622
623 return DivFMasWaitStates - WaitStatesNeeded;
624}
Tom Stellard961811c2016-10-15 00:58:14 +0000625
626int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
627 const SIInstrInfo *TII = ST.getInstrInfo();
628 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
629
630 const int GetRegWaitStates = 2;
631 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
632 return GetRegHWReg == getHWReg(TII, *MI);
633 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000634 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates);
Tom Stellard961811c2016-10-15 00:58:14 +0000635
636 return GetRegWaitStates - WaitStatesNeeded;
637}
Tom Stellard30d30822016-10-27 20:39:09 +0000638
639int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
640 const SIInstrInfo *TII = ST.getInstrInfo();
641 unsigned HWReg = getHWReg(TII, *SetRegInstr);
642
643 const int SetRegWaitStates =
644 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
645 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
646 return HWReg == getHWReg(TII, *MI);
647 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000648 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates);
Tom Stellard30d30822016-10-27 20:39:09 +0000649 return SetRegWaitStates - WaitStatesNeeded;
650}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000651
652int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
653 if (!MI.mayStore())
654 return -1;
655
656 const SIInstrInfo *TII = ST.getInstrInfo();
657 unsigned Opcode = MI.getOpcode();
658 const MCInstrDesc &Desc = MI.getDesc();
659
660 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
661 int VDataRCID = -1;
662 if (VDataIdx != -1)
663 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
664
665 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000666 // There is no hazard if the instruction does not use vector regs
667 // (like wbinvl1)
668 if (VDataIdx == -1)
669 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000670 // For MUBUF/MTBUF instructions this hazard only exists if the
671 // instruction is not using a register in the soffset field.
672 const MachineOperand *SOffset =
673 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
674 // If we have no soffset operand, then assume this field has been
675 // hardcoded to zero.
676 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
677 (!SOffset || !SOffset->isReg()))
678 return VDataIdx;
679 }
680
681 // MIMG instructions create a hazard if they don't use a 256-bit T# and
682 // the store size is greater than 8 bytes and they have more than two bits
683 // of their dmask set.
684 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
685 if (TII->isMIMG(MI)) {
686 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
687 assert(SRsrcIdx != -1 &&
688 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000689 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000690 }
691
692 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000693 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000694 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
695 return DataIdx;
696 }
697
698 return -1;
699}
700
Mark Searlesd29f24a2017-12-07 20:34:25 +0000701int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def,
702 const MachineRegisterInfo &MRI) {
703 // Helper to check for the hazard where VMEM instructions that store more than
704 // 8 bytes can have there store data over written by the next instruction.
705 const SIRegisterInfo *TRI = ST.getRegisterInfo();
706
707 const int VALUWaitStates = 1;
708 int WaitStatesNeeded = 0;
709
710 if (!TRI->isVGPR(MRI, Def.getReg()))
711 return WaitStatesNeeded;
712 unsigned Reg = Def.getReg();
713 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
714 int DataIdx = createsVALUHazard(*MI);
715 return DataIdx >= 0 &&
716 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
717 };
718 int WaitStatesNeededForDef =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000719 VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates);
Mark Searlesd29f24a2017-12-07 20:34:25 +0000720 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
721
722 return WaitStatesNeeded;
723}
724
Tom Stellardb133fbb2016-10-27 23:05:31 +0000725int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
726 // This checks for the hazard where VMEM instructions that store more than
727 // 8 bytes can have there store data over written by the next instruction.
728 if (!ST.has12DWordStoreHazard())
729 return 0;
730
Mark Searlesd29f24a2017-12-07 20:34:25 +0000731 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellardb133fbb2016-10-27 23:05:31 +0000732 int WaitStatesNeeded = 0;
733
734 for (const MachineOperand &Def : VALU->defs()) {
Mark Searlesd29f24a2017-12-07 20:34:25 +0000735 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000736 }
Mark Searlesd29f24a2017-12-07 20:34:25 +0000737
738 return WaitStatesNeeded;
739}
740
741int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) {
742 // This checks for hazards associated with inline asm statements.
743 // Since inline asms can contain just about anything, we use this
744 // to call/leverage other check*Hazard routines. Note that
745 // this function doesn't attempt to address all possible inline asm
746 // hazards (good luck), but is a collection of what has been
747 // problematic thus far.
748
749 // see checkVALUHazards()
750 if (!ST.has12DWordStoreHazard())
751 return 0;
752
753 const MachineRegisterInfo &MRI = MF.getRegInfo();
754 int WaitStatesNeeded = 0;
755
756 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands();
757 I != E; ++I) {
758 const MachineOperand &Op = IA->getOperand(I);
759 if (Op.isReg() && Op.isDef()) {
760 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI));
761 }
762 }
763
Tom Stellardb133fbb2016-10-27 23:05:31 +0000764 return WaitStatesNeeded;
765}
Tom Stellard04051b52016-10-27 23:42:29 +0000766
767int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
768 const SIInstrInfo *TII = ST.getInstrInfo();
769 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Mark Searlesd29f24a2017-12-07 20:34:25 +0000770 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard04051b52016-10-27 23:42:29 +0000771
772 const MachineOperand *LaneSelectOp =
773 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
774
775 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
776 return 0;
777
778 unsigned LaneSelectReg = LaneSelectOp->getReg();
779 auto IsHazardFn = [TII] (MachineInstr *MI) {
780 return TII->isVALU(*MI);
781 };
782
783 const int RWLaneWaitStates = 4;
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000784 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn,
785 RWLaneWaitStates);
Tom Stellard04051b52016-10-27 23:42:29 +0000786 return RWLaneWaitStates - WaitStatesSince;
787}
Tom Stellardaea899e2016-10-27 23:50:21 +0000788
789int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000790 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
791 return 0;
792
793 const SIInstrInfo *TII = ST.getInstrInfo();
794
795 const int RFEWaitStates = 1;
796
797 auto IsHazardFn = [TII] (MachineInstr *MI) {
798 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
799 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000800 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates);
Tom Stellardaea899e2016-10-27 23:50:21 +0000801 return RFEWaitStates - WaitStatesNeeded;
802}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000803
804int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000805 if (MI->isDebugInstr())
Matt Arsenaulte823d922017-02-18 18:29:53 +0000806 return 0;
807
808 const SIRegisterInfo *TRI = ST.getRegisterInfo();
809 if (!ST.hasSMovFedHazard())
810 return 0;
811
812 // Check for any instruction reading an SGPR after a write from
813 // s_mov_fed_b32.
814 int MovFedWaitStates = 1;
815 int WaitStatesNeeded = 0;
816
817 for (const MachineOperand &Use : MI->uses()) {
818 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
819 continue;
820 auto IsHazardFn = [] (MachineInstr *MI) {
821 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
822 };
823 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000824 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn,
825 MovFedWaitStates);
Matt Arsenaulte823d922017-02-18 18:29:53 +0000826 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
827 }
828
829 return WaitStatesNeeded;
830}
831
832int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000833 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000834 const int SMovRelWaitStates = 1;
Matt Arsenaulte823d922017-02-18 18:29:53 +0000835 auto IsHazardFn = [TII] (MachineInstr *MI) {
836 return TII->isSALU(*MI);
837 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000838 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn,
839 SMovRelWaitStates);
Matt Arsenaulte823d922017-02-18 18:29:53 +0000840}
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000841
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000842void GCNHazardRecognizer::fixHazards(MachineInstr *MI) {
843 fixVMEMtoScalarWriteHazards(MI);
Stanislav Mekhanoshin5f581c92019-06-12 17:52:51 +0000844 fixVcmpxPermlaneHazards(MI);
Austin Kerbow8a3d3a92019-05-07 22:12:15 +0000845 fixSMEMtoVectorWriteHazards(MI);
846 fixVcmpxExecWARHazard(MI);
847 fixLdsBranchVmemWARHazard(MI);
848}
849
Stanislav Mekhanoshin5f581c92019-06-12 17:52:51 +0000850bool GCNHazardRecognizer::fixVcmpxPermlaneHazards(MachineInstr *MI) {
851 if (!ST.hasVcmpxPermlaneHazard() || !isPermlane(*MI))
852 return false;
853
854 const SIInstrInfo *TII = ST.getInstrInfo();
855 auto IsHazardFn = [TII] (MachineInstr *MI) {
856 return TII->isVOPC(*MI);
857 };
858
859 auto IsExpiredFn = [] (MachineInstr *MI, int) {
860 if (!MI)
861 return false;
862 unsigned Opc = MI->getOpcode();
863 return SIInstrInfo::isVALU(*MI) &&
864 Opc != AMDGPU::V_NOP_e32 &&
865 Opc != AMDGPU::V_NOP_e64 &&
866 Opc != AMDGPU::V_NOP_sdwa;
867 };
868
869 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) ==
870 std::numeric_limits<int>::max())
871 return false;
872
873 // V_NOP will be discarded by SQ.
874 // Use V_MOB_B32 v?, v?. Register must be alive so use src0 of V_PERMLANE*
875 // which is always a VGPR and available.
876 auto *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0);
877 unsigned Reg = Src0->getReg();
878 bool IsUndef = Src0->isUndef();
879 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
880 TII->get(AMDGPU::V_MOV_B32_e32))
881 .addReg(Reg, RegState::Define | (IsUndef ? RegState::Dead : 0))
882 .addReg(Reg, IsUndef ? RegState::Undef : RegState::Kill);
883
884 return true;
885}
886
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000887bool GCNHazardRecognizer::fixVMEMtoScalarWriteHazards(MachineInstr *MI) {
888 if (!ST.hasVMEMtoScalarWriteHazard())
889 return false;
890
891 if (!SIInstrInfo::isSALU(*MI) && !SIInstrInfo::isSMRD(*MI))
892 return false;
893
894 if (MI->getNumDefs() == 0)
895 return false;
896
897 const SIRegisterInfo *TRI = ST.getRegisterInfo();
898
899 auto IsHazardFn = [TRI, MI] (MachineInstr *I) {
900 if (!SIInstrInfo::isVMEM(*I) && !SIInstrInfo::isDS(*I) &&
901 !SIInstrInfo::isFLAT(*I))
902 return false;
903
904 for (const MachineOperand &Def : MI->defs()) {
905 MachineOperand *Op = I->findRegisterUseOperand(Def.getReg(), false, TRI);
906 if (!Op || (Op->isImplicit() && Op->getReg() == AMDGPU::EXEC))
907 continue;
908 return true;
909 }
910 return false;
911 };
912
913 auto IsExpiredFn = [] (MachineInstr *MI, int) {
914 return MI && (SIInstrInfo::isVALU(*MI) ||
915 (MI->getOpcode() == AMDGPU::S_WAITCNT &&
916 !MI->getOperand(0).getImm()));
917 };
918
919 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) ==
920 std::numeric_limits<int>::max())
921 return false;
922
923 const SIInstrInfo *TII = ST.getInstrInfo();
924 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(AMDGPU::V_NOP_e32));
925 return true;
926}
927
928bool GCNHazardRecognizer::fixSMEMtoVectorWriteHazards(MachineInstr *MI) {
929 if (!ST.hasSMEMtoVectorWriteHazard())
930 return false;
931
932 if (!SIInstrInfo::isVALU(*MI))
933 return false;
934
935 unsigned SDSTName;
936 switch (MI->getOpcode()) {
937 case AMDGPU::V_READLANE_B32:
938 case AMDGPU::V_READFIRSTLANE_B32:
939 SDSTName = AMDGPU::OpName::vdst;
940 break;
941 default:
942 SDSTName = AMDGPU::OpName::sdst;
943 break;
944 }
945
946 const SIInstrInfo *TII = ST.getInstrInfo();
947 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Carl Ritson34e95ce2019-05-20 07:20:12 +0000948 const AMDGPU::IsaVersion IV = AMDGPU::getIsaVersion(ST.getCPU());
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000949 const MachineOperand *SDST = TII->getNamedOperand(*MI, SDSTName);
950 if (!SDST) {
Stanislav Mekhanoshin5ddd5642019-05-04 06:40:20 +0000951 for (const auto &MO : MI->implicit_operands()) {
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000952 if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg()))) {
953 SDST = &MO;
954 break;
955 }
956 }
957 }
958
959 if (!SDST)
960 return false;
961
962 const unsigned SDSTReg = SDST->getReg();
963 auto IsHazardFn = [SDSTReg, TRI] (MachineInstr *I) {
964 return SIInstrInfo::isSMRD(*I) && I->readsRegister(SDSTReg, TRI);
965 };
966
Carl Ritson34e95ce2019-05-20 07:20:12 +0000967 auto IsExpiredFn = [TII, IV] (MachineInstr *MI, int) {
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000968 if (MI) {
969 if (TII->isSALU(*MI)) {
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000970 switch (MI->getOpcode()) {
971 case AMDGPU::S_SETVSKIP:
972 case AMDGPU::S_VERSION:
973 case AMDGPU::S_WAITCNT_VSCNT:
974 case AMDGPU::S_WAITCNT_VMCNT:
975 case AMDGPU::S_WAITCNT_EXPCNT:
Carl Ritson34e95ce2019-05-20 07:20:12 +0000976 // These instructions cannot not mitigate the hazard.
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000977 return false;
Carl Ritson34e95ce2019-05-20 07:20:12 +0000978 case AMDGPU::S_WAITCNT_LGKMCNT:
979 // Reducing lgkmcnt count to 0 always mitigates the hazard.
980 return (MI->getOperand(1).getImm() == 0) &&
981 (MI->getOperand(0).getReg() == AMDGPU::SGPR_NULL);
982 case AMDGPU::S_WAITCNT: {
983 const int64_t Imm = MI->getOperand(0).getImm();
984 AMDGPU::Waitcnt Decoded = AMDGPU::decodeWaitcnt(IV, Imm);
985 return (Decoded.LgkmCnt == 0);
986 }
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000987 default:
Carl Ritson34e95ce2019-05-20 07:20:12 +0000988 // SOPP instructions cannot mitigate the hazard.
989 if (TII->isSOPP(*MI))
990 return false;
991 // At this point the SALU can be assumed to mitigate the hazard
992 // because either:
993 // (a) it is independent of the at risk SMEM (breaking chain),
994 // or
995 // (b) it is dependent on the SMEM, in which case an appropriate
996 // s_waitcnt lgkmcnt _must_ exist between it and the at risk
997 // SMEM instruction.
Stanislav Mekhanoshin51d14152019-05-04 04:30:57 +0000998 return true;
999 }
1000 }
1001 }
1002 return false;
1003 };
1004
1005 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) ==
1006 std::numeric_limits<int>::max())
1007 return false;
1008
1009 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1010 TII->get(AMDGPU::S_MOV_B32), AMDGPU::SGPR_NULL)
1011 .addImm(0);
1012 return true;
1013}
1014
1015bool GCNHazardRecognizer::fixVcmpxExecWARHazard(MachineInstr *MI) {
1016 if (!ST.hasVcmpxExecWARHazard() || !SIInstrInfo::isVALU(*MI))
1017 return false;
1018
1019 const SIRegisterInfo *TRI = ST.getRegisterInfo();
1020 if (!MI->modifiesRegister(AMDGPU::EXEC, TRI))
1021 return false;
1022
1023 auto IsHazardFn = [TRI] (MachineInstr *I) {
1024 if (SIInstrInfo::isVALU(*I))
1025 return false;
1026 return I->readsRegister(AMDGPU::EXEC, TRI);
1027 };
1028
1029 const SIInstrInfo *TII = ST.getInstrInfo();
1030 auto IsExpiredFn = [TII, TRI] (MachineInstr *MI, int) {
1031 if (!MI)
1032 return false;
1033 if (SIInstrInfo::isVALU(*MI)) {
1034 if (TII->getNamedOperand(*MI, AMDGPU::OpName::sdst))
1035 return true;
1036 for (auto MO : MI->implicit_operands())
1037 if (MO.isDef() && TRI->isSGPRClass(TRI->getPhysRegClass(MO.getReg())))
1038 return true;
1039 }
1040 if (MI->getOpcode() == AMDGPU::S_WAITCNT_DEPCTR &&
1041 (MI->getOperand(0).getImm() & 0xfffe) == 0xfffe)
1042 return true;
1043 return false;
1044 };
1045
1046 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) ==
1047 std::numeric_limits<int>::max())
1048 return false;
1049
1050 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1051 TII->get(AMDGPU::S_WAITCNT_DEPCTR))
1052 .addImm(0xfffe);
1053 return true;
1054}
1055
1056bool GCNHazardRecognizer::fixLdsBranchVmemWARHazard(MachineInstr *MI) {
1057 if (!ST.hasLdsBranchVmemWARHazard())
1058 return false;
1059
1060 auto IsHazardInst = [] (const MachineInstr *MI) {
1061 if (SIInstrInfo::isDS(*MI))
1062 return 1;
1063 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isSegmentSpecificFLAT(*MI))
1064 return 2;
1065 return 0;
1066 };
1067
1068 auto InstType = IsHazardInst(MI);
1069 if (!InstType)
1070 return false;
1071
1072 auto IsExpiredFn = [&IsHazardInst] (MachineInstr *I, int) {
1073 return I && (IsHazardInst(I) ||
1074 (I->getOpcode() == AMDGPU::S_WAITCNT_VSCNT &&
1075 I->getOperand(0).getReg() == AMDGPU::SGPR_NULL &&
1076 !I->getOperand(1).getImm()));
1077 };
1078
1079 auto IsHazardFn = [InstType, &IsHazardInst] (MachineInstr *I) {
1080 if (!I->isBranch())
1081 return false;
1082
1083 auto IsHazardFn = [InstType, IsHazardInst] (MachineInstr *I) {
1084 auto InstType2 = IsHazardInst(I);
1085 return InstType2 && InstType != InstType2;
1086 };
1087
1088 auto IsExpiredFn = [InstType, &IsHazardInst] (MachineInstr *I, int) {
1089 if (!I)
1090 return false;
1091
1092 auto InstType2 = IsHazardInst(I);
1093 if (InstType == InstType2)
1094 return true;
1095
1096 return I->getOpcode() == AMDGPU::S_WAITCNT_VSCNT &&
1097 I->getOperand(0).getReg() == AMDGPU::SGPR_NULL &&
1098 !I->getOperand(1).getImm();
1099 };
1100
1101 return ::getWaitStatesSince(IsHazardFn, I, IsExpiredFn) !=
1102 std::numeric_limits<int>::max();
1103 };
1104
1105 if (::getWaitStatesSince(IsHazardFn, MI, IsExpiredFn) ==
1106 std::numeric_limits<int>::max())
1107 return false;
1108
1109 const SIInstrInfo *TII = ST.getInstrInfo();
1110 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
1111 TII->get(AMDGPU::S_WAITCNT_VSCNT))
1112 .addReg(AMDGPU::SGPR_NULL, RegState::Undef)
1113 .addImm(0);
1114
1115 return true;
1116}
1117
1118int GCNHazardRecognizer::checkNSAtoVMEMHazard(MachineInstr *MI) {
1119 int NSAtoVMEMWaitStates = 1;
1120
1121 if (!ST.hasNSAtoVMEMBug())
1122 return 0;
1123
1124 if (!SIInstrInfo::isMUBUF(*MI) && !SIInstrInfo::isMTBUF(*MI))
1125 return 0;
1126
1127 const SIInstrInfo *TII = ST.getInstrInfo();
1128 const auto *Offset = TII->getNamedOperand(*MI, AMDGPU::OpName::offset);
1129 if (!Offset || (Offset->getImm() & 6) == 0)
1130 return 0;
1131
1132 auto IsHazardFn = [TII] (MachineInstr *I) {
1133 if (!SIInstrInfo::isMIMG(*I))
1134 return false;
1135 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(I->getOpcode());
1136 return Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA &&
1137 TII->getInstSizeInBytes(*I) >= 16;
1138 };
1139
1140 return NSAtoVMEMWaitStates - getWaitStatesSince(IsHazardFn, 1);
1141}