32 #define DEBUG_TYPE "si-optimize-exec-masking-pre-ra" 60 return "SI optimize exec mask operations pre-RA";
73 "SI optimize exec mask operations pre-RA",
false,
false)
78 char SIOptimizeExecMaskingPreRA::
ID = 0;
83 return new SIOptimizeExecMaskingPreRA();
89 return MI.
getOpcode() == AMDGPU::S_OR_B32 &&
93 return MI.
getOpcode() == AMDGPU::S_OR_B64 &&
98 unsigned Exec = ST.
isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
111 unsigned Exec = ST.
isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
113 if (
Op->isReg() &&
Op->getReg() != Exec)
116 if (
Op->isReg() &&
Op->getReg() != Exec)
118 return AMDGPU::NoRegister;
126 if (SavedExec == AMDGPU::NoRegister)
137 for ( ; I !=
E; ++
I) {
138 if (
TII->mayReadEXEC(*
MRI, *I))
149 SIOptimizeExecMaskingPreRA::skipIgnoreExecInstsTrivialSucc(
154 It = skipIgnoreExecInsts(It, MBB->
end());
196 const unsigned AndOpc = Wave32 ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
197 const unsigned Andn2Opc = Wave32 ? AMDGPU::S_ANDN2_B32 : AMDGPU::S_ANDN2_B64;
198 const unsigned CondReg = Wave32 ? AMDGPU::VCC_LO : AMDGPU::VCC;
199 const unsigned ExecReg = Wave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
202 unsigned Opc =
MI.getOpcode();
203 return Opc == AMDGPU::S_CBRANCH_VCCZ ||
204 Opc == AMDGPU::S_CBRANCH_VCCNZ; });
206 return AMDGPU::NoRegister;
210 if (!And || And->getOpcode() != AndOpc ||
211 !And->getOperand(1).isReg() || !And->getOperand(2).isReg())
212 return AMDGPU::NoRegister;
217 if (CmpReg == ExecReg) {
218 AndCC = &And->getOperand(2);
221 }
else if (And->getOperand(2).getReg() != ExecReg) {
222 return AMDGPU::NoRegister;
226 if (!Cmp || !(Cmp->getOpcode() == AMDGPU::V_CMP_NE_U32_e32 ||
227 Cmp->getOpcode() == AMDGPU::V_CMP_NE_U32_e64) ||
228 Cmp->getParent() != And->getParent())
229 return AMDGPU::NoRegister;
236 return AMDGPU::NoRegister;
240 if (!Sel || Sel->getOpcode() != AMDGPU::V_CNDMASK_B32_e64)
241 return AMDGPU::NoRegister;
245 return AMDGPU::NoRegister;
252 return AMDGPU::NoRegister;
254 LLVM_DEBUG(
dbgs() <<
"Folding sequence:\n\t" << *Sel <<
'\t' << *Cmp <<
'\t' 260 BuildMI(MBB, *And, And->getDebugLoc(), TII->get(Andn2Opc),
261 And->getOperand(0).getReg())
264 And->eraseFromParent();
272 (CmpReg == CondReg &&
275 return MI.readsRegister(CondReg, TRI);
280 Cmp->eraseFromParent();
287 Sel->eraseFromParent();
294 bool SIOptimizeExecMaskingPreRA::runOnMachineFunction(
MachineFunction &MF) {
306 unsigned Exec = ST.
isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
307 bool Changed =
false;
312 RecalcRegs.insert(
Reg);
313 RecalcRegs.insert(AMDGPU::VCC_LO);
314 RecalcRegs.insert(AMDGPU::VCC_HI);
315 RecalcRegs.insert(AMDGPU::SCC);
334 while (!Blocks.empty()) {
336 auto I = CurBB->rbegin(), E = CurBB->rend();
338 if (I->isUnconditionalBranch() || I->getOpcode() == AMDGPU::S_ENDPGM)
340 else if (I->isBranch())
345 if (I->isDebugInstr()) {
350 if (I->mayStore() || I->isBarrier() || I->isCall() ||
351 I->hasUnmodeledSideEffects() || I->hasOrderedMemoryRef())
355 <<
"Removing no effect instruction: " << *I <<
'\n');
357 for (
auto &
Op : I->operands()) {
359 RecalcRegs.insert(
Op.getReg());
362 auto Next = std::next(I);
364 I->eraseFromParent();
374 for (
auto *Pred : CurBB->predecessors()) {
375 if (Pred->succ_size() == 1)
376 Blocks.push_back(Pred);
389 auto NextLead = skipIgnoreExecInstsTrivialSucc(TmpMBB, std::next(Lead));
390 if (NextLead == TmpMBB->
end() || !
isEndCF(*NextLead, TRI, ST) ||
394 LLVM_DEBUG(
dbgs() <<
"Redundant EXEC = S_OR_B64 found: " << *Lead <<
'\n');
398 for (
auto &
Op : Lead->operands()) {
400 RecalcRegs.insert(
Op.getReg());
404 Lead->eraseFromParent();
414 if (!SaveExec || !SaveExec->isFullCopy())
417 Register SavedExec = SaveExec->getOperand(0).getReg();
418 bool SafeToReplace =
true;
420 if (U.getParent() != SaveExec->getParent()) {
421 SafeToReplace =
false;
425 LLVM_DEBUG(
dbgs() <<
"Redundant EXEC COPY: " << *SaveExec <<
'\n');
430 SaveExec->eraseFromParent();
437 for (
auto Reg : RecalcRegs) {
LiveInterval & createAndComputeVirtRegInterval(Register Reg)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AMDGPU specific subclass of TargetSubtarget.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
This class represents lattice values for constants.
bool hasModifiersSet(const MachineInstr &MI, unsigned OpName) const
unsigned getSubReg() const
static MachineInstr * getOrExecSource(const MachineInstr &MI, const SIInstrInfo &TII, const MachineRegisterInfo &MRI, const GCNSubtarget &ST)
const SIInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
SI optimize exec mask operations pre RA
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineInstr * findReachingDef(unsigned Reg, unsigned SubReg, MachineInstr &Use, MachineRegisterInfo &MRI, LiveIntervals *LIS) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
iterator_range< iterator > terminators()
char & SIOptimizeExecMaskingPreRAID
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
INITIALIZE_PASS_BEGIN(SIOptimizeExecMaskingPreRA, DEBUG_TYPE, "SI optimize exec mask operations pre-RA", false, false) INITIALIZE_PASS_END(SIOptimizeExecMaskingPreRA
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
void initializeSIOptimizeExecMaskingPreRAPass(PassRegistry &)
SI optimize exec mask operations
unsigned getUndefRegState(bool B)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createSIOptimizeExecMaskingPreRAPass()
unsigned const MachineRegisterInfo * MRI
static bool isFullExecCopy(const MachineInstr &MI, const GCNSubtarget &ST)
void removeAllRegUnitsForPhysReg(unsigned Reg)
Remove associated live ranges for the register units associated with Reg.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void removeInterval(unsigned Reg)
Interval removal.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
static bool isEndCF(const MachineInstr &MI, const SIRegisterInfo *TRI, const GCNSubtarget &ST)
self_iterator getIterator()
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
succ_iterator succ_begin()
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
static unsigned optimizeVcndVcmpPair(MachineBasicBlock &MBB, const GCNSubtarget &ST, MachineRegisterInfo &MRI, LiveIntervals *LIS)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register...
LLVM_NODISCARD T pop_back_val()
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
void setPreservesAll()
Set by analyses that do not transform their input at all.
static unsigned getOrNonExecReg(const MachineInstr &MI, const SIInstrInfo &TII, const GCNSubtarget &ST)
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
unsigned succ_size() const
IterT skipDebugInstructionsForward(IterT It, IterT End)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator...
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
bool reg_empty(unsigned RegNo) const
reg_empty - Return true if there are no instructions using or defining the specified register (it may...
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(unsigned Reg) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.
const SIRegisterInfo * getRegisterInfo() const override