1 //===-- SIOptimizeExecMasking.cpp -----------------------------------------===//
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 #include "AMDGPUSubtarget.h"
12 #include "SIInstrInfo.h"
13 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
14 #include "llvm/ADT/SmallSet.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineRegisterInfo.h"
18 #include "llvm/Support/Debug.h"
22 #define DEBUG_TYPE "si-optimize-exec-masking"
26 class SIOptimizeExecMasking : public MachineFunctionPass {
31 SIOptimizeExecMasking() : MachineFunctionPass(ID) {
32 initializeSIOptimizeExecMaskingPass(*PassRegistry::getPassRegistry());
35 bool runOnMachineFunction(MachineFunction &MF) override;
37 StringRef getPassName() const override {
38 return "SI optimize exec mask operations";
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 MachineFunctionPass::getAnalysisUsage(AU);
47 } // End anonymous namespace.
49 INITIALIZE_PASS_BEGIN(SIOptimizeExecMasking, DEBUG_TYPE,
50 "SI optimize exec mask operations", false, false)
51 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
52 INITIALIZE_PASS_END(SIOptimizeExecMasking, DEBUG_TYPE,
53 "SI optimize exec mask operations", false, false)
55 char SIOptimizeExecMasking::ID = 0;
57 char &llvm::SIOptimizeExecMaskingID = SIOptimizeExecMasking::ID;
59 /// If \p MI is a copy from exec, return the register copied to.
60 static unsigned isCopyFromExec(const MachineInstr &MI) {
61 switch (MI.getOpcode()) {
63 case AMDGPU::S_MOV_B64:
64 case AMDGPU::S_MOV_B64_term: {
65 const MachineOperand &Src = MI.getOperand(1);
66 if (Src.isReg() && Src.getReg() == AMDGPU::EXEC)
67 return MI.getOperand(0).getReg();
71 return AMDGPU::NoRegister;
74 /// If \p MI is a copy to exec, return the register copied from.
75 static unsigned isCopyToExec(const MachineInstr &MI) {
76 switch (MI.getOpcode()) {
78 case AMDGPU::S_MOV_B64: {
79 const MachineOperand &Dst = MI.getOperand(0);
80 if (Dst.isReg() && Dst.getReg() == AMDGPU::EXEC && MI.getOperand(1).isReg())
81 return MI.getOperand(1).getReg();
84 case AMDGPU::S_MOV_B64_term:
85 llvm_unreachable("should have been replaced");
88 return AMDGPU::NoRegister;
91 /// If \p MI is a logical operation on an exec value,
92 /// return the register copied to.
93 static unsigned isLogicalOpOnExec(const MachineInstr &MI) {
94 switch (MI.getOpcode()) {
95 case AMDGPU::S_AND_B64:
96 case AMDGPU::S_OR_B64:
97 case AMDGPU::S_XOR_B64:
98 case AMDGPU::S_ANDN2_B64:
99 case AMDGPU::S_ORN2_B64:
100 case AMDGPU::S_NAND_B64:
101 case AMDGPU::S_NOR_B64:
102 case AMDGPU::S_XNOR_B64: {
103 const MachineOperand &Src1 = MI.getOperand(1);
104 if (Src1.isReg() && Src1.getReg() == AMDGPU::EXEC)
105 return MI.getOperand(0).getReg();
106 const MachineOperand &Src2 = MI.getOperand(2);
107 if (Src2.isReg() && Src2.getReg() == AMDGPU::EXEC)
108 return MI.getOperand(0).getReg();
112 return AMDGPU::NoRegister;
115 static unsigned getSaveExecOp(unsigned Opc) {
117 case AMDGPU::S_AND_B64:
118 return AMDGPU::S_AND_SAVEEXEC_B64;
119 case AMDGPU::S_OR_B64:
120 return AMDGPU::S_OR_SAVEEXEC_B64;
121 case AMDGPU::S_XOR_B64:
122 return AMDGPU::S_XOR_SAVEEXEC_B64;
123 case AMDGPU::S_ANDN2_B64:
124 return AMDGPU::S_ANDN2_SAVEEXEC_B64;
125 case AMDGPU::S_ORN2_B64:
126 return AMDGPU::S_ORN2_SAVEEXEC_B64;
127 case AMDGPU::S_NAND_B64:
128 return AMDGPU::S_NAND_SAVEEXEC_B64;
129 case AMDGPU::S_NOR_B64:
130 return AMDGPU::S_NOR_SAVEEXEC_B64;
131 case AMDGPU::S_XNOR_B64:
132 return AMDGPU::S_XNOR_SAVEEXEC_B64;
134 return AMDGPU::INSTRUCTION_LIST_END;
138 // These are only terminators to get correct spill code placement during
139 // register allocation, so turn them back into normal instructions. Only one of
140 // these is expected per block.
141 static bool removeTerminatorBit(const SIInstrInfo &TII, MachineInstr &MI) {
142 switch (MI.getOpcode()) {
143 case AMDGPU::S_MOV_B64_term: {
144 MI.setDesc(TII.get(AMDGPU::COPY));
147 case AMDGPU::S_XOR_B64_term: {
148 // This is only a terminator to get the correct spill code placement during
149 // register allocation.
150 MI.setDesc(TII.get(AMDGPU::S_XOR_B64));
153 case AMDGPU::S_ANDN2_B64_term: {
154 // This is only a terminator to get the correct spill code placement during
155 // register allocation.
156 MI.setDesc(TII.get(AMDGPU::S_ANDN2_B64));
164 static MachineBasicBlock::reverse_iterator fixTerminators(
165 const SIInstrInfo &TII,
166 MachineBasicBlock &MBB) {
167 MachineBasicBlock::reverse_iterator I = MBB.rbegin(), E = MBB.rend();
168 for (; I != E; ++I) {
169 if (!I->isTerminator())
172 if (removeTerminatorBit(TII, *I))
179 static MachineBasicBlock::reverse_iterator findExecCopy(
180 const SIInstrInfo &TII,
181 MachineBasicBlock &MBB,
182 MachineBasicBlock::reverse_iterator I,
183 unsigned CopyToExec) {
184 const unsigned InstLimit = 25;
187 for (unsigned N = 0; N <= InstLimit && I != E; ++I, ++N) {
188 unsigned CopyFromExec = isCopyFromExec(*I);
189 if (CopyFromExec != AMDGPU::NoRegister)
196 // XXX - Seems LivePhysRegs doesn't work correctly since it will incorrectly
197 // repor tthe register as unavailable because a super-register with a lane mask
199 static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg) {
200 for (MachineBasicBlock *Succ : MBB.successors()) {
201 if (Succ->isLiveIn(Reg))
208 bool SIOptimizeExecMasking::runOnMachineFunction(MachineFunction &MF) {
209 if (skipFunction(MF.getFunction()))
212 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
213 const SIRegisterInfo *TRI = ST.getRegisterInfo();
214 const SIInstrInfo *TII = ST.getInstrInfo();
216 // Optimize sequences emitted for control flow lowering. They are originally
217 // emitted as the separate operations because spill code may need to be
218 // inserted for the saved copy of exec.
221 // z = s_<op>_b64 x, y
224 // x = s_<op>_saveexec_b64 y
227 for (MachineBasicBlock &MBB : MF) {
228 MachineBasicBlock::reverse_iterator I = fixTerminators(*TII, MBB);
229 MachineBasicBlock::reverse_iterator E = MBB.rend();
233 unsigned CopyToExec = isCopyToExec(*I);
234 if (CopyToExec == AMDGPU::NoRegister)
237 // Scan backwards to find the def.
238 auto CopyToExecInst = &*I;
239 auto CopyFromExecInst = findExecCopy(*TII, MBB, I, CopyToExec);
240 if (CopyFromExecInst == E) {
241 auto PrepareExecInst = std::next(I);
242 if (PrepareExecInst == E)
244 // Fold exec = COPY (S_AND_B64 reg, exec) -> exec = S_AND_B64 reg, exec
245 if (CopyToExecInst->getOperand(1).isKill() &&
246 isLogicalOpOnExec(*PrepareExecInst) == CopyToExec) {
247 LLVM_DEBUG(dbgs() << "Fold exec copy: " << *PrepareExecInst);
249 PrepareExecInst->getOperand(0).setReg(AMDGPU::EXEC);
251 LLVM_DEBUG(dbgs() << "into: " << *PrepareExecInst << '\n');
253 CopyToExecInst->eraseFromParent();
259 if (isLiveOut(MBB, CopyToExec)) {
260 // The copied register is live out and has a second use in another block.
261 LLVM_DEBUG(dbgs() << "Exec copy source register is live out\n");
265 unsigned CopyFromExec = CopyFromExecInst->getOperand(0).getReg();
266 MachineInstr *SaveExecInst = nullptr;
267 SmallVector<MachineInstr *, 4> OtherUseInsts;
269 for (MachineBasicBlock::iterator J
270 = std::next(CopyFromExecInst->getIterator()), JE = I->getIterator();
272 if (SaveExecInst && J->readsRegister(AMDGPU::EXEC, TRI)) {
273 LLVM_DEBUG(dbgs() << "exec read prevents saveexec: " << *J << '\n');
274 // Make sure this is inserted after any VALU ops that may have been
275 // scheduled in between.
276 SaveExecInst = nullptr;
280 bool ReadsCopyFromExec = J->readsRegister(CopyFromExec, TRI);
282 if (J->modifiesRegister(CopyToExec, TRI)) {
284 LLVM_DEBUG(dbgs() << "Multiple instructions modify "
285 << printReg(CopyToExec, TRI) << '\n');
286 SaveExecInst = nullptr;
290 unsigned SaveExecOp = getSaveExecOp(J->getOpcode());
291 if (SaveExecOp == AMDGPU::INSTRUCTION_LIST_END)
294 if (ReadsCopyFromExec) {
296 LLVM_DEBUG(dbgs() << "Found save exec op: " << *SaveExecInst << '\n');
300 << "Instruction does not read exec copy: " << *J << '\n');
303 } else if (ReadsCopyFromExec && !SaveExecInst) {
304 // Make sure no other instruction is trying to use this copy, before it
305 // will be rewritten by the saveexec, i.e. hasOneUse. There may have
306 // been another use, such as an inserted spill. For example:
308 // %sgpr0_sgpr1 = COPY %exec
309 // spill %sgpr0_sgpr1
310 // %sgpr2_sgpr3 = S_AND_B64 %sgpr0_sgpr1
312 LLVM_DEBUG(dbgs() << "Found second use of save inst candidate: " << *J
317 if (SaveExecInst && J->readsRegister(CopyToExec, TRI)) {
318 assert(SaveExecInst != &*J);
319 OtherUseInsts.push_back(&*J);
326 LLVM_DEBUG(dbgs() << "Insert save exec op: " << *SaveExecInst << '\n');
328 MachineOperand &Src0 = SaveExecInst->getOperand(1);
329 MachineOperand &Src1 = SaveExecInst->getOperand(2);
331 MachineOperand *OtherOp = nullptr;
333 if (Src0.isReg() && Src0.getReg() == CopyFromExec) {
335 } else if (Src1.isReg() && Src1.getReg() == CopyFromExec) {
336 if (!SaveExecInst->isCommutable())
341 llvm_unreachable("unexpected");
343 CopyFromExecInst->eraseFromParent();
345 auto InsPt = SaveExecInst->getIterator();
346 const DebugLoc &DL = SaveExecInst->getDebugLoc();
348 BuildMI(MBB, InsPt, DL, TII->get(getSaveExecOp(SaveExecInst->getOpcode())),
350 .addReg(OtherOp->getReg());
351 SaveExecInst->eraseFromParent();
353 CopyToExecInst->eraseFromParent();
355 for (MachineInstr *OtherInst : OtherUseInsts) {
356 OtherInst->substituteRegister(CopyToExec, AMDGPU::EXEC,
357 AMDGPU::NoSubRegister, *TRI);