blob: a804a5e6d32dec141ee1a05d59b482c941cbd999 [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
Matt Arsenault55d49cf2016-02-12 02:16:10 +000064#define DEBUG_TYPE "si-lower-control-flow"
65
Tom Stellard75aadc22012-12-11 21:25:42 +000066namespace {
67
Matt Arsenault55d49cf2016-02-12 02:16:10 +000068class SILowerControlFlow : public MachineFunctionPass {
Tom Stellard75aadc22012-12-11 21:25:42 +000069private:
Tom Stellarde7b907d2012-12-19 22:10:33 +000070 static const unsigned SkipThreshold = 12;
71
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
Tom Stellard8b0182a2015-04-23 20:32:01 +000091 void LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset = 0);
92 void computeIndirectRegAndOffset(unsigned VecReg, unsigned &Reg, int &Offset);
Christian Konig2989ffc2013-03-18 11:34:16 +000093 void IndirectSrc(MachineInstr &MI);
94 void IndirectDst(MachineInstr &MI);
95
Tom Stellard75aadc22012-12-11 21:25:42 +000096public:
Matt Arsenault55d49cf2016-02-12 02:16:10 +000097 static char ID;
98
99 SILowerControlFlow() :
Craig Topper062a2ba2014-04-25 05:30:21 +0000100 MachineFunctionPass(ID), TRI(nullptr), TII(nullptr) { }
Tom Stellard75aadc22012-12-11 21:25:42 +0000101
Craig Topper5656db42014-04-29 07:57:24 +0000102 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000103
Craig Topper5656db42014-04-29 07:57:24 +0000104 const char *getPassName() const override {
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000105 return "SI Lower control flow pseudo instructions";
Tom Stellard75aadc22012-12-11 21:25:42 +0000106 }
107
Matt Arsenault0cb85172015-09-25 17:21:28 +0000108 void getAnalysisUsage(AnalysisUsage &AU) const override {
109 AU.setPreservesCFG();
110 MachineFunctionPass::getAnalysisUsage(AU);
111 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000112};
113
114} // End anonymous namespace
115
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000116char SILowerControlFlow::ID = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000117
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000118INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
119 "SI lower control flow", false, false)
120
121char &llvm::SILowerControlFlowPassID = SILowerControlFlow::ID;
122
123
124FunctionPass *llvm::createSILowerControlFlowPass() {
125 return new SILowerControlFlow();
Tom Stellard75aadc22012-12-11 21:25:42 +0000126}
127
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000128bool SILowerControlFlow::shouldSkip(MachineBasicBlock *From,
129 MachineBasicBlock *To) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000130
Tom Stellarde7b907d2012-12-19 22:10:33 +0000131 unsigned NumInstr = 0;
132
Tom Stellard92339e82016-03-21 18:56:58 +0000133 for (MachineFunction::iterator MBBI = MachineFunction::iterator(From),
134 ToI = MachineFunction::iterator(To); MBBI != ToI; ++MBBI) {
Tom Stellarde7b907d2012-12-19 22:10:33 +0000135
Tom Stellard92339e82016-03-21 18:56:58 +0000136 MachineBasicBlock &MBB = *MBBI;
137
138 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
Tom Stellarde7b907d2012-12-19 22:10:33 +0000139 NumInstr < SkipThreshold && I != E; ++I) {
140
Nicolai Haehnlefa771812016-03-18 20:32:04 +0000141 if (I->isBundle() || !I->isBundled()) {
Nicolai Haehnleef160de2016-03-16 20:14:33 +0000142 // When a uniform loop is inside non-uniform control flow, the branch
143 // leaving the loop might be an S_CBRANCH_VCCNZ, which is never taken
144 // when EXEC = 0. We should skip the loop lest it becomes infinite.
145 if (I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ)
146 return true;
147
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000148 if (++NumInstr >= SkipThreshold)
149 return true;
Nicolai Haehnlefa771812016-03-18 20:32:04 +0000150 }
Tom Stellarde7b907d2012-12-19 22:10:33 +0000151 }
152 }
153
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000154 return false;
155}
156
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000157void SILowerControlFlow::Skip(MachineInstr &From, MachineOperand &To) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000158
159 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000160 return;
161
162 DebugLoc DL = From.getDebugLoc();
163 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000164 .addOperand(To);
Tom Stellarde7b907d2012-12-19 22:10:33 +0000165}
166
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000167void SILowerControlFlow::SkipIfDead(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000168
169 MachineBasicBlock &MBB = *MI.getParent();
170 DebugLoc DL = MI.getDebugLoc();
171
Matt Arsenault762af962014-07-13 03:06:39 +0000172 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000173 ShaderType::PIXEL ||
174 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000175 return;
176
177 MachineBasicBlock::iterator Insert = &MI;
178 ++Insert;
179
180 // If the exec mask is non-zero, skip the next two instructions
181 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000182 .addImm(3);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000183
184 // Exec mask is zero: Export to NULL target...
185 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
186 .addImm(0)
187 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
188 .addImm(0)
189 .addImm(1)
190 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000191 .addReg(AMDGPU::VGPR0)
192 .addReg(AMDGPU::VGPR0)
193 .addReg(AMDGPU::VGPR0)
194 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000195
196 // ... and terminate wavefront
197 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
198}
199
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000200void SILowerControlFlow::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000201 MachineBasicBlock &MBB = *MI.getParent();
202 DebugLoc DL = MI.getDebugLoc();
203 unsigned Reg = MI.getOperand(0).getReg();
204 unsigned Vcc = MI.getOperand(1).getReg();
205
206 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
207 .addReg(Vcc);
208
209 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
210 .addReg(AMDGPU::EXEC)
211 .addReg(Reg);
212
Tom Stellarde7b907d2012-12-19 22:10:33 +0000213 Skip(MI, MI.getOperand(2));
214
Tom Stellardf8794352012-12-19 22:10:31 +0000215 MI.eraseFromParent();
216}
217
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000218void SILowerControlFlow::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000219 MachineBasicBlock &MBB = *MI.getParent();
220 DebugLoc DL = MI.getDebugLoc();
221 unsigned Dst = MI.getOperand(0).getReg();
222 unsigned Src = MI.getOperand(1).getReg();
223
Christian Konig6a9d3902013-03-26 14:03:44 +0000224 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
225 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000226 .addReg(Src); // Saved EXEC
227
228 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
229 .addReg(AMDGPU::EXEC)
230 .addReg(Dst);
231
Tom Stellarde7b907d2012-12-19 22:10:33 +0000232 Skip(MI, MI.getOperand(2));
233
Tom Stellardf8794352012-12-19 22:10:31 +0000234 MI.eraseFromParent();
235}
236
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000237void SILowerControlFlow::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000238 MachineBasicBlock &MBB = *MI.getParent();
239 DebugLoc DL = MI.getDebugLoc();
240
241 unsigned Dst = MI.getOperand(0).getReg();
242 unsigned Src = MI.getOperand(1).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000243
Tom Stellardf8794352012-12-19 22:10:31 +0000244 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
245 .addReg(AMDGPU::EXEC)
246 .addReg(Src);
247
248 MI.eraseFromParent();
249}
250
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000251void SILowerControlFlow::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000252 MachineBasicBlock &MBB = *MI.getParent();
253 DebugLoc DL = MI.getDebugLoc();
254
255 unsigned Dst = MI.getOperand(0).getReg();
256 unsigned Vcc = MI.getOperand(1).getReg();
257 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000258
Tom Stellardf8794352012-12-19 22:10:31 +0000259 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
260 .addReg(Vcc)
261 .addReg(Src);
262
263 MI.eraseFromParent();
264}
265
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000266void SILowerControlFlow::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000267 MachineBasicBlock &MBB = *MI.getParent();
268 DebugLoc DL = MI.getDebugLoc();
269
270 unsigned Dst = MI.getOperand(0).getReg();
271 unsigned Saved = MI.getOperand(1).getReg();
272 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000273
Tom Stellardf8794352012-12-19 22:10:31 +0000274 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
275 .addReg(Saved)
276 .addReg(Src);
277
278 MI.eraseFromParent();
279}
280
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000281void SILowerControlFlow::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000282 MachineBasicBlock &MBB = *MI.getParent();
283 DebugLoc DL = MI.getDebugLoc();
284 unsigned Src = MI.getOperand(0).getReg();
285
286 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
287 .addReg(AMDGPU::EXEC)
288 .addReg(Src);
289
290 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000291 .addOperand(MI.getOperand(1));
Tom Stellardf8794352012-12-19 22:10:31 +0000292
293 MI.eraseFromParent();
294}
295
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000296void SILowerControlFlow::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000297 MachineBasicBlock &MBB = *MI.getParent();
298 DebugLoc DL = MI.getDebugLoc();
299 unsigned Reg = MI.getOperand(0).getReg();
300
301 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
302 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
303 .addReg(AMDGPU::EXEC)
304 .addReg(Reg);
305
306 MI.eraseFromParent();
307}
308
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000309void SILowerControlFlow::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000310 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
311 MI.eraseFromParent();
312
313 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000314}
315
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000316void SILowerControlFlow::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000317 MachineBasicBlock &MBB = *MI.getParent();
318 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000319 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000320
Matt Arsenault762af962014-07-13 03:06:39 +0000321#ifndef NDEBUG
322 const SIMachineFunctionInfo *MFI
323 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
324 // Kill is only allowed in pixel / geometry shaders.
325 assert(MFI->getShaderType() == ShaderType::PIXEL ||
326 MFI->getShaderType() == ShaderType::GEOMETRY);
327#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000328
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000329 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000330 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000331 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000332 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000333 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
334 .addImm(0);
335 }
336 } else {
Matt Arsenault46359152015-08-08 00:41:48 +0000337 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000338 .addImm(0)
339 .addOperand(Op);
340 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000341
342 MI.eraseFromParent();
343}
344
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000345void SILowerControlFlow::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000346
347 MachineBasicBlock &MBB = *MI.getParent();
348 DebugLoc DL = MI.getDebugLoc();
349 MachineBasicBlock::iterator I = MI;
350
351 unsigned Save = MI.getOperand(1).getReg();
352 unsigned Idx = MI.getOperand(3).getReg();
353
354 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000355 if (Offset) {
356 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
357 .addReg(Idx)
358 .addImm(Offset);
359 } else {
360 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
361 .addReg(Idx);
362 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000363 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000364 } else {
365
366 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000367 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000368
369 // Save the EXEC mask
370 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
371 .addReg(AMDGPU::EXEC);
372
373 // Read the next variant into VCC (lower 32 bits) <- also loop target
374 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
375 AMDGPU::VCC_LO)
376 .addReg(Idx);
377
378 // Move index from VCC into M0
379 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
380 .addReg(AMDGPU::VCC_LO);
381
382 // Compare the just read M0 value to all possible Idx values
Matt Arsenault46359152015-08-08 00:41:48 +0000383 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
384 .addReg(AMDGPU::M0)
385 .addReg(Idx);
Tom Stellard89422762014-06-17 16:53:04 +0000386
387 // Update EXEC, save the original EXEC value to VCC
388 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
389 .addReg(AMDGPU::VCC);
390
Tom Stellard8b0182a2015-04-23 20:32:01 +0000391 if (Offset) {
392 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
393 .addReg(AMDGPU::M0)
394 .addImm(Offset);
395 }
Tom Stellard89422762014-06-17 16:53:04 +0000396 // Do the actual move
397 MBB.insert(I, MovRel);
398
399 // Update EXEC, switch all done bits to 0 and all todo bits to 1
400 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
401 .addReg(AMDGPU::EXEC)
402 .addReg(AMDGPU::VCC);
403
404 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
405 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000406 .addImm(-7);
Tom Stellard89422762014-06-17 16:53:04 +0000407
408 // Restore EXEC
409 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
410 .addReg(Save);
411
Christian Konig2989ffc2013-03-18 11:34:16 +0000412 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000413 MI.eraseFromParent();
414}
415
Tom Stellard8b0182a2015-04-23 20:32:01 +0000416/// \param @VecReg The register which holds element zero of the vector
417/// being addressed into.
418/// \param[out] @Reg The base register to use in the indirect addressing instruction.
419/// \param[in,out] @Offset As an input, this is the constant offset part of the
420// indirect Index. e.g. v0 = v[VecReg + Offset]
421// As an output, this is a constant value that needs
422// to be added to the value stored in M0.
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000423void SILowerControlFlow::computeIndirectRegAndOffset(unsigned VecReg,
424 unsigned &Reg,
425 int &Offset) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000426 unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
427 if (!SubReg)
428 SubReg = VecReg;
429
430 const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
431 int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
432
433 if (RegIdx < 0) {
434 Offset = RegIdx;
435 RegIdx = 0;
436 } else {
437 Offset = 0;
438 }
439
440 Reg = RC->getRegister(RegIdx);
441}
442
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000443void SILowerControlFlow::IndirectSrc(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000444
445 MachineBasicBlock &MBB = *MI.getParent();
446 DebugLoc DL = MI.getDebugLoc();
447
448 unsigned Dst = MI.getOperand(0).getReg();
449 unsigned Vec = MI.getOperand(2).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000450 int Off = MI.getOperand(4).getImm();
451 unsigned Reg;
452
453 computeIndirectRegAndOffset(Vec, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000454
Tom Stellard81d871d2013-11-13 23:36:50 +0000455 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000456 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard8b0182a2015-04-23 20:32:01 +0000457 .addReg(Reg)
Christian Konig2989ffc2013-03-18 11:34:16 +0000458 .addReg(Vec, RegState::Implicit);
459
Tom Stellard8b0182a2015-04-23 20:32:01 +0000460 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000461}
462
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000463void SILowerControlFlow::IndirectDst(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000464
465 MachineBasicBlock &MBB = *MI.getParent();
466 DebugLoc DL = MI.getDebugLoc();
467
468 unsigned Dst = MI.getOperand(0).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000469 int Off = MI.getOperand(4).getImm();
Christian Konig2989ffc2013-03-18 11:34:16 +0000470 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000471 unsigned Reg;
472
473 computeIndirectRegAndOffset(Dst, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000474
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000475 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000476 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard8b0182a2015-04-23 20:32:01 +0000477 .addReg(Reg, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000478 .addReg(Val)
Christian Konig2989ffc2013-03-18 11:34:16 +0000479 .addReg(Dst, RegState::Implicit);
480
Tom Stellard8b0182a2015-04-23 20:32:01 +0000481 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000482}
483
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000484bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000485 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
486 TRI =
487 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000488 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000489
490 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000491 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000492 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000493 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000494
Tom Stellardf8794352012-12-19 22:10:31 +0000495 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
496 BI != BE; ++BI) {
497
Marek Olsaked2213e2016-03-14 15:57:14 +0000498 MachineBasicBlock *EmptyMBBAtEnd = NULL;
Tom Stellardf8794352012-12-19 22:10:31 +0000499 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000500 MachineBasicBlock::iterator I, Next;
501 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000502 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000503
Tom Stellard75aadc22012-12-11 21:25:42 +0000504 MachineInstr &MI = *I;
Matt Arsenault3add6432015-10-20 04:35:43 +0000505 if (TII->isWQM(MI) || TII->isDS(MI))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000506 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000507
Matt Arsenault3f981402014-09-15 15:41:53 +0000508 // Flat uses m0 in case it needs to access LDS.
Matt Arsenault3add6432015-10-20 04:35:43 +0000509 if (TII->isFLAT(MI))
Matt Arsenault3f981402014-09-15 15:41:53 +0000510 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000511
Tom Stellard75aadc22012-12-11 21:25:42 +0000512 switch (MI.getOpcode()) {
513 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000514 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000515 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000516 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000517 break;
518
Tom Stellardf8794352012-12-19 22:10:31 +0000519 case AMDGPU::SI_ELSE:
520 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000521 break;
522
Tom Stellardf8794352012-12-19 22:10:31 +0000523 case AMDGPU::SI_BREAK:
524 Break(MI);
525 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000526
Tom Stellardf8794352012-12-19 22:10:31 +0000527 case AMDGPU::SI_IF_BREAK:
528 IfBreak(MI);
529 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000530
Tom Stellardf8794352012-12-19 22:10:31 +0000531 case AMDGPU::SI_ELSE_BREAK:
532 ElseBreak(MI);
533 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000534
Tom Stellardf8794352012-12-19 22:10:31 +0000535 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000536 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000537 Loop(MI);
538 break;
539
540 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000541 if (--Depth == 0 && HaveKill) {
542 SkipIfDead(MI);
543 HaveKill = false;
544 }
Tom Stellardf8794352012-12-19 22:10:31 +0000545 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000546 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000547
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000548 case AMDGPU::SI_KILL:
549 if (Depth == 0)
550 SkipIfDead(MI);
551 else
552 HaveKill = true;
553 Kill(MI);
554 break;
555
Tom Stellarde7b907d2012-12-19 22:10:33 +0000556 case AMDGPU::S_BRANCH:
557 Branch(MI);
558 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000559
Matt Arsenault28419272015-10-07 00:42:51 +0000560 case AMDGPU::SI_INDIRECT_SRC_V1:
561 case AMDGPU::SI_INDIRECT_SRC_V2:
562 case AMDGPU::SI_INDIRECT_SRC_V4:
563 case AMDGPU::SI_INDIRECT_SRC_V8:
564 case AMDGPU::SI_INDIRECT_SRC_V16:
Christian Konig2989ffc2013-03-18 11:34:16 +0000565 IndirectSrc(MI);
566 break;
567
Tom Stellard81d871d2013-11-13 23:36:50 +0000568 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000569 case AMDGPU::SI_INDIRECT_DST_V2:
570 case AMDGPU::SI_INDIRECT_DST_V4:
571 case AMDGPU::SI_INDIRECT_DST_V8:
572 case AMDGPU::SI_INDIRECT_DST_V16:
573 IndirectDst(MI);
574 break;
Marek Olsaked2213e2016-03-14 15:57:14 +0000575
576 case AMDGPU::S_ENDPGM: {
577 if (MF.getInfo<SIMachineFunctionInfo>()->returnsVoid())
578 break;
579
580 // Graphics shaders returning non-void shouldn't contain S_ENDPGM,
581 // because external bytecode will be appended at the end.
582 if (BI != --MF.end() || I != MBB.getFirstTerminator()) {
583 // S_ENDPGM is not the last instruction. Add an empty block at
584 // the end and jump there.
585 if (!EmptyMBBAtEnd) {
586 EmptyMBBAtEnd = MF.CreateMachineBasicBlock();
587 MF.insert(MF.end(), EmptyMBBAtEnd);
588 }
589
590 MBB.addSuccessor(EmptyMBBAtEnd);
591 BuildMI(*BI, I, MI.getDebugLoc(), TII->get(AMDGPU::S_BRANCH))
592 .addMBB(EmptyMBBAtEnd);
593 }
594
595 I->eraseFromParent();
596 break;
597 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000598 }
599 }
600 }
Tom Stellardf8794352012-12-19 22:10:31 +0000601
Matt Arsenault762af962014-07-13 03:06:39 +0000602 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000603 MachineBasicBlock &MBB = MF.front();
604 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
605 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
606 }
607
Matt Arsenault3f981402014-09-15 15:41:53 +0000608 if (NeedFlat && MFI->IsKernel) {
Matt Arsenault3f981402014-09-15 15:41:53 +0000609 // TODO: What to use with function calls?
Matt Arsenault296b8492016-02-12 06:31:30 +0000610 // We will need to Initialize the flat scratch register pair.
611 if (NeedFlat)
612 MFI->setHasFlatInstructions(true);
Matt Arsenault3f981402014-09-15 15:41:53 +0000613 }
614
Tom Stellard75aadc22012-12-11 21:25:42 +0000615 return true;
616}