blob: 2379b1fcf6a36240013b518b7d0c46d23d46d25c [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
29using namespace llvm;
30
31namespace {
32
33/// \brief One variable for each of the hardware counters
34typedef union {
35 struct {
36 unsigned VM;
37 unsigned EXP;
38 unsigned LGKM;
39 } Named;
40 unsigned Array[3];
41
42} Counters;
43
Marek Olsakfa58e5e2014-12-07 17:17:43 +000044typedef enum {
45 OTHER,
46 SMEM,
47 VMEM
48} InstType;
49
Tom Stellardc4cabef2013-01-18 21:15:53 +000050typedef Counters RegCounters[512];
51typedef std::pair<unsigned, unsigned> RegInterval;
52
53class SIInsertWaits : public MachineFunctionPass {
54
55private:
56 static char ID;
57 const SIInstrInfo *TII;
Bill Wendling37e9adb2013-06-07 20:28:55 +000058 const SIRegisterInfo *TRI;
Tom Stellardc4cabef2013-01-18 21:15:53 +000059 const MachineRegisterInfo *MRI;
60
61 /// \brief Constant hardware limits
62 static const Counters WaitCounts;
63
64 /// \brief Constant zero value
65 static const Counters ZeroCounts;
66
67 /// \brief Counter values we have already waited on.
68 Counters WaitedOn;
69
70 /// \brief Counter values for last instruction issued.
71 Counters LastIssued;
72
73 /// \brief Registers used by async instructions.
74 RegCounters UsedRegs;
75
76 /// \brief Registers defined by async instructions.
77 RegCounters DefinedRegs;
78
79 /// \brief Different export instruction types seen since last wait.
80 unsigned ExpInstrTypesSeen;
81
Marek Olsakfa58e5e2014-12-07 17:17:43 +000082 /// \brief Type of the last opcode.
83 InstType LastOpcodeType;
84
Marek Olsak1bd24632015-02-03 17:37:52 +000085 bool LastInstWritesM0;
86
Tom Stellardc4cabef2013-01-18 21:15:53 +000087 /// \brief Get increment/decrement amount for this instruction.
88 Counters getHwCounts(MachineInstr &MI);
89
90 /// \brief Is operand relevant for async execution?
91 bool isOpRelevant(MachineOperand &Op);
92
93 /// \brief Get register interval an operand affects.
94 RegInterval getRegInterval(MachineOperand &Op);
95
96 /// \brief Handle instructions async components
Marek Olsakfa58e5e2014-12-07 17:17:43 +000097 void pushInstruction(MachineBasicBlock &MBB,
98 MachineBasicBlock::iterator I);
Tom Stellardc4cabef2013-01-18 21:15:53 +000099
100 /// \brief Insert the actual wait instruction
101 bool insertWait(MachineBasicBlock &MBB,
102 MachineBasicBlock::iterator I,
103 const Counters &Counts);
104
Christian Konig862fd9f2013-03-01 09:46:04 +0000105 /// \brief Do we need def2def checks?
106 bool unorderedDefines(MachineInstr &MI);
107
Tom Stellardc4cabef2013-01-18 21:15:53 +0000108 /// \brief Resolve all operand dependencies to counter requirements
109 Counters handleOperands(MachineInstr &MI);
110
Marek Olsak1bd24632015-02-03 17:37:52 +0000111 /// \brief Insert S_NOP between an instruction writing M0 and S_SENDMSG.
112 void handleSendMsg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I);
113
Tom Stellardc4cabef2013-01-18 21:15:53 +0000114public:
115 SIInsertWaits(TargetMachine &tm) :
116 MachineFunctionPass(ID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000117 TII(nullptr),
118 TRI(nullptr),
Evgeniy Stepanovbc8808c2013-08-07 07:47:41 +0000119 ExpInstrTypesSeen(0) { }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000120
Craig Topper5656db42014-04-29 07:57:24 +0000121 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000122
Craig Topper5656db42014-04-29 07:57:24 +0000123 const char *getPassName() const override {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000124 return "SI insert wait instructions";
125 }
126
127};
128
129} // End anonymous namespace
130
131char SIInsertWaits::ID = 0;
132
133const Counters SIInsertWaits::WaitCounts = { { 15, 7, 7 } };
134const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
135
136FunctionPass *llvm::createSIInsertWaits(TargetMachine &tm) {
137 return new SIInsertWaits(tm);
138}
139
140Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
141
142 uint64_t TSFlags = TII->get(MI.getOpcode()).TSFlags;
143 Counters Result;
144
145 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
146
147 // Only consider stores or EXP for EXP_CNT
148 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
Christian Konig862fd9f2013-03-01 09:46:04 +0000149 (MI.getOpcode() == AMDGPU::EXP || MI.getDesc().mayStore()));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000150
151 // LGKM may uses larger values
152 if (TSFlags & SIInstrFlags::LGKM_CNT) {
153
Michel Danzer20680b12013-08-16 16:19:24 +0000154 if (TII->isSMRD(MI.getOpcode())) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000155
Michel Danzer20680b12013-08-16 16:19:24 +0000156 MachineOperand &Op = MI.getOperand(0);
157 assert(Op.isReg() && "First LGKM operand must be a register!");
158
159 unsigned Reg = Op.getReg();
160 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
161 Result.Named.LGKM = Size > 4 ? 2 : 1;
162
163 } else {
164 // DS
165 Result.Named.LGKM = 1;
166 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000167
168 } else {
169 Result.Named.LGKM = 0;
170 }
171
172 return Result;
173}
174
175bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
176
177 // Constants are always irrelevant
178 if (!Op.isReg())
179 return false;
180
181 // Defines are always relevant
182 if (Op.isDef())
183 return true;
184
185 // For exports all registers are relevant
186 MachineInstr &MI = *Op.getParent();
187 if (MI.getOpcode() == AMDGPU::EXP)
188 return true;
189
190 // For stores the stored value is also relevant
191 if (!MI.getDesc().mayStore())
192 return false;
193
Tom Stellardb3931b82015-01-06 19:52:04 +0000194 // Check if this operand is the value being stored.
195 // Special case for DS instructions, since the address
196 // operand comes before the value operand and it may have
197 // multiple data operands.
198
199 if (TII->isDS(MI.getOpcode())) {
200 MachineOperand *Data = TII->getNamedOperand(MI, AMDGPU::OpName::data);
201 if (Data && Op.isIdenticalTo(*Data))
202 return true;
203
204 MachineOperand *Data0 = TII->getNamedOperand(MI, AMDGPU::OpName::data0);
205 if (Data0 && Op.isIdenticalTo(*Data0))
206 return true;
207
208 MachineOperand *Data1 = TII->getNamedOperand(MI, AMDGPU::OpName::data1);
209 if (Data1 && Op.isIdenticalTo(*Data1))
210 return true;
211
212 return false;
213 }
214
215 // NOTE: This assumes that the value operand is before the
216 // address operand, and that there is only one value operand.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000217 for (MachineInstr::mop_iterator I = MI.operands_begin(),
218 E = MI.operands_end(); I != E; ++I) {
219
220 if (I->isReg() && I->isUse())
221 return Op.isIdenticalTo(*I);
222 }
223
224 return false;
225}
226
227RegInterval SIInsertWaits::getRegInterval(MachineOperand &Op) {
228
Tom Stellard81d871d2013-11-13 23:36:50 +0000229 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
Tom Stellardc4cabef2013-01-18 21:15:53 +0000230 return std::make_pair(0, 0);
231
232 unsigned Reg = Op.getReg();
Bill Wendling37e9adb2013-06-07 20:28:55 +0000233 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000234
235 assert(Size >= 4);
236
237 RegInterval Result;
Bill Wendling37e9adb2013-06-07 20:28:55 +0000238 Result.first = TRI->getEncodingValue(Reg);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000239 Result.second = Result.first + Size / 4;
240
241 return Result;
242}
243
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000244void SIInsertWaits::pushInstruction(MachineBasicBlock &MBB,
245 MachineBasicBlock::iterator I) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000246
247 // Get the hardware counter increments and sum them up
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000248 Counters Increment = getHwCounts(*I);
Tom Stellardbd8a0852015-08-21 22:47:27 +0000249 Counters Limit = ZeroCounts;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000250 unsigned Sum = 0;
251
252 for (unsigned i = 0; i < 3; ++i) {
253 LastIssued.Array[i] += Increment.Array[i];
Tom Stellardbd8a0852015-08-21 22:47:27 +0000254 if (Increment.Array[i])
255 Limit.Array[i] = LastIssued.Array[i];
Tom Stellardc4cabef2013-01-18 21:15:53 +0000256 Sum += Increment.Array[i];
257 }
258
259 // If we don't increase anything then that's it
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000260 if (Sum == 0) {
261 LastOpcodeType = OTHER;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000262 return;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000263 }
264
Eric Christopher6c5b5112015-03-11 18:43:21 +0000265 if (MBB.getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() >=
266 AMDGPUSubtarget::VOLCANIC_ISLANDS) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000267 // Any occurrence of consecutive VMEM or SMEM instructions forms a VMEM
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000268 // or SMEM clause, respectively.
269 //
270 // The temporary workaround is to break the clauses with S_NOP.
271 //
272 // The proper solution would be to allocate registers such that all source
273 // and destination registers don't overlap, e.g. this is illegal:
274 // r0 = load r2
275 // r2 = load r0
276 if ((LastOpcodeType == SMEM && TII->isSMRD(I->getOpcode())) ||
277 (LastOpcodeType == VMEM && Increment.Named.VM)) {
278 // Insert a NOP to break the clause.
279 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP))
280 .addImm(0);
Marek Olsak1bd24632015-02-03 17:37:52 +0000281 LastInstWritesM0 = false;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000282 }
283
284 if (TII->isSMRD(I->getOpcode()))
285 LastOpcodeType = SMEM;
286 else if (Increment.Named.VM)
287 LastOpcodeType = VMEM;
288 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000289
290 // Remember which export instructions we have seen
291 if (Increment.Named.EXP) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000292 ExpInstrTypesSeen |= I->getOpcode() == AMDGPU::EXP ? 1 : 2;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000293 }
294
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000295 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000296
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000297 MachineOperand &Op = I->getOperand(i);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000298 if (!isOpRelevant(Op))
299 continue;
300
301 RegInterval Interval = getRegInterval(Op);
302 for (unsigned j = Interval.first; j < Interval.second; ++j) {
303
304 // Remember which registers we define
305 if (Op.isDef())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000306 DefinedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000307
308 // and which one we are using
309 if (Op.isUse())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000310 UsedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000311 }
312 }
313}
314
315bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
316 MachineBasicBlock::iterator I,
317 const Counters &Required) {
318
319 // End of program? No need to wait on anything
320 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM)
321 return false;
322
323 // Figure out if the async instructions execute in order
324 bool Ordered[3];
325
326 // VM_CNT is always ordered
327 Ordered[0] = true;
328
329 // EXP_CNT is unordered if we have both EXP & VM-writes
330 Ordered[1] = ExpInstrTypesSeen == 3;
331
332 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
333 Ordered[2] = false;
334
335 // The values we are going to put into the S_WAITCNT instruction
336 Counters Counts = WaitCounts;
337
338 // Do we really need to wait?
339 bool NeedWait = false;
340
341 for (unsigned i = 0; i < 3; ++i) {
342
343 if (Required.Array[i] <= WaitedOn.Array[i])
344 continue;
345
346 NeedWait = true;
Matt Arsenault97483692014-07-17 17:50:22 +0000347
Tom Stellardc4cabef2013-01-18 21:15:53 +0000348 if (Ordered[i]) {
349 unsigned Value = LastIssued.Array[i] - Required.Array[i];
350
Matt Arsenault97483692014-07-17 17:50:22 +0000351 // Adjust the value to the real hardware possibilities.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000352 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
353
354 } else
355 Counts.Array[i] = 0;
356
Matt Arsenault97483692014-07-17 17:50:22 +0000357 // Remember on what we have waited on.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000358 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
359 }
360
361 if (!NeedWait)
362 return false;
363
364 // Reset EXP_CNT instruction types
365 if (Counts.Named.EXP == 0)
366 ExpInstrTypesSeen = 0;
367
368 // Build the wait instruction
369 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
370 .addImm((Counts.Named.VM & 0xF) |
371 ((Counts.Named.EXP & 0x7) << 4) |
372 ((Counts.Named.LGKM & 0x7) << 8));
373
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000374 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000375 LastInstWritesM0 = false;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000376 return true;
377}
378
379/// \brief helper function for handleOperands
380static void increaseCounters(Counters &Dst, const Counters &Src) {
381
382 for (unsigned i = 0; i < 3; ++i)
383 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
384}
385
386Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
387
388 Counters Result = ZeroCounts;
389
Michel Danzer6064f572014-01-27 07:20:44 +0000390 // S_SENDMSG implicitly waits for all outstanding LGKM transfers to finish,
391 // but we also want to wait for any other outstanding transfers before
392 // signalling other hardware blocks
393 if (MI.getOpcode() == AMDGPU::S_SENDMSG)
394 return LastIssued;
395
Tom Stellardc4cabef2013-01-18 21:15:53 +0000396 // For each register affected by this
397 // instruction increase the result sequence
398 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
399
400 MachineOperand &Op = MI.getOperand(i);
401 RegInterval Interval = getRegInterval(Op);
402 for (unsigned j = Interval.first; j < Interval.second; ++j) {
403
Christian Konig862fd9f2013-03-01 09:46:04 +0000404 if (Op.isDef()) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000405 increaseCounters(Result, UsedRegs[j]);
Christian Konigf1fd5fa2013-03-18 11:33:45 +0000406 increaseCounters(Result, DefinedRegs[j]);
Christian Konig862fd9f2013-03-01 09:46:04 +0000407 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000408
409 if (Op.isUse())
410 increaseCounters(Result, DefinedRegs[j]);
411 }
412 }
413
414 return Result;
415}
416
Marek Olsak1bd24632015-02-03 17:37:52 +0000417void SIInsertWaits::handleSendMsg(MachineBasicBlock &MBB,
418 MachineBasicBlock::iterator I) {
Eric Christopher6c5b5112015-03-11 18:43:21 +0000419 if (MBB.getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() <
420 AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak1bd24632015-02-03 17:37:52 +0000421 return;
422
423 // There must be "S_NOP 0" between an instruction writing M0 and S_SENDMSG.
424 if (LastInstWritesM0 && I->getOpcode() == AMDGPU::S_SENDMSG) {
425 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP)).addImm(0);
426 LastInstWritesM0 = false;
427 return;
428 }
429
430 // Set whether this instruction sets M0
431 LastInstWritesM0 = false;
432
433 unsigned NumOperands = I->getNumOperands();
434 for (unsigned i = 0; i < NumOperands; i++) {
435 const MachineOperand &Op = I->getOperand(i);
436
437 if (Op.isReg() && Op.isDef() && Op.getReg() == AMDGPU::M0)
438 LastInstWritesM0 = true;
439 }
440}
441
Matt Arsenaulta0050b02014-06-19 01:19:19 +0000442// FIXME: Insert waits listed in Table 4.2 "Required User-Inserted Wait States"
443// around other non-memory instructions.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000444bool SIInsertWaits::runOnMachineFunction(MachineFunction &MF) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000445 bool Changes = false;
446
Eric Christopherfc6de422014-08-05 02:39:49 +0000447 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
448 TRI =
449 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Bill Wendling37e9adb2013-06-07 20:28:55 +0000450
Tom Stellardc4cabef2013-01-18 21:15:53 +0000451 MRI = &MF.getRegInfo();
452
453 WaitedOn = ZeroCounts;
454 LastIssued = ZeroCounts;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000455 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000456 LastInstWritesM0 = false;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000457
458 memset(&UsedRegs, 0, sizeof(UsedRegs));
459 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
460
461 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
462 BI != BE; ++BI) {
463
464 MachineBasicBlock &MBB = *BI;
465 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
466 I != E; ++I) {
467
Tom Stellard9d6797a2015-01-06 19:52:07 +0000468 // Wait for everything before a barrier.
469 if (I->getOpcode() == AMDGPU::S_BARRIER)
470 Changes |= insertWait(MBB, I, LastIssued);
471 else
472 Changes |= insertWait(MBB, I, handleOperands(*I));
Marek Olsak1bd24632015-02-03 17:37:52 +0000473
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000474 pushInstruction(MBB, I);
Marek Olsak1bd24632015-02-03 17:37:52 +0000475 handleSendMsg(MBB, I);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000476 }
477
478 // Wait for everything at the end of the MBB
479 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
480 }
481
482 return Changes;
483}