1 //===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This pass lowers the pseudo control flow instructions (SI_IF_NZ, ELSE, ENDIF)
12 /// to predicated instructions.
14 /// All control flow (except loops) is handled using predicated instructions and
15 /// 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.
24 /// %VCC = V_CMP_GT_F32 %VGPR1, %VGPR2
26 /// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
28 /// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
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
35 /// S_CBRANCH_EXECZ label0 // This instruction is an
36 /// // optimization which allows us to
37 /// // branch if all the bits of
39 /// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0 // Do the IF block of the branch
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
48 /// %EXEC = S_OR_B64 %EXEC, %SGPR2 // Re-enable saved exec mask bits
49 //===----------------------------------------------------------------------===//
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"
63 class SILowerControlFlowPass : public MachineFunctionPass {
67 const TargetInstrInfo *TII;
68 std::vector<unsigned> PredicateStack;
69 std::vector<unsigned> UnusedRegisters;
72 void freeReg(unsigned Reg);
75 SILowerControlFlowPass(TargetMachine &tm) :
76 MachineFunctionPass(ID), TII(tm.getInstrInfo()) { }
78 virtual bool runOnMachineFunction(MachineFunction &MF);
80 const char *getPassName() const {
81 return "SI Lower control flow instructions";
86 } // End anonymous namespace
88 char SILowerControlFlowPass::ID = 0;
90 FunctionPass *llvm::createSILowerControlFlowPass(TargetMachine &tm) {
91 return new SILowerControlFlowPass(tm);
94 bool SILowerControlFlowPass::runOnMachineFunction(MachineFunction &MF) {
96 // Find all the unused registers that can be used for the predicate stack.
97 for (TargetRegisterClass::iterator I = AMDGPU::SReg_64RegClass.begin(),
98 S = AMDGPU::SReg_64RegClass.end();
101 if (!MF.getRegInfo().isPhysRegUsed(Reg)) {
102 UnusedRegisters.insert(UnusedRegisters.begin(), Reg);
106 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
108 MachineBasicBlock &MBB = *BB;
109 for (MachineBasicBlock::iterator I = MBB.begin(), Next = llvm::next(I);
110 I != MBB.end(); I = Next) {
111 Next = llvm::next(I);
112 MachineInstr &MI = *I;
114 switch (MI.getOpcode()) {
116 case AMDGPU::SI_IF_NZ:
118 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_AND_SAVEEXEC_B64),
120 .addOperand(MI.getOperand(0)); // VCC
121 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_XOR_B64),
124 .addReg(AMDGPU::EXEC);
125 MI.eraseFromParent();
126 PredicateStack.push_back(Reg);
130 Reg = PredicateStack.back();
131 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_OR_SAVEEXEC_B64),
134 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_XOR_B64),
137 .addReg(AMDGPU::EXEC);
138 MI.eraseFromParent();
142 Reg = PredicateStack.back();
143 PredicateStack.pop_back();
144 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_OR_B64),
146 .addReg(AMDGPU::EXEC)
150 if (MF.getInfo<SIMachineFunctionInfo>()->ShaderType == ShaderType::PIXEL &&
151 PredicateStack.empty()) {
152 // If the exec mask is non-zero, skip the next two instructions
153 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_CBRANCH_EXECNZ))
155 .addReg(AMDGPU::EXEC);
157 // Exec mask is zero: Export to NULL target...
158 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::EXP))
160 .addImm(0x09) // V_008DFC_SQ_EXP_NULL
164 .addReg(AMDGPU::SREG_LIT_0)
165 .addReg(AMDGPU::SREG_LIT_0)
166 .addReg(AMDGPU::SREG_LIT_0)
167 .addReg(AMDGPU::SREG_LIT_0);
169 // ... and terminate wavefront
170 BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(AMDGPU::S_ENDPGM));
172 MI.eraseFromParent();
180 unsigned SILowerControlFlowPass::allocReg() {
182 assert(!UnusedRegisters.empty() && "Ran out of registers for predicate stack");
183 unsigned Reg = UnusedRegisters.back();
184 UnusedRegisters.pop_back();
188 void SILowerControlFlowPass::freeReg(unsigned Reg) {
190 UnusedRegisters.push_back(Reg);