blob: c6396de89c4f6e6dbbd456b390ceecc6cab589c2 [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
Matt Arsenaulta41351e2017-11-17 21:35:32 +000091static bool isSendMsgTraceDataOrGDS(const MachineInstr &MI) {
92 switch (MI.getOpcode()) {
93 case AMDGPU::S_SENDMSG:
94 case AMDGPU::S_SENDMSGHALT:
95 case AMDGPU::S_TTRACEDATA:
96 return true;
97 default:
98 // TODO: GDS
99 return false;
100 }
101}
102
Tom Stellardaea899e2016-10-27 23:50:21 +0000103static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +0000104 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
105 AMDGPU::OpName::simm16);
106 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
107}
108
Tom Stellardcb6ba622016-04-30 00:23:06 +0000109ScheduleHazardRecognizer::HazardType
110GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000111 MachineInstr *MI = SU->getInstr();
112
Aaron Ballman5c190d02016-05-02 14:48:03 +0000113 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000114 return NoopHazard;
115
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000116 // FIXME: Should flat be considered vmem?
117 if ((SIInstrInfo::isVMEM(*MI) ||
118 SIInstrInfo::isFLAT(*MI))
119 && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000120 return NoopHazard;
121
Tom Stellardb133fbb2016-10-27 23:05:31 +0000122 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
123 return NoopHazard;
124
Tom Stellarda27007e2016-05-02 16:23:09 +0000125 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
126 return NoopHazard;
127
Tom Stellard5ab61542016-10-07 23:42:48 +0000128 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
129 return NoopHazard;
130
Tom Stellard04051b52016-10-27 23:42:29 +0000131 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
132 return NoopHazard;
133
Tom Stellard961811c2016-10-15 00:58:14 +0000134 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
135 return NoopHazard;
136
Tom Stellard30d30822016-10-27 20:39:09 +0000137 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
138 return NoopHazard;
139
Tom Stellardaea899e2016-10-27 23:50:21 +0000140 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
141 return NoopHazard;
142
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000143 if (ST.hasReadM0MovRelInterpHazard() &&
144 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
145 checkReadM0Hazards(MI) > 0)
146 return NoopHazard;
147
148 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(*MI) &&
Matt Arsenaulte823d922017-02-18 18:29:53 +0000149 checkReadM0Hazards(MI) > 0)
150 return NoopHazard;
151
Mark Searlesd29f24a2017-12-07 20:34:25 +0000152 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0)
153 return NoopHazard;
154
Matt Arsenaulte823d922017-02-18 18:29:53 +0000155 if (checkAnyInstHazards(MI) > 0)
156 return NoopHazard;
157
Tom Stellardcb6ba622016-04-30 00:23:06 +0000158 return NoHazard;
159}
160
161unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
162 return PreEmitNoops(SU->getInstr());
163}
164
165unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000166 int WaitStates = std::max(0, checkAnyInstHazards(MI));
167
Aaron Ballman5c190d02016-05-02 14:48:03 +0000168 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000169 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000170
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000171 if (SIInstrInfo::isVALU(*MI))
172 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000173
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000174 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
175 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000176
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000177 if (SIInstrInfo::isDPP(*MI))
178 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000179
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000180 if (isDivFMas(MI->getOpcode()))
181 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000182
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000183 if (isRWLane(MI->getOpcode()))
184 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
Tom Stellard5ab61542016-10-07 23:42:48 +0000185
Mark Searlesd29f24a2017-12-07 20:34:25 +0000186 if (MI->isInlineAsm())
187 return std::max(WaitStates, checkInlineAsmHazards(MI));
188
Tom Stellard961811c2016-10-15 00:58:14 +0000189 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000190 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000191
Tom Stellard30d30822016-10-27 20:39:09 +0000192 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000193 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000194
Tom Stellardaea899e2016-10-27 23:50:21 +0000195 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000196 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000197
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000198 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
199 isSMovRel(MI->getOpcode())))
200 return std::max(WaitStates, checkReadM0Hazards(MI));
201
202 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000203 return std::max(WaitStates, checkReadM0Hazards(MI));
204
205 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000206}
207
208void GCNHazardRecognizer::EmitNoop() {
209 EmittedInstrs.push_front(nullptr);
210}
211
212void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000213 // When the scheduler detects a stall, it will call AdvanceCycle() without
214 // emitting any instructions.
215 if (!CurrCycleInstr)
216 return;
217
Carl Ritsonf898edd2018-09-10 10:14:48 +0000218 // Do not track non-instructions which do not affect the wait states.
219 // If included, these instructions can lead to buffer overflow such that
220 // detectable hazards are missed.
221 if (CurrCycleInstr->getOpcode() == AMDGPU::IMPLICIT_DEF)
222 return;
223 else if (CurrCycleInstr->isDebugInstr())
224 return;
225
Matt Arsenault59ece952017-03-17 21:36:28 +0000226 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000227
228 // Keep track of emitted instructions
229 EmittedInstrs.push_front(CurrCycleInstr);
230
231 // Add a nullptr for each additional wait state after the first. Make sure
232 // not to add more than getMaxLookAhead() items to the list, since we
233 // truncate the list to that size right after this loop.
234 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
235 i < e; ++i) {
236 EmittedInstrs.push_front(nullptr);
237 }
238
239 // getMaxLookahead() is the largest number of wait states we will ever need
240 // to insert, so there is no point in keeping track of more than that many
241 // wait states.
242 EmittedInstrs.resize(getMaxLookAhead());
243
244 CurrCycleInstr = nullptr;
245}
246
247void GCNHazardRecognizer::RecedeCycle() {
248 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
249}
250
251//===----------------------------------------------------------------------===//
252// Helper Functions
253//===----------------------------------------------------------------------===//
254
Tom Stellardb133fbb2016-10-27 23:05:31 +0000255int GCNHazardRecognizer::getWaitStatesSince(
Tom Stellard961811c2016-10-15 00:58:14 +0000256 function_ref<bool(MachineInstr *)> IsHazard) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000257 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000258 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000259 if (MI) {
260 if (IsHazard(MI))
261 return WaitStates;
262
263 unsigned Opcode = MI->getOpcode();
Carl Ritsonf898edd2018-09-10 10:14:48 +0000264 if (Opcode == AMDGPU::INLINEASM)
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000265 continue;
266 }
Tom Stellard961811c2016-10-15 00:58:14 +0000267 ++WaitStates;
Tom Stellard961811c2016-10-15 00:58:14 +0000268 }
269 return std::numeric_limits<int>::max();
270}
271
Tom Stellardb133fbb2016-10-27 23:05:31 +0000272int GCNHazardRecognizer::getWaitStatesSinceDef(
273 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
274 const SIRegisterInfo *TRI = ST.getRegisterInfo();
275
276 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
277 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
278 };
279
280 return getWaitStatesSince(IsHazardFn);
281}
282
283int GCNHazardRecognizer::getWaitStatesSinceSetReg(
284 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000285 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
286 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
287 };
288
289 return getWaitStatesSince(IsHazardFn);
290}
291
Tom Stellardcb6ba622016-04-30 00:23:06 +0000292//===----------------------------------------------------------------------===//
293// No-op Hazard Detection
294//===----------------------------------------------------------------------===//
295
Matt Arsenault03c67d12017-11-17 04:18:24 +0000296static void addRegUnits(const SIRegisterInfo &TRI,
297 BitVector &BV, unsigned Reg) {
298 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
299 BV.set(*RUI);
300}
301
302static void addRegsToSet(const SIRegisterInfo &TRI,
303 iterator_range<MachineInstr::const_mop_iterator> Ops,
304 BitVector &Set) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000305 for (const MachineOperand &Op : Ops) {
306 if (Op.isReg())
Matt Arsenault03c67d12017-11-17 04:18:24 +0000307 addRegUnits(TRI, Set, Op.getReg());
Tom Stellard1f520e52016-05-02 17:39:06 +0000308 }
309}
310
Matt Arsenault03c67d12017-11-17 04:18:24 +0000311void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) {
312 // XXX: Do we need to worry about implicit operands
313 addRegsToSet(TRI, MI.defs(), ClauseDefs);
314 addRegsToSet(TRI, MI.uses(), ClauseUses);
315}
316
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000317int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
Matt Arsenault03c67d12017-11-17 04:18:24 +0000318 // SMEM soft clause are only present on VI+, and only matter if xnack is
319 // enabled.
320 if (!ST.isXNACKEnabled())
Tom Stellard1f520e52016-05-02 17:39:06 +0000321 return 0;
322
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000323 bool IsSMRD = TII.isSMRD(*MEM);
324
Matt Arsenault03c67d12017-11-17 04:18:24 +0000325 resetClause();
326
Tom Stellard1f520e52016-05-02 17:39:06 +0000327 // A soft-clause is any group of consecutive SMEM instructions. The
328 // instructions in this group may return out of order and/or may be
329 // replayed (i.e. the same instruction issued more than once).
330 //
331 // In order to handle these situations correctly we need to make sure
332 // that when a clause has more than one instruction, no instruction in the
333 // clause writes to a register that is read another instruction in the clause
334 // (including itself). If we encounter this situaion, we need to break the
335 // clause by inserting a non SMEM instruction.
336
Tom Stellard1f520e52016-05-02 17:39:06 +0000337 for (MachineInstr *MI : EmittedInstrs) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000338 // When we hit a non-SMEM instruction then we have passed the start of the
339 // clause and we can stop.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000340 if (!MI)
341 break;
342
343 if (IsSMRD != SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000344 break;
345
Matt Arsenault03c67d12017-11-17 04:18:24 +0000346 addClauseInst(*MI);
Tom Stellard1f520e52016-05-02 17:39:06 +0000347 }
348
Matt Arsenault03c67d12017-11-17 04:18:24 +0000349 if (ClauseDefs.none())
Tom Stellard1f520e52016-05-02 17:39:06 +0000350 return 0;
351
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000352 // We need to make sure not to put loads and stores in the same clause if they
353 // use the same address. For now, just start a new clause whenever we see a
354 // store.
355 if (MEM->mayStore())
Tom Stellard1f520e52016-05-02 17:39:06 +0000356 return 1;
357
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000358 addClauseInst(*MEM);
Tom Stellard1f520e52016-05-02 17:39:06 +0000359
360 // If the set of defs and uses intersect then we cannot add this instruction
361 // to the clause, so we have a hazard.
Matt Arsenault03c67d12017-11-17 04:18:24 +0000362 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
Tom Stellard1f520e52016-05-02 17:39:06 +0000363}
364
Tom Stellardcb6ba622016-04-30 00:23:06 +0000365int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000366 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Tom Stellard1f520e52016-05-02 17:39:06 +0000367 int WaitStatesNeeded = 0;
368
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000369 WaitStatesNeeded = checkSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000370
371 // This SMRD hazard only affects SI.
Tom Stellard5bfbae52018-07-11 20:59:01 +0000372 if (ST.getGeneration() != AMDGPUSubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000373 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000374
375 // A read of an SGPR by SMRD instruction requires 4 wait states when the
376 // SGPR was written by a VALU instruction.
377 int SmrdSgprWaitStates = 4;
Matt Arsenault59ece952017-03-17 21:36:28 +0000378 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000379 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
380
Matt Arsenault4512d0a2017-11-17 04:18:26 +0000381 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000382
383 for (const MachineOperand &Use : SMRD->uses()) {
384 if (!Use.isReg())
385 continue;
386 int WaitStatesNeededForUse =
387 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
388 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
Marek Olsak22322432017-10-26 14:43:02 +0000389
390 // This fixes what appears to be undocumented hardware behavior in SI where
391 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
392 // needs some number of nops in between. We don't know how many we need, but
393 // let's use 4. This wasn't discovered before probably because the only
394 // case when this happens is when we expand a 64-bit pointer into a full
395 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
396 // probably never encountered in the closed-source land.
397 if (IsBufferSMRD) {
398 int WaitStatesNeededForUse =
399 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
400 IsBufferHazardDefFn);
401 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
402 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000403 }
Marek Olsak22322432017-10-26 14:43:02 +0000404
Tom Stellardcb6ba622016-04-30 00:23:06 +0000405 return WaitStatesNeeded;
406}
407
408int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000409 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000410 return 0;
411
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000412 int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000413
414 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
415 // SGPR was written by a VALU Instruction.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000416 const int VmemSgprWaitStates = 5;
417 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Tom Stellardcb6ba622016-04-30 00:23:06 +0000418
419 for (const MachineOperand &Use : VMEM->uses()) {
420 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
421 continue;
422
423 int WaitStatesNeededForUse =
424 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
425 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
426 }
427 return WaitStatesNeeded;
428}
Tom Stellarda27007e2016-05-02 16:23:09 +0000429
430int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000431 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000432 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000433
Connor Abbott00755362017-08-04 01:09:43 +0000434 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000435 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000436 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000437 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000438 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000439
440 for (const MachineOperand &Use : DPP->uses()) {
441 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
442 continue;
443 int WaitStatesNeededForUse =
444 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
445 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
446 }
447
Connor Abbott00755362017-08-04 01:09:43 +0000448 WaitStatesNeeded = std::max(
449 WaitStatesNeeded,
450 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
451
Tom Stellarda27007e2016-05-02 16:23:09 +0000452 return WaitStatesNeeded;
453}
Tom Stellard5ab61542016-10-07 23:42:48 +0000454
455int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
456 const SIInstrInfo *TII = ST.getInstrInfo();
457
458 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
459 // instruction.
460 const int DivFMasWaitStates = 4;
461 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
462 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
463
464 return DivFMasWaitStates - WaitStatesNeeded;
465}
Tom Stellard961811c2016-10-15 00:58:14 +0000466
467int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
468 const SIInstrInfo *TII = ST.getInstrInfo();
469 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
470
471 const int GetRegWaitStates = 2;
472 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
473 return GetRegHWReg == getHWReg(TII, *MI);
474 };
475 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
476
477 return GetRegWaitStates - WaitStatesNeeded;
478}
Tom Stellard30d30822016-10-27 20:39:09 +0000479
480int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
481 const SIInstrInfo *TII = ST.getInstrInfo();
482 unsigned HWReg = getHWReg(TII, *SetRegInstr);
483
484 const int SetRegWaitStates =
485 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
486 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
487 return HWReg == getHWReg(TII, *MI);
488 };
489 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
490 return SetRegWaitStates - WaitStatesNeeded;
491}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000492
493int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
494 if (!MI.mayStore())
495 return -1;
496
497 const SIInstrInfo *TII = ST.getInstrInfo();
498 unsigned Opcode = MI.getOpcode();
499 const MCInstrDesc &Desc = MI.getDesc();
500
501 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
502 int VDataRCID = -1;
503 if (VDataIdx != -1)
504 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
505
506 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000507 // There is no hazard if the instruction does not use vector regs
508 // (like wbinvl1)
509 if (VDataIdx == -1)
510 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000511 // For MUBUF/MTBUF instructions this hazard only exists if the
512 // instruction is not using a register in the soffset field.
513 const MachineOperand *SOffset =
514 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
515 // If we have no soffset operand, then assume this field has been
516 // hardcoded to zero.
517 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
518 (!SOffset || !SOffset->isReg()))
519 return VDataIdx;
520 }
521
522 // MIMG instructions create a hazard if they don't use a 256-bit T# and
523 // the store size is greater than 8 bytes and they have more than two bits
524 // of their dmask set.
525 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
526 if (TII->isMIMG(MI)) {
527 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
528 assert(SRsrcIdx != -1 &&
529 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000530 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000531 }
532
533 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000534 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000535 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
536 return DataIdx;
537 }
538
539 return -1;
540}
541
Mark Searlesd29f24a2017-12-07 20:34:25 +0000542int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def,
543 const MachineRegisterInfo &MRI) {
544 // Helper to check for the hazard where VMEM instructions that store more than
545 // 8 bytes can have there store data over written by the next instruction.
546 const SIRegisterInfo *TRI = ST.getRegisterInfo();
547
548 const int VALUWaitStates = 1;
549 int WaitStatesNeeded = 0;
550
551 if (!TRI->isVGPR(MRI, Def.getReg()))
552 return WaitStatesNeeded;
553 unsigned Reg = Def.getReg();
554 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
555 int DataIdx = createsVALUHazard(*MI);
556 return DataIdx >= 0 &&
557 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
558 };
559 int WaitStatesNeededForDef =
560 VALUWaitStates - getWaitStatesSince(IsHazardFn);
561 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
562
563 return WaitStatesNeeded;
564}
565
Tom Stellardb133fbb2016-10-27 23:05:31 +0000566int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
567 // This checks for the hazard where VMEM instructions that store more than
568 // 8 bytes can have there store data over written by the next instruction.
569 if (!ST.has12DWordStoreHazard())
570 return 0;
571
Mark Searlesd29f24a2017-12-07 20:34:25 +0000572 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellardb133fbb2016-10-27 23:05:31 +0000573 int WaitStatesNeeded = 0;
574
575 for (const MachineOperand &Def : VALU->defs()) {
Mark Searlesd29f24a2017-12-07 20:34:25 +0000576 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000577 }
Mark Searlesd29f24a2017-12-07 20:34:25 +0000578
579 return WaitStatesNeeded;
580}
581
582int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) {
583 // This checks for hazards associated with inline asm statements.
584 // Since inline asms can contain just about anything, we use this
585 // to call/leverage other check*Hazard routines. Note that
586 // this function doesn't attempt to address all possible inline asm
587 // hazards (good luck), but is a collection of what has been
588 // problematic thus far.
589
590 // see checkVALUHazards()
591 if (!ST.has12DWordStoreHazard())
592 return 0;
593
594 const MachineRegisterInfo &MRI = MF.getRegInfo();
595 int WaitStatesNeeded = 0;
596
597 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands();
598 I != E; ++I) {
599 const MachineOperand &Op = IA->getOperand(I);
600 if (Op.isReg() && Op.isDef()) {
601 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI));
602 }
603 }
604
Tom Stellardb133fbb2016-10-27 23:05:31 +0000605 return WaitStatesNeeded;
606}
Tom Stellard04051b52016-10-27 23:42:29 +0000607
608int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
609 const SIInstrInfo *TII = ST.getInstrInfo();
610 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Mark Searlesd29f24a2017-12-07 20:34:25 +0000611 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard04051b52016-10-27 23:42:29 +0000612
613 const MachineOperand *LaneSelectOp =
614 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
615
616 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
617 return 0;
618
619 unsigned LaneSelectReg = LaneSelectOp->getReg();
620 auto IsHazardFn = [TII] (MachineInstr *MI) {
621 return TII->isVALU(*MI);
622 };
623
624 const int RWLaneWaitStates = 4;
625 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
626 return RWLaneWaitStates - WaitStatesSince;
627}
Tom Stellardaea899e2016-10-27 23:50:21 +0000628
629int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000630 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
631 return 0;
632
633 const SIInstrInfo *TII = ST.getInstrInfo();
634
635 const int RFEWaitStates = 1;
636
637 auto IsHazardFn = [TII] (MachineInstr *MI) {
638 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
639 };
640 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
641 return RFEWaitStates - WaitStatesNeeded;
642}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000643
644int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000645 if (MI->isDebugInstr())
Matt Arsenaulte823d922017-02-18 18:29:53 +0000646 return 0;
647
648 const SIRegisterInfo *TRI = ST.getRegisterInfo();
649 if (!ST.hasSMovFedHazard())
650 return 0;
651
652 // Check for any instruction reading an SGPR after a write from
653 // s_mov_fed_b32.
654 int MovFedWaitStates = 1;
655 int WaitStatesNeeded = 0;
656
657 for (const MachineOperand &Use : MI->uses()) {
658 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
659 continue;
660 auto IsHazardFn = [] (MachineInstr *MI) {
661 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
662 };
663 int WaitStatesNeededForUse =
664 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
665 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
666 }
667
668 return WaitStatesNeeded;
669}
670
671int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000672 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000673 const int SMovRelWaitStates = 1;
Matt Arsenaulte823d922017-02-18 18:29:53 +0000674 auto IsHazardFn = [TII] (MachineInstr *MI) {
675 return TII->isSALU(*MI);
676 };
677 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
678}