10b57cec5SDimitry Andric //===-- SIOptimizeExecMaskingPreRA.cpp ------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric /// \file 105ffd83dbSDimitry Andric /// This pass performs exec mask handling peephole optimizations which needs 115ffd83dbSDimitry Andric /// to be done before register allocation to reduce register pressure. 120b57cec5SDimitry Andric /// 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "AMDGPU.h" 16e8d8bef9SDimitry Andric #include "GCNSubtarget.h" 170b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 20480093f4SDimitry Andric #include "llvm/InitializePasses.h" 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric using namespace llvm; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric #define DEBUG_TYPE "si-optimize-exec-masking-pre-ra" 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric namespace { 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric class SIOptimizeExecMaskingPreRA : public MachineFunctionPass { 290b57cec5SDimitry Andric private: 300b57cec5SDimitry Andric const SIRegisterInfo *TRI; 310b57cec5SDimitry Andric const SIInstrInfo *TII; 320b57cec5SDimitry Andric MachineRegisterInfo *MRI; 33e8d8bef9SDimitry Andric LiveIntervals *LIS; 34e8d8bef9SDimitry Andric 35e8d8bef9SDimitry Andric unsigned AndOpc; 36e8d8bef9SDimitry Andric unsigned Andn2Opc; 37e8d8bef9SDimitry Andric unsigned OrSaveExecOpc; 38e8d8bef9SDimitry Andric unsigned XorTermrOpc; 39e8d8bef9SDimitry Andric MCRegister CondReg; 40e8d8bef9SDimitry Andric MCRegister ExecReg; 41e8d8bef9SDimitry Andric 42e8d8bef9SDimitry Andric Register optimizeVcndVcmpPair(MachineBasicBlock &MBB); 43e8d8bef9SDimitry Andric bool optimizeElseBranch(MachineBasicBlock &MBB); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric public: 460b57cec5SDimitry Andric static char ID; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric SIOptimizeExecMaskingPreRA() : MachineFunctionPass(ID) { 490b57cec5SDimitry Andric initializeSIOptimizeExecMaskingPreRAPass(*PassRegistry::getPassRegistry()); 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric StringRef getPassName() const override { 550b57cec5SDimitry Andric return "SI optimize exec mask operations pre-RA"; 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 590b57cec5SDimitry Andric AU.addRequired<LiveIntervals>(); 600b57cec5SDimitry Andric AU.setPreservesAll(); 610b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric }; 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric } // End anonymous namespace. 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(SIOptimizeExecMaskingPreRA, DEBUG_TYPE, 680b57cec5SDimitry Andric "SI optimize exec mask operations pre-RA", false, false) 690b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 700b57cec5SDimitry Andric INITIALIZE_PASS_END(SIOptimizeExecMaskingPreRA, DEBUG_TYPE, 710b57cec5SDimitry Andric "SI optimize exec mask operations pre-RA", false, false) 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric char SIOptimizeExecMaskingPreRA::ID = 0; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric char &llvm::SIOptimizeExecMaskingPreRAID = SIOptimizeExecMaskingPreRA::ID; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric FunctionPass *llvm::createSIOptimizeExecMaskingPreRAPass() { 780b57cec5SDimitry Andric return new SIOptimizeExecMaskingPreRA(); 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 81e8d8bef9SDimitry Andric // See if there is a def between \p AndIdx and \p SelIdx that needs to live 82e8d8bef9SDimitry Andric // beyond \p AndIdx. 83e8d8bef9SDimitry Andric static bool isDefBetween(const LiveRange &LR, SlotIndex AndIdx, 84e8d8bef9SDimitry Andric SlotIndex SelIdx) { 85e8d8bef9SDimitry Andric LiveQueryResult AndLRQ = LR.Query(AndIdx); 86e8d8bef9SDimitry Andric return (!AndLRQ.isKill() && AndLRQ.valueIn() != LR.Query(SelIdx).valueOut()); 87e8d8bef9SDimitry Andric } 880b57cec5SDimitry Andric 89e8d8bef9SDimitry Andric // FIXME: Why do we bother trying to handle physical registers here? 90e8d8bef9SDimitry Andric static bool isDefBetween(const SIRegisterInfo &TRI, 91e8d8bef9SDimitry Andric LiveIntervals *LIS, Register Reg, 92e8d8bef9SDimitry Andric const MachineInstr &Sel, const MachineInstr &And) { 93e8d8bef9SDimitry Andric SlotIndex AndIdx = LIS->getInstructionIndex(And); 94e8d8bef9SDimitry Andric SlotIndex SelIdx = LIS->getInstructionIndex(Sel); 95e8d8bef9SDimitry Andric 96e8d8bef9SDimitry Andric if (Reg.isVirtual()) 97e8d8bef9SDimitry Andric return isDefBetween(LIS->getInterval(Reg), AndIdx, SelIdx); 98e8d8bef9SDimitry Andric 99e8d8bef9SDimitry Andric for (MCRegUnitIterator UI(Reg.asMCReg(), &TRI); UI.isValid(); ++UI) { 100e8d8bef9SDimitry Andric if (isDefBetween(LIS->getRegUnit(*UI), AndIdx, SelIdx)) 1010b57cec5SDimitry Andric return true; 102e8d8bef9SDimitry Andric } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric return false; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Optimize sequence 1080b57cec5SDimitry Andric // %sel = V_CNDMASK_B32_e64 0, 1, %cc 1090b57cec5SDimitry Andric // %cmp = V_CMP_NE_U32 1, %1 1100b57cec5SDimitry Andric // $vcc = S_AND_B64 $exec, %cmp 1110b57cec5SDimitry Andric // S_CBRANCH_VCC[N]Z 1120b57cec5SDimitry Andric // => 1130b57cec5SDimitry Andric // $vcc = S_ANDN2_B64 $exec, %cc 1140b57cec5SDimitry Andric // S_CBRANCH_VCC[N]Z 1150b57cec5SDimitry Andric // 1160b57cec5SDimitry Andric // It is the negation pattern inserted by DAGCombiner::visitBRCOND() in the 1170b57cec5SDimitry Andric // rebuildSetCC(). We start with S_CBRANCH to avoid exhaustive search, but 1180b57cec5SDimitry Andric // only 3 first instructions are really needed. S_AND_B64 with exec is a 1190b57cec5SDimitry Andric // required part of the pattern since V_CNDMASK_B32 writes zeroes for inactive 1200b57cec5SDimitry Andric // lanes. 1210b57cec5SDimitry Andric // 1220b57cec5SDimitry Andric // Returns %cc register on success. 123e8d8bef9SDimitry Andric Register 124e8d8bef9SDimitry Andric SIOptimizeExecMaskingPreRA::optimizeVcndVcmpPair(MachineBasicBlock &MBB) { 1250b57cec5SDimitry Andric auto I = llvm::find_if(MBB.terminators(), [](const MachineInstr &MI) { 1260b57cec5SDimitry Andric unsigned Opc = MI.getOpcode(); 1270b57cec5SDimitry Andric return Opc == AMDGPU::S_CBRANCH_VCCZ || 1280b57cec5SDimitry Andric Opc == AMDGPU::S_CBRANCH_VCCNZ; }); 1290b57cec5SDimitry Andric if (I == MBB.terminators().end()) 130e8d8bef9SDimitry Andric return Register(); 1310b57cec5SDimitry Andric 132e8d8bef9SDimitry Andric auto *And = 133e8d8bef9SDimitry Andric TRI->findReachingDef(CondReg, AMDGPU::NoSubRegister, *I, *MRI, LIS); 1340b57cec5SDimitry Andric if (!And || And->getOpcode() != AndOpc || 1350b57cec5SDimitry Andric !And->getOperand(1).isReg() || !And->getOperand(2).isReg()) 136e8d8bef9SDimitry Andric return Register(); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric MachineOperand *AndCC = &And->getOperand(1); 1398bcb0991SDimitry Andric Register CmpReg = AndCC->getReg(); 1400b57cec5SDimitry Andric unsigned CmpSubReg = AndCC->getSubReg(); 141e8d8bef9SDimitry Andric if (CmpReg == Register(ExecReg)) { 1420b57cec5SDimitry Andric AndCC = &And->getOperand(2); 1430b57cec5SDimitry Andric CmpReg = AndCC->getReg(); 1440b57cec5SDimitry Andric CmpSubReg = AndCC->getSubReg(); 145e8d8bef9SDimitry Andric } else if (And->getOperand(2).getReg() != Register(ExecReg)) { 146e8d8bef9SDimitry Andric return Register(); 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric 149e8d8bef9SDimitry Andric auto *Cmp = TRI->findReachingDef(CmpReg, CmpSubReg, *And, *MRI, LIS); 1500b57cec5SDimitry Andric if (!Cmp || !(Cmp->getOpcode() == AMDGPU::V_CMP_NE_U32_e32 || 1510b57cec5SDimitry Andric Cmp->getOpcode() == AMDGPU::V_CMP_NE_U32_e64) || 1520b57cec5SDimitry Andric Cmp->getParent() != And->getParent()) 153e8d8bef9SDimitry Andric return Register(); 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric MachineOperand *Op1 = TII->getNamedOperand(*Cmp, AMDGPU::OpName::src0); 1560b57cec5SDimitry Andric MachineOperand *Op2 = TII->getNamedOperand(*Cmp, AMDGPU::OpName::src1); 1570b57cec5SDimitry Andric if (Op1->isImm() && Op2->isReg()) 1580b57cec5SDimitry Andric std::swap(Op1, Op2); 1590b57cec5SDimitry Andric if (!Op1->isReg() || !Op2->isImm() || Op2->getImm() != 1) 160e8d8bef9SDimitry Andric return Register(); 1610b57cec5SDimitry Andric 1628bcb0991SDimitry Andric Register SelReg = Op1->getReg(); 163e8d8bef9SDimitry Andric auto *Sel = TRI->findReachingDef(SelReg, Op1->getSubReg(), *Cmp, *MRI, LIS); 1640b57cec5SDimitry Andric if (!Sel || Sel->getOpcode() != AMDGPU::V_CNDMASK_B32_e64) 165e8d8bef9SDimitry Andric return Register(); 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric if (TII->hasModifiersSet(*Sel, AMDGPU::OpName::src0_modifiers) || 1680b57cec5SDimitry Andric TII->hasModifiersSet(*Sel, AMDGPU::OpName::src1_modifiers)) 169e8d8bef9SDimitry Andric return Register(); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric Op1 = TII->getNamedOperand(*Sel, AMDGPU::OpName::src0); 1720b57cec5SDimitry Andric Op2 = TII->getNamedOperand(*Sel, AMDGPU::OpName::src1); 1730b57cec5SDimitry Andric MachineOperand *CC = TII->getNamedOperand(*Sel, AMDGPU::OpName::src2); 1740b57cec5SDimitry Andric if (!Op1->isImm() || !Op2->isImm() || !CC->isReg() || 1750b57cec5SDimitry Andric Op1->getImm() != 0 || Op2->getImm() != 1) 176e8d8bef9SDimitry Andric return Register(); 177e8d8bef9SDimitry Andric 178e8d8bef9SDimitry Andric Register CCReg = CC->getReg(); 179e8d8bef9SDimitry Andric 180e8d8bef9SDimitry Andric // If there was a def between the select and the and, we would need to move it 181e8d8bef9SDimitry Andric // to fold this. 182e8d8bef9SDimitry Andric if (isDefBetween(*TRI, LIS, CCReg, *Sel, *And)) 183e8d8bef9SDimitry Andric return Register(); 1840b57cec5SDimitry Andric 1858bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Folding sequence:\n\t" << *Sel << '\t' << *Cmp << '\t' 1868bcb0991SDimitry Andric << *And); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(*And); 1898bcb0991SDimitry Andric MachineInstr *Andn2 = 1908bcb0991SDimitry Andric BuildMI(MBB, *And, And->getDebugLoc(), TII->get(Andn2Opc), 1918bcb0991SDimitry Andric And->getOperand(0).getReg()) 1920b57cec5SDimitry Andric .addReg(ExecReg) 1938bcb0991SDimitry Andric .addReg(CCReg, getUndefRegState(CC->isUndef()), CC->getSubReg()); 1945ffd83dbSDimitry Andric MachineOperand &AndSCC = And->getOperand(3); 1955ffd83dbSDimitry Andric assert(AndSCC.getReg() == AMDGPU::SCC); 1965ffd83dbSDimitry Andric MachineOperand &Andn2SCC = Andn2->getOperand(3); 1975ffd83dbSDimitry Andric assert(Andn2SCC.getReg() == AMDGPU::SCC); 1985ffd83dbSDimitry Andric Andn2SCC.setIsDead(AndSCC.isDead()); 1990b57cec5SDimitry Andric And->eraseFromParent(); 2000b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Andn2); 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "=>\n\t" << *Andn2 << '\n'); 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // Try to remove compare. Cmp value should not used in between of cmp 2050b57cec5SDimitry Andric // and s_and_b64 if VCC or just unused if any other register. 206e8d8bef9SDimitry Andric if ((CmpReg.isVirtual() && MRI->use_nodbg_empty(CmpReg)) || 207e8d8bef9SDimitry Andric (CmpReg == Register(CondReg) && 2080b57cec5SDimitry Andric std::none_of(std::next(Cmp->getIterator()), Andn2->getIterator(), 2090b57cec5SDimitry Andric [&](const MachineInstr &MI) { 2108bcb0991SDimitry Andric return MI.readsRegister(CondReg, TRI); 2118bcb0991SDimitry Andric }))) { 2120b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Erasing: " << *Cmp << '\n'); 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(*Cmp); 2150b57cec5SDimitry Andric Cmp->eraseFromParent(); 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric // Try to remove v_cndmask_b32. 218e8d8bef9SDimitry Andric if (SelReg.isVirtual() && MRI->use_nodbg_empty(SelReg)) { 2190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Erasing: " << *Sel << '\n'); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(*Sel); 2220b57cec5SDimitry Andric Sel->eraseFromParent(); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric } 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric return CCReg; 2270b57cec5SDimitry Andric } 2280b57cec5SDimitry Andric 229e8d8bef9SDimitry Andric // Optimize sequence 230e8d8bef9SDimitry Andric // %dst = S_OR_SAVEEXEC %src 231e8d8bef9SDimitry Andric // ... instructions not modifying exec ... 232e8d8bef9SDimitry Andric // %tmp = S_AND $exec, %dst 233e8d8bef9SDimitry Andric // $exec = S_XOR_term $exec, %tmp 234e8d8bef9SDimitry Andric // => 235e8d8bef9SDimitry Andric // %dst = S_OR_SAVEEXEC %src 236e8d8bef9SDimitry Andric // ... instructions not modifying exec ... 237e8d8bef9SDimitry Andric // $exec = S_XOR_term $exec, %dst 238e8d8bef9SDimitry Andric // 239e8d8bef9SDimitry Andric // Clean up potentially unnecessary code added for safety during 240e8d8bef9SDimitry Andric // control flow lowering. 241e8d8bef9SDimitry Andric // 242e8d8bef9SDimitry Andric // Return whether any changes were made to MBB. 243e8d8bef9SDimitry Andric bool SIOptimizeExecMaskingPreRA::optimizeElseBranch(MachineBasicBlock &MBB) { 244e8d8bef9SDimitry Andric if (MBB.empty()) 245e8d8bef9SDimitry Andric return false; 246e8d8bef9SDimitry Andric 247e8d8bef9SDimitry Andric // Check this is an else block. 248e8d8bef9SDimitry Andric auto First = MBB.begin(); 249e8d8bef9SDimitry Andric MachineInstr &SaveExecMI = *First; 250e8d8bef9SDimitry Andric if (SaveExecMI.getOpcode() != OrSaveExecOpc) 251e8d8bef9SDimitry Andric return false; 252e8d8bef9SDimitry Andric 253e8d8bef9SDimitry Andric auto I = llvm::find_if(MBB.terminators(), [this](const MachineInstr &MI) { 254e8d8bef9SDimitry Andric return MI.getOpcode() == XorTermrOpc; 255e8d8bef9SDimitry Andric }); 256e8d8bef9SDimitry Andric if (I == MBB.terminators().end()) 257e8d8bef9SDimitry Andric return false; 258e8d8bef9SDimitry Andric 259e8d8bef9SDimitry Andric MachineInstr &XorTermMI = *I; 260e8d8bef9SDimitry Andric if (XorTermMI.getOperand(1).getReg() != Register(ExecReg)) 261e8d8bef9SDimitry Andric return false; 262e8d8bef9SDimitry Andric 263e8d8bef9SDimitry Andric Register SavedExecReg = SaveExecMI.getOperand(0).getReg(); 264e8d8bef9SDimitry Andric Register DstReg = XorTermMI.getOperand(2).getReg(); 265e8d8bef9SDimitry Andric 266e8d8bef9SDimitry Andric // Find potentially unnecessary S_AND 267e8d8bef9SDimitry Andric MachineInstr *AndExecMI = nullptr; 268e8d8bef9SDimitry Andric I--; 269e8d8bef9SDimitry Andric while (I != First && !AndExecMI) { 270e8d8bef9SDimitry Andric if (I->getOpcode() == AndOpc && I->getOperand(0).getReg() == DstReg && 271e8d8bef9SDimitry Andric I->getOperand(1).getReg() == Register(ExecReg)) 272e8d8bef9SDimitry Andric AndExecMI = &*I; 273e8d8bef9SDimitry Andric I--; 274e8d8bef9SDimitry Andric } 275e8d8bef9SDimitry Andric if (!AndExecMI) 276e8d8bef9SDimitry Andric return false; 277e8d8bef9SDimitry Andric 278e8d8bef9SDimitry Andric // Check for exec modifying instructions. 279e8d8bef9SDimitry Andric // Note: exec defs do not create live ranges beyond the 280e8d8bef9SDimitry Andric // instruction so isDefBetween cannot be used. 281e8d8bef9SDimitry Andric // Instead just check that the def segments are adjacent. 282e8d8bef9SDimitry Andric SlotIndex StartIdx = LIS->getInstructionIndex(SaveExecMI); 283e8d8bef9SDimitry Andric SlotIndex EndIdx = LIS->getInstructionIndex(*AndExecMI); 284e8d8bef9SDimitry Andric for (MCRegUnitIterator UI(ExecReg, TRI); UI.isValid(); ++UI) { 285e8d8bef9SDimitry Andric LiveRange &RegUnit = LIS->getRegUnit(*UI); 286e8d8bef9SDimitry Andric if (RegUnit.find(StartIdx) != std::prev(RegUnit.find(EndIdx))) 287e8d8bef9SDimitry Andric return false; 288e8d8bef9SDimitry Andric } 289e8d8bef9SDimitry Andric 290e8d8bef9SDimitry Andric // Remove unnecessary S_AND 291e8d8bef9SDimitry Andric LIS->removeInterval(SavedExecReg); 292e8d8bef9SDimitry Andric LIS->removeInterval(DstReg); 293e8d8bef9SDimitry Andric 294e8d8bef9SDimitry Andric SaveExecMI.getOperand(0).setReg(DstReg); 295e8d8bef9SDimitry Andric 296e8d8bef9SDimitry Andric LIS->RemoveMachineInstrFromMaps(*AndExecMI); 297e8d8bef9SDimitry Andric AndExecMI->eraseFromParent(); 298e8d8bef9SDimitry Andric 299e8d8bef9SDimitry Andric LIS->createAndComputeVirtRegInterval(DstReg); 300e8d8bef9SDimitry Andric 301e8d8bef9SDimitry Andric return true; 302e8d8bef9SDimitry Andric } 303e8d8bef9SDimitry Andric 3040b57cec5SDimitry Andric bool SIOptimizeExecMaskingPreRA::runOnMachineFunction(MachineFunction &MF) { 3050b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 3060b57cec5SDimitry Andric return false; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 3090b57cec5SDimitry Andric TRI = ST.getRegisterInfo(); 3100b57cec5SDimitry Andric TII = ST.getInstrInfo(); 3110b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 312e8d8bef9SDimitry Andric LIS = &getAnalysis<LiveIntervals>(); 3130b57cec5SDimitry Andric 314e8d8bef9SDimitry Andric const bool Wave32 = ST.isWave32(); 315e8d8bef9SDimitry Andric AndOpc = Wave32 ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 316e8d8bef9SDimitry Andric Andn2Opc = Wave32 ? AMDGPU::S_ANDN2_B32 : AMDGPU::S_ANDN2_B64; 317e8d8bef9SDimitry Andric OrSaveExecOpc = 318e8d8bef9SDimitry Andric Wave32 ? AMDGPU::S_OR_SAVEEXEC_B32 : AMDGPU::S_OR_SAVEEXEC_B64; 319e8d8bef9SDimitry Andric XorTermrOpc = Wave32 ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term; 320e8d8bef9SDimitry Andric CondReg = MCRegister::from(Wave32 ? AMDGPU::VCC_LO : AMDGPU::VCC); 321e8d8bef9SDimitry Andric ExecReg = MCRegister::from(Wave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC); 322e8d8bef9SDimitry Andric 323e8d8bef9SDimitry Andric DenseSet<Register> RecalcRegs({AMDGPU::EXEC_LO, AMDGPU::EXEC_HI}); 3240b57cec5SDimitry Andric bool Changed = false; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 3270b57cec5SDimitry Andric 328e8d8bef9SDimitry Andric if (optimizeElseBranch(MBB)) { 329e8d8bef9SDimitry Andric RecalcRegs.insert(AMDGPU::SCC); 330e8d8bef9SDimitry Andric Changed = true; 331e8d8bef9SDimitry Andric } 332e8d8bef9SDimitry Andric 333e8d8bef9SDimitry Andric if (Register Reg = optimizeVcndVcmpPair(MBB)) { 3340b57cec5SDimitry Andric RecalcRegs.insert(Reg); 3350b57cec5SDimitry Andric RecalcRegs.insert(AMDGPU::VCC_LO); 3360b57cec5SDimitry Andric RecalcRegs.insert(AMDGPU::VCC_HI); 3370b57cec5SDimitry Andric RecalcRegs.insert(AMDGPU::SCC); 3380b57cec5SDimitry Andric Changed = true; 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // Try to remove unneeded instructions before s_endpgm. 3420b57cec5SDimitry Andric if (MBB.succ_empty()) { 3430b57cec5SDimitry Andric if (MBB.empty()) 3440b57cec5SDimitry Andric continue; 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Skip this if the endpgm has any implicit uses, otherwise we would need 3470b57cec5SDimitry Andric // to be careful to update / remove them. 3480b57cec5SDimitry Andric // S_ENDPGM always has a single imm operand that is not used other than to 3490b57cec5SDimitry Andric // end up in the encoding 3500b57cec5SDimitry Andric MachineInstr &Term = MBB.back(); 3510b57cec5SDimitry Andric if (Term.getOpcode() != AMDGPU::S_ENDPGM || Term.getNumOperands() != 1) 3520b57cec5SDimitry Andric continue; 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric SmallVector<MachineBasicBlock*, 4> Blocks({&MBB}); 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric while (!Blocks.empty()) { 3570b57cec5SDimitry Andric auto CurBB = Blocks.pop_back_val(); 3580b57cec5SDimitry Andric auto I = CurBB->rbegin(), E = CurBB->rend(); 3590b57cec5SDimitry Andric if (I != E) { 3600b57cec5SDimitry Andric if (I->isUnconditionalBranch() || I->getOpcode() == AMDGPU::S_ENDPGM) 3610b57cec5SDimitry Andric ++I; 3620b57cec5SDimitry Andric else if (I->isBranch()) 3630b57cec5SDimitry Andric continue; 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric while (I != E) { 3670b57cec5SDimitry Andric if (I->isDebugInstr()) { 3680b57cec5SDimitry Andric I = std::next(I); 3690b57cec5SDimitry Andric continue; 3700b57cec5SDimitry Andric } 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric if (I->mayStore() || I->isBarrier() || I->isCall() || 3730b57cec5SDimitry Andric I->hasUnmodeledSideEffects() || I->hasOrderedMemoryRef()) 3740b57cec5SDimitry Andric break; 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() 3770b57cec5SDimitry Andric << "Removing no effect instruction: " << *I << '\n'); 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric for (auto &Op : I->operands()) { 3800b57cec5SDimitry Andric if (Op.isReg()) 3810b57cec5SDimitry Andric RecalcRegs.insert(Op.getReg()); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric auto Next = std::next(I); 3850b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(*I); 3860b57cec5SDimitry Andric I->eraseFromParent(); 3870b57cec5SDimitry Andric I = Next; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric Changed = true; 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric if (I != E) 3930b57cec5SDimitry Andric continue; 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric // Try to ascend predecessors. 3960b57cec5SDimitry Andric for (auto *Pred : CurBB->predecessors()) { 3970b57cec5SDimitry Andric if (Pred->succ_size() == 1) 3980b57cec5SDimitry Andric Blocks.push_back(Pred); 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric continue; 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4045ffd83dbSDimitry Andric // If the only user of a logical operation is move to exec, fold it now 4055ffd83dbSDimitry Andric // to prevent forming of saveexec. I.e: 4065ffd83dbSDimitry Andric // 4075ffd83dbSDimitry Andric // %0:sreg_64 = COPY $exec 4085ffd83dbSDimitry Andric // %1:sreg_64 = S_AND_B64 %0:sreg_64, %2:sreg_64 4095ffd83dbSDimitry Andric // => 4105ffd83dbSDimitry Andric // %1 = S_AND_B64 $exec, %2:sreg_64 4115ffd83dbSDimitry Andric unsigned ScanThreshold = 10; 4125ffd83dbSDimitry Andric for (auto I = MBB.rbegin(), E = MBB.rend(); I != E 4135ffd83dbSDimitry Andric && ScanThreshold--; ++I) { 414e8d8bef9SDimitry Andric // Continue scanning if this is not a full exec copy 415e8d8bef9SDimitry Andric if (!(I->isFullCopy() && I->getOperand(1).getReg() == Register(ExecReg))) 4160b57cec5SDimitry Andric continue; 4170b57cec5SDimitry Andric 4185ffd83dbSDimitry Andric Register SavedExec = I->getOperand(0).getReg(); 419*fe6060f1SDimitry Andric if (SavedExec.isVirtual() && MRI->hasOneNonDBGUse(SavedExec)) { 420*fe6060f1SDimitry Andric MachineInstr *SingleExecUser = &*MRI->use_instr_nodbg_begin(SavedExec); 421*fe6060f1SDimitry Andric int Idx = SingleExecUser->findRegisterUseOperandIdx(SavedExec); 422*fe6060f1SDimitry Andric assert(Idx != -1); 423*fe6060f1SDimitry Andric if (SingleExecUser->getParent() == I->getParent() && 424*fe6060f1SDimitry Andric !SingleExecUser->getOperand(Idx).isImplicit() && 425*fe6060f1SDimitry Andric TII->isOperandLegal(*SingleExecUser, Idx, &I->getOperand(1))) { 4265ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Redundant EXEC COPY: " << *I << '\n'); 4275ffd83dbSDimitry Andric LIS->RemoveMachineInstrFromMaps(*I); 4285ffd83dbSDimitry Andric I->eraseFromParent(); 429e8d8bef9SDimitry Andric MRI->replaceRegWith(SavedExec, ExecReg); 4300b57cec5SDimitry Andric LIS->removeInterval(SavedExec); 4315ffd83dbSDimitry Andric Changed = true; 4325ffd83dbSDimitry Andric } 433*fe6060f1SDimitry Andric } 4345ffd83dbSDimitry Andric break; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric if (Changed) { 4390b57cec5SDimitry Andric for (auto Reg : RecalcRegs) { 440e8d8bef9SDimitry Andric if (Reg.isVirtual()) { 4410b57cec5SDimitry Andric LIS->removeInterval(Reg); 442e8d8bef9SDimitry Andric if (!MRI->reg_empty(Reg)) 4430b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(Reg); 4440b57cec5SDimitry Andric } else { 4450b57cec5SDimitry Andric LIS->removeAllRegUnitsForPhysReg(Reg); 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric return Changed; 4510b57cec5SDimitry Andric } 452