|
| 1 | +//===-- SIRemoveShortExecBranches.cpp ------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +/// \file |
| 10 | +/// This pass optmizes the s_cbranch_execz instructions. |
| 11 | +/// The pass removes this skip instruction for short branches, |
| 12 | +/// if there is no unwanted sideeffect in the fallthrough code sequence. |
| 13 | +/// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "AMDGPU.h" |
| 17 | +#include "AMDGPUSubtarget.h" |
| 18 | +#include "MCTargetDesc/AMDGPUMCTargetDesc.h" |
| 19 | +#include "SIInstrInfo.h" |
| 20 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | +#include "llvm/Support/CommandLine.h" |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | + |
| 25 | +#define DEBUG_TYPE "si-remove-short-exec-branches" |
| 26 | + |
| 27 | +static unsigned SkipThreshold; |
| 28 | + |
| 29 | +static cl::opt<unsigned, true> SkipThresholdFlag( |
| 30 | + "amdgpu-skip-threshold", cl::Hidden, |
| 31 | + cl::desc( |
| 32 | + "Number of instructions before jumping over divergent control flow"), |
| 33 | + cl::location(SkipThreshold), cl::init(12)); |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +class SIRemoveShortExecBranches : public MachineFunctionPass { |
| 38 | +private: |
| 39 | + const SIInstrInfo *TII = nullptr; |
| 40 | + bool getBlockDestinations(MachineBasicBlock &SrcMBB, |
| 41 | + MachineBasicBlock *&TrueMBB, |
| 42 | + MachineBasicBlock *&FalseMBB, |
| 43 | + SmallVectorImpl<MachineOperand> &Cond); |
| 44 | + bool mustRetainExeczBranch(const MachineBasicBlock &From, |
| 45 | + const MachineBasicBlock &To) const; |
| 46 | + bool removeExeczBranch(MachineInstr &MI, MachineBasicBlock &SrcMBB); |
| 47 | + |
| 48 | +public: |
| 49 | + static char ID; |
| 50 | + |
| 51 | + SIRemoveShortExecBranches() : MachineFunctionPass(ID) { |
| 52 | + initializeSIRemoveShortExecBranchesPass(*PassRegistry::getPassRegistry()); |
| 53 | + } |
| 54 | + |
| 55 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 56 | +}; |
| 57 | + |
| 58 | +} // End anonymous namespace. |
| 59 | + |
| 60 | +INITIALIZE_PASS(SIRemoveShortExecBranches, DEBUG_TYPE, |
| 61 | + "SI remove short exec branches", false, false) |
| 62 | + |
| 63 | +char SIRemoveShortExecBranches::ID = 0; |
| 64 | + |
| 65 | +char &llvm::SIRemoveShortExecBranchesID = SIRemoveShortExecBranches::ID; |
| 66 | + |
| 67 | +bool SIRemoveShortExecBranches::getBlockDestinations( |
| 68 | + MachineBasicBlock &SrcMBB, MachineBasicBlock *&TrueMBB, |
| 69 | + MachineBasicBlock *&FalseMBB, SmallVectorImpl<MachineOperand> &Cond) { |
| 70 | + if (TII->analyzeBranch(SrcMBB, TrueMBB, FalseMBB, Cond)) |
| 71 | + return false; |
| 72 | + |
| 73 | + if (!FalseMBB) |
| 74 | + FalseMBB = SrcMBB.getNextNode(); |
| 75 | + |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +bool SIRemoveShortExecBranches::mustRetainExeczBranch( |
| 80 | + const MachineBasicBlock &From, const MachineBasicBlock &To) const { |
| 81 | + unsigned NumInstr = 0; |
| 82 | + const MachineFunction *MF = From.getParent(); |
| 83 | + |
| 84 | + for (MachineFunction::const_iterator MBBI(&From), ToI(&To), End = MF->end(); |
| 85 | + MBBI != End && MBBI != ToI; ++MBBI) { |
| 86 | + const MachineBasicBlock &MBB = *MBBI; |
| 87 | + |
| 88 | + for (MachineBasicBlock::const_iterator I = MBB.begin(), E = MBB.end(); |
| 89 | + I != E; ++I) { |
| 90 | + // When a uniform loop is inside non-uniform control flow, the branch |
| 91 | + // leaving the loop might be an S_CBRANCH_VCCNZ, which is never taken |
| 92 | + // when EXEC = 0. We should skip the loop lest it becomes infinite. |
| 93 | + if (I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ || |
| 94 | + I->getOpcode() == AMDGPU::S_CBRANCH_VCCZ) |
| 95 | + return true; |
| 96 | + |
| 97 | + if (TII->hasUnwantedEffectsWhenEXECEmpty(*I)) |
| 98 | + return true; |
| 99 | + |
| 100 | + // These instructions are potentially expensive even if EXEC = 0. |
| 101 | + if (TII->isSMRD(*I) || TII->isVMEM(*I) || TII->isFLAT(*I) || |
| 102 | + I->getOpcode() == AMDGPU::S_WAITCNT) |
| 103 | + return true; |
| 104 | + |
| 105 | + ++NumInstr; |
| 106 | + if (NumInstr >= SkipThreshold) |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return false; |
| 112 | +} |
| 113 | + |
| 114 | +// Returns true if the skip branch instruction is removed. |
| 115 | +bool SIRemoveShortExecBranches::removeExeczBranch(MachineInstr &MI, |
| 116 | + MachineBasicBlock &SrcMBB) { |
| 117 | + MachineBasicBlock *TrueMBB = nullptr; |
| 118 | + MachineBasicBlock *FalseMBB = nullptr; |
| 119 | + SmallVector<MachineOperand, 1> Cond; |
| 120 | + |
| 121 | + if (!getBlockDestinations(SrcMBB, TrueMBB, FalseMBB, Cond)) |
| 122 | + return false; |
| 123 | + |
| 124 | + // Consider only the forward branches. |
| 125 | + if ((SrcMBB.getNumber() >= TrueMBB->getNumber()) || |
| 126 | + mustRetainExeczBranch(*FalseMBB, *TrueMBB)) |
| 127 | + return false; |
| 128 | + |
| 129 | + LLVM_DEBUG(dbgs() << "Removing the execz branch: " << MI); |
| 130 | + MI.eraseFromParent(); |
| 131 | + SrcMBB.removeSuccessor(TrueMBB); |
| 132 | + |
| 133 | + return true; |
| 134 | +} |
| 135 | + |
| 136 | +bool SIRemoveShortExecBranches::runOnMachineFunction(MachineFunction &MF) { |
| 137 | + const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); |
| 138 | + TII = ST.getInstrInfo(); |
| 139 | + MF.RenumberBlocks(); |
| 140 | + bool Changed = false; |
| 141 | + |
| 142 | + for (MachineBasicBlock &MBB : MF) { |
| 143 | + MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(); |
| 144 | + if (MBBI == MBB.end()) |
| 145 | + continue; |
| 146 | + |
| 147 | + MachineInstr &MI = *MBBI; |
| 148 | + switch (MI.getOpcode()) { |
| 149 | + case AMDGPU::S_CBRANCH_EXECZ: |
| 150 | + Changed = removeExeczBranch(MI, MBB); |
| 151 | + break; |
| 152 | + default: |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return Changed; |
| 158 | +} |
0 commit comments