blob: abc9c4d61848fc6fe9316b83f49260dac4b1128d [file] [log] [blame]
Tom Stellardc4cabef2013-01-18 21:15:53 +00001//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
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/// \file
11/// \brief Insert wait instructions for memory reads and writes.
12///
13/// Memory reads and writes are issued asynchronously, so we need to insert
14/// S_WAITCNT instructions when we want to access any of their results or
15/// overwrite any register that's used asynchronously.
16//
17//===----------------------------------------------------------------------===//
18
19#include "AMDGPU.h"
Eric Christopherd9134482014-08-04 21:25:23 +000020#include "AMDGPUSubtarget.h"
Matt Arsenault9783e002014-09-29 15:50:26 +000021#include "SIDefines.h"
Matt Arsenault1fd0c622014-09-29 15:53:15 +000022#include "SIInstrInfo.h"
Tom Stellardc4cabef2013-01-18 21:15:53 +000023#include "SIMachineFunctionInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28
Tom Stellard6e1967e2016-02-05 17:42:38 +000029#define DEBUG_TYPE "si-insert-waits"
30
Tom Stellardc4cabef2013-01-18 21:15:53 +000031using namespace llvm;
32
33namespace {
34
35/// \brief One variable for each of the hardware counters
36typedef union {
37 struct {
38 unsigned VM;
39 unsigned EXP;
40 unsigned LGKM;
41 } Named;
42 unsigned Array[3];
43
44} Counters;
45
Marek Olsakfa58e5e2014-12-07 17:17:43 +000046typedef enum {
47 OTHER,
48 SMEM,
49 VMEM
50} InstType;
51
Tom Stellardc4cabef2013-01-18 21:15:53 +000052typedef Counters RegCounters[512];
53typedef std::pair<unsigned, unsigned> RegInterval;
54
55class SIInsertWaits : public MachineFunctionPass {
56
57private:
Tom Stellardc4cabef2013-01-18 21:15:53 +000058 const SIInstrInfo *TII;
Bill Wendling37e9adb2013-06-07 20:28:55 +000059 const SIRegisterInfo *TRI;
Tom Stellardc4cabef2013-01-18 21:15:53 +000060 const MachineRegisterInfo *MRI;
61
62 /// \brief Constant hardware limits
63 static const Counters WaitCounts;
64
65 /// \brief Constant zero value
66 static const Counters ZeroCounts;
67
68 /// \brief Counter values we have already waited on.
69 Counters WaitedOn;
70
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +000071 /// \brief Counter values that we must wait on before the next counter
72 /// increase.
73 Counters DelayedWaitOn;
74
Tom Stellardc4cabef2013-01-18 21:15:53 +000075 /// \brief Counter values for last instruction issued.
76 Counters LastIssued;
77
78 /// \brief Registers used by async instructions.
79 RegCounters UsedRegs;
80
81 /// \brief Registers defined by async instructions.
82 RegCounters DefinedRegs;
83
84 /// \brief Different export instruction types seen since last wait.
85 unsigned ExpInstrTypesSeen;
86
Marek Olsakfa58e5e2014-12-07 17:17:43 +000087 /// \brief Type of the last opcode.
88 InstType LastOpcodeType;
89
Marek Olsak1bd24632015-02-03 17:37:52 +000090 bool LastInstWritesM0;
91
Marek Olsak8e9cc632016-01-13 17:23:09 +000092 /// \brief Whether the machine function returns void
93 bool ReturnsVoid;
94
Tom Stellard30961762016-02-08 19:49:20 +000095 /// Whether the VCCZ bit is possibly corrupt
96 bool VCCZCorrupt;
97
Tom Stellardc4cabef2013-01-18 21:15:53 +000098 /// \brief Get increment/decrement amount for this instruction.
99 Counters getHwCounts(MachineInstr &MI);
100
101 /// \brief Is operand relevant for async execution?
102 bool isOpRelevant(MachineOperand &Op);
103
104 /// \brief Get register interval an operand affects.
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000105 RegInterval getRegInterval(const TargetRegisterClass *RC,
106 const MachineOperand &Reg) const;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000107
108 /// \brief Handle instructions async components
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000109 void pushInstruction(MachineBasicBlock &MBB,
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000110 MachineBasicBlock::iterator I,
111 const Counters& Increment);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000112
113 /// \brief Insert the actual wait instruction
114 bool insertWait(MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator I,
116 const Counters &Counts);
117
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000118 /// \brief Handle existing wait instructions (from intrinsics)
119 void handleExistingWait(MachineBasicBlock::iterator I);
120
Christian Konig862fd9f2013-03-01 09:46:04 +0000121 /// \brief Do we need def2def checks?
122 bool unorderedDefines(MachineInstr &MI);
123
Tom Stellardc4cabef2013-01-18 21:15:53 +0000124 /// \brief Resolve all operand dependencies to counter requirements
125 Counters handleOperands(MachineInstr &MI);
126
Marek Olsak1bd24632015-02-03 17:37:52 +0000127 /// \brief Insert S_NOP between an instruction writing M0 and S_SENDMSG.
128 void handleSendMsg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I);
129
Tom Stellard30961762016-02-08 19:49:20 +0000130 /// Return true if there are LGKM instrucitons that haven't been waited on
131 /// yet.
132 bool hasOutstandingLGKM() const;
133
Tom Stellardc4cabef2013-01-18 21:15:53 +0000134public:
Tom Stellard6e1967e2016-02-05 17:42:38 +0000135 static char ID;
136
137 SIInsertWaits() :
Tom Stellardc4cabef2013-01-18 21:15:53 +0000138 MachineFunctionPass(ID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000139 TII(nullptr),
140 TRI(nullptr),
Tom Stellard30961762016-02-08 19:49:20 +0000141 ExpInstrTypesSeen(0),
142 VCCZCorrupt(false) { }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000143
Craig Topper5656db42014-04-29 07:57:24 +0000144 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000145
Craig Topper5656db42014-04-29 07:57:24 +0000146 const char *getPassName() const override {
Matt Arsenault0cb85172015-09-25 17:21:28 +0000147 return "SI insert wait instructions";
Tom Stellardc4cabef2013-01-18 21:15:53 +0000148 }
149
Matt Arsenault0cb85172015-09-25 17:21:28 +0000150 void getAnalysisUsage(AnalysisUsage &AU) const override {
151 AU.setPreservesCFG();
152 MachineFunctionPass::getAnalysisUsage(AU);
153 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000154};
155
156} // End anonymous namespace
157
Tom Stellard6e1967e2016-02-05 17:42:38 +0000158INITIALIZE_PASS_BEGIN(SIInsertWaits, DEBUG_TYPE,
159 "SI Insert Waits", false, false)
160INITIALIZE_PASS_END(SIInsertWaits, DEBUG_TYPE,
161 "SI Insert Waits", false, false)
162
Tom Stellardc4cabef2013-01-18 21:15:53 +0000163char SIInsertWaits::ID = 0;
164
Tom Stellard6e1967e2016-02-05 17:42:38 +0000165char &llvm::SIInsertWaitsID = SIInsertWaits::ID;
166
167FunctionPass *llvm::createSIInsertWaitsPass() {
168 return new SIInsertWaits();
169}
170
Tom Stellard3d2c8522016-01-28 17:13:44 +0000171const Counters SIInsertWaits::WaitCounts = { { 15, 7, 15 } };
Tom Stellardc4cabef2013-01-18 21:15:53 +0000172const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
173
Tom Stellard30961762016-02-08 19:49:20 +0000174static bool readsVCCZ(unsigned Opcode) {
Matt Arsenaultf2dcb472016-03-02 04:12:39 +0000175 return Opcode == AMDGPU::S_CBRANCH_VCCNZ || Opcode == AMDGPU::S_CBRANCH_VCCZ;
Tom Stellard30961762016-02-08 19:49:20 +0000176}
177
178bool SIInsertWaits::hasOutstandingLGKM() const {
179 return WaitedOn.Named.LGKM != LastIssued.Named.LGKM;
180}
Tom Stellardc4cabef2013-01-18 21:15:53 +0000181
182Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000183 uint64_t TSFlags = MI.getDesc().TSFlags;
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000184 Counters Result = { { 0, 0, 0 } };
Tom Stellardc4cabef2013-01-18 21:15:53 +0000185
186 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
187
188 // Only consider stores or EXP for EXP_CNT
189 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
Christian Konig862fd9f2013-03-01 09:46:04 +0000190 (MI.getOpcode() == AMDGPU::EXP || MI.getDesc().mayStore()));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000191
192 // LGKM may uses larger values
193 if (TSFlags & SIInstrFlags::LGKM_CNT) {
194
Matt Arsenault3add6432015-10-20 04:35:43 +0000195 if (TII->isSMRD(MI)) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000196
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000197 if (MI.getNumOperands() != 0) {
Matt Arsenaultb733f002015-10-01 22:40:35 +0000198 assert(MI.getOperand(0).isReg() &&
199 "First LGKM operand must be a register!");
Michel Danzer20680b12013-08-16 16:19:24 +0000200
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000201 // XXX - What if this is a write into a super register?
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000202 const TargetRegisterClass *RC = TII->getOpRegClass(MI, 0);
203 unsigned Size = RC->getSize();
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000204 Result.Named.LGKM = Size > 4 ? 2 : 1;
205 } else {
206 // s_dcache_inv etc. do not have a a destination register. Assume we
207 // want a wait on these.
208 // XXX - What is the right value?
209 Result.Named.LGKM = 1;
210 }
Michel Danzer20680b12013-08-16 16:19:24 +0000211 } else {
212 // DS
213 Result.Named.LGKM = 1;
214 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000215
216 } else {
217 Result.Named.LGKM = 0;
218 }
219
220 return Result;
221}
222
223bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000224 // Constants are always irrelevant
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000225 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
Tom Stellardc4cabef2013-01-18 21:15:53 +0000226 return false;
227
228 // Defines are always relevant
229 if (Op.isDef())
230 return true;
231
232 // For exports all registers are relevant
233 MachineInstr &MI = *Op.getParent();
234 if (MI.getOpcode() == AMDGPU::EXP)
235 return true;
236
237 // For stores the stored value is also relevant
238 if (!MI.getDesc().mayStore())
239 return false;
240
Tom Stellardb3931b82015-01-06 19:52:04 +0000241 // Check if this operand is the value being stored.
Tom Stellard2d26fe72016-02-19 15:33:13 +0000242 // Special case for DS/FLAT instructions, since the address
Tom Stellardb3931b82015-01-06 19:52:04 +0000243 // operand comes before the value operand and it may have
244 // multiple data operands.
245
Tom Stellard2d26fe72016-02-19 15:33:13 +0000246 if (TII->isDS(MI) || TII->isFLAT(MI)) {
Tom Stellardb3931b82015-01-06 19:52:04 +0000247 MachineOperand *Data = TII->getNamedOperand(MI, AMDGPU::OpName::data);
248 if (Data && Op.isIdenticalTo(*Data))
249 return true;
Tom Stellard2d26fe72016-02-19 15:33:13 +0000250 }
Tom Stellardb3931b82015-01-06 19:52:04 +0000251
Tom Stellard2d26fe72016-02-19 15:33:13 +0000252 if (TII->isDS(MI)) {
Tom Stellardb3931b82015-01-06 19:52:04 +0000253 MachineOperand *Data0 = TII->getNamedOperand(MI, AMDGPU::OpName::data0);
254 if (Data0 && Op.isIdenticalTo(*Data0))
255 return true;
256
257 MachineOperand *Data1 = TII->getNamedOperand(MI, AMDGPU::OpName::data1);
Matt Arsenault8226fc42016-03-02 23:00:21 +0000258 return Data1 && Op.isIdenticalTo(*Data1);
Tom Stellardb3931b82015-01-06 19:52:04 +0000259 }
260
261 // NOTE: This assumes that the value operand is before the
262 // address operand, and that there is only one value operand.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000263 for (MachineInstr::mop_iterator I = MI.operands_begin(),
264 E = MI.operands_end(); I != E; ++I) {
265
266 if (I->isReg() && I->isUse())
267 return Op.isIdenticalTo(*I);
268 }
269
270 return false;
271}
272
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000273RegInterval SIInsertWaits::getRegInterval(const TargetRegisterClass *RC,
274 const MachineOperand &Reg) const {
275 unsigned Size = RC->getSize();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000276 assert(Size >= 4);
277
278 RegInterval Result;
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000279 Result.first = TRI->getEncodingValue(Reg.getReg());
Tom Stellardc4cabef2013-01-18 21:15:53 +0000280 Result.second = Result.first + Size / 4;
281
282 return Result;
283}
284
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000285void SIInsertWaits::pushInstruction(MachineBasicBlock &MBB,
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000286 MachineBasicBlock::iterator I,
287 const Counters &Increment) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000288
289 // Get the hardware counter increments and sum them up
Tom Stellardbd8a0852015-08-21 22:47:27 +0000290 Counters Limit = ZeroCounts;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000291 unsigned Sum = 0;
292
293 for (unsigned i = 0; i < 3; ++i) {
294 LastIssued.Array[i] += Increment.Array[i];
Tom Stellardbd8a0852015-08-21 22:47:27 +0000295 if (Increment.Array[i])
296 Limit.Array[i] = LastIssued.Array[i];
Tom Stellardc4cabef2013-01-18 21:15:53 +0000297 Sum += Increment.Array[i];
298 }
299
300 // If we don't increase anything then that's it
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000301 if (Sum == 0) {
302 LastOpcodeType = OTHER;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000303 return;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000304 }
305
Eric Christopher6c5b5112015-03-11 18:43:21 +0000306 if (MBB.getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() >=
307 AMDGPUSubtarget::VOLCANIC_ISLANDS) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000308 // Any occurrence of consecutive VMEM or SMEM instructions forms a VMEM
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000309 // or SMEM clause, respectively.
310 //
311 // The temporary workaround is to break the clauses with S_NOP.
312 //
313 // The proper solution would be to allocate registers such that all source
314 // and destination registers don't overlap, e.g. this is illegal:
315 // r0 = load r2
316 // r2 = load r0
Tom Stellard1f520e52016-05-02 17:39:06 +0000317 if (LastOpcodeType == VMEM && Increment.Named.VM) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000318 // Insert a NOP to break the clause.
319 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP))
320 .addImm(0);
Marek Olsak1bd24632015-02-03 17:37:52 +0000321 LastInstWritesM0 = false;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000322 }
323
Matt Arsenault3add6432015-10-20 04:35:43 +0000324 if (TII->isSMRD(*I))
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000325 LastOpcodeType = SMEM;
326 else if (Increment.Named.VM)
327 LastOpcodeType = VMEM;
328 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000329
330 // Remember which export instructions we have seen
331 if (Increment.Named.EXP) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000332 ExpInstrTypesSeen |= I->getOpcode() == AMDGPU::EXP ? 1 : 2;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000333 }
334
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000335 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000336 MachineOperand &Op = I->getOperand(i);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000337 if (!isOpRelevant(Op))
338 continue;
339
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000340 const TargetRegisterClass *RC = TII->getOpRegClass(*I, i);
341 RegInterval Interval = getRegInterval(RC, Op);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000342 for (unsigned j = Interval.first; j < Interval.second; ++j) {
343
344 // Remember which registers we define
345 if (Op.isDef())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000346 DefinedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000347
348 // and which one we are using
349 if (Op.isUse())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000350 UsedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000351 }
352 }
353}
354
355bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
356 MachineBasicBlock::iterator I,
357 const Counters &Required) {
358
359 // End of program? No need to wait on anything
Marek Olsak8e9cc632016-01-13 17:23:09 +0000360 // A function not returning void needs to wait, because other bytecode will
361 // be appended after it and we don't know what it will be.
362 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM && ReturnsVoid)
Tom Stellardc4cabef2013-01-18 21:15:53 +0000363 return false;
364
365 // Figure out if the async instructions execute in order
366 bool Ordered[3];
367
368 // VM_CNT is always ordered
369 Ordered[0] = true;
370
371 // EXP_CNT is unordered if we have both EXP & VM-writes
372 Ordered[1] = ExpInstrTypesSeen == 3;
373
374 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
375 Ordered[2] = false;
376
377 // The values we are going to put into the S_WAITCNT instruction
378 Counters Counts = WaitCounts;
379
380 // Do we really need to wait?
381 bool NeedWait = false;
382
383 for (unsigned i = 0; i < 3; ++i) {
384
385 if (Required.Array[i] <= WaitedOn.Array[i])
386 continue;
387
388 NeedWait = true;
Matt Arsenault97483692014-07-17 17:50:22 +0000389
Tom Stellardc4cabef2013-01-18 21:15:53 +0000390 if (Ordered[i]) {
391 unsigned Value = LastIssued.Array[i] - Required.Array[i];
392
Matt Arsenault97483692014-07-17 17:50:22 +0000393 // Adjust the value to the real hardware possibilities.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000394 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
395
396 } else
397 Counts.Array[i] = 0;
398
Matt Arsenault97483692014-07-17 17:50:22 +0000399 // Remember on what we have waited on.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000400 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
401 }
402
403 if (!NeedWait)
404 return false;
405
406 // Reset EXP_CNT instruction types
407 if (Counts.Named.EXP == 0)
408 ExpInstrTypesSeen = 0;
409
410 // Build the wait instruction
411 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
412 .addImm((Counts.Named.VM & 0xF) |
413 ((Counts.Named.EXP & 0x7) << 4) |
Tom Stellard3d2c8522016-01-28 17:13:44 +0000414 ((Counts.Named.LGKM & 0xF) << 8));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000415
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000416 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000417 LastInstWritesM0 = false;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000418 return true;
419}
420
421/// \brief helper function for handleOperands
422static void increaseCounters(Counters &Dst, const Counters &Src) {
423
424 for (unsigned i = 0; i < 3; ++i)
425 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
426}
427
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000428/// \brief check whether any of the counters is non-zero
429static bool countersNonZero(const Counters &Counter) {
430 for (unsigned i = 0; i < 3; ++i)
431 if (Counter.Array[i])
432 return true;
433 return false;
434}
435
436void SIInsertWaits::handleExistingWait(MachineBasicBlock::iterator I) {
437 assert(I->getOpcode() == AMDGPU::S_WAITCNT);
438
439 unsigned Imm = I->getOperand(0).getImm();
440 Counters Counts, WaitOn;
441
442 Counts.Named.VM = Imm & 0xF;
443 Counts.Named.EXP = (Imm >> 4) & 0x7;
444 Counts.Named.LGKM = (Imm >> 8) & 0xF;
445
446 for (unsigned i = 0; i < 3; ++i) {
447 if (Counts.Array[i] <= LastIssued.Array[i])
448 WaitOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
449 else
450 WaitOn.Array[i] = 0;
451 }
452
453 increaseCounters(DelayedWaitOn, WaitOn);
454}
455
Tom Stellardc4cabef2013-01-18 21:15:53 +0000456Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
457
458 Counters Result = ZeroCounts;
459
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000460 // For each register affected by this instruction increase the result
461 // sequence.
462 //
463 // TODO: We could probably just look at explicit operands if we removed VCC /
464 // EXEC from SMRD dest reg classes.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000465 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000466 MachineOperand &Op = MI.getOperand(i);
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000467 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
468 continue;
469
470 const TargetRegisterClass *RC = TII->getOpRegClass(MI, i);
471 RegInterval Interval = getRegInterval(RC, Op);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000472 for (unsigned j = Interval.first; j < Interval.second; ++j) {
473
Christian Konig862fd9f2013-03-01 09:46:04 +0000474 if (Op.isDef()) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000475 increaseCounters(Result, UsedRegs[j]);
Christian Konigf1fd5fa2013-03-18 11:33:45 +0000476 increaseCounters(Result, DefinedRegs[j]);
Christian Konig862fd9f2013-03-01 09:46:04 +0000477 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000478
479 if (Op.isUse())
480 increaseCounters(Result, DefinedRegs[j]);
481 }
482 }
483
484 return Result;
485}
486
Marek Olsak1bd24632015-02-03 17:37:52 +0000487void SIInsertWaits::handleSendMsg(MachineBasicBlock &MBB,
488 MachineBasicBlock::iterator I) {
Eric Christopher6c5b5112015-03-11 18:43:21 +0000489 if (MBB.getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() <
490 AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak1bd24632015-02-03 17:37:52 +0000491 return;
492
493 // There must be "S_NOP 0" between an instruction writing M0 and S_SENDMSG.
494 if (LastInstWritesM0 && I->getOpcode() == AMDGPU::S_SENDMSG) {
495 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP)).addImm(0);
496 LastInstWritesM0 = false;
497 return;
498 }
499
500 // Set whether this instruction sets M0
501 LastInstWritesM0 = false;
502
503 unsigned NumOperands = I->getNumOperands();
504 for (unsigned i = 0; i < NumOperands; i++) {
505 const MachineOperand &Op = I->getOperand(i);
506
507 if (Op.isReg() && Op.isDef() && Op.getReg() == AMDGPU::M0)
508 LastInstWritesM0 = true;
509 }
510}
511
Matt Arsenaulta0050b02014-06-19 01:19:19 +0000512// FIXME: Insert waits listed in Table 4.2 "Required User-Inserted Wait States"
513// around other non-memory instructions.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000514bool SIInsertWaits::runOnMachineFunction(MachineFunction &MF) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000515 bool Changes = false;
516
Eric Christopherfc6de422014-08-05 02:39:49 +0000517 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
518 TRI =
519 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Bill Wendling37e9adb2013-06-07 20:28:55 +0000520
Tom Stellard30961762016-02-08 19:49:20 +0000521 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000522 MRI = &MF.getRegInfo();
523
524 WaitedOn = ZeroCounts;
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000525 DelayedWaitOn = ZeroCounts;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000526 LastIssued = ZeroCounts;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000527 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000528 LastInstWritesM0 = false;
Marek Olsak8e9cc632016-01-13 17:23:09 +0000529 ReturnsVoid = MF.getInfo<SIMachineFunctionInfo>()->returnsVoid();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000530
531 memset(&UsedRegs, 0, sizeof(UsedRegs));
532 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
533
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000534 SmallVector<MachineInstr *, 4> RemoveMI;
535
Tom Stellardc4cabef2013-01-18 21:15:53 +0000536 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
537 BI != BE; ++BI) {
538
539 MachineBasicBlock &MBB = *BI;
540 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
541 I != E; ++I) {
542
Tom Stellard30961762016-02-08 19:49:20 +0000543 if (ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) {
544 // There is a hardware bug on CI/SI where SMRD instruction may corrupt
545 // vccz bit, so when we detect that an instruction may read from a
546 // corrupt vccz bit, we need to:
547 // 1. Insert s_waitcnt lgkm(0) to wait for all outstanding SMRD operations to
548 // complete.
549 // 2. Restore the correct value of vccz by writing the current value
550 // of vcc back to vcc.
551
552 if (TII->isSMRD(I->getOpcode())) {
553 VCCZCorrupt = true;
554 } else if (!hasOutstandingLGKM() && I->modifiesRegister(AMDGPU::VCC, TRI)) {
555 // FIXME: We only care about SMRD instructions here, not LDS or GDS.
556 // Whenever we store a value in vcc, the correct value of vccz is
557 // restored.
558 VCCZCorrupt = false;
559 }
560
561 // Check if we need to apply the bug work-around
562 if (readsVCCZ(I->getOpcode()) && VCCZCorrupt) {
563 DEBUG(dbgs() << "Inserting vccz bug work-around before: " << *I << '\n');
564
565 // Wait on everything, not just LGKM. vccz reads usually come from
566 // terminators, and we always wait on everything at the end of the
567 // block, so if we only wait on LGKM here, we might end up with
568 // another s_waitcnt inserted right after this if there are non-LGKM
569 // instructions still outstanding.
570 insertWait(MBB, I, LastIssued);
571
572 // Restore the vccz bit. Any time a value is written to vcc, the vcc
573 // bit is updated, so we can restore the bit by reading the value of
574 // vcc and then writing it back to the register.
575 BuildMI(MBB, I, I->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64),
576 AMDGPU::VCC)
577 .addReg(AMDGPU::VCC);
578 }
579 }
580
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000581 // Record pre-existing, explicitly requested waits
582 if (I->getOpcode() == AMDGPU::S_WAITCNT) {
583 handleExistingWait(*I);
584 RemoveMI.push_back(I);
585 continue;
586 }
Marek Olsak1bd24632015-02-03 17:37:52 +0000587
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000588 Counters Required;
589
590 // Wait for everything before a barrier.
591 //
592 // S_SENDMSG implicitly waits for all outstanding LGKM transfers to finish,
593 // but we also want to wait for any other outstanding transfers before
594 // signalling other hardware blocks
595 if (I->getOpcode() == AMDGPU::S_BARRIER ||
596 I->getOpcode() == AMDGPU::S_SENDMSG)
597 Required = LastIssued;
598 else
599 Required = handleOperands(*I);
600
601 Counters Increment = getHwCounts(*I);
602
603 if (countersNonZero(Required) || countersNonZero(Increment))
604 increaseCounters(Required, DelayedWaitOn);
605
606 Changes |= insertWait(MBB, I, Required);
607
608 pushInstruction(MBB, I, Increment);
Marek Olsak1bd24632015-02-03 17:37:52 +0000609 handleSendMsg(MBB, I);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000610 }
611
612 // Wait for everything at the end of the MBB
613 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000614 }
615
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000616 for (MachineInstr *I : RemoveMI)
617 I->eraseFromParent();
618
Tom Stellardc4cabef2013-01-18 21:15:53 +0000619 return Changes;
620}