23#define DEBUG_TYPE "si-optimize-exec-masking"
27class SIOptimizeExecMasking {
57 bool IgnoreStart =
false)
const;
64 unsigned MaxInstructions = 20)
const;
65 bool optimizeExecSequence();
67 bool optimizeVCMPSaveExecSequence(
MachineInstr &SaveExecInstr,
71 bool optimizeOrSaveexecXorSequences();
83 return "SI optimize exec mask operations";
97 SIOptimizeExecMasking Impl(&MF);
108 "SI optimize exec mask operations",
false,
false)
113char SIOptimizeExecMaskingLegacy::ID = 0;
119 switch (
MI.getOpcode()) {
121 case AMDGPU::S_MOV_B64:
122 case AMDGPU::S_MOV_B64_term:
123 case AMDGPU::S_MOV_B32:
124 case AMDGPU::S_MOV_B32_term: {
125 const MachineOperand &Src = MI.getOperand(1);
126 if (Src.isReg() && Src.getReg() == LMC.ExecReg)
127 return MI.getOperand(0).getReg();
131 return AMDGPU::NoRegister;
136 switch (
MI.getOpcode()) {
138 case AMDGPU::S_MOV_B64:
139 case AMDGPU::S_MOV_B32: {
141 if (Dst.isReg() && Dst.getReg() == LMC.ExecReg &&
MI.getOperand(1).isReg())
142 return MI.getOperand(1).getReg();
145 case AMDGPU::S_MOV_B64_term:
146 case AMDGPU::S_MOV_B32_term:
156 switch (
MI.getOpcode()) {
157 case AMDGPU::S_AND_B64:
158 case AMDGPU::S_OR_B64:
159 case AMDGPU::S_XOR_B64:
160 case AMDGPU::S_ANDN2_B64:
161 case AMDGPU::S_ORN2_B64:
162 case AMDGPU::S_NAND_B64:
163 case AMDGPU::S_NOR_B64:
164 case AMDGPU::S_XNOR_B64: {
167 return MI.getOperand(0).getReg();
170 return MI.getOperand(0).getReg();
173 case AMDGPU::S_AND_B32:
174 case AMDGPU::S_OR_B32:
175 case AMDGPU::S_XOR_B32:
176 case AMDGPU::S_ANDN2_B32:
177 case AMDGPU::S_ORN2_B32:
178 case AMDGPU::S_NAND_B32:
179 case AMDGPU::S_NOR_B32:
180 case AMDGPU::S_XNOR_B32: {
182 if (Src1.
isReg() && Src1.
getReg() == AMDGPU::EXEC_LO)
183 return MI.getOperand(0).getReg();
185 if (Src2.
isReg() && Src2.
getReg() == AMDGPU::EXEC_LO)
186 return MI.getOperand(0).getReg();
191 return AMDGPU::NoRegister;
196 case AMDGPU::S_AND_B64:
197 return AMDGPU::S_AND_SAVEEXEC_B64;
198 case AMDGPU::S_OR_B64:
199 return AMDGPU::S_OR_SAVEEXEC_B64;
200 case AMDGPU::S_XOR_B64:
201 return AMDGPU::S_XOR_SAVEEXEC_B64;
202 case AMDGPU::S_ANDN2_B64:
203 return AMDGPU::S_ANDN2_SAVEEXEC_B64;
204 case AMDGPU::S_ORN2_B64:
205 return AMDGPU::S_ORN2_SAVEEXEC_B64;
206 case AMDGPU::S_NAND_B64:
207 return AMDGPU::S_NAND_SAVEEXEC_B64;
208 case AMDGPU::S_NOR_B64:
209 return AMDGPU::S_NOR_SAVEEXEC_B64;
210 case AMDGPU::S_XNOR_B64:
211 return AMDGPU::S_XNOR_SAVEEXEC_B64;
212 case AMDGPU::S_AND_B32:
213 return AMDGPU::S_AND_SAVEEXEC_B32;
214 case AMDGPU::S_OR_B32:
215 return AMDGPU::S_OR_SAVEEXEC_B32;
216 case AMDGPU::S_XOR_B32:
217 return AMDGPU::S_XOR_SAVEEXEC_B32;
218 case AMDGPU::S_ANDN2_B32:
219 return AMDGPU::S_ANDN2_SAVEEXEC_B32;
220 case AMDGPU::S_ORN2_B32:
221 return AMDGPU::S_ORN2_SAVEEXEC_B32;
222 case AMDGPU::S_NAND_B32:
223 return AMDGPU::S_NAND_SAVEEXEC_B32;
224 case AMDGPU::S_NOR_B32:
225 return AMDGPU::S_NOR_SAVEEXEC_B32;
226 case AMDGPU::S_XNOR_B32:
227 return AMDGPU::S_XNOR_SAVEEXEC_B32;
229 return AMDGPU::INSTRUCTION_LIST_END;
235bool SIOptimizeExecMasking::removeTerminatorBit(
MachineInstr &
MI)
const {
236 switch (
MI.getOpcode()) {
237 case AMDGPU::S_MOV_B32_term: {
238 bool RegSrc =
MI.getOperand(1).isReg();
239 MI.setDesc(
TII->get(RegSrc ? AMDGPU::COPY : AMDGPU::S_MOV_B32));
242 case AMDGPU::S_MOV_B64_term: {
243 bool RegSrc =
MI.getOperand(1).isReg();
244 MI.setDesc(
TII->get(RegSrc ? AMDGPU::COPY : AMDGPU::S_MOV_B64));
247 case AMDGPU::S_XOR_B64_term: {
250 MI.setDesc(
TII->get(AMDGPU::S_XOR_B64));
253 case AMDGPU::S_XOR_B32_term: {
256 MI.setDesc(
TII->get(AMDGPU::S_XOR_B32));
259 case AMDGPU::S_OR_B64_term: {
262 MI.setDesc(
TII->get(AMDGPU::S_OR_B64));
265 case AMDGPU::S_OR_B32_term: {
268 MI.setDesc(
TII->get(AMDGPU::S_OR_B32));
271 case AMDGPU::S_ANDN2_B64_term: {
274 MI.setDesc(
TII->get(AMDGPU::S_ANDN2_B64));
277 case AMDGPU::S_ANDN2_B32_term: {
280 MI.setDesc(
TII->get(AMDGPU::S_ANDN2_B32));
283 case AMDGPU::S_AND_B64_term: {
286 MI.setDesc(
TII->get(AMDGPU::S_AND_B64));
289 case AMDGPU::S_AND_B32_term: {
292 MI.setDesc(
TII->get(AMDGPU::S_AND_B32));
295 case AMDGPU::V_CMPX_EQ_U32_nosdst_e32_term:
296 MI.setDesc(
TII->get(AMDGPU::V_CMPX_EQ_U32_nosdst_e32));
298 case AMDGPU::V_CMPX_EQ_U64_nosdst_e32_term:
299 MI.setDesc(
TII->get(AMDGPU::V_CMPX_EQ_U64_nosdst_e32));
315 for (;
I !=
E; ++
I) {
316 if (!
I->isTerminator())
317 return Seen ? FirstNonTerm :
I;
319 if (removeTerminatorBit(*
I)) {
332 const unsigned InstLimit = 25;
335 for (
unsigned N = 0;
N <= InstLimit &&
I !=
E; ++
I, ++
N) {
336 Register CopyFromExec = isCopyFromExec(*
I);
349 if (Succ->isLiveIn(
Reg))
365 unsigned MaxInstructions)
const {
368 unsigned CurrentIteration = 0;
370 for (++
A; CurrentIteration < MaxInstructions &&
A !=
E; ++
A) {
371 if (
A->isDebugInstr())
378 if (
A->modifiesRegister(
Reg,
TRI))
385 if (Terminator && KillFlagCandidates &&
A != Terminator &&
388 if (MO.isReg() && MO.isKill()) {
390 if (Candidate !=
Reg &&
TRI->regsOverlap(Candidate,
Reg))
409bool SIOptimizeExecMasking::isRegisterInUseBetween(
MachineInstr &Stop,
413 bool IgnoreStart)
const {
424 if (!
A->isDebugInstr())
428 return !LR.available(
Reg) || MRI->isReserved(
Reg);
433bool SIOptimizeExecMasking::isRegisterInUseAfter(
MachineInstr &Stop,
448bool SIOptimizeExecMasking::optimizeExecSequence() {
460 unsigned SearchCount = 0;
461 const unsigned SearchLimit = 5;
462 while (
I !=
E && SearchCount++ < SearchLimit) {
463 CopyToExec = isCopyToExec(*
I);
473 auto *CopyToExecInst = &*
I;
474 auto CopyFromExecInst = findExecCopy(
MBB,
I);
475 if (CopyFromExecInst ==
E) {
477 if (PrepareExecInst ==
E)
480 if (CopyToExecInst->getOperand(1).isKill() &&
484 PrepareExecInst->getOperand(0).setReg(LMC.ExecReg);
488 CopyToExecInst->eraseFromParent();
501 Register CopyFromExec = CopyFromExecInst->getOperand(0).getReg();
506 J = std::next(CopyFromExecInst->getIterator()),
507 JE =
I->getIterator();
509 if (J->isDebugInstr())
512 if (SaveExecInst && J->readsRegister(LMC.ExecReg,
TRI)) {
513 LLVM_DEBUG(
dbgs() <<
"exec read prevents saveexec: " << *J <<
'\n');
516 SaveExecInst =
nullptr;
520 bool ReadsCopyFromExec = J->readsRegister(CopyFromExec,
TRI);
522 if (J->modifiesRegister(CopyToExec,
TRI)) {
526 SaveExecInst =
nullptr;
531 if (SaveExecOp == AMDGPU::INSTRUCTION_LIST_END)
534 if (ReadsCopyFromExec) {
536 LLVM_DEBUG(
dbgs() <<
"Found save exec op: " << *SaveExecInst <<
'\n');
539 LLVM_DEBUG(
dbgs() <<
"Instruction does not read exec copy: " << *J
543 if (ReadsCopyFromExec && !SaveExecInst) {
552 LLVM_DEBUG(
dbgs() <<
"Found second use of save inst candidate: " << *J
557 if (SaveExecInst && J->readsRegister(CopyToExec,
TRI)) {
558 assert(SaveExecInst != &*J);
566 LLVM_DEBUG(
dbgs() <<
"Insert save exec op: " << *SaveExecInst <<
'\n');
573 if (Src0.
isReg() && Src0.
getReg() == CopyFromExec) {
575 }
else if (Src1.
isReg() && Src1.
getReg() == CopyFromExec) {
583 CopyFromExecInst->eraseFromParent();
593 CopyToExecInst->eraseFromParent();
596 OtherInst->substituteRegister(CopyToExec, LMC.ExecReg,
597 AMDGPU::NoSubRegister, *
TRI);
608bool SIOptimizeExecMasking::optimizeVCMPSaveExecSequence(
622 bool IsSGPR32 =
TRI->getRegSizeInBits(MoveDest, *MRI) == 32;
623 unsigned MovOpcode = IsSGPR32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
634 auto TryAddImmediateValueFromNamedOperand =
635 [&](AMDGPU::OpName OperandName) ->
void {
636 if (
auto *
Mod =
TII->getNamedOperand(VCmp, OperandName))
637 Builder.addImm(
Mod->getImm());
640 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::src0_modifiers);
643 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::src1_modifiers);
646 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::clamp);
648 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::op_sel);
652 MRI->clearKillFlags(Src0->
getReg());
654 MRI->clearKillFlags(Src1->
getReg());
657 MO->setIsKill(
false);
672void SIOptimizeExecMasking::tryRecordVCmpxAndSaveexecSequence(
674 if (!
ST->hasGFX10_3Insts())
677 if (
MI.getOpcode() != LMC.AndSaveExecOpc)
680 Register SaveExecDest =
MI.getOperand(0).getReg();
681 if (!
TRI->isSGPRReg(*MRI, SaveExecDest))
685 if (!SaveExecSrc0->
isReg())
697 VCmp = findInstrBackwards(
703 {LMC.ExecReg, SaveExecSrc0->
getReg()});
709 assert(VCmpDest &&
"Should have an sdst operand!");
731 if (isRegisterInUseBetween(*VCmp,
MI, VCmpDest->
getReg(),
false,
true) ||
732 isRegisterInUseAfter(
MI, VCmpDest->
getReg()))
746 if (!findInstrBackwards(
748 VCmp, &KillFlagCandidates))
752 SaveExecVCmpMapping[&
MI] = VCmp;
760void SIOptimizeExecMasking::tryRecordOrSaveexecXorSequence(
MachineInstr &
MI) {
761 if (
MI.getOpcode() == LMC.XorOpc && &
MI != &
MI.getParent()->front()) {
768 (XorSrc0.
getReg() == LMC.ExecReg || XorSrc1.
getReg() == LMC.ExecReg)) {
772 auto It =
prev_nodbg(
MI.getIterator(),
MI.getParent()->instr_begin());
774 if (PossibleOrSaveexec.
getOpcode() != LMC.OrSaveExecOpc)
780 if ((XorSrc0.
getReg() == LMC.ExecReg &&
783 XorSrc1.
getReg() == LMC.ExecReg)) {
784 OrXors.emplace_back(&PossibleOrSaveexec, &
MI);
791bool SIOptimizeExecMasking::optimizeOrSaveexecXorSequences() {
792 if (OrXors.empty()) {
798 for (
const auto &Pair : OrXors) {
801 std::tie(
Or,
Xor) = Pair;
802 BuildMI(*
Or->getParent(),
Or->getIterator(),
Or->getDebugLoc(),
803 TII->get(LMC.AndN2SaveExecOpc),
Or->getOperand(0).getReg())
804 .
addReg(
Or->getOperand(1).getReg());
806 Or->eraseFromParent();
807 Xor->eraseFromParent();
815bool SIOptimizeExecMaskingLegacy::runOnMachineFunction(
MachineFunction &MF) {
819 return SIOptimizeExecMasking(&MF).run();
822bool SIOptimizeExecMasking::run() {
823 bool Changed = optimizeExecSequence();
826 SaveExecVCmpMapping.clear();
827 KillFlagCandidates.clear();
828 static unsigned SearchWindow = 10;
830 unsigned SearchCount = 0;
833 if (
MI.isDebugInstr())
836 if (SearchCount >= SearchWindow) {
840 tryRecordOrSaveexecXorSequence(
MI);
841 tryRecordVCmpxAndSaveexecSequence(
MI);
843 if (
MI.modifiesRegister(LMC.ExecReg,
TRI)) {
851 Changed |= optimizeOrSaveexecXorSequences();
852 for (
const auto &Entry : SaveExecVCmpMapping) {
856 Changed |= optimizeVCMPSaveExecSequence(*SaveExecInstr, *VCmpInstr);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
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)
static unsigned getSaveExecOp(unsigned Opc)
static Register isLogicalOpOnExec(const MachineInstr &MI)
If MI is a logical operation on an exec value, return the register copied to.
static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg)
Interface definition for SIRegisterInfo.
This file defines the SmallVector class.
static const LaneMaskConstants & get(const GCNSubtarget &ST)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents analyses that only rely on functions' control flow.
A set of register units used to track register liveness.
Wrapper class representing physical registers. Should be passed by value.
Instructions::iterator instr_iterator
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
reverse_iterator rbegin()
MachineInstrBundleIterator< MachineInstr > iterator
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
bool isCommutable(QueryType Type=IgnoreBundle) const
Return true if this may be a 2- or 3-address instruction (of the form "X = op Y, Z,...
mop_range uses()
Returns all operands which may be register uses.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
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.
reverse_self_iterator getReverseIterator()
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY int32_t getVCMPXOpFromVCMP(uint32_t Opcode)
std::reverse_iterator< iterator > rend() const
This is an optimization pass for GlobalISel generic memory operations.
IterT next_nodbg(IterT It, IterT End, bool SkipPseudoOp=true)
Increment It, then continue incrementing it while it points to a debug instruction.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
char & SIOptimizeExecMaskingLegacyID
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
@ Mod
The access may modify the value stored in memory.
@ Xor
Bitwise or logical XOR of integers.
IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It, then continue decrementing it while it points to a debug instruction.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.