blob: 4af8a457a0e5dfe02d7786280ead0d901ce64156 [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"
23#include "llvm/CodeGen/MachineOperand.h"
Tom Stellardcb6ba622016-04-30 00:23:06 +000024#include "llvm/CodeGen/ScheduleDAG.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000025#include "llvm/MC/MCInstrDesc.h"
26#include "llvm/Support/ErrorHandling.h"
27#include <algorithm>
28#include <cassert>
29#include <limits>
30#include <set>
31#include <vector>
Tom Stellardcb6ba622016-04-30 00:23:06 +000032
33using namespace llvm;
34
35//===----------------------------------------------------------------------===//
36// Hazard Recoginizer Implementation
37//===----------------------------------------------------------------------===//
38
39GCNHazardRecognizer::GCNHazardRecognizer(const MachineFunction &MF) :
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +000040 IsHazardRecognizerMode(false),
Tom Stellardcb6ba622016-04-30 00:23:06 +000041 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) {
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000176 IsHazardRecognizerMode = false;
177 return PreEmitNoopsCommon(SU->getInstr());
Tom Stellardcb6ba622016-04-30 00:23:06 +0000178}
179
180unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000181 IsHazardRecognizerMode = true;
182 CurrCycleInstr = MI;
183 unsigned W = PreEmitNoopsCommon(MI);
184 CurrCycleInstr = nullptr;
185 return W;
186}
187
188unsigned GCNHazardRecognizer::PreEmitNoopsCommon(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000189 int WaitStates = std::max(0, checkAnyInstHazards(MI));
190
Aaron Ballman5c190d02016-05-02 14:48:03 +0000191 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000192 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000193
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000194 if (SIInstrInfo::isVALU(*MI))
195 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000196
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000197 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
198 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000199
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000200 if (SIInstrInfo::isDPP(*MI))
201 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000202
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000203 if (isDivFMas(MI->getOpcode()))
204 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000205
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000206 if (isRWLane(MI->getOpcode()))
207 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
Tom Stellard5ab61542016-10-07 23:42:48 +0000208
Mark Searlesd29f24a2017-12-07 20:34:25 +0000209 if (MI->isInlineAsm())
210 return std::max(WaitStates, checkInlineAsmHazards(MI));
211
Tom Stellard961811c2016-10-15 00:58:14 +0000212 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000213 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000214
Tom Stellard30d30822016-10-27 20:39:09 +0000215 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000216 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000217
Tom Stellardaea899e2016-10-27 23:50:21 +0000218 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000219 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000220
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000221 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
222 isSMovRel(MI->getOpcode())))
223 return std::max(WaitStates, checkReadM0Hazards(MI));
224
Marek Olsakc5cec5e2019-01-16 15:43:53 +0000225 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(TII, *MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000226 return std::max(WaitStates, checkReadM0Hazards(MI));
227
228 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000229}
230
231void GCNHazardRecognizer::EmitNoop() {
232 EmittedInstrs.push_front(nullptr);
233}
234
235void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000236 // When the scheduler detects a stall, it will call AdvanceCycle() without
237 // emitting any instructions.
238 if (!CurrCycleInstr)
239 return;
240
Carl Ritsonf898edd2018-09-10 10:14:48 +0000241 // Do not track non-instructions which do not affect the wait states.
242 // If included, these instructions can lead to buffer overflow such that
243 // detectable hazards are missed.
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000244 if (CurrCycleInstr->isImplicitDef())
Carl Ritsonf898edd2018-09-10 10:14:48 +0000245 return;
246 else if (CurrCycleInstr->isDebugInstr())
247 return;
248
Matt Arsenault59ece952017-03-17 21:36:28 +0000249 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000250
251 // Keep track of emitted instructions
252 EmittedInstrs.push_front(CurrCycleInstr);
253
254 // Add a nullptr for each additional wait state after the first. Make sure
255 // not to add more than getMaxLookAhead() items to the list, since we
256 // truncate the list to that size right after this loop.
257 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
258 i < e; ++i) {
259 EmittedInstrs.push_front(nullptr);
260 }
261
262 // getMaxLookahead() is the largest number of wait states we will ever need
263 // to insert, so there is no point in keeping track of more than that many
264 // wait states.
265 EmittedInstrs.resize(getMaxLookAhead());
266
267 CurrCycleInstr = nullptr;
268}
269
270void GCNHazardRecognizer::RecedeCycle() {
271 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
272}
273
274//===----------------------------------------------------------------------===//
275// Helper Functions
276//===----------------------------------------------------------------------===//
277
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000278typedef function_ref<bool(MachineInstr *, int WaitStates)> IsExpiredFn;
279
280// Returns a minimum wait states since \p I walking all predecessors.
281// Only scans until \p IsExpired does not return true.
282// Can only be run in a hazard recognizer mode.
283static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard,
284 MachineBasicBlock *MBB,
285 MachineBasicBlock::reverse_instr_iterator I,
286 int WaitStates,
287 IsExpiredFn IsExpired,
288 DenseSet<const MachineBasicBlock *> &Visited) {
289
290 for (auto E = MBB->rend() ; I != E; ++I) {
291 if (IsHazard(&*I))
292 return WaitStates;
293
294 if (I->isInlineAsm() || I->isImplicitDef() || I->isDebugInstr())
295 continue;
296
297 WaitStates += SIInstrInfo::getNumWaitStates(*I);
298
299 if (IsExpired(&*I, WaitStates))
300 return std::numeric_limits<int>::max();
301 }
302
303 int MinWaitStates = WaitStates;
304 bool Found = false;
305 for (MachineBasicBlock *Pred : MBB->predecessors()) {
306 if (!Visited.insert(Pred).second)
307 continue;
308
309 int W = getWaitStatesSince(IsHazard, Pred, Pred->instr_rbegin(),
310 WaitStates, IsExpired, Visited);
311
312 if (W == std::numeric_limits<int>::max())
313 continue;
314
315 MinWaitStates = Found ? std::min(MinWaitStates, W) : W;
316 if (IsExpired(nullptr, MinWaitStates))
317 return MinWaitStates;
318
319 Found = true;
320 }
321
322 if (Found)
323 return MinWaitStates;
324
325 return std::numeric_limits<int>::max();
326}
327
328static int getWaitStatesSince(GCNHazardRecognizer::IsHazardFn IsHazard,
329 MachineInstr *MI,
330 IsExpiredFn IsExpired) {
331 DenseSet<const MachineBasicBlock *> Visited;
332 return getWaitStatesSince(IsHazard, MI->getParent(),
333 std::next(MI->getReverseIterator()),
334 0, IsExpired, Visited);
335}
336
337int GCNHazardRecognizer::getWaitStatesSince(IsHazardFn IsHazard, int Limit) {
338 if (IsHazardRecognizerMode) {
339 auto IsExpiredFn = [Limit] (MachineInstr *, int WaitStates) {
340 return WaitStates >= Limit;
341 };
342 return ::getWaitStatesSince(IsHazard, CurrCycleInstr, IsExpiredFn);
343 }
344
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000345 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000346 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000347 if (MI) {
348 if (IsHazard(MI))
349 return WaitStates;
350
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000351 if (MI->isInlineAsm())
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000352 continue;
353 }
Tom Stellard961811c2016-10-15 00:58:14 +0000354 ++WaitStates;
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000355
356 if (WaitStates >= Limit)
357 break;
Tom Stellard961811c2016-10-15 00:58:14 +0000358 }
359 return std::numeric_limits<int>::max();
360}
361
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000362int GCNHazardRecognizer::getWaitStatesSinceDef(unsigned Reg,
363 IsHazardFn IsHazardDef,
364 int Limit) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000365 const SIRegisterInfo *TRI = ST.getRegisterInfo();
366
367 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
368 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
369 };
370
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000371 return getWaitStatesSince(IsHazardFn, Limit);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000372}
373
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000374int GCNHazardRecognizer::getWaitStatesSinceSetReg(IsHazardFn IsHazard,
375 int Limit) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000376 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
377 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
378 };
379
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000380 return getWaitStatesSince(IsHazardFn, Limit);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000381}
382
Tom Stellardcb6ba622016-04-30 00:23:06 +0000383//===----------------------------------------------------------------------===//
384// No-op Hazard Detection
385//===----------------------------------------------------------------------===//
386
Matt Arsenault03c67d12017-11-17 04:18:24 +0000387static void addRegUnits(const SIRegisterInfo &TRI,
388 BitVector &BV, unsigned Reg) {
389 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
390 BV.set(*RUI);
391}
392
393static void addRegsToSet(const SIRegisterInfo &TRI,
394 iterator_range<MachineInstr::const_mop_iterator> Ops,
395 BitVector &Set) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000396 for (const MachineOperand &Op : Ops) {
397 if (Op.isReg())
Matt Arsenault03c67d12017-11-17 04:18:24 +0000398 addRegUnits(TRI, Set, Op.getReg());
Tom Stellard1f520e52016-05-02 17:39:06 +0000399 }
400}
401
Matt Arsenault03c67d12017-11-17 04:18:24 +0000402void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) {
403 // XXX: Do we need to worry about implicit operands
404 addRegsToSet(TRI, MI.defs(), ClauseDefs);
405 addRegsToSet(TRI, MI.uses(), ClauseUses);
406}
407
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000408int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
Matt Arsenault03c67d12017-11-17 04:18:24 +0000409 // SMEM soft clause are only present on VI+, and only matter if xnack is
410 // enabled.
411 if (!ST.isXNACKEnabled())
Tom Stellard1f520e52016-05-02 17:39:06 +0000412 return 0;
413
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000414 bool IsSMRD = TII.isSMRD(*MEM);
415
Matt Arsenault03c67d12017-11-17 04:18:24 +0000416 resetClause();
417
Tom Stellard1f520e52016-05-02 17:39:06 +0000418 // A soft-clause is any group of consecutive SMEM instructions. The
419 // instructions in this group may return out of order and/or may be
420 // replayed (i.e. the same instruction issued more than once).
421 //
422 // In order to handle these situations correctly we need to make sure
423 // that when a clause has more than one instruction, no instruction in the
424 // clause writes to a register that is read another instruction in the clause
425 // (including itself). If we encounter this situaion, we need to break the
426 // clause by inserting a non SMEM instruction.
427
Tom Stellard1f520e52016-05-02 17:39:06 +0000428 for (MachineInstr *MI : EmittedInstrs) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000429 // When we hit a non-SMEM instruction then we have passed the start of the
430 // clause and we can stop.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000431 if (!MI)
432 break;
433
434 if (IsSMRD != SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000435 break;
436
Matt Arsenault03c67d12017-11-17 04:18:24 +0000437 addClauseInst(*MI);
Tom Stellard1f520e52016-05-02 17:39:06 +0000438 }
439
Matt Arsenault03c67d12017-11-17 04:18:24 +0000440 if (ClauseDefs.none())
Tom Stellard1f520e52016-05-02 17:39:06 +0000441 return 0;
442
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000443 // We need to make sure not to put loads and stores in the same clause if they
444 // use the same address. For now, just start a new clause whenever we see a
445 // store.
446 if (MEM->mayStore())
Tom Stellard1f520e52016-05-02 17:39:06 +0000447 return 1;
448
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000449 addClauseInst(*MEM);
Tom Stellard1f520e52016-05-02 17:39:06 +0000450
451 // If the set of defs and uses intersect then we cannot add this instruction
452 // to the clause, so we have a hazard.
Matt Arsenault03c67d12017-11-17 04:18:24 +0000453 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
Tom Stellard1f520e52016-05-02 17:39:06 +0000454}
455
Tom Stellardcb6ba622016-04-30 00:23:06 +0000456int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000457 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Tom Stellard1f520e52016-05-02 17:39:06 +0000458 int WaitStatesNeeded = 0;
459
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000460 WaitStatesNeeded = checkSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000461
462 // This SMRD hazard only affects SI.
Tom Stellard5bfbae52018-07-11 20:59:01 +0000463 if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000464 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000465
466 // A read of an SGPR by SMRD instruction requires 4 wait states when the
467 // SGPR was written by a VALU instruction.
468 int SmrdSgprWaitStates = 4;
Matt Arsenault59ece952017-03-17 21:36:28 +0000469 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000470 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
471
Matt Arsenault4512d0a2017-11-17 04:18:26 +0000472 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000473
474 for (const MachineOperand &Use : SMRD->uses()) {
475 if (!Use.isReg())
476 continue;
477 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000478 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn,
479 SmrdSgprWaitStates);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000480 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
Marek Olsak22322432017-10-26 14:43:02 +0000481
482 // This fixes what appears to be undocumented hardware behavior in SI where
483 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
484 // needs some number of nops in between. We don't know how many we need, but
485 // let's use 4. This wasn't discovered before probably because the only
486 // case when this happens is when we expand a 64-bit pointer into a full
487 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
488 // probably never encountered in the closed-source land.
489 if (IsBufferSMRD) {
490 int WaitStatesNeededForUse =
491 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000492 IsBufferHazardDefFn,
493 SmrdSgprWaitStates);
Marek Olsak22322432017-10-26 14:43:02 +0000494 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
495 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000496 }
Marek Olsak22322432017-10-26 14:43:02 +0000497
Tom Stellardcb6ba622016-04-30 00:23:06 +0000498 return WaitStatesNeeded;
499}
500
501int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000502 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000503 return 0;
504
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000505 int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000506
507 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
508 // SGPR was written by a VALU Instruction.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000509 const int VmemSgprWaitStates = 5;
510 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Tom Stellardcb6ba622016-04-30 00:23:06 +0000511
512 for (const MachineOperand &Use : VMEM->uses()) {
513 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
514 continue;
515
516 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000517 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn,
518 VmemSgprWaitStates);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000519 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
520 }
521 return WaitStatesNeeded;
522}
Tom Stellarda27007e2016-05-02 16:23:09 +0000523
524int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000525 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000526 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000527
Connor Abbott00755362017-08-04 01:09:43 +0000528 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000529 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000530 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000531 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000532 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000533
534 for (const MachineOperand &Use : DPP->uses()) {
535 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
536 continue;
537 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000538 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
539 [](MachineInstr *) { return true; },
540 DppVgprWaitStates);
Tom Stellarda27007e2016-05-02 16:23:09 +0000541 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
542 }
543
Connor Abbott00755362017-08-04 01:09:43 +0000544 WaitStatesNeeded = std::max(
545 WaitStatesNeeded,
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000546 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn,
547 DppExecWaitStates));
Connor Abbott00755362017-08-04 01:09:43 +0000548
Tom Stellarda27007e2016-05-02 16:23:09 +0000549 return WaitStatesNeeded;
550}
Tom Stellard5ab61542016-10-07 23:42:48 +0000551
552int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
553 const SIInstrInfo *TII = ST.getInstrInfo();
554
555 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
556 // instruction.
557 const int DivFMasWaitStates = 4;
558 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000559 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn,
560 DivFMasWaitStates);
Tom Stellard5ab61542016-10-07 23:42:48 +0000561
562 return DivFMasWaitStates - WaitStatesNeeded;
563}
Tom Stellard961811c2016-10-15 00:58:14 +0000564
565int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
566 const SIInstrInfo *TII = ST.getInstrInfo();
567 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
568
569 const int GetRegWaitStates = 2;
570 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
571 return GetRegHWReg == getHWReg(TII, *MI);
572 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000573 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, GetRegWaitStates);
Tom Stellard961811c2016-10-15 00:58:14 +0000574
575 return GetRegWaitStates - WaitStatesNeeded;
576}
Tom Stellard30d30822016-10-27 20:39:09 +0000577
578int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
579 const SIInstrInfo *TII = ST.getInstrInfo();
580 unsigned HWReg = getHWReg(TII, *SetRegInstr);
581
582 const int SetRegWaitStates =
583 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
584 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
585 return HWReg == getHWReg(TII, *MI);
586 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000587 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, SetRegWaitStates);
Tom Stellard30d30822016-10-27 20:39:09 +0000588 return SetRegWaitStates - WaitStatesNeeded;
589}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000590
591int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
592 if (!MI.mayStore())
593 return -1;
594
595 const SIInstrInfo *TII = ST.getInstrInfo();
596 unsigned Opcode = MI.getOpcode();
597 const MCInstrDesc &Desc = MI.getDesc();
598
599 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
600 int VDataRCID = -1;
601 if (VDataIdx != -1)
602 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
603
604 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000605 // There is no hazard if the instruction does not use vector regs
606 // (like wbinvl1)
607 if (VDataIdx == -1)
608 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000609 // For MUBUF/MTBUF instructions this hazard only exists if the
610 // instruction is not using a register in the soffset field.
611 const MachineOperand *SOffset =
612 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
613 // If we have no soffset operand, then assume this field has been
614 // hardcoded to zero.
615 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
616 (!SOffset || !SOffset->isReg()))
617 return VDataIdx;
618 }
619
620 // MIMG instructions create a hazard if they don't use a 256-bit T# and
621 // the store size is greater than 8 bytes and they have more than two bits
622 // of their dmask set.
623 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
624 if (TII->isMIMG(MI)) {
625 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
626 assert(SRsrcIdx != -1 &&
627 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000628 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000629 }
630
631 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000632 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000633 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
634 return DataIdx;
635 }
636
637 return -1;
638}
639
Mark Searlesd29f24a2017-12-07 20:34:25 +0000640int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def,
641 const MachineRegisterInfo &MRI) {
642 // Helper to check for the hazard where VMEM instructions that store more than
643 // 8 bytes can have there store data over written by the next instruction.
644 const SIRegisterInfo *TRI = ST.getRegisterInfo();
645
646 const int VALUWaitStates = 1;
647 int WaitStatesNeeded = 0;
648
649 if (!TRI->isVGPR(MRI, Def.getReg()))
650 return WaitStatesNeeded;
651 unsigned Reg = Def.getReg();
652 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
653 int DataIdx = createsVALUHazard(*MI);
654 return DataIdx >= 0 &&
655 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
656 };
657 int WaitStatesNeededForDef =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000658 VALUWaitStates - getWaitStatesSince(IsHazardFn, VALUWaitStates);
Mark Searlesd29f24a2017-12-07 20:34:25 +0000659 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
660
661 return WaitStatesNeeded;
662}
663
Tom Stellardb133fbb2016-10-27 23:05:31 +0000664int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
665 // This checks for the hazard where VMEM instructions that store more than
666 // 8 bytes can have there store data over written by the next instruction.
667 if (!ST.has12DWordStoreHazard())
668 return 0;
669
Mark Searlesd29f24a2017-12-07 20:34:25 +0000670 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellardb133fbb2016-10-27 23:05:31 +0000671 int WaitStatesNeeded = 0;
672
673 for (const MachineOperand &Def : VALU->defs()) {
Mark Searlesd29f24a2017-12-07 20:34:25 +0000674 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000675 }
Mark Searlesd29f24a2017-12-07 20:34:25 +0000676
677 return WaitStatesNeeded;
678}
679
680int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) {
681 // This checks for hazards associated with inline asm statements.
682 // Since inline asms can contain just about anything, we use this
683 // to call/leverage other check*Hazard routines. Note that
684 // this function doesn't attempt to address all possible inline asm
685 // hazards (good luck), but is a collection of what has been
686 // problematic thus far.
687
688 // see checkVALUHazards()
689 if (!ST.has12DWordStoreHazard())
690 return 0;
691
692 const MachineRegisterInfo &MRI = MF.getRegInfo();
693 int WaitStatesNeeded = 0;
694
695 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands();
696 I != E; ++I) {
697 const MachineOperand &Op = IA->getOperand(I);
698 if (Op.isReg() && Op.isDef()) {
699 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI));
700 }
701 }
702
Tom Stellardb133fbb2016-10-27 23:05:31 +0000703 return WaitStatesNeeded;
704}
Tom Stellard04051b52016-10-27 23:42:29 +0000705
706int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
707 const SIInstrInfo *TII = ST.getInstrInfo();
708 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Mark Searlesd29f24a2017-12-07 20:34:25 +0000709 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard04051b52016-10-27 23:42:29 +0000710
711 const MachineOperand *LaneSelectOp =
712 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
713
714 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
715 return 0;
716
717 unsigned LaneSelectReg = LaneSelectOp->getReg();
718 auto IsHazardFn = [TII] (MachineInstr *MI) {
719 return TII->isVALU(*MI);
720 };
721
722 const int RWLaneWaitStates = 4;
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000723 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn,
724 RWLaneWaitStates);
Tom Stellard04051b52016-10-27 23:42:29 +0000725 return RWLaneWaitStates - WaitStatesSince;
726}
Tom Stellardaea899e2016-10-27 23:50:21 +0000727
728int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000729 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
730 return 0;
731
732 const SIInstrInfo *TII = ST.getInstrInfo();
733
734 const int RFEWaitStates = 1;
735
736 auto IsHazardFn = [TII] (MachineInstr *MI) {
737 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
738 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000739 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn, RFEWaitStates);
Tom Stellardaea899e2016-10-27 23:50:21 +0000740 return RFEWaitStates - WaitStatesNeeded;
741}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000742
743int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000744 if (MI->isDebugInstr())
Matt Arsenaulte823d922017-02-18 18:29:53 +0000745 return 0;
746
747 const SIRegisterInfo *TRI = ST.getRegisterInfo();
748 if (!ST.hasSMovFedHazard())
749 return 0;
750
751 // Check for any instruction reading an SGPR after a write from
752 // s_mov_fed_b32.
753 int MovFedWaitStates = 1;
754 int WaitStatesNeeded = 0;
755
756 for (const MachineOperand &Use : MI->uses()) {
757 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
758 continue;
759 auto IsHazardFn = [] (MachineInstr *MI) {
760 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
761 };
762 int WaitStatesNeededForUse =
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000763 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn,
764 MovFedWaitStates);
Matt Arsenaulte823d922017-02-18 18:29:53 +0000765 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
766 }
767
768 return WaitStatesNeeded;
769}
770
771int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000772 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000773 const int SMovRelWaitStates = 1;
Matt Arsenaulte823d922017-02-18 18:29:53 +0000774 auto IsHazardFn = [TII] (MachineInstr *MI) {
775 return TII->isSALU(*MI);
776 };
Stanislav Mekhanoshinf92ed692019-01-21 19:11:26 +0000777 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn,
778 SMovRelWaitStates);
Matt Arsenaulte823d922017-02-18 18:29:53 +0000779}