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 optimizeAndN2WrExecSequence(
MachineInstr &CopyToExecInst,
70 bool optimizeVCMPSaveExecSequence(
MachineInstr &SaveExecInstr,
74 bool optimizeOrSaveexecXorSequences();
86 return "SI optimize exec mask operations";
100 SIOptimizeExecMasking Impl(&MF);
111 "SI optimize exec mask operations",
false,
false)
116char SIOptimizeExecMaskingLegacy::ID = 0;
122 switch (
MI.getOpcode()) {
124 case AMDGPU::S_MOV_B64:
125 case AMDGPU::S_MOV_B64_term:
126 case AMDGPU::S_MOV_B32:
127 case AMDGPU::S_MOV_B32_term: {
128 const MachineOperand &Src = MI.getOperand(1);
129 if (Src.isReg() && Src.getReg() == LMC.ExecReg)
130 return MI.getOperand(0).getReg();
134 return AMDGPU::NoRegister;
139 switch (
MI.getOpcode()) {
141 case AMDGPU::S_MOV_B64:
142 case AMDGPU::S_MOV_B32: {
144 if (Dst.isReg() && Dst.getReg() == LMC.ExecReg &&
MI.getOperand(1).isReg())
145 return MI.getOperand(1).getReg();
148 case AMDGPU::S_MOV_B64_term:
149 case AMDGPU::S_MOV_B32_term:
159 switch (
MI.getOpcode()) {
160 case AMDGPU::S_AND_B64:
161 case AMDGPU::S_OR_B64:
162 case AMDGPU::S_XOR_B64:
163 case AMDGPU::S_ANDN2_B64:
164 case AMDGPU::S_ORN2_B64:
165 case AMDGPU::S_NAND_B64:
166 case AMDGPU::S_NOR_B64:
167 case AMDGPU::S_XNOR_B64: {
170 return MI.getOperand(0).getReg();
173 return MI.getOperand(0).getReg();
176 case AMDGPU::S_AND_B32:
177 case AMDGPU::S_OR_B32:
178 case AMDGPU::S_XOR_B32:
179 case AMDGPU::S_ANDN2_B32:
180 case AMDGPU::S_ORN2_B32:
181 case AMDGPU::S_NAND_B32:
182 case AMDGPU::S_NOR_B32:
183 case AMDGPU::S_XNOR_B32: {
185 if (Src1.
isReg() && Src1.
getReg() == AMDGPU::EXEC_LO)
186 return MI.getOperand(0).getReg();
188 if (Src2.
isReg() && Src2.
getReg() == AMDGPU::EXEC_LO)
189 return MI.getOperand(0).getReg();
194 return AMDGPU::NoRegister;
199 case AMDGPU::S_AND_B64:
200 return AMDGPU::S_AND_SAVEEXEC_B64;
201 case AMDGPU::S_OR_B64:
202 return AMDGPU::S_OR_SAVEEXEC_B64;
203 case AMDGPU::S_XOR_B64:
204 return AMDGPU::S_XOR_SAVEEXEC_B64;
205 case AMDGPU::S_ANDN2_B64:
206 return AMDGPU::S_ANDN2_SAVEEXEC_B64;
207 case AMDGPU::S_ORN2_B64:
208 return AMDGPU::S_ORN2_SAVEEXEC_B64;
209 case AMDGPU::S_NAND_B64:
210 return AMDGPU::S_NAND_SAVEEXEC_B64;
211 case AMDGPU::S_NOR_B64:
212 return AMDGPU::S_NOR_SAVEEXEC_B64;
213 case AMDGPU::S_XNOR_B64:
214 return AMDGPU::S_XNOR_SAVEEXEC_B64;
215 case AMDGPU::S_AND_B32:
216 return AMDGPU::S_AND_SAVEEXEC_B32;
217 case AMDGPU::S_OR_B32:
218 return AMDGPU::S_OR_SAVEEXEC_B32;
219 case AMDGPU::S_XOR_B32:
220 return AMDGPU::S_XOR_SAVEEXEC_B32;
221 case AMDGPU::S_ANDN2_B32:
222 return AMDGPU::S_ANDN2_SAVEEXEC_B32;
223 case AMDGPU::S_ORN2_B32:
224 return AMDGPU::S_ORN2_SAVEEXEC_B32;
225 case AMDGPU::S_NAND_B32:
226 return AMDGPU::S_NAND_SAVEEXEC_B32;
227 case AMDGPU::S_NOR_B32:
228 return AMDGPU::S_NOR_SAVEEXEC_B32;
229 case AMDGPU::S_XNOR_B32:
230 return AMDGPU::S_XNOR_SAVEEXEC_B32;
232 return AMDGPU::INSTRUCTION_LIST_END;
238bool SIOptimizeExecMasking::removeTerminatorBit(
MachineInstr &
MI)
const {
239 switch (
MI.getOpcode()) {
240 case AMDGPU::S_MOV_B32_term: {
241 bool RegSrc =
MI.getOperand(1).isReg();
242 MI.setDesc(
TII->get(RegSrc ? AMDGPU::COPY : AMDGPU::S_MOV_B32));
245 case AMDGPU::S_MOV_B64_term: {
246 bool RegSrc =
MI.getOperand(1).isReg();
247 MI.setDesc(
TII->get(RegSrc ? AMDGPU::COPY : AMDGPU::S_MOV_B64));
250 case AMDGPU::S_XOR_B64_term: {
253 MI.setDesc(
TII->get(AMDGPU::S_XOR_B64));
256 case AMDGPU::S_XOR_B32_term: {
259 MI.setDesc(
TII->get(AMDGPU::S_XOR_B32));
262 case AMDGPU::S_OR_B64_term: {
265 MI.setDesc(
TII->get(AMDGPU::S_OR_B64));
268 case AMDGPU::S_OR_B32_term: {
271 MI.setDesc(
TII->get(AMDGPU::S_OR_B32));
274 case AMDGPU::S_ANDN2_B64_term: {
277 MI.setDesc(
TII->get(AMDGPU::S_ANDN2_B64));
280 case AMDGPU::S_ANDN2_B32_term: {
283 MI.setDesc(
TII->get(AMDGPU::S_ANDN2_B32));
286 case AMDGPU::S_AND_B64_term: {
289 MI.setDesc(
TII->get(AMDGPU::S_AND_B64));
292 case AMDGPU::S_AND_B32_term: {
295 MI.setDesc(
TII->get(AMDGPU::S_AND_B32));
298 case AMDGPU::V_CMPX_EQ_U32_nosdst_e32_term:
299 MI.setDesc(
TII->get(AMDGPU::V_CMPX_EQ_U32_nosdst_e32));
301 case AMDGPU::V_CMPX_EQ_U64_nosdst_e32_term:
302 MI.setDesc(
TII->get(AMDGPU::V_CMPX_EQ_U64_nosdst_e32));
318 for (;
I !=
E; ++
I) {
319 if (!
I->isTerminator())
320 return Seen ? FirstNonTerm :
I;
322 if (removeTerminatorBit(*
I)) {
335 const unsigned InstLimit = 25;
338 for (
unsigned N = 0;
N <= InstLimit &&
I !=
E; ++
I, ++
N) {
339 Register CopyFromExec = isCopyFromExec(*
I);
352 if (Succ->isLiveIn(
Reg))
368 unsigned MaxInstructions)
const {
371 unsigned CurrentIteration = 0;
373 for (++
A; CurrentIteration < MaxInstructions &&
A !=
E; ++
A) {
374 if (
A->isDebugInstr())
381 if (
A->modifiesRegister(
Reg,
TRI))
388 if (Terminator && KillFlagCandidates &&
A != Terminator &&
391 if (MO.isReg() && MO.isKill()) {
393 if (Candidate !=
Reg &&
TRI->regsOverlap(Candidate,
Reg))
412bool SIOptimizeExecMasking::isRegisterInUseBetween(
MachineInstr &Stop,
416 bool IgnoreStart)
const {
427 if (!
A->isDebugInstr())
431 return !LR.available(
Reg) || MRI->isReserved(
Reg);
436bool SIOptimizeExecMasking::isRegisterInUseAfter(
MachineInstr &Stop,
451bool SIOptimizeExecMasking::optimizeExecSequence() {
463 unsigned SearchCount = 0;
464 const unsigned SearchLimit = 5;
465 while (
I !=
E && SearchCount++ < SearchLimit) {
466 CopyToExec = isCopyToExec(*
I);
476 auto *CopyToExecInst = &*
I;
477 auto CopyFromExecInst = findExecCopy(
MBB,
I);
478 if (CopyFromExecInst ==
E) {
480 if (PrepareExecInst ==
E)
483 if (CopyToExecInst->getOperand(1).isKill() &&
487 PrepareExecInst->getOperand(0).setReg(LMC.ExecReg);
491 CopyToExecInst->eraseFromParent();
493 }
else if (optimizeAndN2WrExecSequence(*CopyToExecInst, CopyToExec)) {
506 Register CopyFromExec = CopyFromExecInst->getOperand(0).getReg();
511 J = std::next(CopyFromExecInst->getIterator()),
512 JE =
I->getIterator();
514 if (J->isDebugInstr())
517 if (SaveExecInst && J->readsRegister(LMC.ExecReg,
TRI)) {
518 LLVM_DEBUG(
dbgs() <<
"exec read prevents saveexec: " << *J <<
'\n');
521 SaveExecInst =
nullptr;
525 bool ReadsCopyFromExec = J->readsRegister(CopyFromExec,
TRI);
527 if (J->modifiesRegister(CopyToExec,
TRI)) {
531 SaveExecInst =
nullptr;
536 if (SaveExecOp == AMDGPU::INSTRUCTION_LIST_END)
539 if (ReadsCopyFromExec) {
541 LLVM_DEBUG(
dbgs() <<
"Found save exec op: " << *SaveExecInst <<
'\n');
544 LLVM_DEBUG(
dbgs() <<
"Instruction does not read exec copy: " << *J
548 if (ReadsCopyFromExec && !SaveExecInst) {
557 LLVM_DEBUG(
dbgs() <<
"Found second use of save inst candidate: " << *J
562 if (SaveExecInst && J->readsRegister(CopyToExec,
TRI)) {
563 assert(SaveExecInst != &*J);
571 LLVM_DEBUG(
dbgs() <<
"Insert save exec op: " << *SaveExecInst <<
'\n');
578 if (Src0.
isReg() && Src0.
getReg() == CopyFromExec) {
580 }
else if (Src1.
isReg() && Src1.
getReg() == CopyFromExec) {
588 CopyFromExecInst->eraseFromParent();
598 CopyToExecInst->eraseFromParent();
601 OtherInst->substituteRegister(CopyToExec, LMC.ExecReg,
602 AMDGPU::NoSubRegister, *
TRI);
611bool SIOptimizeExecMasking::blocksAndN2Sink(
const MachineInstr &
MI,
618 if (
TRI->regsOverlap(MO.getReg(), Dst))
620 if (MO.isUse() && MO.getReg() == AMDGPU::SCC)
635bool SIOptimizeExecMasking::optimizeAndN2WrExecSequence(
637 if (!
ST->hasNoSdstCMPX() ||
TII->pseudoToMCOpcode(LMC.AndN2WrExecOpc) == -1)
652 return MI.getOpcode() == LMC.AndN2Opc &&
MI.getOperand(0).getReg() == Dst;
657 return IsAndN2Def(*
Check) || blocksAndN2Sink(*
Check, Dst);
660 if (!AndN2Inst || !IsAndN2Def(*AndN2Inst))
665 if (!Src.isReg() || !
Mask.isReg() ||
Mask.getReg() != LMC.ExecReg)
670 if (
MI.modifiesRegister(Src.getReg(),
TRI))
675 if (isRegisterInUseAfter(CopyToExecInst, AMDGPU::SCC))
691bool SIOptimizeExecMasking::optimizeVCMPSaveExecSequence(
705 bool IsSGPR32 =
TRI->getRegSizeInBits(MoveDest, *MRI) == 32;
706 unsigned MovOpcode = IsSGPR32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64;
717 auto TryAddImmediateValueFromNamedOperand =
718 [&](AMDGPU::OpName OperandName) ->
void {
719 if (
auto *
Mod =
TII->getNamedOperand(VCmp, OperandName))
720 Builder.addImm(
Mod->getImm());
723 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::src0_modifiers);
726 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::src1_modifiers);
729 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::clamp);
731 TryAddImmediateValueFromNamedOperand(AMDGPU::OpName::op_sel);
735 MRI->clearKillFlags(Src0->
getReg());
737 MRI->clearKillFlags(Src1->
getReg());
740 MO->setIsKill(
false);
755void SIOptimizeExecMasking::tryRecordVCmpxAndSaveexecSequence(
757 if (!
ST->hasGFX10_3Insts())
760 if (
MI.getOpcode() != LMC.AndSaveExecOpc)
763 Register SaveExecDest =
MI.getOperand(0).getReg();
764 if (!
TRI->isSGPRReg(*MRI, SaveExecDest))
768 if (!SaveExecSrc0->
isReg())
780 VCmp = findInstrBackwards(
786 {LMC.ExecReg, SaveExecSrc0->
getReg()});
792 assert(VCmpDest &&
"Should have an sdst operand!");
814 if (isRegisterInUseBetween(*VCmp,
MI, VCmpDest->
getReg(),
false,
true) ||
815 isRegisterInUseAfter(
MI, VCmpDest->
getReg()))
829 if (!findInstrBackwards(
831 VCmp, &KillFlagCandidates))
835 SaveExecVCmpMapping[&
MI] = VCmp;
843void SIOptimizeExecMasking::tryRecordOrSaveexecXorSequence(
MachineInstr &
MI) {
844 if (
MI.getOpcode() == LMC.XorOpc && &
MI != &
MI.getParent()->front()) {
851 (XorSrc0.
getReg() == LMC.ExecReg || XorSrc1.
getReg() == LMC.ExecReg)) {
855 auto It =
prev_nodbg(
MI.getIterator(),
MI.getParent()->instr_begin());
857 if (PossibleOrSaveexec.
getOpcode() != LMC.OrSaveExecOpc)
863 if ((XorSrc0.
getReg() == LMC.ExecReg &&
866 XorSrc1.
getReg() == LMC.ExecReg)) {
867 OrXors.emplace_back(&PossibleOrSaveexec, &
MI);
874bool SIOptimizeExecMasking::optimizeOrSaveexecXorSequences() {
875 if (OrXors.empty()) {
881 for (
const auto &Pair : OrXors) {
884 std::tie(
Or,
Xor) = Pair;
885 BuildMI(*
Or->getParent(),
Or->getIterator(),
Or->getDebugLoc(),
886 TII->get(LMC.AndN2SaveExecOpc),
Or->getOperand(0).getReg())
887 .
addReg(
Or->getOperand(1).getReg());
889 Or->eraseFromParent();
890 Xor->eraseFromParent();
898bool SIOptimizeExecMaskingLegacy::runOnMachineFunction(
MachineFunction &MF) {
902 return SIOptimizeExecMasking(&MF).run();
905bool SIOptimizeExecMasking::run() {
906 bool Changed = optimizeExecSequence();
909 SaveExecVCmpMapping.clear();
910 KillFlagCandidates.clear();
911 static unsigned SearchWindow = 10;
913 unsigned SearchCount = 0;
916 if (
MI.isDebugInstr())
919 if (SearchCount >= SearchWindow) {
923 tryRecordOrSaveexecXorSequence(
MI);
924 tryRecordVCmpxAndSaveexecSequence(
MI);
926 if (
MI.modifiesRegister(LMC.ExecReg,
TRI)) {
934 Changed |= optimizeOrSaveexecXorSequences();
935 for (
const auto &Entry : SaveExecVCmpMapping) {
939 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)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
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.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
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.