31 #include <unordered_map>
32 #include <unordered_set>
36 #define DEBUG_TYPE "si-peephole-sdwa"
38 STATISTIC(NumSDWAPatternsFound,
"Number of SDWA patterns found.");
40 "Number of instruction converted to SDWA.");
55 std::unordered_map<MachineInstr *, std::unique_ptr<SDWAOperand>> SDWAOperands;
56 std::unordered_map<MachineInstr *, SDWAOperandsVector> PotentialMatches;
71 bool convertToSDWA(
MachineInstr &
MI,
const SDWAOperandsVector &SDWAOperands);
74 StringRef getPassName()
const override {
return "SI Peephole SDWA"; }
89 :
Target(TargetOp), Replaced(ReplacedOp) {
94 virtual ~SDWAOperand() {}
103 return &getParentInst()->getParent()->getParent()->getRegInfo();
109 class SDWASrcOperand :
public SDWAOperand {
118 SdwaSel SrcSel_ =
DWORD,
bool Abs_ =
false,
bool Neg_ =
false,
120 : SDWAOperand(TargetOp, ReplacedOp), SrcSel(SrcSel_), Abs(Abs_),
121 Neg(Neg_), Sext(Sext_) {}
126 SdwaSel getSrcSel()
const {
return SrcSel; }
127 bool getAbs()
const {
return Abs; }
128 bool getNeg()
const {
return Neg; }
129 bool getSext()
const {
return Sext; }
135 class SDWADstOperand :
public SDWAOperand {
143 : SDWAOperand(TargetOp, ReplacedOp), DstSel(DstSel_), DstUn(DstUn_) {}
148 SdwaSel getDstSel()
const {
return DstSel; }
149 DstUnused getDstUnused()
const {
return DstUn; }
156 char SIPeepholeSDWA::
ID = 0;
161 return new SIPeepholeSDWA();
168 case BYTE_0: OS <<
"BYTE_0";
break;
169 case BYTE_1: OS <<
"BYTE_1";
break;
170 case BYTE_2: OS <<
"BYTE_2";
break;
171 case BYTE_3: OS <<
"BYTE_3";
break;
172 case WORD_0: OS <<
"WORD_0";
break;
173 case WORD_1: OS <<
"WORD_1";
break;
174 case DWORD: OS <<
"DWORD";
break;
189 OS <<
"SDWA src: " << *Src.getTargetOperand()
190 <<
" src_sel:" << Src.getSrcSel()
191 <<
" abs:" << Src.getAbs() <<
" neg:" << Src.getNeg()
192 <<
" sext:" << Src.getSext() <<
'\n';
197 OS <<
"SDWA dst: " << *Dst.getTargetOperand()
198 <<
" dst_sel:" << Dst.getDstSel()
199 <<
" dst_unused:" << Dst.getDstUnused() <<
'\n';
218 return LHS.
isReg() &&
239 SuperMask |= ~SubMask;
240 return SuperMask.
all();
249 Mods = Mod->getImm();
253 Mods = Mod->getImm();
258 "Float and integer src modifiers can't be set simulteniously");
287 if (PotentialMI ==
nullptr) {
289 }
else if (PotentialMI != PotentialMO.getParent()) {
306 if (!
isSameReg(*Src, *getReplacedOperand())) {
314 if ((MI.
getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
315 MI.
getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
316 !
isSameReg(*Src, *getReplacedOperand())) {
325 SrcSel->
setImm(getSrcSel());
326 SrcMods->
setImm(getSrcMods(TII, Src));
327 getTargetOperand()->setIsKill(
false);
349 UseMO.getParent() != ParentMI) {
355 return PotentialMO.getParent();
364 if ((MI.
getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
365 MI.
getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
374 isSameReg(*Operand, *getReplacedOperand()));
378 DstSel->
setImm(getDstSel());
381 DstUnused->
setImm(getDstUnused());
385 getParentInst()->eraseFromParent();
421 case AMDGPU::V_LSHRREV_B32_e32:
422 case AMDGPU::V_ASHRREV_I32_e32:
423 case AMDGPU::V_LSHLREV_B32_e32:
424 case AMDGPU::V_LSHRREV_B32_e64:
425 case AMDGPU::V_ASHRREV_I32_e64:
426 case AMDGPU::V_LSHLREV_B32_e64: {
436 auto Imm = foldToImm(*Src0);
440 if (*Imm != 16 && *Imm != 24)
445 if (TRI->isPhysicalRegister(Src1->
getReg()) ||
446 TRI->isPhysicalRegister(Dst->
getReg()))
449 if (Opcode == AMDGPU::V_LSHLREV_B32_e32 ||
450 Opcode == AMDGPU::V_LSHLREV_B32_e64) {
451 auto SDWADst = make_unique<SDWADstOperand>(
453 DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *SDWADst <<
'\n');
454 SDWAOperands[&
MI] = std::move(SDWADst);
455 ++NumSDWAPatternsFound;
457 auto SDWASrc = make_unique<SDWASrcOperand>(
459 Opcode != AMDGPU::V_LSHRREV_B32_e32 &&
460 Opcode != AMDGPU::V_LSHRREV_B32_e64);
461 DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *SDWASrc <<
'\n');
462 SDWAOperands[&
MI] = std::move(SDWASrc);
463 ++NumSDWAPatternsFound;
468 case AMDGPU::V_LSHRREV_B16_e32:
469 case AMDGPU::V_ASHRREV_I16_e32:
470 case AMDGPU::V_LSHLREV_B16_e32:
471 case AMDGPU::V_LSHRREV_B16_e64:
472 case AMDGPU::V_ASHRREV_I16_e64:
473 case AMDGPU::V_LSHLREV_B16_e64: {
483 auto Imm = foldToImm(*Src0);
484 if (!Imm || *Imm != 8)
490 if (TRI->isPhysicalRegister(Src1->
getReg()) ||
491 TRI->isPhysicalRegister(Dst->
getReg()))
494 if (Opcode == AMDGPU::V_LSHLREV_B16_e32 ||
495 Opcode == AMDGPU::V_LSHLREV_B16_e64) {
498 DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *SDWADst <<
'\n');
499 SDWAOperands[&
MI] = std::move(SDWADst);
500 ++NumSDWAPatternsFound;
502 auto SDWASrc = make_unique<SDWASrcOperand>(
503 Src1, Dst,
BYTE_1,
false,
false,
504 Opcode != AMDGPU::V_LSHRREV_B16_e32 &&
505 Opcode != AMDGPU::V_LSHRREV_B16_e64);
506 DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *SDWASrc <<
'\n');
507 SDWAOperands[&
MI] = std::move(SDWASrc);
508 ++NumSDWAPatternsFound;
513 case AMDGPU::V_BFE_I32:
514 case AMDGPU::V_BFE_U32: {
530 auto Offset = foldToImm(*Src1);
535 auto Width = foldToImm(*Src2);
541 if (*
Offset == 0 && *Width == 8)
543 else if (*
Offset == 0 && *Width == 16)
545 else if (*
Offset == 0 && *Width == 32)
547 else if (*
Offset == 8 && *Width == 8)
549 else if (*
Offset == 16 && *Width == 8)
551 else if (*
Offset == 16 && *Width == 16)
553 else if (*
Offset == 24 && *Width == 8)
561 if (TRI->isPhysicalRegister(Src0->
getReg()) ||
562 TRI->isPhysicalRegister(Dst->
getReg()))
565 auto SDWASrc = make_unique<SDWASrcOperand>(
566 Src0, Dst, SrcSel,
false,
false,
567 Opcode == AMDGPU::V_BFE_U32 ?
false :
true);
568 DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *SDWASrc <<
'\n');
569 SDWAOperands[&
MI] = std::move(SDWASrc);
570 ++NumSDWAPatternsFound;
573 case AMDGPU::V_AND_B32_e32:
574 case AMDGPU::V_AND_B32_e64: {
582 auto Imm = foldToImm(*Src0);
585 Imm = foldToImm(*Src1);
589 if (!Imm || (*Imm != 0x0000ffff && *Imm != 0x000000ff))
594 if (TRI->isPhysicalRegister(Src1->
getReg()) ||
595 TRI->isPhysicalRegister(Dst->
getReg()))
598 auto SDWASrc = make_unique<SDWASrcOperand>(
600 DEBUG(
dbgs() <<
"Match: " << MI <<
"To: " << *SDWASrc <<
'\n');
601 SDWAOperands[&
MI] = std::move(SDWASrc);
602 ++NumSDWAPatternsFound;
610 bool SIPeepholeSDWA::isConvertibleToSDWA(
const MachineInstr &MI,
626 if (SDst && SDst->
getReg() != AMDGPU::VCC)
640 if (!ST.
hasSDWAMac() && (Opc == AMDGPU::V_MAC_F16_e32 ||
641 Opc == AMDGPU::V_MAC_F32_e32))
648 const SDWAOperandsVector &SDWAOperands) {
651 if (SDWAOpcode == -1)
655 const MCInstrDesc &SDWADesc = TII->get(SDWAOpcode);
682 if (
auto *Mod = TII->
getNamedOperand(MI, AMDGPU::OpName::src0_modifiers))
683 SDWAInst.
addImm(Mod->getImm());
694 if (
auto *Mod = TII->
getNamedOperand(MI, AMDGPU::OpName::src1_modifiers))
695 SDWAInst.
addImm(Mod->getImm());
701 if (SDWAOpcode == AMDGPU::V_MAC_F16_sdwa ||
702 SDWAOpcode == AMDGPU::V_MAC_F32_sdwa) {
713 SDWAInst.
add(*Clamp);
750 bool Converted =
false;
751 for (
auto &Operand : SDWAOperands) {
762 if (PotentialMatches.count(Operand->getParentInst()) == 0)
763 Converted |= Operand->convertToSDWA(*SDWAInst, TII);
766 ConvertedInstructions.push_back(SDWAInst);
772 DEBUG(
dbgs() <<
"Convert instruction:" << MI
773 <<
"Into:" << *SDWAInst <<
'\n');
774 ++NumSDWAInstructionsPeepholed;
784 unsigned ConstantBusCount = 0;
795 TRI->isSGPRReg(*MRI, Op.
getReg())) {
802 TII->get(AMDGPU::V_MOV_B32_e32), VGPR);
823 matchSDWAOperands(MF);
825 for (
const auto &OperandPair : SDWAOperands) {
826 const auto &Operand = OperandPair.second;
827 MachineInstr *PotentialMI = Operand->potentialToConvert(TII);
828 if (PotentialMI && isConvertibleToSDWA(*PotentialMI, ST)) {
829 PotentialMatches[PotentialMI].push_back(Operand.get());
833 for (
auto &PotentialPair : PotentialMatches) {
835 convertToSDWA(PotentialMI, PotentialPair.second);
838 PotentialMatches.clear();
839 SDWAOperands.clear();
841 bool Ret = !ConvertedInstructions.empty();
842 while (!ConvertedInstructions.empty())
843 legalizeScalarOperands(*ConvertedInstructions.pop_back_val(),
ST);
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()
STATISTIC(NumFunctions,"Total number of functions")
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...
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
iterator_range< use_iterator > use_operands(unsigned Reg) const
Describe properties that are true of each instruction in the target description file.
void setIsUndef(bool Val=true)
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
const MachineInstrBuilder & add(const MachineOperand &MO) const
const SIInstrInfo * getInstrInfo() const override
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
static bool isSameReg(const MachineOperand &LHS, const MachineOperand &RHS)
void setIsDead(bool Val=true)
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx. ...
bool hasSDWAScalar() const
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
const TargetRegisterInfo * getTargetRegisterInfo() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool hasSDWAOutModsVOPC() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
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.
unsigned const MachineRegisterInfo * MRI
static void copyRegOperand(MachineOperand &To, const MachineOperand &From)
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void initializeSIPeepholeSDWAPass(PassRegistry &)
Represent the analysis usage information of a pass.
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
self_iterator getIterator()
static bool isSubregOf(const MachineOperand &SubReg, const MachineOperand &SuperReg, const TargetRegisterInfo *TRI)
unsigned getSubReg() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
iterator_range< mop_iterator > explicit_uses()
void setIsKill(bool Val=true)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
LLVM_READONLY int getVOPe32(uint16_t Opcode)
const SIRegisterInfo * getRegisterInfo() const override
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...
static const char * Target
void setPreservesCFG()
This function should be called by the pass, iff they do not:
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Target - Wrapper for Target specific information.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
bool isFoldableCopy(const MachineInstr &MI) const
Representation of each machine instruction.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
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)
constexpr bool all() const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasModifiersSet(const MachineInstr &MI, unsigned OpName) const
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 MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.