xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/SIPeepholeSDWA.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===- SIPeepholeSDWA.cpp - Peephole optimization for SDWA instructions ---===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric /// \file This pass tries to apply several peephole SDWA patterns.
100b57cec5SDimitry Andric ///
110b57cec5SDimitry Andric /// E.g. original:
120b57cec5SDimitry Andric ///   V_LSHRREV_B32_e32 %0, 16, %1
13e8d8bef9SDimitry Andric ///   V_ADD_CO_U32_e32 %2, %0, %3
140b57cec5SDimitry Andric ///   V_LSHLREV_B32_e32 %4, 16, %2
150b57cec5SDimitry Andric ///
160b57cec5SDimitry Andric /// Replace:
17e8d8bef9SDimitry Andric ///   V_ADD_CO_U32_sdwa %4, %1, %3
180b57cec5SDimitry Andric ///       dst_sel:WORD_1 dst_unused:UNUSED_PAD src0_sel:WORD_1 src1_sel:DWORD
190b57cec5SDimitry Andric ///
200b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #include "AMDGPU.h"
23e8d8bef9SDimitry Andric #include "GCNSubtarget.h"
240b57cec5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
25480093f4SDimitry Andric #include "llvm/ADT/MapVector.h"
260b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
28bdd1243dSDimitry Andric #include <optional>
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define DEBUG_TYPE "si-peephole-sdwa"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric STATISTIC(NumSDWAPatternsFound, "Number of SDWA patterns found.");
350b57cec5SDimitry Andric STATISTIC(NumSDWAInstructionsPeepholed,
360b57cec5SDimitry Andric           "Number of instruction converted to SDWA.");
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric namespace {
390b57cec5SDimitry Andric 
40*0fca6ea1SDimitry Andric bool isConvertibleToSDWA(MachineInstr &MI, const GCNSubtarget &ST,
41*0fca6ea1SDimitry Andric                          const SIInstrInfo *TII);
420b57cec5SDimitry Andric class SDWAOperand;
430b57cec5SDimitry Andric class SDWADstOperand;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric using SDWAOperandsVector = SmallVector<SDWAOperand *, 4>;
46*0fca6ea1SDimitry Andric using SDWAOperandsMap = MapVector<MachineInstr *, SDWAOperandsVector>;
470b57cec5SDimitry Andric 
48*0fca6ea1SDimitry Andric class SIPeepholeSDWA : public MachineFunctionPass {
490b57cec5SDimitry Andric private:
500b57cec5SDimitry Andric   MachineRegisterInfo *MRI;
510b57cec5SDimitry Andric   const SIRegisterInfo *TRI;
520b57cec5SDimitry Andric   const SIInstrInfo *TII;
530b57cec5SDimitry Andric 
54480093f4SDimitry Andric   MapVector<MachineInstr *, std::unique_ptr<SDWAOperand>> SDWAOperands;
55*0fca6ea1SDimitry Andric   SDWAOperandsMap PotentialMatches;
560b57cec5SDimitry Andric   SmallVector<MachineInstr *, 8> ConvertedInstructions;
570b57cec5SDimitry Andric 
58bdd1243dSDimitry Andric   std::optional<int64_t> foldToImm(const MachineOperand &Op) const;
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric public:
610b57cec5SDimitry Andric   static char ID;
620b57cec5SDimitry Andric 
SIPeepholeSDWA()630b57cec5SDimitry Andric   SIPeepholeSDWA() : MachineFunctionPass(ID) {
640b57cec5SDimitry Andric     initializeSIPeepholeSDWAPass(*PassRegistry::getPassRegistry());
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
680b57cec5SDimitry Andric   void matchSDWAOperands(MachineBasicBlock &MBB);
690b57cec5SDimitry Andric   std::unique_ptr<SDWAOperand> matchSDWAOperand(MachineInstr &MI);
700b57cec5SDimitry Andric   void pseudoOpConvertToVOP2(MachineInstr &MI,
710b57cec5SDimitry Andric                              const GCNSubtarget &ST) const;
720b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SDWAOperandsVector &SDWAOperands);
730b57cec5SDimitry Andric   void legalizeScalarOperands(MachineInstr &MI, const GCNSubtarget &ST) const;
740b57cec5SDimitry Andric 
getPassName() const750b57cec5SDimitry Andric   StringRef getPassName() const override { return "SI Peephole SDWA"; }
760b57cec5SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const770b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
780b57cec5SDimitry Andric     AU.setPreservesCFG();
790b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
800b57cec5SDimitry Andric   }
810b57cec5SDimitry Andric };
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric class SDWAOperand {
840b57cec5SDimitry Andric private:
850b57cec5SDimitry Andric   MachineOperand *Target; // Operand that would be used in converted instruction
860b57cec5SDimitry Andric   MachineOperand *Replaced; // Operand that would be replace by Target
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric public:
SDWAOperand(MachineOperand * TargetOp,MachineOperand * ReplacedOp)890b57cec5SDimitry Andric   SDWAOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp)
900b57cec5SDimitry Andric       : Target(TargetOp), Replaced(ReplacedOp) {
910b57cec5SDimitry Andric     assert(Target->isReg());
920b57cec5SDimitry Andric     assert(Replaced->isReg());
930b57cec5SDimitry Andric   }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   virtual ~SDWAOperand() = default;
960b57cec5SDimitry Andric 
97*0fca6ea1SDimitry Andric   virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII,
98*0fca6ea1SDimitry Andric                                            const GCNSubtarget &ST,
99*0fca6ea1SDimitry Andric                                            SDWAOperandsMap *PotentialMatches = nullptr) = 0;
1000b57cec5SDimitry Andric   virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) = 0;
1010b57cec5SDimitry Andric 
getTargetOperand() const1020b57cec5SDimitry Andric   MachineOperand *getTargetOperand() const { return Target; }
getReplacedOperand() const1030b57cec5SDimitry Andric   MachineOperand *getReplacedOperand() const { return Replaced; }
getParentInst() const1040b57cec5SDimitry Andric   MachineInstr *getParentInst() const { return Target->getParent(); }
1050b57cec5SDimitry Andric 
getMRI() const1060b57cec5SDimitry Andric   MachineRegisterInfo *getMRI() const {
1070b57cec5SDimitry Andric     return &getParentInst()->getParent()->getParent()->getRegInfo();
1080b57cec5SDimitry Andric   }
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1110b57cec5SDimitry Andric   virtual void print(raw_ostream& OS) const = 0;
dump() const1120b57cec5SDimitry Andric   void dump() const { print(dbgs()); }
1130b57cec5SDimitry Andric #endif
1140b57cec5SDimitry Andric };
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric using namespace AMDGPU::SDWA;
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric class SDWASrcOperand : public SDWAOperand {
1190b57cec5SDimitry Andric private:
1200b57cec5SDimitry Andric   SdwaSel SrcSel;
1210b57cec5SDimitry Andric   bool Abs;
1220b57cec5SDimitry Andric   bool Neg;
1230b57cec5SDimitry Andric   bool Sext;
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric public:
SDWASrcOperand(MachineOperand * TargetOp,MachineOperand * ReplacedOp,SdwaSel SrcSel_=DWORD,bool Abs_=false,bool Neg_=false,bool Sext_=false)1260b57cec5SDimitry Andric   SDWASrcOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
1270b57cec5SDimitry Andric                  SdwaSel SrcSel_ = DWORD, bool Abs_ = false, bool Neg_ = false,
1280b57cec5SDimitry Andric                  bool Sext_ = false)
1290b57cec5SDimitry Andric       : SDWAOperand(TargetOp, ReplacedOp),
1300b57cec5SDimitry Andric         SrcSel(SrcSel_), Abs(Abs_), Neg(Neg_), Sext(Sext_) {}
1310b57cec5SDimitry Andric 
132*0fca6ea1SDimitry Andric   MachineInstr *potentialToConvert(const SIInstrInfo *TII,
133*0fca6ea1SDimitry Andric                                    const GCNSubtarget &ST,
134*0fca6ea1SDimitry Andric                                    SDWAOperandsMap *PotentialMatches = nullptr) override;
1350b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
1360b57cec5SDimitry Andric 
getSrcSel() const1370b57cec5SDimitry Andric   SdwaSel getSrcSel() const { return SrcSel; }
getAbs() const1380b57cec5SDimitry Andric   bool getAbs() const { return Abs; }
getNeg() const1390b57cec5SDimitry Andric   bool getNeg() const { return Neg; }
getSext() const1400b57cec5SDimitry Andric   bool getSext() const { return Sext; }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   uint64_t getSrcMods(const SIInstrInfo *TII,
1430b57cec5SDimitry Andric                       const MachineOperand *SrcOp) const;
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1460b57cec5SDimitry Andric   void print(raw_ostream& OS) const override;
1470b57cec5SDimitry Andric #endif
1480b57cec5SDimitry Andric };
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric class SDWADstOperand : public SDWAOperand {
1510b57cec5SDimitry Andric private:
1520b57cec5SDimitry Andric   SdwaSel DstSel;
1530b57cec5SDimitry Andric   DstUnused DstUn;
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric public:
1560b57cec5SDimitry Andric 
SDWADstOperand(MachineOperand * TargetOp,MachineOperand * ReplacedOp,SdwaSel DstSel_=DWORD,DstUnused DstUn_=UNUSED_PAD)1570b57cec5SDimitry Andric   SDWADstOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
1580b57cec5SDimitry Andric                  SdwaSel DstSel_ = DWORD, DstUnused DstUn_ = UNUSED_PAD)
1590b57cec5SDimitry Andric     : SDWAOperand(TargetOp, ReplacedOp), DstSel(DstSel_), DstUn(DstUn_) {}
1600b57cec5SDimitry Andric 
161*0fca6ea1SDimitry Andric   MachineInstr *potentialToConvert(const SIInstrInfo *TII,
162*0fca6ea1SDimitry Andric                                    const GCNSubtarget &ST,
163*0fca6ea1SDimitry Andric                                    SDWAOperandsMap *PotentialMatches = nullptr) override;
1640b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
1650b57cec5SDimitry Andric 
getDstSel() const1660b57cec5SDimitry Andric   SdwaSel getDstSel() const { return DstSel; }
getDstUnused() const1670b57cec5SDimitry Andric   DstUnused getDstUnused() const { return DstUn; }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1700b57cec5SDimitry Andric   void print(raw_ostream& OS) const override;
1710b57cec5SDimitry Andric #endif
1720b57cec5SDimitry Andric };
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric class SDWADstPreserveOperand : public SDWADstOperand {
1750b57cec5SDimitry Andric private:
1760b57cec5SDimitry Andric   MachineOperand *Preserve;
1770b57cec5SDimitry Andric 
1780b57cec5SDimitry Andric public:
SDWADstPreserveOperand(MachineOperand * TargetOp,MachineOperand * ReplacedOp,MachineOperand * PreserveOp,SdwaSel DstSel_=DWORD)1790b57cec5SDimitry Andric   SDWADstPreserveOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
1800b57cec5SDimitry Andric                          MachineOperand *PreserveOp, SdwaSel DstSel_ = DWORD)
1810b57cec5SDimitry Andric       : SDWADstOperand(TargetOp, ReplacedOp, DstSel_, UNUSED_PRESERVE),
1820b57cec5SDimitry Andric         Preserve(PreserveOp) {}
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
1850b57cec5SDimitry Andric 
getPreservedOperand() const1860b57cec5SDimitry Andric   MachineOperand *getPreservedOperand() const { return Preserve; }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1890b57cec5SDimitry Andric   void print(raw_ostream& OS) const override;
1900b57cec5SDimitry Andric #endif
1910b57cec5SDimitry Andric };
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric } // end anonymous namespace
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric INITIALIZE_PASS(SIPeepholeSDWA, DEBUG_TYPE, "SI Peephole SDWA", false, false)
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric char SIPeepholeSDWA::ID = 0;
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric char &llvm::SIPeepholeSDWAID = SIPeepholeSDWA::ID;
2000b57cec5SDimitry Andric 
createSIPeepholeSDWAPass()2010b57cec5SDimitry Andric FunctionPass *llvm::createSIPeepholeSDWAPass() {
2020b57cec5SDimitry Andric   return new SIPeepholeSDWA();
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
operator <<(raw_ostream & OS,SdwaSel Sel)2070b57cec5SDimitry Andric static raw_ostream& operator<<(raw_ostream &OS, SdwaSel Sel) {
2080b57cec5SDimitry Andric   switch(Sel) {
2090b57cec5SDimitry Andric   case BYTE_0: OS << "BYTE_0"; break;
2100b57cec5SDimitry Andric   case BYTE_1: OS << "BYTE_1"; break;
2110b57cec5SDimitry Andric   case BYTE_2: OS << "BYTE_2"; break;
2120b57cec5SDimitry Andric   case BYTE_3: OS << "BYTE_3"; break;
2130b57cec5SDimitry Andric   case WORD_0: OS << "WORD_0"; break;
2140b57cec5SDimitry Andric   case WORD_1: OS << "WORD_1"; break;
2150b57cec5SDimitry Andric   case DWORD:  OS << "DWORD"; break;
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric   return OS;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
operator <<(raw_ostream & OS,const DstUnused & Un)2200b57cec5SDimitry Andric static raw_ostream& operator<<(raw_ostream &OS, const DstUnused &Un) {
2210b57cec5SDimitry Andric   switch(Un) {
2220b57cec5SDimitry Andric   case UNUSED_PAD: OS << "UNUSED_PAD"; break;
2230b57cec5SDimitry Andric   case UNUSED_SEXT: OS << "UNUSED_SEXT"; break;
2240b57cec5SDimitry Andric   case UNUSED_PRESERVE: OS << "UNUSED_PRESERVE"; break;
2250b57cec5SDimitry Andric   }
2260b57cec5SDimitry Andric   return OS;
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric LLVM_DUMP_METHOD
print(raw_ostream & OS) const2300b57cec5SDimitry Andric void SDWASrcOperand::print(raw_ostream& OS) const {
2310b57cec5SDimitry Andric   OS << "SDWA src: " << *getTargetOperand()
2320b57cec5SDimitry Andric     << " src_sel:" << getSrcSel()
2330b57cec5SDimitry Andric     << " abs:" << getAbs() << " neg:" << getNeg()
2340b57cec5SDimitry Andric     << " sext:" << getSext() << '\n';
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric LLVM_DUMP_METHOD
print(raw_ostream & OS) const2380b57cec5SDimitry Andric void SDWADstOperand::print(raw_ostream& OS) const {
2390b57cec5SDimitry Andric   OS << "SDWA dst: " << *getTargetOperand()
2400b57cec5SDimitry Andric     << " dst_sel:" << getDstSel()
2410b57cec5SDimitry Andric     << " dst_unused:" << getDstUnused() << '\n';
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric LLVM_DUMP_METHOD
print(raw_ostream & OS) const2450b57cec5SDimitry Andric void SDWADstPreserveOperand::print(raw_ostream& OS) const {
2460b57cec5SDimitry Andric   OS << "SDWA preserve dst: " << *getTargetOperand()
2470b57cec5SDimitry Andric     << " dst_sel:" << getDstSel()
2480b57cec5SDimitry Andric     << " preserve:" << *getPreservedOperand() << '\n';
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric #endif
2520b57cec5SDimitry Andric 
copyRegOperand(MachineOperand & To,const MachineOperand & From)2530b57cec5SDimitry Andric static void copyRegOperand(MachineOperand &To, const MachineOperand &From) {
2540b57cec5SDimitry Andric   assert(To.isReg() && From.isReg());
2550b57cec5SDimitry Andric   To.setReg(From.getReg());
2560b57cec5SDimitry Andric   To.setSubReg(From.getSubReg());
2570b57cec5SDimitry Andric   To.setIsUndef(From.isUndef());
2580b57cec5SDimitry Andric   if (To.isUse()) {
2590b57cec5SDimitry Andric     To.setIsKill(From.isKill());
2600b57cec5SDimitry Andric   } else {
2610b57cec5SDimitry Andric     To.setIsDead(From.isDead());
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
isSameReg(const MachineOperand & LHS,const MachineOperand & RHS)2650b57cec5SDimitry Andric static bool isSameReg(const MachineOperand &LHS, const MachineOperand &RHS) {
2660b57cec5SDimitry Andric   return LHS.isReg() &&
2670b57cec5SDimitry Andric          RHS.isReg() &&
2680b57cec5SDimitry Andric          LHS.getReg() == RHS.getReg() &&
2690b57cec5SDimitry Andric          LHS.getSubReg() == RHS.getSubReg();
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric 
findSingleRegUse(const MachineOperand * Reg,const MachineRegisterInfo * MRI)2720b57cec5SDimitry Andric static MachineOperand *findSingleRegUse(const MachineOperand *Reg,
2730b57cec5SDimitry Andric                                         const MachineRegisterInfo *MRI) {
2740b57cec5SDimitry Andric   if (!Reg->isReg() || !Reg->isDef())
2750b57cec5SDimitry Andric     return nullptr;
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   MachineOperand *ResMO = nullptr;
2780b57cec5SDimitry Andric   for (MachineOperand &UseMO : MRI->use_nodbg_operands(Reg->getReg())) {
2790b57cec5SDimitry Andric     // If there exist use of subreg of Reg then return nullptr
2800b57cec5SDimitry Andric     if (!isSameReg(UseMO, *Reg))
2810b57cec5SDimitry Andric       return nullptr;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric     // Check that there is only one instruction that uses Reg
2840b57cec5SDimitry Andric     if (!ResMO) {
2850b57cec5SDimitry Andric       ResMO = &UseMO;
2860b57cec5SDimitry Andric     } else if (ResMO->getParent() != UseMO.getParent()) {
2870b57cec5SDimitry Andric       return nullptr;
2880b57cec5SDimitry Andric     }
2890b57cec5SDimitry Andric   }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric   return ResMO;
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric 
findSingleRegDef(const MachineOperand * Reg,const MachineRegisterInfo * MRI)2940b57cec5SDimitry Andric static MachineOperand *findSingleRegDef(const MachineOperand *Reg,
2950b57cec5SDimitry Andric                                         const MachineRegisterInfo *MRI) {
2960b57cec5SDimitry Andric   if (!Reg->isReg())
2970b57cec5SDimitry Andric     return nullptr;
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   MachineInstr *DefInstr = MRI->getUniqueVRegDef(Reg->getReg());
3000b57cec5SDimitry Andric   if (!DefInstr)
3010b57cec5SDimitry Andric     return nullptr;
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   for (auto &DefMO : DefInstr->defs()) {
3040b57cec5SDimitry Andric     if (DefMO.isReg() && DefMO.getReg() == Reg->getReg())
3050b57cec5SDimitry Andric       return &DefMO;
3060b57cec5SDimitry Andric   }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric   // Ignore implicit defs.
3090b57cec5SDimitry Andric   return nullptr;
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric 
getSrcMods(const SIInstrInfo * TII,const MachineOperand * SrcOp) const3120b57cec5SDimitry Andric uint64_t SDWASrcOperand::getSrcMods(const SIInstrInfo *TII,
3130b57cec5SDimitry Andric                                     const MachineOperand *SrcOp) const {
3140b57cec5SDimitry Andric   uint64_t Mods = 0;
3150b57cec5SDimitry Andric   const auto *MI = SrcOp->getParent();
3160b57cec5SDimitry Andric   if (TII->getNamedOperand(*MI, AMDGPU::OpName::src0) == SrcOp) {
3170b57cec5SDimitry Andric     if (auto *Mod = TII->getNamedOperand(*MI, AMDGPU::OpName::src0_modifiers)) {
3180b57cec5SDimitry Andric       Mods = Mod->getImm();
3190b57cec5SDimitry Andric     }
3200b57cec5SDimitry Andric   } else if (TII->getNamedOperand(*MI, AMDGPU::OpName::src1) == SrcOp) {
3210b57cec5SDimitry Andric     if (auto *Mod = TII->getNamedOperand(*MI, AMDGPU::OpName::src1_modifiers)) {
3220b57cec5SDimitry Andric       Mods = Mod->getImm();
3230b57cec5SDimitry Andric     }
3240b57cec5SDimitry Andric   }
3250b57cec5SDimitry Andric   if (Abs || Neg) {
3260b57cec5SDimitry Andric     assert(!Sext &&
32781ad6265SDimitry Andric            "Float and integer src modifiers can't be set simultaneously");
3280b57cec5SDimitry Andric     Mods |= Abs ? SISrcMods::ABS : 0u;
3290b57cec5SDimitry Andric     Mods ^= Neg ? SISrcMods::NEG : 0u;
3300b57cec5SDimitry Andric   } else if (Sext) {
3310b57cec5SDimitry Andric     Mods |= SISrcMods::SEXT;
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   return Mods;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
potentialToConvert(const SIInstrInfo * TII,const GCNSubtarget & ST,SDWAOperandsMap * PotentialMatches)337*0fca6ea1SDimitry Andric MachineInstr *SDWASrcOperand::potentialToConvert(const SIInstrInfo *TII,
338*0fca6ea1SDimitry Andric                                                  const GCNSubtarget &ST,
339*0fca6ea1SDimitry Andric                                                  SDWAOperandsMap *PotentialMatches) {
340*0fca6ea1SDimitry Andric   if (PotentialMatches != nullptr) {
341*0fca6ea1SDimitry Andric     // Fill out the map for all uses if all can be converted
342*0fca6ea1SDimitry Andric     MachineOperand *Reg = getReplacedOperand();
343*0fca6ea1SDimitry Andric     if (!Reg->isReg() || !Reg->isDef())
344*0fca6ea1SDimitry Andric       return nullptr;
345*0fca6ea1SDimitry Andric 
346*0fca6ea1SDimitry Andric     for (MachineInstr &UseMI : getMRI()->use_nodbg_instructions(Reg->getReg()))
347*0fca6ea1SDimitry Andric       // Check that all instructions that use Reg can be converted
348*0fca6ea1SDimitry Andric       if (!isConvertibleToSDWA(UseMI, ST, TII))
349*0fca6ea1SDimitry Andric         return nullptr;
350*0fca6ea1SDimitry Andric 
351*0fca6ea1SDimitry Andric     // Now that it's guaranteed all uses are legal, iterate over the uses again
352*0fca6ea1SDimitry Andric     // to add them for later conversion.
353*0fca6ea1SDimitry Andric     for (MachineOperand &UseMO : getMRI()->use_nodbg_operands(Reg->getReg())) {
354*0fca6ea1SDimitry Andric       // Should not get a subregister here
355*0fca6ea1SDimitry Andric       assert(isSameReg(UseMO, *Reg));
356*0fca6ea1SDimitry Andric 
357*0fca6ea1SDimitry Andric       SDWAOperandsMap &potentialMatchesMap = *PotentialMatches;
358*0fca6ea1SDimitry Andric       MachineInstr *UseMI = UseMO.getParent();
359*0fca6ea1SDimitry Andric       potentialMatchesMap[UseMI].push_back(this);
360*0fca6ea1SDimitry Andric     }
361*0fca6ea1SDimitry Andric     return nullptr;
362*0fca6ea1SDimitry Andric   }
363*0fca6ea1SDimitry Andric 
3640b57cec5SDimitry Andric   // For SDWA src operand potential instruction is one that use register
3650b57cec5SDimitry Andric   // defined by parent instruction
3660b57cec5SDimitry Andric   MachineOperand *PotentialMO = findSingleRegUse(getReplacedOperand(), getMRI());
3670b57cec5SDimitry Andric   if (!PotentialMO)
3680b57cec5SDimitry Andric     return nullptr;
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   return PotentialMO->getParent();
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric 
convertToSDWA(MachineInstr & MI,const SIInstrInfo * TII)3730b57cec5SDimitry Andric bool SDWASrcOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
374*0fca6ea1SDimitry Andric   switch (MI.getOpcode()) {
375*0fca6ea1SDimitry Andric   case AMDGPU::V_CVT_F32_FP8_sdwa:
376*0fca6ea1SDimitry Andric   case AMDGPU::V_CVT_F32_BF8_sdwa:
377*0fca6ea1SDimitry Andric   case AMDGPU::V_CVT_PK_F32_FP8_sdwa:
378*0fca6ea1SDimitry Andric   case AMDGPU::V_CVT_PK_F32_BF8_sdwa:
379*0fca6ea1SDimitry Andric     // Does not support input modifiers: noabs, noneg, nosext.
380*0fca6ea1SDimitry Andric     return false;
381*0fca6ea1SDimitry Andric   }
382*0fca6ea1SDimitry Andric 
3830b57cec5SDimitry Andric   // Find operand in instruction that matches source operand and replace it with
3840b57cec5SDimitry Andric   // target operand. Set corresponding src_sel
3850b57cec5SDimitry Andric   bool IsPreserveSrc = false;
3860b57cec5SDimitry Andric   MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
3870b57cec5SDimitry Andric   MachineOperand *SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src0_sel);
3880b57cec5SDimitry Andric   MachineOperand *SrcMods =
3890b57cec5SDimitry Andric       TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
3900b57cec5SDimitry Andric   assert(Src && (Src->isReg() || Src->isImm()));
3910b57cec5SDimitry Andric   if (!isSameReg(*Src, *getReplacedOperand())) {
3920b57cec5SDimitry Andric     // If this is not src0 then it could be src1
3930b57cec5SDimitry Andric     Src = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
3940b57cec5SDimitry Andric     SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src1_sel);
3950b57cec5SDimitry Andric     SrcMods = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric     if (!Src ||
3980b57cec5SDimitry Andric         !isSameReg(*Src, *getReplacedOperand())) {
3990b57cec5SDimitry Andric       // It's possible this Src is a tied operand for
4000b57cec5SDimitry Andric       // UNUSED_PRESERVE, in which case we can either
4010b57cec5SDimitry Andric       // abandon the peephole attempt, or if legal we can
4020b57cec5SDimitry Andric       // copy the target operand into the tied slot
4030b57cec5SDimitry Andric       // if the preserve operation will effectively cause the same
4040b57cec5SDimitry Andric       // result by overwriting the rest of the dst.
4050b57cec5SDimitry Andric       MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
4060b57cec5SDimitry Andric       MachineOperand *DstUnused =
4070b57cec5SDimitry Andric         TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric       if (Dst &&
4100b57cec5SDimitry Andric           DstUnused->getImm() == AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE) {
411349cc55cSDimitry Andric         // This will work if the tied src is accessing WORD_0, and the dst is
4120b57cec5SDimitry Andric         // writing WORD_1. Modifiers don't matter because all the bits that
4130b57cec5SDimitry Andric         // would be impacted are being overwritten by the dst.
4140b57cec5SDimitry Andric         // Any other case will not work.
4150b57cec5SDimitry Andric         SdwaSel DstSel = static_cast<SdwaSel>(
4160b57cec5SDimitry Andric             TII->getNamedImmOperand(MI, AMDGPU::OpName::dst_sel));
4170b57cec5SDimitry Andric         if (DstSel == AMDGPU::SDWA::SdwaSel::WORD_1 &&
4180b57cec5SDimitry Andric             getSrcSel() == AMDGPU::SDWA::SdwaSel::WORD_0) {
4190b57cec5SDimitry Andric           IsPreserveSrc = true;
4200b57cec5SDimitry Andric           auto DstIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
4210b57cec5SDimitry Andric                                                    AMDGPU::OpName::vdst);
4220b57cec5SDimitry Andric           auto TiedIdx = MI.findTiedOperandIdx(DstIdx);
4230b57cec5SDimitry Andric           Src = &MI.getOperand(TiedIdx);
4240b57cec5SDimitry Andric           SrcSel = nullptr;
4250b57cec5SDimitry Andric           SrcMods = nullptr;
4260b57cec5SDimitry Andric         } else {
4270b57cec5SDimitry Andric           // Not legal to convert this src
4280b57cec5SDimitry Andric           return false;
4290b57cec5SDimitry Andric         }
4300b57cec5SDimitry Andric       }
4310b57cec5SDimitry Andric     }
4320b57cec5SDimitry Andric     assert(Src && Src->isReg());
4330b57cec5SDimitry Andric 
4340b57cec5SDimitry Andric     if ((MI.getOpcode() == AMDGPU::V_FMAC_F16_sdwa ||
4350b57cec5SDimitry Andric          MI.getOpcode() == AMDGPU::V_FMAC_F32_sdwa ||
4360b57cec5SDimitry Andric          MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
4370b57cec5SDimitry Andric          MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
4380b57cec5SDimitry Andric          !isSameReg(*Src, *getReplacedOperand())) {
4390b57cec5SDimitry Andric       // In case of v_mac_f16/32_sdwa this pass can try to apply src operand to
4400b57cec5SDimitry Andric       // src2. This is not allowed.
4410b57cec5SDimitry Andric       return false;
4420b57cec5SDimitry Andric     }
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric     assert(isSameReg(*Src, *getReplacedOperand()) &&
4450b57cec5SDimitry Andric            (IsPreserveSrc || (SrcSel && SrcMods)));
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric   copyRegOperand(*Src, *getTargetOperand());
4480b57cec5SDimitry Andric   if (!IsPreserveSrc) {
4490b57cec5SDimitry Andric     SrcSel->setImm(getSrcSel());
4500b57cec5SDimitry Andric     SrcMods->setImm(getSrcMods(TII, Src));
4510b57cec5SDimitry Andric   }
4520b57cec5SDimitry Andric   getTargetOperand()->setIsKill(false);
4530b57cec5SDimitry Andric   return true;
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric 
potentialToConvert(const SIInstrInfo * TII,const GCNSubtarget & ST,SDWAOperandsMap * PotentialMatches)456*0fca6ea1SDimitry Andric MachineInstr *SDWADstOperand::potentialToConvert(const SIInstrInfo *TII,
457*0fca6ea1SDimitry Andric                                                  const GCNSubtarget &ST,
458*0fca6ea1SDimitry Andric                                                  SDWAOperandsMap *PotentialMatches) {
4590b57cec5SDimitry Andric   // For SDWA dst operand potential instruction is one that defines register
4600b57cec5SDimitry Andric   // that this operand uses
4610b57cec5SDimitry Andric   MachineRegisterInfo *MRI = getMRI();
4620b57cec5SDimitry Andric   MachineInstr *ParentMI = getParentInst();
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   MachineOperand *PotentialMO = findSingleRegDef(getReplacedOperand(), MRI);
4650b57cec5SDimitry Andric   if (!PotentialMO)
4660b57cec5SDimitry Andric     return nullptr;
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric   // Check that ParentMI is the only instruction that uses replaced register
4690b57cec5SDimitry Andric   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(PotentialMO->getReg())) {
4700b57cec5SDimitry Andric     if (&UseInst != ParentMI)
4710b57cec5SDimitry Andric       return nullptr;
4720b57cec5SDimitry Andric   }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   return PotentialMO->getParent();
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric 
convertToSDWA(MachineInstr & MI,const SIInstrInfo * TII)4770b57cec5SDimitry Andric bool SDWADstOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
4780b57cec5SDimitry Andric   // Replace vdst operand in MI with target operand. Set dst_sel and dst_unused
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   if ((MI.getOpcode() == AMDGPU::V_FMAC_F16_sdwa ||
4810b57cec5SDimitry Andric        MI.getOpcode() == AMDGPU::V_FMAC_F32_sdwa ||
4820b57cec5SDimitry Andric        MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
4830b57cec5SDimitry Andric        MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
4840b57cec5SDimitry Andric       getDstSel() != AMDGPU::SDWA::DWORD) {
4850b57cec5SDimitry Andric     // v_mac_f16/32_sdwa allow dst_sel to be equal only to DWORD
4860b57cec5SDimitry Andric     return false;
4870b57cec5SDimitry Andric   }
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   MachineOperand *Operand = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
4900b57cec5SDimitry Andric   assert(Operand &&
4910b57cec5SDimitry Andric          Operand->isReg() &&
4920b57cec5SDimitry Andric          isSameReg(*Operand, *getReplacedOperand()));
4930b57cec5SDimitry Andric   copyRegOperand(*Operand, *getTargetOperand());
4940b57cec5SDimitry Andric   MachineOperand *DstSel= TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel);
4950b57cec5SDimitry Andric   assert(DstSel);
4960b57cec5SDimitry Andric   DstSel->setImm(getDstSel());
4970b57cec5SDimitry Andric   MachineOperand *DstUnused= TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
4980b57cec5SDimitry Andric   assert(DstUnused);
4990b57cec5SDimitry Andric   DstUnused->setImm(getDstUnused());
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   // Remove original instruction  because it would conflict with our new
5020b57cec5SDimitry Andric   // instruction by register definition
5030b57cec5SDimitry Andric   getParentInst()->eraseFromParent();
5040b57cec5SDimitry Andric   return true;
5050b57cec5SDimitry Andric }
5060b57cec5SDimitry Andric 
convertToSDWA(MachineInstr & MI,const SIInstrInfo * TII)5070b57cec5SDimitry Andric bool SDWADstPreserveOperand::convertToSDWA(MachineInstr &MI,
5080b57cec5SDimitry Andric                                            const SIInstrInfo *TII) {
5090b57cec5SDimitry Andric   // MI should be moved right before v_or_b32.
5100b57cec5SDimitry Andric   // For this we should clear all kill flags on uses of MI src-operands or else
5110b57cec5SDimitry Andric   // we can encounter problem with use of killed operand.
5120b57cec5SDimitry Andric   for (MachineOperand &MO : MI.uses()) {
5130b57cec5SDimitry Andric     if (!MO.isReg())
5140b57cec5SDimitry Andric       continue;
5150b57cec5SDimitry Andric     getMRI()->clearKillFlags(MO.getReg());
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric 
5180b57cec5SDimitry Andric   // Move MI before v_or_b32
519*0fca6ea1SDimitry Andric   MI.getParent()->remove(&MI);
520*0fca6ea1SDimitry Andric   getParentInst()->getParent()->insert(getParentInst(), &MI);
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   // Add Implicit use of preserved register
523*0fca6ea1SDimitry Andric   MachineInstrBuilder MIB(*MI.getMF(), MI);
5240b57cec5SDimitry Andric   MIB.addReg(getPreservedOperand()->getReg(),
5250b57cec5SDimitry Andric              RegState::ImplicitKill,
5260b57cec5SDimitry Andric              getPreservedOperand()->getSubReg());
5270b57cec5SDimitry Andric 
5280b57cec5SDimitry Andric   // Tie dst to implicit use
5290b57cec5SDimitry Andric   MI.tieOperands(AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst),
5300b57cec5SDimitry Andric                  MI.getNumOperands() - 1);
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   // Convert MI as any other SDWADstOperand and remove v_or_b32
5330b57cec5SDimitry Andric   return SDWADstOperand::convertToSDWA(MI, TII);
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric 
536bdd1243dSDimitry Andric std::optional<int64_t>
foldToImm(const MachineOperand & Op) const537bdd1243dSDimitry Andric SIPeepholeSDWA::foldToImm(const MachineOperand &Op) const {
5380b57cec5SDimitry Andric   if (Op.isImm()) {
5390b57cec5SDimitry Andric     return Op.getImm();
5400b57cec5SDimitry Andric   }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   // If this is not immediate then it can be copy of immediate value, e.g.:
5430b57cec5SDimitry Andric   // %1 = S_MOV_B32 255;
5440b57cec5SDimitry Andric   if (Op.isReg()) {
5450b57cec5SDimitry Andric     for (const MachineOperand &Def : MRI->def_operands(Op.getReg())) {
5460b57cec5SDimitry Andric       if (!isSameReg(Op, Def))
5470b57cec5SDimitry Andric         continue;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric       const MachineInstr *DefInst = Def.getParent();
5500b57cec5SDimitry Andric       if (!TII->isFoldableCopy(*DefInst))
551bdd1243dSDimitry Andric         return std::nullopt;
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric       const MachineOperand &Copied = DefInst->getOperand(1);
5540b57cec5SDimitry Andric       if (!Copied.isImm())
555bdd1243dSDimitry Andric         return std::nullopt;
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric       return Copied.getImm();
5580b57cec5SDimitry Andric     }
5590b57cec5SDimitry Andric   }
5600b57cec5SDimitry Andric 
561bdd1243dSDimitry Andric   return std::nullopt;
5620b57cec5SDimitry Andric }
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric std::unique_ptr<SDWAOperand>
matchSDWAOperand(MachineInstr & MI)5650b57cec5SDimitry Andric SIPeepholeSDWA::matchSDWAOperand(MachineInstr &MI) {
5660b57cec5SDimitry Andric   unsigned Opcode = MI.getOpcode();
5670b57cec5SDimitry Andric   switch (Opcode) {
5680b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B32_e32:
5690b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I32_e32:
5700b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B32_e32:
5710b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B32_e64:
5720b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I32_e64:
5730b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B32_e64: {
5740b57cec5SDimitry Andric     // from: v_lshrrev_b32_e32 v1, 16/24, v0
5750b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:WORD_1/BYTE_3
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric     // from: v_ashrrev_i32_e32 v1, 16/24, v0
5780b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:WORD_1/BYTE_3 sext:1
5790b57cec5SDimitry Andric 
5800b57cec5SDimitry Andric     // from: v_lshlrev_b32_e32 v1, 16/24, v0
5810b57cec5SDimitry Andric     // to SDWA dst:v1 dst_sel:WORD_1/BYTE_3 dst_unused:UNUSED_PAD
5820b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
5830b57cec5SDimitry Andric     auto Imm = foldToImm(*Src0);
5840b57cec5SDimitry Andric     if (!Imm)
5850b57cec5SDimitry Andric       break;
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric     if (*Imm != 16 && *Imm != 24)
5880b57cec5SDimitry Andric       break;
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
5910b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
5925f757f3fSDimitry Andric     if (!Src1->isReg() || Src1->getReg().isPhysical() ||
5935f757f3fSDimitry Andric         Dst->getReg().isPhysical())
5940b57cec5SDimitry Andric       break;
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric     if (Opcode == AMDGPU::V_LSHLREV_B32_e32 ||
5970b57cec5SDimitry Andric         Opcode == AMDGPU::V_LSHLREV_B32_e64) {
5988bcb0991SDimitry Andric       return std::make_unique<SDWADstOperand>(
5990b57cec5SDimitry Andric           Dst, Src1, *Imm == 16 ? WORD_1 : BYTE_3, UNUSED_PAD);
600*0fca6ea1SDimitry Andric     }
6018bcb0991SDimitry Andric     return std::make_unique<SDWASrcOperand>(
6020b57cec5SDimitry Andric         Src1, Dst, *Imm == 16 ? WORD_1 : BYTE_3, false, false,
6030b57cec5SDimitry Andric         Opcode != AMDGPU::V_LSHRREV_B32_e32 &&
6040b57cec5SDimitry Andric             Opcode != AMDGPU::V_LSHRREV_B32_e64);
6050b57cec5SDimitry Andric     break;
6060b57cec5SDimitry Andric   }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B16_e32:
6090b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I16_e32:
6100b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B16_e32:
6110b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B16_e64:
6120b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I16_e64:
6130b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B16_e64: {
6140b57cec5SDimitry Andric     // from: v_lshrrev_b16_e32 v1, 8, v0
6150b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:BYTE_1
6160b57cec5SDimitry Andric 
6170b57cec5SDimitry Andric     // from: v_ashrrev_i16_e32 v1, 8, v0
6180b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:BYTE_1 sext:1
6190b57cec5SDimitry Andric 
6200b57cec5SDimitry Andric     // from: v_lshlrev_b16_e32 v1, 8, v0
6210b57cec5SDimitry Andric     // to SDWA dst:v1 dst_sel:BYTE_1 dst_unused:UNUSED_PAD
6220b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
6230b57cec5SDimitry Andric     auto Imm = foldToImm(*Src0);
6240b57cec5SDimitry Andric     if (!Imm || *Imm != 8)
6250b57cec5SDimitry Andric       break;
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
6280b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
6290b57cec5SDimitry Andric 
6305f757f3fSDimitry Andric     if (!Src1->isReg() || Src1->getReg().isPhysical() ||
6315f757f3fSDimitry Andric         Dst->getReg().isPhysical())
6320b57cec5SDimitry Andric       break;
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric     if (Opcode == AMDGPU::V_LSHLREV_B16_e32 ||
635*0fca6ea1SDimitry Andric         Opcode == AMDGPU::V_LSHLREV_B16_e64)
6368bcb0991SDimitry Andric       return std::make_unique<SDWADstOperand>(Dst, Src1, BYTE_1, UNUSED_PAD);
6378bcb0991SDimitry Andric     return std::make_unique<SDWASrcOperand>(
6380b57cec5SDimitry Andric         Src1, Dst, BYTE_1, false, false,
6390b57cec5SDimitry Andric         Opcode != AMDGPU::V_LSHRREV_B16_e32 &&
6400b57cec5SDimitry Andric             Opcode != AMDGPU::V_LSHRREV_B16_e64);
6410b57cec5SDimitry Andric     break;
6420b57cec5SDimitry Andric   }
6430b57cec5SDimitry Andric 
644e8d8bef9SDimitry Andric   case AMDGPU::V_BFE_I32_e64:
645e8d8bef9SDimitry Andric   case AMDGPU::V_BFE_U32_e64: {
6460b57cec5SDimitry Andric     // e.g.:
6470b57cec5SDimitry Andric     // from: v_bfe_u32 v1, v0, 8, 8
6480b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:BYTE_1
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric     // offset | width | src_sel
6510b57cec5SDimitry Andric     // ------------------------
6520b57cec5SDimitry Andric     // 0      | 8     | BYTE_0
6530b57cec5SDimitry Andric     // 0      | 16    | WORD_0
6540b57cec5SDimitry Andric     // 0      | 32    | DWORD ?
6550b57cec5SDimitry Andric     // 8      | 8     | BYTE_1
6560b57cec5SDimitry Andric     // 16     | 8     | BYTE_2
6570b57cec5SDimitry Andric     // 16     | 16    | WORD_1
6580b57cec5SDimitry Andric     // 24     | 8     | BYTE_3
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
6610b57cec5SDimitry Andric     auto Offset = foldToImm(*Src1);
6620b57cec5SDimitry Andric     if (!Offset)
6630b57cec5SDimitry Andric       break;
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric     MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
6660b57cec5SDimitry Andric     auto Width = foldToImm(*Src2);
6670b57cec5SDimitry Andric     if (!Width)
6680b57cec5SDimitry Andric       break;
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric     SdwaSel SrcSel = DWORD;
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric     if (*Offset == 0 && *Width == 8)
6730b57cec5SDimitry Andric       SrcSel = BYTE_0;
6740b57cec5SDimitry Andric     else if (*Offset == 0 && *Width == 16)
6750b57cec5SDimitry Andric       SrcSel = WORD_0;
6760b57cec5SDimitry Andric     else if (*Offset == 0 && *Width == 32)
6770b57cec5SDimitry Andric       SrcSel = DWORD;
6780b57cec5SDimitry Andric     else if (*Offset == 8 && *Width == 8)
6790b57cec5SDimitry Andric       SrcSel = BYTE_1;
6800b57cec5SDimitry Andric     else if (*Offset == 16 && *Width == 8)
6810b57cec5SDimitry Andric       SrcSel = BYTE_2;
6820b57cec5SDimitry Andric     else if (*Offset == 16 && *Width == 16)
6830b57cec5SDimitry Andric       SrcSel = WORD_1;
6840b57cec5SDimitry Andric     else if (*Offset == 24 && *Width == 8)
6850b57cec5SDimitry Andric       SrcSel = BYTE_3;
6860b57cec5SDimitry Andric     else
6870b57cec5SDimitry Andric       break;
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
6900b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
6910b57cec5SDimitry Andric 
6925f757f3fSDimitry Andric     if (!Src0->isReg() || Src0->getReg().isPhysical() ||
6935f757f3fSDimitry Andric         Dst->getReg().isPhysical())
6940b57cec5SDimitry Andric       break;
6950b57cec5SDimitry Andric 
6968bcb0991SDimitry Andric     return std::make_unique<SDWASrcOperand>(
697e8d8bef9SDimitry Andric           Src0, Dst, SrcSel, false, false, Opcode != AMDGPU::V_BFE_U32_e64);
6980b57cec5SDimitry Andric   }
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric   case AMDGPU::V_AND_B32_e32:
7010b57cec5SDimitry Andric   case AMDGPU::V_AND_B32_e64: {
7020b57cec5SDimitry Andric     // e.g.:
7030b57cec5SDimitry Andric     // from: v_and_b32_e32 v1, 0x0000ffff/0x000000ff, v0
7040b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:WORD_0/BYTE_0
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
7070b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
7080b57cec5SDimitry Andric     auto ValSrc = Src1;
7090b57cec5SDimitry Andric     auto Imm = foldToImm(*Src0);
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric     if (!Imm) {
7120b57cec5SDimitry Andric       Imm = foldToImm(*Src1);
7130b57cec5SDimitry Andric       ValSrc = Src0;
7140b57cec5SDimitry Andric     }
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric     if (!Imm || (*Imm != 0x0000ffff && *Imm != 0x000000ff))
7170b57cec5SDimitry Andric       break;
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
7200b57cec5SDimitry Andric 
7215f757f3fSDimitry Andric     if (!ValSrc->isReg() || ValSrc->getReg().isPhysical() ||
7225f757f3fSDimitry Andric         Dst->getReg().isPhysical())
7230b57cec5SDimitry Andric       break;
7240b57cec5SDimitry Andric 
7258bcb0991SDimitry Andric     return std::make_unique<SDWASrcOperand>(
7260b57cec5SDimitry Andric         ValSrc, Dst, *Imm == 0x0000ffff ? WORD_0 : BYTE_0);
7270b57cec5SDimitry Andric   }
7280b57cec5SDimitry Andric 
7290b57cec5SDimitry Andric   case AMDGPU::V_OR_B32_e32:
7300b57cec5SDimitry Andric   case AMDGPU::V_OR_B32_e64: {
7310b57cec5SDimitry Andric     // Patterns for dst_unused:UNUSED_PRESERVE.
7320b57cec5SDimitry Andric     // e.g., from:
7330b57cec5SDimitry Andric     // v_add_f16_sdwa v0, v1, v2 dst_sel:WORD_1 dst_unused:UNUSED_PAD
7340b57cec5SDimitry Andric     //                           src1_sel:WORD_1 src2_sel:WORD1
7350b57cec5SDimitry Andric     // v_add_f16_e32 v3, v1, v2
7360b57cec5SDimitry Andric     // v_or_b32_e32 v4, v0, v3
7370b57cec5SDimitry Andric     // to SDWA preserve dst:v4 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE preserve:v3
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric     // Check if one of operands of v_or_b32 is SDWA instruction
740bdd1243dSDimitry Andric     using CheckRetType =
741bdd1243dSDimitry Andric         std::optional<std::pair<MachineOperand *, MachineOperand *>>;
7420b57cec5SDimitry Andric     auto CheckOROperandsForSDWA =
7430b57cec5SDimitry Andric       [&](const MachineOperand *Op1, const MachineOperand *Op2) -> CheckRetType {
7440b57cec5SDimitry Andric         if (!Op1 || !Op1->isReg() || !Op2 || !Op2->isReg())
745bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric         MachineOperand *Op1Def = findSingleRegDef(Op1, MRI);
7480b57cec5SDimitry Andric         if (!Op1Def)
749bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric         MachineInstr *Op1Inst = Op1Def->getParent();
7520b57cec5SDimitry Andric         if (!TII->isSDWA(*Op1Inst))
753bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric         MachineOperand *Op2Def = findSingleRegDef(Op2, MRI);
7560b57cec5SDimitry Andric         if (!Op2Def)
757bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7580b57cec5SDimitry Andric 
759bdd1243dSDimitry Andric         return CheckRetType(std::pair(Op1Def, Op2Def));
7600b57cec5SDimitry Andric       };
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric     MachineOperand *OrSDWA = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
7630b57cec5SDimitry Andric     MachineOperand *OrOther = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
7640b57cec5SDimitry Andric     assert(OrSDWA && OrOther);
7650b57cec5SDimitry Andric     auto Res = CheckOROperandsForSDWA(OrSDWA, OrOther);
7660b57cec5SDimitry Andric     if (!Res) {
7670b57cec5SDimitry Andric       OrSDWA = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
7680b57cec5SDimitry Andric       OrOther = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
7690b57cec5SDimitry Andric       assert(OrSDWA && OrOther);
7700b57cec5SDimitry Andric       Res = CheckOROperandsForSDWA(OrSDWA, OrOther);
7710b57cec5SDimitry Andric       if (!Res)
7720b57cec5SDimitry Andric         break;
7730b57cec5SDimitry Andric     }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric     MachineOperand *OrSDWADef = Res->first;
7760b57cec5SDimitry Andric     MachineOperand *OrOtherDef = Res->second;
7770b57cec5SDimitry Andric     assert(OrSDWADef && OrOtherDef);
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric     MachineInstr *SDWAInst = OrSDWADef->getParent();
7800b57cec5SDimitry Andric     MachineInstr *OtherInst = OrOtherDef->getParent();
7810b57cec5SDimitry Andric 
7820b57cec5SDimitry Andric     // Check that OtherInstr is actually bitwise compatible with SDWAInst = their
7830b57cec5SDimitry Andric     // destination patterns don't overlap. Compatible instruction can be either
7840b57cec5SDimitry Andric     // regular instruction with compatible bitness or SDWA instruction with
7850b57cec5SDimitry Andric     // correct dst_sel
7860b57cec5SDimitry Andric     // SDWAInst | OtherInst bitness / OtherInst dst_sel
7870b57cec5SDimitry Andric     // -----------------------------------------------------
7880b57cec5SDimitry Andric     // DWORD    | no                    / no
7890b57cec5SDimitry Andric     // WORD_0   | no                    / BYTE_2/3, WORD_1
7900b57cec5SDimitry Andric     // WORD_1   | 8/16-bit instructions / BYTE_0/1, WORD_0
7910b57cec5SDimitry Andric     // BYTE_0   | no                    / BYTE_1/2/3, WORD_1
7920b57cec5SDimitry Andric     // BYTE_1   | 8-bit                 / BYTE_0/2/3, WORD_1
7930b57cec5SDimitry Andric     // BYTE_2   | 8/16-bit              / BYTE_0/1/3. WORD_0
7940b57cec5SDimitry Andric     // BYTE_3   | 8/16/24-bit           / BYTE_0/1/2, WORD_0
7950b57cec5SDimitry Andric     // E.g. if SDWAInst is v_add_f16_sdwa dst_sel:WORD_1 then v_add_f16 is OK
7960b57cec5SDimitry Andric     // but v_add_f32 is not.
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric     // TODO: add support for non-SDWA instructions as OtherInst.
7990b57cec5SDimitry Andric     // For now this only works with SDWA instructions. For regular instructions
8000b57cec5SDimitry Andric     // there is no way to determine if the instruction writes only 8/16/24-bit
8010b57cec5SDimitry Andric     // out of full register size and all registers are at min 32-bit wide.
8020b57cec5SDimitry Andric     if (!TII->isSDWA(*OtherInst))
8030b57cec5SDimitry Andric       break;
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric     SdwaSel DstSel = static_cast<SdwaSel>(
80606c3fb27SDimitry Andric         TII->getNamedImmOperand(*SDWAInst, AMDGPU::OpName::dst_sel));
8070b57cec5SDimitry Andric     SdwaSel OtherDstSel = static_cast<SdwaSel>(
8080b57cec5SDimitry Andric       TII->getNamedImmOperand(*OtherInst, AMDGPU::OpName::dst_sel));
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric     bool DstSelAgree = false;
8110b57cec5SDimitry Andric     switch (DstSel) {
8120b57cec5SDimitry Andric     case WORD_0: DstSelAgree = ((OtherDstSel == BYTE_2) ||
8130b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
8140b57cec5SDimitry Andric                                 (OtherDstSel == WORD_1));
8150b57cec5SDimitry Andric       break;
8160b57cec5SDimitry Andric     case WORD_1: DstSelAgree = ((OtherDstSel == BYTE_0) ||
8170b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_1) ||
8180b57cec5SDimitry Andric                                 (OtherDstSel == WORD_0));
8190b57cec5SDimitry Andric       break;
8200b57cec5SDimitry Andric     case BYTE_0: DstSelAgree = ((OtherDstSel == BYTE_1) ||
8210b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_2) ||
8220b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
8230b57cec5SDimitry Andric                                 (OtherDstSel == WORD_1));
8240b57cec5SDimitry Andric       break;
8250b57cec5SDimitry Andric     case BYTE_1: DstSelAgree = ((OtherDstSel == BYTE_0) ||
8260b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_2) ||
8270b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
8280b57cec5SDimitry Andric                                 (OtherDstSel == WORD_1));
8290b57cec5SDimitry Andric       break;
8300b57cec5SDimitry Andric     case BYTE_2: DstSelAgree = ((OtherDstSel == BYTE_0) ||
8310b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_1) ||
8320b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
8330b57cec5SDimitry Andric                                 (OtherDstSel == WORD_0));
8340b57cec5SDimitry Andric       break;
8350b57cec5SDimitry Andric     case BYTE_3: DstSelAgree = ((OtherDstSel == BYTE_0) ||
8360b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_1) ||
8370b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_2) ||
8380b57cec5SDimitry Andric                                 (OtherDstSel == WORD_0));
8390b57cec5SDimitry Andric       break;
8400b57cec5SDimitry Andric     default: DstSelAgree = false;
8410b57cec5SDimitry Andric     }
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric     if (!DstSelAgree)
8440b57cec5SDimitry Andric       break;
8450b57cec5SDimitry Andric 
8460b57cec5SDimitry Andric     // Also OtherInst dst_unused should be UNUSED_PAD
8470b57cec5SDimitry Andric     DstUnused OtherDstUnused = static_cast<DstUnused>(
8480b57cec5SDimitry Andric       TII->getNamedImmOperand(*OtherInst, AMDGPU::OpName::dst_unused));
8490b57cec5SDimitry Andric     if (OtherDstUnused != DstUnused::UNUSED_PAD)
8500b57cec5SDimitry Andric       break;
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric     // Create DstPreserveOperand
8530b57cec5SDimitry Andric     MachineOperand *OrDst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
8540b57cec5SDimitry Andric     assert(OrDst && OrDst->isReg());
8550b57cec5SDimitry Andric 
8568bcb0991SDimitry Andric     return std::make_unique<SDWADstPreserveOperand>(
8570b57cec5SDimitry Andric       OrDst, OrSDWADef, OrOtherDef, DstSel);
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric   }
8600b57cec5SDimitry Andric   }
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   return std::unique_ptr<SDWAOperand>(nullptr);
8630b57cec5SDimitry Andric }
8640b57cec5SDimitry Andric 
86547395794SDimitry Andric #if !defined(NDEBUG)
operator <<(raw_ostream & OS,const SDWAOperand & Operand)86647395794SDimitry Andric static raw_ostream& operator<<(raw_ostream &OS, const SDWAOperand &Operand) {
86747395794SDimitry Andric   Operand.print(OS);
86847395794SDimitry Andric   return OS;
86947395794SDimitry Andric }
87047395794SDimitry Andric #endif
87147395794SDimitry Andric 
matchSDWAOperands(MachineBasicBlock & MBB)8720b57cec5SDimitry Andric void SIPeepholeSDWA::matchSDWAOperands(MachineBasicBlock &MBB) {
8730b57cec5SDimitry Andric   for (MachineInstr &MI : MBB) {
8740b57cec5SDimitry Andric     if (auto Operand = matchSDWAOperand(MI)) {
8750b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Match: " << MI << "To: " << *Operand << '\n');
8760b57cec5SDimitry Andric       SDWAOperands[&MI] = std::move(Operand);
8770b57cec5SDimitry Andric       ++NumSDWAPatternsFound;
8780b57cec5SDimitry Andric     }
8790b57cec5SDimitry Andric   }
8800b57cec5SDimitry Andric }
8810b57cec5SDimitry Andric 
882bdd1243dSDimitry Andric // Convert the V_ADD_CO_U32_e64 into V_ADD_CO_U32_e32. This allows
883bdd1243dSDimitry Andric // isConvertibleToSDWA to perform its transformation on V_ADD_CO_U32_e32 into
884bdd1243dSDimitry Andric // V_ADD_CO_U32_sdwa.
8850b57cec5SDimitry Andric //
8860b57cec5SDimitry Andric // We are transforming from a VOP3 into a VOP2 form of the instruction.
8870b57cec5SDimitry Andric //   %19:vgpr_32 = V_AND_B32_e32 255,
8880b57cec5SDimitry Andric //       killed %16:vgpr_32, implicit $exec
889e8d8bef9SDimitry Andric //   %47:vgpr_32, %49:sreg_64_xexec = V_ADD_CO_U32_e64
8900b57cec5SDimitry Andric //       %26.sub0:vreg_64, %19:vgpr_32, implicit $exec
8910b57cec5SDimitry Andric //  %48:vgpr_32, dead %50:sreg_64_xexec = V_ADDC_U32_e64
8920b57cec5SDimitry Andric //       %26.sub1:vreg_64, %54:vgpr_32, killed %49:sreg_64_xexec, implicit $exec
8930b57cec5SDimitry Andric //
8940b57cec5SDimitry Andric // becomes
895e8d8bef9SDimitry Andric //   %47:vgpr_32 = V_ADD_CO_U32_sdwa
8960b57cec5SDimitry Andric //       0, %26.sub0:vreg_64, 0, killed %16:vgpr_32, 0, 6, 0, 6, 0,
8970b57cec5SDimitry Andric //       implicit-def $vcc, implicit $exec
898bdd1243dSDimitry Andric //  %48:vgpr_32, dead %50:sreg_64_xexec = V_ADDC_U32_e64
899bdd1243dSDimitry Andric //       %26.sub1:vreg_64, %54:vgpr_32, killed $vcc, implicit $exec
pseudoOpConvertToVOP2(MachineInstr & MI,const GCNSubtarget & ST) const9000b57cec5SDimitry Andric void SIPeepholeSDWA::pseudoOpConvertToVOP2(MachineInstr &MI,
9010b57cec5SDimitry Andric                                            const GCNSubtarget &ST) const {
9020b57cec5SDimitry Andric   int Opc = MI.getOpcode();
903e8d8bef9SDimitry Andric   assert((Opc == AMDGPU::V_ADD_CO_U32_e64 || Opc == AMDGPU::V_SUB_CO_U32_e64) &&
904e8d8bef9SDimitry Andric          "Currently only handles V_ADD_CO_U32_e64 or V_SUB_CO_U32_e64");
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric   // Can the candidate MI be shrunk?
9070b57cec5SDimitry Andric   if (!TII->canShrink(MI, *MRI))
9080b57cec5SDimitry Andric     return;
9090b57cec5SDimitry Andric   Opc = AMDGPU::getVOPe32(Opc);
9100b57cec5SDimitry Andric   // Find the related ADD instruction.
9110b57cec5SDimitry Andric   const MachineOperand *Sdst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
9120b57cec5SDimitry Andric   if (!Sdst)
9130b57cec5SDimitry Andric     return;
9140b57cec5SDimitry Andric   MachineOperand *NextOp = findSingleRegUse(Sdst, MRI);
9150b57cec5SDimitry Andric   if (!NextOp)
9160b57cec5SDimitry Andric     return;
9170b57cec5SDimitry Andric   MachineInstr &MISucc = *NextOp->getParent();
918bdd1243dSDimitry Andric 
9190b57cec5SDimitry Andric   // Make sure the carry in/out are subsequently unused.
9200b57cec5SDimitry Andric   MachineOperand *CarryIn = TII->getNamedOperand(MISucc, AMDGPU::OpName::src2);
9210b57cec5SDimitry Andric   if (!CarryIn)
9220b57cec5SDimitry Andric     return;
9230b57cec5SDimitry Andric   MachineOperand *CarryOut = TII->getNamedOperand(MISucc, AMDGPU::OpName::sdst);
9240b57cec5SDimitry Andric   if (!CarryOut)
9250b57cec5SDimitry Andric     return;
9260b57cec5SDimitry Andric   if (!MRI->hasOneUse(CarryIn->getReg()) || !MRI->use_empty(CarryOut->getReg()))
9270b57cec5SDimitry Andric     return;
9280b57cec5SDimitry Andric   // Make sure VCC or its subregs are dead before MI.
9290b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
9300b57cec5SDimitry Andric   auto Liveness = MBB.computeRegisterLiveness(TRI, AMDGPU::VCC, MI, 25);
9310b57cec5SDimitry Andric   if (Liveness != MachineBasicBlock::LQR_Dead)
9320b57cec5SDimitry Andric     return;
9330b57cec5SDimitry Andric   // Check if VCC is referenced in range of (MI,MISucc].
9340b57cec5SDimitry Andric   for (auto I = std::next(MI.getIterator()), E = MISucc.getIterator();
9350b57cec5SDimitry Andric        I != E; ++I) {
9360b57cec5SDimitry Andric     if (I->modifiesRegister(AMDGPU::VCC, TRI))
9370b57cec5SDimitry Andric       return;
9380b57cec5SDimitry Andric   }
9395ffd83dbSDimitry Andric 
9400b57cec5SDimitry Andric   // Replace MI with V_{SUB|ADD}_I32_e32
9415ffd83dbSDimitry Andric   BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(Opc))
9425ffd83dbSDimitry Andric     .add(*TII->getNamedOperand(MI, AMDGPU::OpName::vdst))
9435ffd83dbSDimitry Andric     .add(*TII->getNamedOperand(MI, AMDGPU::OpName::src0))
9445ffd83dbSDimitry Andric     .add(*TII->getNamedOperand(MI, AMDGPU::OpName::src1))
9455ffd83dbSDimitry Andric     .setMIFlags(MI.getFlags());
9465ffd83dbSDimitry Andric 
9470b57cec5SDimitry Andric   MI.eraseFromParent();
9485ffd83dbSDimitry Andric 
949bdd1243dSDimitry Andric   // Since the carry output of MI is now VCC, update its use in MISucc.
9505ffd83dbSDimitry Andric 
951bdd1243dSDimitry Andric   MISucc.substituteRegister(CarryIn->getReg(), TRI->getVCC(), 0, *TRI);
9520b57cec5SDimitry Andric }
9530b57cec5SDimitry Andric 
954*0fca6ea1SDimitry Andric namespace {
isConvertibleToSDWA(MachineInstr & MI,const GCNSubtarget & ST,const SIInstrInfo * TII)955*0fca6ea1SDimitry Andric bool isConvertibleToSDWA(MachineInstr &MI,
956*0fca6ea1SDimitry Andric                          const GCNSubtarget &ST,
957*0fca6ea1SDimitry Andric                          const SIInstrInfo* TII) {
9580b57cec5SDimitry Andric   // Check if this is already an SDWA instruction
9590b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
9600b57cec5SDimitry Andric   if (TII->isSDWA(Opc))
9610b57cec5SDimitry Andric     return true;
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric   // Check if this instruction has opcode that supports SDWA
9640b57cec5SDimitry Andric   if (AMDGPU::getSDWAOp(Opc) == -1)
9650b57cec5SDimitry Andric     Opc = AMDGPU::getVOPe32(Opc);
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric   if (AMDGPU::getSDWAOp(Opc) == -1)
9680b57cec5SDimitry Andric     return false;
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   if (!ST.hasSDWAOmod() && TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
9710b57cec5SDimitry Andric     return false;
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric   if (TII->isVOPC(Opc)) {
9740b57cec5SDimitry Andric     if (!ST.hasSDWASdst()) {
9750b57cec5SDimitry Andric       const MachineOperand *SDst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
9760b57cec5SDimitry Andric       if (SDst && (SDst->getReg() != AMDGPU::VCC &&
9770b57cec5SDimitry Andric                    SDst->getReg() != AMDGPU::VCC_LO))
9780b57cec5SDimitry Andric         return false;
9790b57cec5SDimitry Andric     }
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric     if (!ST.hasSDWAOutModsVOPC() &&
9820b57cec5SDimitry Andric         (TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) ||
9830b57cec5SDimitry Andric          TII->hasModifiersSet(MI, AMDGPU::OpName::omod)))
9840b57cec5SDimitry Andric       return false;
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   } else if (TII->getNamedOperand(MI, AMDGPU::OpName::sdst) ||
9870b57cec5SDimitry Andric              !TII->getNamedOperand(MI, AMDGPU::OpName::vdst)) {
9880b57cec5SDimitry Andric     return false;
9890b57cec5SDimitry Andric   }
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   if (!ST.hasSDWAMac() && (Opc == AMDGPU::V_FMAC_F16_e32 ||
9920b57cec5SDimitry Andric                            Opc == AMDGPU::V_FMAC_F32_e32 ||
9930b57cec5SDimitry Andric                            Opc == AMDGPU::V_MAC_F16_e32 ||
9940b57cec5SDimitry Andric                            Opc == AMDGPU::V_MAC_F32_e32))
9950b57cec5SDimitry Andric     return false;
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   // Check if target supports this SDWA opcode
9980b57cec5SDimitry Andric   if (TII->pseudoToMCOpcode(Opc) == -1)
9990b57cec5SDimitry Andric     return false;
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric   // FIXME: has SDWA but require handling of implicit VCC use
10020b57cec5SDimitry Andric   if (Opc == AMDGPU::V_CNDMASK_B32_e32)
10030b57cec5SDimitry Andric     return false;
10040b57cec5SDimitry Andric 
1005e8d8bef9SDimitry Andric   if (MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0)) {
1006e8d8bef9SDimitry Andric     if (!Src0->isReg() && !Src0->isImm())
1007e8d8bef9SDimitry Andric       return false;
1008e8d8bef9SDimitry Andric   }
1009e8d8bef9SDimitry Andric 
1010e8d8bef9SDimitry Andric   if (MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1)) {
1011e8d8bef9SDimitry Andric     if (!Src1->isReg() && !Src1->isImm())
1012e8d8bef9SDimitry Andric       return false;
1013e8d8bef9SDimitry Andric   }
1014e8d8bef9SDimitry Andric 
10150b57cec5SDimitry Andric   return true;
10160b57cec5SDimitry Andric }
1017*0fca6ea1SDimitry Andric } // namespace
10180b57cec5SDimitry Andric 
convertToSDWA(MachineInstr & MI,const SDWAOperandsVector & SDWAOperands)10190b57cec5SDimitry Andric bool SIPeepholeSDWA::convertToSDWA(MachineInstr &MI,
10200b57cec5SDimitry Andric                                    const SDWAOperandsVector &SDWAOperands) {
10210b57cec5SDimitry Andric 
10220b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Convert instruction:" << MI);
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric   // Convert to sdwa
10250b57cec5SDimitry Andric   int SDWAOpcode;
10260b57cec5SDimitry Andric   unsigned Opcode = MI.getOpcode();
10270b57cec5SDimitry Andric   if (TII->isSDWA(Opcode)) {
10280b57cec5SDimitry Andric     SDWAOpcode = Opcode;
10290b57cec5SDimitry Andric   } else {
10300b57cec5SDimitry Andric     SDWAOpcode = AMDGPU::getSDWAOp(Opcode);
10310b57cec5SDimitry Andric     if (SDWAOpcode == -1)
10320b57cec5SDimitry Andric       SDWAOpcode = AMDGPU::getSDWAOp(AMDGPU::getVOPe32(Opcode));
10330b57cec5SDimitry Andric   }
10340b57cec5SDimitry Andric   assert(SDWAOpcode != -1);
10350b57cec5SDimitry Andric 
10360b57cec5SDimitry Andric   const MCInstrDesc &SDWADesc = TII->get(SDWAOpcode);
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric   // Create SDWA version of instruction MI and initialize its operands
10390b57cec5SDimitry Andric   MachineInstrBuilder SDWAInst =
10405ffd83dbSDimitry Andric     BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), SDWADesc)
10415ffd83dbSDimitry Andric     .setMIFlags(MI.getFlags());
10420b57cec5SDimitry Andric 
10430b57cec5SDimitry Andric   // Copy dst, if it is present in original then should also be present in SDWA
10440b57cec5SDimitry Andric   MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
10450b57cec5SDimitry Andric   if (Dst) {
1046bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::vdst));
10470b57cec5SDimitry Andric     SDWAInst.add(*Dst);
10480b57cec5SDimitry Andric   } else if ((Dst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst))) {
1049bdd1243dSDimitry Andric     assert(Dst && AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::sdst));
10500b57cec5SDimitry Andric     SDWAInst.add(*Dst);
10510b57cec5SDimitry Andric   } else {
1052bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::sdst));
10530b57cec5SDimitry Andric     SDWAInst.addReg(TRI->getVCC(), RegState::Define);
10540b57cec5SDimitry Andric   }
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric   // Copy src0, initialize src0_modifiers. All sdwa instructions has src0 and
10570b57cec5SDimitry Andric   // src0_modifiers (except for v_nop_sdwa, but it can't get here)
10580b57cec5SDimitry Andric   MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1059bdd1243dSDimitry Andric   assert(Src0 && AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src0) &&
1060bdd1243dSDimitry Andric          AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src0_modifiers));
10610b57cec5SDimitry Andric   if (auto *Mod = TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers))
10620b57cec5SDimitry Andric     SDWAInst.addImm(Mod->getImm());
10630b57cec5SDimitry Andric   else
10640b57cec5SDimitry Andric     SDWAInst.addImm(0);
10650b57cec5SDimitry Andric   SDWAInst.add(*Src0);
10660b57cec5SDimitry Andric 
10670b57cec5SDimitry Andric   // Copy src1 if present, initialize src1_modifiers.
10680b57cec5SDimitry Andric   MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
10690b57cec5SDimitry Andric   if (Src1) {
1070bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src1) &&
1071bdd1243dSDimitry Andric            AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src1_modifiers));
10720b57cec5SDimitry Andric     if (auto *Mod = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers))
10730b57cec5SDimitry Andric       SDWAInst.addImm(Mod->getImm());
10740b57cec5SDimitry Andric     else
10750b57cec5SDimitry Andric       SDWAInst.addImm(0);
10760b57cec5SDimitry Andric     SDWAInst.add(*Src1);
10770b57cec5SDimitry Andric   }
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   if (SDWAOpcode == AMDGPU::V_FMAC_F16_sdwa ||
10800b57cec5SDimitry Andric       SDWAOpcode == AMDGPU::V_FMAC_F32_sdwa ||
10810b57cec5SDimitry Andric       SDWAOpcode == AMDGPU::V_MAC_F16_sdwa ||
10820b57cec5SDimitry Andric       SDWAOpcode == AMDGPU::V_MAC_F32_sdwa) {
10830b57cec5SDimitry Andric     // v_mac_f16/32 has additional src2 operand tied to vdst
10840b57cec5SDimitry Andric     MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
10850b57cec5SDimitry Andric     assert(Src2);
10860b57cec5SDimitry Andric     SDWAInst.add(*Src2);
10870b57cec5SDimitry Andric   }
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   // Copy clamp if present, initialize otherwise
1090bdd1243dSDimitry Andric   assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::clamp));
10910b57cec5SDimitry Andric   MachineOperand *Clamp = TII->getNamedOperand(MI, AMDGPU::OpName::clamp);
10920b57cec5SDimitry Andric   if (Clamp) {
10930b57cec5SDimitry Andric     SDWAInst.add(*Clamp);
10940b57cec5SDimitry Andric   } else {
10950b57cec5SDimitry Andric     SDWAInst.addImm(0);
10960b57cec5SDimitry Andric   }
10970b57cec5SDimitry Andric 
10980b57cec5SDimitry Andric   // Copy omod if present, initialize otherwise if needed
1099bdd1243dSDimitry Andric   if (AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::omod)) {
11000b57cec5SDimitry Andric     MachineOperand *OMod = TII->getNamedOperand(MI, AMDGPU::OpName::omod);
11010b57cec5SDimitry Andric     if (OMod) {
11020b57cec5SDimitry Andric       SDWAInst.add(*OMod);
11030b57cec5SDimitry Andric     } else {
11040b57cec5SDimitry Andric       SDWAInst.addImm(0);
11050b57cec5SDimitry Andric     }
11060b57cec5SDimitry Andric   }
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric   // Copy dst_sel if present, initialize otherwise if needed
1109bdd1243dSDimitry Andric   if (AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::dst_sel)) {
11100b57cec5SDimitry Andric     MachineOperand *DstSel = TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel);
11110b57cec5SDimitry Andric     if (DstSel) {
11120b57cec5SDimitry Andric       SDWAInst.add(*DstSel);
11130b57cec5SDimitry Andric     } else {
11140b57cec5SDimitry Andric       SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
11150b57cec5SDimitry Andric     }
11160b57cec5SDimitry Andric   }
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric   // Copy dst_unused if present, initialize otherwise if needed
1119bdd1243dSDimitry Andric   if (AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::dst_unused)) {
11200b57cec5SDimitry Andric     MachineOperand *DstUnused = TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
11210b57cec5SDimitry Andric     if (DstUnused) {
11220b57cec5SDimitry Andric       SDWAInst.add(*DstUnused);
11230b57cec5SDimitry Andric     } else {
11240b57cec5SDimitry Andric       SDWAInst.addImm(AMDGPU::SDWA::DstUnused::UNUSED_PAD);
11250b57cec5SDimitry Andric     }
11260b57cec5SDimitry Andric   }
11270b57cec5SDimitry Andric 
11280b57cec5SDimitry Andric   // Copy src0_sel if present, initialize otherwise
1129bdd1243dSDimitry Andric   assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src0_sel));
11300b57cec5SDimitry Andric   MachineOperand *Src0Sel = TII->getNamedOperand(MI, AMDGPU::OpName::src0_sel);
11310b57cec5SDimitry Andric   if (Src0Sel) {
11320b57cec5SDimitry Andric     SDWAInst.add(*Src0Sel);
11330b57cec5SDimitry Andric   } else {
11340b57cec5SDimitry Andric     SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
11350b57cec5SDimitry Andric   }
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric   // Copy src1_sel if present, initialize otherwise if needed
11380b57cec5SDimitry Andric   if (Src1) {
1139bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src1_sel));
11400b57cec5SDimitry Andric     MachineOperand *Src1Sel = TII->getNamedOperand(MI, AMDGPU::OpName::src1_sel);
11410b57cec5SDimitry Andric     if (Src1Sel) {
11420b57cec5SDimitry Andric       SDWAInst.add(*Src1Sel);
11430b57cec5SDimitry Andric     } else {
11440b57cec5SDimitry Andric       SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
11450b57cec5SDimitry Andric     }
11460b57cec5SDimitry Andric   }
11470b57cec5SDimitry Andric 
11480b57cec5SDimitry Andric   // Check for a preserved register that needs to be copied.
11490b57cec5SDimitry Andric   auto DstUnused = TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
11500b57cec5SDimitry Andric   if (DstUnused &&
11510b57cec5SDimitry Andric       DstUnused->getImm() == AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE) {
11520b57cec5SDimitry Andric     // We expect, if we are here, that the instruction was already in it's SDWA form,
11530b57cec5SDimitry Andric     // with a tied operand.
11540b57cec5SDimitry Andric     assert(Dst && Dst->isTied());
11550b57cec5SDimitry Andric     assert(Opcode == static_cast<unsigned int>(SDWAOpcode));
11560b57cec5SDimitry Andric     // We also expect a vdst, since sdst can't preserve.
11570b57cec5SDimitry Andric     auto PreserveDstIdx = AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::vdst);
11580b57cec5SDimitry Andric     assert(PreserveDstIdx != -1);
11590b57cec5SDimitry Andric 
11600b57cec5SDimitry Andric     auto TiedIdx = MI.findTiedOperandIdx(PreserveDstIdx);
11610b57cec5SDimitry Andric     auto Tied = MI.getOperand(TiedIdx);
11620b57cec5SDimitry Andric 
11630b57cec5SDimitry Andric     SDWAInst.add(Tied);
11640b57cec5SDimitry Andric     SDWAInst->tieOperands(PreserveDstIdx, SDWAInst->getNumOperands() - 1);
11650b57cec5SDimitry Andric   }
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric   // Apply all sdwa operand patterns.
11680b57cec5SDimitry Andric   bool Converted = false;
11690b57cec5SDimitry Andric   for (auto &Operand : SDWAOperands) {
11700b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << *SDWAInst << "\nOperand: " << *Operand);
117181ad6265SDimitry Andric     // There should be no intersection between SDWA operands and potential MIs
11720b57cec5SDimitry Andric     // e.g.:
11730b57cec5SDimitry Andric     // v_and_b32 v0, 0xff, v1 -> src:v1 sel:BYTE_0
11740b57cec5SDimitry Andric     // v_and_b32 v2, 0xff, v0 -> src:v0 sel:BYTE_0
11750b57cec5SDimitry Andric     // v_add_u32 v3, v4, v2
11760b57cec5SDimitry Andric     //
117781ad6265SDimitry Andric     // In that example it is possible that we would fold 2nd instruction into
117881ad6265SDimitry Andric     // 3rd (v_add_u32_sdwa) and then try to fold 1st instruction into 2nd (that
117981ad6265SDimitry Andric     // was already destroyed). So if SDWAOperand is also a potential MI then do
118081ad6265SDimitry Andric     // not apply it.
11810b57cec5SDimitry Andric     if (PotentialMatches.count(Operand->getParentInst()) == 0)
11820b57cec5SDimitry Andric       Converted |= Operand->convertToSDWA(*SDWAInst, TII);
11830b57cec5SDimitry Andric   }
1184*0fca6ea1SDimitry Andric 
11850b57cec5SDimitry Andric   if (Converted) {
11860b57cec5SDimitry Andric     ConvertedInstructions.push_back(SDWAInst);
1187*0fca6ea1SDimitry Andric     for (MachineOperand &MO : SDWAInst->uses()) {
1188*0fca6ea1SDimitry Andric       if (!MO.isReg())
1189*0fca6ea1SDimitry Andric         continue;
1190*0fca6ea1SDimitry Andric 
1191*0fca6ea1SDimitry Andric       MRI->clearKillFlags(MO.getReg());
1192*0fca6ea1SDimitry Andric     }
11930b57cec5SDimitry Andric   } else {
11940b57cec5SDimitry Andric     SDWAInst->eraseFromParent();
11950b57cec5SDimitry Andric     return false;
11960b57cec5SDimitry Andric   }
11970b57cec5SDimitry Andric 
11980b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nInto:" << *SDWAInst << '\n');
11990b57cec5SDimitry Andric   ++NumSDWAInstructionsPeepholed;
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric   MI.eraseFromParent();
12020b57cec5SDimitry Andric   return true;
12030b57cec5SDimitry Andric }
12040b57cec5SDimitry Andric 
12050b57cec5SDimitry Andric // If an instruction was converted to SDWA it should not have immediates or SGPR
12060b57cec5SDimitry Andric // operands (allowed one SGPR on GFX9). Copy its scalar operands into VGPRs.
legalizeScalarOperands(MachineInstr & MI,const GCNSubtarget & ST) const12070b57cec5SDimitry Andric void SIPeepholeSDWA::legalizeScalarOperands(MachineInstr &MI,
12080b57cec5SDimitry Andric                                             const GCNSubtarget &ST) const {
12090b57cec5SDimitry Andric   const MCInstrDesc &Desc = TII->get(MI.getOpcode());
12100b57cec5SDimitry Andric   unsigned ConstantBusCount = 0;
12110b57cec5SDimitry Andric   for (MachineOperand &Op : MI.explicit_uses()) {
12120b57cec5SDimitry Andric     if (!Op.isImm() && !(Op.isReg() && !TRI->isVGPR(*MRI, Op.getReg())))
12130b57cec5SDimitry Andric       continue;
12140b57cec5SDimitry Andric 
121506c3fb27SDimitry Andric     unsigned I = Op.getOperandNo();
1216bdd1243dSDimitry Andric     if (Desc.operands()[I].RegClass == -1 ||
1217bdd1243dSDimitry Andric         !TRI->isVSSuperClass(TRI->getRegClass(Desc.operands()[I].RegClass)))
12180b57cec5SDimitry Andric       continue;
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric     if (ST.hasSDWAScalar() && ConstantBusCount == 0 && Op.isReg() &&
12210b57cec5SDimitry Andric         TRI->isSGPRReg(*MRI, Op.getReg())) {
12220b57cec5SDimitry Andric       ++ConstantBusCount;
12230b57cec5SDimitry Andric       continue;
12240b57cec5SDimitry Andric     }
12250b57cec5SDimitry Andric 
12268bcb0991SDimitry Andric     Register VGPR = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
12270b57cec5SDimitry Andric     auto Copy = BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
12280b57cec5SDimitry Andric                         TII->get(AMDGPU::V_MOV_B32_e32), VGPR);
12290b57cec5SDimitry Andric     if (Op.isImm())
12300b57cec5SDimitry Andric       Copy.addImm(Op.getImm());
12310b57cec5SDimitry Andric     else if (Op.isReg())
12320b57cec5SDimitry Andric       Copy.addReg(Op.getReg(), Op.isKill() ? RegState::Kill : 0,
12330b57cec5SDimitry Andric                   Op.getSubReg());
12340b57cec5SDimitry Andric     Op.ChangeToRegister(VGPR, false);
12350b57cec5SDimitry Andric   }
12360b57cec5SDimitry Andric }
12370b57cec5SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)12380b57cec5SDimitry Andric bool SIPeepholeSDWA::runOnMachineFunction(MachineFunction &MF) {
12390b57cec5SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
12400b57cec5SDimitry Andric 
12410b57cec5SDimitry Andric   if (!ST.hasSDWA() || skipFunction(MF.getFunction()))
12420b57cec5SDimitry Andric     return false;
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
12450b57cec5SDimitry Andric   TRI = ST.getRegisterInfo();
12460b57cec5SDimitry Andric   TII = ST.getInstrInfo();
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric   // Find all SDWA operands in MF.
12490b57cec5SDimitry Andric   bool Ret = false;
12500b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
12510b57cec5SDimitry Andric     bool Changed = false;
12520b57cec5SDimitry Andric     do {
12530b57cec5SDimitry Andric       // Preprocess the ADD/SUB pairs so they could be SDWA'ed.
12540b57cec5SDimitry Andric       // Look for a possible ADD or SUB that resulted from a previously lowered
12550b57cec5SDimitry Andric       // V_{ADD|SUB}_U64_PSEUDO. The function pseudoOpConvertToVOP2
12560b57cec5SDimitry Andric       // lowers the pair of instructions into e32 form.
12570b57cec5SDimitry Andric       matchSDWAOperands(MBB);
12580b57cec5SDimitry Andric       for (const auto &OperandPair : SDWAOperands) {
12590b57cec5SDimitry Andric         const auto &Operand = OperandPair.second;
1260*0fca6ea1SDimitry Andric         MachineInstr *PotentialMI = Operand->potentialToConvert(TII, ST);
12610b57cec5SDimitry Andric         if (PotentialMI &&
1262e8d8bef9SDimitry Andric            (PotentialMI->getOpcode() == AMDGPU::V_ADD_CO_U32_e64 ||
1263e8d8bef9SDimitry Andric             PotentialMI->getOpcode() == AMDGPU::V_SUB_CO_U32_e64))
12640b57cec5SDimitry Andric           pseudoOpConvertToVOP2(*PotentialMI, ST);
12650b57cec5SDimitry Andric       }
12660b57cec5SDimitry Andric       SDWAOperands.clear();
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric       // Generate potential match list.
12690b57cec5SDimitry Andric       matchSDWAOperands(MBB);
12700b57cec5SDimitry Andric 
12710b57cec5SDimitry Andric       for (const auto &OperandPair : SDWAOperands) {
12720b57cec5SDimitry Andric         const auto &Operand = OperandPair.second;
1273*0fca6ea1SDimitry Andric         MachineInstr *PotentialMI = Operand->potentialToConvert(TII, ST, &PotentialMatches);
1274*0fca6ea1SDimitry Andric         if (PotentialMI && isConvertibleToSDWA(*PotentialMI, ST, TII)) {
12750b57cec5SDimitry Andric           PotentialMatches[PotentialMI].push_back(Operand.get());
12760b57cec5SDimitry Andric         }
12770b57cec5SDimitry Andric       }
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric       for (auto &PotentialPair : PotentialMatches) {
12800b57cec5SDimitry Andric         MachineInstr &PotentialMI = *PotentialPair.first;
12810b57cec5SDimitry Andric         convertToSDWA(PotentialMI, PotentialPair.second);
12820b57cec5SDimitry Andric       }
12830b57cec5SDimitry Andric 
12840b57cec5SDimitry Andric       PotentialMatches.clear();
12850b57cec5SDimitry Andric       SDWAOperands.clear();
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric       Changed = !ConvertedInstructions.empty();
12880b57cec5SDimitry Andric 
12890b57cec5SDimitry Andric       if (Changed)
12900b57cec5SDimitry Andric         Ret = true;
12910b57cec5SDimitry Andric       while (!ConvertedInstructions.empty())
12920b57cec5SDimitry Andric         legalizeScalarOperands(*ConvertedInstructions.pop_back_val(), ST);
12930b57cec5SDimitry Andric     } while (Changed);
12940b57cec5SDimitry Andric   }
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric   return Ret;
12970b57cec5SDimitry Andric }
1298