blob: 712d97d33e90246150ffafde48b94e6ff5cdcf2a [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
44typedef Counters RegCounters[512];
45typedef std::pair<unsigned, unsigned> RegInterval;
46
47class SIInsertWaits : public MachineFunctionPass {
48
49private:
50 static char ID;
51 const SIInstrInfo *TII;
Bill Wendling37e9adb2013-06-07 20:28:55 +000052 const SIRegisterInfo *TRI;
Tom Stellardc4cabef2013-01-18 21:15:53 +000053 const MachineRegisterInfo *MRI;
54
55 /// \brief Constant hardware limits
56 static const Counters WaitCounts;
57
58 /// \brief Constant zero value
59 static const Counters ZeroCounts;
60
61 /// \brief Counter values we have already waited on.
62 Counters WaitedOn;
63
64 /// \brief Counter values for last instruction issued.
65 Counters LastIssued;
66
67 /// \brief Registers used by async instructions.
68 RegCounters UsedRegs;
69
70 /// \brief Registers defined by async instructions.
71 RegCounters DefinedRegs;
72
73 /// \brief Different export instruction types seen since last wait.
74 unsigned ExpInstrTypesSeen;
75
76 /// \brief Get increment/decrement amount for this instruction.
77 Counters getHwCounts(MachineInstr &MI);
78
79 /// \brief Is operand relevant for async execution?
80 bool isOpRelevant(MachineOperand &Op);
81
82 /// \brief Get register interval an operand affects.
83 RegInterval getRegInterval(MachineOperand &Op);
84
85 /// \brief Handle instructions async components
86 void pushInstruction(MachineInstr &MI);
87
88 /// \brief Insert the actual wait instruction
89 bool insertWait(MachineBasicBlock &MBB,
90 MachineBasicBlock::iterator I,
91 const Counters &Counts);
92
Christian Konig862fd9f2013-03-01 09:46:04 +000093 /// \brief Do we need def2def checks?
94 bool unorderedDefines(MachineInstr &MI);
95
Tom Stellardc4cabef2013-01-18 21:15:53 +000096 /// \brief Resolve all operand dependencies to counter requirements
97 Counters handleOperands(MachineInstr &MI);
98
99public:
100 SIInsertWaits(TargetMachine &tm) :
101 MachineFunctionPass(ID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000102 TII(nullptr),
103 TRI(nullptr),
Evgeniy Stepanovbc8808c2013-08-07 07:47:41 +0000104 ExpInstrTypesSeen(0) { }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000105
Craig Topper5656db42014-04-29 07:57:24 +0000106 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000107
Craig Topper5656db42014-04-29 07:57:24 +0000108 const char *getPassName() const override {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000109 return "SI insert wait instructions";
110 }
111
112};
113
114} // End anonymous namespace
115
116char SIInsertWaits::ID = 0;
117
118const Counters SIInsertWaits::WaitCounts = { { 15, 7, 7 } };
119const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
120
121FunctionPass *llvm::createSIInsertWaits(TargetMachine &tm) {
122 return new SIInsertWaits(tm);
123}
124
125Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
126
127 uint64_t TSFlags = TII->get(MI.getOpcode()).TSFlags;
128 Counters Result;
129
130 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
131
132 // Only consider stores or EXP for EXP_CNT
133 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
Christian Konig862fd9f2013-03-01 09:46:04 +0000134 (MI.getOpcode() == AMDGPU::EXP || MI.getDesc().mayStore()));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000135
136 // LGKM may uses larger values
137 if (TSFlags & SIInstrFlags::LGKM_CNT) {
138
Michel Danzer20680b12013-08-16 16:19:24 +0000139 if (TII->isSMRD(MI.getOpcode())) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000140
Michel Danzer20680b12013-08-16 16:19:24 +0000141 MachineOperand &Op = MI.getOperand(0);
142 assert(Op.isReg() && "First LGKM operand must be a register!");
143
144 unsigned Reg = Op.getReg();
145 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
146 Result.Named.LGKM = Size > 4 ? 2 : 1;
147
148 } else {
149 // DS
150 Result.Named.LGKM = 1;
151 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000152
153 } else {
154 Result.Named.LGKM = 0;
155 }
156
157 return Result;
158}
159
160bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
161
162 // Constants are always irrelevant
163 if (!Op.isReg())
164 return false;
165
166 // Defines are always relevant
167 if (Op.isDef())
168 return true;
169
170 // For exports all registers are relevant
171 MachineInstr &MI = *Op.getParent();
172 if (MI.getOpcode() == AMDGPU::EXP)
173 return true;
174
175 // For stores the stored value is also relevant
176 if (!MI.getDesc().mayStore())
177 return false;
178
179 for (MachineInstr::mop_iterator I = MI.operands_begin(),
180 E = MI.operands_end(); I != E; ++I) {
181
182 if (I->isReg() && I->isUse())
183 return Op.isIdenticalTo(*I);
184 }
185
186 return false;
187}
188
189RegInterval SIInsertWaits::getRegInterval(MachineOperand &Op) {
190
Tom Stellard81d871d2013-11-13 23:36:50 +0000191 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
Tom Stellardc4cabef2013-01-18 21:15:53 +0000192 return std::make_pair(0, 0);
193
194 unsigned Reg = Op.getReg();
Bill Wendling37e9adb2013-06-07 20:28:55 +0000195 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000196
197 assert(Size >= 4);
198
199 RegInterval Result;
Bill Wendling37e9adb2013-06-07 20:28:55 +0000200 Result.first = TRI->getEncodingValue(Reg);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000201 Result.second = Result.first + Size / 4;
202
203 return Result;
204}
205
206void SIInsertWaits::pushInstruction(MachineInstr &MI) {
207
208 // Get the hardware counter increments and sum them up
209 Counters Increment = getHwCounts(MI);
210 unsigned Sum = 0;
211
212 for (unsigned i = 0; i < 3; ++i) {
213 LastIssued.Array[i] += Increment.Array[i];
214 Sum += Increment.Array[i];
215 }
216
217 // If we don't increase anything then that's it
218 if (Sum == 0)
219 return;
220
221 // Remember which export instructions we have seen
222 if (Increment.Named.EXP) {
223 ExpInstrTypesSeen |= MI.getOpcode() == AMDGPU::EXP ? 1 : 2;
224 }
225
226 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
227
228 MachineOperand &Op = MI.getOperand(i);
229 if (!isOpRelevant(Op))
230 continue;
231
232 RegInterval Interval = getRegInterval(Op);
233 for (unsigned j = Interval.first; j < Interval.second; ++j) {
234
235 // Remember which registers we define
236 if (Op.isDef())
237 DefinedRegs[j] = LastIssued;
238
239 // and which one we are using
240 if (Op.isUse())
241 UsedRegs[j] = LastIssued;
242 }
243 }
244}
245
246bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
247 MachineBasicBlock::iterator I,
248 const Counters &Required) {
249
250 // End of program? No need to wait on anything
251 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM)
252 return false;
253
254 // Figure out if the async instructions execute in order
255 bool Ordered[3];
256
257 // VM_CNT is always ordered
258 Ordered[0] = true;
259
260 // EXP_CNT is unordered if we have both EXP & VM-writes
261 Ordered[1] = ExpInstrTypesSeen == 3;
262
263 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
264 Ordered[2] = false;
265
266 // The values we are going to put into the S_WAITCNT instruction
267 Counters Counts = WaitCounts;
268
269 // Do we really need to wait?
270 bool NeedWait = false;
271
272 for (unsigned i = 0; i < 3; ++i) {
273
274 if (Required.Array[i] <= WaitedOn.Array[i])
275 continue;
276
277 NeedWait = true;
Matt Arsenault97483692014-07-17 17:50:22 +0000278
Tom Stellardc4cabef2013-01-18 21:15:53 +0000279 if (Ordered[i]) {
280 unsigned Value = LastIssued.Array[i] - Required.Array[i];
281
Matt Arsenault97483692014-07-17 17:50:22 +0000282 // Adjust the value to the real hardware possibilities.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000283 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
284
285 } else
286 Counts.Array[i] = 0;
287
Matt Arsenault97483692014-07-17 17:50:22 +0000288 // Remember on what we have waited on.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000289 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
290 }
291
292 if (!NeedWait)
293 return false;
294
295 // Reset EXP_CNT instruction types
296 if (Counts.Named.EXP == 0)
297 ExpInstrTypesSeen = 0;
298
299 // Build the wait instruction
300 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
301 .addImm((Counts.Named.VM & 0xF) |
302 ((Counts.Named.EXP & 0x7) << 4) |
303 ((Counts.Named.LGKM & 0x7) << 8));
304
305 return true;
306}
307
308/// \brief helper function for handleOperands
309static void increaseCounters(Counters &Dst, const Counters &Src) {
310
311 for (unsigned i = 0; i < 3; ++i)
312 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
313}
314
315Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
316
317 Counters Result = ZeroCounts;
318
Michel Danzer6064f572014-01-27 07:20:44 +0000319 // S_SENDMSG implicitly waits for all outstanding LGKM transfers to finish,
320 // but we also want to wait for any other outstanding transfers before
321 // signalling other hardware blocks
322 if (MI.getOpcode() == AMDGPU::S_SENDMSG)
323 return LastIssued;
324
Tom Stellardc4cabef2013-01-18 21:15:53 +0000325 // For each register affected by this
326 // instruction increase the result sequence
327 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
328
329 MachineOperand &Op = MI.getOperand(i);
330 RegInterval Interval = getRegInterval(Op);
331 for (unsigned j = Interval.first; j < Interval.second; ++j) {
332
Christian Konig862fd9f2013-03-01 09:46:04 +0000333 if (Op.isDef()) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000334 increaseCounters(Result, UsedRegs[j]);
Christian Konigf1fd5fa2013-03-18 11:33:45 +0000335 increaseCounters(Result, DefinedRegs[j]);
Christian Konig862fd9f2013-03-01 09:46:04 +0000336 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000337
338 if (Op.isUse())
339 increaseCounters(Result, DefinedRegs[j]);
340 }
341 }
342
343 return Result;
344}
345
Matt Arsenaulta0050b02014-06-19 01:19:19 +0000346// FIXME: Insert waits listed in Table 4.2 "Required User-Inserted Wait States"
347// around other non-memory instructions.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000348bool SIInsertWaits::runOnMachineFunction(MachineFunction &MF) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000349 bool Changes = false;
350
Eric Christopherfc6de422014-08-05 02:39:49 +0000351 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
352 TRI =
353 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Bill Wendling37e9adb2013-06-07 20:28:55 +0000354
Tom Stellardc4cabef2013-01-18 21:15:53 +0000355 MRI = &MF.getRegInfo();
356
357 WaitedOn = ZeroCounts;
358 LastIssued = ZeroCounts;
359
360 memset(&UsedRegs, 0, sizeof(UsedRegs));
361 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
362
363 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
364 BI != BE; ++BI) {
365
366 MachineBasicBlock &MBB = *BI;
367 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
368 I != E; ++I) {
369
370 Changes |= insertWait(MBB, I, handleOperands(*I));
371 pushInstruction(*I);
372 }
373
374 // Wait for everything at the end of the MBB
375 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
376 }
377
378 return Changes;
379}