blob: 16b0b788318e8a9c2c0c6d404919f1a48a7e8d6f [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
Tom Stellardcb6ba622016-04-30 00:23:06 +000014#include "AMDGPUSubtarget.h"
Eugene Zelenko734bb7b2017-01-20 17:52:16 +000015#include "GCNHazardRecognizer.h"
16#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),
42 ST(MF.getSubtarget<SISubtarget>()) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000043 MaxLookAhead = 5;
44}
45
46void GCNHazardRecognizer::EmitInstruction(SUnit *SU) {
47 EmitInstruction(SU->getInstr());
48}
49
50void GCNHazardRecognizer::EmitInstruction(MachineInstr *MI) {
51 CurrCycleInstr = MI;
52}
53
Tom Stellard5ab61542016-10-07 23:42:48 +000054static bool isDivFMas(unsigned Opcode) {
55 return Opcode == AMDGPU::V_DIV_FMAS_F32 || Opcode == AMDGPU::V_DIV_FMAS_F64;
56}
57
Tom Stellard961811c2016-10-15 00:58:14 +000058static bool isSGetReg(unsigned Opcode) {
59 return Opcode == AMDGPU::S_GETREG_B32;
60}
61
62static bool isSSetReg(unsigned Opcode) {
63 return Opcode == AMDGPU::S_SETREG_B32 || Opcode == AMDGPU::S_SETREG_IMM32_B32;
64}
65
Tom Stellard04051b52016-10-27 23:42:29 +000066static bool isRWLane(unsigned Opcode) {
67 return Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32;
68}
69
Tom Stellardaea899e2016-10-27 23:50:21 +000070static bool isRFE(unsigned Opcode) {
71 return Opcode == AMDGPU::S_RFE_B64;
72}
73
74static unsigned getHWReg(const SIInstrInfo *TII, const MachineInstr &RegInstr) {
Tom Stellard961811c2016-10-15 00:58:14 +000075 const MachineOperand *RegOp = TII->getNamedOperand(RegInstr,
76 AMDGPU::OpName::simm16);
77 return RegOp->getImm() & AMDGPU::Hwreg::ID_MASK_;
78}
79
Tom Stellardcb6ba622016-04-30 00:23:06 +000080ScheduleHazardRecognizer::HazardType
81GCNHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
Tom Stellardcb6ba622016-04-30 00:23:06 +000082 MachineInstr *MI = SU->getInstr();
83
Aaron Ballman5c190d02016-05-02 14:48:03 +000084 if (SIInstrInfo::isSMRD(*MI) && checkSMRDHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +000085 return NoopHazard;
86
Aaron Ballman5c190d02016-05-02 14:48:03 +000087 if (SIInstrInfo::isVMEM(*MI) && checkVMEMHazards(MI) > 0)
Tom Stellardcb6ba622016-04-30 00:23:06 +000088 return NoopHazard;
89
Tom Stellardb133fbb2016-10-27 23:05:31 +000090 if (SIInstrInfo::isVALU(*MI) && checkVALUHazards(MI) > 0)
91 return NoopHazard;
92
Tom Stellarda27007e2016-05-02 16:23:09 +000093 if (SIInstrInfo::isDPP(*MI) && checkDPPHazards(MI) > 0)
94 return NoopHazard;
95
Tom Stellard5ab61542016-10-07 23:42:48 +000096 if (isDivFMas(MI->getOpcode()) && checkDivFMasHazards(MI) > 0)
97 return NoopHazard;
98
Tom Stellard04051b52016-10-27 23:42:29 +000099 if (isRWLane(MI->getOpcode()) && checkRWLaneHazards(MI) > 0)
100 return NoopHazard;
101
Tom Stellard961811c2016-10-15 00:58:14 +0000102 if (isSGetReg(MI->getOpcode()) && checkGetRegHazards(MI) > 0)
103 return NoopHazard;
104
Tom Stellard30d30822016-10-27 20:39:09 +0000105 if (isSSetReg(MI->getOpcode()) && checkSetRegHazards(MI) > 0)
106 return NoopHazard;
107
Tom Stellardaea899e2016-10-27 23:50:21 +0000108 if (isRFE(MI->getOpcode()) && checkRFEHazards(MI) > 0)
109 return NoopHazard;
110
Tom Stellardcb6ba622016-04-30 00:23:06 +0000111 return NoHazard;
112}
113
114unsigned GCNHazardRecognizer::PreEmitNoops(SUnit *SU) {
115 return PreEmitNoops(SU->getInstr());
116}
117
118unsigned GCNHazardRecognizer::PreEmitNoops(MachineInstr *MI) {
Aaron Ballman5c190d02016-05-02 14:48:03 +0000119 if (SIInstrInfo::isSMRD(*MI))
Tom Stellardcb6ba622016-04-30 00:23:06 +0000120 return std::max(0, checkSMRDHazards(MI));
121
Tom Stellardb133fbb2016-10-27 23:05:31 +0000122 if (SIInstrInfo::isVALU(*MI)) {
123 int WaitStates = std::max(0, checkVALUHazards(MI));
Tom Stellardcb6ba622016-04-30 00:23:06 +0000124
Tom Stellardb133fbb2016-10-27 23:05:31 +0000125 if (SIInstrInfo::isVMEM(*MI))
126 WaitStates = std::max(WaitStates, checkVMEMHazards(MI));
Tom Stellarda27007e2016-05-02 16:23:09 +0000127
Tom Stellardb133fbb2016-10-27 23:05:31 +0000128 if (SIInstrInfo::isDPP(*MI))
129 WaitStates = std::max(WaitStates, checkDPPHazards(MI));
130
131 if (isDivFMas(MI->getOpcode()))
132 WaitStates = std::max(WaitStates, checkDivFMasHazards(MI));
133
Tom Stellard04051b52016-10-27 23:42:29 +0000134 if (isRWLane(MI->getOpcode()))
135 WaitStates = std::max(WaitStates, checkRWLaneHazards(MI));
136
Tom Stellardb133fbb2016-10-27 23:05:31 +0000137 return WaitStates;
138 }
Tom Stellard5ab61542016-10-07 23:42:48 +0000139
Tom Stellard961811c2016-10-15 00:58:14 +0000140 if (isSGetReg(MI->getOpcode()))
141 return std::max(0, checkGetRegHazards(MI));
142
Tom Stellard30d30822016-10-27 20:39:09 +0000143 if (isSSetReg(MI->getOpcode()))
144 return std::max(0, checkSetRegHazards(MI));
145
Tom Stellardaea899e2016-10-27 23:50:21 +0000146 if (isRFE(MI->getOpcode()))
147 return std::max(0, checkRFEHazards(MI));
148
Tom Stellardcb6ba622016-04-30 00:23:06 +0000149 return 0;
150}
151
152void GCNHazardRecognizer::EmitNoop() {
153 EmittedInstrs.push_front(nullptr);
154}
155
156void GCNHazardRecognizer::AdvanceCycle() {
Tom Stellardcb6ba622016-04-30 00:23:06 +0000157 // When the scheduler detects a stall, it will call AdvanceCycle() without
158 // emitting any instructions.
159 if (!CurrCycleInstr)
160 return;
161
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000162 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellardcb6ba622016-04-30 00:23:06 +0000163 unsigned NumWaitStates = TII->getNumWaitStates(*CurrCycleInstr);
164
165 // Keep track of emitted instructions
166 EmittedInstrs.push_front(CurrCycleInstr);
167
168 // Add a nullptr for each additional wait state after the first. Make sure
169 // not to add more than getMaxLookAhead() items to the list, since we
170 // truncate the list to that size right after this loop.
171 for (unsigned i = 1, e = std::min(NumWaitStates, getMaxLookAhead());
172 i < e; ++i) {
173 EmittedInstrs.push_front(nullptr);
174 }
175
176 // getMaxLookahead() is the largest number of wait states we will ever need
177 // to insert, so there is no point in keeping track of more than that many
178 // wait states.
179 EmittedInstrs.resize(getMaxLookAhead());
180
181 CurrCycleInstr = nullptr;
182}
183
184void GCNHazardRecognizer::RecedeCycle() {
185 llvm_unreachable("hazard recognizer does not support bottom-up scheduling.");
186}
187
188//===----------------------------------------------------------------------===//
189// Helper Functions
190//===----------------------------------------------------------------------===//
191
Tom Stellardb133fbb2016-10-27 23:05:31 +0000192int GCNHazardRecognizer::getWaitStatesSince(
Tom Stellard961811c2016-10-15 00:58:14 +0000193 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellard961811c2016-10-15 00:58:14 +0000194 int WaitStates = -1;
195 for (MachineInstr *MI : EmittedInstrs) {
196 ++WaitStates;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000197 if (!MI || !IsHazard(MI))
Tom Stellard961811c2016-10-15 00:58:14 +0000198 continue;
199 return WaitStates;
200 }
201 return std::numeric_limits<int>::max();
202}
203
Tom Stellardb133fbb2016-10-27 23:05:31 +0000204int GCNHazardRecognizer::getWaitStatesSinceDef(
205 unsigned Reg, function_ref<bool(MachineInstr *)> IsHazardDef) {
206 const SIRegisterInfo *TRI = ST.getRegisterInfo();
207
208 auto IsHazardFn = [IsHazardDef, TRI, Reg] (MachineInstr *MI) {
209 return IsHazardDef(MI) && MI->modifiesRegister(Reg, TRI);
210 };
211
212 return getWaitStatesSince(IsHazardFn);
213}
214
215int GCNHazardRecognizer::getWaitStatesSinceSetReg(
216 function_ref<bool(MachineInstr *)> IsHazard) {
Tom Stellardb133fbb2016-10-27 23:05:31 +0000217 auto IsHazardFn = [IsHazard] (MachineInstr *MI) {
218 return isSSetReg(MI->getOpcode()) && IsHazard(MI);
219 };
220
221 return getWaitStatesSince(IsHazardFn);
222}
223
Tom Stellardcb6ba622016-04-30 00:23:06 +0000224//===----------------------------------------------------------------------===//
225// No-op Hazard Detection
226//===----------------------------------------------------------------------===//
227
Tom Stellard1f520e52016-05-02 17:39:06 +0000228static void addRegsToSet(iterator_range<MachineInstr::const_mop_iterator> Ops,
229 std::set<unsigned> &Set) {
230 for (const MachineOperand &Op : Ops) {
231 if (Op.isReg())
232 Set.insert(Op.getReg());
233 }
234}
235
236int GCNHazardRecognizer::checkSMEMSoftClauseHazards(MachineInstr *SMEM) {
Tom Stellard1f520e52016-05-02 17:39:06 +0000237 // SMEM soft clause are only present on VI+
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000238 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000239 return 0;
240
241 // A soft-clause is any group of consecutive SMEM instructions. The
242 // instructions in this group may return out of order and/or may be
243 // replayed (i.e. the same instruction issued more than once).
244 //
245 // In order to handle these situations correctly we need to make sure
246 // that when a clause has more than one instruction, no instruction in the
247 // clause writes to a register that is read another instruction in the clause
248 // (including itself). If we encounter this situaion, we need to break the
249 // clause by inserting a non SMEM instruction.
250
Tom Stellard1f520e52016-05-02 17:39:06 +0000251 std::set<unsigned> ClauseDefs;
252 std::set<unsigned> ClauseUses;
253
254 for (MachineInstr *MI : EmittedInstrs) {
255
256 // When we hit a non-SMEM instruction then we have passed the start of the
257 // clause and we can stop.
Aaron Ballman3bd56b32016-05-03 15:17:25 +0000258 if (!MI || !SIInstrInfo::isSMRD(*MI))
Tom Stellard1f520e52016-05-02 17:39:06 +0000259 break;
260
261 addRegsToSet(MI->defs(), ClauseDefs);
262 addRegsToSet(MI->uses(), ClauseUses);
263 }
264
265 if (ClauseDefs.empty())
266 return 0;
267
268 // FIXME: When we support stores, we need to make sure not to put loads and
269 // stores in the same clause if they use the same address. For now, just
270 // start a new clause whenever we see a store.
271 if (SMEM->mayStore())
272 return 1;
273
274 addRegsToSet(SMEM->defs(), ClauseDefs);
275 addRegsToSet(SMEM->uses(), ClauseUses);
276
277 std::vector<unsigned> Result(std::max(ClauseDefs.size(), ClauseUses.size()));
278 std::vector<unsigned>::iterator End;
279
280 End = std::set_intersection(ClauseDefs.begin(), ClauseDefs.end(),
281 ClauseUses.begin(), ClauseUses.end(), Result.begin());
282
283 // If the set of defs and uses intersect then we cannot add this instruction
284 // to the clause, so we have a hazard.
285 if (End != Result.begin())
286 return 1;
287
288 return 0;
289}
290
Tom Stellardcb6ba622016-04-30 00:23:06 +0000291int GCNHazardRecognizer::checkSMRDHazards(MachineInstr *SMRD) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000292 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
293 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellard1f520e52016-05-02 17:39:06 +0000294 int WaitStatesNeeded = 0;
295
296 WaitStatesNeeded = checkSMEMSoftClauseHazards(SMRD);
Tom Stellardcb6ba622016-04-30 00:23:06 +0000297
298 // This SMRD hazard only affects SI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000299 if (ST.getGeneration() != SISubtarget::SOUTHERN_ISLANDS)
Tom Stellard1f520e52016-05-02 17:39:06 +0000300 return WaitStatesNeeded;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000301
302 // A read of an SGPR by SMRD instruction requires 4 wait states when the
303 // SGPR was written by a VALU instruction.
304 int SmrdSgprWaitStates = 4;
Tom Stellardcb6ba622016-04-30 00:23:06 +0000305 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
306
307 for (const MachineOperand &Use : SMRD->uses()) {
308 if (!Use.isReg())
309 continue;
310 int WaitStatesNeededForUse =
311 SmrdSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
312 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
313 }
314 return WaitStatesNeeded;
315}
316
317int GCNHazardRecognizer::checkVMEMHazards(MachineInstr* VMEM) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000318 const SIInstrInfo *TII = ST.getInstrInfo();
Tom Stellardcb6ba622016-04-30 00:23:06 +0000319
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000320 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Tom Stellardcb6ba622016-04-30 00:23:06 +0000321 return 0;
322
323 const SIRegisterInfo &TRI = TII->getRegisterInfo();
324
325 // A read of an SGPR by a VMEM instruction requires 5 wait states when the
326 // SGPR was written by a VALU Instruction.
327 int VmemSgprWaitStates = 5;
328 int WaitStatesNeeded = 0;
329 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
330
331 for (const MachineOperand &Use : VMEM->uses()) {
332 if (!Use.isReg() || TRI.isVGPR(MF.getRegInfo(), Use.getReg()))
333 continue;
334
335 int WaitStatesNeededForUse =
336 VmemSgprWaitStates - getWaitStatesSinceDef(Use.getReg(), IsHazardDefFn);
337 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
338 }
339 return WaitStatesNeeded;
340}
Tom Stellarda27007e2016-05-02 16:23:09 +0000341
342int GCNHazardRecognizer::checkDPPHazards(MachineInstr *DPP) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000343 const SIRegisterInfo *TRI = ST.getRegisterInfo();
Tom Stellarda27007e2016-05-02 16:23:09 +0000344
345 // Check for DPP VGPR read after VALU VGPR write.
346 int DppVgprWaitStates = 2;
347 int WaitStatesNeeded = 0;
348
349 for (const MachineOperand &Use : DPP->uses()) {
350 if (!Use.isReg() || !TRI->isVGPR(MF.getRegInfo(), Use.getReg()))
351 continue;
352 int WaitStatesNeededForUse =
353 DppVgprWaitStates - getWaitStatesSinceDef(Use.getReg());
354 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForUse);
355 }
356
357 return WaitStatesNeeded;
358}
Tom Stellard5ab61542016-10-07 23:42:48 +0000359
360int GCNHazardRecognizer::checkDivFMasHazards(MachineInstr *DivFMas) {
361 const SIInstrInfo *TII = ST.getInstrInfo();
362
363 // v_div_fmas requires 4 wait states after a write to vcc from a VALU
364 // instruction.
365 const int DivFMasWaitStates = 4;
366 auto IsHazardDefFn = [TII] (MachineInstr *MI) { return TII->isVALU(*MI); };
367 int WaitStatesNeeded = getWaitStatesSinceDef(AMDGPU::VCC, IsHazardDefFn);
368
369 return DivFMasWaitStates - WaitStatesNeeded;
370}
Tom Stellard961811c2016-10-15 00:58:14 +0000371
372int GCNHazardRecognizer::checkGetRegHazards(MachineInstr *GetRegInstr) {
373 const SIInstrInfo *TII = ST.getInstrInfo();
374 unsigned GetRegHWReg = getHWReg(TII, *GetRegInstr);
375
376 const int GetRegWaitStates = 2;
377 auto IsHazardFn = [TII, GetRegHWReg] (MachineInstr *MI) {
378 return GetRegHWReg == getHWReg(TII, *MI);
379 };
380 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
381
382 return GetRegWaitStates - WaitStatesNeeded;
383}
Tom Stellard30d30822016-10-27 20:39:09 +0000384
385int GCNHazardRecognizer::checkSetRegHazards(MachineInstr *SetRegInstr) {
386 const SIInstrInfo *TII = ST.getInstrInfo();
387 unsigned HWReg = getHWReg(TII, *SetRegInstr);
388
389 const int SetRegWaitStates =
390 ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ? 1 : 2;
391 auto IsHazardFn = [TII, HWReg] (MachineInstr *MI) {
392 return HWReg == getHWReg(TII, *MI);
393 };
394 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
395 return SetRegWaitStates - WaitStatesNeeded;
396}
Tom Stellardb133fbb2016-10-27 23:05:31 +0000397
398int GCNHazardRecognizer::createsVALUHazard(const MachineInstr &MI) {
399 if (!MI.mayStore())
400 return -1;
401
402 const SIInstrInfo *TII = ST.getInstrInfo();
403 unsigned Opcode = MI.getOpcode();
404 const MCInstrDesc &Desc = MI.getDesc();
405
406 int VDataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
407 int VDataRCID = -1;
408 if (VDataIdx != -1)
409 VDataRCID = Desc.OpInfo[VDataIdx].RegClass;
410
411 if (TII->isMUBUF(MI) || TII->isMTBUF(MI)) {
Jan Veselye8cc3952016-11-15 23:55:15 +0000412 // There is no hazard if the instruction does not use vector regs
413 // (like wbinvl1)
414 if (VDataIdx == -1)
415 return -1;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000416 // For MUBUF/MTBUF instructions this hazard only exists if the
417 // instruction is not using a register in the soffset field.
418 const MachineOperand *SOffset =
419 TII->getNamedOperand(MI, AMDGPU::OpName::soffset);
420 // If we have no soffset operand, then assume this field has been
421 // hardcoded to zero.
422 if (AMDGPU::getRegBitWidth(VDataRCID) > 64 &&
423 (!SOffset || !SOffset->isReg()))
424 return VDataIdx;
425 }
426
427 // MIMG instructions create a hazard if they don't use a 256-bit T# and
428 // the store size is greater than 8 bytes and they have more than two bits
429 // of their dmask set.
430 // All our MIMG definitions use a 256-bit T#, so we can skip checking for them.
431 if (TII->isMIMG(MI)) {
432 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc);
433 assert(SRsrcIdx != -1 &&
434 AMDGPU::getRegBitWidth(Desc.OpInfo[SRsrcIdx].RegClass) == 256);
Tom Stellard6b9c1be2016-10-27 23:28:03 +0000435 (void)SRsrcIdx;
Tom Stellardb133fbb2016-10-27 23:05:31 +0000436 }
437
438 if (TII->isFLAT(MI)) {
Matt Arsenault97279a82016-11-29 19:30:44 +0000439 int DataIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdata);
Tom Stellardb133fbb2016-10-27 23:05:31 +0000440 if (AMDGPU::getRegBitWidth(Desc.OpInfo[DataIdx].RegClass) > 64)
441 return DataIdx;
442 }
443
444 return -1;
445}
446
447int GCNHazardRecognizer::checkVALUHazards(MachineInstr *VALU) {
448 // This checks for the hazard where VMEM instructions that store more than
449 // 8 bytes can have there store data over written by the next instruction.
450 if (!ST.has12DWordStoreHazard())
451 return 0;
452
453 const SIRegisterInfo *TRI = ST.getRegisterInfo();
454 const MachineRegisterInfo &MRI = VALU->getParent()->getParent()->getRegInfo();
455
456 const int VALUWaitStates = 1;
457 int WaitStatesNeeded = 0;
458
459 for (const MachineOperand &Def : VALU->defs()) {
460 if (!TRI->isVGPR(MRI, Def.getReg()))
461 continue;
462 unsigned Reg = Def.getReg();
463 auto IsHazardFn = [this, Reg, TRI] (MachineInstr *MI) {
464 int DataIdx = createsVALUHazard(*MI);
465 return DataIdx >= 0 &&
466 TRI->regsOverlap(MI->getOperand(DataIdx).getReg(), Reg);
467 };
468 int WaitStatesNeededForDef =
469 VALUWaitStates - getWaitStatesSince(IsHazardFn);
470 WaitStatesNeeded = std::max(WaitStatesNeeded, WaitStatesNeededForDef);
471 }
472 return WaitStatesNeeded;
473}
Tom Stellard04051b52016-10-27 23:42:29 +0000474
475int GCNHazardRecognizer::checkRWLaneHazards(MachineInstr *RWLane) {
476 const SIInstrInfo *TII = ST.getInstrInfo();
477 const SIRegisterInfo *TRI = ST.getRegisterInfo();
478 const MachineRegisterInfo &MRI =
479 RWLane->getParent()->getParent()->getRegInfo();
480
481 const MachineOperand *LaneSelectOp =
482 TII->getNamedOperand(*RWLane, AMDGPU::OpName::src1);
483
484 if (!LaneSelectOp->isReg() || !TRI->isSGPRReg(MRI, LaneSelectOp->getReg()))
485 return 0;
486
487 unsigned LaneSelectReg = LaneSelectOp->getReg();
488 auto IsHazardFn = [TII] (MachineInstr *MI) {
489 return TII->isVALU(*MI);
490 };
491
492 const int RWLaneWaitStates = 4;
493 int WaitStatesSince = getWaitStatesSinceDef(LaneSelectReg, IsHazardFn);
494 return RWLaneWaitStates - WaitStatesSince;
495}
Tom Stellardaea899e2016-10-27 23:50:21 +0000496
497int GCNHazardRecognizer::checkRFEHazards(MachineInstr *RFE) {
Tom Stellardaea899e2016-10-27 23:50:21 +0000498 if (ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
499 return 0;
500
501 const SIInstrInfo *TII = ST.getInstrInfo();
502
503 const int RFEWaitStates = 1;
504
505 auto IsHazardFn = [TII] (MachineInstr *MI) {
506 return getHWReg(TII, *MI) == AMDGPU::Hwreg::ID_TRAPSTS;
507 };
508 int WaitStatesNeeded = getWaitStatesSinceSetReg(IsHazardFn);
509 return RFEWaitStates - WaitStatesNeeded;
510}