blob: 2a645d15ca2a9eda71cc406256094e114f98a8c6 [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())
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;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000148 }
149 }
150
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000151 return false;
152}
153
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000154void SILowerControlFlow::Skip(MachineInstr &From, MachineOperand &To) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000155
156 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000157 return;
158
159 DebugLoc DL = From.getDebugLoc();
160 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000161 .addOperand(To);
Tom Stellarde7b907d2012-12-19 22:10:33 +0000162}
163
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000164void SILowerControlFlow::SkipIfDead(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000165
166 MachineBasicBlock &MBB = *MI.getParent();
167 DebugLoc DL = MI.getDebugLoc();
168
Matt Arsenault762af962014-07-13 03:06:39 +0000169 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000170 ShaderType::PIXEL ||
171 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000172 return;
173
174 MachineBasicBlock::iterator Insert = &MI;
175 ++Insert;
176
177 // If the exec mask is non-zero, skip the next two instructions
178 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000179 .addImm(3);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000180
181 // Exec mask is zero: Export to NULL target...
182 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
183 .addImm(0)
184 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
185 .addImm(0)
186 .addImm(1)
187 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000188 .addReg(AMDGPU::VGPR0)
189 .addReg(AMDGPU::VGPR0)
190 .addReg(AMDGPU::VGPR0)
191 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000192
193 // ... and terminate wavefront
194 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
195}
196
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000197void SILowerControlFlow::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000198 MachineBasicBlock &MBB = *MI.getParent();
199 DebugLoc DL = MI.getDebugLoc();
200 unsigned Reg = MI.getOperand(0).getReg();
201 unsigned Vcc = MI.getOperand(1).getReg();
202
203 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
204 .addReg(Vcc);
205
206 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
207 .addReg(AMDGPU::EXEC)
208 .addReg(Reg);
209
Tom Stellarde7b907d2012-12-19 22:10:33 +0000210 Skip(MI, MI.getOperand(2));
211
Tom Stellardf8794352012-12-19 22:10:31 +0000212 MI.eraseFromParent();
213}
214
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000215void SILowerControlFlow::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000216 MachineBasicBlock &MBB = *MI.getParent();
217 DebugLoc DL = MI.getDebugLoc();
218 unsigned Dst = MI.getOperand(0).getReg();
219 unsigned Src = MI.getOperand(1).getReg();
220
Christian Konig6a9d3902013-03-26 14:03:44 +0000221 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
222 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000223 .addReg(Src); // Saved EXEC
224
225 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
226 .addReg(AMDGPU::EXEC)
227 .addReg(Dst);
228
Tom Stellarde7b907d2012-12-19 22:10:33 +0000229 Skip(MI, MI.getOperand(2));
230
Tom Stellardf8794352012-12-19 22:10:31 +0000231 MI.eraseFromParent();
232}
233
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000234void SILowerControlFlow::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000235 MachineBasicBlock &MBB = *MI.getParent();
236 DebugLoc DL = MI.getDebugLoc();
237
238 unsigned Dst = MI.getOperand(0).getReg();
239 unsigned Src = MI.getOperand(1).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000240
Tom Stellardf8794352012-12-19 22:10:31 +0000241 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
242 .addReg(AMDGPU::EXEC)
243 .addReg(Src);
244
245 MI.eraseFromParent();
246}
247
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000248void SILowerControlFlow::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000249 MachineBasicBlock &MBB = *MI.getParent();
250 DebugLoc DL = MI.getDebugLoc();
251
252 unsigned Dst = MI.getOperand(0).getReg();
253 unsigned Vcc = MI.getOperand(1).getReg();
254 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000255
Tom Stellardf8794352012-12-19 22:10:31 +0000256 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
257 .addReg(Vcc)
258 .addReg(Src);
259
260 MI.eraseFromParent();
261}
262
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000263void SILowerControlFlow::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000264 MachineBasicBlock &MBB = *MI.getParent();
265 DebugLoc DL = MI.getDebugLoc();
266
267 unsigned Dst = MI.getOperand(0).getReg();
268 unsigned Saved = MI.getOperand(1).getReg();
269 unsigned Src = MI.getOperand(2).getReg();
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000270
Tom Stellardf8794352012-12-19 22:10:31 +0000271 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
272 .addReg(Saved)
273 .addReg(Src);
274
275 MI.eraseFromParent();
276}
277
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000278void SILowerControlFlow::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000279 MachineBasicBlock &MBB = *MI.getParent();
280 DebugLoc DL = MI.getDebugLoc();
281 unsigned Src = MI.getOperand(0).getReg();
282
283 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
284 .addReg(AMDGPU::EXEC)
285 .addReg(Src);
286
287 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000288 .addOperand(MI.getOperand(1));
Tom Stellardf8794352012-12-19 22:10:31 +0000289
290 MI.eraseFromParent();
291}
292
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000293void SILowerControlFlow::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000294 MachineBasicBlock &MBB = *MI.getParent();
295 DebugLoc DL = MI.getDebugLoc();
296 unsigned Reg = MI.getOperand(0).getReg();
297
298 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
299 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
300 .addReg(AMDGPU::EXEC)
301 .addReg(Reg);
302
303 MI.eraseFromParent();
304}
305
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000306void SILowerControlFlow::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000307 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
308 MI.eraseFromParent();
309
310 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000311}
312
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000313void SILowerControlFlow::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000314 MachineBasicBlock &MBB = *MI.getParent();
315 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000316 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000317
Matt Arsenault762af962014-07-13 03:06:39 +0000318#ifndef NDEBUG
319 const SIMachineFunctionInfo *MFI
320 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
321 // Kill is only allowed in pixel / geometry shaders.
322 assert(MFI->getShaderType() == ShaderType::PIXEL ||
323 MFI->getShaderType() == ShaderType::GEOMETRY);
324#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000325
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000326 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000327 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000328 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000329 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000330 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
331 .addImm(0);
332 }
333 } else {
Matt Arsenault46359152015-08-08 00:41:48 +0000334 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000335 .addImm(0)
336 .addOperand(Op);
337 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000338
339 MI.eraseFromParent();
340}
341
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000342void SILowerControlFlow::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000343
344 MachineBasicBlock &MBB = *MI.getParent();
345 DebugLoc DL = MI.getDebugLoc();
346 MachineBasicBlock::iterator I = MI;
347
348 unsigned Save = MI.getOperand(1).getReg();
349 unsigned Idx = MI.getOperand(3).getReg();
350
351 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000352 if (Offset) {
353 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
354 .addReg(Idx)
355 .addImm(Offset);
356 } else {
357 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
358 .addReg(Idx);
359 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000360 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000361 } else {
362
363 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000364 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000365
366 // Save the EXEC mask
367 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
368 .addReg(AMDGPU::EXEC);
369
370 // Read the next variant into VCC (lower 32 bits) <- also loop target
371 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
372 AMDGPU::VCC_LO)
373 .addReg(Idx);
374
375 // Move index from VCC into M0
376 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
377 .addReg(AMDGPU::VCC_LO);
378
379 // Compare the just read M0 value to all possible Idx values
Matt Arsenault46359152015-08-08 00:41:48 +0000380 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
381 .addReg(AMDGPU::M0)
382 .addReg(Idx);
Tom Stellard89422762014-06-17 16:53:04 +0000383
384 // Update EXEC, save the original EXEC value to VCC
385 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
386 .addReg(AMDGPU::VCC);
387
Tom Stellard8b0182a2015-04-23 20:32:01 +0000388 if (Offset) {
389 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
390 .addReg(AMDGPU::M0)
391 .addImm(Offset);
392 }
Tom Stellard89422762014-06-17 16:53:04 +0000393 // Do the actual move
394 MBB.insert(I, MovRel);
395
396 // Update EXEC, switch all done bits to 0 and all todo bits to 1
397 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
398 .addReg(AMDGPU::EXEC)
399 .addReg(AMDGPU::VCC);
400
401 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
402 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000403 .addImm(-7);
Tom Stellard89422762014-06-17 16:53:04 +0000404
405 // Restore EXEC
406 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
407 .addReg(Save);
408
Christian Konig2989ffc2013-03-18 11:34:16 +0000409 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000410 MI.eraseFromParent();
411}
412
Tom Stellard8b0182a2015-04-23 20:32:01 +0000413/// \param @VecReg The register which holds element zero of the vector
414/// being addressed into.
415/// \param[out] @Reg The base register to use in the indirect addressing instruction.
416/// \param[in,out] @Offset As an input, this is the constant offset part of the
417// indirect Index. e.g. v0 = v[VecReg + Offset]
418// As an output, this is a constant value that needs
419// to be added to the value stored in M0.
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000420void SILowerControlFlow::computeIndirectRegAndOffset(unsigned VecReg,
421 unsigned &Reg,
422 int &Offset) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000423 unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
424 if (!SubReg)
425 SubReg = VecReg;
426
427 const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
428 int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
429
430 if (RegIdx < 0) {
431 Offset = RegIdx;
432 RegIdx = 0;
433 } else {
434 Offset = 0;
435 }
436
437 Reg = RC->getRegister(RegIdx);
438}
439
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000440void SILowerControlFlow::IndirectSrc(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000441
442 MachineBasicBlock &MBB = *MI.getParent();
443 DebugLoc DL = MI.getDebugLoc();
444
445 unsigned Dst = MI.getOperand(0).getReg();
446 unsigned Vec = MI.getOperand(2).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000447 int Off = MI.getOperand(4).getImm();
448 unsigned Reg;
449
450 computeIndirectRegAndOffset(Vec, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000451
Tom Stellard81d871d2013-11-13 23:36:50 +0000452 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000453 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard8b0182a2015-04-23 20:32:01 +0000454 .addReg(Reg)
Christian Konig2989ffc2013-03-18 11:34:16 +0000455 .addReg(Vec, RegState::Implicit);
456
Tom Stellard8b0182a2015-04-23 20:32:01 +0000457 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000458}
459
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000460void SILowerControlFlow::IndirectDst(MachineInstr &MI) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000461
462 MachineBasicBlock &MBB = *MI.getParent();
463 DebugLoc DL = MI.getDebugLoc();
464
465 unsigned Dst = MI.getOperand(0).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000466 int Off = MI.getOperand(4).getImm();
Christian Konig2989ffc2013-03-18 11:34:16 +0000467 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000468 unsigned Reg;
469
470 computeIndirectRegAndOffset(Dst, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000471
Matt Arsenault806dd0a2016-02-12 02:16:07 +0000472 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000473 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard8b0182a2015-04-23 20:32:01 +0000474 .addReg(Reg, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000475 .addReg(Val)
Christian Konig2989ffc2013-03-18 11:34:16 +0000476 .addReg(Dst, RegState::Implicit);
477
Tom Stellard8b0182a2015-04-23 20:32:01 +0000478 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000479}
480
Matt Arsenault55d49cf2016-02-12 02:16:10 +0000481bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000482 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
483 TRI =
484 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000485 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000486
487 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000488 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000489 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000490 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000491
Tom Stellardf8794352012-12-19 22:10:31 +0000492 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
493 BI != BE; ++BI) {
494
Marek Olsaked2213e2016-03-14 15:57:14 +0000495 MachineBasicBlock *EmptyMBBAtEnd = NULL;
Tom Stellardf8794352012-12-19 22:10:31 +0000496 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000497 MachineBasicBlock::iterator I, Next;
498 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000499 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000500
Tom Stellard75aadc22012-12-11 21:25:42 +0000501 MachineInstr &MI = *I;
Matt Arsenault3add6432015-10-20 04:35:43 +0000502 if (TII->isWQM(MI) || TII->isDS(MI))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000503 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000504
Matt Arsenault3f981402014-09-15 15:41:53 +0000505 // Flat uses m0 in case it needs to access LDS.
Matt Arsenault3add6432015-10-20 04:35:43 +0000506 if (TII->isFLAT(MI))
Matt Arsenault3f981402014-09-15 15:41:53 +0000507 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000508
Tom Stellard75aadc22012-12-11 21:25:42 +0000509 switch (MI.getOpcode()) {
510 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000511 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000512 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000513 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000514 break;
515
Tom Stellardf8794352012-12-19 22:10:31 +0000516 case AMDGPU::SI_ELSE:
517 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000518 break;
519
Tom Stellardf8794352012-12-19 22:10:31 +0000520 case AMDGPU::SI_BREAK:
521 Break(MI);
522 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000523
Tom Stellardf8794352012-12-19 22:10:31 +0000524 case AMDGPU::SI_IF_BREAK:
525 IfBreak(MI);
526 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000527
Tom Stellardf8794352012-12-19 22:10:31 +0000528 case AMDGPU::SI_ELSE_BREAK:
529 ElseBreak(MI);
530 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000531
Tom Stellardf8794352012-12-19 22:10:31 +0000532 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000533 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000534 Loop(MI);
535 break;
536
537 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000538 if (--Depth == 0 && HaveKill) {
539 SkipIfDead(MI);
540 HaveKill = false;
541 }
Tom Stellardf8794352012-12-19 22:10:31 +0000542 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000543 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000544
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000545 case AMDGPU::SI_KILL:
546 if (Depth == 0)
547 SkipIfDead(MI);
548 else
549 HaveKill = true;
550 Kill(MI);
551 break;
552
Tom Stellarde7b907d2012-12-19 22:10:33 +0000553 case AMDGPU::S_BRANCH:
554 Branch(MI);
555 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000556
Matt Arsenault28419272015-10-07 00:42:51 +0000557 case AMDGPU::SI_INDIRECT_SRC_V1:
558 case AMDGPU::SI_INDIRECT_SRC_V2:
559 case AMDGPU::SI_INDIRECT_SRC_V4:
560 case AMDGPU::SI_INDIRECT_SRC_V8:
561 case AMDGPU::SI_INDIRECT_SRC_V16:
Christian Konig2989ffc2013-03-18 11:34:16 +0000562 IndirectSrc(MI);
563 break;
564
Tom Stellard81d871d2013-11-13 23:36:50 +0000565 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000566 case AMDGPU::SI_INDIRECT_DST_V2:
567 case AMDGPU::SI_INDIRECT_DST_V4:
568 case AMDGPU::SI_INDIRECT_DST_V8:
569 case AMDGPU::SI_INDIRECT_DST_V16:
570 IndirectDst(MI);
571 break;
Marek Olsaked2213e2016-03-14 15:57:14 +0000572
573 case AMDGPU::S_ENDPGM: {
574 if (MF.getInfo<SIMachineFunctionInfo>()->returnsVoid())
575 break;
576
577 // Graphics shaders returning non-void shouldn't contain S_ENDPGM,
578 // because external bytecode will be appended at the end.
579 if (BI != --MF.end() || I != MBB.getFirstTerminator()) {
580 // S_ENDPGM is not the last instruction. Add an empty block at
581 // the end and jump there.
582 if (!EmptyMBBAtEnd) {
583 EmptyMBBAtEnd = MF.CreateMachineBasicBlock();
584 MF.insert(MF.end(), EmptyMBBAtEnd);
585 }
586
587 MBB.addSuccessor(EmptyMBBAtEnd);
588 BuildMI(*BI, I, MI.getDebugLoc(), TII->get(AMDGPU::S_BRANCH))
589 .addMBB(EmptyMBBAtEnd);
590 }
591
592 I->eraseFromParent();
593 break;
594 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000595 }
596 }
597 }
Tom Stellardf8794352012-12-19 22:10:31 +0000598
Matt Arsenault762af962014-07-13 03:06:39 +0000599 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000600 MachineBasicBlock &MBB = MF.front();
601 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
602 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
603 }
604
Matt Arsenault3f981402014-09-15 15:41:53 +0000605 if (NeedFlat && MFI->IsKernel) {
Matt Arsenault3f981402014-09-15 15:41:53 +0000606 // TODO: What to use with function calls?
Matt Arsenault296b8492016-02-12 06:31:30 +0000607 // We will need to Initialize the flat scratch register pair.
608 if (NeedFlat)
609 MFI->setHasFlatInstructions(true);
Matt Arsenault3f981402014-09-15 15:41:53 +0000610 }
611
Tom Stellard75aadc22012-12-11 21:25:42 +0000612 return true;
613}