41 #define DEBUG_TYPE "si-insert-skips" 44 "amdgpu-skip-threshold",
45 cl::desc(
"Number of instructions before jumping over divergent control flow"),
54 unsigned SkipThreshold = 0;
78 return "SI insert s_cbranch_execz instructions";
91 "SI insert s_cbranch_execz instructions",
false,
false)
96 if (MI.isMetaInstruction())
100 switch (MI.getOpcode()) {
101 case AMDGPU::SI_MASK_BRANCH:
110 unsigned NumInstr = 0;
114 MBBI != End && MBBI != ToI; ++MBBI) {
118 NumInstr < SkipThreshold &&
I !=
E; ++
I) {
128 if (
I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ ||
129 I->getOpcode() == AMDGPU::S_CBRANCH_VCCZ)
132 if (
TII->hasUnwantedEffectsWhenEXECEmpty(*
I))
136 if (
TII->isSMRD(*
I) ||
TII->isVMEM(*
I) ||
TII->isFLAT(*
I) ||
137 I->getOpcode() == AMDGPU::S_WAITCNT)
141 if (NumInstr >= SkipThreshold)
162 BuildMI(&MBB, DL,
TII->get(AMDGPU::S_CBRANCH_EXECNZ))
168 BuildMI(*SkipBB, Insert, DL,
TII->get(AMDGPU::EXP_DONE))
179 BuildMI(*SkipBB, Insert, DL,
TII->get(AMDGPU::S_ENDPGM)).addImm(0);
189 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: {
197 Opcode = AMDGPU::V_CMPX_EQ_F32_e64;
201 Opcode = AMDGPU::V_CMPX_LT_F32_e64;
205 Opcode = AMDGPU::V_CMPX_LE_F32_e64;
209 Opcode = AMDGPU::V_CMPX_GT_F32_e64;
213 Opcode = AMDGPU::V_CMPX_GE_F32_e64;
217 Opcode = AMDGPU::V_CMPX_LG_F32_e64;
220 Opcode = AMDGPU::V_CMPX_O_F32_e64;
223 Opcode = AMDGPU::V_CMPX_U_F32_e64;
226 Opcode = AMDGPU::V_CMPX_NLG_F32_e64;
229 Opcode = AMDGPU::V_CMPX_NGE_F32_e64;
232 Opcode = AMDGPU::V_CMPX_NGT_F32_e64;
235 Opcode = AMDGPU::V_CMPX_NLE_F32_e64;
238 Opcode = AMDGPU::V_CMPX_NLT_F32_e64;
241 Opcode = AMDGPU::V_CMPX_NEQ_F32_e64;
273 case AMDGPU::SI_KILL_I1_TERMINATOR: {
276 unsigned Exec = ST.
isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
279 assert(KillVal == 0 || KillVal == -1);
283 int64_t Imm = Op.
getImm();
284 assert(Imm == 0 || Imm == -1);
288 : AMDGPU::S_MOV_B64), Exec)
293 unsigned Opcode = KillVal ? AMDGPU::S_ANDN2_B64 : AMDGPU::S_AND_B64;
295 Opcode = KillVal ? AMDGPU::S_ANDN2_B32 : AMDGPU::S_AND_B32;
325 if (!shouldSkip(**SrcMBB.
succ_begin(), *DestBB))
331 BuildMI(SrcMBB, InsPt, DL,
TII->get(AMDGPU::S_CBRANCH_EXECZ))
337 bool SIInsertSkips::optimizeVccBranch(
MachineInstr &MI)
const {
344 bool Changed =
false;
347 const bool IsWave32 = ST.
isWave32();
348 const unsigned CondReg =
TRI->getVCC();
349 const unsigned ExecReg = IsWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC;
350 const unsigned And = IsWave32 ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64;
354 bool ReadsCond =
false;
356 for (++A ; A !=
E ; ++A) {
359 if (A->modifiesRegister(ExecReg,
TRI))
361 if (A->modifiesRegister(CondReg,
TRI)) {
362 if (!A->definesRegister(CondReg,
TRI) || A->getOpcode() != And)
366 ReadsCond |= A->readsRegister(CondReg,
TRI);
374 TII->commuteInstruction(*A);
377 if (Op1.
getReg() != ExecReg)
382 unsigned SReg = AMDGPU::NoRegister;
385 auto M = std::next(A);
386 bool ReadsSreg =
false;
387 for ( ; M !=
E ; ++M) {
388 if (M->definesRegister(SReg,
TRI))
390 if (M->modifiesRegister(SReg,
TRI))
392 ReadsSreg |= M->readsRegister(SReg,
TRI);
395 !M->isMoveImmediate() ||
396 !M->getOperand(1).isImm() ||
397 M->getOperand(1).getImm() != -1)
401 if (!ReadsSreg && Op2.
isKill()) {
402 A->getOperand(2).ChangeToImmediate(-1);
403 M->eraseFromParent();
407 if (!ReadsCond && A->registerDefIsDead(AMDGPU::SCC) &&
409 A->eraseFromParent();
411 bool IsVCCZ = MI.
getOpcode() == AMDGPU::S_CBRANCH_VCCZ;
412 if (SReg == ExecReg) {
419 MI.
setDesc(
TII->get(IsVCCZ ? AMDGPU::S_CBRANCH_EXECZ
420 : AMDGPU::S_CBRANCH_EXECNZ));
435 bool HaveKill =
false;
436 bool MadeChange =
false;
446 BI != BE; BI = NextBB) {
447 NextBB = std::next(BI);
449 bool HaveSkipBlock =
false;
451 if (!ExecBranchStack.
empty() && ExecBranchStack.
back() == &MBB) {
456 if (HaveKill && ExecBranchStack.
empty()) {
463 for (I = MBB.
begin(); I != MBB.
end(); I = Next) {
469 case AMDGPU::SI_MASK_BRANCH:
471 MadeChange |= skipMaskBranch(MI, MBB);
474 case AMDGPU::S_BRANCH:
479 }
else if (HaveSkipBlock) {
487 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR:
488 case AMDGPU::SI_KILL_I1_TERMINATOR:
492 if (ExecBranchStack.
empty()) {
493 if (NextBB != BE && skipIfDead(MI, *NextBB)) {
494 HaveSkipBlock =
true;
495 NextBB = std::next(BI);
505 case AMDGPU::SI_RETURN_TO_EPILOG:
514 if (!EmptyMBBAtEnd) {
521 .addMBB(EmptyMBBAtEnd);
522 I->eraseFromParent();
526 case AMDGPU::S_CBRANCH_VCCZ:
527 case AMDGPU::S_CBRANCH_VCCNZ:
528 MadeChange |= optimizeVccBranch(MI);
const MachineInstrBuilder & add(const MachineOperand &MO) const
AMDGPU specific subclass of TargetSubtarget.
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
const SIInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static const AMDGPUSubtarget & get(const MachineFunction &MF)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
static INITIALIZE_PASS(SIInsertSkips, DEBUG_TYPE, "SI insert s_cbranch_execz instructions", false, false) char &llvm bool opcodeEmitsNoInsts(const MachineInstr &MI)
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
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.
bool killsRegister(Register Reg, const TargetRegisterInfo *TRI=nullptr) const
Return true if the MachineInstr kills the specified register.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
char & SIInsertSkipsPassID
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
self_iterator getIterator()
succ_iterator succ_begin()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Iterator for intrusive lists based on ilist_node.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
BlockVerifier::State From
LLVM_READONLY int getVOPe32(uint16_t Opcode)
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
Calling convention used for Mesa/AMDPAL pixel shaders.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_READONLY int getVCMPXNoSDstOp(uint16_t Opcode)
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
static cl::opt< unsigned > Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), cl::init(100), cl::Hidden)
void addImplicitDefUseOperands(MachineFunction &MF)
Add all implicit def and use operands to this instruction.
int findRegisterUseOperandIdx(Register Reg, bool isKill=false, const TargetRegisterInfo *TRI=nullptr) const
Returns the operand index that is a use of the specific register or -1 if it is not found...
const MachineBasicBlock * getParent() const
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
bool hasNoSdstCMPX() const
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Interface definition for SIInstrInfo.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
reverse_self_iterator getReverseIterator()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
const MachineBasicBlock & back() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
StringRef - Represent a constant reference to a string, i.e.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
static cl::opt< unsigned > SkipThresholdFlag("amdgpu-skip-threshold", cl::desc("Number of instructions before jumping over divergent control flow"), cl::init(12), cl::Hidden)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const SIRegisterInfo * getRegisterInfo() const override