blob: 068b22f37704872d115226acdd19ae89e4fdbed3 [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"
Eric Christopherd9134482014-08-04 21:25:23 +000052#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000053#include "SIInstrInfo.h"
54#include "SIMachineFunctionInfo.h"
Matt Arsenault3f981402014-09-15 15:41:53 +000055#include "llvm/CodeGen/MachineFrameInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000056#include "llvm/CodeGen/MachineFunction.h"
57#include "llvm/CodeGen/MachineFunctionPass.h"
58#include "llvm/CodeGen/MachineInstrBuilder.h"
59#include "llvm/CodeGen/MachineRegisterInfo.h"
Michel Danzer9e61c4b2014-02-27 01:47:09 +000060#include "llvm/IR/Constants.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000061
62using namespace llvm;
63
64namespace {
65
66class SILowerControlFlowPass : public MachineFunctionPass {
67
68private:
Tom Stellarde7b907d2012-12-19 22:10:33 +000069 static const unsigned SkipThreshold = 12;
70
Tom Stellard75aadc22012-12-11 21:25:42 +000071 static char ID;
Tom Stellard1bd80722014-04-30 15:31:33 +000072 const SIRegisterInfo *TRI;
Tom Stellard5d7aaae2014-02-10 16:58:30 +000073 const SIInstrInfo *TII;
Tom Stellard75aadc22012-12-11 21:25:42 +000074
Tom Stellardbe8ebee2013-01-18 21:15:50 +000075 bool shouldSkip(MachineBasicBlock *From, MachineBasicBlock *To);
76
77 void Skip(MachineInstr &From, MachineOperand &To);
78 void SkipIfDead(MachineInstr &MI);
Tom Stellarde7b907d2012-12-19 22:10:33 +000079
Tom Stellardf8794352012-12-19 22:10:31 +000080 void If(MachineInstr &MI);
81 void Else(MachineInstr &MI);
82 void Break(MachineInstr &MI);
83 void IfBreak(MachineInstr &MI);
84 void ElseBreak(MachineInstr &MI);
85 void Loop(MachineInstr &MI);
86 void EndCf(MachineInstr &MI);
Tom Stellard75aadc22012-12-11 21:25:42 +000087
Tom Stellardbe8ebee2013-01-18 21:15:50 +000088 void Kill(MachineInstr &MI);
Tom Stellarde7b907d2012-12-19 22:10:33 +000089 void Branch(MachineInstr &MI);
90
Christian Konig2989ffc2013-03-18 11:34:16 +000091 void LoadM0(MachineInstr &MI, MachineInstr *MovRel);
92 void IndirectSrc(MachineInstr &MI);
93 void IndirectDst(MachineInstr &MI);
94
Tom Stellard75aadc22012-12-11 21:25:42 +000095public:
96 SILowerControlFlowPass(TargetMachine &tm) :
Craig Topper062a2ba2014-04-25 05:30:21 +000097 MachineFunctionPass(ID), TRI(nullptr), TII(nullptr) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000098
Craig Topper5656db42014-04-29 07:57:24 +000099 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000100
Craig Topper5656db42014-04-29 07:57:24 +0000101 const char *getPassName() const override {
Tom Stellard75aadc22012-12-11 21:25:42 +0000102 return "SI Lower control flow instructions";
103 }
104
105};
106
107} // End anonymous namespace
108
109char SILowerControlFlowPass::ID = 0;
110
111FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
112 return new SILowerControlFlowPass(tm);
113}
114
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000115bool SILowerControlFlowPass::shouldSkip(MachineBasicBlock *From,
116 MachineBasicBlock *To) {
117
Tom Stellarde7b907d2012-12-19 22:10:33 +0000118 unsigned NumInstr = 0;
119
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000120 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellarde7b907d2012-12-19 22:10:33 +0000121 MBB = *MBB->succ_begin()) {
122
123 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
124 NumInstr < SkipThreshold && I != E; ++I) {
125
126 if (I->isBundle() || !I->isBundled())
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000127 if (++NumInstr >= SkipThreshold)
128 return true;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000129 }
130 }
131
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000132 return false;
133}
134
135void SILowerControlFlowPass::Skip(MachineInstr &From, MachineOperand &To) {
136
137 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000138 return;
139
140 DebugLoc DL = From.getDebugLoc();
141 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
142 .addOperand(To)
143 .addReg(AMDGPU::EXEC);
144}
145
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000146void SILowerControlFlowPass::SkipIfDead(MachineInstr &MI) {
147
148 MachineBasicBlock &MBB = *MI.getParent();
149 DebugLoc DL = MI.getDebugLoc();
150
Matt Arsenault762af962014-07-13 03:06:39 +0000151 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000152 ShaderType::PIXEL ||
153 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000154 return;
155
156 MachineBasicBlock::iterator Insert = &MI;
157 ++Insert;
158
159 // If the exec mask is non-zero, skip the next two instructions
160 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
161 .addImm(3)
162 .addReg(AMDGPU::EXEC);
163
164 // Exec mask is zero: Export to NULL target...
165 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
166 .addImm(0)
167 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
168 .addImm(0)
169 .addImm(1)
170 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000171 .addReg(AMDGPU::VGPR0)
172 .addReg(AMDGPU::VGPR0)
173 .addReg(AMDGPU::VGPR0)
174 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000175
176 // ... and terminate wavefront
177 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
178}
179
Tom Stellardf8794352012-12-19 22:10:31 +0000180void SILowerControlFlowPass::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000181 MachineBasicBlock &MBB = *MI.getParent();
182 DebugLoc DL = MI.getDebugLoc();
183 unsigned Reg = MI.getOperand(0).getReg();
184 unsigned Vcc = MI.getOperand(1).getReg();
185
186 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
187 .addReg(Vcc);
188
189 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
190 .addReg(AMDGPU::EXEC)
191 .addReg(Reg);
192
Tom Stellarde7b907d2012-12-19 22:10:33 +0000193 Skip(MI, MI.getOperand(2));
194
Tom Stellardf8794352012-12-19 22:10:31 +0000195 MI.eraseFromParent();
196}
197
198void SILowerControlFlowPass::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000199 MachineBasicBlock &MBB = *MI.getParent();
200 DebugLoc DL = MI.getDebugLoc();
201 unsigned Dst = MI.getOperand(0).getReg();
202 unsigned Src = MI.getOperand(1).getReg();
203
Christian Konig6a9d3902013-03-26 14:03:44 +0000204 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
205 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000206 .addReg(Src); // Saved EXEC
207
208 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
209 .addReg(AMDGPU::EXEC)
210 .addReg(Dst);
211
Tom Stellarde7b907d2012-12-19 22:10:33 +0000212 Skip(MI, MI.getOperand(2));
213
Tom Stellardf8794352012-12-19 22:10:31 +0000214 MI.eraseFromParent();
215}
216
217void SILowerControlFlowPass::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000218 MachineBasicBlock &MBB = *MI.getParent();
219 DebugLoc DL = MI.getDebugLoc();
220
221 unsigned Dst = MI.getOperand(0).getReg();
222 unsigned Src = MI.getOperand(1).getReg();
223
224 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
225 .addReg(AMDGPU::EXEC)
226 .addReg(Src);
227
228 MI.eraseFromParent();
229}
230
231void SILowerControlFlowPass::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000232 MachineBasicBlock &MBB = *MI.getParent();
233 DebugLoc DL = MI.getDebugLoc();
234
235 unsigned Dst = MI.getOperand(0).getReg();
236 unsigned Vcc = MI.getOperand(1).getReg();
237 unsigned Src = MI.getOperand(2).getReg();
238
239 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
240 .addReg(Vcc)
241 .addReg(Src);
242
243 MI.eraseFromParent();
244}
245
246void SILowerControlFlowPass::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000247 MachineBasicBlock &MBB = *MI.getParent();
248 DebugLoc DL = MI.getDebugLoc();
249
250 unsigned Dst = MI.getOperand(0).getReg();
251 unsigned Saved = MI.getOperand(1).getReg();
252 unsigned Src = MI.getOperand(2).getReg();
253
254 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
255 .addReg(Saved)
256 .addReg(Src);
257
258 MI.eraseFromParent();
259}
260
261void SILowerControlFlowPass::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000262 MachineBasicBlock &MBB = *MI.getParent();
263 DebugLoc DL = MI.getDebugLoc();
264 unsigned Src = MI.getOperand(0).getReg();
265
266 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
267 .addReg(AMDGPU::EXEC)
268 .addReg(Src);
269
270 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
271 .addOperand(MI.getOperand(1))
272 .addReg(AMDGPU::EXEC);
273
274 MI.eraseFromParent();
275}
276
277void SILowerControlFlowPass::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000278 MachineBasicBlock &MBB = *MI.getParent();
279 DebugLoc DL = MI.getDebugLoc();
280 unsigned Reg = MI.getOperand(0).getReg();
281
282 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
283 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
284 .addReg(AMDGPU::EXEC)
285 .addReg(Reg);
286
287 MI.eraseFromParent();
288}
289
Tom Stellarde7b907d2012-12-19 22:10:33 +0000290void SILowerControlFlowPass::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000291 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
292 MI.eraseFromParent();
293
294 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000295}
296
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000297void SILowerControlFlowPass::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000298 MachineBasicBlock &MBB = *MI.getParent();
299 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000300 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000301
Matt Arsenault762af962014-07-13 03:06:39 +0000302#ifndef NDEBUG
303 const SIMachineFunctionInfo *MFI
304 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
305 // Kill is only allowed in pixel / geometry shaders.
306 assert(MFI->getShaderType() == ShaderType::PIXEL ||
307 MFI->getShaderType() == ShaderType::GEOMETRY);
308#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000309
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000310 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000311 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000312 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000313 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000314 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
Christian Konig2989ffc2013-03-18 11:34:16 +0000326void SILowerControlFlowPass::LoadM0(MachineInstr &MI, MachineInstr *MovRel) {
327
328 MachineBasicBlock &MBB = *MI.getParent();
329 DebugLoc DL = MI.getDebugLoc();
330 MachineBasicBlock::iterator I = MI;
331
332 unsigned Save = MI.getOperand(1).getReg();
333 unsigned Idx = MI.getOperand(3).getReg();
334
335 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
336 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
337 .addReg(Idx);
338 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000339 } else {
340
341 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000342 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000343
344 // Save the EXEC mask
345 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
346 .addReg(AMDGPU::EXEC);
347
348 // Read the next variant into VCC (lower 32 bits) <- also loop target
349 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
350 AMDGPU::VCC_LO)
351 .addReg(Idx);
352
353 // Move index from VCC into M0
354 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
355 .addReg(AMDGPU::VCC_LO);
356
357 // Compare the just read M0 value to all possible Idx values
358 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32), AMDGPU::VCC)
359 .addReg(AMDGPU::M0)
360 .addReg(Idx);
361
362 // Update EXEC, save the original EXEC value to VCC
363 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
364 .addReg(AMDGPU::VCC);
365
366 // Do the actual move
367 MBB.insert(I, MovRel);
368
369 // Update EXEC, switch all done bits to 0 and all todo bits to 1
370 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
371 .addReg(AMDGPU::EXEC)
372 .addReg(AMDGPU::VCC);
373
374 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
375 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
376 .addImm(-7)
377 .addReg(AMDGPU::EXEC);
378
379 // Restore EXEC
380 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
381 .addReg(Save);
382
Christian Konig2989ffc2013-03-18 11:34:16 +0000383 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000384 MI.eraseFromParent();
385}
386
387void SILowerControlFlowPass::IndirectSrc(MachineInstr &MI) {
388
389 MachineBasicBlock &MBB = *MI.getParent();
390 DebugLoc DL = MI.getDebugLoc();
391
392 unsigned Dst = MI.getOperand(0).getReg();
393 unsigned Vec = MI.getOperand(2).getReg();
394 unsigned Off = MI.getOperand(4).getImm();
Tom Stellard81d871d2013-11-13 23:36:50 +0000395 unsigned SubReg = TRI->getSubReg(Vec, AMDGPU::sub0);
396 if (!SubReg)
397 SubReg = Vec;
Christian Konig2989ffc2013-03-18 11:34:16 +0000398
Tom Stellard81d871d2013-11-13 23:36:50 +0000399 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000400 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard81d871d2013-11-13 23:36:50 +0000401 .addReg(SubReg + Off)
Christian Konig2989ffc2013-03-18 11:34:16 +0000402 .addReg(AMDGPU::M0, RegState::Implicit)
403 .addReg(Vec, RegState::Implicit);
404
405 LoadM0(MI, MovRel);
406}
407
408void SILowerControlFlowPass::IndirectDst(MachineInstr &MI) {
409
410 MachineBasicBlock &MBB = *MI.getParent();
411 DebugLoc DL = MI.getDebugLoc();
412
413 unsigned Dst = MI.getOperand(0).getReg();
414 unsigned Off = MI.getOperand(4).getImm();
415 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard81d871d2013-11-13 23:36:50 +0000416 unsigned SubReg = TRI->getSubReg(Dst, AMDGPU::sub0);
417 if (!SubReg)
418 SubReg = Dst;
Christian Konig2989ffc2013-03-18 11:34:16 +0000419
420 MachineInstr *MovRel =
421 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard81d871d2013-11-13 23:36:50 +0000422 .addReg(SubReg + Off, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000423 .addReg(Val)
424 .addReg(AMDGPU::M0, RegState::Implicit)
425 .addReg(Dst, RegState::Implicit);
426
427 LoadM0(MI, MovRel);
428}
429
Tom Stellard75aadc22012-12-11 21:25:42 +0000430bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000431 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
432 TRI =
433 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000434 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000435
436 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000437 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000438 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000439 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000440
Tom Stellardf8794352012-12-19 22:10:31 +0000441 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
442 BI != BE; ++BI) {
443
444 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000445 MachineBasicBlock::iterator I, Next;
446 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000447 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000448
Tom Stellard75aadc22012-12-11 21:25:42 +0000449 MachineInstr &MI = *I;
Aaron Ballman41580de2014-11-24 14:03:16 +0000450 if (TII->isDS(MI.getOpcode()))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000451 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000452
Matt Arsenault3f981402014-09-15 15:41:53 +0000453 // Flat uses m0 in case it needs to access LDS.
Aaron Ballman41580de2014-11-24 14:03:16 +0000454 if (TII->isFLAT(MI.getOpcode()))
Matt Arsenault3f981402014-09-15 15:41:53 +0000455 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000456
Tom Stellard75aadc22012-12-11 21:25:42 +0000457 switch (MI.getOpcode()) {
458 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000459 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000460 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000461 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000462 break;
463
Tom Stellardf8794352012-12-19 22:10:31 +0000464 case AMDGPU::SI_ELSE:
465 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000466 break;
467
Tom Stellardf8794352012-12-19 22:10:31 +0000468 case AMDGPU::SI_BREAK:
469 Break(MI);
470 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000471
Tom Stellardf8794352012-12-19 22:10:31 +0000472 case AMDGPU::SI_IF_BREAK:
473 IfBreak(MI);
474 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000475
Tom Stellardf8794352012-12-19 22:10:31 +0000476 case AMDGPU::SI_ELSE_BREAK:
477 ElseBreak(MI);
478 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000479
Tom Stellardf8794352012-12-19 22:10:31 +0000480 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000481 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000482 Loop(MI);
483 break;
484
485 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000486 if (--Depth == 0 && HaveKill) {
487 SkipIfDead(MI);
488 HaveKill = false;
489 }
Tom Stellardf8794352012-12-19 22:10:31 +0000490 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000491 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000492
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000493 case AMDGPU::SI_KILL:
494 if (Depth == 0)
495 SkipIfDead(MI);
496 else
497 HaveKill = true;
498 Kill(MI);
499 break;
500
Tom Stellarde7b907d2012-12-19 22:10:33 +0000501 case AMDGPU::S_BRANCH:
502 Branch(MI);
503 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000504
505 case AMDGPU::SI_INDIRECT_SRC:
506 IndirectSrc(MI);
507 break;
508
Tom Stellard81d871d2013-11-13 23:36:50 +0000509 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000510 case AMDGPU::SI_INDIRECT_DST_V2:
511 case AMDGPU::SI_INDIRECT_DST_V4:
512 case AMDGPU::SI_INDIRECT_DST_V8:
513 case AMDGPU::SI_INDIRECT_DST_V16:
514 IndirectDst(MI);
515 break;
Christian Konig737d4a12013-03-26 14:03:50 +0000516
517 case AMDGPU::V_INTERP_P1_F32:
518 case AMDGPU::V_INTERP_P2_F32:
519 case AMDGPU::V_INTERP_MOV_F32:
520 NeedWQM = true;
521 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000522 }
523 }
524 }
Tom Stellardf8794352012-12-19 22:10:31 +0000525
Matt Arsenault762af962014-07-13 03:06:39 +0000526 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000527 MachineBasicBlock &MBB = MF.front();
528 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
529 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
530 }
531
Matt Arsenault3f981402014-09-15 15:41:53 +0000532 // FIXME: This seems inappropriate to do here.
533 if (NeedFlat && MFI->IsKernel) {
534 // Insert the prologue initializing the SGPRs pointing to the scratch space
535 // for flat accesses.
536 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
537
538 // TODO: What to use with function calls?
539
540 // FIXME: This is reporting stack size that is used in a scratch buffer
541 // rather than registers as well.
542 uint64_t StackSizeBytes = FrameInfo->getStackSize();
543
544 int IndirectBegin
545 = static_cast<const AMDGPUInstrInfo*>(TII)->getIndirectIndexBegin(MF);
546 // Convert register index to 256-byte unit.
547 uint64_t StackOffset = IndirectBegin < 0 ? 0 : (4 * IndirectBegin / 256);
548
549 assert((StackSizeBytes < 0xffff) && StackOffset < 0xffff &&
550 "Stack limits should be smaller than 16-bits");
551
552 // Initialize the flat scratch register pair.
553 // TODO: Can we use one s_mov_b64 here?
554
555 // Offset is in units of 256-bytes.
556 MachineBasicBlock &MBB = MF.front();
557 DebugLoc NoDL;
558 MachineBasicBlock::iterator Start = MBB.getFirstNonPHI();
559 const MCInstrDesc &SMovK = TII->get(AMDGPU::S_MOVK_I32);
560
Matt Arsenault77849922014-11-13 20:44:23 +0000561 assert(isInt<16>(StackOffset) && isInt<16>(StackSizeBytes));
562
Matt Arsenault3f981402014-09-15 15:41:53 +0000563 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_LO)
564 .addImm(StackOffset);
565
566 // Documentation says size is "per-thread scratch size in bytes"
567 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_HI)
568 .addImm(StackSizeBytes);
569 }
570
Tom Stellard75aadc22012-12-11 21:25:42 +0000571 return true;
572}