blob: 94e614750d2fd6d6c476942825d7c27dfacbb4a5 [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
Marek Olsak8e9cc632016-01-13 17:23:09 +000087 /// \brief Whether the machine function returns void
88 bool ReturnsVoid;
89
Tom Stellardc4cabef2013-01-18 21:15:53 +000090 /// \brief Get increment/decrement amount for this instruction.
91 Counters getHwCounts(MachineInstr &MI);
92
93 /// \brief Is operand relevant for async execution?
94 bool isOpRelevant(MachineOperand &Op);
95
96 /// \brief Get register interval an operand affects.
Matt Arsenaultd1d499a2015-10-01 21:43:15 +000097 RegInterval getRegInterval(const TargetRegisterClass *RC,
98 const MachineOperand &Reg) const;
Tom Stellardc4cabef2013-01-18 21:15:53 +000099
100 /// \brief Handle instructions async components
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000101 void pushInstruction(MachineBasicBlock &MBB,
102 MachineBasicBlock::iterator I);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000103
104 /// \brief Insert the actual wait instruction
105 bool insertWait(MachineBasicBlock &MBB,
106 MachineBasicBlock::iterator I,
107 const Counters &Counts);
108
Christian Konig862fd9f2013-03-01 09:46:04 +0000109 /// \brief Do we need def2def checks?
110 bool unorderedDefines(MachineInstr &MI);
111
Tom Stellardc4cabef2013-01-18 21:15:53 +0000112 /// \brief Resolve all operand dependencies to counter requirements
113 Counters handleOperands(MachineInstr &MI);
114
Marek Olsak1bd24632015-02-03 17:37:52 +0000115 /// \brief Insert S_NOP between an instruction writing M0 and S_SENDMSG.
116 void handleSendMsg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I);
117
Tom Stellardc4cabef2013-01-18 21:15:53 +0000118public:
119 SIInsertWaits(TargetMachine &tm) :
120 MachineFunctionPass(ID),
Craig Topper062a2ba2014-04-25 05:30:21 +0000121 TII(nullptr),
122 TRI(nullptr),
Evgeniy Stepanovbc8808c2013-08-07 07:47:41 +0000123 ExpInstrTypesSeen(0) { }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000124
Craig Topper5656db42014-04-29 07:57:24 +0000125 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000126
Craig Topper5656db42014-04-29 07:57:24 +0000127 const char *getPassName() const override {
Matt Arsenault0cb85172015-09-25 17:21:28 +0000128 return "SI insert wait instructions";
Tom Stellardc4cabef2013-01-18 21:15:53 +0000129 }
130
Matt Arsenault0cb85172015-09-25 17:21:28 +0000131 void getAnalysisUsage(AnalysisUsage &AU) const override {
132 AU.setPreservesCFG();
133 MachineFunctionPass::getAnalysisUsage(AU);
134 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000135};
136
137} // End anonymous namespace
138
139char SIInsertWaits::ID = 0;
140
141const Counters SIInsertWaits::WaitCounts = { { 15, 7, 7 } };
142const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
143
144FunctionPass *llvm::createSIInsertWaits(TargetMachine &tm) {
145 return new SIInsertWaits(tm);
146}
147
148Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000149 uint64_t TSFlags = MI.getDesc().TSFlags;
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000150 Counters Result = { { 0, 0, 0 } };
Tom Stellardc4cabef2013-01-18 21:15:53 +0000151
152 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
153
154 // Only consider stores or EXP for EXP_CNT
155 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
Christian Konig862fd9f2013-03-01 09:46:04 +0000156 (MI.getOpcode() == AMDGPU::EXP || MI.getDesc().mayStore()));
Tom Stellardc4cabef2013-01-18 21:15:53 +0000157
158 // LGKM may uses larger values
159 if (TSFlags & SIInstrFlags::LGKM_CNT) {
160
Matt Arsenault3add6432015-10-20 04:35:43 +0000161 if (TII->isSMRD(MI)) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000162
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000163 if (MI.getNumOperands() != 0) {
Matt Arsenaultb733f002015-10-01 22:40:35 +0000164 assert(MI.getOperand(0).isReg() &&
165 "First LGKM operand must be a register!");
Michel Danzer20680b12013-08-16 16:19:24 +0000166
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000167 // XXX - What if this is a write into a super register?
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000168 const TargetRegisterClass *RC = TII->getOpRegClass(MI, 0);
169 unsigned Size = RC->getSize();
Matt Arsenaulte66621b2015-09-24 19:52:27 +0000170 Result.Named.LGKM = Size > 4 ? 2 : 1;
171 } else {
172 // s_dcache_inv etc. do not have a a destination register. Assume we
173 // want a wait on these.
174 // XXX - What is the right value?
175 Result.Named.LGKM = 1;
176 }
Michel Danzer20680b12013-08-16 16:19:24 +0000177 } else {
178 // DS
179 Result.Named.LGKM = 1;
180 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000181
182 } else {
183 Result.Named.LGKM = 0;
184 }
185
186 return Result;
187}
188
189bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000190 // Constants are always irrelevant
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000191 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
Tom Stellardc4cabef2013-01-18 21:15:53 +0000192 return false;
193
194 // Defines are always relevant
195 if (Op.isDef())
196 return true;
197
198 // For exports all registers are relevant
199 MachineInstr &MI = *Op.getParent();
200 if (MI.getOpcode() == AMDGPU::EXP)
201 return true;
202
203 // For stores the stored value is also relevant
204 if (!MI.getDesc().mayStore())
205 return false;
206
Tom Stellardb3931b82015-01-06 19:52:04 +0000207 // Check if this operand is the value being stored.
208 // Special case for DS instructions, since the address
209 // operand comes before the value operand and it may have
210 // multiple data operands.
211
Matt Arsenault3add6432015-10-20 04:35:43 +0000212 if (TII->isDS(MI)) {
Tom Stellardb3931b82015-01-06 19:52:04 +0000213 MachineOperand *Data = TII->getNamedOperand(MI, AMDGPU::OpName::data);
214 if (Data && Op.isIdenticalTo(*Data))
215 return true;
216
217 MachineOperand *Data0 = TII->getNamedOperand(MI, AMDGPU::OpName::data0);
218 if (Data0 && Op.isIdenticalTo(*Data0))
219 return true;
220
221 MachineOperand *Data1 = TII->getNamedOperand(MI, AMDGPU::OpName::data1);
222 if (Data1 && Op.isIdenticalTo(*Data1))
223 return true;
224
225 return false;
226 }
227
228 // NOTE: This assumes that the value operand is before the
229 // address operand, and that there is only one value operand.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000230 for (MachineInstr::mop_iterator I = MI.operands_begin(),
231 E = MI.operands_end(); I != E; ++I) {
232
233 if (I->isReg() && I->isUse())
234 return Op.isIdenticalTo(*I);
235 }
236
237 return false;
238}
239
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000240RegInterval SIInsertWaits::getRegInterval(const TargetRegisterClass *RC,
241 const MachineOperand &Reg) const {
242 unsigned Size = RC->getSize();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000243 assert(Size >= 4);
244
245 RegInterval Result;
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000246 Result.first = TRI->getEncodingValue(Reg.getReg());
Tom Stellardc4cabef2013-01-18 21:15:53 +0000247 Result.second = Result.first + Size / 4;
248
249 return Result;
250}
251
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000252void SIInsertWaits::pushInstruction(MachineBasicBlock &MBB,
253 MachineBasicBlock::iterator I) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000254
255 // Get the hardware counter increments and sum them up
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000256 Counters Increment = getHwCounts(*I);
Tom Stellardbd8a0852015-08-21 22:47:27 +0000257 Counters Limit = ZeroCounts;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000258 unsigned Sum = 0;
259
260 for (unsigned i = 0; i < 3; ++i) {
261 LastIssued.Array[i] += Increment.Array[i];
Tom Stellardbd8a0852015-08-21 22:47:27 +0000262 if (Increment.Array[i])
263 Limit.Array[i] = LastIssued.Array[i];
Tom Stellardc4cabef2013-01-18 21:15:53 +0000264 Sum += Increment.Array[i];
265 }
266
267 // If we don't increase anything then that's it
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000268 if (Sum == 0) {
269 LastOpcodeType = OTHER;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000270 return;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000271 }
272
Eric Christopher6c5b5112015-03-11 18:43:21 +0000273 if (MBB.getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() >=
274 AMDGPUSubtarget::VOLCANIC_ISLANDS) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000275 // Any occurrence of consecutive VMEM or SMEM instructions forms a VMEM
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000276 // or SMEM clause, respectively.
277 //
278 // The temporary workaround is to break the clauses with S_NOP.
279 //
280 // The proper solution would be to allocate registers such that all source
281 // and destination registers don't overlap, e.g. this is illegal:
282 // r0 = load r2
283 // r2 = load r0
Matt Arsenault3add6432015-10-20 04:35:43 +0000284 if ((LastOpcodeType == SMEM && TII->isSMRD(*I)) ||
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000285 (LastOpcodeType == VMEM && Increment.Named.VM)) {
286 // Insert a NOP to break the clause.
287 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP))
288 .addImm(0);
Marek Olsak1bd24632015-02-03 17:37:52 +0000289 LastInstWritesM0 = false;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000290 }
291
Matt Arsenault3add6432015-10-20 04:35:43 +0000292 if (TII->isSMRD(*I))
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000293 LastOpcodeType = SMEM;
294 else if (Increment.Named.VM)
295 LastOpcodeType = VMEM;
296 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000297
298 // Remember which export instructions we have seen
299 if (Increment.Named.EXP) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000300 ExpInstrTypesSeen |= I->getOpcode() == AMDGPU::EXP ? 1 : 2;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000301 }
302
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000303 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000304 MachineOperand &Op = I->getOperand(i);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000305 if (!isOpRelevant(Op))
306 continue;
307
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000308 const TargetRegisterClass *RC = TII->getOpRegClass(*I, i);
309 RegInterval Interval = getRegInterval(RC, Op);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000310 for (unsigned j = Interval.first; j < Interval.second; ++j) {
311
312 // Remember which registers we define
313 if (Op.isDef())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000314 DefinedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000315
316 // and which one we are using
317 if (Op.isUse())
Tom Stellardbd8a0852015-08-21 22:47:27 +0000318 UsedRegs[j] = Limit;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000319 }
320 }
321}
322
323bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
324 MachineBasicBlock::iterator I,
325 const Counters &Required) {
326
327 // End of program? No need to wait on anything
Marek Olsak8e9cc632016-01-13 17:23:09 +0000328 // A function not returning void needs to wait, because other bytecode will
329 // be appended after it and we don't know what it will be.
330 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM && ReturnsVoid)
Tom Stellardc4cabef2013-01-18 21:15:53 +0000331 return false;
332
333 // Figure out if the async instructions execute in order
334 bool Ordered[3];
335
336 // VM_CNT is always ordered
337 Ordered[0] = true;
338
339 // EXP_CNT is unordered if we have both EXP & VM-writes
340 Ordered[1] = ExpInstrTypesSeen == 3;
341
342 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
343 Ordered[2] = false;
344
345 // The values we are going to put into the S_WAITCNT instruction
346 Counters Counts = WaitCounts;
347
348 // Do we really need to wait?
349 bool NeedWait = false;
350
351 for (unsigned i = 0; i < 3; ++i) {
352
353 if (Required.Array[i] <= WaitedOn.Array[i])
354 continue;
355
356 NeedWait = true;
Matt Arsenault97483692014-07-17 17:50:22 +0000357
Tom Stellardc4cabef2013-01-18 21:15:53 +0000358 if (Ordered[i]) {
359 unsigned Value = LastIssued.Array[i] - Required.Array[i];
360
Matt Arsenault97483692014-07-17 17:50:22 +0000361 // Adjust the value to the real hardware possibilities.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000362 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
363
364 } else
365 Counts.Array[i] = 0;
366
Matt Arsenault97483692014-07-17 17:50:22 +0000367 // Remember on what we have waited on.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000368 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
369 }
370
371 if (!NeedWait)
372 return false;
373
374 // Reset EXP_CNT instruction types
375 if (Counts.Named.EXP == 0)
376 ExpInstrTypesSeen = 0;
377
378 // Build the wait instruction
379 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
380 .addImm((Counts.Named.VM & 0xF) |
381 ((Counts.Named.EXP & 0x7) << 4) |
382 ((Counts.Named.LGKM & 0x7) << 8));
383
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000384 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000385 LastInstWritesM0 = false;
Tom Stellardc4cabef2013-01-18 21:15:53 +0000386 return true;
387}
388
389/// \brief helper function for handleOperands
390static void increaseCounters(Counters &Dst, const Counters &Src) {
391
392 for (unsigned i = 0; i < 3; ++i)
393 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
394}
395
396Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
397
398 Counters Result = ZeroCounts;
399
Michel Danzer6064f572014-01-27 07:20:44 +0000400 // S_SENDMSG implicitly waits for all outstanding LGKM transfers to finish,
401 // but we also want to wait for any other outstanding transfers before
402 // signalling other hardware blocks
403 if (MI.getOpcode() == AMDGPU::S_SENDMSG)
404 return LastIssued;
405
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000406 // For each register affected by this instruction increase the result
407 // sequence.
408 //
409 // TODO: We could probably just look at explicit operands if we removed VCC /
410 // EXEC from SMRD dest reg classes.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000411 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000412 MachineOperand &Op = MI.getOperand(i);
Matt Arsenaultd1d499a2015-10-01 21:43:15 +0000413 if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()))
414 continue;
415
416 const TargetRegisterClass *RC = TII->getOpRegClass(MI, i);
417 RegInterval Interval = getRegInterval(RC, Op);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000418 for (unsigned j = Interval.first; j < Interval.second; ++j) {
419
Christian Konig862fd9f2013-03-01 09:46:04 +0000420 if (Op.isDef()) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000421 increaseCounters(Result, UsedRegs[j]);
Christian Konigf1fd5fa2013-03-18 11:33:45 +0000422 increaseCounters(Result, DefinedRegs[j]);
Christian Konig862fd9f2013-03-01 09:46:04 +0000423 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000424
425 if (Op.isUse())
426 increaseCounters(Result, DefinedRegs[j]);
427 }
428 }
429
430 return Result;
431}
432
Marek Olsak1bd24632015-02-03 17:37:52 +0000433void SIInsertWaits::handleSendMsg(MachineBasicBlock &MBB,
434 MachineBasicBlock::iterator I) {
Eric Christopher6c5b5112015-03-11 18:43:21 +0000435 if (MBB.getParent()->getSubtarget<AMDGPUSubtarget>().getGeneration() <
436 AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak1bd24632015-02-03 17:37:52 +0000437 return;
438
439 // There must be "S_NOP 0" between an instruction writing M0 and S_SENDMSG.
440 if (LastInstWritesM0 && I->getOpcode() == AMDGPU::S_SENDMSG) {
441 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_NOP)).addImm(0);
442 LastInstWritesM0 = false;
443 return;
444 }
445
446 // Set whether this instruction sets M0
447 LastInstWritesM0 = false;
448
449 unsigned NumOperands = I->getNumOperands();
450 for (unsigned i = 0; i < NumOperands; i++) {
451 const MachineOperand &Op = I->getOperand(i);
452
453 if (Op.isReg() && Op.isDef() && Op.getReg() == AMDGPU::M0)
454 LastInstWritesM0 = true;
455 }
456}
457
Matt Arsenaulta0050b02014-06-19 01:19:19 +0000458// FIXME: Insert waits listed in Table 4.2 "Required User-Inserted Wait States"
459// around other non-memory instructions.
Tom Stellardc4cabef2013-01-18 21:15:53 +0000460bool SIInsertWaits::runOnMachineFunction(MachineFunction &MF) {
Tom Stellardc4cabef2013-01-18 21:15:53 +0000461 bool Changes = false;
462
Eric Christopherfc6de422014-08-05 02:39:49 +0000463 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
464 TRI =
465 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Bill Wendling37e9adb2013-06-07 20:28:55 +0000466
Tom Stellardc4cabef2013-01-18 21:15:53 +0000467 MRI = &MF.getRegInfo();
468
469 WaitedOn = ZeroCounts;
470 LastIssued = ZeroCounts;
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000471 LastOpcodeType = OTHER;
Marek Olsak1bd24632015-02-03 17:37:52 +0000472 LastInstWritesM0 = false;
Marek Olsak8e9cc632016-01-13 17:23:09 +0000473 ReturnsVoid = MF.getInfo<SIMachineFunctionInfo>()->returnsVoid();
Tom Stellardc4cabef2013-01-18 21:15:53 +0000474
475 memset(&UsedRegs, 0, sizeof(UsedRegs));
476 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
477
478 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
479 BI != BE; ++BI) {
480
481 MachineBasicBlock &MBB = *BI;
482 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
483 I != E; ++I) {
484
Tom Stellard9d6797a2015-01-06 19:52:07 +0000485 // Wait for everything before a barrier.
486 if (I->getOpcode() == AMDGPU::S_BARRIER)
487 Changes |= insertWait(MBB, I, LastIssued);
488 else
489 Changes |= insertWait(MBB, I, handleOperands(*I));
Marek Olsak1bd24632015-02-03 17:37:52 +0000490
Marek Olsakfa58e5e2014-12-07 17:17:43 +0000491 pushInstruction(MBB, I);
Marek Olsak1bd24632015-02-03 17:37:52 +0000492 handleSendMsg(MBB, I);
Tom Stellardc4cabef2013-01-18 21:15:53 +0000493 }
494
495 // Wait for everything at the end of the MBB
496 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
Marek Olsak3c0ebc72016-01-13 17:23:12 +0000497
498 // Functions returning something shouldn't contain S_ENDPGM, because other
499 // bytecode will be appended after it.
500 if (!ReturnsVoid) {
501 MachineBasicBlock::iterator I = MBB.getFirstTerminator();
502 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM)
503 I->eraseFromParent();
504 }
Tom Stellardc4cabef2013-01-18 21:15:53 +0000505 }
506
507 return Changes;
508}