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
16349cc55cSDimitry Andric /// by writing 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:
41349cc55cSDimitry Andric /// %sgpr0 = S_OR_SAVEEXEC_B64 %sgpr0 // Restore the exec mask for the Then
42349cc55cSDimitry Andric /// // block
435ffd83dbSDimitry Andric /// %exec = S_XOR_B64 %sgpr0, %exec // Update the exec mask
440b57cec5SDimitry Andric /// S_BRANCH_EXECZ label1 // Use our branch optimization
450b57cec5SDimitry Andric /// // instruction again.
460b57cec5SDimitry Andric /// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr // Do the THEN block
470b57cec5SDimitry Andric /// label1:
480b57cec5SDimitry Andric /// %exec = S_OR_B64 %exec, %sgpr0 // Re-enable saved exec mask bits
490b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric #include "AMDGPU.h"
52e8d8bef9SDimitry Andric #include "GCNSubtarget.h"
530b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
545ffd83dbSDimitry Andric #include "llvm/ADT/SmallSet.h"
550b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
56349cc55cSDimitry Andric #include "llvm/CodeGen/LiveVariables.h"
57349cc55cSDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
580b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
5904eeddc0SDimitry Andric #include "llvm/Target/TargetMachine.h"
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric using namespace llvm;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric #define DEBUG_TYPE "si-lower-control-flow"
640b57cec5SDimitry Andric
655ffd83dbSDimitry Andric static cl::opt<bool>
665ffd83dbSDimitry Andric RemoveRedundantEndcf("amdgpu-remove-redundant-endcf",
675ffd83dbSDimitry Andric cl::init(true), cl::ReallyHidden);
685ffd83dbSDimitry Andric
690b57cec5SDimitry Andric namespace {
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric class SILowerControlFlow : public MachineFunctionPass {
720b57cec5SDimitry Andric private:
730b57cec5SDimitry Andric const SIRegisterInfo *TRI = nullptr;
740b57cec5SDimitry Andric const SIInstrInfo *TII = nullptr;
750b57cec5SDimitry Andric LiveIntervals *LIS = nullptr;
76349cc55cSDimitry Andric LiveVariables *LV = nullptr;
77349cc55cSDimitry Andric MachineDominatorTree *MDT = nullptr;
780b57cec5SDimitry Andric MachineRegisterInfo *MRI = nullptr;
795ffd83dbSDimitry Andric SetVector<MachineInstr*> LoweredEndCf;
805ffd83dbSDimitry Andric DenseSet<Register> LoweredIf;
81fe6060f1SDimitry Andric SmallSet<MachineBasicBlock *, 4> KillBlocks;
825f757f3fSDimitry Andric SmallSet<Register, 8> RecomputeRegs;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric const TargetRegisterClass *BoolRC = nullptr;
850b57cec5SDimitry Andric unsigned AndOpc;
860b57cec5SDimitry Andric unsigned OrOpc;
870b57cec5SDimitry Andric unsigned XorOpc;
880b57cec5SDimitry Andric unsigned MovTermOpc;
890b57cec5SDimitry Andric unsigned Andn2TermOpc;
900b57cec5SDimitry Andric unsigned XorTermrOpc;
91e8d8bef9SDimitry Andric unsigned OrTermrOpc;
920b57cec5SDimitry Andric unsigned OrSaveExecOpc;
930b57cec5SDimitry Andric unsigned Exec;
940b57cec5SDimitry Andric
9504eeddc0SDimitry Andric bool EnableOptimizeEndCf = false;
9604eeddc0SDimitry Andric
97fe6060f1SDimitry Andric bool hasKill(const MachineBasicBlock *Begin, const MachineBasicBlock *End);
98fe6060f1SDimitry Andric
990b57cec5SDimitry Andric void emitIf(MachineInstr &MI);
1000b57cec5SDimitry Andric void emitElse(MachineInstr &MI);
1010b57cec5SDimitry Andric void emitIfBreak(MachineInstr &MI);
1020b57cec5SDimitry Andric void emitLoop(MachineInstr &MI);
103e8d8bef9SDimitry Andric
104e8d8bef9SDimitry Andric MachineBasicBlock *emitEndCf(MachineInstr &MI);
105e8d8bef9SDimitry Andric
1060b57cec5SDimitry Andric void findMaskOperands(MachineInstr &MI, unsigned OpNo,
1070b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Src) const;
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric void combineMasks(MachineInstr &MI);
1100b57cec5SDimitry Andric
111e8d8bef9SDimitry Andric bool removeMBBifRedundant(MachineBasicBlock &MBB);
112e8d8bef9SDimitry Andric
113e8d8bef9SDimitry Andric MachineBasicBlock *process(MachineInstr &MI);
1145ffd83dbSDimitry Andric
1155ffd83dbSDimitry Andric // Skip to the next instruction, ignoring debug instructions, and trivial
1165ffd83dbSDimitry Andric // block boundaries (blocks that have one (typically fallthrough) successor,
1175ffd83dbSDimitry Andric // and the successor has one predecessor.
1185ffd83dbSDimitry Andric MachineBasicBlock::iterator
1195ffd83dbSDimitry Andric skipIgnoreExecInstsTrivialSucc(MachineBasicBlock &MBB,
1205ffd83dbSDimitry Andric MachineBasicBlock::iterator It) const;
1215ffd83dbSDimitry Andric
122e8d8bef9SDimitry Andric /// Find the insertion point for a new conditional branch.
123e8d8bef9SDimitry Andric MachineBasicBlock::iterator
skipToUncondBrOrEnd(MachineBasicBlock & MBB,MachineBasicBlock::iterator I) const124e8d8bef9SDimitry Andric skipToUncondBrOrEnd(MachineBasicBlock &MBB,
125e8d8bef9SDimitry Andric MachineBasicBlock::iterator I) const {
126e8d8bef9SDimitry Andric assert(I->isTerminator());
127e8d8bef9SDimitry Andric
128e8d8bef9SDimitry Andric // FIXME: What if we had multiple pre-existing conditional branches?
129e8d8bef9SDimitry Andric MachineBasicBlock::iterator End = MBB.end();
130e8d8bef9SDimitry Andric while (I != End && !I->isUnconditionalBranch())
131e8d8bef9SDimitry Andric ++I;
132e8d8bef9SDimitry Andric return I;
133e8d8bef9SDimitry Andric }
134e8d8bef9SDimitry Andric
1355ffd83dbSDimitry Andric // Remove redundant SI_END_CF instructions.
1365ffd83dbSDimitry Andric void optimizeEndCf();
1375ffd83dbSDimitry Andric
1380b57cec5SDimitry Andric public:
1390b57cec5SDimitry Andric static char ID;
1400b57cec5SDimitry Andric
SILowerControlFlow()1410b57cec5SDimitry Andric SILowerControlFlow() : MachineFunctionPass(ID) {}
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override;
1440b57cec5SDimitry Andric
getPassName() const1450b57cec5SDimitry Andric StringRef getPassName() const override {
1460b57cec5SDimitry Andric return "SI Lower control flow pseudo instructions";
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const1490b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
150*0fca6ea1SDimitry Andric AU.addUsedIfAvailable<LiveIntervalsWrapperPass>();
1510b57cec5SDimitry Andric // Should preserve the same set that TwoAddressInstructions does.
152*0fca6ea1SDimitry Andric AU.addPreserved<MachineDominatorTreeWrapperPass>();
153*0fca6ea1SDimitry Andric AU.addPreserved<SlotIndexesWrapperPass>();
154*0fca6ea1SDimitry Andric AU.addPreserved<LiveIntervalsWrapperPass>();
1550b57cec5SDimitry Andric AU.addPreservedID(LiveVariablesID);
1560b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
1570b57cec5SDimitry Andric }
1580b57cec5SDimitry Andric };
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric } // end anonymous namespace
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric char SILowerControlFlow::ID = 0;
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
1650b57cec5SDimitry Andric "SI lower control flow", false, false)
1660b57cec5SDimitry Andric
setImpSCCDefDead(MachineInstr & MI,bool IsDead)1670b57cec5SDimitry Andric static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) {
1680b57cec5SDimitry Andric MachineOperand &ImpDefSCC = MI.getOperand(3);
1690b57cec5SDimitry Andric assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric ImpDefSCC.setIsDead(IsDead);
1720b57cec5SDimitry Andric }
1730b57cec5SDimitry Andric
1740b57cec5SDimitry Andric char &llvm::SILowerControlFlowID = SILowerControlFlow::ID;
1750b57cec5SDimitry Andric
hasKill(const MachineBasicBlock * Begin,const MachineBasicBlock * End)176fe6060f1SDimitry Andric bool SILowerControlFlow::hasKill(const MachineBasicBlock *Begin,
177fe6060f1SDimitry Andric const MachineBasicBlock *End) {
1785ffd83dbSDimitry Andric DenseSet<const MachineBasicBlock*> Visited;
179e8d8bef9SDimitry Andric SmallVector<MachineBasicBlock *, 4> Worklist(Begin->successors());
1805ffd83dbSDimitry Andric
1815ffd83dbSDimitry Andric while (!Worklist.empty()) {
1825ffd83dbSDimitry Andric MachineBasicBlock *MBB = Worklist.pop_back_val();
1835ffd83dbSDimitry Andric
1845ffd83dbSDimitry Andric if (MBB == End || !Visited.insert(MBB).second)
1855ffd83dbSDimitry Andric continue;
186fe6060f1SDimitry Andric if (KillBlocks.contains(MBB))
1875ffd83dbSDimitry Andric return true;
1885ffd83dbSDimitry Andric
1895ffd83dbSDimitry Andric Worklist.append(MBB->succ_begin(), MBB->succ_end());
1905ffd83dbSDimitry Andric }
1915ffd83dbSDimitry Andric
1925ffd83dbSDimitry Andric return false;
1935ffd83dbSDimitry Andric }
1945ffd83dbSDimitry Andric
isSimpleIf(const MachineInstr & MI,const MachineRegisterInfo * MRI)1955ffd83dbSDimitry Andric static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI) {
1968bcb0991SDimitry Andric Register SaveExecReg = MI.getOperand(0).getReg();
1970b57cec5SDimitry Andric auto U = MRI->use_instr_nodbg_begin(SaveExecReg);
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric if (U == MRI->use_instr_nodbg_end() ||
2000b57cec5SDimitry Andric std::next(U) != MRI->use_instr_nodbg_end() ||
2010b57cec5SDimitry Andric U->getOpcode() != AMDGPU::SI_END_CF)
2020b57cec5SDimitry Andric return false;
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric return true;
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric
emitIf(MachineInstr & MI)2070b57cec5SDimitry Andric void SILowerControlFlow::emitIf(MachineInstr &MI) {
2080b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
2090b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc();
2100b57cec5SDimitry Andric MachineBasicBlock::iterator I(&MI);
2115ffd83dbSDimitry Andric Register SaveExecReg = MI.getOperand(0).getReg();
2120b57cec5SDimitry Andric MachineOperand& Cond = MI.getOperand(1);
2138bcb0991SDimitry Andric assert(Cond.getSubReg() == AMDGPU::NoSubRegister);
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric MachineOperand &ImpDefSCC = MI.getOperand(4);
2160b57cec5SDimitry Andric assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric // If there is only one use of save exec register and that use is SI_END_CF,
2190b57cec5SDimitry Andric // we can optimize SI_IF by returning the full saved exec mask instead of
2200b57cec5SDimitry Andric // just cleared bits.
2215ffd83dbSDimitry Andric bool SimpleIf = isSimpleIf(MI, MRI);
2225ffd83dbSDimitry Andric
223fe6060f1SDimitry Andric if (SimpleIf) {
2245ffd83dbSDimitry Andric // Check for SI_KILL_*_TERMINATOR on path from if to endif.
2255ffd83dbSDimitry Andric // if there is any such terminator simplifications are not safe.
2265ffd83dbSDimitry Andric auto UseMI = MRI->use_instr_nodbg_begin(SaveExecReg);
227fe6060f1SDimitry Andric SimpleIf = !hasKill(MI.getParent(), UseMI->getParent());
2285ffd83dbSDimitry Andric }
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric // Add an implicit def of exec to discourage scheduling VALU after this which
2310b57cec5SDimitry Andric // will interfere with trying to form s_and_saveexec_b64 later.
2320b57cec5SDimitry Andric Register CopyReg = SimpleIf ? SaveExecReg
2330b57cec5SDimitry Andric : MRI->createVirtualRegister(BoolRC);
2340b57cec5SDimitry Andric MachineInstr *CopyExec =
2350b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg)
2360b57cec5SDimitry Andric .addReg(Exec)
2370b57cec5SDimitry Andric .addReg(Exec, RegState::ImplicitDefine);
2385ffd83dbSDimitry Andric LoweredIf.insert(CopyReg);
2390b57cec5SDimitry Andric
2408bcb0991SDimitry Andric Register Tmp = MRI->createVirtualRegister(BoolRC);
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric MachineInstr *And =
2430b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(AndOpc), Tmp)
2440b57cec5SDimitry Andric .addReg(CopyReg)
2450b57cec5SDimitry Andric .add(Cond);
246349cc55cSDimitry Andric if (LV)
247349cc55cSDimitry Andric LV->replaceKillInstruction(Cond.getReg(), MI, *And);
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric setImpSCCDefDead(*And, true);
2500b57cec5SDimitry Andric
2510b57cec5SDimitry Andric MachineInstr *Xor = nullptr;
2520b57cec5SDimitry Andric if (!SimpleIf) {
2530b57cec5SDimitry Andric Xor =
2540b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(XorOpc), SaveExecReg)
2550b57cec5SDimitry Andric .addReg(Tmp)
2560b57cec5SDimitry Andric .addReg(CopyReg);
2570b57cec5SDimitry Andric setImpSCCDefDead(*Xor, ImpDefSCC.isDead());
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric // Use a copy that is a terminator to get correct spill code placement it with
2610b57cec5SDimitry Andric // fast regalloc.
2620b57cec5SDimitry Andric MachineInstr *SetExec =
2630b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(MovTermOpc), Exec)
2640b57cec5SDimitry Andric .addReg(Tmp, RegState::Kill);
265349cc55cSDimitry Andric if (LV)
266349cc55cSDimitry Andric LV->getVarInfo(Tmp).Kills.push_back(SetExec);
2670b57cec5SDimitry Andric
268e8d8bef9SDimitry Andric // Skip ahead to the unconditional branch in case there are other terminators
269e8d8bef9SDimitry Andric // present.
270e8d8bef9SDimitry Andric I = skipToUncondBrOrEnd(MBB, I);
271e8d8bef9SDimitry Andric
2725ffd83dbSDimitry Andric // Insert the S_CBRANCH_EXECZ instruction which will be optimized later
2735ffd83dbSDimitry Andric // during SIRemoveShortExecBranches.
2745ffd83dbSDimitry Andric MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
2750b57cec5SDimitry Andric .add(MI.getOperand(2));
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric if (!LIS) {
2780b57cec5SDimitry Andric MI.eraseFromParent();
2790b57cec5SDimitry Andric return;
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*CopyExec);
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // Replace with and so we don't need to fix the live interval for condition
2850b57cec5SDimitry Andric // register.
2860b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *And);
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric if (!SimpleIf)
2890b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Xor);
2900b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*SetExec);
2910b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*NewBr);
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
2940b57cec5SDimitry Andric MI.eraseFromParent();
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric // FIXME: Is there a better way of adjusting the liveness? It shouldn't be
2970b57cec5SDimitry Andric // hard to add another def here but I'm not sure how to correctly update the
2980b57cec5SDimitry Andric // valno.
2995f757f3fSDimitry Andric RecomputeRegs.insert(SaveExecReg);
3000b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(Tmp);
3010b57cec5SDimitry Andric if (!SimpleIf)
3020b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(CopyReg);
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
emitElse(MachineInstr & MI)3050b57cec5SDimitry Andric void SILowerControlFlow::emitElse(MachineInstr &MI) {
3060b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
3070b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc();
3080b57cec5SDimitry Andric
3095ffd83dbSDimitry Andric Register DstReg = MI.getOperand(0).getReg();
3105f757f3fSDimitry Andric Register SrcReg = MI.getOperand(1).getReg();
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric MachineBasicBlock::iterator Start = MBB.begin();
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric // This must be inserted before phis and any spill code inserted before the
3150b57cec5SDimitry Andric // else.
316e8d8bef9SDimitry Andric Register SaveReg = MRI->createVirtualRegister(BoolRC);
3170b57cec5SDimitry Andric MachineInstr *OrSaveExec =
3180b57cec5SDimitry Andric BuildMI(MBB, Start, DL, TII->get(OrSaveExecOpc), SaveReg)
319e8d8bef9SDimitry Andric .add(MI.getOperand(1)); // Saved EXEC
320349cc55cSDimitry Andric if (LV)
3215f757f3fSDimitry Andric LV->replaceKillInstruction(SrcReg, MI, *OrSaveExec);
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric MachineBasicBlock *DestBB = MI.getOperand(2).getMBB();
3240b57cec5SDimitry Andric
3250b57cec5SDimitry Andric MachineBasicBlock::iterator ElsePt(MI);
3260b57cec5SDimitry Andric
327e8d8bef9SDimitry Andric // This accounts for any modification of the EXEC mask within the block and
328e8d8bef9SDimitry Andric // can be optimized out pre-RA when not required.
329e8d8bef9SDimitry Andric MachineInstr *And = BuildMI(MBB, ElsePt, DL, TII->get(AndOpc), DstReg)
3300b57cec5SDimitry Andric .addReg(Exec)
3310b57cec5SDimitry Andric .addReg(SaveReg);
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric MachineInstr *Xor =
3340b57cec5SDimitry Andric BuildMI(MBB, ElsePt, DL, TII->get(XorTermrOpc), Exec)
3350b57cec5SDimitry Andric .addReg(Exec)
3360b57cec5SDimitry Andric .addReg(DstReg);
3370b57cec5SDimitry Andric
338e8d8bef9SDimitry Andric // Skip ahead to the unconditional branch in case there are other terminators
339e8d8bef9SDimitry Andric // present.
340e8d8bef9SDimitry Andric ElsePt = skipToUncondBrOrEnd(MBB, ElsePt);
341e8d8bef9SDimitry Andric
3420b57cec5SDimitry Andric MachineInstr *Branch =
3435ffd83dbSDimitry Andric BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
3440b57cec5SDimitry Andric .addMBB(DestBB);
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andric if (!LIS) {
3470b57cec5SDimitry Andric MI.eraseFromParent();
3480b57cec5SDimitry Andric return;
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric LIS->RemoveMachineInstrFromMaps(MI);
3520b57cec5SDimitry Andric MI.eraseFromParent();
3530b57cec5SDimitry Andric
3540b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*OrSaveExec);
3555f757f3fSDimitry Andric LIS->InsertMachineInstrInMaps(*And);
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Xor);
3580b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Branch);
3590b57cec5SDimitry Andric
3605f757f3fSDimitry Andric RecomputeRegs.insert(SrcReg);
3615f757f3fSDimitry Andric RecomputeRegs.insert(DstReg);
3620b57cec5SDimitry Andric LIS->createAndComputeVirtRegInterval(SaveReg);
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric // Let this be recomputed.
3650b57cec5SDimitry Andric LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric
emitIfBreak(MachineInstr & MI)3680b57cec5SDimitry Andric void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
3690b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
3700b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc();
3715ffd83dbSDimitry Andric auto Dst = MI.getOperand(0).getReg();
3720b57cec5SDimitry Andric
3730b57cec5SDimitry Andric // Skip ANDing with exec if the break condition is already masked by exec
3740b57cec5SDimitry Andric // because it is a V_CMP in the same basic block. (We know the break
3750b57cec5SDimitry Andric // condition operand was an i1 in IR, so if it is a VALU instruction it must
3760b57cec5SDimitry Andric // be one with a carry-out.)
3770b57cec5SDimitry Andric bool SkipAnding = false;
3780b57cec5SDimitry Andric if (MI.getOperand(1).isReg()) {
3790b57cec5SDimitry Andric if (MachineInstr *Def = MRI->getUniqueVRegDef(MI.getOperand(1).getReg())) {
3800b57cec5SDimitry Andric SkipAnding = Def->getParent() == MI.getParent()
3810b57cec5SDimitry Andric && SIInstrInfo::isVALU(*Def);
3820b57cec5SDimitry Andric }
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric // AND the break condition operand with exec, then OR that into the "loop
3860b57cec5SDimitry Andric // exit" mask.
3870b57cec5SDimitry Andric MachineInstr *And = nullptr, *Or = nullptr;
3885f757f3fSDimitry Andric Register AndReg;
3890b57cec5SDimitry Andric if (!SkipAnding) {
3905f757f3fSDimitry Andric AndReg = MRI->createVirtualRegister(BoolRC);
391480093f4SDimitry Andric And = BuildMI(MBB, &MI, DL, TII->get(AndOpc), AndReg)
3920b57cec5SDimitry Andric .addReg(Exec)
3930b57cec5SDimitry Andric .add(MI.getOperand(1));
394349cc55cSDimitry Andric if (LV)
395349cc55cSDimitry Andric LV->replaceKillInstruction(MI.getOperand(1).getReg(), MI, *And);
3960b57cec5SDimitry Andric Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
397480093f4SDimitry Andric .addReg(AndReg)
3980b57cec5SDimitry Andric .add(MI.getOperand(2));
399349cc55cSDimitry Andric } else {
4000b57cec5SDimitry Andric Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
4010b57cec5SDimitry Andric .add(MI.getOperand(1))
4020b57cec5SDimitry Andric .add(MI.getOperand(2));
403349cc55cSDimitry Andric if (LV)
404349cc55cSDimitry Andric LV->replaceKillInstruction(MI.getOperand(1).getReg(), MI, *Or);
405349cc55cSDimitry Andric }
406349cc55cSDimitry Andric if (LV)
407349cc55cSDimitry Andric LV->replaceKillInstruction(MI.getOperand(2).getReg(), MI, *Or);
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andric if (LIS) {
4100b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *Or);
4115f757f3fSDimitry Andric if (And) {
4125f757f3fSDimitry Andric // Read of original operand 1 is on And now not Or.
4135f757f3fSDimitry Andric RecomputeRegs.insert(And->getOperand(2).getReg());
4145f757f3fSDimitry Andric LIS->InsertMachineInstrInMaps(*And);
4155f757f3fSDimitry Andric LIS->createAndComputeVirtRegInterval(AndReg);
4165f757f3fSDimitry Andric }
4170b57cec5SDimitry Andric }
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andric MI.eraseFromParent();
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric
emitLoop(MachineInstr & MI)4220b57cec5SDimitry Andric void SILowerControlFlow::emitLoop(MachineInstr &MI) {
4230b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
4240b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc();
4250b57cec5SDimitry Andric
4260b57cec5SDimitry Andric MachineInstr *AndN2 =
4270b57cec5SDimitry Andric BuildMI(MBB, &MI, DL, TII->get(Andn2TermOpc), Exec)
4280b57cec5SDimitry Andric .addReg(Exec)
4290b57cec5SDimitry Andric .add(MI.getOperand(0));
43006c3fb27SDimitry Andric if (LV)
43106c3fb27SDimitry Andric LV->replaceKillInstruction(MI.getOperand(0).getReg(), MI, *AndN2);
4320b57cec5SDimitry Andric
433e8d8bef9SDimitry Andric auto BranchPt = skipToUncondBrOrEnd(MBB, MI.getIterator());
4340b57cec5SDimitry Andric MachineInstr *Branch =
435e8d8bef9SDimitry Andric BuildMI(MBB, BranchPt, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
4360b57cec5SDimitry Andric .add(MI.getOperand(1));
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric if (LIS) {
4395f757f3fSDimitry Andric RecomputeRegs.insert(MI.getOperand(0).getReg());
4400b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *AndN2);
4410b57cec5SDimitry Andric LIS->InsertMachineInstrInMaps(*Branch);
4420b57cec5SDimitry Andric }
4430b57cec5SDimitry Andric
4440b57cec5SDimitry Andric MI.eraseFromParent();
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric
4475ffd83dbSDimitry Andric MachineBasicBlock::iterator
skipIgnoreExecInstsTrivialSucc(MachineBasicBlock & MBB,MachineBasicBlock::iterator It) const4485ffd83dbSDimitry Andric SILowerControlFlow::skipIgnoreExecInstsTrivialSucc(
4495ffd83dbSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const {
4505ffd83dbSDimitry Andric
4515ffd83dbSDimitry Andric SmallSet<const MachineBasicBlock *, 4> Visited;
4525ffd83dbSDimitry Andric MachineBasicBlock *B = &MBB;
4535ffd83dbSDimitry Andric do {
4545ffd83dbSDimitry Andric if (!Visited.insert(B).second)
4555ffd83dbSDimitry Andric return MBB.end();
4565ffd83dbSDimitry Andric
4575ffd83dbSDimitry Andric auto E = B->end();
4585ffd83dbSDimitry Andric for ( ; It != E; ++It) {
4595ffd83dbSDimitry Andric if (TII->mayReadEXEC(*MRI, *It))
4605ffd83dbSDimitry Andric break;
4615ffd83dbSDimitry Andric }
4625ffd83dbSDimitry Andric
4635ffd83dbSDimitry Andric if (It != E)
4645ffd83dbSDimitry Andric return It;
4655ffd83dbSDimitry Andric
4665ffd83dbSDimitry Andric if (B->succ_size() != 1)
4675ffd83dbSDimitry Andric return MBB.end();
4685ffd83dbSDimitry Andric
4695ffd83dbSDimitry Andric // If there is one trivial successor, advance to the next block.
4705ffd83dbSDimitry Andric MachineBasicBlock *Succ = *B->succ_begin();
4715ffd83dbSDimitry Andric
4725ffd83dbSDimitry Andric It = Succ->begin();
4735ffd83dbSDimitry Andric B = Succ;
4745ffd83dbSDimitry Andric } while (true);
4755ffd83dbSDimitry Andric }
4765ffd83dbSDimitry Andric
emitEndCf(MachineInstr & MI)477e8d8bef9SDimitry Andric MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
4780b57cec5SDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
4790b57cec5SDimitry Andric const DebugLoc &DL = MI.getDebugLoc();
4800b57cec5SDimitry Andric
481e8d8bef9SDimitry Andric MachineBasicBlock::iterator InsPt = MBB.begin();
482e8d8bef9SDimitry Andric
483e8d8bef9SDimitry Andric // If we have instructions that aren't prolog instructions, split the block
484e8d8bef9SDimitry Andric // and emit a terminator instruction. This ensures correct spill placement.
485e8d8bef9SDimitry Andric // FIXME: We should unconditionally split the block here.
486e8d8bef9SDimitry Andric bool NeedBlockSplit = false;
487e8d8bef9SDimitry Andric Register DataReg = MI.getOperand(0).getReg();
488e8d8bef9SDimitry Andric for (MachineBasicBlock::iterator I = InsPt, E = MI.getIterator();
489e8d8bef9SDimitry Andric I != E; ++I) {
490e8d8bef9SDimitry Andric if (I->modifiesRegister(DataReg, TRI)) {
491e8d8bef9SDimitry Andric NeedBlockSplit = true;
492e8d8bef9SDimitry Andric break;
493e8d8bef9SDimitry Andric }
494e8d8bef9SDimitry Andric }
495e8d8bef9SDimitry Andric
496e8d8bef9SDimitry Andric unsigned Opcode = OrOpc;
497e8d8bef9SDimitry Andric MachineBasicBlock *SplitBB = &MBB;
498e8d8bef9SDimitry Andric if (NeedBlockSplit) {
499e8d8bef9SDimitry Andric SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS);
500349cc55cSDimitry Andric if (MDT && SplitBB != &MBB) {
501349cc55cSDimitry Andric MachineDomTreeNode *MBBNode = (*MDT)[&MBB];
502349cc55cSDimitry Andric SmallVector<MachineDomTreeNode *> Children(MBBNode->begin(),
503349cc55cSDimitry Andric MBBNode->end());
504349cc55cSDimitry Andric MachineDomTreeNode *SplitBBNode = MDT->addNewBlock(SplitBB, &MBB);
505349cc55cSDimitry Andric for (MachineDomTreeNode *Child : Children)
506349cc55cSDimitry Andric MDT->changeImmediateDominator(Child, SplitBBNode);
507349cc55cSDimitry Andric }
508e8d8bef9SDimitry Andric Opcode = OrTermrOpc;
509e8d8bef9SDimitry Andric InsPt = MI;
510e8d8bef9SDimitry Andric }
511e8d8bef9SDimitry Andric
512e8d8bef9SDimitry Andric MachineInstr *NewMI =
513e8d8bef9SDimitry Andric BuildMI(MBB, InsPt, DL, TII->get(Opcode), Exec)
5140b57cec5SDimitry Andric .addReg(Exec)
5150b57cec5SDimitry Andric .add(MI.getOperand(0));
51681ad6265SDimitry Andric if (LV) {
51781ad6265SDimitry Andric LV->replaceKillInstruction(DataReg, MI, *NewMI);
51881ad6265SDimitry Andric
51981ad6265SDimitry Andric if (SplitBB != &MBB) {
52006c3fb27SDimitry Andric // Track the set of registers defined in the original block so we don't
52106c3fb27SDimitry Andric // accidentally add the original block to AliveBlocks. AliveBlocks only
52206c3fb27SDimitry Andric // includes blocks which are live through, which excludes live outs and
52306c3fb27SDimitry Andric // local defs.
52406c3fb27SDimitry Andric DenseSet<Register> DefInOrigBlock;
52506c3fb27SDimitry Andric
52606c3fb27SDimitry Andric for (MachineBasicBlock *BlockPiece : {&MBB, SplitBB}) {
52706c3fb27SDimitry Andric for (MachineInstr &X : *BlockPiece) {
52806c3fb27SDimitry Andric for (MachineOperand &Op : X.all_defs()) {
52906c3fb27SDimitry Andric if (Op.getReg().isVirtual())
53006c3fb27SDimitry Andric DefInOrigBlock.insert(Op.getReg());
53106c3fb27SDimitry Andric }
53281ad6265SDimitry Andric }
53381ad6265SDimitry Andric }
53481ad6265SDimitry Andric
53581ad6265SDimitry Andric for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
53681ad6265SDimitry Andric Register Reg = Register::index2VirtReg(i);
53781ad6265SDimitry Andric LiveVariables::VarInfo &VI = LV->getVarInfo(Reg);
53881ad6265SDimitry Andric
53981ad6265SDimitry Andric if (VI.AliveBlocks.test(MBB.getNumber()))
54081ad6265SDimitry Andric VI.AliveBlocks.set(SplitBB->getNumber());
54181ad6265SDimitry Andric else {
54281ad6265SDimitry Andric for (MachineInstr *Kill : VI.Kills) {
54306c3fb27SDimitry Andric if (Kill->getParent() == SplitBB && !DefInOrigBlock.contains(Reg))
54481ad6265SDimitry Andric VI.AliveBlocks.set(MBB.getNumber());
54581ad6265SDimitry Andric }
54681ad6265SDimitry Andric }
54781ad6265SDimitry Andric }
54881ad6265SDimitry Andric }
54981ad6265SDimitry Andric }
5500b57cec5SDimitry Andric
5515ffd83dbSDimitry Andric LoweredEndCf.insert(NewMI);
5525ffd83dbSDimitry Andric
553fe6060f1SDimitry Andric if (LIS)
5540b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric MI.eraseFromParent();
5570b57cec5SDimitry Andric
5580b57cec5SDimitry Andric if (LIS)
5590b57cec5SDimitry Andric LIS->handleMove(*NewMI);
560e8d8bef9SDimitry Andric return SplitBB;
5610b57cec5SDimitry Andric }
5620b57cec5SDimitry Andric
5630b57cec5SDimitry Andric // Returns replace operands for a logical operation, either single result
5640b57cec5SDimitry Andric // for exec or two operands if source was another equivalent operation.
findMaskOperands(MachineInstr & MI,unsigned OpNo,SmallVectorImpl<MachineOperand> & Src) const5650b57cec5SDimitry Andric void SILowerControlFlow::findMaskOperands(MachineInstr &MI, unsigned OpNo,
5660b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Src) const {
5670b57cec5SDimitry Andric MachineOperand &Op = MI.getOperand(OpNo);
568e8d8bef9SDimitry Andric if (!Op.isReg() || !Op.getReg().isVirtual()) {
5690b57cec5SDimitry Andric Src.push_back(Op);
5700b57cec5SDimitry Andric return;
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric
5730b57cec5SDimitry Andric MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg());
5740b57cec5SDimitry Andric if (!Def || Def->getParent() != MI.getParent() ||
5750b57cec5SDimitry Andric !(Def->isFullCopy() || (Def->getOpcode() == MI.getOpcode())))
5760b57cec5SDimitry Andric return;
5770b57cec5SDimitry Andric
5780b57cec5SDimitry Andric // Make sure we do not modify exec between def and use.
57981ad6265SDimitry Andric // A copy with implicitly defined exec inserted earlier is an exclusion, it
5800b57cec5SDimitry Andric // does not really modify exec.
5810b57cec5SDimitry Andric for (auto I = Def->getIterator(); I != MI.getIterator(); ++I)
5820b57cec5SDimitry Andric if (I->modifiesRegister(AMDGPU::EXEC, TRI) &&
5830b57cec5SDimitry Andric !(I->isCopy() && I->getOperand(0).getReg() != Exec))
5840b57cec5SDimitry Andric return;
5850b57cec5SDimitry Andric
5860b57cec5SDimitry Andric for (const auto &SrcOp : Def->explicit_operands())
5870b57cec5SDimitry Andric if (SrcOp.isReg() && SrcOp.isUse() &&
588e8d8bef9SDimitry Andric (SrcOp.getReg().isVirtual() || SrcOp.getReg() == Exec))
5890b57cec5SDimitry Andric Src.push_back(SrcOp);
5900b57cec5SDimitry Andric }
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric // Search and combine pairs of equivalent instructions, like
5930b57cec5SDimitry Andric // S_AND_B64 x, (S_AND_B64 x, y) => S_AND_B64 x, y
5940b57cec5SDimitry Andric // S_OR_B64 x, (S_OR_B64 x, y) => S_OR_B64 x, y
5950b57cec5SDimitry Andric // One of the operands is exec mask.
combineMasks(MachineInstr & MI)5960b57cec5SDimitry Andric void SILowerControlFlow::combineMasks(MachineInstr &MI) {
5970b57cec5SDimitry Andric assert(MI.getNumExplicitOperands() == 3);
5980b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Ops;
5990b57cec5SDimitry Andric unsigned OpToReplace = 1;
6000b57cec5SDimitry Andric findMaskOperands(MI, 1, Ops);
6010b57cec5SDimitry Andric if (Ops.size() == 1) OpToReplace = 2; // First operand can be exec or its copy
6020b57cec5SDimitry Andric findMaskOperands(MI, 2, Ops);
6030b57cec5SDimitry Andric if (Ops.size() != 3) return;
6040b57cec5SDimitry Andric
6050b57cec5SDimitry Andric unsigned UniqueOpndIdx;
6060b57cec5SDimitry Andric if (Ops[0].isIdenticalTo(Ops[1])) UniqueOpndIdx = 2;
6070b57cec5SDimitry Andric else if (Ops[0].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
6080b57cec5SDimitry Andric else if (Ops[1].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
6090b57cec5SDimitry Andric else return;
6100b57cec5SDimitry Andric
6118bcb0991SDimitry Andric Register Reg = MI.getOperand(OpToReplace).getReg();
61281ad6265SDimitry Andric MI.removeOperand(OpToReplace);
6130b57cec5SDimitry Andric MI.addOperand(Ops[UniqueOpndIdx]);
6140b57cec5SDimitry Andric if (MRI->use_empty(Reg))
6150b57cec5SDimitry Andric MRI->getUniqueVRegDef(Reg)->eraseFromParent();
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric
optimizeEndCf()6185ffd83dbSDimitry Andric void SILowerControlFlow::optimizeEndCf() {
61981ad6265SDimitry Andric // If the only instruction immediately following this END_CF is another
6205ffd83dbSDimitry Andric // END_CF in the only successor we can avoid emitting exec mask restore here.
62104eeddc0SDimitry Andric if (!EnableOptimizeEndCf)
6225ffd83dbSDimitry Andric return;
6230b57cec5SDimitry Andric
62404eeddc0SDimitry Andric for (MachineInstr *MI : reverse(LoweredEndCf)) {
6255ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI->getParent();
6265ffd83dbSDimitry Andric auto Next =
6275ffd83dbSDimitry Andric skipIgnoreExecInstsTrivialSucc(MBB, std::next(MI->getIterator()));
6285ffd83dbSDimitry Andric if (Next == MBB.end() || !LoweredEndCf.count(&*Next))
6295ffd83dbSDimitry Andric continue;
6305ffd83dbSDimitry Andric // Only skip inner END_CF if outer ENDCF belongs to SI_IF.
6315ffd83dbSDimitry Andric // If that belongs to SI_ELSE then saved mask has an inverted value.
6325ffd83dbSDimitry Andric Register SavedExec
6335ffd83dbSDimitry Andric = TII->getNamedOperand(*Next, AMDGPU::OpName::src1)->getReg();
6345ffd83dbSDimitry Andric assert(SavedExec.isVirtual() && "Expected saved exec to be src1!");
6350b57cec5SDimitry Andric
6365ffd83dbSDimitry Andric const MachineInstr *Def = MRI->getUniqueVRegDef(SavedExec);
6375ffd83dbSDimitry Andric if (Def && LoweredIf.count(SavedExec)) {
6385ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Skip redundant "; MI->dump());
6395ffd83dbSDimitry Andric if (LIS)
6405ffd83dbSDimitry Andric LIS->RemoveMachineInstrFromMaps(*MI);
641349cc55cSDimitry Andric Register Reg;
642349cc55cSDimitry Andric if (LV)
643349cc55cSDimitry Andric Reg = TII->getNamedOperand(*MI, AMDGPU::OpName::src1)->getReg();
6445ffd83dbSDimitry Andric MI->eraseFromParent();
645349cc55cSDimitry Andric if (LV)
646349cc55cSDimitry Andric LV->recomputeForSingleDefVirtReg(Reg);
647e8d8bef9SDimitry Andric removeMBBifRedundant(MBB);
6485ffd83dbSDimitry Andric }
6495ffd83dbSDimitry Andric }
6500b57cec5SDimitry Andric }
6510b57cec5SDimitry Andric
process(MachineInstr & MI)652e8d8bef9SDimitry Andric MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) {
6535ffd83dbSDimitry Andric MachineBasicBlock &MBB = *MI.getParent();
6545ffd83dbSDimitry Andric MachineBasicBlock::iterator I(MI);
6555ffd83dbSDimitry Andric MachineInstr *Prev = (I != MBB.begin()) ? &*(std::prev(I)) : nullptr;
6560b57cec5SDimitry Andric
657e8d8bef9SDimitry Andric MachineBasicBlock *SplitBB = &MBB;
658e8d8bef9SDimitry Andric
6590b57cec5SDimitry Andric switch (MI.getOpcode()) {
6600b57cec5SDimitry Andric case AMDGPU::SI_IF:
6610b57cec5SDimitry Andric emitIf(MI);
6620b57cec5SDimitry Andric break;
6630b57cec5SDimitry Andric
6640b57cec5SDimitry Andric case AMDGPU::SI_ELSE:
6650b57cec5SDimitry Andric emitElse(MI);
6660b57cec5SDimitry Andric break;
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andric case AMDGPU::SI_IF_BREAK:
6690b57cec5SDimitry Andric emitIfBreak(MI);
6700b57cec5SDimitry Andric break;
6710b57cec5SDimitry Andric
6720b57cec5SDimitry Andric case AMDGPU::SI_LOOP:
6730b57cec5SDimitry Andric emitLoop(MI);
6740b57cec5SDimitry Andric break;
6750b57cec5SDimitry Andric
676fe6060f1SDimitry Andric case AMDGPU::SI_WATERFALL_LOOP:
677fe6060f1SDimitry Andric MI.setDesc(TII->get(AMDGPU::S_CBRANCH_EXECNZ));
678fe6060f1SDimitry Andric break;
679fe6060f1SDimitry Andric
6800b57cec5SDimitry Andric case AMDGPU::SI_END_CF:
681e8d8bef9SDimitry Andric SplitBB = emitEndCf(MI);
6820b57cec5SDimitry Andric break;
6830b57cec5SDimitry Andric
6845ffd83dbSDimitry Andric default:
6855ffd83dbSDimitry Andric assert(false && "Attempt to process unsupported instruction");
6865ffd83dbSDimitry Andric break;
6875ffd83dbSDimitry Andric }
6885ffd83dbSDimitry Andric
6895ffd83dbSDimitry Andric MachineBasicBlock::iterator Next;
6905ffd83dbSDimitry Andric for (I = Prev ? Prev->getIterator() : MBB.begin(); I != MBB.end(); I = Next) {
6915ffd83dbSDimitry Andric Next = std::next(I);
6925ffd83dbSDimitry Andric MachineInstr &MaskMI = *I;
6935ffd83dbSDimitry Andric switch (MaskMI.getOpcode()) {
6940b57cec5SDimitry Andric case AMDGPU::S_AND_B64:
6950b57cec5SDimitry Andric case AMDGPU::S_OR_B64:
6960b57cec5SDimitry Andric case AMDGPU::S_AND_B32:
6970b57cec5SDimitry Andric case AMDGPU::S_OR_B32:
6980b57cec5SDimitry Andric // Cleanup bit manipulations on exec mask
6995ffd83dbSDimitry Andric combineMasks(MaskMI);
7005ffd83dbSDimitry Andric break;
7015ffd83dbSDimitry Andric default:
7025ffd83dbSDimitry Andric I = MBB.end();
7035ffd83dbSDimitry Andric break;
7045ffd83dbSDimitry Andric }
7055ffd83dbSDimitry Andric }
706e8d8bef9SDimitry Andric
707e8d8bef9SDimitry Andric return SplitBB;
708e8d8bef9SDimitry Andric }
709e8d8bef9SDimitry Andric
removeMBBifRedundant(MachineBasicBlock & MBB)710e8d8bef9SDimitry Andric bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
711e8d8bef9SDimitry Andric for (auto &I : MBB.instrs()) {
712e8d8bef9SDimitry Andric if (!I.isDebugInstr() && !I.isUnconditionalBranch())
713e8d8bef9SDimitry Andric return false;
714e8d8bef9SDimitry Andric }
715e8d8bef9SDimitry Andric
716e8d8bef9SDimitry Andric assert(MBB.succ_size() == 1 && "MBB has more than one successor");
717e8d8bef9SDimitry Andric
718e8d8bef9SDimitry Andric MachineBasicBlock *Succ = *MBB.succ_begin();
719e8d8bef9SDimitry Andric MachineBasicBlock *FallThrough = nullptr;
720e8d8bef9SDimitry Andric
721e8d8bef9SDimitry Andric while (!MBB.predecessors().empty()) {
722e8d8bef9SDimitry Andric MachineBasicBlock *P = *MBB.pred_begin();
7235f757f3fSDimitry Andric if (P->getFallThrough(false) == &MBB)
724e8d8bef9SDimitry Andric FallThrough = P;
725e8d8bef9SDimitry Andric P->ReplaceUsesOfBlockWith(&MBB, Succ);
726e8d8bef9SDimitry Andric }
727e8d8bef9SDimitry Andric MBB.removeSuccessor(Succ);
728e8d8bef9SDimitry Andric if (LIS) {
729e8d8bef9SDimitry Andric for (auto &I : MBB.instrs())
730e8d8bef9SDimitry Andric LIS->RemoveMachineInstrFromMaps(I);
731e8d8bef9SDimitry Andric }
732349cc55cSDimitry Andric if (MDT) {
733349cc55cSDimitry Andric // If Succ, the single successor of MBB, is dominated by MBB, MDT needs
734349cc55cSDimitry Andric // updating by changing Succ's idom to the one of MBB; otherwise, MBB must
735349cc55cSDimitry Andric // be a leaf node in MDT and could be erased directly.
736349cc55cSDimitry Andric if (MDT->dominates(&MBB, Succ))
737349cc55cSDimitry Andric MDT->changeImmediateDominator(MDT->getNode(Succ),
738349cc55cSDimitry Andric MDT->getNode(&MBB)->getIDom());
739349cc55cSDimitry Andric MDT->eraseNode(&MBB);
740349cc55cSDimitry Andric }
741e8d8bef9SDimitry Andric MBB.clear();
742e8d8bef9SDimitry Andric MBB.eraseFromParent();
743e8d8bef9SDimitry Andric if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) {
7445f757f3fSDimitry Andric // Note: we cannot update block layout and preserve live intervals;
7455f757f3fSDimitry Andric // hence we must insert a branch.
7465f757f3fSDimitry Andric MachineInstr *BranchMI = BuildMI(*FallThrough, FallThrough->end(),
747e8d8bef9SDimitry Andric FallThrough->findBranchDebugLoc(), TII->get(AMDGPU::S_BRANCH))
748e8d8bef9SDimitry Andric .addMBB(Succ);
7495f757f3fSDimitry Andric if (LIS)
7505f757f3fSDimitry Andric LIS->InsertMachineInstrInMaps(*BranchMI);
751e8d8bef9SDimitry Andric }
752e8d8bef9SDimitry Andric
753e8d8bef9SDimitry Andric return true;
7545ffd83dbSDimitry Andric }
7555ffd83dbSDimitry Andric
runOnMachineFunction(MachineFunction & MF)7565ffd83dbSDimitry Andric bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
7575ffd83dbSDimitry Andric const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
7585ffd83dbSDimitry Andric TII = ST.getInstrInfo();
7595ffd83dbSDimitry Andric TRI = &TII->getRegisterInfo();
7605f757f3fSDimitry Andric EnableOptimizeEndCf = RemoveRedundantEndcf &&
7615f757f3fSDimitry Andric MF.getTarget().getOptLevel() > CodeGenOptLevel::None;
7625ffd83dbSDimitry Andric
7635ffd83dbSDimitry Andric // This doesn't actually need LiveIntervals, but we can preserve them.
764*0fca6ea1SDimitry Andric auto *LISWrapper = getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
765*0fca6ea1SDimitry Andric LIS = LISWrapper ? &LISWrapper->getLIS() : nullptr;
766349cc55cSDimitry Andric // This doesn't actually need LiveVariables, but we can preserve them.
767*0fca6ea1SDimitry Andric auto *LVWrapper = getAnalysisIfAvailable<LiveVariablesWrapperPass>();
768*0fca6ea1SDimitry Andric LV = LVWrapper ? &LVWrapper->getLV() : nullptr;
769*0fca6ea1SDimitry Andric auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
770*0fca6ea1SDimitry Andric MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
7715ffd83dbSDimitry Andric MRI = &MF.getRegInfo();
7725ffd83dbSDimitry Andric BoolRC = TRI->getBoolRC();
7735ffd83dbSDimitry Andric
7745ffd83dbSDimitry Andric if (ST.isWave32()) {
7755ffd83dbSDimitry Andric AndOpc = AMDGPU::S_AND_B32;
7765ffd83dbSDimitry Andric OrOpc = AMDGPU::S_OR_B32;
7775ffd83dbSDimitry Andric XorOpc = AMDGPU::S_XOR_B32;
7785ffd83dbSDimitry Andric MovTermOpc = AMDGPU::S_MOV_B32_term;
7795ffd83dbSDimitry Andric Andn2TermOpc = AMDGPU::S_ANDN2_B32_term;
7805ffd83dbSDimitry Andric XorTermrOpc = AMDGPU::S_XOR_B32_term;
781e8d8bef9SDimitry Andric OrTermrOpc = AMDGPU::S_OR_B32_term;
7825ffd83dbSDimitry Andric OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B32;
7835ffd83dbSDimitry Andric Exec = AMDGPU::EXEC_LO;
7845ffd83dbSDimitry Andric } else {
7855ffd83dbSDimitry Andric AndOpc = AMDGPU::S_AND_B64;
7865ffd83dbSDimitry Andric OrOpc = AMDGPU::S_OR_B64;
7875ffd83dbSDimitry Andric XorOpc = AMDGPU::S_XOR_B64;
7885ffd83dbSDimitry Andric MovTermOpc = AMDGPU::S_MOV_B64_term;
7895ffd83dbSDimitry Andric Andn2TermOpc = AMDGPU::S_ANDN2_B64_term;
7905ffd83dbSDimitry Andric XorTermrOpc = AMDGPU::S_XOR_B64_term;
791e8d8bef9SDimitry Andric OrTermrOpc = AMDGPU::S_OR_B64_term;
7925ffd83dbSDimitry Andric OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B64;
7935ffd83dbSDimitry Andric Exec = AMDGPU::EXEC;
7945ffd83dbSDimitry Andric }
7955ffd83dbSDimitry Andric
796fe6060f1SDimitry Andric // Compute set of blocks with kills
797fe6060f1SDimitry Andric const bool CanDemote =
798fe6060f1SDimitry Andric MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS;
799fe6060f1SDimitry Andric for (auto &MBB : MF) {
800fe6060f1SDimitry Andric bool IsKillBlock = false;
801fe6060f1SDimitry Andric for (auto &Term : MBB.terminators()) {
802fe6060f1SDimitry Andric if (TII->isKillTerminator(Term.getOpcode())) {
803fe6060f1SDimitry Andric KillBlocks.insert(&MBB);
804fe6060f1SDimitry Andric IsKillBlock = true;
805fe6060f1SDimitry Andric break;
806fe6060f1SDimitry Andric }
807fe6060f1SDimitry Andric }
808fe6060f1SDimitry Andric if (CanDemote && !IsKillBlock) {
809fe6060f1SDimitry Andric for (auto &MI : MBB) {
810fe6060f1SDimitry Andric if (MI.getOpcode() == AMDGPU::SI_DEMOTE_I1) {
811fe6060f1SDimitry Andric KillBlocks.insert(&MBB);
812fe6060f1SDimitry Andric break;
813fe6060f1SDimitry Andric }
814fe6060f1SDimitry Andric }
815fe6060f1SDimitry Andric }
816fe6060f1SDimitry Andric }
8175ffd83dbSDimitry Andric
81881ad6265SDimitry Andric bool Changed = false;
8195ffd83dbSDimitry Andric MachineFunction::iterator NextBB;
820e8d8bef9SDimitry Andric for (MachineFunction::iterator BI = MF.begin();
821e8d8bef9SDimitry Andric BI != MF.end(); BI = NextBB) {
8225ffd83dbSDimitry Andric NextBB = std::next(BI);
823e8d8bef9SDimitry Andric MachineBasicBlock *MBB = &*BI;
8245ffd83dbSDimitry Andric
825e8d8bef9SDimitry Andric MachineBasicBlock::iterator I, E, Next;
826e8d8bef9SDimitry Andric E = MBB->end();
827e8d8bef9SDimitry Andric for (I = MBB->begin(); I != E; I = Next) {
8285ffd83dbSDimitry Andric Next = std::next(I);
8295ffd83dbSDimitry Andric MachineInstr &MI = *I;
830e8d8bef9SDimitry Andric MachineBasicBlock *SplitMBB = MBB;
8315ffd83dbSDimitry Andric
8325ffd83dbSDimitry Andric switch (MI.getOpcode()) {
8335ffd83dbSDimitry Andric case AMDGPU::SI_IF:
8345ffd83dbSDimitry Andric case AMDGPU::SI_ELSE:
8355ffd83dbSDimitry Andric case AMDGPU::SI_IF_BREAK:
836fe6060f1SDimitry Andric case AMDGPU::SI_WATERFALL_LOOP:
8375ffd83dbSDimitry Andric case AMDGPU::SI_LOOP:
8385ffd83dbSDimitry Andric case AMDGPU::SI_END_CF:
839e8d8bef9SDimitry Andric SplitMBB = process(MI);
84081ad6265SDimitry Andric Changed = true;
841e8d8bef9SDimitry Andric break;
8425ffd83dbSDimitry Andric }
843e8d8bef9SDimitry Andric
844e8d8bef9SDimitry Andric if (SplitMBB != MBB) {
845e8d8bef9SDimitry Andric MBB = Next->getParent();
846e8d8bef9SDimitry Andric E = MBB->end();
847e8d8bef9SDimitry Andric }
8485ffd83dbSDimitry Andric }
8490b57cec5SDimitry Andric }
8500b57cec5SDimitry Andric
8515ffd83dbSDimitry Andric optimizeEndCf();
8525ffd83dbSDimitry Andric
8535f757f3fSDimitry Andric if (LIS) {
8545f757f3fSDimitry Andric for (Register Reg : RecomputeRegs) {
8555f757f3fSDimitry Andric LIS->removeInterval(Reg);
8565f757f3fSDimitry Andric LIS->createAndComputeVirtRegInterval(Reg);
8575f757f3fSDimitry Andric }
8585f757f3fSDimitry Andric }
8595f757f3fSDimitry Andric
8605f757f3fSDimitry Andric RecomputeRegs.clear();
8615ffd83dbSDimitry Andric LoweredEndCf.clear();
8625ffd83dbSDimitry Andric LoweredIf.clear();
863fe6060f1SDimitry Andric KillBlocks.clear();
8640b57cec5SDimitry Andric
86581ad6265SDimitry Andric return Changed;
8660b57cec5SDimitry Andric }
867