blob: 75b5a5e027ff9e57da5bdc5be837c448c2a0e58e [file] [log] [blame]
Tom Stellard75aadc22012-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 Stellardf8794352012-12-19 22:10:31 +000011/// \brief This pass lowers the pseudo control flow instructions to real
12/// machine instructions.
Tom Stellard75aadc22012-12-11 21:25:42 +000013///
Tom Stellardf8794352012-12-19 22:10:31 +000014/// All control flow is handled using predicated instructions and
Tom Stellard75aadc22012-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 Stellardf8794352012-12-19 22:10:31 +000025/// %SGPR0 = SI_IF %VCC
Tom Stellard75aadc22012-12-11 21:25:42 +000026/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
Tom Stellardf8794352012-12-19 22:10:31 +000027/// %SGPR0 = SI_ELSE %SGPR0
Tom Stellard75aadc22012-12-11 21:25:42 +000028/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
Tom Stellardf8794352012-12-19 22:10:31 +000029/// SI_END_CF %SGPR0
Tom Stellard75aadc22012-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 Stellardf8794352012-12-19 22:10:31 +000035/// S_CBRANCH_EXECZ label0 // This instruction is an optional
Tom Stellard75aadc22012-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 Stellardf8794352012-12-19 22:10:31 +000048/// %EXEC = S_OR_B64 %EXEC, %SGPR0 // Re-enable saved exec mask bits
Tom Stellard75aadc22012-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"
Michel Danzer9e61c4b2014-02-27 01:47:09 +000058#include "llvm/IR/Constants.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000059
60using namespace llvm;
61
62namespace {
63
64class SILowerControlFlowPass : public MachineFunctionPass {
65
66private:
Tom Stellarde7b907d2012-12-19 22:10:33 +000067 static const unsigned SkipThreshold = 12;
68
Tom Stellard75aadc22012-12-11 21:25:42 +000069 static char ID;
Tom Stellard1bd80722014-04-30 15:31:33 +000070 const SIRegisterInfo *TRI;
Tom Stellard5d7aaae2014-02-10 16:58:30 +000071 const SIInstrInfo *TII;
Tom Stellard75aadc22012-12-11 21:25:42 +000072
Tom Stellardbe8ebee2013-01-18 21:15:50 +000073 bool shouldSkip(MachineBasicBlock *From, MachineBasicBlock *To);
74
75 void Skip(MachineInstr &From, MachineOperand &To);
76 void SkipIfDead(MachineInstr &MI);
Tom Stellarde7b907d2012-12-19 22:10:33 +000077
Tom Stellardf8794352012-12-19 22:10:31 +000078 void If(MachineInstr &MI);
79 void Else(MachineInstr &MI);
80 void Break(MachineInstr &MI);
81 void IfBreak(MachineInstr &MI);
82 void ElseBreak(MachineInstr &MI);
83 void Loop(MachineInstr &MI);
84 void EndCf(MachineInstr &MI);
Tom Stellard75aadc22012-12-11 21:25:42 +000085
Tom Stellardbe8ebee2013-01-18 21:15:50 +000086 void Kill(MachineInstr &MI);
Tom Stellarde7b907d2012-12-19 22:10:33 +000087 void Branch(MachineInstr &MI);
88
Tom Stellard89422762014-06-17 16:53:04 +000089 void InitM0ForLDS(MachineBasicBlock::iterator MI);
Christian Konig2989ffc2013-03-18 11:34:16 +000090 void LoadM0(MachineInstr &MI, MachineInstr *MovRel);
91 void IndirectSrc(MachineInstr &MI);
92 void IndirectDst(MachineInstr &MI);
93
Tom Stellard75aadc22012-12-11 21:25:42 +000094public:
95 SILowerControlFlowPass(TargetMachine &tm) :
Craig Topper062a2ba2014-04-25 05:30:21 +000096 MachineFunctionPass(ID), TRI(nullptr), TII(nullptr) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000097
Craig Topper5656db42014-04-29 07:57:24 +000098 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard75aadc22012-12-11 21:25:42 +000099
Craig Topper5656db42014-04-29 07:57:24 +0000100 const char *getPassName() const override {
Tom Stellard75aadc22012-12-11 21:25:42 +0000101 return "SI Lower control flow instructions";
102 }
103
104};
105
106} // End anonymous namespace
107
108char SILowerControlFlowPass::ID = 0;
109
110FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
111 return new SILowerControlFlowPass(tm);
112}
113
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000114bool SILowerControlFlowPass::shouldSkip(MachineBasicBlock *From,
115 MachineBasicBlock *To) {
116
Tom Stellarde7b907d2012-12-19 22:10:33 +0000117 unsigned NumInstr = 0;
118
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000119 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellarde7b907d2012-12-19 22:10:33 +0000120 MBB = *MBB->succ_begin()) {
121
122 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
123 NumInstr < SkipThreshold && I != E; ++I) {
124
125 if (I->isBundle() || !I->isBundled())
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000126 if (++NumInstr >= SkipThreshold)
127 return true;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000128 }
129 }
130
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000131 return false;
132}
133
134void SILowerControlFlowPass::Skip(MachineInstr &From, MachineOperand &To) {
135
136 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000137 return;
138
139 DebugLoc DL = From.getDebugLoc();
140 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
141 .addOperand(To)
142 .addReg(AMDGPU::EXEC);
143}
144
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000145void SILowerControlFlowPass::SkipIfDead(MachineInstr &MI) {
146
147 MachineBasicBlock &MBB = *MI.getParent();
148 DebugLoc DL = MI.getDebugLoc();
149
Matt Arsenault762af962014-07-13 03:06:39 +0000150 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000151 ShaderType::PIXEL ||
152 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000153 return;
154
155 MachineBasicBlock::iterator Insert = &MI;
156 ++Insert;
157
158 // If the exec mask is non-zero, skip the next two instructions
159 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
160 .addImm(3)
161 .addReg(AMDGPU::EXEC);
162
163 // Exec mask is zero: Export to NULL target...
164 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
165 .addImm(0)
166 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
167 .addImm(0)
168 .addImm(1)
169 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000170 .addReg(AMDGPU::VGPR0)
171 .addReg(AMDGPU::VGPR0)
172 .addReg(AMDGPU::VGPR0)
173 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000174
175 // ... and terminate wavefront
176 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
177}
178
Tom Stellardf8794352012-12-19 22:10:31 +0000179void SILowerControlFlowPass::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000180 MachineBasicBlock &MBB = *MI.getParent();
181 DebugLoc DL = MI.getDebugLoc();
182 unsigned Reg = MI.getOperand(0).getReg();
183 unsigned Vcc = MI.getOperand(1).getReg();
184
185 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
186 .addReg(Vcc);
187
188 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
189 .addReg(AMDGPU::EXEC)
190 .addReg(Reg);
191
Tom Stellarde7b907d2012-12-19 22:10:33 +0000192 Skip(MI, MI.getOperand(2));
193
Tom Stellardf8794352012-12-19 22:10:31 +0000194 MI.eraseFromParent();
195}
196
197void SILowerControlFlowPass::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000198 MachineBasicBlock &MBB = *MI.getParent();
199 DebugLoc DL = MI.getDebugLoc();
200 unsigned Dst = MI.getOperand(0).getReg();
201 unsigned Src = MI.getOperand(1).getReg();
202
Christian Konig6a9d3902013-03-26 14:03:44 +0000203 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
204 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000205 .addReg(Src); // Saved EXEC
206
207 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
208 .addReg(AMDGPU::EXEC)
209 .addReg(Dst);
210
Tom Stellarde7b907d2012-12-19 22:10:33 +0000211 Skip(MI, MI.getOperand(2));
212
Tom Stellardf8794352012-12-19 22:10:31 +0000213 MI.eraseFromParent();
214}
215
216void SILowerControlFlowPass::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000217 MachineBasicBlock &MBB = *MI.getParent();
218 DebugLoc DL = MI.getDebugLoc();
219
220 unsigned Dst = MI.getOperand(0).getReg();
221 unsigned Src = MI.getOperand(1).getReg();
222
223 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
224 .addReg(AMDGPU::EXEC)
225 .addReg(Src);
226
227 MI.eraseFromParent();
228}
229
230void SILowerControlFlowPass::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000231 MachineBasicBlock &MBB = *MI.getParent();
232 DebugLoc DL = MI.getDebugLoc();
233
234 unsigned Dst = MI.getOperand(0).getReg();
235 unsigned Vcc = MI.getOperand(1).getReg();
236 unsigned Src = MI.getOperand(2).getReg();
237
238 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
239 .addReg(Vcc)
240 .addReg(Src);
241
242 MI.eraseFromParent();
243}
244
245void SILowerControlFlowPass::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000246 MachineBasicBlock &MBB = *MI.getParent();
247 DebugLoc DL = MI.getDebugLoc();
248
249 unsigned Dst = MI.getOperand(0).getReg();
250 unsigned Saved = MI.getOperand(1).getReg();
251 unsigned Src = MI.getOperand(2).getReg();
252
253 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
254 .addReg(Saved)
255 .addReg(Src);
256
257 MI.eraseFromParent();
258}
259
260void SILowerControlFlowPass::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000261 MachineBasicBlock &MBB = *MI.getParent();
262 DebugLoc DL = MI.getDebugLoc();
263 unsigned Src = MI.getOperand(0).getReg();
264
265 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
266 .addReg(AMDGPU::EXEC)
267 .addReg(Src);
268
269 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
270 .addOperand(MI.getOperand(1))
271 .addReg(AMDGPU::EXEC);
272
273 MI.eraseFromParent();
274}
275
276void SILowerControlFlowPass::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000277 MachineBasicBlock &MBB = *MI.getParent();
278 DebugLoc DL = MI.getDebugLoc();
279 unsigned Reg = MI.getOperand(0).getReg();
280
281 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
282 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
283 .addReg(AMDGPU::EXEC)
284 .addReg(Reg);
285
286 MI.eraseFromParent();
287}
288
Tom Stellarde7b907d2012-12-19 22:10:33 +0000289void SILowerControlFlowPass::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000290 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
291 MI.eraseFromParent();
292
293 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000294}
295
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000296void SILowerControlFlowPass::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000297 MachineBasicBlock &MBB = *MI.getParent();
298 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000299 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000300
Matt Arsenault762af962014-07-13 03:06:39 +0000301#ifndef NDEBUG
302 const SIMachineFunctionInfo *MFI
303 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
304 // Kill is only allowed in pixel / geometry shaders.
305 assert(MFI->getShaderType() == ShaderType::PIXEL ||
306 MFI->getShaderType() == ShaderType::GEOMETRY);
307#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000308
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000309 // Clear this thread from the exec mask if the operand is negative
310 if ((Op.isImm() || Op.isFPImm())) {
311 // Constant operand: Set exec mask to 0 or do nothing
312 if (Op.isImm() ? (Op.getImm() & 0x80000000) :
313 Op.getFPImm()->isNegative()) {
314 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
315 .addImm(0);
316 }
317 } else {
318 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32), AMDGPU::VCC)
319 .addImm(0)
320 .addOperand(Op);
321 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000322
323 MI.eraseFromParent();
324}
325
Tom Stellard89422762014-06-17 16:53:04 +0000326/// The m0 register stores the maximum allowable address for LDS reads and
327/// writes. Its value must be at least the size in bytes of LDS allocated by
328/// the shader. For simplicity, we set it to the maximum possible value.
329void SILowerControlFlowPass::InitM0ForLDS(MachineBasicBlock::iterator MI) {
330 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32),
331 AMDGPU::M0).addImm(0xffffffff);
332}
333
Christian Konig2989ffc2013-03-18 11:34:16 +0000334void SILowerControlFlowPass::LoadM0(MachineInstr &MI, MachineInstr *MovRel) {
335
336 MachineBasicBlock &MBB = *MI.getParent();
337 DebugLoc DL = MI.getDebugLoc();
338 MachineBasicBlock::iterator I = MI;
339
340 unsigned Save = MI.getOperand(1).getReg();
341 unsigned Idx = MI.getOperand(3).getReg();
342
343 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
344 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
345 .addReg(Idx);
346 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000347 } else {
348
349 assert(AMDGPU::SReg_64RegClass.contains(Save));
350 assert(AMDGPU::VReg_32RegClass.contains(Idx));
351
352 // Save the EXEC mask
353 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
354 .addReg(AMDGPU::EXEC);
355
356 // Read the next variant into VCC (lower 32 bits) <- also loop target
357 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
358 AMDGPU::VCC_LO)
359 .addReg(Idx);
360
361 // Move index from VCC into M0
362 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
363 .addReg(AMDGPU::VCC_LO);
364
365 // Compare the just read M0 value to all possible Idx values
366 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32), AMDGPU::VCC)
367 .addReg(AMDGPU::M0)
368 .addReg(Idx);
369
370 // Update EXEC, save the original EXEC value to VCC
371 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
372 .addReg(AMDGPU::VCC);
373
374 // Do the actual move
375 MBB.insert(I, MovRel);
376
377 // Update EXEC, switch all done bits to 0 and all todo bits to 1
378 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
379 .addReg(AMDGPU::EXEC)
380 .addReg(AMDGPU::VCC);
381
382 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
383 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
384 .addImm(-7)
385 .addReg(AMDGPU::EXEC);
386
387 // Restore EXEC
388 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
389 .addReg(Save);
390
Christian Konig2989ffc2013-03-18 11:34:16 +0000391 }
Tom Stellard89422762014-06-17 16:53:04 +0000392 // FIXME: Are there any values other than the LDS address clamp that need to
393 // be stored in the m0 register and may be live for more than a few
394 // instructions? If so, we should save the m0 register at the beginning
395 // of this function and restore it here.
396 // FIXME: Add support for LDS direct loads.
397 InitM0ForLDS(&MI);
Christian Konig2989ffc2013-03-18 11:34:16 +0000398 MI.eraseFromParent();
399}
400
401void SILowerControlFlowPass::IndirectSrc(MachineInstr &MI) {
402
403 MachineBasicBlock &MBB = *MI.getParent();
404 DebugLoc DL = MI.getDebugLoc();
405
406 unsigned Dst = MI.getOperand(0).getReg();
407 unsigned Vec = MI.getOperand(2).getReg();
408 unsigned Off = MI.getOperand(4).getImm();
Tom Stellard81d871d2013-11-13 23:36:50 +0000409 unsigned SubReg = TRI->getSubReg(Vec, AMDGPU::sub0);
410 if (!SubReg)
411 SubReg = Vec;
Christian Konig2989ffc2013-03-18 11:34:16 +0000412
Tom Stellard81d871d2013-11-13 23:36:50 +0000413 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000414 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard81d871d2013-11-13 23:36:50 +0000415 .addReg(SubReg + Off)
Christian Konig2989ffc2013-03-18 11:34:16 +0000416 .addReg(AMDGPU::M0, RegState::Implicit)
417 .addReg(Vec, RegState::Implicit);
418
419 LoadM0(MI, MovRel);
420}
421
422void SILowerControlFlowPass::IndirectDst(MachineInstr &MI) {
423
424 MachineBasicBlock &MBB = *MI.getParent();
425 DebugLoc DL = MI.getDebugLoc();
426
427 unsigned Dst = MI.getOperand(0).getReg();
428 unsigned Off = MI.getOperand(4).getImm();
429 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard81d871d2013-11-13 23:36:50 +0000430 unsigned SubReg = TRI->getSubReg(Dst, AMDGPU::sub0);
431 if (!SubReg)
432 SubReg = Dst;
Christian Konig2989ffc2013-03-18 11:34:16 +0000433
434 MachineInstr *MovRel =
435 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard81d871d2013-11-13 23:36:50 +0000436 .addReg(SubReg + Off, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000437 .addReg(Val)
438 .addReg(AMDGPU::M0, RegState::Implicit)
439 .addReg(Dst, RegState::Implicit);
440
441 LoadM0(MI, MovRel);
442}
443
Tom Stellard75aadc22012-12-11 21:25:42 +0000444bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000445 TII = static_cast<const SIInstrInfo*>(MF.getTarget().getInstrInfo());
Tom Stellard1bd80722014-04-30 15:31:33 +0000446 TRI = static_cast<const SIRegisterInfo*>(MF.getTarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000447 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000448
449 bool HaveKill = false;
Michel Danzer1c454302013-07-10 16:36:43 +0000450 bool NeedM0 = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000451 bool NeedWQM = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000452 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000453
Tom Stellardf8794352012-12-19 22:10:31 +0000454 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
455 BI != BE; ++BI) {
456
457 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000458 MachineBasicBlock::iterator I, Next;
459 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000460 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000461
Tom Stellard75aadc22012-12-11 21:25:42 +0000462 MachineInstr &MI = *I;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000463 if (TII->isDS(MI.getOpcode())) {
464 NeedM0 = true;
465 NeedWQM = true;
466 }
467
Tom Stellard75aadc22012-12-11 21:25:42 +0000468 switch (MI.getOpcode()) {
469 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000470 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000471 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000472 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000473 break;
474
Tom Stellardf8794352012-12-19 22:10:31 +0000475 case AMDGPU::SI_ELSE:
476 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000477 break;
478
Tom Stellardf8794352012-12-19 22:10:31 +0000479 case AMDGPU::SI_BREAK:
480 Break(MI);
481 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000482
Tom Stellardf8794352012-12-19 22:10:31 +0000483 case AMDGPU::SI_IF_BREAK:
484 IfBreak(MI);
485 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000486
Tom Stellardf8794352012-12-19 22:10:31 +0000487 case AMDGPU::SI_ELSE_BREAK:
488 ElseBreak(MI);
489 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000490
Tom Stellardf8794352012-12-19 22:10:31 +0000491 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000492 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000493 Loop(MI);
494 break;
495
496 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000497 if (--Depth == 0 && HaveKill) {
498 SkipIfDead(MI);
499 HaveKill = false;
500 }
Tom Stellardf8794352012-12-19 22:10:31 +0000501 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000502 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000503
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000504 case AMDGPU::SI_KILL:
505 if (Depth == 0)
506 SkipIfDead(MI);
507 else
508 HaveKill = true;
509 Kill(MI);
510 break;
511
Tom Stellarde7b907d2012-12-19 22:10:33 +0000512 case AMDGPU::S_BRANCH:
513 Branch(MI);
514 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000515
516 case AMDGPU::SI_INDIRECT_SRC:
517 IndirectSrc(MI);
518 break;
519
Tom Stellard81d871d2013-11-13 23:36:50 +0000520 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000521 case AMDGPU::SI_INDIRECT_DST_V2:
522 case AMDGPU::SI_INDIRECT_DST_V4:
523 case AMDGPU::SI_INDIRECT_DST_V8:
524 case AMDGPU::SI_INDIRECT_DST_V16:
525 IndirectDst(MI);
526 break;
Christian Konig737d4a12013-03-26 14:03:50 +0000527
528 case AMDGPU::V_INTERP_P1_F32:
529 case AMDGPU::V_INTERP_P2_F32:
530 case AMDGPU::V_INTERP_MOV_F32:
531 NeedWQM = true;
532 break;
533
Tom Stellard75aadc22012-12-11 21:25:42 +0000534 }
535 }
536 }
Tom Stellardf8794352012-12-19 22:10:31 +0000537
Michel Danzer1c454302013-07-10 16:36:43 +0000538 if (NeedM0) {
539 MachineBasicBlock &MBB = MF.front();
540 // Initialize M0 to a value that won't cause LDS access to be discarded
541 // due to offset clamping
Tom Stellard89422762014-06-17 16:53:04 +0000542 InitM0ForLDS(MBB.getFirstNonPHI());
Michel Danzer1c454302013-07-10 16:36:43 +0000543 }
544
Matt Arsenault762af962014-07-13 03:06:39 +0000545 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000546 MachineBasicBlock &MBB = MF.front();
547 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
548 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
549 }
550
Tom Stellard75aadc22012-12-11 21:25:42 +0000551 return true;
552}