blob: b9551bed2562ebfbafac95828cb59f69afbd36b4 [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:
Matt Arsenault43e92fe2016-06-24 06:30:11 +000058 const SISubtarget *ST;
Tom Stellardc4cabef2013-01-18 21:15:53 +000059 const SIInstrInfo *TII;
Bill Wendling37e9adb2013-06-07 20:28:55 +000060 const SIRegisterInfo *TRI;
Tom Stellardc4cabef2013-01-18 21:15:53 +000061 const MachineRegisterInfo *MRI;
62
63 /// \brief Constant hardware limits
64 static const Counters WaitCounts;
65
66 /// \brief Constant zero value
67 static const Counters ZeroCounts;
68
69 /// \brief Counter values we have already waited on.
70 Counters WaitedOn;
71
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +000072 /// \brief Counter values that we must wait on before the next counter
73 /// increase.
74 Counters DelayedWaitOn;
75
Tom Stellardc4cabef2013-01-18 21:15:53 +000076 /// \brief Counter values for last instruction issued.
77 Counters LastIssued;
78
79 /// \brief Registers used by async instructions.
80 RegCounters UsedRegs;
81
82 /// \brief Registers defined by async instructions.
83 RegCounters DefinedRegs;
84
85 /// \brief Different export instruction types seen since last wait.
86 unsigned ExpInstrTypesSeen;
87
Marek Olsakfa58e5e2014-12-07 17:17:43 +000088 /// \brief Type of the last opcode.
89 InstType LastOpcodeType;
90
Marek Olsak1bd24632015-02-03 17:37:52 +000091 bool LastInstWritesM0;
92
Marek Olsak8e9cc632016-01-13 17:23:09 +000093 /// \brief Whether the machine function returns void
94 bool ReturnsVoid;
95
Tom Stellard30961762016-02-08 19:49:20 +000096 /// Whether the VCCZ bit is possibly corrupt
97 bool VCCZCorrupt;
98
Tom Stellardc4cabef2013-01-18 21:15:53 +000099 /// \brief Get increment/decrement amount for this instruction.
100 Counters getHwCounts(MachineInstr &MI);
101
102 /// \brief Is operand relevant for async execution?
103 bool isOpRelevant(MachineOperand &Op);
104
105 /// \brief Get register interval an operand affects.
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000106 RegInterval getRegInterval(const TargetRegisterClass *RC,
107 const MachineOperand &Reg) const;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000108
109 /// \brief Handle instructions async components
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000110 void pushInstruction(MachineBasicBlock &MBB,
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000111 MachineBasicBlock::iterator I,
112 const Counters& Increment);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000113
114 /// \brief Insert the actual wait instruction
115 bool insertWait(MachineBasicBlock &MBB,
116 MachineBasicBlock::iterator I,
117 const Counters &Counts);
118
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000119 /// \brief Handle existing wait instructions (from intrinsics)
120 void handleExistingWait(MachineBasicBlock::iterator I);
121
Christian Konig862fd9f2013-03-01 09:46:04 +0000122 /// \brief Do we need def2def checks?
123 bool unorderedDefines(MachineInstr &MI);
124
Tom Stellardc4cabef2013-01-18 21:15:53 +0000125 /// \brief Resolve all operand dependencies to counter requirements
126 Counters handleOperands(MachineInstr &MI);
127
Marek Olsak1bd24632015-02-03 17:37:52 +0000128 /// \brief Insert S_NOP between an instruction writing M0 and S_SENDMSG.
129 void handleSendMsg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I);
130
Tom Stellard30961762016-02-08 19:49:20 +0000131 /// Return true if there are LGKM instrucitons that haven't been waited on
132 /// yet.
133 bool hasOutstandingLGKM() const;
134
Tom Stellardc4cabef2013-01-18 21:15:53 +0000135public:
Tom Stellard6e1967e2016-02-05 17:42:38 +0000136 static char ID;
137
138 SIInsertWaits() :
Tom Stellardc4cabef2013-01-18 21:15:53 +0000139 MachineFunctionPass(ID),
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000140 ST(nullptr),
Craig Topper062a2ba2014-04-25 05:30:21 +0000141 TII(nullptr),
142 TRI(nullptr),
Tom Stellard30961762016-02-08 19:49:20 +0000143 ExpInstrTypesSeen(0),
144 VCCZCorrupt(false) { }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000145
Craig Topper5656db42014-04-29 07:57:24 +0000146 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000147
Craig Topper5656db42014-04-29 07:57:24 +0000148 const char *getPassName() const override {
Matt Arsenault0cb85172015-09-25 17:21:28 +0000149 return "SI insert wait instructions";
Tom Stellardc4cabef2013-01-18 21:15:53 +0000150 }
151
Matt Arsenault0cb85172015-09-25 17:21:28 +0000152 void getAnalysisUsage(AnalysisUsage &AU) const override {
153 AU.setPreservesCFG();
154 MachineFunctionPass::getAnalysisUsage(AU);
155 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000156};
157
158} // End anonymous namespace
159
Tom Stellard6e1967e2016-02-05 17:42:38 +0000160INITIALIZE_PASS_BEGIN(SIInsertWaits, DEBUG_TYPE,
161 "SI Insert Waits", false, false)
162INITIALIZE_PASS_END(SIInsertWaits, DEBUG_TYPE,
163 "SI Insert Waits", false, false)
164
Tom Stellardc4cabef2013-01-18 21:15:53 +0000165char SIInsertWaits::ID = 0;
166
Tom Stellard6e1967e2016-02-05 17:42:38 +0000167char &llvm::SIInsertWaitsID = SIInsertWaits::ID;
168
169FunctionPass *llvm::createSIInsertWaitsPass() {
170 return new SIInsertWaits();
171}
172
Tom Stellard3d2c8522016-01-28 17:13:44 +0000173const Counters SIInsertWaits::WaitCounts = { { 15, 7, 15 } };
Tom Stellardc4cabef2013-01-18 21:15:53 +0000174const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
175
Tom Stellard30961762016-02-08 19:49:20 +0000176static bool readsVCCZ(unsigned Opcode) {
Matt Arsenaultf2dcb472016-03-02 04:12:39 +0000177 return Opcode == AMDGPU::S_CBRANCH_VCCNZ || Opcode == AMDGPU::S_CBRANCH_VCCZ;
Tom Stellard30961762016-02-08 19:49:20 +0000178}
179
180bool SIInsertWaits::hasOutstandingLGKM() const {
181 return WaitedOn.Named.LGKM != LastIssued.Named.LGKM;
182}
Tom Stellardc4cabef2013-01-18 21:15:53 +0000183
184Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000185 uint64_t TSFlags = MI.getDesc().TSFlags;
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000186 Counters Result = { { 0, 0, 0 } };
Tom Stellardc4cabef2013-01-18 21:15:53 +0000187
188 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
189
190 // Only consider stores or EXP for EXP_CNT
191 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
Christian Konig862fd9f2013-03-01 09:46:04 +0000192 (MI.getOpcode() == AMDGPU::EXP || MI.getDesc().mayStore()));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000193
194 // LGKM may uses larger values
195 if (TSFlags & SIInstrFlags::LGKM_CNT) {
196
Matt Arsenault3add6432015-10-20 04:35:43 +0000197 if (TII->isSMRD(MI)) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000198
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000199 if (MI.getNumOperands() != 0) {
Matt Arsenaultb733f002015-10-01 22:40:35 +0000200 assert(MI.getOperand(0).isReg() &&
201 "First LGKM operand must be a register!");
Michel Danzer20680b12013-08-16 16:19:24 +0000202
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000203 // XXX - What if this is a write into a super register?
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000204 const TargetRegisterClass *RC = TII->getOpRegClass(MI, 0);
205 unsigned Size = RC->getSize();
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000206 Result.Named.LGKM = Size > 4 ? 2 : 1;
207 } else {
208 // s_dcache_inv etc. do not have a a destination register. Assume we
209 // want a wait on these.
210 // XXX - What is the right value?
211 Result.Named.LGKM = 1;
212 }
Michel Danzer20680b12013-08-16 16:19:24 +0000213 } else {
214 // DS
215 Result.Named.LGKM = 1;
216 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000217
218 } else {
219 Result.Named.LGKM = 0;
220 }
221
222 return Result;
223}
224
225bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000226 // Constants are always irrelevant
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000227 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
Tom Stellardc4cabef2013-01-18 21:15:53 +0000228 return false;
229
230 // Defines are always relevant
231 if (Op.isDef())
232 return true;
233
234 // For exports all registers are relevant
235 MachineInstr &MI = *Op.getParent();
236 if (MI.getOpcode() == AMDGPU::EXP)
237 return true;
238
239 // For stores the stored value is also relevant
240 if (!MI.getDesc().mayStore())
241 return false;
242
Tom Stellardb3931b82015-01-06 19:52:04 +0000243 // Check if this operand is the value being stored.
Tom Stellard2d26fe72016-02-19 15:33:13 +0000244 // Special case for DS/FLAT instructions, since the address
Tom Stellardb3931b82015-01-06 19:52:04 +0000245 // operand comes before the value operand and it may have
246 // multiple data operands.
247
Tom Stellard2d26fe72016-02-19 15:33:13 +0000248 if (TII->isDS(MI) || TII->isFLAT(MI)) {
Tom Stellardb3931b82015-01-06 19:52:04 +0000249 MachineOperand *Data = TII->getNamedOperand(MI, AMDGPU::OpName::data);
250 if (Data && Op.isIdenticalTo(*Data))
251 return true;
Tom Stellard2d26fe72016-02-19 15:33:13 +0000252 }
Tom Stellardb3931b82015-01-06 19:52:04 +0000253
Tom Stellard2d26fe72016-02-19 15:33:13 +0000254 if (TII->isDS(MI)) {
Tom Stellardb3931b82015-01-06 19:52:04 +0000255 MachineOperand *Data0 = TII->getNamedOperand(MI, AMDGPU::OpName::data0);
256 if (Data0 && Op.isIdenticalTo(*Data0))
257 return true;
258
259 MachineOperand *Data1 = TII->getNamedOperand(MI, AMDGPU::OpName::data1);
Matt Arsenault8226fc42016-03-02 23:00:21 +0000260 return Data1 && Op.isIdenticalTo(*Data1);
Tom Stellardb3931b82015-01-06 19:52:04 +0000261 }
262
263 // NOTE: This assumes that the value operand is before the
264 // address operand, and that there is only one value operand.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000265 for (MachineInstr::mop_iterator I = MI.operands_begin(),
266 E = MI.operands_end(); I != E; ++I) {
267
268 if (I->isReg() && I->isUse())
269 return Op.isIdenticalTo(*I);
270 }
271
272 return false;
273}
274
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000275RegInterval SIInsertWaits::getRegInterval(const TargetRegisterClass *RC,
276 const MachineOperand &Reg) const {
277 unsigned Size = RC->getSize();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000278 assert(Size >= 4);
279
280 RegInterval Result;
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000281 Result.first = TRI->getEncodingValue(Reg.getReg());
Tom Stellardc4cabef2013-01-18 21:15:53 +0000282 Result.second = Result.first + Size / 4;
283
284 return Result;
285}
286
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000287void SIInsertWaits::pushInstruction(MachineBasicBlock &MBB,
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000288 MachineBasicBlock::iterator I,
289 const Counters &Increment) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000290
291 // Get the hardware counter increments and sum them up
Tom Stellardbd8a0852015-08-21 22:47:27 +0000292 Counters Limit = ZeroCounts;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000293 unsigned Sum = 0;
294
295 for (unsigned i = 0; i < 3; ++i) {
296 LastIssued.Array[i] += Increment.Array[i];
Tom Stellardbd8a0852015-08-21 22:47:27 +0000297 if (Increment.Array[i])
298 Limit.Array[i] = LastIssued.Array[i];
Tom Stellardc4cabef2013-01-18 21:15:53 +0000299 Sum += Increment.Array[i];
300 }
301
302 // If we don't increase anything then that's it
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000303 if (Sum == 0) {
304 LastOpcodeType = OTHER;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000305 return;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000306 }
307
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000308 if (ST->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000309 // Any occurrence of consecutive VMEM or SMEM instructions forms a VMEM
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000310 // or SMEM clause, respectively.
311 //
312 // The temporary workaround is to break the clauses with S_NOP.
313 //
314 // The proper solution would be to allocate registers such that all source
315 // and destination registers don't overlap, e.g. this is illegal:
316 // r0 = load r2
317 // r2 = load r0
Tom Stellard1f520e52016-05-02 17:39:06 +0000318 if (LastOpcodeType == VMEM && Increment.Named.VM) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000319 // Insert a NOP to break the clause.
320 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP))
321 .addImm(0);
Marek Olsak1bd24632015-02-03 17:37:52 +0000322 LastInstWritesM0 = false;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000323 }
324
Matt Arsenault3add6432015-10-20 04:35:43 +0000325 if (TII->isSMRD(*I))
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000326 LastOpcodeType = SMEM;
327 else if (Increment.Named.VM)
328 LastOpcodeType = VMEM;
329 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000330
331 // Remember which export instructions we have seen
332 if (Increment.Named.EXP) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000333 ExpInstrTypesSeen |= I->getOpcode() == AMDGPU::EXP ? 1 : 2;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000334 }
335
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000336 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000337 MachineOperand &Op = I->getOperand(i);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000338 if (!isOpRelevant(Op))
339 continue;
340
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000341 const TargetRegisterClass *RC = TII->getOpRegClass(*I, i);
342 RegInterval Interval = getRegInterval(RC, Op);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000343 for (unsigned j = Interval.first; j < Interval.second; ++j) {
344
345 // Remember which registers we define
346 if (Op.isDef())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000347 DefinedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000348
349 // and which one we are using
350 if (Op.isUse())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000351 UsedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000352 }
353 }
354}
355
356bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
357 MachineBasicBlock::iterator I,
358 const Counters &Required) {
359
360 // End of program? No need to wait on anything
Marek Olsak8e9cc632016-01-13 17:23:09 +0000361 // A function not returning void needs to wait, because other bytecode will
362 // be appended after it and we don't know what it will be.
363 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM && ReturnsVoid)
Tom Stellardc4cabef2013-01-18 21:15:53 +0000364 return false;
365
366 // Figure out if the async instructions execute in order
367 bool Ordered[3];
368
369 // VM_CNT is always ordered
370 Ordered[0] = true;
371
372 // EXP_CNT is unordered if we have both EXP & VM-writes
373 Ordered[1] = ExpInstrTypesSeen == 3;
374
375 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
376 Ordered[2] = false;
377
378 // The values we are going to put into the S_WAITCNT instruction
379 Counters Counts = WaitCounts;
380
381 // Do we really need to wait?
382 bool NeedWait = false;
383
384 for (unsigned i = 0; i < 3; ++i) {
385
386 if (Required.Array[i] <= WaitedOn.Array[i])
387 continue;
388
389 NeedWait = true;
Matt Arsenault97483692014-07-17 17:50:22 +0000390
Tom Stellardc4cabef2013-01-18 21:15:53 +0000391 if (Ordered[i]) {
392 unsigned Value = LastIssued.Array[i] - Required.Array[i];
393
Matt Arsenault97483692014-07-17 17:50:22 +0000394 // Adjust the value to the real hardware possibilities.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000395 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
396
397 } else
398 Counts.Array[i] = 0;
399
Matt Arsenault97483692014-07-17 17:50:22 +0000400 // Remember on what we have waited on.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000401 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
402 }
403
404 if (!NeedWait)
405 return false;
406
407 // Reset EXP_CNT instruction types
408 if (Counts.Named.EXP == 0)
409 ExpInstrTypesSeen = 0;
410
411 // Build the wait instruction
412 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
413 .addImm((Counts.Named.VM & 0xF) |
414 ((Counts.Named.EXP & 0x7) << 4) |
Tom Stellard3d2c8522016-01-28 17:13:44 +0000415 ((Counts.Named.LGKM & 0xF) << 8));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000416
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000417 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000418 LastInstWritesM0 = false;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000419 return true;
420}
421
422/// \brief helper function for handleOperands
423static void increaseCounters(Counters &Dst, const Counters &Src) {
424
425 for (unsigned i = 0; i < 3; ++i)
426 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
427}
428
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000429/// \brief check whether any of the counters is non-zero
430static bool countersNonZero(const Counters &Counter) {
431 for (unsigned i = 0; i < 3; ++i)
432 if (Counter.Array[i])
433 return true;
434 return false;
435}
436
437void SIInsertWaits::handleExistingWait(MachineBasicBlock::iterator I) {
438 assert(I->getOpcode() == AMDGPU::S_WAITCNT);
439
440 unsigned Imm = I->getOperand(0).getImm();
441 Counters Counts, WaitOn;
442
443 Counts.Named.VM = Imm & 0xF;
444 Counts.Named.EXP = (Imm >> 4) & 0x7;
445 Counts.Named.LGKM = (Imm >> 8) & 0xF;
446
447 for (unsigned i = 0; i < 3; ++i) {
448 if (Counts.Array[i] <= LastIssued.Array[i])
449 WaitOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
450 else
451 WaitOn.Array[i] = 0;
452 }
453
454 increaseCounters(DelayedWaitOn, WaitOn);
455}
456
Tom Stellardc4cabef2013-01-18 21:15:53 +0000457Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
458
459 Counters Result = ZeroCounts;
460
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000461 // For each register affected by this instruction increase the result
462 // sequence.
463 //
464 // TODO: We could probably just look at explicit operands if we removed VCC /
465 // EXEC from SMRD dest reg classes.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000466 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000467 MachineOperand &Op = MI.getOperand(i);
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000468 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
469 continue;
470
471 const TargetRegisterClass *RC = TII->getOpRegClass(MI, i);
472 RegInterval Interval = getRegInterval(RC, Op);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000473 for (unsigned j = Interval.first; j < Interval.second; ++j) {
474
Christian Konig862fd9f2013-03-01 09:46:04 +0000475 if (Op.isDef()) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000476 increaseCounters(Result, UsedRegs[j]);
Christian Konigf1fd5fa2013-03-18 11:33:45 +0000477 increaseCounters(Result, DefinedRegs[j]);
Christian Konig862fd9f2013-03-01 09:46:04 +0000478 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000479
480 if (Op.isUse())
481 increaseCounters(Result, DefinedRegs[j]);
482 }
483 }
484
485 return Result;
486}
487
Marek Olsak1bd24632015-02-03 17:37:52 +0000488void SIInsertWaits::handleSendMsg(MachineBasicBlock &MBB,
489 MachineBasicBlock::iterator I) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000490 if (ST->getGeneration() < SISubtarget::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
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000517 ST = &MF.getSubtarget<SISubtarget>();
518 TII = ST->getInstrInfo();
519 TRI = &TII->getRegisterInfo();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000520 MRI = &MF.getRegInfo();
521
522 WaitedOn = ZeroCounts;
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000523 DelayedWaitOn = ZeroCounts;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000524 LastIssued = ZeroCounts;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000525 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000526 LastInstWritesM0 = false;
Marek Olsak8e9cc632016-01-13 17:23:09 +0000527 ReturnsVoid = MF.getInfo<SIMachineFunctionInfo>()->returnsVoid();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000528
529 memset(&UsedRegs, 0, sizeof(UsedRegs));
530 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
531
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000532 SmallVector<MachineInstr *, 4> RemoveMI;
533
Tom Stellardc4cabef2013-01-18 21:15:53 +0000534 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
535 BI != BE; ++BI) {
536
537 MachineBasicBlock &MBB = *BI;
538 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
539 I != E; ++I) {
540
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000541 if (ST->getGeneration() <= SISubtarget::SEA_ISLANDS) {
Tom Stellard30961762016-02-08 19:49:20 +0000542 // There is a hardware bug on CI/SI where SMRD instruction may corrupt
543 // vccz bit, so when we detect that an instruction may read from a
544 // corrupt vccz bit, we need to:
545 // 1. Insert s_waitcnt lgkm(0) to wait for all outstanding SMRD operations to
546 // complete.
547 // 2. Restore the correct value of vccz by writing the current value
548 // of vcc back to vcc.
549
550 if (TII->isSMRD(I->getOpcode())) {
551 VCCZCorrupt = true;
552 } else if (!hasOutstandingLGKM() && I->modifiesRegister(AMDGPU::VCC, TRI)) {
553 // FIXME: We only care about SMRD instructions here, not LDS or GDS.
554 // Whenever we store a value in vcc, the correct value of vccz is
555 // restored.
556 VCCZCorrupt = false;
557 }
558
559 // Check if we need to apply the bug work-around
560 if (readsVCCZ(I->getOpcode()) && VCCZCorrupt) {
561 DEBUG(dbgs() << "Inserting vccz bug work-around before: " << *I << '\n');
562
563 // Wait on everything, not just LGKM. vccz reads usually come from
564 // terminators, and we always wait on everything at the end of the
565 // block, so if we only wait on LGKM here, we might end up with
566 // another s_waitcnt inserted right after this if there are non-LGKM
567 // instructions still outstanding.
568 insertWait(MBB, I, LastIssued);
569
570 // Restore the vccz bit. Any time a value is written to vcc, the vcc
571 // bit is updated, so we can restore the bit by reading the value of
572 // vcc and then writing it back to the register.
573 BuildMI(MBB, I, I->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64),
574 AMDGPU::VCC)
575 .addReg(AMDGPU::VCC);
576 }
577 }
578
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000579 // Record pre-existing, explicitly requested waits
580 if (I->getOpcode() == AMDGPU::S_WAITCNT) {
581 handleExistingWait(*I);
Duncan P. N. Exon Smith4d295112016-07-08 19:16:05 +0000582 RemoveMI.push_back(&*I);
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000583 continue;
584 }
Marek Olsak1bd24632015-02-03 17:37:52 +0000585
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000586 Counters Required;
587
588 // Wait for everything before a barrier.
589 //
590 // S_SENDMSG implicitly waits for all outstanding LGKM transfers to finish,
591 // but we also want to wait for any other outstanding transfers before
592 // signalling other hardware blocks
Konstantin Zhuravlyovd7bdf242016-09-30 16:50:36 +0000593 if ((I->getOpcode() == AMDGPU::S_BARRIER &&
594 ST->needWaitcntBeforeBarrier()) ||
595 I->getOpcode() == AMDGPU::S_SENDMSG)
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000596 Required = LastIssued;
597 else
598 Required = handleOperands(*I);
599
600 Counters Increment = getHwCounts(*I);
601
602 if (countersNonZero(Required) || countersNonZero(Increment))
603 increaseCounters(Required, DelayedWaitOn);
604
605 Changes |= insertWait(MBB, I, Required);
606
607 pushInstruction(MBB, I, Increment);
Marek Olsak1bd24632015-02-03 17:37:52 +0000608 handleSendMsg(MBB, I);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000609 }
610
611 // Wait for everything at the end of the MBB
612 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000613 }
614
Nicolai Haehnlef66bdb52016-04-27 15:46:01 +0000615 for (MachineInstr *I : RemoveMI)
616 I->eraseFromParent();
617
Tom Stellardc4cabef2013-01-18 21:15:53 +0000618 return Changes;
619}