blob: 37ba7eef3d6569ef5e8079bd525ea95b3df8be89 [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
139 if (I->isBundle() || !I->isBundled())
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000140 if (++NumInstr >= SkipThreshold)
141 return true;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000142 }
143 }
144
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000145 return false;
146}
147
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000148void SILowerControlFlow::Skip(MachineInstr &From, MachineOperand &To) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000149
150 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000151 return;
152
153 DebugLoc DL = From.getDebugLoc();
154 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000155 .addOperand(To);
Tom Stellarde7b907d2012-12-19 22:10:33 +0000156}
157
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000158void SILowerControlFlow::SkipIfDead(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000159
160 MachineBasicBlock &MBB = *MI.getParent();
161 DebugLoc DL = MI.getDebugLoc();
162
Matt Arsenault762af962014-07-13 03:06:39 +0000163 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000164 ShaderType::PIXEL ||
165 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000166 return;
167
168 MachineBasicBlock::iterator Insert = &MI;
169 ++Insert;
170
171 // If the exec mask is non-zero, skip the next two instructions
172 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000173 .addImm(3);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000174
175 // Exec mask is zero: Export to NULL target...
176 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
177 .addImm(0)
178 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
179 .addImm(0)
180 .addImm(1)
181 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000182 .addReg(AMDGPU::VGPR0)
183 .addReg(AMDGPU::VGPR0)
184 .addReg(AMDGPU::VGPR0)
185 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000186
187 // ... and terminate wavefront
188 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
189}
190
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000191void SILowerControlFlow::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000192 MachineBasicBlock &MBB = *MI.getParent();
193 DebugLoc DL = MI.getDebugLoc();
194 unsigned Reg = MI.getOperand(0).getReg();
195 unsigned Vcc = MI.getOperand(1).getReg();
196
197 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
198 .addReg(Vcc);
199
200 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
201 .addReg(AMDGPU::EXEC)
202 .addReg(Reg);
203
Tom Stellarde7b907d2012-12-19 22:10:33 +0000204 Skip(MI, MI.getOperand(2));
205
Tom Stellardf8794352012-12-19 22:10:31 +0000206 MI.eraseFromParent();
207}
208
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000209void SILowerControlFlow::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000210 MachineBasicBlock &MBB = *MI.getParent();
211 DebugLoc DL = MI.getDebugLoc();
212 unsigned Dst = MI.getOperand(0).getReg();
213 unsigned Src = MI.getOperand(1).getReg();
214
Christian Konig6a9d3902013-03-26 14:03:44 +0000215 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
216 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000217 .addReg(Src); // Saved EXEC
218
219 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
220 .addReg(AMDGPU::EXEC)
221 .addReg(Dst);
222
Tom Stellarde7b907d2012-12-19 22:10:33 +0000223 Skip(MI, MI.getOperand(2));
224
Tom Stellardf8794352012-12-19 22:10:31 +0000225 MI.eraseFromParent();
226}
227
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000228void SILowerControlFlow::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000229 MachineBasicBlock &MBB = *MI.getParent();
230 DebugLoc DL = MI.getDebugLoc();
231
232 unsigned Dst = MI.getOperand(0).getReg();
233 unsigned Src = MI.getOperand(1).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000234
Tom Stellardf8794352012-12-19 22:10:31 +0000235 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
236 .addReg(AMDGPU::EXEC)
237 .addReg(Src);
238
239 MI.eraseFromParent();
240}
241
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000242void SILowerControlFlow::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000243 MachineBasicBlock &MBB = *MI.getParent();
244 DebugLoc DL = MI.getDebugLoc();
245
246 unsigned Dst = MI.getOperand(0).getReg();
247 unsigned Vcc = MI.getOperand(1).getReg();
248 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000249
Tom Stellardf8794352012-12-19 22:10:31 +0000250 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
251 .addReg(Vcc)
252 .addReg(Src);
253
254 MI.eraseFromParent();
255}
256
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000257void SILowerControlFlow::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000258 MachineBasicBlock &MBB = *MI.getParent();
259 DebugLoc DL = MI.getDebugLoc();
260
261 unsigned Dst = MI.getOperand(0).getReg();
262 unsigned Saved = MI.getOperand(1).getReg();
263 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000264
Tom Stellardf8794352012-12-19 22:10:31 +0000265 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
266 .addReg(Saved)
267 .addReg(Src);
268
269 MI.eraseFromParent();
270}
271
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000272void SILowerControlFlow::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000273 MachineBasicBlock &MBB = *MI.getParent();
274 DebugLoc DL = MI.getDebugLoc();
275 unsigned Src = MI.getOperand(0).getReg();
276
277 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
278 .addReg(AMDGPU::EXEC)
279 .addReg(Src);
280
281 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000282 .addOperand(MI.getOperand(1));
Tom Stellardf8794352012-12-19 22:10:31 +0000283
284 MI.eraseFromParent();
285}
286
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000287void SILowerControlFlow::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000288 MachineBasicBlock &MBB = *MI.getParent();
289 DebugLoc DL = MI.getDebugLoc();
290 unsigned Reg = MI.getOperand(0).getReg();
291
292 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
293 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
294 .addReg(AMDGPU::EXEC)
295 .addReg(Reg);
296
297 MI.eraseFromParent();
298}
299
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000300void SILowerControlFlow::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000301 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
302 MI.eraseFromParent();
303
304 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000305}
306
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000307void SILowerControlFlow::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000308 MachineBasicBlock &MBB = *MI.getParent();
309 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000310 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000311
Matt Arsenault762af962014-07-13 03:06:39 +0000312#ifndef NDEBUG
313 const SIMachineFunctionInfo *MFI
314 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
315 // Kill is only allowed in pixel / geometry shaders.
316 assert(MFI->getShaderType() == ShaderType::PIXEL ||
317 MFI->getShaderType() == ShaderType::GEOMETRY);
318#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000319
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000320 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000321 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000322 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000323 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000324 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
325 .addImm(0);
326 }
327 } else {
Matt Arsenault46359152015-08-08 00:41:48 +0000328 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000329 .addImm(0)
330 .addOperand(Op);
331 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000332
333 MI.eraseFromParent();
334}
335
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000336void SILowerControlFlow::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000337
338 MachineBasicBlock &MBB = *MI.getParent();
339 DebugLoc DL = MI.getDebugLoc();
340 MachineBasicBlock::iterator I = MI;
341
342 unsigned Save = MI.getOperand(1).getReg();
343 unsigned Idx = MI.getOperand(3).getReg();
344
345 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000346 if (Offset) {
347 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
348 .addReg(Idx)
349 .addImm(Offset);
350 } else {
351 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
352 .addReg(Idx);
353 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000354 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000355 } else {
356
357 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000358 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000359
360 // Save the EXEC mask
361 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
362 .addReg(AMDGPU::EXEC);
363
364 // Read the next variant into VCC (lower 32 bits) <- also loop target
365 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
366 AMDGPU::VCC_LO)
367 .addReg(Idx);
368
369 // Move index from VCC into M0
370 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
371 .addReg(AMDGPU::VCC_LO);
372
373 // Compare the just read M0 value to all possible Idx values
Matt Arsenault46359152015-08-08 00:41:48 +0000374 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
375 .addReg(AMDGPU::M0)
376 .addReg(Idx);
Tom Stellard89422762014-06-17 16:53:04 +0000377
378 // Update EXEC, save the original EXEC value to VCC
379 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
380 .addReg(AMDGPU::VCC);
381
Tom Stellard8b0182a2015-04-23 20:32:01 +0000382 if (Offset) {
383 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
384 .addReg(AMDGPU::M0)
385 .addImm(Offset);
386 }
Tom Stellard89422762014-06-17 16:53:04 +0000387 // Do the actual move
388 MBB.insert(I, MovRel);
389
390 // Update EXEC, switch all done bits to 0 and all todo bits to 1
391 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
392 .addReg(AMDGPU::EXEC)
393 .addReg(AMDGPU::VCC);
394
395 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
396 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000397 .addImm(-7);
Tom Stellard89422762014-06-17 16:53:04 +0000398
399 // Restore EXEC
400 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
401 .addReg(Save);
402
Christian Konig2989ffc2013-03-18 11:34:16 +0000403 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000404 MI.eraseFromParent();
405}
406
Tom Stellard8b0182a2015-04-23 20:32:01 +0000407/// \param @VecReg The register which holds element zero of the vector
408/// being addressed into.
409/// \param[out] @Reg The base register to use in the indirect addressing instruction.
410/// \param[in,out] @Offset As an input, this is the constant offset part of the
411// indirect Index. e.g. v0 = v[VecReg + Offset]
412// As an output, this is a constant value that needs
413// to be added to the value stored in M0.
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000414void SILowerControlFlow::computeIndirectRegAndOffset(unsigned VecReg,
415 unsigned &Reg,
416 int &Offset) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000417 unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
418 if (!SubReg)
419 SubReg = VecReg;
420
421 const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
422 int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
423
424 if (RegIdx < 0) {
425 Offset = RegIdx;
426 RegIdx = 0;
427 } else {
428 Offset = 0;
429 }
430
431 Reg = RC->getRegister(RegIdx);
432}
433
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000434void SILowerControlFlow::IndirectSrc(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000435
436 MachineBasicBlock &MBB = *MI.getParent();
437 DebugLoc DL = MI.getDebugLoc();
438
439 unsigned Dst = MI.getOperand(0).getReg();
440 unsigned Vec = MI.getOperand(2).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000441 int Off = MI.getOperand(4).getImm();
442 unsigned Reg;
443
444 computeIndirectRegAndOffset(Vec, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000445
Tom Stellard81d871d2013-11-13 23:36:50 +0000446 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000447 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard8b0182a2015-04-23 20:32:01 +0000448 .addReg(Reg)
Christian Konig2989ffc2013-03-18 11:34:16 +0000449 .addReg(Vec, RegState::Implicit);
450
Tom Stellard8b0182a2015-04-23 20:32:01 +0000451 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000452}
453
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000454void SILowerControlFlow::IndirectDst(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000455
456 MachineBasicBlock &MBB = *MI.getParent();
457 DebugLoc DL = MI.getDebugLoc();
458
459 unsigned Dst = MI.getOperand(0).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000460 int Off = MI.getOperand(4).getImm();
Christian Konig2989ffc2013-03-18 11:34:16 +0000461 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000462 unsigned Reg;
463
464 computeIndirectRegAndOffset(Dst, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000465
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000466 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000467 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard8b0182a2015-04-23 20:32:01 +0000468 .addReg(Reg, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000469 .addReg(Val)
Christian Konig2989ffc2013-03-18 11:34:16 +0000470 .addReg(Dst, RegState::Implicit);
471
Tom Stellard8b0182a2015-04-23 20:32:01 +0000472 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000473}
474
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000475bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000476 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
477 TRI =
478 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000479 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000480
481 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000482 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000483 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000484 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000485
Tom Stellardf8794352012-12-19 22:10:31 +0000486 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
487 BI != BE; ++BI) {
488
489 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000490 MachineBasicBlock::iterator I, Next;
491 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000492 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000493
Tom Stellard75aadc22012-12-11 21:25:42 +0000494 MachineInstr &MI = *I;
Matt Arsenault3add6432015-10-20 04:35:43 +0000495 if (TII->isWQM(MI) || TII->isDS(MI))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000496 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000497
Matt Arsenault3f981402014-09-15 15:41:53 +0000498 // Flat uses m0 in case it needs to access LDS.
Matt Arsenault3add6432015-10-20 04:35:43 +0000499 if (TII->isFLAT(MI))
Matt Arsenault3f981402014-09-15 15:41:53 +0000500 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000501
Tom Stellard75aadc22012-12-11 21:25:42 +0000502 switch (MI.getOpcode()) {
503 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000504 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000505 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000506 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000507 break;
508
Tom Stellardf8794352012-12-19 22:10:31 +0000509 case AMDGPU::SI_ELSE:
510 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000511 break;
512
Tom Stellardf8794352012-12-19 22:10:31 +0000513 case AMDGPU::SI_BREAK:
514 Break(MI);
515 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000516
Tom Stellardf8794352012-12-19 22:10:31 +0000517 case AMDGPU::SI_IF_BREAK:
518 IfBreak(MI);
519 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000520
Tom Stellardf8794352012-12-19 22:10:31 +0000521 case AMDGPU::SI_ELSE_BREAK:
522 ElseBreak(MI);
523 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000524
Tom Stellardf8794352012-12-19 22:10:31 +0000525 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000526 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000527 Loop(MI);
528 break;
529
530 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000531 if (--Depth == 0 && HaveKill) {
532 SkipIfDead(MI);
533 HaveKill = false;
534 }
Tom Stellardf8794352012-12-19 22:10:31 +0000535 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000536 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000537
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000538 case AMDGPU::SI_KILL:
539 if (Depth == 0)
540 SkipIfDead(MI);
541 else
542 HaveKill = true;
543 Kill(MI);
544 break;
545
Tom Stellarde7b907d2012-12-19 22:10:33 +0000546 case AMDGPU::S_BRANCH:
547 Branch(MI);
548 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000549
Matt Arsenault28419272015-10-07 00:42:51 +0000550 case AMDGPU::SI_INDIRECT_SRC_V1:
551 case AMDGPU::SI_INDIRECT_SRC_V2:
552 case AMDGPU::SI_INDIRECT_SRC_V4:
553 case AMDGPU::SI_INDIRECT_SRC_V8:
554 case AMDGPU::SI_INDIRECT_SRC_V16:
Christian Konig2989ffc2013-03-18 11:34:16 +0000555 IndirectSrc(MI);
556 break;
557
Tom Stellard81d871d2013-11-13 23:36:50 +0000558 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000559 case AMDGPU::SI_INDIRECT_DST_V2:
560 case AMDGPU::SI_INDIRECT_DST_V4:
561 case AMDGPU::SI_INDIRECT_DST_V8:
562 case AMDGPU::SI_INDIRECT_DST_V16:
563 IndirectDst(MI);
564 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000565 }
566 }
567 }
Tom Stellardf8794352012-12-19 22:10:31 +0000568
Matt Arsenault762af962014-07-13 03:06:39 +0000569 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000570 MachineBasicBlock &MBB = MF.front();
571 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
572 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
573 }
574
Matt Arsenault3f981402014-09-15 15:41:53 +0000575 // FIXME: This seems inappropriate to do here.
576 if (NeedFlat && MFI->IsKernel) {
577 // Insert the prologue initializing the SGPRs pointing to the scratch space
578 // for flat accesses.
579 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
580
581 // TODO: What to use with function calls?
582
583 // FIXME: This is reporting stack size that is used in a scratch buffer
584 // rather than registers as well.
585 uint64_t StackSizeBytes = FrameInfo->getStackSize();
586
587 int IndirectBegin
588 = static_cast<const AMDGPUInstrInfo*>(TII)->getIndirectIndexBegin(MF);
589 // Convert register index to 256-byte unit.
590 uint64_t StackOffset = IndirectBegin < 0 ? 0 : (4 * IndirectBegin / 256);
591
592 assert((StackSizeBytes < 0xffff) && StackOffset < 0xffff &&
593 "Stack limits should be smaller than 16-bits");
594
595 // Initialize the flat scratch register pair.
596 // TODO: Can we use one s_mov_b64 here?
597
598 // Offset is in units of 256-bytes.
599 MachineBasicBlock &MBB = MF.front();
600 DebugLoc NoDL;
601 MachineBasicBlock::iterator Start = MBB.getFirstNonPHI();
602 const MCInstrDesc &SMovK = TII->get(AMDGPU::S_MOVK_I32);
603
Matt Arsenault77849922014-11-13 20:44:23 +0000604 assert(isInt<16>(StackOffset) && isInt<16>(StackSizeBytes));
605
Matt Arsenault3f981402014-09-15 15:41:53 +0000606 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_LO)
607 .addImm(StackOffset);
608
609 // Documentation says size is "per-thread scratch size in bytes"
610 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_HI)
611 .addImm(StackSizeBytes);
612 }
613
Tom Stellard75aadc22012-12-11 21:25:42 +0000614 return true;
615}