blob: a6c43bbb2c5e0b7af81ebe380c246a24d2ab7f98 [file] [log] [blame]
Tom Stellardf98f2ce2012-12-11 21:25:42 +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
Tom Stellard6b7d99d2012-12-19 22:10:31 +000011/// \brief This pass lowers the pseudo control flow instructions to real
12/// machine instructions.
Tom Stellardf98f2ce2012-12-11 21:25:42 +000013///
Tom Stellard6b7d99d2012-12-19 22:10:31 +000014/// All control flow is handled using predicated instructions and
Tom Stellardf98f2ce2012-12-11 21:25:42 +000015/// a predicate stack. Each Scalar ALU controls the operations of 64 Vector
16/// ALUs. The Scalar ALU can update the predicate for any of the Vector ALUs
17/// by writting to the 64-bit EXEC register (each bit corresponds to a
18/// single vector ALU). Typically, for predicates, a vector ALU will write
19/// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each
20/// Vector ALU) and then the ScalarALU will AND the VCC register with the
21/// EXEC to update the predicates.
22///
23/// For example:
24/// %VCC = V_CMP_GT_F32 %VGPR1, %VGPR2
Tom Stellard6b7d99d2012-12-19 22:10:31 +000025/// %SGPR0 = SI_IF %VCC
Tom Stellardf98f2ce2012-12-11 21:25:42 +000026/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
Tom Stellard6b7d99d2012-12-19 22:10:31 +000027/// %SGPR0 = SI_ELSE %SGPR0
Tom Stellardf98f2ce2012-12-11 21:25:42 +000028/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
Tom Stellard6b7d99d2012-12-19 22:10:31 +000029/// SI_END_CF %SGPR0
Tom Stellardf98f2ce2012-12-11 21:25:42 +000030///
31/// becomes:
32///
33/// %SGPR0 = S_AND_SAVEEXEC_B64 %VCC // Save and update the exec mask
34/// %SGPR0 = S_XOR_B64 %SGPR0, %EXEC // Clear live bits from saved exec mask
Tom Stellard6b7d99d2012-12-19 22:10:31 +000035/// S_CBRANCH_EXECZ label0 // This instruction is an optional
Tom Stellardf98f2ce2012-12-11 21:25:42 +000036/// // optimization which allows us to
37/// // branch if all the bits of
38/// // EXEC are zero.
39/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0 // Do the IF block of the branch
40///
41/// label0:
42/// %SGPR0 = S_OR_SAVEEXEC_B64 %EXEC // Restore the exec mask for the Then block
43/// %EXEC = S_XOR_B64 %SGPR0, %EXEC // Clear live bits from saved exec mask
44/// S_BRANCH_EXECZ label1 // Use our branch optimization
45/// // instruction again.
46/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR // Do the THEN block
47/// label1:
Tom Stellard6b7d99d2012-12-19 22:10:31 +000048/// %EXEC = S_OR_B64 %EXEC, %SGPR0 // Re-enable saved exec mask bits
Tom Stellardf98f2ce2012-12-11 21:25:42 +000049//===----------------------------------------------------------------------===//
50
51#include "AMDGPU.h"
52#include "SIInstrInfo.h"
53#include "SIMachineFunctionInfo.h"
54#include "llvm/CodeGen/MachineFunction.h"
55#include "llvm/CodeGen/MachineFunctionPass.h"
56#include "llvm/CodeGen/MachineInstrBuilder.h"
57#include "llvm/CodeGen/MachineRegisterInfo.h"
58
59using namespace llvm;
60
61namespace {
62
63class SILowerControlFlowPass : public MachineFunctionPass {
64
65private:
Tom Stellardd09d43a2012-12-19 22:10:33 +000066 static const unsigned SkipThreshold = 12;
67
Tom Stellardf98f2ce2012-12-11 21:25:42 +000068 static char ID;
Christian Konigb9e86782013-03-18 11:34:16 +000069 const TargetRegisterInfo *TRI;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000070 const TargetInstrInfo *TII;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000071
Tom Stellard935a9152013-01-18 21:15:50 +000072 bool shouldSkip(MachineBasicBlock *From, MachineBasicBlock *To);
73
74 void Skip(MachineInstr &From, MachineOperand &To);
75 void SkipIfDead(MachineInstr &MI);
Tom Stellardd09d43a2012-12-19 22:10:33 +000076
Tom Stellard6b7d99d2012-12-19 22:10:31 +000077 void If(MachineInstr &MI);
78 void Else(MachineInstr &MI);
79 void Break(MachineInstr &MI);
80 void IfBreak(MachineInstr &MI);
81 void ElseBreak(MachineInstr &MI);
82 void Loop(MachineInstr &MI);
83 void EndCf(MachineInstr &MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000084
Tom Stellard935a9152013-01-18 21:15:50 +000085 void Kill(MachineInstr &MI);
Tom Stellardd09d43a2012-12-19 22:10:33 +000086 void Branch(MachineInstr &MI);
87
Christian Konigb9e86782013-03-18 11:34:16 +000088 void LoadM0(MachineInstr &MI, MachineInstr *MovRel);
89 void IndirectSrc(MachineInstr &MI);
90 void IndirectDst(MachineInstr &MI);
91
Tom Stellardf98f2ce2012-12-11 21:25:42 +000092public:
93 SILowerControlFlowPass(TargetMachine &tm) :
Bill Wendlingb5632b52013-06-07 20:28:55 +000094 MachineFunctionPass(ID), TRI(0), TII(0) { }
Tom Stellardf98f2ce2012-12-11 21:25:42 +000095
96 virtual bool runOnMachineFunction(MachineFunction &MF);
97
98 const char *getPassName() const {
99 return "SI Lower control flow instructions";
100 }
101
102};
103
104} // End anonymous namespace
105
106char SILowerControlFlowPass::ID = 0;
107
108FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
109 return new SILowerControlFlowPass(tm);
110}
111
Tom Stellard935a9152013-01-18 21:15:50 +0000112bool SILowerControlFlowPass::shouldSkip(MachineBasicBlock *From,
113 MachineBasicBlock *To) {
114
Tom Stellardd09d43a2012-12-19 22:10:33 +0000115 unsigned NumInstr = 0;
116
Tom Stellard935a9152013-01-18 21:15:50 +0000117 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellardd09d43a2012-12-19 22:10:33 +0000118 MBB = *MBB->succ_begin()) {
119
120 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
121 NumInstr < SkipThreshold && I != E; ++I) {
122
123 if (I->isBundle() || !I->isBundled())
Tom Stellard935a9152013-01-18 21:15:50 +0000124 if (++NumInstr >= SkipThreshold)
125 return true;
Tom Stellardd09d43a2012-12-19 22:10:33 +0000126 }
127 }
128
Tom Stellard935a9152013-01-18 21:15:50 +0000129 return false;
130}
131
132void SILowerControlFlowPass::Skip(MachineInstr &From, MachineOperand &To) {
133
134 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellardd09d43a2012-12-19 22:10:33 +0000135 return;
136
137 DebugLoc DL = From.getDebugLoc();
138 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
139 .addOperand(To)
140 .addReg(AMDGPU::EXEC);
141}
142
Tom Stellard935a9152013-01-18 21:15:50 +0000143void SILowerControlFlowPass::SkipIfDead(MachineInstr &MI) {
144
145 MachineBasicBlock &MBB = *MI.getParent();
146 DebugLoc DL = MI.getDebugLoc();
147
148 if (!shouldSkip(&MBB, &MBB.getParent()->back()))
149 return;
150
151 MachineBasicBlock::iterator Insert = &MI;
152 ++Insert;
153
154 // If the exec mask is non-zero, skip the next two instructions
155 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
156 .addImm(3)
157 .addReg(AMDGPU::EXEC);
158
159 // Exec mask is zero: Export to NULL target...
160 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
161 .addImm(0)
162 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
163 .addImm(0)
164 .addImm(1)
165 .addImm(1)
Christian Konige25e4902013-02-16 11:28:22 +0000166 .addReg(AMDGPU::VGPR0)
167 .addReg(AMDGPU::VGPR0)
168 .addReg(AMDGPU::VGPR0)
169 .addReg(AMDGPU::VGPR0);
Tom Stellard935a9152013-01-18 21:15:50 +0000170
171 // ... and terminate wavefront
172 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
173}
174
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000175void SILowerControlFlowPass::If(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000176 MachineBasicBlock &MBB = *MI.getParent();
177 DebugLoc DL = MI.getDebugLoc();
178 unsigned Reg = MI.getOperand(0).getReg();
179 unsigned Vcc = MI.getOperand(1).getReg();
180
181 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
182 .addReg(Vcc);
183
184 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
185 .addReg(AMDGPU::EXEC)
186 .addReg(Reg);
187
Tom Stellardd09d43a2012-12-19 22:10:33 +0000188 Skip(MI, MI.getOperand(2));
189
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000190 MI.eraseFromParent();
191}
192
193void SILowerControlFlowPass::Else(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000194 MachineBasicBlock &MBB = *MI.getParent();
195 DebugLoc DL = MI.getDebugLoc();
196 unsigned Dst = MI.getOperand(0).getReg();
197 unsigned Src = MI.getOperand(1).getReg();
198
Christian Konige9818022013-03-26 14:03:44 +0000199 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
200 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000201 .addReg(Src); // Saved EXEC
202
203 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
204 .addReg(AMDGPU::EXEC)
205 .addReg(Dst);
206
Tom Stellardd09d43a2012-12-19 22:10:33 +0000207 Skip(MI, MI.getOperand(2));
208
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000209 MI.eraseFromParent();
210}
211
212void SILowerControlFlowPass::Break(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000213 MachineBasicBlock &MBB = *MI.getParent();
214 DebugLoc DL = MI.getDebugLoc();
215
216 unsigned Dst = MI.getOperand(0).getReg();
217 unsigned Src = MI.getOperand(1).getReg();
218
219 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
220 .addReg(AMDGPU::EXEC)
221 .addReg(Src);
222
223 MI.eraseFromParent();
224}
225
226void SILowerControlFlowPass::IfBreak(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000227 MachineBasicBlock &MBB = *MI.getParent();
228 DebugLoc DL = MI.getDebugLoc();
229
230 unsigned Dst = MI.getOperand(0).getReg();
231 unsigned Vcc = MI.getOperand(1).getReg();
232 unsigned Src = MI.getOperand(2).getReg();
233
234 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
235 .addReg(Vcc)
236 .addReg(Src);
237
238 MI.eraseFromParent();
239}
240
241void SILowerControlFlowPass::ElseBreak(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000242 MachineBasicBlock &MBB = *MI.getParent();
243 DebugLoc DL = MI.getDebugLoc();
244
245 unsigned Dst = MI.getOperand(0).getReg();
246 unsigned Saved = MI.getOperand(1).getReg();
247 unsigned Src = MI.getOperand(2).getReg();
248
249 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
250 .addReg(Saved)
251 .addReg(Src);
252
253 MI.eraseFromParent();
254}
255
256void SILowerControlFlowPass::Loop(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000257 MachineBasicBlock &MBB = *MI.getParent();
258 DebugLoc DL = MI.getDebugLoc();
259 unsigned Src = MI.getOperand(0).getReg();
260
261 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
262 .addReg(AMDGPU::EXEC)
263 .addReg(Src);
264
265 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
266 .addOperand(MI.getOperand(1))
267 .addReg(AMDGPU::EXEC);
268
269 MI.eraseFromParent();
270}
271
272void SILowerControlFlowPass::EndCf(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000273 MachineBasicBlock &MBB = *MI.getParent();
274 DebugLoc DL = MI.getDebugLoc();
275 unsigned Reg = MI.getOperand(0).getReg();
276
277 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
278 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
279 .addReg(AMDGPU::EXEC)
280 .addReg(Reg);
281
282 MI.eraseFromParent();
283}
284
Tom Stellardd09d43a2012-12-19 22:10:33 +0000285void SILowerControlFlowPass::Branch(MachineInstr &MI) {
Tom Stellardd09d43a2012-12-19 22:10:33 +0000286 MachineBasicBlock *Next = MI.getParent()->getNextNode();
287 MachineBasicBlock *Target = MI.getOperand(0).getMBB();
288 if (Target == Next)
289 MI.eraseFromParent();
290 else
291 assert(0);
292}
293
Tom Stellard935a9152013-01-18 21:15:50 +0000294void SILowerControlFlowPass::Kill(MachineInstr &MI) {
295
296 MachineBasicBlock &MBB = *MI.getParent();
297 DebugLoc DL = MI.getDebugLoc();
298
299 // Kill is only allowed in pixel shaders
NAKAMURA Takumi9262a642013-01-21 14:06:48 +0000300 assert(MBB.getParent()->getInfo<SIMachineFunctionInfo>()->ShaderType ==
301 ShaderType::PIXEL);
Tom Stellard935a9152013-01-18 21:15:50 +0000302
303 // Clear this pixel from the exec mask if the operand is negative
304 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32), AMDGPU::VCC)
Christian Konige25e4902013-02-16 11:28:22 +0000305 .addImm(0)
Tom Stellard935a9152013-01-18 21:15:50 +0000306 .addOperand(MI.getOperand(0));
307
308 MI.eraseFromParent();
309}
310
Christian Konigb9e86782013-03-18 11:34:16 +0000311void SILowerControlFlowPass::LoadM0(MachineInstr &MI, MachineInstr *MovRel) {
312
313 MachineBasicBlock &MBB = *MI.getParent();
314 DebugLoc DL = MI.getDebugLoc();
315 MachineBasicBlock::iterator I = MI;
316
317 unsigned Save = MI.getOperand(1).getReg();
318 unsigned Idx = MI.getOperand(3).getReg();
319
320 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
321 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
322 .addReg(Idx);
323 MBB.insert(I, MovRel);
324 MI.eraseFromParent();
325 return;
326 }
327
328 assert(AMDGPU::SReg_64RegClass.contains(Save));
329 assert(AMDGPU::VReg_32RegClass.contains(Idx));
330
331 // Save the EXEC mask
332 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
333 .addReg(AMDGPU::EXEC);
334
335 // Read the next variant into VCC (lower 32 bits) <- also loop target
336 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32_e32), AMDGPU::VCC)
337 .addReg(Idx);
338
339 // Move index from VCC into M0
340 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
341 .addReg(AMDGPU::VCC);
342
343 // Compare the just read M0 value to all possible Idx values
344 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32), AMDGPU::VCC)
345 .addReg(AMDGPU::M0)
346 .addReg(Idx);
347
348 // Update EXEC, save the original EXEC value to VCC
349 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
350 .addReg(AMDGPU::VCC);
351
352 // Do the actual move
353 MBB.insert(I, MovRel);
354
355 // Update EXEC, switch all done bits to 0 and all todo bits to 1
356 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
357 .addReg(AMDGPU::EXEC)
358 .addReg(AMDGPU::VCC);
359
360 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
361 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
362 .addImm(-7)
363 .addReg(AMDGPU::EXEC);
364
365 // Restore EXEC
366 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
367 .addReg(Save);
368
369 MI.eraseFromParent();
370}
371
372void SILowerControlFlowPass::IndirectSrc(MachineInstr &MI) {
373
374 MachineBasicBlock &MBB = *MI.getParent();
375 DebugLoc DL = MI.getDebugLoc();
376
377 unsigned Dst = MI.getOperand(0).getReg();
378 unsigned Vec = MI.getOperand(2).getReg();
379 unsigned Off = MI.getOperand(4).getImm();
380
381 MachineInstr *MovRel =
382 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
383 .addReg(TRI->getSubReg(Vec, AMDGPU::sub0) + Off)
384 .addReg(AMDGPU::M0, RegState::Implicit)
385 .addReg(Vec, RegState::Implicit);
386
387 LoadM0(MI, MovRel);
388}
389
390void SILowerControlFlowPass::IndirectDst(MachineInstr &MI) {
391
392 MachineBasicBlock &MBB = *MI.getParent();
393 DebugLoc DL = MI.getDebugLoc();
394
395 unsigned Dst = MI.getOperand(0).getReg();
396 unsigned Off = MI.getOperand(4).getImm();
397 unsigned Val = MI.getOperand(5).getReg();
398
399 MachineInstr *MovRel =
400 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
401 .addReg(TRI->getSubReg(Dst, AMDGPU::sub0) + Off, RegState::Define)
402 .addReg(Val)
403 .addReg(AMDGPU::M0, RegState::Implicit)
404 .addReg(Dst, RegState::Implicit);
405
406 LoadM0(MI, MovRel);
407}
408
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000409bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingb5632b52013-06-07 20:28:55 +0000410 TII = MF.getTarget().getInstrInfo();
411 TRI = MF.getTarget().getRegisterInfo();
Tom Stellard402b8e22013-09-05 18:37:52 +0000412 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard935a9152013-01-18 21:15:50 +0000413
414 bool HaveKill = false;
Michel Danzer7740daa2013-07-10 16:36:43 +0000415 bool NeedM0 = false;
Christian Konig03cd75e2013-03-26 14:03:50 +0000416 bool NeedWQM = false;
Tom Stellard935a9152013-01-18 21:15:50 +0000417 unsigned Depth = 0;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000418
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000419 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
420 BI != BE; ++BI) {
421
422 MachineBasicBlock &MBB = *BI;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000423 for (MachineBasicBlock::iterator I = MBB.begin(), Next = llvm::next(I);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000424 I != MBB.end(); I = Next) {
425
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000426 Next = llvm::next(I);
427 MachineInstr &MI = *I;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000428 switch (MI.getOpcode()) {
429 default: break;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000430 case AMDGPU::SI_IF:
Tom Stellard935a9152013-01-18 21:15:50 +0000431 ++Depth;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000432 If(MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000433 break;
434
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000435 case AMDGPU::SI_ELSE:
436 Else(MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000437 break;
438
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000439 case AMDGPU::SI_BREAK:
440 Break(MI);
441 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000442
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000443 case AMDGPU::SI_IF_BREAK:
444 IfBreak(MI);
445 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000446
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000447 case AMDGPU::SI_ELSE_BREAK:
448 ElseBreak(MI);
449 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000450
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000451 case AMDGPU::SI_LOOP:
Tom Stellard935a9152013-01-18 21:15:50 +0000452 ++Depth;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000453 Loop(MI);
454 break;
455
456 case AMDGPU::SI_END_CF:
Tom Stellard935a9152013-01-18 21:15:50 +0000457 if (--Depth == 0 && HaveKill) {
458 SkipIfDead(MI);
459 HaveKill = false;
460 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000461 EndCf(MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000462 break;
Tom Stellardd09d43a2012-12-19 22:10:33 +0000463
Tom Stellard935a9152013-01-18 21:15:50 +0000464 case AMDGPU::SI_KILL:
465 if (Depth == 0)
466 SkipIfDead(MI);
467 else
468 HaveKill = true;
469 Kill(MI);
470 break;
471
Tom Stellardd09d43a2012-12-19 22:10:33 +0000472 case AMDGPU::S_BRANCH:
473 Branch(MI);
474 break;
Christian Konigb9e86782013-03-18 11:34:16 +0000475
476 case AMDGPU::SI_INDIRECT_SRC:
477 IndirectSrc(MI);
478 break;
479
480 case AMDGPU::SI_INDIRECT_DST_V2:
481 case AMDGPU::SI_INDIRECT_DST_V4:
482 case AMDGPU::SI_INDIRECT_DST_V8:
483 case AMDGPU::SI_INDIRECT_DST_V16:
484 IndirectDst(MI);
485 break;
Christian Konig03cd75e2013-03-26 14:03:50 +0000486
Michel Danzer7740daa2013-07-10 16:36:43 +0000487 case AMDGPU::DS_READ_B32:
488 NeedWQM = true;
489 // Fall through
490 case AMDGPU::DS_WRITE_B32:
Tom Stellard79916942013-09-05 18:38:09 +0000491 case AMDGPU::DS_ADD_U32_RTN:
Michel Danzer7740daa2013-07-10 16:36:43 +0000492 NeedM0 = true;
493 break;
494
Christian Konig03cd75e2013-03-26 14:03:50 +0000495 case AMDGPU::V_INTERP_P1_F32:
496 case AMDGPU::V_INTERP_P2_F32:
497 case AMDGPU::V_INTERP_MOV_F32:
498 NeedWQM = true;
499 break;
500
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000501 }
502 }
503 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000504
Michel Danzer7740daa2013-07-10 16:36:43 +0000505 if (NeedM0) {
506 MachineBasicBlock &MBB = MF.front();
507 // Initialize M0 to a value that won't cause LDS access to be discarded
508 // due to offset clamping
509 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_MOV_B32),
510 AMDGPU::M0).addImm(0xffffffff);
511 }
512
Tom Stellard402b8e22013-09-05 18:37:52 +0000513 if (NeedWQM && MFI->ShaderType != ShaderType::COMPUTE) {
Christian Konig03cd75e2013-03-26 14:03:50 +0000514 MachineBasicBlock &MBB = MF.front();
515 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
516 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
517 }
518
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000519 return true;
520}