blob: 42bd2023c8cb5c0ec0f1178a5edacfdde5b6a8db [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
Tom Stellardaea899e2016-10-27 23:50:21 +000090static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +000091 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
92 AMDGPU::OpName::simm16);
93 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
94}
95
Tom Stellardcb6ba622016-04-30 00:23:06 +000096ScheduleHazardRecognizer::HazardType
97GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000098 MachineInstr *MI = SU->getInstr();
99
Aaron Ballman5c190d02016-05-02 14:48:03 +0000100 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000101 return NoopHazard;
102
Aaron Ballman5c190d02016-05-02 14:48:03 +0000103 if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000104 return NoopHazard;
105
Tom Stellardb133fbb2016-10-27 23:05:31 +0000106 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
107 return NoopHazard;
108
Tom Stellarda27007e2016-05-02 16:23:09 +0000109 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
110 return NoopHazard;
111
Tom Stellard5ab61542016-10-07 23:42:48 +0000112 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
113 return NoopHazard;
114
Tom Stellard04051b52016-10-27 23:42:29 +0000115 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
116 return NoopHazard;
117
Tom Stellard961811c2016-10-15 00:58:14 +0000118 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
119 return NoopHazard;
120
Tom Stellard30d30822016-10-27 20:39:09 +0000121 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
122 return NoopHazard;
123
Tom Stellardaea899e2016-10-27 23:50:21 +0000124 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
125 return NoopHazard;
126
Matt Arsenault59ece952017-03-17 21:36:28 +0000127 if ((TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
Matt Arsenaulte823d922017-02-18 18:29:53 +0000128 checkReadM0Hazards(MI) > 0)
129 return NoopHazard;
130
131 if (checkAnyInstHazards(MI) > 0)
132 return NoopHazard;
133
Tom Stellardcb6ba622016-04-30 00:23:06 +0000134 return NoHazard;
135}
136
137unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
138 return PreEmitNoops(SU->getInstr());
139}
140
141unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000142 int WaitStates = std::max(0, checkAnyInstHazards(MI));
143
Aaron Ballman5c190d02016-05-02 14:48:03 +0000144 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000145 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000146
Tom Stellardb133fbb2016-10-27 23:05:31 +0000147 if (SIInstrInfo::isVALU(*MI)) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000148 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000149
Tom Stellardb133fbb2016-10-27 23:05:31 +0000150 if (SIInstrInfo::isVMEM(*MI))
151 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000152
Tom Stellardb133fbb2016-10-27 23:05:31 +0000153 if (SIInstrInfo::isDPP(*MI))
154 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
155
156 if (isDivFMas(MI->getOpcode()))
157 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
158
Tom Stellard04051b52016-10-27 23:42:29 +0000159 if (isRWLane(MI->getOpcode()))
160 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
161
Matt Arsenault59ece952017-03-17 21:36:28 +0000162 if (TII.isVINTRP(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000163 WaitStates = std::max(WaitStates, checkReadM0Hazards(MI));
164
Tom Stellardb133fbb2016-10-27 23:05:31 +0000165 return WaitStates;
166 }
Tom Stellard5ab61542016-10-07 23:42:48 +0000167
Tom Stellard961811c2016-10-15 00:58:14 +0000168 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000169 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000170
Tom Stellard30d30822016-10-27 20:39:09 +0000171 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000172 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000173
Tom Stellardaea899e2016-10-27 23:50:21 +0000174 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000175 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000176
Matt Arsenault59ece952017-03-17 21:36:28 +0000177 if (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000178 return std::max(WaitStates, checkReadM0Hazards(MI));
179
180 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000181}
182
183void GCNHazardRecognizer::EmitNoop() {
184 EmittedInstrs.push_front(nullptr);
185}
186
187void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000188 // When the scheduler detects a stall, it will call AdvanceCycle() without
189 // emitting any instructions.
190 if (!CurrCycleInstr)
191 return;
192
Matt Arsenault59ece952017-03-17 21:36:28 +0000193 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000194
195 // Keep track of emitted instructions
196 EmittedInstrs.push_front(CurrCycleInstr);
197
198 // Add a nullptr for each additional wait state after the first. Make sure
199 // not to add more than getMaxLookAhead() items to the list, since we
200 // truncate the list to that size right after this loop.
201 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
202 i < e; ++i) {
203 EmittedInstrs.push_front(nullptr);
204 }
205
206 // getMaxLookahead() is the largest number of wait states we will ever need
207 // to insert, so there is no point in keeping track of more than that many
208 // wait states.
209 EmittedInstrs.resize(getMaxLookAhead());
210
211 CurrCycleInstr = nullptr;
212}
213
214void GCNHazardRecognizer::RecedeCycle() {
215 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
216}
217
218//===----------------------------------------------------------------------===//
219// Helper Functions
220//===----------------------------------------------------------------------===//
221
Tom Stellardb133fbb2016-10-27 23:05:31 +0000222int GCNHazardRecognizer::getWaitStatesSince(
Tom Stellard961811c2016-10-15 00:58:14 +0000223 function_ref<bool(MachineInstr *)> IsHazard) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000224 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000225 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000226 if (MI) {
227 if (IsHazard(MI))
228 return WaitStates;
229
230 unsigned Opcode = MI->getOpcode();
Nicolai Haehnle52382712017-09-06 13:50:13 +0000231 if (Opcode == AMDGPU::DBG_VALUE || Opcode == AMDGPU::IMPLICIT_DEF ||
232 Opcode == AMDGPU::INLINEASM)
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000233 continue;
234 }
Tom Stellard961811c2016-10-15 00:58:14 +0000235 ++WaitStates;
Tom Stellard961811c2016-10-15 00:58:14 +0000236 }
237 return std::numeric_limits<int>::max();
238}
239
Tom Stellardb133fbb2016-10-27 23:05:31 +0000240int GCNHazardRecognizer::getWaitStatesSinceDef(
241 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
242 const SIRegisterInfo *TRI = ST.getRegisterInfo();
243
244 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
245 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
246 };
247
248 return getWaitStatesSince(IsHazardFn);
249}
250
251int GCNHazardRecognizer::getWaitStatesSinceSetReg(
252 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000253 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
254 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
255 };
256
257 return getWaitStatesSince(IsHazardFn);
258}
259
Tom Stellardcb6ba622016-04-30 00:23:06 +0000260//===----------------------------------------------------------------------===//
261// No-op Hazard Detection
262//===----------------------------------------------------------------------===//
263
Matt Arsenault03c67d12017-11-17 04:18:24 +0000264static void addRegUnits(const SIRegisterInfo &TRI,
265 BitVector &BV, unsigned Reg) {
266 for (MCRegUnitIterator RUI(Reg, &TRI); RUI.isValid(); ++RUI)
267 BV.set(*RUI);
268}
269
270static void addRegsToSet(const SIRegisterInfo &TRI,
271 iterator_range<MachineInstr::const_mop_iterator> Ops,
272 BitVector &Set) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000273 for (const MachineOperand &Op : Ops) {
274 if (Op.isReg())
Matt Arsenault03c67d12017-11-17 04:18:24 +0000275 addRegUnits(TRI, Set, Op.getReg());
Tom Stellard1f520e52016-05-02 17:39:06 +0000276 }
277}
278
Matt Arsenault03c67d12017-11-17 04:18:24 +0000279void GCNHazardRecognizer::addClauseInst(const MachineInstr &MI) {
280 // XXX: Do we need to worry about implicit operands
281 addRegsToSet(TRI, MI.defs(), ClauseDefs);
282 addRegsToSet(TRI, MI.uses(), ClauseUses);
283}
284
Tom Stellard1f520e52016-05-02 17:39:06 +0000285int GCNHazardRecognizer::checkSMEMSoftClauseHazards(MachineInstr *SMEM) {
Matt Arsenault03c67d12017-11-17 04:18:24 +0000286 // SMEM soft clause are only present on VI+, and only matter if xnack is
287 // enabled.
288 if (!ST.isXNACKEnabled())
Tom Stellard1f520e52016-05-02 17:39:06 +0000289 return 0;
290
Matt Arsenault03c67d12017-11-17 04:18:24 +0000291 resetClause();
292
Tom Stellard1f520e52016-05-02 17:39:06 +0000293 // A soft-clause is any group of consecutive SMEM instructions. The
294 // instructions in this group may return out of order and/or may be
295 // replayed (i.e. the same instruction issued more than once).
296 //
297 // In order to handle these situations correctly we need to make sure
298 // that when a clause has more than one instruction, no instruction in the
299 // clause writes to a register that is read another instruction in the clause
300 // (including itself). If we encounter this situaion, we need to break the
301 // clause by inserting a non SMEM instruction.
302
Tom Stellard1f520e52016-05-02 17:39:06 +0000303 for (MachineInstr *MI : EmittedInstrs) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000304 // When we hit a non-SMEM instruction then we have passed the start of the
305 // clause and we can stop.
Aaron Ballman3bd56b32016-05-03 15:17:25 +0000306 if (!MI || !SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000307 break;
308
Matt Arsenault03c67d12017-11-17 04:18:24 +0000309 addClauseInst(*MI);
Tom Stellard1f520e52016-05-02 17:39:06 +0000310 }
311
Matt Arsenault03c67d12017-11-17 04:18:24 +0000312 if (ClauseDefs.none())
Tom Stellard1f520e52016-05-02 17:39:06 +0000313 return 0;
314
315 // FIXME: When we support stores, we need to make sure not to put loads and
316 // stores in the same clause if they use the same address. For now, just
317 // start a new clause whenever we see a store.
318 if (SMEM->mayStore())
319 return 1;
320
Matt Arsenault03c67d12017-11-17 04:18:24 +0000321 addClauseInst(*SMEM);
Tom Stellard1f520e52016-05-02 17:39:06 +0000322
323 // If the set of defs and uses intersect then we cannot add this instruction
324 // to the clause, so we have a hazard.
Matt Arsenault03c67d12017-11-17 04:18:24 +0000325 return ClauseDefs.anyCommon(ClauseUses) ? 1 : 0;
Tom Stellard1f520e52016-05-02 17:39:06 +0000326}
327
Tom Stellardcb6ba622016-04-30 00:23:06 +0000328int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000329 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Tom Stellard1f520e52016-05-02 17:39:06 +0000330 int WaitStatesNeeded = 0;
331
332 WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000333
334 // This SMRD hazard only affects SI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000335 if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000336 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000337
338 // A read of an SGPR by SMRD instruction requires 4 wait states when the
339 // SGPR was written by a VALU instruction.
340 int SmrdSgprWaitStates = 4;
Matt Arsenault59ece952017-03-17 21:36:28 +0000341 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000342 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
343
Matt Arsenault4512d0a2017-11-17 04:18:26 +0000344 bool IsBufferSMRD = TII.isBufferSMRD(*SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000345
346 for (const MachineOperand &Use : SMRD->uses()) {
347 if (!Use.isReg())
348 continue;
349 int WaitStatesNeededForUse =
350 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
351 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
Marek Olsak22322432017-10-26 14:43:02 +0000352
353 // This fixes what appears to be undocumented hardware behavior in SI where
354 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
355 // needs some number of nops in between. We don't know how many we need, but
356 // let's use 4. This wasn't discovered before probably because the only
357 // case when this happens is when we expand a 64-bit pointer into a full
358 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
359 // probably never encountered in the closed-source land.
360 if (IsBufferSMRD) {
361 int WaitStatesNeededForUse =
362 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
363 IsBufferHazardDefFn);
364 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
365 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000366 }
Marek Olsak22322432017-10-26 14:43:02 +0000367
Tom Stellardcb6ba622016-04-30 00:23:06 +0000368 return WaitStatesNeeded;
369}
370
371int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000372 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellardcb6ba622016-04-30 00:23:06 +0000373
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000374 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000375 return 0;
376
377 const SIRegisterInfo &TRI = TII->getRegisterInfo();
378
379 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
380 // SGPR was written by a VALU Instruction.
381 int VmemSgprWaitStates = 5;
382 int WaitStatesNeeded = 0;
383 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
384
385 for (const MachineOperand &Use : VMEM->uses()) {
386 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
387 continue;
388
389 int WaitStatesNeededForUse =
390 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
391 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
392 }
393 return WaitStatesNeeded;
394}
Tom Stellarda27007e2016-05-02 16:23:09 +0000395
396int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000397 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000398 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000399
Connor Abbott00755362017-08-04 01:09:43 +0000400 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000401 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000402 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000403 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000404 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000405
406 for (const MachineOperand &Use : DPP->uses()) {
407 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
408 continue;
409 int WaitStatesNeededForUse =
410 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
411 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
412 }
413
Connor Abbott00755362017-08-04 01:09:43 +0000414 WaitStatesNeeded = std::max(
415 WaitStatesNeeded,
416 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
417
Tom Stellarda27007e2016-05-02 16:23:09 +0000418 return WaitStatesNeeded;
419}
Tom Stellard5ab61542016-10-07 23:42:48 +0000420
421int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
422 const SIInstrInfo *TII = ST.getInstrInfo();
423
424 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
425 // instruction.
426 const int DivFMasWaitStates = 4;
427 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
428 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
429
430 return DivFMasWaitStates - WaitStatesNeeded;
431}
Tom Stellard961811c2016-10-15 00:58:14 +0000432
433int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
434 const SIInstrInfo *TII = ST.getInstrInfo();
435 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
436
437 const int GetRegWaitStates = 2;
438 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
439 return GetRegHWReg == getHWReg(TII, *MI);
440 };
441 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
442
443 return GetRegWaitStates - WaitStatesNeeded;
444}
Tom Stellard30d30822016-10-27 20:39:09 +0000445
446int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
447 const SIInstrInfo *TII = ST.getInstrInfo();
448 unsigned HWReg = getHWReg(TII, *SetRegInstr);
449
450 const int SetRegWaitStates =
451 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
452 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
453 return HWReg == getHWReg(TII, *MI);
454 };
455 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
456 return SetRegWaitStates - WaitStatesNeeded;
457}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000458
459int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
460 if (!MI.mayStore())
461 return -1;
462
463 const SIInstrInfo *TII = ST.getInstrInfo();
464 unsigned Opcode = MI.getOpcode();
465 const MCInstrDesc &Desc = MI.getDesc();
466
467 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
468 int VDataRCID = -1;
469 if (VDataIdx != -1)
470 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
471
472 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000473 // There is no hazard if the instruction does not use vector regs
474 // (like wbinvl1)
475 if (VDataIdx == -1)
476 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000477 // For MUBUF/MTBUF instructions this hazard only exists if the
478 // instruction is not using a register in the soffset field.
479 const MachineOperand *SOffset =
480 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
481 // If we have no soffset operand, then assume this field has been
482 // hardcoded to zero.
483 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
484 (!SOffset || !SOffset->isReg()))
485 return VDataIdx;
486 }
487
488 // MIMG instructions create a hazard if they don't use a 256-bit T# and
489 // the store size is greater than 8 bytes and they have more than two bits
490 // of their dmask set.
491 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
492 if (TII->isMIMG(MI)) {
493 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
494 assert(SRsrcIdx != -1 &&
495 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000496 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000497 }
498
499 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000500 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000501 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
502 return DataIdx;
503 }
504
505 return -1;
506}
507
508int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
509 // This checks for the hazard where VMEM instructions that store more than
510 // 8 bytes can have there store data over written by the next instruction.
511 if (!ST.has12DWordStoreHazard())
512 return 0;
513
514 const SIRegisterInfo *TRI = ST.getRegisterInfo();
515 const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo();
516
517 const int VALUWaitStates = 1;
518 int WaitStatesNeeded = 0;
519
520 for (const MachineOperand &Def : VALU->defs()) {
521 if (!TRI->isVGPR(MRI, Def.getReg()))
522 continue;
523 unsigned Reg = Def.getReg();
524 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
525 int DataIdx = createsVALUHazard(*MI);
526 return DataIdx >= 0 &&
527 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
528 };
529 int WaitStatesNeededForDef =
530 VALUWaitStates - getWaitStatesSince(IsHazardFn);
531 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
532 }
533 return WaitStatesNeeded;
534}
Tom Stellard04051b52016-10-27 23:42:29 +0000535
536int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
537 const SIInstrInfo *TII = ST.getInstrInfo();
538 const SIRegisterInfo *TRI = ST.getRegisterInfo();
539 const MachineRegisterInfo &MRI =
540 RWLane->getParent()->getParent()->getRegInfo();
541
542 const MachineOperand *LaneSelectOp =
543 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
544
545 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
546 return 0;
547
548 unsigned LaneSelectReg = LaneSelectOp->getReg();
549 auto IsHazardFn = [TII] (MachineInstr *MI) {
550 return TII->isVALU(*MI);
551 };
552
553 const int RWLaneWaitStates = 4;
554 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
555 return RWLaneWaitStates - WaitStatesSince;
556}
Tom Stellardaea899e2016-10-27 23:50:21 +0000557
558int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000559 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
560 return 0;
561
562 const SIInstrInfo *TII = ST.getInstrInfo();
563
564 const int RFEWaitStates = 1;
565
566 auto IsHazardFn = [TII] (MachineInstr *MI) {
567 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
568 };
569 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
570 return RFEWaitStates - WaitStatesNeeded;
571}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000572
573int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
574 if (MI->isDebugValue())
575 return 0;
576
577 const SIRegisterInfo *TRI = ST.getRegisterInfo();
578 if (!ST.hasSMovFedHazard())
579 return 0;
580
581 // Check for any instruction reading an SGPR after a write from
582 // s_mov_fed_b32.
583 int MovFedWaitStates = 1;
584 int WaitStatesNeeded = 0;
585
586 for (const MachineOperand &Use : MI->uses()) {
587 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
588 continue;
589 auto IsHazardFn = [] (MachineInstr *MI) {
590 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
591 };
592 int WaitStatesNeededForUse =
593 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
594 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
595 }
596
597 return WaitStatesNeeded;
598}
599
600int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
601 if (!ST.hasReadM0Hazard())
602 return 0;
603
604 const SIInstrInfo *TII = ST.getInstrInfo();
605 int SMovRelWaitStates = 1;
606 auto IsHazardFn = [TII] (MachineInstr *MI) {
607 return TII->isSALU(*MI);
608 };
609 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
610}