22 #define DEBUG_TYPE "si-optimize-exec-masking" 38 return "SI optimize exec mask operations";
50 "SI optimize exec mask operations",
false,
false)
55 char SIOptimizeExecMasking::
ID = 0;
61 switch (
MI.getOpcode()) {
63 case AMDGPU::S_MOV_B64:
64 case AMDGPU::S_MOV_B64_term:
65 case AMDGPU::S_MOV_B32:
66 case AMDGPU::S_MOV_B32_term: {
69 Src.
getReg() == (
ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC))
70 return MI.getOperand(0).getReg();
74 return AMDGPU::NoRegister;
81 case AMDGPU::S_MOV_B64:
82 case AMDGPU::S_MOV_B32: {
85 Dst.
getReg() == (ST.
isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC) &&
90 case AMDGPU::S_MOV_B64_term:
91 case AMDGPU::S_MOV_B32_term:
102 case AMDGPU::S_AND_B64:
103 case AMDGPU::S_OR_B64:
104 case AMDGPU::S_XOR_B64:
105 case AMDGPU::S_ANDN2_B64:
106 case AMDGPU::S_ORN2_B64:
107 case AMDGPU::S_NAND_B64:
108 case AMDGPU::S_NOR_B64:
109 case AMDGPU::S_XNOR_B64: {
118 case AMDGPU::S_AND_B32:
119 case AMDGPU::S_OR_B32:
120 case AMDGPU::S_XOR_B32:
121 case AMDGPU::S_ANDN2_B32:
122 case AMDGPU::S_ORN2_B32:
123 case AMDGPU::S_NAND_B32:
124 case AMDGPU::S_NOR_B32:
125 case AMDGPU::S_XNOR_B32: {
127 if (Src1.
isReg() && Src1.
getReg() == AMDGPU::EXEC_LO)
130 if (Src2.
isReg() && Src2.
getReg() == AMDGPU::EXEC_LO)
136 return AMDGPU::NoRegister;
141 case AMDGPU::S_AND_B64:
142 return AMDGPU::S_AND_SAVEEXEC_B64;
143 case AMDGPU::S_OR_B64:
144 return AMDGPU::S_OR_SAVEEXEC_B64;
145 case AMDGPU::S_XOR_B64:
146 return AMDGPU::S_XOR_SAVEEXEC_B64;
147 case AMDGPU::S_ANDN2_B64:
148 return AMDGPU::S_ANDN2_SAVEEXEC_B64;
149 case AMDGPU::S_ORN2_B64:
150 return AMDGPU::S_ORN2_SAVEEXEC_B64;
151 case AMDGPU::S_NAND_B64:
152 return AMDGPU::S_NAND_SAVEEXEC_B64;
153 case AMDGPU::S_NOR_B64:
154 return AMDGPU::S_NOR_SAVEEXEC_B64;
155 case AMDGPU::S_XNOR_B64:
156 return AMDGPU::S_XNOR_SAVEEXEC_B64;
157 case AMDGPU::S_AND_B32:
158 return AMDGPU::S_AND_SAVEEXEC_B32;
159 case AMDGPU::S_OR_B32:
160 return AMDGPU::S_OR_SAVEEXEC_B32;
161 case AMDGPU::S_XOR_B32:
162 return AMDGPU::S_XOR_SAVEEXEC_B32;
163 case AMDGPU::S_ANDN2_B32:
164 return AMDGPU::S_ANDN2_SAVEEXEC_B32;
165 case AMDGPU::S_ORN2_B32:
166 return AMDGPU::S_ORN2_SAVEEXEC_B32;
167 case AMDGPU::S_NAND_B32:
168 return AMDGPU::S_NAND_SAVEEXEC_B32;
169 case AMDGPU::S_NOR_B32:
170 return AMDGPU::S_NOR_SAVEEXEC_B32;
171 case AMDGPU::S_XNOR_B32:
172 return AMDGPU::S_XNOR_SAVEEXEC_B32;
174 return AMDGPU::INSTRUCTION_LIST_END;
183 case AMDGPU::S_MOV_B64_term:
184 case AMDGPU::S_MOV_B32_term: {
185 MI.
setDesc(TII.get(AMDGPU::COPY));
188 case AMDGPU::S_XOR_B64_term: {
191 MI.
setDesc(TII.get(AMDGPU::S_XOR_B64));
194 case AMDGPU::S_XOR_B32_term: {
197 MI.
setDesc(TII.get(AMDGPU::S_XOR_B32));
200 case AMDGPU::S_OR_B32_term: {
203 MI.
setDesc(TII.get(AMDGPU::S_OR_B32));
206 case AMDGPU::S_ANDN2_B64_term: {
209 MI.
setDesc(TII.get(AMDGPU::S_ANDN2_B64));
212 case AMDGPU::S_ANDN2_B32_term: {
215 MI.
setDesc(TII.get(AMDGPU::S_ANDN2_B32));
227 for (; I !=
E; ++
I) {
228 if (!I->isTerminator())
243 unsigned CopyToExec) {
244 const unsigned InstLimit = 25;
247 for (
unsigned N = 0;
N <= InstLimit && I !=
E; ++
I, ++
N) {
261 if (Succ->isLiveIn(Reg))
268 bool SIOptimizeExecMasking::runOnMachineFunction(
MachineFunction &MF) {
299 auto CopyToExecInst = &*
I;
300 auto CopyFromExecInst =
findExecCopy(*TII, ST, MBB, I, CopyToExec);
301 if (CopyFromExecInst == E) {
302 auto PrepareExecInst = std::next(I);
303 if (PrepareExecInst == E)
306 if (CopyToExecInst->getOperand(1).isKill() &&
310 PrepareExecInst->getOperand(0).setReg(Exec);
314 CopyToExecInst->eraseFromParent();
326 Register CopyFromExec = CopyFromExecInst->getOperand(0).getReg();
331 = std::next(CopyFromExecInst->getIterator()), JE = I->getIterator();
334 LLVM_DEBUG(
dbgs() <<
"exec read prevents saveexec: " << *J <<
'\n');
337 SaveExecInst =
nullptr;
341 bool ReadsCopyFromExec = J->
readsRegister(CopyFromExec, TRI);
343 if (J->modifiesRegister(CopyToExec, TRI)) {
346 <<
printReg(CopyToExec, TRI) <<
'\n');
347 SaveExecInst =
nullptr;
352 if (SaveExecOp == AMDGPU::INSTRUCTION_LIST_END)
355 if (ReadsCopyFromExec) {
357 LLVM_DEBUG(
dbgs() <<
"Found save exec op: " << *SaveExecInst <<
'\n');
361 <<
"Instruction does not read exec copy: " << *J <<
'\n');
364 }
else if (ReadsCopyFromExec && !SaveExecInst) {
373 LLVM_DEBUG(
dbgs() <<
"Found second use of save inst candidate: " << *J
379 assert(SaveExecInst != &*J);
387 LLVM_DEBUG(
dbgs() <<
"Insert save exec op: " << *SaveExecInst <<
'\n');
394 if (Src0.
isReg() && Src0.
getReg() == CopyFromExec) {
396 }
else if (Src1.
isReg() && Src1.
getReg() == CopyFromExec) {
404 CopyFromExecInst->eraseFromParent();
411 .addReg(OtherOp->
getReg());
414 CopyToExecInst->eraseFromParent();
417 OtherInst->substituteRegister(CopyToExec, Exec,
418 AMDGPU::NoSubRegister, *TRI);
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
AMDGPU specific subclass of TargetSubtarget.
Wrapper class representing physical registers. Should be passed by value.
This class represents lattice values for constants.
static Register isCopyFromExec(const MachineInstr &MI, const GCNSubtarget &ST)
If MI is a copy from exec, return the register copied to.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const SIInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
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.
static bool removeTerminatorBit(const SIInstrInfo &TII, MachineInstr &MI)
static MachineBasicBlock::reverse_iterator findExecCopy(const SIInstrInfo &TII, const GCNSubtarget &ST, MachineBasicBlock &MBB, MachineBasicBlock::reverse_iterator I, unsigned CopyToExec)
static Register isCopyToExec(const MachineInstr &MI, const GCNSubtarget &ST)
If MI is a copy to exec, return the register copied from.
static MachineBasicBlock::reverse_iterator fixTerminators(const SIInstrInfo &TII, MachineBasicBlock &MBB)
iterator_range< succ_iterator > successors()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
void initializeSIOptimizeExecMaskingPass(PassRegistry &)
SI optimize exec mask operations
reverse_iterator rbegin()
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
self_iterator getIterator()
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
char & SIOptimizeExecMaskingID
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr reads the specified register.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
MachineOperand class - Representation of each machine instruction operand.
Promote Memory to Register
void setPreservesCFG()
This function should be called by the pass, iff they do not:
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.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
Interface definition for SIInstrInfo.
static Register isLogicalOpOnExec(const MachineInstr &MI)
If MI is a logical operation on an exec value, return the register copied to.
INITIALIZE_PASS_BEGIN(SIOptimizeExecMasking, DEBUG_TYPE, "SI optimize exec mask operations", false, false) INITIALIZE_PASS_END(SIOptimizeExecMasking
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static unsigned getSaveExecOp(unsigned Opc)
static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg)
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) 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, ..."), which produces the same result if Y and Z are exchanged.
Wrapper class representing virtual and physical registers.
const SIRegisterInfo * getRegisterInfo() const override