blob: 126f6245dfc0fffa97043fff4688008c8e346410 [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
Matt Arsenault0cb85172015-09-25 17:21:28 +0000106 void getAnalysisUsage(AnalysisUsage &AU) const override {
107 AU.setPreservesCFG();
108 MachineFunctionPass::getAnalysisUsage(AU);
109 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000110};
111
112} // End anonymous namespace
113
114char SILowerControlFlowPass::ID = 0;
115
116FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
117 return new SILowerControlFlowPass(tm);
118}
119
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000120bool SILowerControlFlowPass::shouldSkip(MachineBasicBlock *From,
121 MachineBasicBlock *To) {
122
Tom Stellarde7b907d2012-12-19 22:10:33 +0000123 unsigned NumInstr = 0;
124
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000125 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellarde7b907d2012-12-19 22:10:33 +0000126 MBB = *MBB->succ_begin()) {
127
128 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
129 NumInstr < SkipThreshold && I != E; ++I) {
130
131 if (I->isBundle() || !I->isBundled())
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000132 if (++NumInstr >= SkipThreshold)
133 return true;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000134 }
135 }
136
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000137 return false;
138}
139
140void SILowerControlFlowPass::Skip(MachineInstr &From, MachineOperand &To) {
141
142 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellarde7b907d2012-12-19 22:10:33 +0000143 return;
144
145 DebugLoc DL = From.getDebugLoc();
146 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000147 .addOperand(To);
Tom Stellarde7b907d2012-12-19 22:10:33 +0000148}
149
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000150void SILowerControlFlowPass::SkipIfDead(MachineInstr &MI) {
151
152 MachineBasicBlock &MBB = *MI.getParent();
153 DebugLoc DL = MI.getDebugLoc();
154
Matt Arsenault762af962014-07-13 03:06:39 +0000155 if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
Michel Danzer6f273c52014-02-27 01:47:02 +0000156 ShaderType::PIXEL ||
157 !shouldSkip(&MBB, &MBB.getParent()->back()))
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000158 return;
159
160 MachineBasicBlock::iterator Insert = &MI;
161 ++Insert;
162
163 // If the exec mask is non-zero, skip the next two instructions
164 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000165 .addImm(3);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000166
167 // Exec mask is zero: Export to NULL target...
168 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
169 .addImm(0)
170 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
171 .addImm(0)
172 .addImm(1)
173 .addImm(1)
Christian Konigc756cb992013-02-16 11:28:22 +0000174 .addReg(AMDGPU::VGPR0)
175 .addReg(AMDGPU::VGPR0)
176 .addReg(AMDGPU::VGPR0)
177 .addReg(AMDGPU::VGPR0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000178
179 // ... and terminate wavefront
180 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
181}
182
Tom Stellardf8794352012-12-19 22:10:31 +0000183void SILowerControlFlowPass::If(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000184 MachineBasicBlock &MBB = *MI.getParent();
185 DebugLoc DL = MI.getDebugLoc();
186 unsigned Reg = MI.getOperand(0).getReg();
187 unsigned Vcc = MI.getOperand(1).getReg();
188
189 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
190 .addReg(Vcc);
191
192 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
193 .addReg(AMDGPU::EXEC)
194 .addReg(Reg);
195
Tom Stellarde7b907d2012-12-19 22:10:33 +0000196 Skip(MI, MI.getOperand(2));
197
Tom Stellardf8794352012-12-19 22:10:31 +0000198 MI.eraseFromParent();
199}
200
201void SILowerControlFlowPass::Else(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000202 MachineBasicBlock &MBB = *MI.getParent();
203 DebugLoc DL = MI.getDebugLoc();
204 unsigned Dst = MI.getOperand(0).getReg();
205 unsigned Src = MI.getOperand(1).getReg();
206
Christian Konig6a9d3902013-03-26 14:03:44 +0000207 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
208 TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
Tom Stellardf8794352012-12-19 22:10:31 +0000209 .addReg(Src); // Saved EXEC
210
211 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
212 .addReg(AMDGPU::EXEC)
213 .addReg(Dst);
214
Tom Stellarde7b907d2012-12-19 22:10:33 +0000215 Skip(MI, MI.getOperand(2));
216
Tom Stellardf8794352012-12-19 22:10:31 +0000217 MI.eraseFromParent();
218}
219
220void SILowerControlFlowPass::Break(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000221 MachineBasicBlock &MBB = *MI.getParent();
222 DebugLoc DL = MI.getDebugLoc();
223
224 unsigned Dst = MI.getOperand(0).getReg();
225 unsigned Src = MI.getOperand(1).getReg();
226
227 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
228 .addReg(AMDGPU::EXEC)
229 .addReg(Src);
230
231 MI.eraseFromParent();
232}
233
234void SILowerControlFlowPass::IfBreak(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 Vcc = MI.getOperand(1).getReg();
240 unsigned Src = MI.getOperand(2).getReg();
241
242 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
243 .addReg(Vcc)
244 .addReg(Src);
245
246 MI.eraseFromParent();
247}
248
249void SILowerControlFlowPass::ElseBreak(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 Saved = MI.getOperand(1).getReg();
255 unsigned Src = MI.getOperand(2).getReg();
256
257 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
258 .addReg(Saved)
259 .addReg(Src);
260
261 MI.eraseFromParent();
262}
263
264void SILowerControlFlowPass::Loop(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000265 MachineBasicBlock &MBB = *MI.getParent();
266 DebugLoc DL = MI.getDebugLoc();
267 unsigned Src = MI.getOperand(0).getReg();
268
269 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
270 .addReg(AMDGPU::EXEC)
271 .addReg(Src);
272
273 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000274 .addOperand(MI.getOperand(1));
Tom Stellardf8794352012-12-19 22:10:31 +0000275
276 MI.eraseFromParent();
277}
278
279void SILowerControlFlowPass::EndCf(MachineInstr &MI) {
Tom Stellardf8794352012-12-19 22:10:31 +0000280 MachineBasicBlock &MBB = *MI.getParent();
281 DebugLoc DL = MI.getDebugLoc();
282 unsigned Reg = MI.getOperand(0).getReg();
283
284 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
285 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
286 .addReg(AMDGPU::EXEC)
287 .addReg(Reg);
288
289 MI.eraseFromParent();
290}
291
Tom Stellarde7b907d2012-12-19 22:10:33 +0000292void SILowerControlFlowPass::Branch(MachineInstr &MI) {
Matt Arsenault71b71d22014-02-11 21:12:38 +0000293 if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
294 MI.eraseFromParent();
295
296 // If these aren't equal, this is probably an infinite loop.
Tom Stellarde7b907d2012-12-19 22:10:33 +0000297}
298
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000299void SILowerControlFlowPass::Kill(MachineInstr &MI) {
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000300 MachineBasicBlock &MBB = *MI.getParent();
301 DebugLoc DL = MI.getDebugLoc();
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000302 const MachineOperand &Op = MI.getOperand(0);
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000303
Matt Arsenault762af962014-07-13 03:06:39 +0000304#ifndef NDEBUG
305 const SIMachineFunctionInfo *MFI
306 = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
307 // Kill is only allowed in pixel / geometry shaders.
308 assert(MFI->getShaderType() == ShaderType::PIXEL ||
309 MFI->getShaderType() == ShaderType::GEOMETRY);
310#endif
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000311
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000312 // Clear this thread from the exec mask if the operand is negative
Tom Stellardfb77f002015-01-13 22:59:41 +0000313 if ((Op.isImm())) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000314 // Constant operand: Set exec mask to 0 or do nothing
Tom Stellardfb77f002015-01-13 22:59:41 +0000315 if (Op.getImm() & 0x80000000) {
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000316 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
317 .addImm(0);
318 }
319 } else {
Matt Arsenault46359152015-08-08 00:41:48 +0000320 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
Michel Danzer9e61c4b2014-02-27 01:47:09 +0000321 .addImm(0)
322 .addOperand(Op);
323 }
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000324
325 MI.eraseFromParent();
326}
327
Tom Stellard8b0182a2015-04-23 20:32:01 +0000328void SILowerControlFlowPass::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
Christian Konig2989ffc2013-03-18 11:34:16 +0000329
330 MachineBasicBlock &MBB = *MI.getParent();
331 DebugLoc DL = MI.getDebugLoc();
332 MachineBasicBlock::iterator I = MI;
333
334 unsigned Save = MI.getOperand(1).getReg();
335 unsigned Idx = MI.getOperand(3).getReg();
336
337 if (AMDGPU::SReg_32RegClass.contains(Idx)) {
Tom Stellard8b0182a2015-04-23 20:32:01 +0000338 if (Offset) {
339 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
340 .addReg(Idx)
341 .addImm(Offset);
342 } else {
343 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
344 .addReg(Idx);
345 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000346 MBB.insert(I, MovRel);
Tom Stellard89422762014-06-17 16:53:04 +0000347 } else {
348
349 assert(AMDGPU::SReg_64RegClass.contains(Save));
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000350 assert(AMDGPU::VGPR_32RegClass.contains(Idx));
Tom Stellard89422762014-06-17 16:53:04 +0000351
352 // Save the EXEC mask
353 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
354 .addReg(AMDGPU::EXEC);
355
356 // Read the next variant into VCC (lower 32 bits) <- also loop target
357 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
358 AMDGPU::VCC_LO)
359 .addReg(Idx);
360
361 // Move index from VCC into M0
362 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
363 .addReg(AMDGPU::VCC_LO);
364
365 // Compare the just read M0 value to all possible Idx values
Matt Arsenault46359152015-08-08 00:41:48 +0000366 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
367 .addReg(AMDGPU::M0)
368 .addReg(Idx);
Tom Stellard89422762014-06-17 16:53:04 +0000369
370 // Update EXEC, save the original EXEC value to VCC
371 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
372 .addReg(AMDGPU::VCC);
373
Tom Stellard8b0182a2015-04-23 20:32:01 +0000374 if (Offset) {
375 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
376 .addReg(AMDGPU::M0)
377 .addImm(Offset);
378 }
Tom Stellard89422762014-06-17 16:53:04 +0000379 // Do the actual move
380 MBB.insert(I, MovRel);
381
382 // Update EXEC, switch all done bits to 0 and all todo bits to 1
383 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
384 .addReg(AMDGPU::EXEC)
385 .addReg(AMDGPU::VCC);
386
387 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
388 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
Matt Arsenault95f06062015-08-05 16:42:57 +0000389 .addImm(-7);
Tom Stellard89422762014-06-17 16:53:04 +0000390
391 // Restore EXEC
392 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
393 .addReg(Save);
394
Christian Konig2989ffc2013-03-18 11:34:16 +0000395 }
Christian Konig2989ffc2013-03-18 11:34:16 +0000396 MI.eraseFromParent();
397}
398
Tom Stellard8b0182a2015-04-23 20:32:01 +0000399/// \param @VecReg The register which holds element zero of the vector
400/// being addressed into.
401/// \param[out] @Reg The base register to use in the indirect addressing instruction.
402/// \param[in,out] @Offset As an input, this is the constant offset part of the
403// indirect Index. e.g. v0 = v[VecReg + Offset]
404// As an output, this is a constant value that needs
405// to be added to the value stored in M0.
406void SILowerControlFlowPass::computeIndirectRegAndOffset(unsigned VecReg,
407 unsigned &Reg,
408 int &Offset) {
409 unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
410 if (!SubReg)
411 SubReg = VecReg;
412
413 const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
414 int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
415
416 if (RegIdx < 0) {
417 Offset = RegIdx;
418 RegIdx = 0;
419 } else {
420 Offset = 0;
421 }
422
423 Reg = RC->getRegister(RegIdx);
424}
425
Christian Konig2989ffc2013-03-18 11:34:16 +0000426void SILowerControlFlowPass::IndirectSrc(MachineInstr &MI) {
427
428 MachineBasicBlock &MBB = *MI.getParent();
429 DebugLoc DL = MI.getDebugLoc();
430
431 unsigned Dst = MI.getOperand(0).getReg();
432 unsigned Vec = MI.getOperand(2).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000433 int Off = MI.getOperand(4).getImm();
434 unsigned Reg;
435
436 computeIndirectRegAndOffset(Vec, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000437
Tom Stellard81d871d2013-11-13 23:36:50 +0000438 MachineInstr *MovRel =
Christian Konig2989ffc2013-03-18 11:34:16 +0000439 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Tom Stellard8b0182a2015-04-23 20:32:01 +0000440 .addReg(Reg)
Christian Konig2989ffc2013-03-18 11:34:16 +0000441 .addReg(Vec, RegState::Implicit);
442
Tom Stellard8b0182a2015-04-23 20:32:01 +0000443 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000444}
445
446void SILowerControlFlowPass::IndirectDst(MachineInstr &MI) {
447
448 MachineBasicBlock &MBB = *MI.getParent();
449 DebugLoc DL = MI.getDebugLoc();
450
451 unsigned Dst = MI.getOperand(0).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000452 int Off = MI.getOperand(4).getImm();
Christian Konig2989ffc2013-03-18 11:34:16 +0000453 unsigned Val = MI.getOperand(5).getReg();
Tom Stellard8b0182a2015-04-23 20:32:01 +0000454 unsigned Reg;
455
456 computeIndirectRegAndOffset(Dst, Reg, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000457
458 MachineInstr *MovRel =
459 BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
Tom Stellard8b0182a2015-04-23 20:32:01 +0000460 .addReg(Reg, RegState::Define)
Christian Konig2989ffc2013-03-18 11:34:16 +0000461 .addReg(Val)
Christian Konig2989ffc2013-03-18 11:34:16 +0000462 .addReg(Dst, RegState::Implicit);
463
Tom Stellard8b0182a2015-04-23 20:32:01 +0000464 LoadM0(MI, MovRel, Off);
Christian Konig2989ffc2013-03-18 11:34:16 +0000465}
466
Tom Stellard75aadc22012-12-11 21:25:42 +0000467bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000468 TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
469 TRI =
470 static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
Tom Stellardd50bb3c2013-09-05 18:37:52 +0000471 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000472
473 bool HaveKill = false;
Christian Konig737d4a12013-03-26 14:03:50 +0000474 bool NeedWQM = false;
Matt Arsenault3f981402014-09-15 15:41:53 +0000475 bool NeedFlat = false;
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000476 unsigned Depth = 0;
Tom Stellard75aadc22012-12-11 21:25:42 +0000477
Tom Stellardf8794352012-12-19 22:10:31 +0000478 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
479 BI != BE; ++BI) {
480
481 MachineBasicBlock &MBB = *BI;
Tim Northover24f46612014-03-28 13:52:56 +0000482 MachineBasicBlock::iterator I, Next;
483 for (I = MBB.begin(); I != MBB.end(); I = Next) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000484 Next = std::next(I);
Tim Northover24f46612014-03-28 13:52:56 +0000485
Tom Stellard75aadc22012-12-11 21:25:42 +0000486 MachineInstr &MI = *I;
Matt Arsenault3add6432015-10-20 04:35:43 +0000487 if (TII->isWQM(MI) || TII->isDS(MI))
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000488 NeedWQM = true;
Tom Stellard5d7aaae2014-02-10 16:58:30 +0000489
Matt Arsenault3f981402014-09-15 15:41:53 +0000490 // Flat uses m0 in case it needs to access LDS.
Matt Arsenault3add6432015-10-20 04:35:43 +0000491 if (TII->isFLAT(MI))
Matt Arsenault3f981402014-09-15 15:41:53 +0000492 NeedFlat = true;
Matt Arsenault3f981402014-09-15 15:41:53 +0000493
Tom Stellard75aadc22012-12-11 21:25:42 +0000494 switch (MI.getOpcode()) {
495 default: break;
Tom Stellardf8794352012-12-19 22:10:31 +0000496 case AMDGPU::SI_IF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000497 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000498 If(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000499 break;
500
Tom Stellardf8794352012-12-19 22:10:31 +0000501 case AMDGPU::SI_ELSE:
502 Else(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000503 break;
504
Tom Stellardf8794352012-12-19 22:10:31 +0000505 case AMDGPU::SI_BREAK:
506 Break(MI);
507 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000508
Tom Stellardf8794352012-12-19 22:10:31 +0000509 case AMDGPU::SI_IF_BREAK:
510 IfBreak(MI);
511 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000512
Tom Stellardf8794352012-12-19 22:10:31 +0000513 case AMDGPU::SI_ELSE_BREAK:
514 ElseBreak(MI);
515 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000516
Tom Stellardf8794352012-12-19 22:10:31 +0000517 case AMDGPU::SI_LOOP:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000518 ++Depth;
Tom Stellardf8794352012-12-19 22:10:31 +0000519 Loop(MI);
520 break;
521
522 case AMDGPU::SI_END_CF:
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000523 if (--Depth == 0 && HaveKill) {
524 SkipIfDead(MI);
525 HaveKill = false;
526 }
Tom Stellardf8794352012-12-19 22:10:31 +0000527 EndCf(MI);
Tom Stellard75aadc22012-12-11 21:25:42 +0000528 break;
Tom Stellarde7b907d2012-12-19 22:10:33 +0000529
Tom Stellardbe8ebee2013-01-18 21:15:50 +0000530 case AMDGPU::SI_KILL:
531 if (Depth == 0)
532 SkipIfDead(MI);
533 else
534 HaveKill = true;
535 Kill(MI);
536 break;
537
Tom Stellarde7b907d2012-12-19 22:10:33 +0000538 case AMDGPU::S_BRANCH:
539 Branch(MI);
540 break;
Christian Konig2989ffc2013-03-18 11:34:16 +0000541
Matt Arsenault28419272015-10-07 00:42:51 +0000542 case AMDGPU::SI_INDIRECT_SRC_V1:
543 case AMDGPU::SI_INDIRECT_SRC_V2:
544 case AMDGPU::SI_INDIRECT_SRC_V4:
545 case AMDGPU::SI_INDIRECT_SRC_V8:
546 case AMDGPU::SI_INDIRECT_SRC_V16:
Christian Konig2989ffc2013-03-18 11:34:16 +0000547 IndirectSrc(MI);
548 break;
549
Tom Stellard81d871d2013-11-13 23:36:50 +0000550 case AMDGPU::SI_INDIRECT_DST_V1:
Christian Konig2989ffc2013-03-18 11:34:16 +0000551 case AMDGPU::SI_INDIRECT_DST_V2:
552 case AMDGPU::SI_INDIRECT_DST_V4:
553 case AMDGPU::SI_INDIRECT_DST_V8:
554 case AMDGPU::SI_INDIRECT_DST_V16:
555 IndirectDst(MI);
556 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000557 }
558 }
559 }
Tom Stellardf8794352012-12-19 22:10:31 +0000560
Matt Arsenault762af962014-07-13 03:06:39 +0000561 if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
Christian Konig737d4a12013-03-26 14:03:50 +0000562 MachineBasicBlock &MBB = MF.front();
563 BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
564 AMDGPU::EXEC).addReg(AMDGPU::EXEC);
565 }
566
Matt Arsenault3f981402014-09-15 15:41:53 +0000567 // FIXME: This seems inappropriate to do here.
568 if (NeedFlat && MFI->IsKernel) {
569 // Insert the prologue initializing the SGPRs pointing to the scratch space
570 // for flat accesses.
571 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
572
573 // TODO: What to use with function calls?
574
575 // FIXME: This is reporting stack size that is used in a scratch buffer
576 // rather than registers as well.
577 uint64_t StackSizeBytes = FrameInfo->getStackSize();
578
579 int IndirectBegin
580 = static_cast<const AMDGPUInstrInfo*>(TII)->getIndirectIndexBegin(MF);
581 // Convert register index to 256-byte unit.
582 uint64_t StackOffset = IndirectBegin < 0 ? 0 : (4 * IndirectBegin / 256);
583
584 assert((StackSizeBytes < 0xffff) && StackOffset < 0xffff &&
585 "Stack limits should be smaller than 16-bits");
586
587 // Initialize the flat scratch register pair.
588 // TODO: Can we use one s_mov_b64 here?
589
590 // Offset is in units of 256-bytes.
591 MachineBasicBlock &MBB = MF.front();
592 DebugLoc NoDL;
593 MachineBasicBlock::iterator Start = MBB.getFirstNonPHI();
594 const MCInstrDesc &SMovK = TII->get(AMDGPU::S_MOVK_I32);
595
Matt Arsenault77849922014-11-13 20:44:23 +0000596 assert(isInt<16>(StackOffset) && isInt<16>(StackSizeBytes));
597
Matt Arsenault3f981402014-09-15 15:41:53 +0000598 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_LO)
599 .addImm(StackOffset);
600
601 // Documentation says size is "per-thread scratch size in bytes"
602 BuildMI(MBB, Start, NoDL, SMovK, AMDGPU::FLAT_SCR_HI)
603 .addImm(StackSizeBytes);
604 }
605
Tom Stellard75aadc22012-12-11 21:25:42 +0000606 return true;
607}