blob: 1d6627fb18a75fb7f6c8147e1fd97dbf4fb8461b [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
Tom Stellardf8794352012-12-19 22:10:31 +000011/// \brief This pass lowers the pseudo control flow instructions to real
12/// machine instructions.
Tom Stellard75aadc22012-12-11 21:25:42 +000013///
Tom Stellardf8794352012-12-19 22:10:31 +000014/// All control flow is handled using predicated instructions and
Tom Stellard75aadc22012-12-11 21:25:42 +000015/// a predicate stack. Each Scalar ALU controls the operations of 64 Vector
16/// ALUs. The Scalar ALU can update the predicate for any of the Vector ALUs
17/// by writting to the 64-bit EXEC register (each bit corresponds to a
18/// single vector ALU). Typically, for predicates, a vector ALU will write
19/// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each
20/// Vector ALU) and then the ScalarALU will AND the VCC register with the
21/// EXEC to update the predicates.
22///
23/// For example:
24/// %VCC = V_CMP_GT_F32 %VGPR1, %VGPR2
Tom Stellardf8794352012-12-19 22:10:31 +000025/// %SGPR0 = SI_IF %VCC
Tom Stellard75aadc22012-12-11 21:25:42 +000026/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
Tom Stellardf8794352012-12-19 22:10:31 +000027/// %SGPR0 = SI_ELSE %SGPR0
Tom Stellard75aadc22012-12-11 21:25:42 +000028/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
Tom Stellardf8794352012-12-19 22:10:31 +000029/// SI_END_CF %SGPR0
Tom Stellard75aadc22012-12-11 21:25:42 +000030///
31/// becomes:
32///
33/// %SGPR0 = S_AND_SAVEEXEC_B64 %VCC // Save and update the exec mask
34/// %SGPR0 = S_XOR_B64 %SGPR0, %EXEC // Clear live bits from saved exec mask
Tom Stellardf8794352012-12-19 22:10:31 +000035/// S_CBRANCH_EXECZ label0 // This instruction is an optional
Tom Stellard75aadc22012-12-11 21:25:42 +000036/// // optimization which allows us to
37/// // branch if all the bits of
38/// // EXEC are zero.
39/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0 // Do the IF block of the branch
40///
41/// label0:
42/// %SGPR0 = S_OR_SAVEEXEC_B64 %EXEC // Restore the exec mask for the Then block
43/// %EXEC = S_XOR_B64 %SGPR0, %EXEC // Clear live bits from saved exec mask
44/// S_BRANCH_EXECZ label1 // Use our branch optimization
45/// // instruction again.
46/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR // Do the THEN block
47/// label1:
Tom Stellardf8794352012-12-19 22:10:31 +000048/// %EXEC = S_OR_B64 %EXEC, %SGPR0 // Re-enable saved exec mask bits
Tom Stellard75aadc22012-12-11 21:25:42 +000049//===----------------------------------------------------------------------===//
50
51#include "AMDGPU.h"
Eric Christopherd9134482014-08-04 21:25:23 +000052#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000053#include "SIInstrInfo.h"
54#include "SIMachineFunctionInfo.h"
Matt Arsenault3f981402014-09-15 15:41:53 +000055#include "llvm/CodeGen/MachineFrameInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000056#include "llvm/CodeGen/MachineFunction.h"
57#include "llvm/CodeGen/MachineFunctionPass.h"
58#include "llvm/CodeGen/MachineInstrBuilder.h"
59#include "llvm/CodeGen/MachineRegisterInfo.h"
Michel Danzer9e61c4b2014-02-27 01:47:09 +000060#include "llvm/IR/Constants.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000061
62using namespace llvm;
63
64namespace {
65
66class SILowerControlFlowPass : public MachineFunctionPass {
67
68private:
Tom Stellarde7b907d2012-12-19 22:10:33 +000069 static const unsigned SkipThreshold = 12;
70
Tom Stellard75aadc22012-12-11 21:25:42 +000071 static char ID;
Tom Stellard1bd80722014-04-30 15:31:33 +000072 const SIRegisterInfo *TRI;
Tom Stellard5d7aaae2014-02-10 16:58:30 +000073 const SIInstrInfo *TII;
Tom Stellard75aadc22012-12-11 21:25:42 +000074
Tom Stellardbe8ebee2013-01-18 21:15:50 +000075 bool shouldSkip(MachineBasicBlock *From, MachineBasicBlock *To);
76
77 void Skip(MachineInstr &From, MachineOperand &To);
78 void SkipIfDead(MachineInstr &MI);
Tom Stellarde7b907d2012-12-19 22:10:33 +000079
Tom Stellardf8794352012-12-19 22:10:31 +000080 void If(MachineInstr &MI);
81 void Else(MachineInstr &MI);
82 void Break(MachineInstr &MI);
83 void IfBreak(MachineInstr &MI);
84 void ElseBreak(MachineInstr &MI);
85 void Loop(MachineInstr &MI);
86 void EndCf(MachineInstr &MI);
Tom Stellard75aadc22012-12-11 21:25:42 +000087
Tom Stellardbe8ebee2013-01-18 21:15:50 +000088 void Kill(MachineInstr &MI);
Tom Stellarde7b907d2012-12-19 22:10:33 +000089 void Branch(MachineInstr &MI);
90
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:
97 SILowerControlFlowPass(TargetMachine &tm) :
Craig Topper062a2ba2014-04-25 05:30:21 +000098 MachineFunctionPass(ID), TRI(nullptr), TII(nullptr) { }
Tom Stellard75aadc22012-12-11 21:25:42 +000099
Craig Topper5656db42014-04-29 07:57:24 +0000100 bool runOnMachineFunction(MachineFunction &MF) override;
Tom Stellard75aadc22012-12-11 21:25:42 +0000101
Craig Topper5656db42014-04-29 07:57:24 +0000102 const char *getPassName() const override {
Tom Stellard75aadc22012-12-11 21:25:42 +0000103 return "SI Lower control flow instructions";
104 }
105
106};
107
108} // End anonymous namespace
109
110char SILowerControlFlowPass::ID = 0;
111
112FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
113 return new SILowerControlFlowPass(tm);
114}
115
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000116bool SILowerControlFlowPass::shouldSkip(MachineBasicBlock *From,
117 MachineBasicBlock *To) {
118
Tom Stellarde7b907d2012-12-19 22:10:33 +0000119 unsigned NumInstr = 0;
120
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000121 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellarde7b907d2012-12-19 22:10:33 +0000122 MBB = *MBB->succ_begin()) {
123
124 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
125 NumInstr < SkipThreshold && I != E; ++I) {
126
127 if (I->isBundle() || !I->isBundled())
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000128 if (++NumInstr >= SkipThreshold)
129 return true;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000130 }
131 }
132
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000133 return false;
134}
135
136void SILowerControlFlowPass::Skip(MachineInstr &From, MachineOperand &To) {
137
138 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000139 return;
140
141 DebugLoc DL = From.getDebugLoc();
142 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000143 .addOperand(To);
Tom Stellarde7b907d2012-12-19 22:10:33 +0000144}
145
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000146void SILowerControlFlowPass::SkipIfDead(MachineInstr &MI) {
147
148 MachineBasicBlock &MBB = *MI.getParent();
149 DebugLoc DL = MI.getDebugLoc();
150
Matt Arsenault762af962014-07-13 03:06:39 +0000151 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000152 ShaderType::PIXEL ||
153 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000154 return;
155
156 MachineBasicBlock::iterator Insert = &MI;
157 ++Insert;
158
159 // If the exec mask is non-zero, skip the next two instructions
160 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000161 .addImm(3);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000162
163 // Exec mask is zero: Export to NULL target...
164 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
165 .addImm(0)
166 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
167 .addImm(0)
168 .addImm(1)
169 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000170 .addReg(AMDGPU::VGPR0)
171 .addReg(AMDGPU::VGPR0)
172 .addReg(AMDGPU::VGPR0)
173 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000174
175 // ... and terminate wavefront
176 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
177}
178
Tom Stellardf8794352012-12-19 22:10:31 +0000179void SILowerControlFlowPass::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000180 MachineBasicBlock &MBB = *MI.getParent();
181 DebugLoc DL = MI.getDebugLoc();
182 unsigned Reg = MI.getOperand(0).getReg();
183 unsigned Vcc = MI.getOperand(1).getReg();
184
185 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
186 .addReg(Vcc);
187
188 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
189 .addReg(AMDGPU::EXEC)
190 .addReg(Reg);
191
Tom Stellarde7b907d2012-12-19 22:10:33 +0000192 Skip(MI, MI.getOperand(2));
193
Tom Stellardf8794352012-12-19 22:10:31 +0000194 MI.eraseFromParent();
195}
196
197void SILowerControlFlowPass::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000198 MachineBasicBlock &MBB = *MI.getParent();
199 DebugLoc DL = MI.getDebugLoc();
200 unsigned Dst = MI.getOperand(0).getReg();
201 unsigned Src = MI.getOperand(1).getReg();
202
Christian Konig6a9d3902013-03-26 14:03:44 +0000203 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
204 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000205 .addReg(Src); // Saved EXEC
206
207 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
208 .addReg(AMDGPU::EXEC)
209 .addReg(Dst);
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
216void SILowerControlFlowPass::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000217 MachineBasicBlock &MBB = *MI.getParent();
218 DebugLoc DL = MI.getDebugLoc();
219
220 unsigned Dst = MI.getOperand(0).getReg();
221 unsigned Src = MI.getOperand(1).getReg();
222
223 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
224 .addReg(AMDGPU::EXEC)
225 .addReg(Src);
226
227 MI.eraseFromParent();
228}
229
230void SILowerControlFlowPass::IfBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000231 MachineBasicBlock &MBB = *MI.getParent();
232 DebugLoc DL = MI.getDebugLoc();
233
234 unsigned Dst = MI.getOperand(0).getReg();
235 unsigned Vcc = MI.getOperand(1).getReg();
236 unsigned Src = MI.getOperand(2).getReg();
237
238 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
239 .addReg(Vcc)
240 .addReg(Src);
241
242 MI.eraseFromParent();
243}
244
245void SILowerControlFlowPass::ElseBreak(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000246 MachineBasicBlock &MBB = *MI.getParent();
247 DebugLoc DL = MI.getDebugLoc();
248
249 unsigned Dst = MI.getOperand(0).getReg();
250 unsigned Saved = MI.getOperand(1).getReg();
251 unsigned Src = MI.getOperand(2).getReg();
252
253 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
254 .addReg(Saved)
255 .addReg(Src);
256
257 MI.eraseFromParent();
258}
259
260void SILowerControlFlowPass::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000261 MachineBasicBlock &MBB = *MI.getParent();
262 DebugLoc DL = MI.getDebugLoc();
263 unsigned Src = MI.getOperand(0).getReg();
264
265 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
266 .addReg(AMDGPU::EXEC)
267 .addReg(Src);
268
269 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000270 .addOperand(MI.getOperand(1));
Tom Stellardf8794352012-12-19 22:10:31 +0000271
272 MI.eraseFromParent();
273}
274
275void SILowerControlFlowPass::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000276 MachineBasicBlock &MBB = *MI.getParent();
277 DebugLoc DL = MI.getDebugLoc();
278 unsigned Reg = MI.getOperand(0).getReg();
279
280 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
281 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
282 .addReg(AMDGPU::EXEC)
283 .addReg(Reg);
284
285 MI.eraseFromParent();
286}
287
Tom Stellarde7b907d2012-12-19 22:10:33 +0000288void SILowerControlFlowPass::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000289 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
290 MI.eraseFromParent();
291
292 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000293}
294
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000295void SILowerControlFlowPass::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000296 MachineBasicBlock &MBB = *MI.getParent();
297 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000298 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000299
Matt Arsenault762af962014-07-13 03:06:39 +0000300#ifndef NDEBUG
301 const SIMachineFunctionInfo *MFI
302 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
303 // Kill is only allowed in pixel / geometry shaders.
304 assert(MFI->getShaderType() == ShaderType::PIXEL ||
305 MFI->getShaderType() == ShaderType::GEOMETRY);
306#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000307
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000308 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000309 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000310 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000311 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000312 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
313 .addImm(0);
314 }
315 } else {
316 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32), AMDGPU::VCC)
317 .addImm(0)
318 .addOperand(Op);
319 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000320
321 MI.eraseFromParent();
322}
323
Tom Stellard8b0182a2015-04-23 20:32:01 +0000324void SILowerControlFlowPass::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000325
326 MachineBasicBlock &MBB = *MI.getParent();
327 DebugLoc DL = MI.getDebugLoc();
328 MachineBasicBlock::iterator I = MI;
329
330 unsigned Save = MI.getOperand(1).getReg();
331 unsigned Idx = MI.getOperand(3).getReg();
332
333 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000334 if (Offset) {
335 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
336 .addReg(Idx)
337 .addImm(Offset);
338 } else {
339 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
340 .addReg(Idx);
341 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000342 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000343 } else {
344
345 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000346 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000347
348 // Save the EXEC mask
349 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
350 .addReg(AMDGPU::EXEC);
351
352 // Read the next variant into VCC (lower 32 bits) <- also loop target
353 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
354 AMDGPU::VCC_LO)
355 .addReg(Idx);
356
357 // Move index from VCC into M0
358 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
359 .addReg(AMDGPU::VCC_LO);
360
361 // Compare the just read M0 value to all possible Idx values
362 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32), AMDGPU::VCC)
363 .addReg(AMDGPU::M0)
364 .addReg(Idx);
365
366 // Update EXEC, save the original EXEC value to VCC
367 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
368 .addReg(AMDGPU::VCC);
369
Tom Stellard8b0182a2015-04-23 20:32:01 +0000370 if (Offset) {
371 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
372 .addReg(AMDGPU::M0)
373 .addImm(Offset);
374 }
Tom Stellard89422762014-06-17 16:53:04 +0000375 // Do the actual move
376 MBB.insert(I, MovRel);
377
378 // Update EXEC, switch all done bits to 0 and all todo bits to 1
379 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
380 .addReg(AMDGPU::EXEC)
381 .addReg(AMDGPU::VCC);
382
383 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
384 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000385 .addImm(-7);
Tom Stellard89422762014-06-17 16:53:04 +0000386
387 // Restore EXEC
388 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
389 .addReg(Save);
390
Christian Konig2989ffc2013-03-18 11:34:16 +0000391 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000392 MI.eraseFromParent();
393}
394
Tom Stellard8b0182a2015-04-23 20:32:01 +0000395/// \param @VecReg The register which holds element zero of the vector
396/// being addressed into.
397/// \param[out] @Reg The base register to use in the indirect addressing instruction.
398/// \param[in,out] @Offset As an input, this is the constant offset part of the
399// indirect Index. e.g. v0 = v[VecReg + Offset]
400// As an output, this is a constant value that needs
401// to be added to the value stored in M0.
402void SILowerControlFlowPass::computeIndirectRegAndOffset(unsigned VecReg,
403 unsigned &Reg,
404 int &Offset) {
405 unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
406 if (!SubReg)
407 SubReg = VecReg;
408
409 const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
410 int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
411
412 if (RegIdx < 0) {
413 Offset = RegIdx;
414 RegIdx = 0;
415 } else {
416 Offset = 0;
417 }
418
419 Reg = RC->getRegister(RegIdx);
420}
421
Christian Konig2989ffc2013-03-18 11:34:16 +0000422void SILowerControlFlowPass::IndirectSrc(MachineInstr &MI) {
423
424 MachineBasicBlock &MBB = *MI.getParent();
425 DebugLoc DL = MI.getDebugLoc();
426
427 unsigned Dst = MI.getOperand(0).getReg();
428 unsigned Vec = MI.getOperand(2).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000429 int Off = MI.getOperand(4).getImm();
430 unsigned Reg;
431
432 computeIndirectRegAndOffset(Vec, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000433
Tom Stellard81d871d2013-11-13 23:36:50 +0000434 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000435 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard8b0182a2015-04-23 20:32:01 +0000436 .addReg(Reg)
Christian Konig2989ffc2013-03-18 11:34:16 +0000437 .addReg(AMDGPU::M0, RegState::Implicit)
438 .addReg(Vec, RegState::Implicit);
439
Tom Stellard8b0182a2015-04-23 20:32:01 +0000440 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000441}
442
443void SILowerControlFlowPass::IndirectDst(MachineInstr &MI) {
444
445 MachineBasicBlock &MBB = *MI.getParent();
446 DebugLoc DL = MI.getDebugLoc();
447
448 unsigned Dst = MI.getOperand(0).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000449 int Off = MI.getOperand(4).getImm();
Christian Konig2989ffc2013-03-18 11:34:16 +0000450 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000451 unsigned Reg;
452
453 computeIndirectRegAndOffset(Dst, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000454
455 MachineInstr *MovRel =
456 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard8b0182a2015-04-23 20:32:01 +0000457 .addReg(Reg, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000458 .addReg(Val)
459 .addReg(AMDGPU::M0, RegState::Implicit)
460 .addReg(Dst, RegState::Implicit);
461
Tom Stellard8b0182a2015-04-23 20:32:01 +0000462 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000463}
464
Tom Stellard75aadc22012-12-11 21:25:42 +0000465bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000466 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
467 TRI =
468 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000469 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000470
471 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000472 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000473 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000474 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000475
Tom Stellardf8794352012-12-19 22:10:31 +0000476 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
477 BI != BE; ++BI) {
478
479 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000480 MachineBasicBlock::iterator I, Next;
481 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000482 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000483
Tom Stellard75aadc22012-12-11 21:25:42 +0000484 MachineInstr &MI = *I;
Michel Danzer494391b2015-02-06 02:51:20 +0000485 if (TII->isWQM(MI.getOpcode()) || TII->isDS(MI.getOpcode()))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000486 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000487
Matt Arsenault3f981402014-09-15 15:41:53 +0000488 // Flat uses m0 in case it needs to access LDS.
Aaron Ballman41580de2014-11-24 14:03:16 +0000489 if (TII->isFLAT(MI.getOpcode()))
Matt Arsenault3f981402014-09-15 15:41:53 +0000490 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000491
Tom Stellard75aadc22012-12-11 21:25:42 +0000492 switch (MI.getOpcode()) {
493 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000494 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000495 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000496 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000497 break;
498
Tom Stellardf8794352012-12-19 22:10:31 +0000499 case AMDGPU::SI_ELSE:
500 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000501 break;
502
Tom Stellardf8794352012-12-19 22:10:31 +0000503 case AMDGPU::SI_BREAK:
504 Break(MI);
505 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000506
Tom Stellardf8794352012-12-19 22:10:31 +0000507 case AMDGPU::SI_IF_BREAK:
508 IfBreak(MI);
509 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000510
Tom Stellardf8794352012-12-19 22:10:31 +0000511 case AMDGPU::SI_ELSE_BREAK:
512 ElseBreak(MI);
513 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000514
Tom Stellardf8794352012-12-19 22:10:31 +0000515 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000516 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000517 Loop(MI);
518 break;
519
520 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000521 if (--Depth == 0 && HaveKill) {
522 SkipIfDead(MI);
523 HaveKill = false;
524 }
Tom Stellardf8794352012-12-19 22:10:31 +0000525 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000526 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000527
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000528 case AMDGPU::SI_KILL:
529 if (Depth == 0)
530 SkipIfDead(MI);
531 else
532 HaveKill = true;
533 Kill(MI);
534 break;
535
Tom Stellarde7b907d2012-12-19 22:10:33 +0000536 case AMDGPU::S_BRANCH:
537 Branch(MI);
538 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000539
540 case AMDGPU::SI_INDIRECT_SRC:
541 IndirectSrc(MI);
542 break;
543
Tom Stellard81d871d2013-11-13 23:36:50 +0000544 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000545 case AMDGPU::SI_INDIRECT_DST_V2:
546 case AMDGPU::SI_INDIRECT_DST_V4:
547 case AMDGPU::SI_INDIRECT_DST_V8:
548 case AMDGPU::SI_INDIRECT_DST_V16:
549 IndirectDst(MI);
550 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000551 }
552 }
553 }
Tom Stellardf8794352012-12-19 22:10:31 +0000554
Matt Arsenault762af962014-07-13 03:06:39 +0000555 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000556 MachineBasicBlock &MBB = MF.front();
557 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
558 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
559 }
560
Matt Arsenault3f981402014-09-15 15:41:53 +0000561 // FIXME: This seems inappropriate to do here.
562 if (NeedFlat && MFI->IsKernel) {
563 // Insert the prologue initializing the SGPRs pointing to the scratch space
564 // for flat accesses.
565 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
566
567 // TODO: What to use with function calls?
568
569 // FIXME: This is reporting stack size that is used in a scratch buffer
570 // rather than registers as well.
571 uint64_t StackSizeBytes = FrameInfo->getStackSize();
572
573 int IndirectBegin
574 = static_cast<const AMDGPUInstrInfo*>(TII)->getIndirectIndexBegin(MF);
575 // Convert register index to 256-byte unit.
576 uint64_t StackOffset = IndirectBegin < 0 ? 0 : (4 * IndirectBegin / 256);
577
578 assert((StackSizeBytes < 0xffff) && StackOffset < 0xffff &&
579 "Stack limits should be smaller than 16-bits");
580
581 // Initialize the flat scratch register pair.
582 // TODO: Can we use one s_mov_b64 here?
583
584 // Offset is in units of 256-bytes.
585 MachineBasicBlock &MBB = MF.front();
586 DebugLoc NoDL;
587 MachineBasicBlock::iterator Start = MBB.getFirstNonPHI();
588 const MCInstrDesc &SMovK = TII->get(AMDGPU::S_MOVK_I32);
589
Matt Arsenault77849922014-11-13 20:44:23 +0000590 assert(isInt<16>(StackOffset) && isInt<16>(StackSizeBytes));
591
Matt Arsenault3f981402014-09-15 15:41:53 +0000592 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_LO)
593 .addImm(StackOffset);
594
595 // Documentation says size is "per-thread scratch size in bytes"
596 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_HI)
597 .addImm(StackSizeBytes);
598 }
599
Tom Stellard75aadc22012-12-11 21:25:42 +0000600 return true;
601}