10b57cec5SDimitry Andric //===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===// 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 100b57cec5SDimitry Andric /// This pass lowers the pseudo control flow instructions to real 110b57cec5SDimitry Andric /// machine instructions. 120b57cec5SDimitry Andric /// 130b57cec5SDimitry Andric /// All control flow is handled using predicated instructions and 140b57cec5SDimitry Andric /// a predicate stack. Each Scalar ALU controls the operations of 64 Vector 150b57cec5SDimitry Andric /// ALUs. The Scalar ALU can update the predicate for any of the Vector ALUs 160b57cec5SDimitry Andric /// by writting to the 64-bit EXEC register (each bit corresponds to a 170b57cec5SDimitry Andric /// single vector ALU). Typically, for predicates, a vector ALU will write 180b57cec5SDimitry Andric /// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each 190b57cec5SDimitry Andric /// Vector ALU) and then the ScalarALU will AND the VCC register with the 200b57cec5SDimitry Andric /// EXEC to update the predicates. 210b57cec5SDimitry Andric /// 220b57cec5SDimitry Andric /// For example: 230b57cec5SDimitry Andric /// %vcc = V_CMP_GT_F32 %vgpr1, %vgpr2 240b57cec5SDimitry Andric /// %sgpr0 = SI_IF %vcc 250b57cec5SDimitry Andric /// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 260b57cec5SDimitry Andric /// %sgpr0 = SI_ELSE %sgpr0 270b57cec5SDimitry Andric /// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr0 280b57cec5SDimitry Andric /// SI_END_CF %sgpr0 290b57cec5SDimitry Andric /// 300b57cec5SDimitry Andric /// becomes: 310b57cec5SDimitry Andric /// 320b57cec5SDimitry Andric /// %sgpr0 = S_AND_SAVEEXEC_B64 %vcc // Save and update the exec mask 330b57cec5SDimitry Andric /// %sgpr0 = S_XOR_B64 %sgpr0, %exec // Clear live bits from saved exec mask 340b57cec5SDimitry Andric /// S_CBRANCH_EXECZ label0 // This instruction is an optional 350b57cec5SDimitry Andric /// // optimization which allows us to 360b57cec5SDimitry Andric /// // branch if all the bits of 370b57cec5SDimitry Andric /// // EXEC are zero. 380b57cec5SDimitry Andric /// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 // Do the IF block of the branch 390b57cec5SDimitry Andric /// 400b57cec5SDimitry Andric /// label0: 415ffd83dbSDimitry Andric /// %sgpr0 = S_OR_SAVEEXEC_B64 %sgpr0 // Restore the exec mask for the Then block 425ffd83dbSDimitry Andric /// %exec = S_XOR_B64 %sgpr0, %exec // Update the exec mask 430b57cec5SDimitry Andric /// S_BRANCH_EXECZ label1 // Use our branch optimization 440b57cec5SDimitry Andric /// // instruction again. 450b57cec5SDimitry Andric /// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr // Do the THEN block 460b57cec5SDimitry Andric /// label1: 470b57cec5SDimitry Andric /// %exec = S_OR_B64 %exec, %sgpr0 // Re-enable saved exec mask bits 480b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric #include "AMDGPU.h" 51*e8d8bef9SDimitry Andric #include "GCNSubtarget.h" 520b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 535ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h" 540b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 550b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric using namespace llvm; 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric #define DEBUG_TYPE "si-lower-control-flow" 600b57cec5SDimitry Andric 615ffd83dbSDimitry Andric static cl::opt<bool> 625ffd83dbSDimitry Andric RemoveRedundantEndcf("amdgpu-remove-redundant-endcf", 635ffd83dbSDimitry Andric cl::init(true), cl::ReallyHidden); 645ffd83dbSDimitry Andric 650b57cec5SDimitry Andric namespace { 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric class SILowerControlFlow : public MachineFunctionPass { 680b57cec5SDimitry Andric private: 690b57cec5SDimitry Andric const SIRegisterInfo *TRI = nullptr; 700b57cec5SDimitry Andric const SIInstrInfo *TII = nullptr; 710b57cec5SDimitry Andric LiveIntervals *LIS = nullptr; 720b57cec5SDimitry Andric MachineRegisterInfo *MRI = nullptr; 735ffd83dbSDimitry Andric SetVector<MachineInstr*> LoweredEndCf; 745ffd83dbSDimitry Andric DenseSet<Register> LoweredIf; 755ffd83dbSDimitry Andric SmallSet<MachineInstr *, 16> NeedsKillCleanup; 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric const TargetRegisterClass *BoolRC = nullptr; 785ffd83dbSDimitry Andric bool InsertKillCleanups; 790b57cec5SDimitry Andric unsigned AndOpc; 800b57cec5SDimitry Andric unsigned OrOpc; 810b57cec5SDimitry Andric unsigned XorOpc; 820b57cec5SDimitry Andric unsigned MovTermOpc; 830b57cec5SDimitry Andric unsigned Andn2TermOpc; 840b57cec5SDimitry Andric unsigned XorTermrOpc; 85*e8d8bef9SDimitry Andric unsigned OrTermrOpc; 860b57cec5SDimitry Andric unsigned OrSaveExecOpc; 870b57cec5SDimitry Andric unsigned Exec; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric void emitIf(MachineInstr &MI); 900b57cec5SDimitry Andric void emitElse(MachineInstr &MI); 910b57cec5SDimitry Andric void emitIfBreak(MachineInstr &MI); 920b57cec5SDimitry Andric void emitLoop(MachineInstr &MI); 93*e8d8bef9SDimitry Andric 94*e8d8bef9SDimitry Andric MachineBasicBlock *emitEndCf(MachineInstr &MI); 95*e8d8bef9SDimitry Andric 96*e8d8bef9SDimitry Andric void lowerInitExec(MachineBasicBlock *MBB, MachineInstr &MI); 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric void findMaskOperands(MachineInstr &MI, unsigned OpNo, 990b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Src) const; 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric void combineMasks(MachineInstr &MI); 1020b57cec5SDimitry Andric 103*e8d8bef9SDimitry Andric bool removeMBBifRedundant(MachineBasicBlock &MBB); 104*e8d8bef9SDimitry Andric 105*e8d8bef9SDimitry Andric MachineBasicBlock *process(MachineInstr &MI); 1065ffd83dbSDimitry Andric 1075ffd83dbSDimitry Andric // Skip to the next instruction, ignoring debug instructions, and trivial 1085ffd83dbSDimitry Andric // block boundaries (blocks that have one (typically fallthrough) successor, 1095ffd83dbSDimitry Andric // and the successor has one predecessor. 1105ffd83dbSDimitry Andric MachineBasicBlock::iterator 1115ffd83dbSDimitry Andric skipIgnoreExecInstsTrivialSucc(MachineBasicBlock &MBB, 1125ffd83dbSDimitry Andric MachineBasicBlock::iterator It) const; 1135ffd83dbSDimitry Andric 114*e8d8bef9SDimitry Andric /// Find the insertion point for a new conditional branch. 115*e8d8bef9SDimitry Andric MachineBasicBlock::iterator 116*e8d8bef9SDimitry Andric skipToUncondBrOrEnd(MachineBasicBlock &MBB, 117*e8d8bef9SDimitry Andric MachineBasicBlock::iterator I) const { 118*e8d8bef9SDimitry Andric assert(I->isTerminator()); 119*e8d8bef9SDimitry Andric 120*e8d8bef9SDimitry Andric // FIXME: What if we had multiple pre-existing conditional branches? 121*e8d8bef9SDimitry Andric MachineBasicBlock::iterator End = MBB.end(); 122*e8d8bef9SDimitry Andric while (I != End && !I->isUnconditionalBranch()) 123*e8d8bef9SDimitry Andric ++I; 124*e8d8bef9SDimitry Andric return I; 125*e8d8bef9SDimitry Andric } 126*e8d8bef9SDimitry Andric 1275ffd83dbSDimitry Andric // Remove redundant SI_END_CF instructions. 1285ffd83dbSDimitry Andric void optimizeEndCf(); 1295ffd83dbSDimitry Andric 1300b57cec5SDimitry Andric public: 1310b57cec5SDimitry Andric static char ID; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric SILowerControlFlow() : MachineFunctionPass(ID) {} 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric StringRef getPassName() const override { 1380b57cec5SDimitry Andric return "SI Lower control flow pseudo instructions"; 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1420b57cec5SDimitry Andric // Should preserve the same set that TwoAddressInstructions does. 1430b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>(); 1440b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>(); 1450b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID); 1460b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 1470b57cec5SDimitry Andric } 1480b57cec5SDimitry Andric }; 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric } // end anonymous namespace 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric char SILowerControlFlow::ID = 0; 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE, 1550b57cec5SDimitry Andric "SI lower control flow", false, false) 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) { 1580b57cec5SDimitry Andric MachineOperand &ImpDefSCC = MI.getOperand(3); 1590b57cec5SDimitry Andric assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef()); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric ImpDefSCC.setIsDead(IsDead); 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric char &llvm::SILowerControlFlowID = SILowerControlFlow::ID; 1650b57cec5SDimitry Andric 1665ffd83dbSDimitry Andric static bool hasKill(const MachineBasicBlock *Begin, 1675ffd83dbSDimitry Andric const MachineBasicBlock *End, const SIInstrInfo *TII) { 1685ffd83dbSDimitry Andric DenseSet<const MachineBasicBlock*> Visited; 169*e8d8bef9SDimitry Andric SmallVector<MachineBasicBlock *, 4> Worklist(Begin->successors()); 1705ffd83dbSDimitry Andric 1715ffd83dbSDimitry Andric while (!Worklist.empty()) { 1725ffd83dbSDimitry Andric MachineBasicBlock *MBB = Worklist.pop_back_val(); 1735ffd83dbSDimitry Andric 1745ffd83dbSDimitry Andric if (MBB == End || !Visited.insert(MBB).second) 1755ffd83dbSDimitry Andric continue; 1765ffd83dbSDimitry Andric for (auto &Term : MBB->terminators()) 1775ffd83dbSDimitry Andric if (TII->isKillTerminator(Term.getOpcode())) 1785ffd83dbSDimitry Andric return true; 1795ffd83dbSDimitry Andric 1805ffd83dbSDimitry Andric Worklist.append(MBB->succ_begin(), MBB->succ_end()); 1815ffd83dbSDimitry Andric } 1825ffd83dbSDimitry Andric 1835ffd83dbSDimitry Andric return false; 1845ffd83dbSDimitry Andric } 1855ffd83dbSDimitry Andric 1865ffd83dbSDimitry Andric static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI) { 1878bcb0991SDimitry Andric Register SaveExecReg = MI.getOperand(0).getReg(); 1880b57cec5SDimitry Andric auto U = MRI->use_instr_nodbg_begin(SaveExecReg); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric if (U == MRI->use_instr_nodbg_end() || 1910b57cec5SDimitry Andric std::next(U) != MRI->use_instr_nodbg_end() || 1920b57cec5SDimitry Andric U->getOpcode() != AMDGPU::SI_END_CF) 1930b57cec5SDimitry Andric return false; 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric return true; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric void SILowerControlFlow::emitIf(MachineInstr &MI) { 1990b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 2000b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc(); 2010b57cec5SDimitry Andric MachineBasicBlock::iterator I(&MI); 2025ffd83dbSDimitry Andric Register SaveExecReg = MI.getOperand(0).getReg(); 2030b57cec5SDimitry Andric MachineOperand& Cond = MI.getOperand(1); 2048bcb0991SDimitry Andric assert(Cond.getSubReg() == AMDGPU::NoSubRegister); 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric MachineOperand &ImpDefSCC = MI.getOperand(4); 2070b57cec5SDimitry Andric assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef()); 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // If there is only one use of save exec register and that use is SI_END_CF, 2100b57cec5SDimitry Andric // we can optimize SI_IF by returning the full saved exec mask instead of 2110b57cec5SDimitry Andric // just cleared bits. 2125ffd83dbSDimitry Andric bool SimpleIf = isSimpleIf(MI, MRI); 2135ffd83dbSDimitry Andric 2145ffd83dbSDimitry Andric if (InsertKillCleanups) { 2155ffd83dbSDimitry Andric // Check for SI_KILL_*_TERMINATOR on full path of control flow and 2165ffd83dbSDimitry Andric // flag the associated SI_END_CF for insertion of a kill cleanup. 2175ffd83dbSDimitry Andric auto UseMI = MRI->use_instr_nodbg_begin(SaveExecReg); 2185ffd83dbSDimitry Andric while (UseMI->getOpcode() != AMDGPU::SI_END_CF) { 2195ffd83dbSDimitry Andric assert(std::next(UseMI) == MRI->use_instr_nodbg_end()); 2205ffd83dbSDimitry Andric assert(UseMI->getOpcode() == AMDGPU::SI_ELSE); 2215ffd83dbSDimitry Andric MachineOperand &NextExec = UseMI->getOperand(0); 2225ffd83dbSDimitry Andric Register NextExecReg = NextExec.getReg(); 2235ffd83dbSDimitry Andric if (NextExec.isDead()) { 2245ffd83dbSDimitry Andric assert(!SimpleIf); 2255ffd83dbSDimitry Andric break; 2265ffd83dbSDimitry Andric } 2275ffd83dbSDimitry Andric UseMI = MRI->use_instr_nodbg_begin(NextExecReg); 2285ffd83dbSDimitry Andric } 2295ffd83dbSDimitry Andric if (UseMI->getOpcode() == AMDGPU::SI_END_CF) { 2305ffd83dbSDimitry Andric if (hasKill(MI.getParent(), UseMI->getParent(), TII)) { 2315ffd83dbSDimitry Andric NeedsKillCleanup.insert(&*UseMI); 2325ffd83dbSDimitry Andric SimpleIf = false; 2335ffd83dbSDimitry Andric } 2345ffd83dbSDimitry Andric } 2355ffd83dbSDimitry Andric } else if (SimpleIf) { 2365ffd83dbSDimitry Andric // Check for SI_KILL_*_TERMINATOR on path from if to endif. 2375ffd83dbSDimitry Andric // if there is any such terminator simplifications are not safe. 2385ffd83dbSDimitry Andric auto UseMI = MRI->use_instr_nodbg_begin(SaveExecReg); 2395ffd83dbSDimitry Andric SimpleIf = !hasKill(MI.getParent(), UseMI->getParent(), TII); 2405ffd83dbSDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric // Add an implicit def of exec to discourage scheduling VALU after this which 2430b57cec5SDimitry Andric // will interfere with trying to form s_and_saveexec_b64 later. 2440b57cec5SDimitry Andric Register CopyReg = SimpleIf ? SaveExecReg 2450b57cec5SDimitry Andric : MRI->createVirtualRegister(BoolRC); 2460b57cec5SDimitry Andric MachineInstr *CopyExec = 2470b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg) 2480b57cec5SDimitry Andric .addReg(Exec) 2490b57cec5SDimitry Andric .addReg(Exec, RegState::ImplicitDefine); 2505ffd83dbSDimitry Andric LoweredIf.insert(CopyReg); 2510b57cec5SDimitry Andric 2528bcb0991SDimitry Andric Register Tmp = MRI->createVirtualRegister(BoolRC); 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric MachineInstr *And = 2550b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(AndOpc), Tmp) 2560b57cec5SDimitry Andric .addReg(CopyReg) 2570b57cec5SDimitry Andric .add(Cond); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric setImpSCCDefDead(*And, true); 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric MachineInstr *Xor = nullptr; 2620b57cec5SDimitry Andric if (!SimpleIf) { 2630b57cec5SDimitry Andric Xor = 2640b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(XorOpc), SaveExecReg) 2650b57cec5SDimitry Andric .addReg(Tmp) 2660b57cec5SDimitry Andric .addReg(CopyReg); 2670b57cec5SDimitry Andric setImpSCCDefDead(*Xor, ImpDefSCC.isDead()); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // Use a copy that is a terminator to get correct spill code placement it with 2710b57cec5SDimitry Andric // fast regalloc. 2720b57cec5SDimitry Andric MachineInstr *SetExec = 2730b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(MovTermOpc), Exec) 2740b57cec5SDimitry Andric .addReg(Tmp, RegState::Kill); 2750b57cec5SDimitry Andric 276*e8d8bef9SDimitry Andric // Skip ahead to the unconditional branch in case there are other terminators 277*e8d8bef9SDimitry Andric // present. 278*e8d8bef9SDimitry Andric I = skipToUncondBrOrEnd(MBB, I); 279*e8d8bef9SDimitry Andric 2805ffd83dbSDimitry Andric // Insert the S_CBRANCH_EXECZ instruction which will be optimized later 2815ffd83dbSDimitry Andric // during SIRemoveShortExecBranches. 2825ffd83dbSDimitry Andric MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ)) 2830b57cec5SDimitry Andric .add(MI.getOperand(2)); 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric if (!LIS) { 2860b57cec5SDimitry Andric MI.eraseFromParent(); 2870b57cec5SDimitry Andric return; 2880b57cec5SDimitry Andric } 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*CopyExec); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // Replace with and so we don't need to fix the live interval for condition 2930b57cec5SDimitry Andric // register. 2940b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *And); 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric if (!SimpleIf) 2970b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Xor); 2980b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*SetExec); 2990b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*NewBr); 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC); 3020b57cec5SDimitry Andric MI.eraseFromParent(); 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric // FIXME: Is there a better way of adjusting the liveness? It shouldn't be 3050b57cec5SDimitry Andric // hard to add another def here but I'm not sure how to correctly update the 3060b57cec5SDimitry Andric // valno. 3070b57cec5SDimitry Andric LIS->removeInterval(SaveExecReg); 3080b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(SaveExecReg); 3090b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(Tmp); 3100b57cec5SDimitry Andric if (!SimpleIf) 3110b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(CopyReg); 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric void SILowerControlFlow::emitElse(MachineInstr &MI) { 3150b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 3160b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc(); 3170b57cec5SDimitry Andric 3185ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric MachineBasicBlock::iterator Start = MBB.begin(); 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric // This must be inserted before phis and any spill code inserted before the 3230b57cec5SDimitry Andric // else. 324*e8d8bef9SDimitry Andric Register SaveReg = MRI->createVirtualRegister(BoolRC); 3250b57cec5SDimitry Andric MachineInstr *OrSaveExec = 3260b57cec5SDimitry Andric BuildMI(MBB, Start, DL, TII->get(OrSaveExecOpc), SaveReg) 327*e8d8bef9SDimitry Andric .add(MI.getOperand(1)); // Saved EXEC 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric MachineBasicBlock *DestBB = MI.getOperand(2).getMBB(); 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric MachineBasicBlock::iterator ElsePt(MI); 3320b57cec5SDimitry Andric 333*e8d8bef9SDimitry Andric // This accounts for any modification of the EXEC mask within the block and 334*e8d8bef9SDimitry Andric // can be optimized out pre-RA when not required. 335*e8d8bef9SDimitry Andric MachineInstr *And = BuildMI(MBB, ElsePt, DL, TII->get(AndOpc), DstReg) 3360b57cec5SDimitry Andric .addReg(Exec) 3370b57cec5SDimitry Andric .addReg(SaveReg); 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric if (LIS) 3400b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*And); 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric MachineInstr *Xor = 3430b57cec5SDimitry Andric BuildMI(MBB, ElsePt, DL, TII->get(XorTermrOpc), Exec) 3440b57cec5SDimitry Andric .addReg(Exec) 3450b57cec5SDimitry Andric .addReg(DstReg); 3460b57cec5SDimitry Andric 347*e8d8bef9SDimitry Andric // Skip ahead to the unconditional branch in case there are other terminators 348*e8d8bef9SDimitry Andric // present. 349*e8d8bef9SDimitry Andric ElsePt = skipToUncondBrOrEnd(MBB, ElsePt); 350*e8d8bef9SDimitry Andric 3510b57cec5SDimitry Andric MachineInstr *Branch = 3525ffd83dbSDimitry Andric BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ)) 3530b57cec5SDimitry Andric .addMBB(DestBB); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric if (!LIS) { 3560b57cec5SDimitry Andric MI.eraseFromParent(); 3570b57cec5SDimitry Andric return; 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(MI); 3610b57cec5SDimitry Andric MI.eraseFromParent(); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*OrSaveExec); 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Xor); 3660b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Branch); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric LIS->removeInterval(DstReg); 3690b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(DstReg); 3700b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(SaveReg); 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric // Let this be recomputed. 3730b57cec5SDimitry Andric LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC); 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric void SILowerControlFlow::emitIfBreak(MachineInstr &MI) { 3770b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 3780b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc(); 3795ffd83dbSDimitry Andric auto Dst = MI.getOperand(0).getReg(); 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric // Skip ANDing with exec if the break condition is already masked by exec 3820b57cec5SDimitry Andric // because it is a V_CMP in the same basic block. (We know the break 3830b57cec5SDimitry Andric // condition operand was an i1 in IR, so if it is a VALU instruction it must 3840b57cec5SDimitry Andric // be one with a carry-out.) 3850b57cec5SDimitry Andric bool SkipAnding = false; 3860b57cec5SDimitry Andric if (MI.getOperand(1).isReg()) { 3870b57cec5SDimitry Andric if (MachineInstr *Def = MRI->getUniqueVRegDef(MI.getOperand(1).getReg())) { 3880b57cec5SDimitry Andric SkipAnding = Def->getParent() == MI.getParent() 3890b57cec5SDimitry Andric && SIInstrInfo::isVALU(*Def); 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric // AND the break condition operand with exec, then OR that into the "loop 3940b57cec5SDimitry Andric // exit" mask. 3950b57cec5SDimitry Andric MachineInstr *And = nullptr, *Or = nullptr; 3960b57cec5SDimitry Andric if (!SkipAnding) { 397480093f4SDimitry Andric Register AndReg = MRI->createVirtualRegister(BoolRC); 398480093f4SDimitry Andric And = BuildMI(MBB, &MI, DL, TII->get(AndOpc), AndReg) 3990b57cec5SDimitry Andric .addReg(Exec) 4000b57cec5SDimitry Andric .add(MI.getOperand(1)); 4010b57cec5SDimitry Andric Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst) 402480093f4SDimitry Andric .addReg(AndReg) 4030b57cec5SDimitry Andric .add(MI.getOperand(2)); 404480093f4SDimitry Andric if (LIS) 405480093f4SDimitry Andric LIS->createAndComputeVirtRegInterval(AndReg); 4060b57cec5SDimitry Andric } else 4070b57cec5SDimitry Andric Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst) 4080b57cec5SDimitry Andric .add(MI.getOperand(1)) 4090b57cec5SDimitry Andric .add(MI.getOperand(2)); 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric if (LIS) { 4120b57cec5SDimitry Andric if (And) 4130b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*And); 4140b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *Or); 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric MI.eraseFromParent(); 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric void SILowerControlFlow::emitLoop(MachineInstr &MI) { 4210b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 4220b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc(); 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric MachineInstr *AndN2 = 4250b57cec5SDimitry Andric BuildMI(MBB, &MI, DL, TII->get(Andn2TermOpc), Exec) 4260b57cec5SDimitry Andric .addReg(Exec) 4270b57cec5SDimitry Andric .add(MI.getOperand(0)); 4280b57cec5SDimitry Andric 429*e8d8bef9SDimitry Andric auto BranchPt = skipToUncondBrOrEnd(MBB, MI.getIterator()); 4300b57cec5SDimitry Andric MachineInstr *Branch = 431*e8d8bef9SDimitry Andric BuildMI(MBB, BranchPt, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 4320b57cec5SDimitry Andric .add(MI.getOperand(1)); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric if (LIS) { 4350b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *AndN2); 4360b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Branch); 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric MI.eraseFromParent(); 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric 4425ffd83dbSDimitry Andric MachineBasicBlock::iterator 4435ffd83dbSDimitry Andric SILowerControlFlow::skipIgnoreExecInstsTrivialSucc( 4445ffd83dbSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const { 4455ffd83dbSDimitry Andric 4465ffd83dbSDimitry Andric SmallSet<const MachineBasicBlock *, 4> Visited; 4475ffd83dbSDimitry Andric MachineBasicBlock *B = &MBB; 4485ffd83dbSDimitry Andric do { 4495ffd83dbSDimitry Andric if (!Visited.insert(B).second) 4505ffd83dbSDimitry Andric return MBB.end(); 4515ffd83dbSDimitry Andric 4525ffd83dbSDimitry Andric auto E = B->end(); 4535ffd83dbSDimitry Andric for ( ; It != E; ++It) { 4545ffd83dbSDimitry Andric if (It->getOpcode() == AMDGPU::SI_KILL_CLEANUP) 4555ffd83dbSDimitry Andric continue; 4565ffd83dbSDimitry Andric if (TII->mayReadEXEC(*MRI, *It)) 4575ffd83dbSDimitry Andric break; 4585ffd83dbSDimitry Andric } 4595ffd83dbSDimitry Andric 4605ffd83dbSDimitry Andric if (It != E) 4615ffd83dbSDimitry Andric return It; 4625ffd83dbSDimitry Andric 4635ffd83dbSDimitry Andric if (B->succ_size() != 1) 4645ffd83dbSDimitry Andric return MBB.end(); 4655ffd83dbSDimitry Andric 4665ffd83dbSDimitry Andric // If there is one trivial successor, advance to the next block. 4675ffd83dbSDimitry Andric MachineBasicBlock *Succ = *B->succ_begin(); 4685ffd83dbSDimitry Andric 4695ffd83dbSDimitry Andric It = Succ->begin(); 4705ffd83dbSDimitry Andric B = Succ; 4715ffd83dbSDimitry Andric } while (true); 4725ffd83dbSDimitry Andric } 4735ffd83dbSDimitry Andric 474*e8d8bef9SDimitry Andric MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) { 4750b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 4760b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc(); 4770b57cec5SDimitry Andric 478*e8d8bef9SDimitry Andric MachineBasicBlock::iterator InsPt = MBB.begin(); 479*e8d8bef9SDimitry Andric 480*e8d8bef9SDimitry Andric // If we have instructions that aren't prolog instructions, split the block 481*e8d8bef9SDimitry Andric // and emit a terminator instruction. This ensures correct spill placement. 482*e8d8bef9SDimitry Andric // FIXME: We should unconditionally split the block here. 483*e8d8bef9SDimitry Andric bool NeedBlockSplit = false; 484*e8d8bef9SDimitry Andric Register DataReg = MI.getOperand(0).getReg(); 485*e8d8bef9SDimitry Andric for (MachineBasicBlock::iterator I = InsPt, E = MI.getIterator(); 486*e8d8bef9SDimitry Andric I != E; ++I) { 487*e8d8bef9SDimitry Andric if (I->modifiesRegister(DataReg, TRI)) { 488*e8d8bef9SDimitry Andric NeedBlockSplit = true; 489*e8d8bef9SDimitry Andric break; 490*e8d8bef9SDimitry Andric } 491*e8d8bef9SDimitry Andric } 492*e8d8bef9SDimitry Andric 493*e8d8bef9SDimitry Andric unsigned Opcode = OrOpc; 494*e8d8bef9SDimitry Andric MachineBasicBlock *SplitBB = &MBB; 495*e8d8bef9SDimitry Andric if (NeedBlockSplit) { 496*e8d8bef9SDimitry Andric SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS); 497*e8d8bef9SDimitry Andric Opcode = OrTermrOpc; 498*e8d8bef9SDimitry Andric InsPt = MI; 499*e8d8bef9SDimitry Andric } 500*e8d8bef9SDimitry Andric 501*e8d8bef9SDimitry Andric MachineInstr *NewMI = 502*e8d8bef9SDimitry Andric BuildMI(MBB, InsPt, DL, TII->get(Opcode), Exec) 5030b57cec5SDimitry Andric .addReg(Exec) 5040b57cec5SDimitry Andric .add(MI.getOperand(0)); 5050b57cec5SDimitry Andric 5065ffd83dbSDimitry Andric LoweredEndCf.insert(NewMI); 5075ffd83dbSDimitry Andric 5085ffd83dbSDimitry Andric // If this ends control flow which contains kills (as flagged in emitIf) 5095ffd83dbSDimitry Andric // then insert an SI_KILL_CLEANUP immediately following the exec mask 5105ffd83dbSDimitry Andric // manipulation. This can be lowered to early termination if appropriate. 5115ffd83dbSDimitry Andric MachineInstr *CleanUpMI = nullptr; 5125ffd83dbSDimitry Andric if (NeedsKillCleanup.count(&MI)) 5135ffd83dbSDimitry Andric CleanUpMI = BuildMI(MBB, InsPt, DL, TII->get(AMDGPU::SI_KILL_CLEANUP)); 5145ffd83dbSDimitry Andric 5155ffd83dbSDimitry Andric if (LIS) { 5160b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *NewMI); 5175ffd83dbSDimitry Andric if (CleanUpMI) 5185ffd83dbSDimitry Andric LIS->InsertMachineInstrInMaps(*CleanUpMI); 5195ffd83dbSDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric MI.eraseFromParent(); 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric if (LIS) 5240b57cec5SDimitry Andric LIS->handleMove(*NewMI); 525*e8d8bef9SDimitry Andric return SplitBB; 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric // Returns replace operands for a logical operation, either single result 5290b57cec5SDimitry Andric // for exec or two operands if source was another equivalent operation. 5300b57cec5SDimitry Andric void SILowerControlFlow::findMaskOperands(MachineInstr &MI, unsigned OpNo, 5310b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Src) const { 5320b57cec5SDimitry Andric MachineOperand &Op = MI.getOperand(OpNo); 533*e8d8bef9SDimitry Andric if (!Op.isReg() || !Op.getReg().isVirtual()) { 5340b57cec5SDimitry Andric Src.push_back(Op); 5350b57cec5SDimitry Andric return; 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg()); 5390b57cec5SDimitry Andric if (!Def || Def->getParent() != MI.getParent() || 5400b57cec5SDimitry Andric !(Def->isFullCopy() || (Def->getOpcode() == MI.getOpcode()))) 5410b57cec5SDimitry Andric return; 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric // Make sure we do not modify exec between def and use. 5440b57cec5SDimitry Andric // A copy with implcitly defined exec inserted earlier is an exclusion, it 5450b57cec5SDimitry Andric // does not really modify exec. 5460b57cec5SDimitry Andric for (auto I = Def->getIterator(); I != MI.getIterator(); ++I) 5470b57cec5SDimitry Andric if (I->modifiesRegister(AMDGPU::EXEC, TRI) && 5480b57cec5SDimitry Andric !(I->isCopy() && I->getOperand(0).getReg() != Exec)) 5490b57cec5SDimitry Andric return; 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric for (const auto &SrcOp : Def->explicit_operands()) 5520b57cec5SDimitry Andric if (SrcOp.isReg() && SrcOp.isUse() && 553*e8d8bef9SDimitry Andric (SrcOp.getReg().isVirtual() || SrcOp.getReg() == Exec)) 5540b57cec5SDimitry Andric Src.push_back(SrcOp); 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric // Search and combine pairs of equivalent instructions, like 5580b57cec5SDimitry Andric // S_AND_B64 x, (S_AND_B64 x, y) => S_AND_B64 x, y 5590b57cec5SDimitry Andric // S_OR_B64 x, (S_OR_B64 x, y) => S_OR_B64 x, y 5600b57cec5SDimitry Andric // One of the operands is exec mask. 5610b57cec5SDimitry Andric void SILowerControlFlow::combineMasks(MachineInstr &MI) { 5620b57cec5SDimitry Andric assert(MI.getNumExplicitOperands() == 3); 5630b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Ops; 5640b57cec5SDimitry Andric unsigned OpToReplace = 1; 5650b57cec5SDimitry Andric findMaskOperands(MI, 1, Ops); 5660b57cec5SDimitry Andric if (Ops.size() == 1) OpToReplace = 2; // First operand can be exec or its copy 5670b57cec5SDimitry Andric findMaskOperands(MI, 2, Ops); 5680b57cec5SDimitry Andric if (Ops.size() != 3) return; 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric unsigned UniqueOpndIdx; 5710b57cec5SDimitry Andric if (Ops[0].isIdenticalTo(Ops[1])) UniqueOpndIdx = 2; 5720b57cec5SDimitry Andric else if (Ops[0].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1; 5730b57cec5SDimitry Andric else if (Ops[1].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1; 5740b57cec5SDimitry Andric else return; 5750b57cec5SDimitry Andric 5768bcb0991SDimitry Andric Register Reg = MI.getOperand(OpToReplace).getReg(); 5770b57cec5SDimitry Andric MI.RemoveOperand(OpToReplace); 5780b57cec5SDimitry Andric MI.addOperand(Ops[UniqueOpndIdx]); 5790b57cec5SDimitry Andric if (MRI->use_empty(Reg)) 5800b57cec5SDimitry Andric MRI->getUniqueVRegDef(Reg)->eraseFromParent(); 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric 5835ffd83dbSDimitry Andric void SILowerControlFlow::optimizeEndCf() { 5845ffd83dbSDimitry Andric // If the only instruction immediately following this END_CF is an another 5855ffd83dbSDimitry Andric // END_CF in the only successor we can avoid emitting exec mask restore here. 5865ffd83dbSDimitry Andric if (!RemoveRedundantEndcf) 5875ffd83dbSDimitry Andric return; 5880b57cec5SDimitry Andric 5895ffd83dbSDimitry Andric for (MachineInstr *MI : LoweredEndCf) { 5905ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI->getParent(); 5915ffd83dbSDimitry Andric auto Next = 5925ffd83dbSDimitry Andric skipIgnoreExecInstsTrivialSucc(MBB, std::next(MI->getIterator())); 5935ffd83dbSDimitry Andric if (Next == MBB.end() || !LoweredEndCf.count(&*Next)) 5945ffd83dbSDimitry Andric continue; 5955ffd83dbSDimitry Andric // Only skip inner END_CF if outer ENDCF belongs to SI_IF. 5965ffd83dbSDimitry Andric // If that belongs to SI_ELSE then saved mask has an inverted value. 5975ffd83dbSDimitry Andric Register SavedExec 5985ffd83dbSDimitry Andric = TII->getNamedOperand(*Next, AMDGPU::OpName::src1)->getReg(); 5995ffd83dbSDimitry Andric assert(SavedExec.isVirtual() && "Expected saved exec to be src1!"); 6000b57cec5SDimitry Andric 6015ffd83dbSDimitry Andric const MachineInstr *Def = MRI->getUniqueVRegDef(SavedExec); 6025ffd83dbSDimitry Andric if (Def && LoweredIf.count(SavedExec)) { 6035ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Skip redundant "; MI->dump()); 6045ffd83dbSDimitry Andric if (LIS) 6055ffd83dbSDimitry Andric LIS->RemoveMachineInstrFromMaps(*MI); 6065ffd83dbSDimitry Andric MI->eraseFromParent(); 607*e8d8bef9SDimitry Andric removeMBBifRedundant(MBB); 6085ffd83dbSDimitry Andric } 6095ffd83dbSDimitry Andric } 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 612*e8d8bef9SDimitry Andric MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) { 6135ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent(); 6145ffd83dbSDimitry Andric MachineBasicBlock::iterator I(MI); 6155ffd83dbSDimitry Andric MachineInstr *Prev = (I != MBB.begin()) ? &*(std::prev(I)) : nullptr; 6160b57cec5SDimitry Andric 617*e8d8bef9SDimitry Andric MachineBasicBlock *SplitBB = &MBB; 618*e8d8bef9SDimitry Andric 6190b57cec5SDimitry Andric switch (MI.getOpcode()) { 6200b57cec5SDimitry Andric case AMDGPU::SI_IF: 6210b57cec5SDimitry Andric emitIf(MI); 6220b57cec5SDimitry Andric break; 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric case AMDGPU::SI_ELSE: 6250b57cec5SDimitry Andric emitElse(MI); 6260b57cec5SDimitry Andric break; 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric case AMDGPU::SI_IF_BREAK: 6290b57cec5SDimitry Andric emitIfBreak(MI); 6300b57cec5SDimitry Andric break; 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric case AMDGPU::SI_LOOP: 6330b57cec5SDimitry Andric emitLoop(MI); 6340b57cec5SDimitry Andric break; 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric case AMDGPU::SI_END_CF: 637*e8d8bef9SDimitry Andric SplitBB = emitEndCf(MI); 6380b57cec5SDimitry Andric break; 6390b57cec5SDimitry Andric 6405ffd83dbSDimitry Andric default: 6415ffd83dbSDimitry Andric assert(false && "Attempt to process unsupported instruction"); 6425ffd83dbSDimitry Andric break; 6435ffd83dbSDimitry Andric } 6445ffd83dbSDimitry Andric 6455ffd83dbSDimitry Andric MachineBasicBlock::iterator Next; 6465ffd83dbSDimitry Andric for (I = Prev ? Prev->getIterator() : MBB.begin(); I != MBB.end(); I = Next) { 6475ffd83dbSDimitry Andric Next = std::next(I); 6485ffd83dbSDimitry Andric MachineInstr &MaskMI = *I; 6495ffd83dbSDimitry Andric switch (MaskMI.getOpcode()) { 6500b57cec5SDimitry Andric case AMDGPU::S_AND_B64: 6510b57cec5SDimitry Andric case AMDGPU::S_OR_B64: 6520b57cec5SDimitry Andric case AMDGPU::S_AND_B32: 6530b57cec5SDimitry Andric case AMDGPU::S_OR_B32: 6540b57cec5SDimitry Andric // Cleanup bit manipulations on exec mask 6555ffd83dbSDimitry Andric combineMasks(MaskMI); 6565ffd83dbSDimitry Andric break; 6575ffd83dbSDimitry Andric default: 6585ffd83dbSDimitry Andric I = MBB.end(); 6595ffd83dbSDimitry Andric break; 6605ffd83dbSDimitry Andric } 6615ffd83dbSDimitry Andric } 662*e8d8bef9SDimitry Andric 663*e8d8bef9SDimitry Andric return SplitBB; 664*e8d8bef9SDimitry Andric } 665*e8d8bef9SDimitry Andric 666*e8d8bef9SDimitry Andric void SILowerControlFlow::lowerInitExec(MachineBasicBlock *MBB, 667*e8d8bef9SDimitry Andric MachineInstr &MI) { 668*e8d8bef9SDimitry Andric MachineFunction &MF = *MBB->getParent(); 669*e8d8bef9SDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 670*e8d8bef9SDimitry Andric bool IsWave32 = ST.isWave32(); 671*e8d8bef9SDimitry Andric 672*e8d8bef9SDimitry Andric if (MI.getOpcode() == AMDGPU::SI_INIT_EXEC) { 673*e8d8bef9SDimitry Andric // This should be before all vector instructions. 674*e8d8bef9SDimitry Andric BuildMI(*MBB, MBB->begin(), MI.getDebugLoc(), 675*e8d8bef9SDimitry Andric TII->get(IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64), Exec) 676*e8d8bef9SDimitry Andric .addImm(MI.getOperand(0).getImm()); 677*e8d8bef9SDimitry Andric if (LIS) 678*e8d8bef9SDimitry Andric LIS->RemoveMachineInstrFromMaps(MI); 679*e8d8bef9SDimitry Andric MI.eraseFromParent(); 680*e8d8bef9SDimitry Andric return; 681*e8d8bef9SDimitry Andric } 682*e8d8bef9SDimitry Andric 683*e8d8bef9SDimitry Andric // Extract the thread count from an SGPR input and set EXEC accordingly. 684*e8d8bef9SDimitry Andric // Since BFM can't shift by 64, handle that case with CMP + CMOV. 685*e8d8bef9SDimitry Andric // 686*e8d8bef9SDimitry Andric // S_BFE_U32 count, input, {shift, 7} 687*e8d8bef9SDimitry Andric // S_BFM_B64 exec, count, 0 688*e8d8bef9SDimitry Andric // S_CMP_EQ_U32 count, 64 689*e8d8bef9SDimitry Andric // S_CMOV_B64 exec, -1 690*e8d8bef9SDimitry Andric Register InputReg = MI.getOperand(0).getReg(); 691*e8d8bef9SDimitry Andric MachineInstr *FirstMI = &*MBB->begin(); 692*e8d8bef9SDimitry Andric if (InputReg.isVirtual()) { 693*e8d8bef9SDimitry Andric MachineInstr *DefInstr = MRI->getVRegDef(InputReg); 694*e8d8bef9SDimitry Andric assert(DefInstr && DefInstr->isCopy()); 695*e8d8bef9SDimitry Andric if (DefInstr->getParent() == MBB) { 696*e8d8bef9SDimitry Andric if (DefInstr != FirstMI) { 697*e8d8bef9SDimitry Andric // If the `InputReg` is defined in current block, we also need to 698*e8d8bef9SDimitry Andric // move that instruction to the beginning of the block. 699*e8d8bef9SDimitry Andric DefInstr->removeFromParent(); 700*e8d8bef9SDimitry Andric MBB->insert(FirstMI, DefInstr); 701*e8d8bef9SDimitry Andric if (LIS) 702*e8d8bef9SDimitry Andric LIS->handleMove(*DefInstr); 703*e8d8bef9SDimitry Andric } else { 704*e8d8bef9SDimitry Andric // If first instruction is definition then move pointer after it. 705*e8d8bef9SDimitry Andric FirstMI = &*std::next(FirstMI->getIterator()); 706*e8d8bef9SDimitry Andric } 707*e8d8bef9SDimitry Andric } 708*e8d8bef9SDimitry Andric } 709*e8d8bef9SDimitry Andric 710*e8d8bef9SDimitry Andric // Insert instruction sequence at block beginning (before vector operations). 711*e8d8bef9SDimitry Andric const DebugLoc DL = MI.getDebugLoc(); 712*e8d8bef9SDimitry Andric const unsigned WavefrontSize = ST.getWavefrontSize(); 713*e8d8bef9SDimitry Andric const unsigned Mask = (WavefrontSize << 1) - 1; 714*e8d8bef9SDimitry Andric Register CountReg = MRI->createVirtualRegister(&AMDGPU::SGPR_32RegClass); 715*e8d8bef9SDimitry Andric auto BfeMI = BuildMI(*MBB, FirstMI, DL, TII->get(AMDGPU::S_BFE_U32), CountReg) 716*e8d8bef9SDimitry Andric .addReg(InputReg) 717*e8d8bef9SDimitry Andric .addImm((MI.getOperand(1).getImm() & Mask) | 0x70000); 718*e8d8bef9SDimitry Andric auto BfmMI = 719*e8d8bef9SDimitry Andric BuildMI(*MBB, FirstMI, DL, 720*e8d8bef9SDimitry Andric TII->get(IsWave32 ? AMDGPU::S_BFM_B32 : AMDGPU::S_BFM_B64), Exec) 721*e8d8bef9SDimitry Andric .addReg(CountReg) 722*e8d8bef9SDimitry Andric .addImm(0); 723*e8d8bef9SDimitry Andric auto CmpMI = BuildMI(*MBB, FirstMI, DL, TII->get(AMDGPU::S_CMP_EQ_U32)) 724*e8d8bef9SDimitry Andric .addReg(CountReg, RegState::Kill) 725*e8d8bef9SDimitry Andric .addImm(WavefrontSize); 726*e8d8bef9SDimitry Andric auto CmovMI = 727*e8d8bef9SDimitry Andric BuildMI(*MBB, FirstMI, DL, 728*e8d8bef9SDimitry Andric TII->get(IsWave32 ? AMDGPU::S_CMOV_B32 : AMDGPU::S_CMOV_B64), 729*e8d8bef9SDimitry Andric Exec) 730*e8d8bef9SDimitry Andric .addImm(-1); 731*e8d8bef9SDimitry Andric 732*e8d8bef9SDimitry Andric if (!LIS) { 733*e8d8bef9SDimitry Andric MI.eraseFromParent(); 734*e8d8bef9SDimitry Andric return; 735*e8d8bef9SDimitry Andric } 736*e8d8bef9SDimitry Andric 737*e8d8bef9SDimitry Andric LIS->RemoveMachineInstrFromMaps(MI); 738*e8d8bef9SDimitry Andric MI.eraseFromParent(); 739*e8d8bef9SDimitry Andric 740*e8d8bef9SDimitry Andric LIS->InsertMachineInstrInMaps(*BfeMI); 741*e8d8bef9SDimitry Andric LIS->InsertMachineInstrInMaps(*BfmMI); 742*e8d8bef9SDimitry Andric LIS->InsertMachineInstrInMaps(*CmpMI); 743*e8d8bef9SDimitry Andric LIS->InsertMachineInstrInMaps(*CmovMI); 744*e8d8bef9SDimitry Andric 745*e8d8bef9SDimitry Andric LIS->removeInterval(InputReg); 746*e8d8bef9SDimitry Andric LIS->createAndComputeVirtRegInterval(InputReg); 747*e8d8bef9SDimitry Andric LIS->createAndComputeVirtRegInterval(CountReg); 748*e8d8bef9SDimitry Andric } 749*e8d8bef9SDimitry Andric 750*e8d8bef9SDimitry Andric bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) { 751*e8d8bef9SDimitry Andric auto GetFallThroughSucc = [=](MachineBasicBlock *B) -> MachineBasicBlock * { 752*e8d8bef9SDimitry Andric auto *S = B->getNextNode(); 753*e8d8bef9SDimitry Andric if (!S) 754*e8d8bef9SDimitry Andric return nullptr; 755*e8d8bef9SDimitry Andric if (B->isSuccessor(S)) { 756*e8d8bef9SDimitry Andric // The only fallthrough candidate 757*e8d8bef9SDimitry Andric MachineBasicBlock::iterator I(B->getFirstInstrTerminator()); 758*e8d8bef9SDimitry Andric MachineBasicBlock::iterator E = B->end(); 759*e8d8bef9SDimitry Andric for (; I != E; I++) { 760*e8d8bef9SDimitry Andric if (I->isBranch() && TII->getBranchDestBlock(*I) == S) 761*e8d8bef9SDimitry Andric // We have unoptimized branch to layout successor 762*e8d8bef9SDimitry Andric return nullptr; 763*e8d8bef9SDimitry Andric } 764*e8d8bef9SDimitry Andric } 765*e8d8bef9SDimitry Andric return S; 766*e8d8bef9SDimitry Andric }; 767*e8d8bef9SDimitry Andric 768*e8d8bef9SDimitry Andric for (auto &I : MBB.instrs()) { 769*e8d8bef9SDimitry Andric if (!I.isDebugInstr() && !I.isUnconditionalBranch()) 770*e8d8bef9SDimitry Andric return false; 771*e8d8bef9SDimitry Andric } 772*e8d8bef9SDimitry Andric 773*e8d8bef9SDimitry Andric assert(MBB.succ_size() == 1 && "MBB has more than one successor"); 774*e8d8bef9SDimitry Andric 775*e8d8bef9SDimitry Andric MachineBasicBlock *Succ = *MBB.succ_begin(); 776*e8d8bef9SDimitry Andric MachineBasicBlock *FallThrough = nullptr; 777*e8d8bef9SDimitry Andric 778*e8d8bef9SDimitry Andric while (!MBB.predecessors().empty()) { 779*e8d8bef9SDimitry Andric MachineBasicBlock *P = *MBB.pred_begin(); 780*e8d8bef9SDimitry Andric if (GetFallThroughSucc(P) == &MBB) 781*e8d8bef9SDimitry Andric FallThrough = P; 782*e8d8bef9SDimitry Andric P->ReplaceUsesOfBlockWith(&MBB, Succ); 783*e8d8bef9SDimitry Andric } 784*e8d8bef9SDimitry Andric MBB.removeSuccessor(Succ); 785*e8d8bef9SDimitry Andric if (LIS) { 786*e8d8bef9SDimitry Andric for (auto &I : MBB.instrs()) 787*e8d8bef9SDimitry Andric LIS->RemoveMachineInstrFromMaps(I); 788*e8d8bef9SDimitry Andric } 789*e8d8bef9SDimitry Andric MBB.clear(); 790*e8d8bef9SDimitry Andric MBB.eraseFromParent(); 791*e8d8bef9SDimitry Andric if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) { 792*e8d8bef9SDimitry Andric if (!GetFallThroughSucc(Succ)) { 793*e8d8bef9SDimitry Andric MachineFunction *MF = FallThrough->getParent(); 794*e8d8bef9SDimitry Andric MachineFunction::iterator FallThroughPos(FallThrough); 795*e8d8bef9SDimitry Andric MF->splice(std::next(FallThroughPos), Succ); 796*e8d8bef9SDimitry Andric } else 797*e8d8bef9SDimitry Andric BuildMI(*FallThrough, FallThrough->end(), 798*e8d8bef9SDimitry Andric FallThrough->findBranchDebugLoc(), TII->get(AMDGPU::S_BRANCH)) 799*e8d8bef9SDimitry Andric .addMBB(Succ); 800*e8d8bef9SDimitry Andric } 801*e8d8bef9SDimitry Andric 802*e8d8bef9SDimitry Andric return true; 8035ffd83dbSDimitry Andric } 8045ffd83dbSDimitry Andric 8055ffd83dbSDimitry Andric bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) { 8065ffd83dbSDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 8075ffd83dbSDimitry Andric TII = ST.getInstrInfo(); 8085ffd83dbSDimitry Andric TRI = &TII->getRegisterInfo(); 8095ffd83dbSDimitry Andric 8105ffd83dbSDimitry Andric // This doesn't actually need LiveIntervals, but we can preserve them. 8115ffd83dbSDimitry Andric LIS = getAnalysisIfAvailable<LiveIntervals>(); 8125ffd83dbSDimitry Andric MRI = &MF.getRegInfo(); 8135ffd83dbSDimitry Andric BoolRC = TRI->getBoolRC(); 8145ffd83dbSDimitry Andric InsertKillCleanups = 8155ffd83dbSDimitry Andric MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS; 8165ffd83dbSDimitry Andric 8175ffd83dbSDimitry Andric if (ST.isWave32()) { 8185ffd83dbSDimitry Andric AndOpc = AMDGPU::S_AND_B32; 8195ffd83dbSDimitry Andric OrOpc = AMDGPU::S_OR_B32; 8205ffd83dbSDimitry Andric XorOpc = AMDGPU::S_XOR_B32; 8215ffd83dbSDimitry Andric MovTermOpc = AMDGPU::S_MOV_B32_term; 8225ffd83dbSDimitry Andric Andn2TermOpc = AMDGPU::S_ANDN2_B32_term; 8235ffd83dbSDimitry Andric XorTermrOpc = AMDGPU::S_XOR_B32_term; 824*e8d8bef9SDimitry Andric OrTermrOpc = AMDGPU::S_OR_B32_term; 8255ffd83dbSDimitry Andric OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B32; 8265ffd83dbSDimitry Andric Exec = AMDGPU::EXEC_LO; 8275ffd83dbSDimitry Andric } else { 8285ffd83dbSDimitry Andric AndOpc = AMDGPU::S_AND_B64; 8295ffd83dbSDimitry Andric OrOpc = AMDGPU::S_OR_B64; 8305ffd83dbSDimitry Andric XorOpc = AMDGPU::S_XOR_B64; 8315ffd83dbSDimitry Andric MovTermOpc = AMDGPU::S_MOV_B64_term; 8325ffd83dbSDimitry Andric Andn2TermOpc = AMDGPU::S_ANDN2_B64_term; 8335ffd83dbSDimitry Andric XorTermrOpc = AMDGPU::S_XOR_B64_term; 834*e8d8bef9SDimitry Andric OrTermrOpc = AMDGPU::S_OR_B64_term; 8355ffd83dbSDimitry Andric OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B64; 8365ffd83dbSDimitry Andric Exec = AMDGPU::EXEC; 8375ffd83dbSDimitry Andric } 8385ffd83dbSDimitry Andric 8395ffd83dbSDimitry Andric SmallVector<MachineInstr *, 32> Worklist; 8405ffd83dbSDimitry Andric 8415ffd83dbSDimitry Andric MachineFunction::iterator NextBB; 842*e8d8bef9SDimitry Andric for (MachineFunction::iterator BI = MF.begin(); 843*e8d8bef9SDimitry Andric BI != MF.end(); BI = NextBB) { 8445ffd83dbSDimitry Andric NextBB = std::next(BI); 845*e8d8bef9SDimitry Andric MachineBasicBlock *MBB = &*BI; 8465ffd83dbSDimitry Andric 847*e8d8bef9SDimitry Andric MachineBasicBlock::iterator I, E, Next; 848*e8d8bef9SDimitry Andric E = MBB->end(); 849*e8d8bef9SDimitry Andric for (I = MBB->begin(); I != E; I = Next) { 8505ffd83dbSDimitry Andric Next = std::next(I); 8515ffd83dbSDimitry Andric MachineInstr &MI = *I; 852*e8d8bef9SDimitry Andric MachineBasicBlock *SplitMBB = MBB; 8535ffd83dbSDimitry Andric 8545ffd83dbSDimitry Andric switch (MI.getOpcode()) { 8555ffd83dbSDimitry Andric case AMDGPU::SI_IF: 856*e8d8bef9SDimitry Andric SplitMBB = process(MI); 8575ffd83dbSDimitry Andric break; 8585ffd83dbSDimitry Andric 8595ffd83dbSDimitry Andric case AMDGPU::SI_ELSE: 8605ffd83dbSDimitry Andric case AMDGPU::SI_IF_BREAK: 8615ffd83dbSDimitry Andric case AMDGPU::SI_LOOP: 8625ffd83dbSDimitry Andric case AMDGPU::SI_END_CF: 8635ffd83dbSDimitry Andric // Only build worklist if SI_IF instructions must be processed first. 8645ffd83dbSDimitry Andric if (InsertKillCleanups) 8655ffd83dbSDimitry Andric Worklist.push_back(&MI); 8665ffd83dbSDimitry Andric else 867*e8d8bef9SDimitry Andric SplitMBB = process(MI); 868*e8d8bef9SDimitry Andric break; 869*e8d8bef9SDimitry Andric 870*e8d8bef9SDimitry Andric // FIXME: find a better place for this 871*e8d8bef9SDimitry Andric case AMDGPU::SI_INIT_EXEC: 872*e8d8bef9SDimitry Andric case AMDGPU::SI_INIT_EXEC_FROM_INPUT: 873*e8d8bef9SDimitry Andric lowerInitExec(MBB, MI); 874*e8d8bef9SDimitry Andric if (LIS) 875*e8d8bef9SDimitry Andric LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC); 8765ffd83dbSDimitry Andric break; 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric default: 8795ffd83dbSDimitry Andric break; 8805ffd83dbSDimitry Andric } 881*e8d8bef9SDimitry Andric 882*e8d8bef9SDimitry Andric if (SplitMBB != MBB) { 883*e8d8bef9SDimitry Andric MBB = Next->getParent(); 884*e8d8bef9SDimitry Andric E = MBB->end(); 885*e8d8bef9SDimitry Andric } 8865ffd83dbSDimitry Andric } 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric 8895ffd83dbSDimitry Andric for (MachineInstr *MI : Worklist) 8905ffd83dbSDimitry Andric process(*MI); 8915ffd83dbSDimitry Andric 8925ffd83dbSDimitry Andric optimizeEndCf(); 8935ffd83dbSDimitry Andric 8945ffd83dbSDimitry Andric LoweredEndCf.clear(); 8955ffd83dbSDimitry Andric LoweredIf.clear(); 8965ffd83dbSDimitry Andric NeedsKillCleanup.clear(); 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric return true; 8990b57cec5SDimitry Andric } 900