blob: dd515b0bf2f105dca8710b4ba5de49cd7920b51d [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"
19#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) :
40 CurrCycleInstr(nullptr),
Matt Arsenault43e92fe2016-06-24 06:30:11 +000041 MF(MF),
Matt Arsenault59ece952017-03-17 21:36:28 +000042 ST(MF.getSubtarget<SISubtarget>()),
Matt Arsenault03c67d12017-11-17 04:18:24 +000043 TII(*ST.getInstrInfo()),
44 TRI(TII.getRegisterInfo()),
45 ClauseUses(TRI.getNumRegUnits()),
46 ClauseDefs(TRI.getNumRegUnits()) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000047 MaxLookAhead = 5;
48}
49
50void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
51 EmitInstruction(SU->getInstr());
52}
53
54void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
55 CurrCycleInstr = MI;
56}
57
Tom Stellard5ab61542016-10-07 23:42:48 +000058static bool isDivFMas(unsigned Opcode) {
59 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
60}
61
Tom Stellard961811c2016-10-15 00:58:14 +000062static bool isSGetReg(unsigned Opcode) {
63 return Opcode == AMDGPU::S_GETREG_B32;
64}
65
66static bool isSSetReg(unsigned Opcode) {
67 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
68}
69
Tom Stellard04051b52016-10-27 23:42:29 +000070static bool isRWLane(unsigned Opcode) {
71 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
72}
73
Tom Stellardaea899e2016-10-27 23:50:21 +000074static bool isRFE(unsigned Opcode) {
75 return Opcode == AMDGPU::S_RFE_B64;
76}
77
Matt Arsenaulte823d922017-02-18 18:29:53 +000078static bool isSMovRel(unsigned Opcode) {
Matt Arsenault59ece952017-03-17 21:36:28 +000079 switch (Opcode) {
80 case AMDGPU::S_MOVRELS_B32:
81 case AMDGPU::S_MOVRELS_B64:
82 case AMDGPU::S_MOVRELD_B32:
83 case AMDGPU::S_MOVRELD_B64:
84 return true;
85 default:
86 return false;
87 }
Matt Arsenaulte823d922017-02-18 18:29:53 +000088}
89
Matt Arsenaulta41351e2017-11-17 21:35:32 +000090static bool isSendMsgTraceDataOrGDS(const MachineInstr &MI) {
91 switch (MI.getOpcode()) {
92 case AMDGPU::S_SENDMSG:
93 case AMDGPU::S_SENDMSGHALT:
94 case AMDGPU::S_TTRACEDATA:
95 return true;
96 default:
97 // TODO: GDS
98 return false;
99 }
100}
101
Tom Stellardaea899e2016-10-27 23:50:21 +0000102static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +0000103 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
104 AMDGPU::OpName::simm16);
105 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
106}
107
Tom Stellardcb6ba622016-04-30 00:23:06 +0000108ScheduleHazardRecognizer::HazardType
109GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000110 MachineInstr *MI = SU->getInstr();
111
Aaron Ballman5c190d02016-05-02 14:48:03 +0000112 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000113 return NoopHazard;
114
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000115 // FIXME: Should flat be considered vmem?
116 if ((SIInstrInfo::isVMEM(*MI) ||
117 SIInstrInfo::isFLAT(*MI))
118 && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000119 return NoopHazard;
120
Tom Stellardb133fbb2016-10-27 23:05:31 +0000121 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
122 return NoopHazard;
123
Tom Stellarda27007e2016-05-02 16:23:09 +0000124 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
125 return NoopHazard;
126
Tom Stellard5ab61542016-10-07 23:42:48 +0000127 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
128 return NoopHazard;
129
Tom Stellard04051b52016-10-27 23:42:29 +0000130 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
131 return NoopHazard;
132
Tom Stellard961811c2016-10-15 00:58:14 +0000133 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
134 return NoopHazard;
135
Tom Stellard30d30822016-10-27 20:39:09 +0000136 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
137 return NoopHazard;
138
Tom Stellardaea899e2016-10-27 23:50:21 +0000139 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
140 return NoopHazard;
141
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000142 if (ST.hasReadM0MovRelInterpHazard() &&
143 (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
144 checkReadM0Hazards(MI) > 0)
145 return NoopHazard;
146
147 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(*MI) &&
Matt Arsenaulte823d922017-02-18 18:29:53 +0000148 checkReadM0Hazards(MI) > 0)
149 return NoopHazard;
150
Mark Searlesd29f24a2017-12-07 20:34:25 +0000151 if (MI->isInlineAsm() && checkInlineAsmHazards(MI) > 0)
152 return NoopHazard;
153
Matt Arsenaulte823d922017-02-18 18:29:53 +0000154 if (checkAnyInstHazards(MI) > 0)
155 return NoopHazard;
156
Tom Stellardcb6ba622016-04-30 00:23:06 +0000157 return NoHazard;
158}
159
160unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
161 return PreEmitNoops(SU->getInstr());
162}
163
164unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000165 int WaitStates = std::max(0, checkAnyInstHazards(MI));
166
Aaron Ballman5c190d02016-05-02 14:48:03 +0000167 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000168 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000169
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000170 if (SIInstrInfo::isVALU(*MI))
171 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000172
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000173 if (SIInstrInfo::isVMEM(*MI) || SIInstrInfo::isFLAT(*MI))
174 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000175
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000176 if (SIInstrInfo::isDPP(*MI))
177 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000178
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000179 if (isDivFMas(MI->getOpcode()))
180 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000181
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000182 if (isRWLane(MI->getOpcode()))
183 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
Tom Stellard5ab61542016-10-07 23:42:48 +0000184
Mark Searlesd29f24a2017-12-07 20:34:25 +0000185 if (MI->isInlineAsm())
186 return std::max(WaitStates, checkInlineAsmHazards(MI));
187
Tom Stellard961811c2016-10-15 00:58:14 +0000188 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000189 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000190
Tom Stellard30d30822016-10-27 20:39:09 +0000191 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000192 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000193
Tom Stellardaea899e2016-10-27 23:50:21 +0000194 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000195 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000196
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000197 if (ST.hasReadM0MovRelInterpHazard() && (TII.isVINTRP(*MI) ||
198 isSMovRel(MI->getOpcode())))
199 return std::max(WaitStates, checkReadM0Hazards(MI));
200
201 if (ST.hasReadM0SendMsgHazard() && isSendMsgTraceDataOrGDS(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000202 return std::max(WaitStates, checkReadM0Hazards(MI));
203
204 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000205}
206
207void GCNHazardRecognizer::EmitNoop() {
208 EmittedInstrs.push_front(nullptr);
209}
210
211void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000212 // When the scheduler detects a stall, it will call AdvanceCycle() without
213 // emitting any instructions.
214 if (!CurrCycleInstr)
215 return;
216
Matt Arsenault59ece952017-03-17 21:36:28 +0000217 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000218
219 // Keep track of emitted instructions
220 EmittedInstrs.push_front(CurrCycleInstr);
221
222 // Add a nullptr for each additional wait state after the first. Make sure
223 // not to add more than getMaxLookAhead() items to the list, since we
224 // truncate the list to that size right after this loop.
225 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
226 i < e; ++i) {
227 EmittedInstrs.push_front(nullptr);
228 }
229
230 // getMaxLookahead() is the largest number of wait states we will ever need
231 // to insert, so there is no point in keeping track of more than that many
232 // wait states.
233 EmittedInstrs.resize(getMaxLookAhead());
234
235 CurrCycleInstr = nullptr;
236}
237
238void GCNHazardRecognizer::RecedeCycle() {
239 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
240}
241
242//===----------------------------------------------------------------------===//
243// Helper Functions
244//===----------------------------------------------------------------------===//
245
Tom Stellardb133fbb2016-10-27 23:05:31 +0000246int GCNHazardRecognizer::getWaitStatesSince(
Tom Stellard961811c2016-10-15 00:58:14 +0000247 function_ref<bool(MachineInstr *)> IsHazard) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000248 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000249 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000250 if (MI) {
251 if (IsHazard(MI))
252 return WaitStates;
253
254 unsigned Opcode = MI->getOpcode();
Nicolai Haehnle52382712017-09-06 13:50:13 +0000255 if (Opcode == AMDGPU::DBG_VALUE || Opcode == AMDGPU::IMPLICIT_DEF ||
256 Opcode == AMDGPU::INLINEASM)
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000257 continue;
258 }
Tom Stellard961811c2016-10-15 00:58:14 +0000259 ++WaitStates;
Tom Stellard961811c2016-10-15 00:58:14 +0000260 }
261 return std::numeric_limits<int>::max();
262}
263
Tom Stellardb133fbb2016-10-27 23:05:31 +0000264int GCNHazardRecognizer::getWaitStatesSinceDef(
265 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
266 const SIRegisterInfo *TRI = ST.getRegisterInfo();
267
268 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
269 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
270 };
271
272 return getWaitStatesSince(IsHazardFn);
273}
274
275int GCNHazardRecognizer::getWaitStatesSinceSetReg(
276 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000277 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
278 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
279 };
280
281 return getWaitStatesSince(IsHazardFn);
282}
283
Tom Stellardcb6ba622016-04-30 00:23:06 +0000284//===----------------------------------------------------------------------===//
285// No-op Hazard Detection
286//===----------------------------------------------------------------------===//
287
Matt Arsenault03c67d12017-11-17 04:18:24 +0000288static void addRegUnits(const SIRegisterInfo &TRI,
289 BitVector &BV, unsigned Reg) {
290 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
291 BV.set(*RUI);
292}
293
294static void addRegsToSet(const SIRegisterInfo &TRI,
295 iterator_range<MachineInstr::const_mop_iterator> Ops,
296 BitVector &Set) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000297 for (const MachineOperand &Op : Ops) {
298 if (Op.isReg())
Matt Arsenault03c67d12017-11-17 04:18:24 +0000299 addRegUnits(TRI, Set, Op.getReg());
Tom Stellard1f520e52016-05-02 17:39:06 +0000300 }
301}
302
Matt Arsenault03c67d12017-11-17 04:18:24 +0000303void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) {
304 // XXX: Do we need to worry about implicit operands
305 addRegsToSet(TRI, MI.defs(), ClauseDefs);
306 addRegsToSet(TRI, MI.uses(), ClauseUses);
307}
308
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000309int GCNHazardRecognizer::checkSoftClauseHazards(MachineInstr *MEM) {
Matt Arsenault03c67d12017-11-17 04:18:24 +0000310 // SMEM soft clause are only present on VI+, and only matter if xnack is
311 // enabled.
312 if (!ST.isXNACKEnabled())
Tom Stellard1f520e52016-05-02 17:39:06 +0000313 return 0;
314
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000315 bool IsSMRD = TII.isSMRD(*MEM);
316
Matt Arsenault03c67d12017-11-17 04:18:24 +0000317 resetClause();
318
Tom Stellard1f520e52016-05-02 17:39:06 +0000319 // A soft-clause is any group of consecutive SMEM instructions. The
320 // instructions in this group may return out of order and/or may be
321 // replayed (i.e. the same instruction issued more than once).
322 //
323 // In order to handle these situations correctly we need to make sure
324 // that when a clause has more than one instruction, no instruction in the
325 // clause writes to a register that is read another instruction in the clause
326 // (including itself). If we encounter this situaion, we need to break the
327 // clause by inserting a non SMEM instruction.
328
Tom Stellard1f520e52016-05-02 17:39:06 +0000329 for (MachineInstr *MI : EmittedInstrs) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000330 // When we hit a non-SMEM instruction then we have passed the start of the
331 // clause and we can stop.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000332 if (!MI)
333 break;
334
335 if (IsSMRD != SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000336 break;
337
Matt Arsenault03c67d12017-11-17 04:18:24 +0000338 addClauseInst(*MI);
Tom Stellard1f520e52016-05-02 17:39:06 +0000339 }
340
Matt Arsenault03c67d12017-11-17 04:18:24 +0000341 if (ClauseDefs.none())
Tom Stellard1f520e52016-05-02 17:39:06 +0000342 return 0;
343
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000344 // We need to make sure not to put loads and stores in the same clause if they
345 // use the same address. For now, just start a new clause whenever we see a
346 // store.
347 if (MEM->mayStore())
Tom Stellard1f520e52016-05-02 17:39:06 +0000348 return 1;
349
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000350 addClauseInst(*MEM);
Tom Stellard1f520e52016-05-02 17:39:06 +0000351
352 // If the set of defs and uses intersect then we cannot add this instruction
353 // to the clause, so we have a hazard.
Matt Arsenault03c67d12017-11-17 04:18:24 +0000354 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
Tom Stellard1f520e52016-05-02 17:39:06 +0000355}
356
Tom Stellardcb6ba622016-04-30 00:23:06 +0000357int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000358 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Tom Stellard1f520e52016-05-02 17:39:06 +0000359 int WaitStatesNeeded = 0;
360
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000361 WaitStatesNeeded = checkSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000362
363 // This SMRD hazard only affects SI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000364 if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000365 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000366
367 // A read of an SGPR by SMRD instruction requires 4 wait states when the
368 // SGPR was written by a VALU instruction.
369 int SmrdSgprWaitStates = 4;
Matt Arsenault59ece952017-03-17 21:36:28 +0000370 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000371 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
372
Matt Arsenault4512d0a2017-11-17 04:18:26 +0000373 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000374
375 for (const MachineOperand &Use : SMRD->uses()) {
376 if (!Use.isReg())
377 continue;
378 int WaitStatesNeededForUse =
379 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
380 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
Marek Olsak22322432017-10-26 14:43:02 +0000381
382 // This fixes what appears to be undocumented hardware behavior in SI where
383 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
384 // needs some number of nops in between. We don't know how many we need, but
385 // let's use 4. This wasn't discovered before probably because the only
386 // case when this happens is when we expand a 64-bit pointer into a full
387 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
388 // probably never encountered in the closed-source land.
389 if (IsBufferSMRD) {
390 int WaitStatesNeededForUse =
391 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
392 IsBufferHazardDefFn);
393 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
394 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000395 }
Marek Olsak22322432017-10-26 14:43:02 +0000396
Tom Stellardcb6ba622016-04-30 00:23:06 +0000397 return WaitStatesNeeded;
398}
399
400int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000401 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000402 return 0;
403
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000404 int WaitStatesNeeded = checkSoftClauseHazards(VMEM);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000405
406 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
407 // SGPR was written by a VALU Instruction.
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000408 const int VmemSgprWaitStates = 5;
409 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Tom Stellardcb6ba622016-04-30 00:23:06 +0000410
411 for (const MachineOperand &Use : VMEM->uses()) {
412 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
413 continue;
414
415 int WaitStatesNeededForUse =
416 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
417 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
418 }
419 return WaitStatesNeeded;
420}
Tom Stellarda27007e2016-05-02 16:23:09 +0000421
422int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000423 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000424 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000425
Connor Abbott00755362017-08-04 01:09:43 +0000426 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000427 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000428 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000429 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000430 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000431
432 for (const MachineOperand &Use : DPP->uses()) {
433 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
434 continue;
435 int WaitStatesNeededForUse =
436 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
437 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
438 }
439
Connor Abbott00755362017-08-04 01:09:43 +0000440 WaitStatesNeeded = std::max(
441 WaitStatesNeeded,
442 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
443
Tom Stellarda27007e2016-05-02 16:23:09 +0000444 return WaitStatesNeeded;
445}
Tom Stellard5ab61542016-10-07 23:42:48 +0000446
447int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
448 const SIInstrInfo *TII = ST.getInstrInfo();
449
450 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
451 // instruction.
452 const int DivFMasWaitStates = 4;
453 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
454 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
455
456 return DivFMasWaitStates - WaitStatesNeeded;
457}
Tom Stellard961811c2016-10-15 00:58:14 +0000458
459int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
460 const SIInstrInfo *TII = ST.getInstrInfo();
461 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
462
463 const int GetRegWaitStates = 2;
464 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
465 return GetRegHWReg == getHWReg(TII, *MI);
466 };
467 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
468
469 return GetRegWaitStates - WaitStatesNeeded;
470}
Tom Stellard30d30822016-10-27 20:39:09 +0000471
472int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
473 const SIInstrInfo *TII = ST.getInstrInfo();
474 unsigned HWReg = getHWReg(TII, *SetRegInstr);
475
476 const int SetRegWaitStates =
477 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
478 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
479 return HWReg == getHWReg(TII, *MI);
480 };
481 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
482 return SetRegWaitStates - WaitStatesNeeded;
483}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000484
485int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
486 if (!MI.mayStore())
487 return -1;
488
489 const SIInstrInfo *TII = ST.getInstrInfo();
490 unsigned Opcode = MI.getOpcode();
491 const MCInstrDesc &Desc = MI.getDesc();
492
493 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
494 int VDataRCID = -1;
495 if (VDataIdx != -1)
496 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
497
498 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000499 // There is no hazard if the instruction does not use vector regs
500 // (like wbinvl1)
501 if (VDataIdx == -1)
502 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000503 // For MUBUF/MTBUF instructions this hazard only exists if the
504 // instruction is not using a register in the soffset field.
505 const MachineOperand *SOffset =
506 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
507 // If we have no soffset operand, then assume this field has been
508 // hardcoded to zero.
509 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
510 (!SOffset || !SOffset->isReg()))
511 return VDataIdx;
512 }
513
514 // MIMG instructions create a hazard if they don't use a 256-bit T# and
515 // the store size is greater than 8 bytes and they have more than two bits
516 // of their dmask set.
517 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
518 if (TII->isMIMG(MI)) {
519 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
520 assert(SRsrcIdx != -1 &&
521 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000522 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000523 }
524
525 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000526 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000527 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
528 return DataIdx;
529 }
530
531 return -1;
532}
533
Mark Searlesd29f24a2017-12-07 20:34:25 +0000534int GCNHazardRecognizer::checkVALUHazardsHelper(const MachineOperand &Def,
535 const MachineRegisterInfo &MRI) {
536 // Helper to check for the hazard where VMEM instructions that store more than
537 // 8 bytes can have there store data over written by the next instruction.
538 const SIRegisterInfo *TRI = ST.getRegisterInfo();
539
540 const int VALUWaitStates = 1;
541 int WaitStatesNeeded = 0;
542
543 if (!TRI->isVGPR(MRI, Def.getReg()))
544 return WaitStatesNeeded;
545 unsigned Reg = Def.getReg();
546 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
547 int DataIdx = createsVALUHazard(*MI);
548 return DataIdx >= 0 &&
549 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
550 };
551 int WaitStatesNeededForDef =
552 VALUWaitStates - getWaitStatesSince(IsHazardFn);
553 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
554
555 return WaitStatesNeeded;
556}
557
Tom Stellardb133fbb2016-10-27 23:05:31 +0000558int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
559 // This checks for the hazard where VMEM instructions that store more than
560 // 8 bytes can have there store data over written by the next instruction.
561 if (!ST.has12DWordStoreHazard())
562 return 0;
563
Mark Searlesd29f24a2017-12-07 20:34:25 +0000564 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellardb133fbb2016-10-27 23:05:31 +0000565 int WaitStatesNeeded = 0;
566
567 for (const MachineOperand &Def : VALU->defs()) {
Mark Searlesd29f24a2017-12-07 20:34:25 +0000568 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Def, MRI));
Tom Stellardb133fbb2016-10-27 23:05:31 +0000569 }
Mark Searlesd29f24a2017-12-07 20:34:25 +0000570
571 return WaitStatesNeeded;
572}
573
574int GCNHazardRecognizer::checkInlineAsmHazards(MachineInstr *IA) {
575 // This checks for hazards associated with inline asm statements.
576 // Since inline asms can contain just about anything, we use this
577 // to call/leverage other check*Hazard routines. Note that
578 // this function doesn't attempt to address all possible inline asm
579 // hazards (good luck), but is a collection of what has been
580 // problematic thus far.
581
582 // see checkVALUHazards()
583 if (!ST.has12DWordStoreHazard())
584 return 0;
585
586 const MachineRegisterInfo &MRI = MF.getRegInfo();
587 int WaitStatesNeeded = 0;
588
589 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = IA->getNumOperands();
590 I != E; ++I) {
591 const MachineOperand &Op = IA->getOperand(I);
592 if (Op.isReg() && Op.isDef()) {
593 WaitStatesNeeded = std::max(WaitStatesNeeded, checkVALUHazardsHelper(Op, MRI));
594 }
595 }
596
Tom Stellardb133fbb2016-10-27 23:05:31 +0000597 return WaitStatesNeeded;
598}
Tom Stellard04051b52016-10-27 23:42:29 +0000599
600int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
601 const SIInstrInfo *TII = ST.getInstrInfo();
602 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Mark Searlesd29f24a2017-12-07 20:34:25 +0000603 const MachineRegisterInfo &MRI = MF.getRegInfo();
Tom Stellard04051b52016-10-27 23:42:29 +0000604
605 const MachineOperand *LaneSelectOp =
606 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
607
608 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
609 return 0;
610
611 unsigned LaneSelectReg = LaneSelectOp->getReg();
612 auto IsHazardFn = [TII] (MachineInstr *MI) {
613 return TII->isVALU(*MI);
614 };
615
616 const int RWLaneWaitStates = 4;
617 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
618 return RWLaneWaitStates - WaitStatesSince;
619}
Tom Stellardaea899e2016-10-27 23:50:21 +0000620
621int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000622 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
623 return 0;
624
625 const SIInstrInfo *TII = ST.getInstrInfo();
626
627 const int RFEWaitStates = 1;
628
629 auto IsHazardFn = [TII] (MachineInstr *MI) {
630 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
631 };
632 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
633 return RFEWaitStates - WaitStatesNeeded;
634}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000635
636int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
637 if (MI->isDebugValue())
638 return 0;
639
640 const SIRegisterInfo *TRI = ST.getRegisterInfo();
641 if (!ST.hasSMovFedHazard())
642 return 0;
643
644 // Check for any instruction reading an SGPR after a write from
645 // s_mov_fed_b32.
646 int MovFedWaitStates = 1;
647 int WaitStatesNeeded = 0;
648
649 for (const MachineOperand &Use : MI->uses()) {
650 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
651 continue;
652 auto IsHazardFn = [] (MachineInstr *MI) {
653 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
654 };
655 int WaitStatesNeededForUse =
656 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
657 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
658 }
659
660 return WaitStatesNeeded;
661}
662
663int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000664 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaulta41351e2017-11-17 21:35:32 +0000665 const int SMovRelWaitStates = 1;
Matt Arsenaulte823d922017-02-18 18:29:53 +0000666 auto IsHazardFn = [TII] (MachineInstr *MI) {
667 return TII->isSALU(*MI);
668 };
669 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
670}