60 #define DEBUG_TYPE "gcn-dpp-combine" 62 STATISTIC(NumDPPMovsCombined,
"Number of DPP moves combined.");
88 int64_t
Mask = -1)
const;
101 StringRef getPassName()
const override {
return "GCN DPP Combine"; }
109 int getDPPOp(
unsigned Op)
const;
116 char GCNDPPCombine::ID = 0;
121 return new GCNDPPCombine();
124 int GCNDPPCombine::getDPPOp(
unsigned Op)
const {
142 switch(
Def->getOpcode()) {
144 case AMDGPU::IMPLICIT_DEF:
147 case AMDGPU::V_MOV_B32_e32: {
148 auto &Op1 =
Def->getOperand(1);
160 bool CombBCZ)
const {
164 auto DPPOp = getDPPOp(OrigOp);
181 assert(OldIdx == NumOperands);
189 LLVM_DEBUG(
dbgs() <<
" failed: no old operand in DPP instruction," 196 AMDGPU::OpName::src0_modifiers)) {
198 AMDGPU::OpName::src0_modifiers));
200 DPPInst.addImm(Mod0->getImm());
203 AMDGPU::OpName::src0_modifiers) != -1) {
209 if (!TII->
isOperandLegal(*DPPInst.getInstr(), NumOperands, Src0)) {
215 DPPInst->getOperand(NumOperands).setIsKill(
false);
219 AMDGPU::OpName::src1_modifiers)) {
221 AMDGPU::OpName::src1_modifiers));
223 DPPInst.addImm(Mod1->getImm());
226 AMDGPU::OpName::src1_modifiers) != -1) {
231 if (!TII->
isOperandLegal(*DPPInst.getInstr(), NumOperands, Src1)) {
241 if (!TII->
getNamedOperand(*DPPInst.getInstr(), AMDGPU::OpName::src2) ||
253 DPPInst.addImm(CombBCZ ? 1 : 0);
257 DPPInst.getInstr()->eraseFromParent();
261 return DPPInst.getInstr();
268 case AMDGPU::V_ADD_U32_e32:
269 case AMDGPU::V_ADD_U32_e64:
270 case AMDGPU::V_ADD_I32_e32:
271 case AMDGPU::V_ADD_I32_e64:
272 case AMDGPU::V_OR_B32_e32:
273 case AMDGPU::V_OR_B32_e64:
274 case AMDGPU::V_SUBREV_U32_e32:
275 case AMDGPU::V_SUBREV_U32_e64:
276 case AMDGPU::V_SUBREV_I32_e32:
277 case AMDGPU::V_SUBREV_I32_e64:
278 case AMDGPU::V_MAX_U32_e32:
279 case AMDGPU::V_MAX_U32_e64:
280 case AMDGPU::V_XOR_B32_e32:
281 case AMDGPU::V_XOR_B32_e64:
282 if (OldOpnd->
getImm() == 0)
285 case AMDGPU::V_AND_B32_e32:
286 case AMDGPU::V_AND_B32_e64:
287 case AMDGPU::V_MIN_U32_e32:
288 case AMDGPU::V_MIN_U32_e64:
289 if (static_cast<uint32_t>(OldOpnd->
getImm()) ==
293 case AMDGPU::V_MIN_I32_e32:
294 case AMDGPU::V_MIN_I32_e64:
295 if (static_cast<int32_t>(OldOpnd->
getImm()) ==
299 case AMDGPU::V_MAX_I32_e32:
300 case AMDGPU::V_MAX_I32_e64:
301 if (static_cast<int32_t>(OldOpnd->
getImm()) ==
302 std::numeric_limits<int32_t>::min())
305 case AMDGPU::V_MUL_I32_I24_e32:
306 case AMDGPU::V_MUL_I32_I24_e64:
307 case AMDGPU::V_MUL_U32_U24_e32:
308 case AMDGPU::V_MUL_U32_U24_e64:
309 if (OldOpnd->
getImm() == 1)
320 bool CombBCZ)
const {
322 if (!CombBCZ && OldOpndValue && OldOpndValue->
isImm()) {
324 if (!Src1 || !Src1->isReg()) {
325 LLVM_DEBUG(
dbgs() <<
" failed: no src1 or it isn't a register\n");
329 LLVM_DEBUG(
dbgs() <<
" failed: old immediate isn't an identity\n");
333 if (!
isOfRegClass(CombOldVGPR, AMDGPU::VGPR_32RegClass, *MRI)) {
338 return createDPPInst(OrigMI, MovMI, CombOldVGPR, CombBCZ);
343 bool GCNDPPCombine::hasNoImmOrEqual(
MachineInstr &
MI,
unsigned OpndName,
350 return (Imm->getImm() &
Mask) == Value;
353 bool GCNDPPCombine::combineDPPMov(
MachineInstr &MovMI)
const {
358 assert(DstOpnd && DstOpnd->isReg());
359 auto DPPMovReg = DstOpnd->getReg();
360 if (DPPMovReg.isPhysical()) {
370 auto *RowMaskOpnd = TII->
getNamedOperand(MovMI, AMDGPU::OpName::row_mask);
371 assert(RowMaskOpnd && RowMaskOpnd->isImm());
372 auto *BankMaskOpnd = TII->
getNamedOperand(MovMI, AMDGPU::OpName::bank_mask);
373 assert(BankMaskOpnd && BankMaskOpnd->isImm());
374 const bool MaskAllLanes = RowMaskOpnd->getImm() == 0xF &&
375 BankMaskOpnd->getImm() == 0xF;
377 auto *BCZOpnd = TII->
getNamedOperand(MovMI, AMDGPU::OpName::bound_ctrl);
378 assert(BCZOpnd && BCZOpnd->isImm());
379 bool BoundCtrlZero = BCZOpnd->getImm();
384 assert(SrcOpnd && SrcOpnd->isReg());
390 auto *
const OldOpndValue = getOldOpndValue(*OldOpnd);
395 assert(!OldOpndValue || OldOpndValue->
isImm() || OldOpndValue == OldOpnd);
397 bool CombBCZ =
false;
399 if (MaskAllLanes && BoundCtrlZero) {
402 if (!OldOpndValue || !OldOpndValue->
isImm()) {
409 " failed: old reg def and mov should be in the same BB\n");
413 if (OldOpndValue->
getImm() == 0) {
418 }
else if (BoundCtrlZero) {
421 " failed: old!=0 and bctrl:0 and not all lanes isn't combinable\n");
430 dbgs() << *OldOpndValue;
431 dbgs() <<
", bound_ctrl=" << CombBCZ <<
'\n');
437 if (CombBCZ && OldOpndValue) {
441 TII->get(AMDGPU::IMPLICIT_DEF), CombOldVGPR.
Reg);
442 DPPMIs.push_back(UndefInst.getInstr());
445 OrigMIs.push_back(&MovMI);
446 bool Rollback =
true;
453 while (!Uses.
empty()) {
461 if (OrigOp == AMDGPU::REG_SEQUENCE) {
463 unsigned FwdSubReg = 0;
472 for (OpNo = 1; OpNo <
E; OpNo += 2) {
483 if (Op.getSubReg() == FwdSubReg)
486 RegSeqWithOpNos[&OrigMI].push_back(OpNo);
490 if (TII->
isVOP3(OrigOp)) {
497 if (!hasNoImmOrEqual(OrigMI, AMDGPU::OpName::src0_modifiers, 0, Mask) ||
498 !hasNoImmOrEqual(OrigMI, AMDGPU::OpName::src1_modifiers, 0, Mask) ||
499 !hasNoImmOrEqual(OrigMI, AMDGPU::OpName::clamp, 0) ||
500 !hasNoImmOrEqual(OrigMI, AMDGPU::OpName::omod, 0)) {
501 LLVM_DEBUG(
dbgs() <<
" failed: VOP3 has non-default modifiers\n");
504 }
else if (!TII->
isVOP1(OrigOp) && !TII->
isVOP2(OrigOp)) {
511 if (
auto *DPPInst = createDPPInst(OrigMI, MovMI, CombOldVGPR,
512 OldOpndValue, CombBCZ)) {
513 DPPMIs.push_back(DPPInst);
520 BB->insert(OrigMI, NewMI);
521 if (TII->commuteInstruction(*NewMI)) {
523 if (
auto *DPPInst = createDPPInst(*NewMI, MovMI, CombOldVGPR,
524 OldOpndValue, CombBCZ)) {
525 DPPMIs.push_back(DPPInst);
530 NewMI->eraseFromParent();
535 OrigMIs.push_back(&OrigMI);
538 Rollback |= !Uses.
empty();
540 for (
auto *MI : *(Rollback? &DPPMIs : &OrigMIs))
544 for (
auto &S : RegSeqWithOpNos) {
546 S.first->eraseFromParent();
549 while (!S.second.empty())
550 S.first->getOperand(S.second.pop_back_val()).setIsUndef(
true);
563 TII =
ST.getInstrInfo();
567 bool Changed =
false;
568 for (
auto &MBB : MF) {
569 for (
auto I = MBB.rbegin(),
E = MBB.rend();
I !=
E;) {
571 if (MI.
getOpcode() == AMDGPU::V_MOV_B32_dpp && combineDPPMov(MI)) {
573 ++NumDPPMovsCombined;
574 }
else if (MI.
getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO) {
576 for (
auto M : { Split.first, Split.second }) {
577 if (combineDPPMov(*M))
578 ++NumDPPMovsCombined;
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool use_nodbg_empty(unsigned RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register...
AMDGPU specific subclass of TargetSubtarget.
This class represents lattice values for constants.
MachineInstr * getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, MachineRegisterInfo &MRI)
Return the defining instruction for a given reg:subreg pair skipping copy like instructions and subre...
bool isOfRegClass(const TargetInstrInfo::RegSubRegPair &P, const TargetRegisterClass &TRC, MachineRegisterInfo &MRI)
Returns true if a reg:subreg pair P has a TRC class.
bool isOperandLegal(const MachineInstr &MI, unsigned OpIdx, const MachineOperand *MO=nullptr) const
Check if MO is a legal operand if it was the OpIdx Operand for MI.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
STATISTIC(NumFunctions, "Total number of functions")
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
LLVM_READONLY int getDPPOp32(uint16_t Opcode)
TargetInstrInfo::RegSubRegPair RegSubRegPair
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned const MachineRegisterInfo * MRI
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")
FunctionPass * createGCNDPPCombinePass()
Represent the analysis usage information of a pass.
bool hasVALU32BitEncoding(unsigned Opcode) const
Return true if this 64-bit VALU instruction has a 32-bit encoding.
std::pair< MachineInstr *, MachineInstr * > expandMovDPP64(MachineInstr &MI) const
FunctionPass class - This class is used to implement most global optimizations.
static bool isVOP2(const MachineInstr &MI)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
int pseudoToMCOpcode(int Opcode) const
Return a target-specific opcode if Opcode is a pseudo instruction.
Align max(MaybeAlign Lhs, Align Rhs)
LLVM_READONLY int getVOPe32(uint16_t Opcode)
TargetInstrInfo::RegSubRegPair getRegSubRegPair(const MachineOperand &O)
Create RegSubRegPair from a register MachineOperand.
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
Create a new MachineInstr which is a copy of Orig, identical in all ways except the instruction has n...
MachineOperand class - Representation of each machine instruction operand.
A pair composed of a register and a sub-register index.
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
void initializeGCNDPPCombinePass(PassRegistry &)
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.
bool execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, Register VReg, const MachineInstr &DefMI)
Return false if EXEC is not changed between the def of VReg at DefMI and all its uses.
static bool isVOP3(const MachineInstr &MI)
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
static bool isIdentityValue(unsigned OrigMIOp, MachineOperand *OldOpnd)
const MachineOperand & getOperand(unsigned i) const
static bool isVOP1(const MachineInstr &MI)
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.