blob: 3780e4051af1c8cface5fa53d89aa51aa0b4fcd5 [file] [log] [blame]
Tom Stellardf98f2ce2012-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 Stellard6b7d99d2012-12-19 22:10:31 +000011/// \brief This pass lowers the pseudo control flow instructions to real
12/// machine instructions.
Tom Stellardf98f2ce2012-12-11 21:25:42 +000013///
Tom Stellard6b7d99d2012-12-19 22:10:31 +000014/// All control flow is handled using predicated instructions and
Tom Stellardf98f2ce2012-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 Stellard6b7d99d2012-12-19 22:10:31 +000025/// %SGPR0 = SI_IF %VCC
Tom Stellardf98f2ce2012-12-11 21:25:42 +000026/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
Tom Stellard6b7d99d2012-12-19 22:10:31 +000027/// %SGPR0 = SI_ELSE %SGPR0
Tom Stellardf98f2ce2012-12-11 21:25:42 +000028/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
Tom Stellard6b7d99d2012-12-19 22:10:31 +000029/// SI_END_CF %SGPR0
Tom Stellardf98f2ce2012-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 Stellard6b7d99d2012-12-19 22:10:31 +000035/// S_CBRANCH_EXECZ label0 // This instruction is an optional
Tom Stellardf98f2ce2012-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 Stellard6b7d99d2012-12-19 22:10:31 +000048/// %EXEC = S_OR_B64 %EXEC, %SGPR0 // Re-enable saved exec mask bits
Tom Stellardf98f2ce2012-12-11 21:25:42 +000049//===----------------------------------------------------------------------===//
50
51#include "AMDGPU.h"
52#include "SIInstrInfo.h"
53#include "SIMachineFunctionInfo.h"
54#include "llvm/CodeGen/MachineFunction.h"
55#include "llvm/CodeGen/MachineFunctionPass.h"
56#include "llvm/CodeGen/MachineInstrBuilder.h"
57#include "llvm/CodeGen/MachineRegisterInfo.h"
58
59using namespace llvm;
60
61namespace {
62
63class SILowerControlFlowPass : public MachineFunctionPass {
64
65private:
Tom Stellardd09d43a2012-12-19 22:10:33 +000066 static const unsigned SkipThreshold = 12;
67
Tom Stellardf98f2ce2012-12-11 21:25:42 +000068 static char ID;
69 const TargetInstrInfo *TII;
Tom Stellardf98f2ce2012-12-11 21:25:42 +000070
Tom Stellard935a9152013-01-18 21:15:50 +000071 bool shouldSkip(MachineBasicBlock *From, MachineBasicBlock *To);
72
73 void Skip(MachineInstr &From, MachineOperand &To);
74 void SkipIfDead(MachineInstr &MI);
Tom Stellardd09d43a2012-12-19 22:10:33 +000075
Tom Stellard6b7d99d2012-12-19 22:10:31 +000076 void If(MachineInstr &MI);
77 void Else(MachineInstr &MI);
78 void Break(MachineInstr &MI);
79 void IfBreak(MachineInstr &MI);
80 void ElseBreak(MachineInstr &MI);
81 void Loop(MachineInstr &MI);
82 void EndCf(MachineInstr &MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +000083
Tom Stellard935a9152013-01-18 21:15:50 +000084 void Kill(MachineInstr &MI);
Tom Stellardd09d43a2012-12-19 22:10:33 +000085 void Branch(MachineInstr &MI);
86
Tom Stellardf98f2ce2012-12-11 21:25:42 +000087public:
88 SILowerControlFlowPass(TargetMachine &tm) :
89 MachineFunctionPass(ID), TII(tm.getInstrInfo()) { }
90
91 virtual bool runOnMachineFunction(MachineFunction &MF);
92
93 const char *getPassName() const {
94 return "SI Lower control flow instructions";
95 }
96
97};
98
99} // End anonymous namespace
100
101char SILowerControlFlowPass::ID = 0;
102
103FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
104 return new SILowerControlFlowPass(tm);
105}
106
Tom Stellard935a9152013-01-18 21:15:50 +0000107bool SILowerControlFlowPass::shouldSkip(MachineBasicBlock *From,
108 MachineBasicBlock *To) {
109
Tom Stellardd09d43a2012-12-19 22:10:33 +0000110 unsigned NumInstr = 0;
111
Tom Stellard935a9152013-01-18 21:15:50 +0000112 for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
Tom Stellardd09d43a2012-12-19 22:10:33 +0000113 MBB = *MBB->succ_begin()) {
114
115 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
116 NumInstr < SkipThreshold && I != E; ++I) {
117
118 if (I->isBundle() || !I->isBundled())
Tom Stellard935a9152013-01-18 21:15:50 +0000119 if (++NumInstr >= SkipThreshold)
120 return true;
Tom Stellardd09d43a2012-12-19 22:10:33 +0000121 }
122 }
123
Tom Stellard935a9152013-01-18 21:15:50 +0000124 return false;
125}
126
127void SILowerControlFlowPass::Skip(MachineInstr &From, MachineOperand &To) {
128
129 if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
Tom Stellardd09d43a2012-12-19 22:10:33 +0000130 return;
131
132 DebugLoc DL = From.getDebugLoc();
133 BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
134 .addOperand(To)
135 .addReg(AMDGPU::EXEC);
136}
137
Tom Stellard935a9152013-01-18 21:15:50 +0000138void SILowerControlFlowPass::SkipIfDead(MachineInstr &MI) {
139
140 MachineBasicBlock &MBB = *MI.getParent();
141 DebugLoc DL = MI.getDebugLoc();
142
143 if (!shouldSkip(&MBB, &MBB.getParent()->back()))
144 return;
145
146 MachineBasicBlock::iterator Insert = &MI;
147 ++Insert;
148
149 // If the exec mask is non-zero, skip the next two instructions
150 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
151 .addImm(3)
152 .addReg(AMDGPU::EXEC);
153
154 // Exec mask is zero: Export to NULL target...
155 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
156 .addImm(0)
157 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
158 .addImm(0)
159 .addImm(1)
160 .addImm(1)
161 .addReg(AMDGPU::SREG_LIT_0)
162 .addReg(AMDGPU::SREG_LIT_0)
163 .addReg(AMDGPU::SREG_LIT_0)
164 .addReg(AMDGPU::SREG_LIT_0);
165
166 // ... and terminate wavefront
167 BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
168}
169
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000170void SILowerControlFlowPass::If(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000171 MachineBasicBlock &MBB = *MI.getParent();
172 DebugLoc DL = MI.getDebugLoc();
173 unsigned Reg = MI.getOperand(0).getReg();
174 unsigned Vcc = MI.getOperand(1).getReg();
175
176 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
177 .addReg(Vcc);
178
179 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
180 .addReg(AMDGPU::EXEC)
181 .addReg(Reg);
182
Tom Stellardd09d43a2012-12-19 22:10:33 +0000183 Skip(MI, MI.getOperand(2));
184
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000185 MI.eraseFromParent();
186}
187
188void SILowerControlFlowPass::Else(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000189 MachineBasicBlock &MBB = *MI.getParent();
190 DebugLoc DL = MI.getDebugLoc();
191 unsigned Dst = MI.getOperand(0).getReg();
192 unsigned Src = MI.getOperand(1).getReg();
193
194 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
195 .addReg(Src); // Saved EXEC
196
197 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
198 .addReg(AMDGPU::EXEC)
199 .addReg(Dst);
200
Tom Stellardd09d43a2012-12-19 22:10:33 +0000201 Skip(MI, MI.getOperand(2));
202
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000203 MI.eraseFromParent();
204}
205
206void SILowerControlFlowPass::Break(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000207 MachineBasicBlock &MBB = *MI.getParent();
208 DebugLoc DL = MI.getDebugLoc();
209
210 unsigned Dst = MI.getOperand(0).getReg();
211 unsigned Src = MI.getOperand(1).getReg();
212
213 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
214 .addReg(AMDGPU::EXEC)
215 .addReg(Src);
216
217 MI.eraseFromParent();
218}
219
220void SILowerControlFlowPass::IfBreak(MachineInstr &MI) {
Tom Stellard6b7d99d2012-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 Vcc = MI.getOperand(1).getReg();
226 unsigned Src = MI.getOperand(2).getReg();
227
228 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
229 .addReg(Vcc)
230 .addReg(Src);
231
232 MI.eraseFromParent();
233}
234
235void SILowerControlFlowPass::ElseBreak(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000236 MachineBasicBlock &MBB = *MI.getParent();
237 DebugLoc DL = MI.getDebugLoc();
238
239 unsigned Dst = MI.getOperand(0).getReg();
240 unsigned Saved = MI.getOperand(1).getReg();
241 unsigned Src = MI.getOperand(2).getReg();
242
243 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
244 .addReg(Saved)
245 .addReg(Src);
246
247 MI.eraseFromParent();
248}
249
250void SILowerControlFlowPass::Loop(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000251 MachineBasicBlock &MBB = *MI.getParent();
252 DebugLoc DL = MI.getDebugLoc();
253 unsigned Src = MI.getOperand(0).getReg();
254
255 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
256 .addReg(AMDGPU::EXEC)
257 .addReg(Src);
258
259 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
260 .addOperand(MI.getOperand(1))
261 .addReg(AMDGPU::EXEC);
262
263 MI.eraseFromParent();
264}
265
266void SILowerControlFlowPass::EndCf(MachineInstr &MI) {
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000267 MachineBasicBlock &MBB = *MI.getParent();
268 DebugLoc DL = MI.getDebugLoc();
269 unsigned Reg = MI.getOperand(0).getReg();
270
271 BuildMI(MBB, MBB.getFirstNonPHI(), DL,
272 TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
273 .addReg(AMDGPU::EXEC)
274 .addReg(Reg);
275
276 MI.eraseFromParent();
277}
278
Tom Stellardd09d43a2012-12-19 22:10:33 +0000279void SILowerControlFlowPass::Branch(MachineInstr &MI) {
Tom Stellardd09d43a2012-12-19 22:10:33 +0000280 MachineBasicBlock *Next = MI.getParent()->getNextNode();
281 MachineBasicBlock *Target = MI.getOperand(0).getMBB();
282 if (Target == Next)
283 MI.eraseFromParent();
284 else
285 assert(0);
286}
287
Tom Stellard935a9152013-01-18 21:15:50 +0000288void SILowerControlFlowPass::Kill(MachineInstr &MI) {
289
290 MachineBasicBlock &MBB = *MI.getParent();
291 DebugLoc DL = MI.getDebugLoc();
292
293 // Kill is only allowed in pixel shaders
294 MachineFunction &MF = *MBB.getParent();
295 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
296 assert(Info->ShaderType == ShaderType::PIXEL);
297
298 // Clear this pixel from the exec mask if the operand is negative
299 BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32), AMDGPU::VCC)
300 .addReg(AMDGPU::SREG_LIT_0)
301 .addOperand(MI.getOperand(0));
302
303 MI.eraseFromParent();
304}
305
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000306bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard935a9152013-01-18 21:15:50 +0000307
308 bool HaveKill = false;
309 unsigned Depth = 0;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000310
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000311 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
312 BI != BE; ++BI) {
313
314 MachineBasicBlock &MBB = *BI;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000315 for (MachineBasicBlock::iterator I = MBB.begin(), Next = llvm::next(I);
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000316 I != MBB.end(); I = Next) {
317
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000318 Next = llvm::next(I);
319 MachineInstr &MI = *I;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000320 switch (MI.getOpcode()) {
321 default: break;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000322 case AMDGPU::SI_IF:
Tom Stellard935a9152013-01-18 21:15:50 +0000323 ++Depth;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000324 If(MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000325 break;
326
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000327 case AMDGPU::SI_ELSE:
328 Else(MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000329 break;
330
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000331 case AMDGPU::SI_BREAK:
332 Break(MI);
333 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000334
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000335 case AMDGPU::SI_IF_BREAK:
336 IfBreak(MI);
337 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000338
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000339 case AMDGPU::SI_ELSE_BREAK:
340 ElseBreak(MI);
341 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000342
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000343 case AMDGPU::SI_LOOP:
Tom Stellard935a9152013-01-18 21:15:50 +0000344 ++Depth;
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000345 Loop(MI);
346 break;
347
348 case AMDGPU::SI_END_CF:
Tom Stellard935a9152013-01-18 21:15:50 +0000349 if (--Depth == 0 && HaveKill) {
350 SkipIfDead(MI);
351 HaveKill = false;
352 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000353 EndCf(MI);
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000354 break;
Tom Stellardd09d43a2012-12-19 22:10:33 +0000355
Tom Stellard935a9152013-01-18 21:15:50 +0000356 case AMDGPU::SI_KILL:
357 if (Depth == 0)
358 SkipIfDead(MI);
359 else
360 HaveKill = true;
361 Kill(MI);
362 break;
363
Tom Stellardd09d43a2012-12-19 22:10:33 +0000364 case AMDGPU::S_BRANCH:
365 Branch(MI);
366 break;
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000367 }
368 }
369 }
Tom Stellard6b7d99d2012-12-19 22:10:31 +0000370
Tom Stellardf98f2ce2012-12-11 21:25:42 +0000371 return true;
372}