43 #include "llvm/Config/llvm-config.h" 53 #include <unordered_map> 57 #define DEBUG_TYPE "si-peephole-sdwa" 59 STATISTIC(NumSDWAPatternsFound,
"Number of SDWA patterns found.");
61 "Number of instruction converted to SDWA.");
77 std::unordered_map<MachineInstr *, std::unique_ptr<SDWAOperand>> SDWAOperands;
78 std::unordered_map<MachineInstr *, SDWAOperandsVector> PotentialMatches;
94 bool convertToSDWA(
MachineInstr &
MI,
const SDWAOperandsVector &SDWAOperands);
97 StringRef getPassName()
const override {
return "SI Peephole SDWA"; }
112 : Target(TargetOp), Replaced(ReplacedOp) {
117 virtual ~SDWAOperand() =
default;
127 return &getParentInst()->getParent()->getParent()->getRegInfo();
130 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 138 class SDWASrcOperand :
public SDWAOperand {
147 SdwaSel SrcSel_ =
DWORD,
bool Abs_ =
false,
bool Neg_ =
false,
149 : SDWAOperand(TargetOp, ReplacedOp),
150 SrcSel(SrcSel_), Abs(Abs_), Neg(Neg_), Sext(Sext_) {}
155 SdwaSel getSrcSel()
const {
return SrcSel; }
156 bool getAbs()
const {
return Abs; }
157 bool getNeg()
const {
return Neg; }
158 bool getSext()
const {
return Sext; }
163 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 168 class SDWADstOperand :
public SDWAOperand {
177 : SDWAOperand(TargetOp, ReplacedOp), DstSel(DstSel_), DstUn(DstUn_) {}
182 SdwaSel getDstSel()
const {
return DstSel; }
183 DstUnused getDstUnused()
const {
return DstUn; }
185 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 190 class SDWADstPreserveOperand :
public SDWADstOperand {
198 Preserve(PreserveOp) {}
204 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 213 char SIPeepholeSDWA::ID = 0;
218 return new SIPeepholeSDWA();
222 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 225 case BYTE_0: OS <<
"BYTE_0";
break;
226 case BYTE_1: OS <<
"BYTE_1";
break;
227 case BYTE_2: OS <<
"BYTE_2";
break;
228 case BYTE_3: OS <<
"BYTE_3";
break;
229 case WORD_0: OS <<
"WORD_0";
break;
230 case WORD_1: OS <<
"WORD_1";
break;
231 case DWORD: OS <<
"DWORD";
break;
252 OS <<
"SDWA src: " << *getTargetOperand()
253 <<
" src_sel:" << getSrcSel()
254 <<
" abs:" << getAbs() <<
" neg:" << getNeg()
255 <<
" sext:" << getSext() <<
'\n';
260 OS <<
"SDWA dst: " << *getTargetOperand()
261 <<
" dst_sel:" << getDstSel()
262 <<
" dst_unused:" << getDstUnused() <<
'\n';
267 OS <<
"SDWA preserve dst: " << *getTargetOperand()
268 <<
" dst_sel:" << getDstSel()
269 <<
" preserve:" << *getPreservedOperand() <<
'\n';
287 return LHS.
isReg() &&
324 for (
auto &DefMO : DefInstr->
defs()) {
325 if (DefMO.isReg() && DefMO.getReg() == Reg->
getReg())
333 uint64_t SDWASrcOperand::getSrcMods(
const SIInstrInfo *TII,
339 Mods =
Mod->getImm();
343 Mods =
Mod->getImm();
348 "Float and integer src modifiers can't be set simulteniously");
371 bool IsPreserveSrc =
false;
377 if (!
isSameReg(*Src, *getReplacedOperand())) {
384 !
isSameReg(*Src, *getReplacedOperand())) {
405 IsPreserveSrc =
true;
407 AMDGPU::OpName::vdst);
420 if ((MI.
getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
421 MI.
getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
422 !
isSameReg(*Src, *getReplacedOperand())) {
429 (IsPreserveSrc || (SrcSel && SrcMods)));
432 if (!IsPreserveSrc) {
433 SrcSel->
setImm(getSrcSel());
434 SrcMods->
setImm(getSrcMods(TII, Src));
436 getTargetOperand()->setIsKill(
false);
452 if (&UseInst != ParentMI)
462 if ((MI.
getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
463 MI.
getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
472 isSameReg(*Operand, *getReplacedOperand()));
476 DstSel->
setImm(getDstSel());
479 DstUnused->
setImm(getDstUnused());
483 getParentInst()->eraseFromParent();
487 bool SDWADstPreserveOperand::convertToSDWA(
MachineInstr &MI,
495 getMRI()->clearKillFlags(MO.getReg());
501 MBB->insert(getParentInst(), &MI);
507 getPreservedOperand()->getSubReg());
514 return SDWADstOperand::convertToSDWA(MI, TII);
544 std::unique_ptr<SDWAOperand>
548 case AMDGPU::V_LSHRREV_B32_e32:
549 case AMDGPU::V_ASHRREV_I32_e32:
550 case AMDGPU::V_LSHLREV_B32_e32:
551 case AMDGPU::V_LSHRREV_B32_e64:
552 case AMDGPU::V_ASHRREV_I32_e64:
553 case AMDGPU::V_LSHLREV_B32_e64: {
563 auto Imm = foldToImm(*Src0);
567 if (*Imm != 16 && *Imm != 24)
572 if (TRI->isPhysicalRegister(Src1->
getReg()) ||
573 TRI->isPhysicalRegister(Dst->
getReg()))
576 if (Opcode == AMDGPU::V_LSHLREV_B32_e32 ||
577 Opcode == AMDGPU::V_LSHLREV_B32_e64) {
578 return make_unique<SDWADstOperand>(
581 return make_unique<SDWASrcOperand>(
583 Opcode != AMDGPU::V_LSHRREV_B32_e32 &&
584 Opcode != AMDGPU::V_LSHRREV_B32_e64);
589 case AMDGPU::V_LSHRREV_B16_e32:
590 case AMDGPU::V_ASHRREV_I16_e32:
591 case AMDGPU::V_LSHLREV_B16_e32:
592 case AMDGPU::V_LSHRREV_B16_e64:
593 case AMDGPU::V_ASHRREV_I16_e64:
594 case AMDGPU::V_LSHLREV_B16_e64: {
604 auto Imm = foldToImm(*Src0);
605 if (!Imm || *Imm != 8)
611 if (TRI->isPhysicalRegister(Src1->
getReg()) ||
612 TRI->isPhysicalRegister(Dst->
getReg()))
615 if (Opcode == AMDGPU::V_LSHLREV_B16_e32 ||
616 Opcode == AMDGPU::V_LSHLREV_B16_e64) {
619 return make_unique<SDWASrcOperand>(
620 Src1, Dst,
BYTE_1,
false,
false,
621 Opcode != AMDGPU::V_LSHRREV_B16_e32 &&
622 Opcode != AMDGPU::V_LSHRREV_B16_e64);
627 case AMDGPU::V_BFE_I32:
628 case AMDGPU::V_BFE_U32: {
644 auto Offset = foldToImm(*Src1);
649 auto Width = foldToImm(*Src2);
655 if (*
Offset == 0 && *Width == 8)
657 else if (*
Offset == 0 && *Width == 16)
659 else if (*
Offset == 0 && *Width == 32)
661 else if (*
Offset == 8 && *Width == 8)
663 else if (*
Offset == 16 && *Width == 8)
665 else if (*
Offset == 16 && *Width == 16)
667 else if (*
Offset == 24 && *Width == 8)
675 if (TRI->isPhysicalRegister(Src0->
getReg()) ||
676 TRI->isPhysicalRegister(Dst->
getReg()))
679 return make_unique<SDWASrcOperand>(
680 Src0, Dst, SrcSel,
false,
false, Opcode != AMDGPU::V_BFE_U32);
683 case AMDGPU::V_AND_B32_e32:
684 case AMDGPU::V_AND_B32_e64: {
692 auto Imm = foldToImm(*Src0);
695 Imm = foldToImm(*Src1);
699 if (!Imm || (*Imm != 0x0000ffff && *Imm != 0x000000ff))
704 if (TRI->isPhysicalRegister(ValSrc->getReg()) ||
705 TRI->isPhysicalRegister(Dst->
getReg()))
708 return make_unique<SDWASrcOperand>(
712 case AMDGPU::V_OR_B32_e32:
713 case AMDGPU::V_OR_B32_e64: {
724 auto CheckOROperandsForSDWA =
726 if (!Op1 || !Op1->
isReg() || !Op2 || !Op2->isReg())
727 return CheckRetType(
None);
731 return CheckRetType(
None);
734 if (!TII->
isSDWA(*Op1Inst))
735 return CheckRetType(
None);
739 return CheckRetType(
None);
741 return CheckRetType(std::make_pair(Op1Def, Op2Def));
746 assert(OrSDWA && OrOther);
747 auto Res = CheckOROperandsForSDWA(OrSDWA, OrOther);
751 assert(OrSDWA && OrOther);
752 Res = CheckOROperandsForSDWA(OrSDWA, OrOther);
759 assert(OrSDWADef && OrOtherDef);
784 if (!TII->
isSDWA(*OtherInst))
792 bool DstSelAgree =
false;
795 (OtherDstSel ==
BYTE_3) ||
799 (OtherDstSel ==
BYTE_1) ||
803 (OtherDstSel ==
BYTE_2) ||
804 (OtherDstSel ==
BYTE_3) ||
808 (OtherDstSel ==
BYTE_2) ||
809 (OtherDstSel ==
BYTE_3) ||
813 (OtherDstSel ==
BYTE_1) ||
814 (OtherDstSel ==
BYTE_3) ||
818 (OtherDstSel ==
BYTE_1) ||
819 (OtherDstSel ==
BYTE_2) ||
822 default: DstSelAgree =
false;
838 return make_unique<SDWADstPreserveOperand>(
839 OrDst, OrSDWADef, OrOtherDef, DstSel);
844 return std::unique_ptr<SDWAOperand>(
nullptr);
849 if (
auto Operand = matchSDWAOperand(MI)) {
850 LLVM_DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *Operand <<
'\n');
851 SDWAOperands[&
MI] = std::move(Operand);
852 ++NumSDWAPatternsFound;
857 bool SIPeepholeSDWA::isConvertibleToSDWA(
const MachineInstr &MI,
877 if (SDst && SDst->
getReg() != AMDGPU::VCC)
891 if (!ST.
hasSDWAMac() && (Opc == AMDGPU::V_MAC_F16_e32 ||
892 Opc == AMDGPU::V_MAC_F32_e32))
896 if (Opc == AMDGPU::V_CNDMASK_B32_e32)
903 const SDWAOperandsVector &SDWAOperands) {
910 if (TII->
isSDWA(Opcode)) {
914 if (SDWAOpcode == -1)
919 const MCInstrDesc &SDWADesc = TII->get(SDWAOpcode);
965 if (SDWAOpcode == AMDGPU::V_MAC_F16_sdwa ||
966 SDWAOpcode == AMDGPU::V_MAC_F32_sdwa) {
977 SDWAInst.
add(*Clamp);
996 SDWAInst.
add(*DstSel);
1006 SDWAInst.
add(*DstUnused);
1016 SDWAInst.
add(*Src0Sel);
1026 SDWAInst.
add(*Src1Sel);
1038 assert(Dst && Dst->isTied());
1039 assert(Opcode == static_cast<unsigned int>(SDWAOpcode));
1042 assert(PreserveDstIdx != -1);
1052 bool Converted =
false;
1053 for (
auto &Operand : SDWAOperands) {
1065 if (PotentialMatches.count(Operand->getParentInst()) == 0)
1066 Converted |= Operand->convertToSDWA(*SDWAInst, TII);
1069 ConvertedInstructions.
push_back(SDWAInst);
1076 ++NumSDWAInstructionsPeepholed;
1084 void SIPeepholeSDWA::legalizeScalarOperands(
MachineInstr &MI,
1087 unsigned ConstantBusCount = 0;
1105 TII->get(AMDGPU::V_MOV_B32_e32), VGPR);
1107 Copy.addImm(Op.
getImm());
1108 else if (Op.
isReg())
1128 bool Changed =
false;
1130 matchSDWAOperands(MBB);
1132 for (
const auto &OperandPair : SDWAOperands) {
1133 const auto &Operand = OperandPair.second;
1134 MachineInstr *PotentialMI = Operand->potentialToConvert(TII);
1135 if (PotentialMI && isConvertibleToSDWA(*PotentialMI, ST)) {
1136 PotentialMatches[PotentialMI].push_back(Operand.get());
1140 for (
auto &PotentialPair : PotentialMatches) {
1142 convertToSDWA(PotentialMI, PotentialPair.second);
1145 PotentialMatches.clear();
1146 SDWAOperands.clear();
1148 Changed = !ConvertedInstructions.
empty();
1152 while (!ConvertedInstructions.
empty())
1153 legalizeScalarOperands(*ConvertedInstructions.
pop_back_val(),
ST);
void push_back(const T &Elt)
const MachineInstrBuilder & add(const MachineOperand &MO) const
Interface definition for SIRegisterInfo.
A common definition of LaneBitmask for use in TableGen and CodeGen.
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...
AMDGPU specific subclass of TargetSubtarget.
FunctionPass * createSIPeepholeSDWAPass()
Compute iterated dominance frontiers using a linear time algorithm.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
bool hasModifiersSet(const MachineInstr &MI, unsigned OpName) const
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
Describe properties that are true of each instruction in the target description file.
unsigned getReg() const
getReg - Returns the register number.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
void setIsUndef(bool Val=true)
unsigned getSubReg() const
static bool isSameReg(const MachineOperand &LHS, const MachineOperand &RHS)
const SIInstrInfo * getInstrInfo() const override
int64_t getNamedImmOperand(const MachineInstr &MI, unsigned OpName) const
Get required immediate operand.
static MachineOperand * findSingleRegDef(const MachineOperand *Reg, const MachineRegisterInfo *MRI)
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
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
bool isSGPRReg(const MachineRegisterInfo &MRI, unsigned Reg) const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
static MachineOperand * findSingleRegUse(const MachineOperand *Reg, const MachineRegisterInfo *MRI)
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool hasSDWAOutModsVOPC() const
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isVGPR(const MachineRegisterInfo &MRI, unsigned Reg) const
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
iterator_range< def_iterator > def_operands(unsigned Reg) const
LLVM_READONLY int getSDWAOp(uint16_t Opcode)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
bool isFoldableCopy(const MachineInstr &MI) const
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 void copyRegOperand(MachineOperand &To, const MachineOperand &From)
bool hasVGPRs(const TargetRegisterClass *RC) const
void initializeSIPeepholeSDWAPass(PassRegistry &)
Represent the analysis usage information of a pass.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
self_iterator getIterator()
iterator_range< mop_iterator > explicit_uses()
void setIsKill(bool Val=true)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
unsigned findTiedOperandIdx(unsigned OpIdx) const
Given the index of a tied register operand, find the operand it is tied to.
LLVM_READONLY int getVOPe32(uint16_t Opcode)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
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.
The access may modify the value stored in memory.
Target - Wrapper for Target specific information.
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
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.
Interface definition for SIInstrInfo.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
static bool isVOPC(const MachineInstr &MI)
int16_t RegClass
This specifies the register class enumeration of the operand if the operand is a register.
void setReg(unsigned Reg)
Change the register this operand corresponds to.
void setSubReg(unsigned subReg)
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool hasSDWAScalar() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isSDWA(const MachineInstr &MI)
const MCOperandInfo * OpInfo
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(unsigned Reg) const
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.
const SIRegisterInfo * getRegisterInfo() const override