blob: a2dfc641d7585f355da916b7d2fef258bd468d86 [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 Stellardbe8ebee2013-01-18 21:15:50 +0000133 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellarde7b907d2012-12-19 22:10:33 +0000134 MBB = *MBB->succ_begin()) {
135
136 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
137 NumInstr < SkipThreshold && I != E; ++I) {
138
Nicolai Haehnlefa771812016-03-18 20:32:04 +0000139 if (I->isBundle() || !I->isBundled()) {
Nicolai Haehnleef160de2016-03-16 20:14:33 +0000140 // When a uniform loop is inside non-uniform control flow, the branch
141 // leaving the loop might be an S_CBRANCH_VCCNZ, which is never taken
142 // when EXEC = 0. We should skip the loop lest it becomes infinite.
143 if (I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ)
144 return true;
145
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000146 if (++NumInstr >= SkipThreshold)
147 return true;
Nicolai Haehnlefa771812016-03-18 20:32:04 +0000148 }
Tom Stellarde7b907d2012-12-19 22:10:33 +0000149 }
150 }
151
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000152 return false;
153}
154
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000155void SILowerControlFlow::Skip(MachineInstr &From, MachineOperand &To) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000156
157 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000158 return;
159
160 DebugLoc DL = From.getDebugLoc();
161 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000162 .addOperand(To);
Tom Stellarde7b907d2012-12-19 22:10:33 +0000163}
164
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000165void SILowerControlFlow::SkipIfDead(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000166
167 MachineBasicBlock &MBB = *MI.getParent();
168 DebugLoc DL = MI.getDebugLoc();
169
Matt Arsenault762af962014-07-13 03:06:39 +0000170 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000171 ShaderType::PIXEL ||
172 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000173 return;
174
175 MachineBasicBlock::iterator Insert = &MI;
176 ++Insert;
177
178 // If the exec mask is non-zero, skip the next two instructions
179 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000180 .addImm(3);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000181
182 // Exec mask is zero: Export to NULL target...
183 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
184 .addImm(0)
185 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
186 .addImm(0)
187 .addImm(1)
188 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000189 .addReg(AMDGPU::VGPR0)
190 .addReg(AMDGPU::VGPR0)
191 .addReg(AMDGPU::VGPR0)
192 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000193
194 // ... and terminate wavefront
195 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
196}
197
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000198void SILowerControlFlow::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000199 MachineBasicBlock &MBB = *MI.getParent();
200 DebugLoc DL = MI.getDebugLoc();
201 unsigned Reg = MI.getOperand(0).getReg();
202 unsigned Vcc = MI.getOperand(1).getReg();
203
204 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
205 .addReg(Vcc);
206
207 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
208 .addReg(AMDGPU::EXEC)
209 .addReg(Reg);
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
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000216void SILowerControlFlow::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000217 MachineBasicBlock &MBB = *MI.getParent();
218 DebugLoc DL = MI.getDebugLoc();
219 unsigned Dst = MI.getOperand(0).getReg();
220 unsigned Src = MI.getOperand(1).getReg();
221
Christian Konig6a9d3902013-03-26 14:03:44 +0000222 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
223 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000224 .addReg(Src); // Saved EXEC
225
226 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
227 .addReg(AMDGPU::EXEC)
228 .addReg(Dst);
229
Tom Stellarde7b907d2012-12-19 22:10:33 +0000230 Skip(MI, MI.getOperand(2));
231
Tom Stellardf8794352012-12-19 22:10:31 +0000232 MI.eraseFromParent();
233}
234
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000235void SILowerControlFlow::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000236 MachineBasicBlock &MBB = *MI.getParent();
237 DebugLoc DL = MI.getDebugLoc();
238
239 unsigned Dst = MI.getOperand(0).getReg();
240 unsigned Src = MI.getOperand(1).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000241
Tom Stellardf8794352012-12-19 22:10:31 +0000242 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
243 .addReg(AMDGPU::EXEC)
244 .addReg(Src);
245
246 MI.eraseFromParent();
247}
248
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000249void SILowerControlFlow::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000250 MachineBasicBlock &MBB = *MI.getParent();
251 DebugLoc DL = MI.getDebugLoc();
252
253 unsigned Dst = MI.getOperand(0).getReg();
254 unsigned Vcc = MI.getOperand(1).getReg();
255 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000256
Tom Stellardf8794352012-12-19 22:10:31 +0000257 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
258 .addReg(Vcc)
259 .addReg(Src);
260
261 MI.eraseFromParent();
262}
263
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000264void SILowerControlFlow::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000265 MachineBasicBlock &MBB = *MI.getParent();
266 DebugLoc DL = MI.getDebugLoc();
267
268 unsigned Dst = MI.getOperand(0).getReg();
269 unsigned Saved = MI.getOperand(1).getReg();
270 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000271
Tom Stellardf8794352012-12-19 22:10:31 +0000272 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
273 .addReg(Saved)
274 .addReg(Src);
275
276 MI.eraseFromParent();
277}
278
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000279void SILowerControlFlow::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000280 MachineBasicBlock &MBB = *MI.getParent();
281 DebugLoc DL = MI.getDebugLoc();
282 unsigned Src = MI.getOperand(0).getReg();
283
284 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
285 .addReg(AMDGPU::EXEC)
286 .addReg(Src);
287
288 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000289 .addOperand(MI.getOperand(1));
Tom Stellardf8794352012-12-19 22:10:31 +0000290
291 MI.eraseFromParent();
292}
293
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000294void SILowerControlFlow::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000295 MachineBasicBlock &MBB = *MI.getParent();
296 DebugLoc DL = MI.getDebugLoc();
297 unsigned Reg = MI.getOperand(0).getReg();
298
299 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
300 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
301 .addReg(AMDGPU::EXEC)
302 .addReg(Reg);
303
304 MI.eraseFromParent();
305}
306
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000307void SILowerControlFlow::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000308 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
309 MI.eraseFromParent();
310
311 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000312}
313
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000314void SILowerControlFlow::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000315 MachineBasicBlock &MBB = *MI.getParent();
316 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000317 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000318
Matt Arsenault762af962014-07-13 03:06:39 +0000319#ifndef NDEBUG
320 const SIMachineFunctionInfo *MFI
321 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
322 // Kill is only allowed in pixel / geometry shaders.
323 assert(MFI->getShaderType() == ShaderType::PIXEL ||
324 MFI->getShaderType() == ShaderType::GEOMETRY);
325#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000326
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000327 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000328 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000329 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000330 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000331 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
332 .addImm(0);
333 }
334 } else {
Matt Arsenault46359152015-08-08 00:41:48 +0000335 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000336 .addImm(0)
337 .addOperand(Op);
338 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000339
340 MI.eraseFromParent();
341}
342
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000343void SILowerControlFlow::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000344
345 MachineBasicBlock &MBB = *MI.getParent();
346 DebugLoc DL = MI.getDebugLoc();
347 MachineBasicBlock::iterator I = MI;
348
349 unsigned Save = MI.getOperand(1).getReg();
350 unsigned Idx = MI.getOperand(3).getReg();
351
352 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000353 if (Offset) {
354 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
355 .addReg(Idx)
356 .addImm(Offset);
357 } else {
358 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
359 .addReg(Idx);
360 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000361 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000362 } else {
363
364 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000365 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000366
367 // Save the EXEC mask
368 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
369 .addReg(AMDGPU::EXEC);
370
371 // Read the next variant into VCC (lower 32 bits) <- also loop target
372 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
373 AMDGPU::VCC_LO)
374 .addReg(Idx);
375
376 // Move index from VCC into M0
377 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
378 .addReg(AMDGPU::VCC_LO);
379
380 // Compare the just read M0 value to all possible Idx values
Matt Arsenault46359152015-08-08 00:41:48 +0000381 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
382 .addReg(AMDGPU::M0)
383 .addReg(Idx);
Tom Stellard89422762014-06-17 16:53:04 +0000384
385 // Update EXEC, save the original EXEC value to VCC
386 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
387 .addReg(AMDGPU::VCC);
388
Tom Stellard8b0182a2015-04-23 20:32:01 +0000389 if (Offset) {
390 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
391 .addReg(AMDGPU::M0)
392 .addImm(Offset);
393 }
Tom Stellard89422762014-06-17 16:53:04 +0000394 // Do the actual move
395 MBB.insert(I, MovRel);
396
397 // Update EXEC, switch all done bits to 0 and all todo bits to 1
398 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
399 .addReg(AMDGPU::EXEC)
400 .addReg(AMDGPU::VCC);
401
402 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
403 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000404 .addImm(-7);
Tom Stellard89422762014-06-17 16:53:04 +0000405
406 // Restore EXEC
407 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
408 .addReg(Save);
409
Christian Konig2989ffc2013-03-18 11:34:16 +0000410 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000411 MI.eraseFromParent();
412}
413
Tom Stellard8b0182a2015-04-23 20:32:01 +0000414/// \param @VecReg The register which holds element zero of the vector
415/// being addressed into.
416/// \param[out] @Reg The base register to use in the indirect addressing instruction.
417/// \param[in,out] @Offset As an input, this is the constant offset part of the
418// indirect Index. e.g. v0 = v[VecReg + Offset]
419// As an output, this is a constant value that needs
420// to be added to the value stored in M0.
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000421void SILowerControlFlow::computeIndirectRegAndOffset(unsigned VecReg,
422 unsigned &Reg,
423 int &Offset) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000424 unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
425 if (!SubReg)
426 SubReg = VecReg;
427
428 const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
429 int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
430
431 if (RegIdx < 0) {
432 Offset = RegIdx;
433 RegIdx = 0;
434 } else {
435 Offset = 0;
436 }
437
438 Reg = RC->getRegister(RegIdx);
439}
440
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000441void SILowerControlFlow::IndirectSrc(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000442
443 MachineBasicBlock &MBB = *MI.getParent();
444 DebugLoc DL = MI.getDebugLoc();
445
446 unsigned Dst = MI.getOperand(0).getReg();
447 unsigned Vec = MI.getOperand(2).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000448 int Off = MI.getOperand(4).getImm();
449 unsigned Reg;
450
451 computeIndirectRegAndOffset(Vec, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000452
Tom Stellard81d871d2013-11-13 23:36:50 +0000453 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000454 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard8b0182a2015-04-23 20:32:01 +0000455 .addReg(Reg)
Christian Konig2989ffc2013-03-18 11:34:16 +0000456 .addReg(Vec, RegState::Implicit);
457
Tom Stellard8b0182a2015-04-23 20:32:01 +0000458 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000459}
460
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000461void SILowerControlFlow::IndirectDst(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000462
463 MachineBasicBlock &MBB = *MI.getParent();
464 DebugLoc DL = MI.getDebugLoc();
465
466 unsigned Dst = MI.getOperand(0).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000467 int Off = MI.getOperand(4).getImm();
Christian Konig2989ffc2013-03-18 11:34:16 +0000468 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000469 unsigned Reg;
470
471 computeIndirectRegAndOffset(Dst, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000472
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000473 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000474 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard8b0182a2015-04-23 20:32:01 +0000475 .addReg(Reg, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000476 .addReg(Val)
Christian Konig2989ffc2013-03-18 11:34:16 +0000477 .addReg(Dst, RegState::Implicit);
478
Tom Stellard8b0182a2015-04-23 20:32:01 +0000479 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000480}
481
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000482bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000483 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
484 TRI =
485 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000486 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000487
488 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000489 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000490 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000491 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000492
Tom Stellardf8794352012-12-19 22:10:31 +0000493 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
494 BI != BE; ++BI) {
495
Marek Olsaked2213e2016-03-14 15:57:14 +0000496 MachineBasicBlock *EmptyMBBAtEnd = NULL;
Tom Stellardf8794352012-12-19 22:10:31 +0000497 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000498 MachineBasicBlock::iterator I, Next;
499 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000500 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000501
Tom Stellard75aadc22012-12-11 21:25:42 +0000502 MachineInstr &MI = *I;
Matt Arsenault3add6432015-10-20 04:35:43 +0000503 if (TII->isWQM(MI) || TII->isDS(MI))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000504 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000505
Matt Arsenault3f981402014-09-15 15:41:53 +0000506 // Flat uses m0 in case it needs to access LDS.
Matt Arsenault3add6432015-10-20 04:35:43 +0000507 if (TII->isFLAT(MI))
Matt Arsenault3f981402014-09-15 15:41:53 +0000508 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000509
Tom Stellard75aadc22012-12-11 21:25:42 +0000510 switch (MI.getOpcode()) {
511 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000512 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000513 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000514 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000515 break;
516
Tom Stellardf8794352012-12-19 22:10:31 +0000517 case AMDGPU::SI_ELSE:
518 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000519 break;
520
Tom Stellardf8794352012-12-19 22:10:31 +0000521 case AMDGPU::SI_BREAK:
522 Break(MI);
523 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000524
Tom Stellardf8794352012-12-19 22:10:31 +0000525 case AMDGPU::SI_IF_BREAK:
526 IfBreak(MI);
527 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000528
Tom Stellardf8794352012-12-19 22:10:31 +0000529 case AMDGPU::SI_ELSE_BREAK:
530 ElseBreak(MI);
531 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000532
Tom Stellardf8794352012-12-19 22:10:31 +0000533 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000534 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000535 Loop(MI);
536 break;
537
538 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000539 if (--Depth == 0 && HaveKill) {
540 SkipIfDead(MI);
541 HaveKill = false;
542 }
Tom Stellardf8794352012-12-19 22:10:31 +0000543 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000544 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000545
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000546 case AMDGPU::SI_KILL:
547 if (Depth == 0)
548 SkipIfDead(MI);
549 else
550 HaveKill = true;
551 Kill(MI);
552 break;
553
Tom Stellarde7b907d2012-12-19 22:10:33 +0000554 case AMDGPU::S_BRANCH:
555 Branch(MI);
556 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000557
Matt Arsenault28419272015-10-07 00:42:51 +0000558 case AMDGPU::SI_INDIRECT_SRC_V1:
559 case AMDGPU::SI_INDIRECT_SRC_V2:
560 case AMDGPU::SI_INDIRECT_SRC_V4:
561 case AMDGPU::SI_INDIRECT_SRC_V8:
562 case AMDGPU::SI_INDIRECT_SRC_V16:
Christian Konig2989ffc2013-03-18 11:34:16 +0000563 IndirectSrc(MI);
564 break;
565
Tom Stellard81d871d2013-11-13 23:36:50 +0000566 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000567 case AMDGPU::SI_INDIRECT_DST_V2:
568 case AMDGPU::SI_INDIRECT_DST_V4:
569 case AMDGPU::SI_INDIRECT_DST_V8:
570 case AMDGPU::SI_INDIRECT_DST_V16:
571 IndirectDst(MI);
572 break;
Marek Olsaked2213e2016-03-14 15:57:14 +0000573
574 case AMDGPU::S_ENDPGM: {
575 if (MF.getInfo<SIMachineFunctionInfo>()->returnsVoid())
576 break;
577
578 // Graphics shaders returning non-void shouldn't contain S_ENDPGM,
579 // because external bytecode will be appended at the end.
580 if (BI != --MF.end() || I != MBB.getFirstTerminator()) {
581 // S_ENDPGM is not the last instruction. Add an empty block at
582 // the end and jump there.
583 if (!EmptyMBBAtEnd) {
584 EmptyMBBAtEnd = MF.CreateMachineBasicBlock();
585 MF.insert(MF.end(), EmptyMBBAtEnd);
586 }
587
588 MBB.addSuccessor(EmptyMBBAtEnd);
589 BuildMI(*BI, I, MI.getDebugLoc(), TII->get(AMDGPU::S_BRANCH))
590 .addMBB(EmptyMBBAtEnd);
591 }
592
593 I->eraseFromParent();
594 break;
595 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000596 }
597 }
598 }
Tom Stellardf8794352012-12-19 22:10:31 +0000599
Matt Arsenault762af962014-07-13 03:06:39 +0000600 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000601 MachineBasicBlock &MBB = MF.front();
602 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
603 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
604 }
605
Matt Arsenault3f981402014-09-15 15:41:53 +0000606 if (NeedFlat && MFI->IsKernel) {
Matt Arsenault3f981402014-09-15 15:41:53 +0000607 // TODO: What to use with function calls?
Matt Arsenault296b8492016-02-12 06:31:30 +0000608 // We will need to Initialize the flat scratch register pair.
609 if (NeedFlat)
610 MFI->setHasFlatInstructions(true);
Matt Arsenault3f981402014-09-15 15:41:53 +0000611 }
612
Tom Stellard75aadc22012-12-11 21:25:42 +0000613 return true;
614}