25#define DEBUG_TYPE "si-optimize-exec-masking-pre-ra"
29class SIOptimizeExecMaskingPreRA {
62 return "SI optimize exec mask operations pre-RA";
75 "SI optimize exec mask operations pre-RA",
false,
false)
80char SIOptimizeExecMaskingPreRALegacy::ID = 0;
89 return (!AndLRQ.
isKill() && AndLRQ.
valueIn() != LR.Query(SelIdx).valueOut());
102 for (MCRegUnit Unit :
TRI.regunits(
Reg.asMCReg())) {
110void SIOptimizeExecMaskingPreRA::removePhysRegDefs(
const MachineInstr &
MI,
112 for (
const MachineOperand &
Op :
MI.all_defs()) {
113 if (
Op.getReg().isPhysical()) {
115 Idx.getRegSlot(
Op.isEarlyClobber()));
136bool SIOptimizeExecMaskingPreRA::optimizeVcndVcmpPair(MachineBasicBlock &
MBB) {
138 unsigned Opc = MI.getOpcode();
139 return Opc == AMDGPU::S_CBRANCH_VCCZ ||
140 Opc == AMDGPU::S_CBRANCH_VCCNZ; });
145 TRI->findReachingDef(CondReg, AMDGPU::NoSubRegister, *
I, *
MRI, LIS);
146 if (!
And ||
And->getOpcode() != LMC.
AndOpc || !
And->getOperand(1).isReg() ||
147 !
And->getOperand(2).isReg())
150 MachineOperand *AndCC = &
And->getOperand(1);
154 AndCC = &
And->getOperand(2);
157 }
else if (
And->getOperand(2).getReg() !=
Register(ExecReg)) {
161 auto *
Cmp =
TRI->findReachingDef(CmpReg, CmpSubReg, *
And, *
MRI, LIS);
162 if (!Cmp || !(
Cmp->getOpcode() == AMDGPU::V_CMP_NE_U32_e32 ||
163 Cmp->getOpcode() == AMDGPU::V_CMP_NE_U32_e64) ||
164 Cmp->getParent() !=
And->getParent())
167 MachineOperand *Op1 =
TII->getNamedOperand(*Cmp, AMDGPU::OpName::src0);
168 MachineOperand *Op2 =
TII->getNamedOperand(*Cmp, AMDGPU::OpName::src1);
178 auto *Sel =
TRI->findReachingDef(SelReg, Op1->
getSubReg(), *Cmp, *
MRI, LIS);
179 if (!Sel || Sel->getOpcode() != AMDGPU::V_CNDMASK_B32_e64)
182 if (
TII->hasModifiersSet(*Sel, AMDGPU::OpName::src0_modifiers) ||
183 TII->hasModifiersSet(*Sel, AMDGPU::OpName::src1_modifiers))
186 Op1 =
TII->getNamedOperand(*Sel, AMDGPU::OpName::src0);
187 Op2 =
TII->getNamedOperand(*Sel, AMDGPU::OpName::src1);
188 MachineOperand *
CC =
TII->getNamedOperand(*Sel, AMDGPU::OpName::src2);
205 [](
const VNInfo *VNI) {
206 return VNI->isPHIDef();
211 LLVM_DEBUG(
dbgs() <<
"Folding sequence:\n\t" << *Sel <<
'\t' << *Cmp <<
'\t'
214 MachineInstr *Andn2 =
216 And->getOperand(0).getReg())
221 MachineOperand &Andn2SCC = Andn2->
getOperand(3);
226 And->eraseFromParent();
249 [&](
const MachineInstr &
MI) {
250 return MI.readsRegister(CondReg, TRI);
255 removePhysRegDefs(*Cmp, CmpIdx);
257 Cmp->eraseFromParent();
269 bool ShrinkSel = Sel->getOperand(0).readsReg();
270 Sel->eraseFromParent();
296bool SIOptimizeExecMaskingPreRA::optimizeElseBranch(MachineBasicBlock &
MBB) {
302 MachineInstr &SaveExecMI = *
First;
307 return MI.getOpcode() == LMC.XorTermOpc;
312 MachineInstr &XorTermMI = *
I;
320 MachineInstr *AndExecMI =
nullptr;
322 while (
I !=
First && !AndExecMI) {
323 if (
I->getOpcode() == LMC.
AndOpc &&
I->getOperand(0).getReg() == DstReg &&
324 I->getOperand(1).getReg() ==
Register(ExecReg))
337 for (MCRegUnit Unit :
TRI->regunits(ExecReg)) {
339 if (RegUnit.
find(StartIdx) != std::prev(RegUnit.
find(EndIdx)))
349 removePhysRegDefs(*AndExecMI, EndIdx);
362 SIOptimizeExecMaskingPreRA(MF, &LIS).
run(MF);
366bool SIOptimizeExecMaskingPreRALegacy::runOnMachineFunction(
371 auto *LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS();
372 return SIOptimizeExecMaskingPreRA(MF, LIS).run(MF);
387 if (
MBB.succ_empty()) {
396 if (Term.getOpcode() != AMDGPU::S_ENDPGM || Term.getNumOperands() != 1)
401 while (!Blocks.empty()) {
402 auto *CurBB = Blocks.pop_back_val();
403 auto I = CurBB->rbegin(),
E = CurBB->rend();
405 if (
I->isUnconditionalBranch() ||
I->getOpcode() == AMDGPU::S_ENDPGM)
407 else if (
I->isBranch())
412 if (
I->isDebugInstr()) {
417 if (
I->mayStore() ||
I->isBarrier() ||
I->isCall() ||
418 I->hasUnmodeledSideEffects() ||
I->hasOrderedMemoryRef())
422 <<
"Removing no effect instruction: " << *
I <<
'\n');
424 for (
auto &
Op :
I->operands()) {
429 auto Next = std::next(
I);
431 I->eraseFromParent();
441 for (
auto *
Pred : CurBB->predecessors()) {
442 if (
Pred->succ_size() == 1)
443 Blocks.push_back(
Pred);
456 constexpr unsigned ScanThreshold = 10;
457 unsigned ScanCount = 0;
459 I !=
E && ScanCount < ScanThreshold; ++
I) {
460 if (
I->isMetaInstruction())
464 if (!(
I->isFullCopy() &&
I->getOperand(1).getReg() ==
Register(ExecReg)))
467 Register SavedExec =
I->getOperand(0).getReg();
475 static_cast<unsigned>(Idx) <
477 TII->isOperandLegal(*SingleExecUser, Idx, &
I->getOperand(1))) {
481 I->eraseFromParent();
484 RecalcRegs.
erase(SavedExec);
493 for (
auto Reg : RecalcRegs) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
static bool isDefBetween(Register Reg, SlotIndex First, SlotIndex Last, const MachineRegisterInfo *MRI, const LiveIntervals *LIS)
static bool isDefBetween(const LiveRange &LR, SlotIndex AndIdx, SlotIndex SelIdx)
SI Optimize VGPR LiveRange
static const LaneMaskConstants & get(const GCNSubtarget &ST)
const unsigned OrSaveExecOpc
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Implements a dense probed hash-table based set.
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
void removeAllRegUnitsForPhysReg(MCRegister Reg)
Remove associated live ranges for the register units associated with Reg.
SlotIndex getInstructionIndex(const MachineInstr &Instr) const
Returns the base index of the given instruction.
void RemoveMachineInstrFromMaps(MachineInstr &MI)
LiveInterval & getInterval(Register Reg)
void removeInterval(Register Reg)
Interval removal.
LiveRange & getRegUnit(MCRegUnit Unit)
Return the live range for register unit Unit.
LLVM_ABI bool shrinkToUses(LiveInterval *li, SmallVectorImpl< MachineInstr * > *dead=nullptr, LaneBitmask SubRangeMask=LaneBitmask::getAll())
After removing some uses of a register, shrink its live range to just the remaining uses.
LLVM_ABI void removeVRegDefAt(LiveInterval &LI, SlotIndex Pos)
Remove value number and related live segments of LI and its subranges that start at position Pos.
LLVM_ABI void removePhysRegDefAt(MCRegister Reg, SlotIndex Pos)
Remove value numbers and related live segments starting at position Pos that are part of any liverang...
LiveInterval & createAndComputeVirtRegInterval(Register Reg)
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
Result of a LiveRange query.
bool isDeadDef() const
Return true if this instruction has a dead def.
VNInfo * valueIn() const
Return the value that is live-in to the instruction.
bool isKill() const
Return true if the live-in value is killed by this instruction.
This class represents the liveness of a register, stack slot, etc.
iterator_range< vni_iterator > vnis()
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
LLVM_ABI iterator find(SlotIndex Pos)
find - Return an iterator pointing to the first segment that ends after Pos, or end().
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Wrapper class representing physical registers. Should be passed by value.
static MCRegister from(unsigned Val)
Check the provided unsigned value is a valid MCRegister.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< iterator > terminators()
reverse_iterator rbegin()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
LLVM_ABI int findRegisterUseOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isKill=false) const
Returns the operand index that is a use of the specific register or -1 if it is not found.
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const
bool reg_empty(Register RegNo) const
reg_empty - Return true if there are no instructions using or defining the specified register (it may...
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
SlotIndex - An opaque wrapper around machine indexes.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
std::pair< iterator, bool > insert(const ValueT &V)
bool erase(const ValueT &V)
self_iterator getIterator()
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
char & SIOptimizeExecMaskingPreRAID
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ And
Bitwise or logical AND of integers.
DWARFExpression::Operation Op
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
constexpr RegState getUndefRegState(bool B)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.