blob: 3f54f8b81432b3c88062d9c904055ede56314283 [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>()),
43 TII(*ST.getInstrInfo()) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000044 MaxLookAhead = 5;
45}
46
47void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
48 EmitInstruction(SU->getInstr());
49}
50
51void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
52 CurrCycleInstr = MI;
53}
54
Tom Stellard5ab61542016-10-07 23:42:48 +000055static bool isDivFMas(unsigned Opcode) {
56 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
57}
58
Tom Stellard961811c2016-10-15 00:58:14 +000059static bool isSGetReg(unsigned Opcode) {
60 return Opcode == AMDGPU::S_GETREG_B32;
61}
62
63static bool isSSetReg(unsigned Opcode) {
64 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
65}
66
Tom Stellard04051b52016-10-27 23:42:29 +000067static bool isRWLane(unsigned Opcode) {
68 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
69}
70
Tom Stellardaea899e2016-10-27 23:50:21 +000071static bool isRFE(unsigned Opcode) {
72 return Opcode == AMDGPU::S_RFE_B64;
73}
74
Matt Arsenaulte823d922017-02-18 18:29:53 +000075static bool isSMovRel(unsigned Opcode) {
Matt Arsenault59ece952017-03-17 21:36:28 +000076 switch (Opcode) {
77 case AMDGPU::S_MOVRELS_B32:
78 case AMDGPU::S_MOVRELS_B64:
79 case AMDGPU::S_MOVRELD_B32:
80 case AMDGPU::S_MOVRELD_B64:
81 return true;
82 default:
83 return false;
84 }
Matt Arsenaulte823d922017-02-18 18:29:53 +000085}
86
Tom Stellardaea899e2016-10-27 23:50:21 +000087static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +000088 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
89 AMDGPU::OpName::simm16);
90 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
91}
92
Tom Stellardcb6ba622016-04-30 00:23:06 +000093ScheduleHazardRecognizer::HazardType
94GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000095 MachineInstr *MI = SU->getInstr();
96
Aaron Ballman5c190d02016-05-02 14:48:03 +000097 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +000098 return NoopHazard;
99
Aaron Ballman5c190d02016-05-02 14:48:03 +0000100 if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000101 return NoopHazard;
102
Tom Stellardb133fbb2016-10-27 23:05:31 +0000103 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
104 return NoopHazard;
105
Tom Stellarda27007e2016-05-02 16:23:09 +0000106 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
107 return NoopHazard;
108
Tom Stellard5ab61542016-10-07 23:42:48 +0000109 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
110 return NoopHazard;
111
Tom Stellard04051b52016-10-27 23:42:29 +0000112 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
113 return NoopHazard;
114
Tom Stellard961811c2016-10-15 00:58:14 +0000115 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
116 return NoopHazard;
117
Tom Stellard30d30822016-10-27 20:39:09 +0000118 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
119 return NoopHazard;
120
Tom Stellardaea899e2016-10-27 23:50:21 +0000121 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
122 return NoopHazard;
123
Matt Arsenault59ece952017-03-17 21:36:28 +0000124 if ((TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode())) &&
Matt Arsenaulte823d922017-02-18 18:29:53 +0000125 checkReadM0Hazards(MI) > 0)
126 return NoopHazard;
127
128 if (checkAnyInstHazards(MI) > 0)
129 return NoopHazard;
130
Tom Stellardcb6ba622016-04-30 00:23:06 +0000131 return NoHazard;
132}
133
134unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
135 return PreEmitNoops(SU->getInstr());
136}
137
138unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000139 int WaitStates = std::max(0, checkAnyInstHazards(MI));
140
Aaron Ballman5c190d02016-05-02 14:48:03 +0000141 if (SIInstrInfo::isSMRD(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000142 return std::max(WaitStates, checkSMRDHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000143
Tom Stellardb133fbb2016-10-27 23:05:31 +0000144 if (SIInstrInfo::isVALU(*MI)) {
Matt Arsenaulte823d922017-02-18 18:29:53 +0000145 WaitStates = std::max(WaitStates, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000146
Tom Stellardb133fbb2016-10-27 23:05:31 +0000147 if (SIInstrInfo::isVMEM(*MI))
148 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000149
Tom Stellardb133fbb2016-10-27 23:05:31 +0000150 if (SIInstrInfo::isDPP(*MI))
151 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
152
153 if (isDivFMas(MI->getOpcode()))
154 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
155
Tom Stellard04051b52016-10-27 23:42:29 +0000156 if (isRWLane(MI->getOpcode()))
157 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
158
Matt Arsenault59ece952017-03-17 21:36:28 +0000159 if (TII.isVINTRP(*MI))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000160 WaitStates = std::max(WaitStates, checkReadM0Hazards(MI));
161
Tom Stellardb133fbb2016-10-27 23:05:31 +0000162 return WaitStates;
163 }
Tom Stellard5ab61542016-10-07 23:42:48 +0000164
Tom Stellard961811c2016-10-15 00:58:14 +0000165 if (isSGetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000166 return std::max(WaitStates, checkGetRegHazards(MI));
Tom Stellard961811c2016-10-15 00:58:14 +0000167
Tom Stellard30d30822016-10-27 20:39:09 +0000168 if (isSSetReg(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000169 return std::max(WaitStates, checkSetRegHazards(MI));
Tom Stellard30d30822016-10-27 20:39:09 +0000170
Tom Stellardaea899e2016-10-27 23:50:21 +0000171 if (isRFE(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000172 return std::max(WaitStates, checkRFEHazards(MI));
Tom Stellardaea899e2016-10-27 23:50:21 +0000173
Matt Arsenault59ece952017-03-17 21:36:28 +0000174 if (TII.isVINTRP(*MI) || isSMovRel(MI->getOpcode()))
Matt Arsenaulte823d922017-02-18 18:29:53 +0000175 return std::max(WaitStates, checkReadM0Hazards(MI));
176
177 return WaitStates;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000178}
179
180void GCNHazardRecognizer::EmitNoop() {
181 EmittedInstrs.push_front(nullptr);
182}
183
184void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000185 // When the scheduler detects a stall, it will call AdvanceCycle() without
186 // emitting any instructions.
187 if (!CurrCycleInstr)
188 return;
189
Matt Arsenault59ece952017-03-17 21:36:28 +0000190 unsigned NumWaitStates = TII.getNumWaitStates(*CurrCycleInstr);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000191
192 // Keep track of emitted instructions
193 EmittedInstrs.push_front(CurrCycleInstr);
194
195 // Add a nullptr for each additional wait state after the first. Make sure
196 // not to add more than getMaxLookAhead() items to the list, since we
197 // truncate the list to that size right after this loop.
198 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
199 i < e; ++i) {
200 EmittedInstrs.push_front(nullptr);
201 }
202
203 // getMaxLookahead() is the largest number of wait states we will ever need
204 // to insert, so there is no point in keeping track of more than that many
205 // wait states.
206 EmittedInstrs.resize(getMaxLookAhead());
207
208 CurrCycleInstr = nullptr;
209}
210
211void GCNHazardRecognizer::RecedeCycle() {
212 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
213}
214
215//===----------------------------------------------------------------------===//
216// Helper Functions
217//===----------------------------------------------------------------------===//
218
Tom Stellardb133fbb2016-10-27 23:05:31 +0000219int GCNHazardRecognizer::getWaitStatesSince(
Tom Stellard961811c2016-10-15 00:58:14 +0000220 function_ref<bool(MachineInstr *)> IsHazard) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000221 int WaitStates = 0;
Tom Stellard961811c2016-10-15 00:58:14 +0000222 for (MachineInstr *MI : EmittedInstrs) {
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000223 if (MI) {
224 if (IsHazard(MI))
225 return WaitStates;
226
227 unsigned Opcode = MI->getOpcode();
Nicolai Haehnle52382712017-09-06 13:50:13 +0000228 if (Opcode == AMDGPU::DBG_VALUE || Opcode == AMDGPU::IMPLICIT_DEF ||
229 Opcode == AMDGPU::INLINEASM)
Nicolai Haehnle75c98c32017-09-01 16:56:32 +0000230 continue;
231 }
Tom Stellard961811c2016-10-15 00:58:14 +0000232 ++WaitStates;
Tom Stellard961811c2016-10-15 00:58:14 +0000233 }
234 return std::numeric_limits<int>::max();
235}
236
Tom Stellardb133fbb2016-10-27 23:05:31 +0000237int GCNHazardRecognizer::getWaitStatesSinceDef(
238 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
239 const SIRegisterInfo *TRI = ST.getRegisterInfo();
240
241 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
242 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
243 };
244
245 return getWaitStatesSince(IsHazardFn);
246}
247
248int GCNHazardRecognizer::getWaitStatesSinceSetReg(
249 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000250 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
251 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
252 };
253
254 return getWaitStatesSince(IsHazardFn);
255}
256
Tom Stellardcb6ba622016-04-30 00:23:06 +0000257//===----------------------------------------------------------------------===//
258// No-op Hazard Detection
259//===----------------------------------------------------------------------===//
260
Tom Stellard1f520e52016-05-02 17:39:06 +0000261static void addRegsToSet(iterator_range<MachineInstr::const_mop_iterator> Ops,
262 std::set<unsigned> &Set) {
263 for (const MachineOperand &Op : Ops) {
264 if (Op.isReg())
265 Set.insert(Op.getReg());
266 }
267}
268
269int GCNHazardRecognizer::checkSMEMSoftClauseHazards(MachineInstr *SMEM) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000270 // SMEM soft clause are only present on VI+
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000271 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000272 return 0;
273
274 // A soft-clause is any group of consecutive SMEM instructions. The
275 // instructions in this group may return out of order and/or may be
276 // replayed (i.e. the same instruction issued more than once).
277 //
278 // In order to handle these situations correctly we need to make sure
279 // that when a clause has more than one instruction, no instruction in the
280 // clause writes to a register that is read another instruction in the clause
281 // (including itself). If we encounter this situaion, we need to break the
282 // clause by inserting a non SMEM instruction.
283
Tom Stellard1f520e52016-05-02 17:39:06 +0000284 std::set<unsigned> ClauseDefs;
285 std::set<unsigned> ClauseUses;
286
287 for (MachineInstr *MI : EmittedInstrs) {
288
289 // When we hit a non-SMEM instruction then we have passed the start of the
290 // clause and we can stop.
Aaron Ballman3bd56b32016-05-03 15:17:25 +0000291 if (!MI || !SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000292 break;
293
294 addRegsToSet(MI->defs(), ClauseDefs);
295 addRegsToSet(MI->uses(), ClauseUses);
296 }
297
298 if (ClauseDefs.empty())
299 return 0;
300
301 // FIXME: When we support stores, we need to make sure not to put loads and
302 // stores in the same clause if they use the same address. For now, just
303 // start a new clause whenever we see a store.
304 if (SMEM->mayStore())
305 return 1;
306
307 addRegsToSet(SMEM->defs(), ClauseDefs);
308 addRegsToSet(SMEM->uses(), ClauseUses);
309
310 std::vector<unsigned> Result(std::max(ClauseDefs.size(), ClauseUses.size()));
311 std::vector<unsigned>::iterator End;
312
313 End = std::set_intersection(ClauseDefs.begin(), ClauseDefs.end(),
314 ClauseUses.begin(), ClauseUses.end(), Result.begin());
315
316 // If the set of defs and uses intersect then we cannot add this instruction
317 // to the clause, so we have a hazard.
318 if (End != Result.begin())
319 return 1;
320
321 return 0;
322}
323
Tom Stellardcb6ba622016-04-30 00:23:06 +0000324int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000325 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Tom Stellard1f520e52016-05-02 17:39:06 +0000326 int WaitStatesNeeded = 0;
327
328 WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000329
330 // This SMRD hazard only affects SI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000331 if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000332 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000333
334 // A read of an SGPR by SMRD instruction requires 4 wait states when the
335 // SGPR was written by a VALU instruction.
336 int SmrdSgprWaitStates = 4;
Matt Arsenault59ece952017-03-17 21:36:28 +0000337 auto IsHazardDefFn = [this] (MachineInstr *MI) { return TII.isVALU(*MI); };
Marek Olsak22322432017-10-26 14:43:02 +0000338 auto IsBufferHazardDefFn = [this] (MachineInstr *MI) { return TII.isSALU(*MI); };
339
340 bool IsBufferSMRD = SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORD_IMM ||
341 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX2_IMM ||
342 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX4_IMM ||
343 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX8_IMM ||
344 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX16_IMM ||
345 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORD_SGPR ||
346 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX2_SGPR ||
347 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX4_SGPR ||
348 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX8_SGPR ||
349 SMRD->getOpcode() == AMDGPU::S_BUFFER_LOAD_DWORDX16_SGPR;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000350
351 for (const MachineOperand &Use : SMRD->uses()) {
352 if (!Use.isReg())
353 continue;
354 int WaitStatesNeededForUse =
355 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
356 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
Marek Olsak22322432017-10-26 14:43:02 +0000357
358 // This fixes what appears to be undocumented hardware behavior in SI where
359 // s_mov writing a descriptor and s_buffer_load_dword reading the descriptor
360 // needs some number of nops in between. We don't know how many we need, but
361 // let's use 4. This wasn't discovered before probably because the only
362 // case when this happens is when we expand a 64-bit pointer into a full
363 // descriptor and use s_buffer_load_dword instead of s_load_dword, which was
364 // probably never encountered in the closed-source land.
365 if (IsBufferSMRD) {
366 int WaitStatesNeededForUse =
367 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(),
368 IsBufferHazardDefFn);
369 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
370 }
Tom Stellardcb6ba622016-04-30 00:23:06 +0000371 }
Marek Olsak22322432017-10-26 14:43:02 +0000372
Tom Stellardcb6ba622016-04-30 00:23:06 +0000373 return WaitStatesNeeded;
374}
375
376int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000377 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellardcb6ba622016-04-30 00:23:06 +0000378
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000379 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000380 return 0;
381
382 const SIRegisterInfo &TRI = TII->getRegisterInfo();
383
384 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
385 // SGPR was written by a VALU Instruction.
386 int VmemSgprWaitStates = 5;
387 int WaitStatesNeeded = 0;
388 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
389
390 for (const MachineOperand &Use : VMEM->uses()) {
391 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
392 continue;
393
394 int WaitStatesNeededForUse =
395 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
396 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
397 }
398 return WaitStatesNeeded;
399}
Tom Stellarda27007e2016-05-02 16:23:09 +0000400
401int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000402 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Connor Abbott00755362017-08-04 01:09:43 +0000403 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000404
Connor Abbott00755362017-08-04 01:09:43 +0000405 // Check for DPP VGPR read after VALU VGPR write and EXEC write.
Tom Stellarda27007e2016-05-02 16:23:09 +0000406 int DppVgprWaitStates = 2;
Connor Abbott00755362017-08-04 01:09:43 +0000407 int DppExecWaitStates = 5;
Tom Stellarda27007e2016-05-02 16:23:09 +0000408 int WaitStatesNeeded = 0;
Connor Abbott00755362017-08-04 01:09:43 +0000409 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
Tom Stellarda27007e2016-05-02 16:23:09 +0000410
411 for (const MachineOperand &Use : DPP->uses()) {
412 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
413 continue;
414 int WaitStatesNeededForUse =
415 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
416 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
417 }
418
Connor Abbott00755362017-08-04 01:09:43 +0000419 WaitStatesNeeded = std::max(
420 WaitStatesNeeded,
421 DppExecWaitStates - getWaitStatesSinceDef(AMDGPU::EXEC, IsHazardDefFn));
422
Tom Stellarda27007e2016-05-02 16:23:09 +0000423 return WaitStatesNeeded;
424}
Tom Stellard5ab61542016-10-07 23:42:48 +0000425
426int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
427 const SIInstrInfo *TII = ST.getInstrInfo();
428
429 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
430 // instruction.
431 const int DivFMasWaitStates = 4;
432 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
433 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
434
435 return DivFMasWaitStates - WaitStatesNeeded;
436}
Tom Stellard961811c2016-10-15 00:58:14 +0000437
438int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
439 const SIInstrInfo *TII = ST.getInstrInfo();
440 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
441
442 const int GetRegWaitStates = 2;
443 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
444 return GetRegHWReg == getHWReg(TII, *MI);
445 };
446 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
447
448 return GetRegWaitStates - WaitStatesNeeded;
449}
Tom Stellard30d30822016-10-27 20:39:09 +0000450
451int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
452 const SIInstrInfo *TII = ST.getInstrInfo();
453 unsigned HWReg = getHWReg(TII, *SetRegInstr);
454
455 const int SetRegWaitStates =
456 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
457 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
458 return HWReg == getHWReg(TII, *MI);
459 };
460 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
461 return SetRegWaitStates - WaitStatesNeeded;
462}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000463
464int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
465 if (!MI.mayStore())
466 return -1;
467
468 const SIInstrInfo *TII = ST.getInstrInfo();
469 unsigned Opcode = MI.getOpcode();
470 const MCInstrDesc &Desc = MI.getDesc();
471
472 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
473 int VDataRCID = -1;
474 if (VDataIdx != -1)
475 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
476
477 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000478 // There is no hazard if the instruction does not use vector regs
479 // (like wbinvl1)
480 if (VDataIdx == -1)
481 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000482 // For MUBUF/MTBUF instructions this hazard only exists if the
483 // instruction is not using a register in the soffset field.
484 const MachineOperand *SOffset =
485 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
486 // If we have no soffset operand, then assume this field has been
487 // hardcoded to zero.
488 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
489 (!SOffset || !SOffset->isReg()))
490 return VDataIdx;
491 }
492
493 // MIMG instructions create a hazard if they don't use a 256-bit T# and
494 // the store size is greater than 8 bytes and they have more than two bits
495 // of their dmask set.
496 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
497 if (TII->isMIMG(MI)) {
498 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
499 assert(SRsrcIdx != -1 &&
500 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000501 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000502 }
503
504 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000505 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000506 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
507 return DataIdx;
508 }
509
510 return -1;
511}
512
513int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
514 // This checks for the hazard where VMEM instructions that store more than
515 // 8 bytes can have there store data over written by the next instruction.
516 if (!ST.has12DWordStoreHazard())
517 return 0;
518
519 const SIRegisterInfo *TRI = ST.getRegisterInfo();
520 const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo();
521
522 const int VALUWaitStates = 1;
523 int WaitStatesNeeded = 0;
524
525 for (const MachineOperand &Def : VALU->defs()) {
526 if (!TRI->isVGPR(MRI, Def.getReg()))
527 continue;
528 unsigned Reg = Def.getReg();
529 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
530 int DataIdx = createsVALUHazard(*MI);
531 return DataIdx >= 0 &&
532 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
533 };
534 int WaitStatesNeededForDef =
535 VALUWaitStates - getWaitStatesSince(IsHazardFn);
536 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
537 }
538 return WaitStatesNeeded;
539}
Tom Stellard04051b52016-10-27 23:42:29 +0000540
541int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
542 const SIInstrInfo *TII = ST.getInstrInfo();
543 const SIRegisterInfo *TRI = ST.getRegisterInfo();
544 const MachineRegisterInfo &MRI =
545 RWLane->getParent()->getParent()->getRegInfo();
546
547 const MachineOperand *LaneSelectOp =
548 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
549
550 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
551 return 0;
552
553 unsigned LaneSelectReg = LaneSelectOp->getReg();
554 auto IsHazardFn = [TII] (MachineInstr *MI) {
555 return TII->isVALU(*MI);
556 };
557
558 const int RWLaneWaitStates = 4;
559 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
560 return RWLaneWaitStates - WaitStatesSince;
561}
Tom Stellardaea899e2016-10-27 23:50:21 +0000562
563int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000564 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
565 return 0;
566
567 const SIInstrInfo *TII = ST.getInstrInfo();
568
569 const int RFEWaitStates = 1;
570
571 auto IsHazardFn = [TII] (MachineInstr *MI) {
572 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
573 };
574 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
575 return RFEWaitStates - WaitStatesNeeded;
576}
Matt Arsenaulte823d922017-02-18 18:29:53 +0000577
578int GCNHazardRecognizer::checkAnyInstHazards(MachineInstr *MI) {
579 if (MI->isDebugValue())
580 return 0;
581
582 const SIRegisterInfo *TRI = ST.getRegisterInfo();
583 if (!ST.hasSMovFedHazard())
584 return 0;
585
586 // Check for any instruction reading an SGPR after a write from
587 // s_mov_fed_b32.
588 int MovFedWaitStates = 1;
589 int WaitStatesNeeded = 0;
590
591 for (const MachineOperand &Use : MI->uses()) {
592 if (!Use.isReg() || TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
593 continue;
594 auto IsHazardFn = [] (MachineInstr *MI) {
595 return MI->getOpcode() == AMDGPU::S_MOV_FED_B32;
596 };
597 int WaitStatesNeededForUse =
598 MovFedWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardFn);
599 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
600 }
601
602 return WaitStatesNeeded;
603}
604
605int GCNHazardRecognizer::checkReadM0Hazards(MachineInstr *MI) {
606 if (!ST.hasReadM0Hazard())
607 return 0;
608
609 const SIInstrInfo *TII = ST.getInstrInfo();
610 int SMovRelWaitStates = 1;
611 auto IsHazardFn = [TII] (MachineInstr *MI) {
612 return TII->isSALU(*MI);
613 };
614 return SMovRelWaitStates - getWaitStatesSinceDef(AMDGPU::M0, IsHazardFn);
615}