xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/SIPeepholeSDWA.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 
400b57cec5SDimitry Andric class SDWAOperand;
410b57cec5SDimitry Andric class SDWADstOperand;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric class SIPeepholeSDWA : public MachineFunctionPass {
440b57cec5SDimitry Andric public:
450b57cec5SDimitry Andric   using SDWAOperandsVector = SmallVector<SDWAOperand *, 4>;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric private:
480b57cec5SDimitry Andric   MachineRegisterInfo *MRI;
490b57cec5SDimitry Andric   const SIRegisterInfo *TRI;
500b57cec5SDimitry Andric   const SIInstrInfo *TII;
510b57cec5SDimitry Andric 
52480093f4SDimitry Andric   MapVector<MachineInstr *, std::unique_ptr<SDWAOperand>> SDWAOperands;
53480093f4SDimitry Andric   MapVector<MachineInstr *, SDWAOperandsVector> PotentialMatches;
540b57cec5SDimitry Andric   SmallVector<MachineInstr *, 8> ConvertedInstructions;
550b57cec5SDimitry Andric 
56bdd1243dSDimitry Andric   std::optional<int64_t> foldToImm(const MachineOperand &Op) const;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric public:
590b57cec5SDimitry Andric   static char ID;
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   SIPeepholeSDWA() : MachineFunctionPass(ID) {
620b57cec5SDimitry Andric     initializeSIPeepholeSDWAPass(*PassRegistry::getPassRegistry());
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override;
660b57cec5SDimitry Andric   void matchSDWAOperands(MachineBasicBlock &MBB);
670b57cec5SDimitry Andric   std::unique_ptr<SDWAOperand> matchSDWAOperand(MachineInstr &MI);
680b57cec5SDimitry Andric   bool isConvertibleToSDWA(MachineInstr &MI, const GCNSubtarget &ST) const;
690b57cec5SDimitry Andric   void pseudoOpConvertToVOP2(MachineInstr &MI,
700b57cec5SDimitry Andric                              const GCNSubtarget &ST) const;
710b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SDWAOperandsVector &SDWAOperands);
720b57cec5SDimitry Andric   void legalizeScalarOperands(MachineInstr &MI, const GCNSubtarget &ST) const;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   StringRef getPassName() const override { return "SI Peephole SDWA"; }
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
770b57cec5SDimitry Andric     AU.setPreservesCFG();
780b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
790b57cec5SDimitry Andric   }
800b57cec5SDimitry Andric };
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric class SDWAOperand {
830b57cec5SDimitry Andric private:
840b57cec5SDimitry Andric   MachineOperand *Target; // Operand that would be used in converted instruction
850b57cec5SDimitry Andric   MachineOperand *Replaced; // Operand that would be replace by Target
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric public:
880b57cec5SDimitry Andric   SDWAOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp)
890b57cec5SDimitry Andric       : Target(TargetOp), Replaced(ReplacedOp) {
900b57cec5SDimitry Andric     assert(Target->isReg());
910b57cec5SDimitry Andric     assert(Replaced->isReg());
920b57cec5SDimitry Andric   }
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   virtual ~SDWAOperand() = default;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) = 0;
970b57cec5SDimitry Andric   virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) = 0;
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   MachineOperand *getTargetOperand() const { return Target; }
1000b57cec5SDimitry Andric   MachineOperand *getReplacedOperand() const { return Replaced; }
1010b57cec5SDimitry Andric   MachineInstr *getParentInst() const { return Target->getParent(); }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   MachineRegisterInfo *getMRI() const {
1040b57cec5SDimitry Andric     return &getParentInst()->getParent()->getParent()->getRegInfo();
1050b57cec5SDimitry Andric   }
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1080b57cec5SDimitry Andric   virtual void print(raw_ostream& OS) const = 0;
1090b57cec5SDimitry Andric   void dump() const { print(dbgs()); }
1100b57cec5SDimitry Andric #endif
1110b57cec5SDimitry Andric };
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric using namespace AMDGPU::SDWA;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric class SDWASrcOperand : public SDWAOperand {
1160b57cec5SDimitry Andric private:
1170b57cec5SDimitry Andric   SdwaSel SrcSel;
1180b57cec5SDimitry Andric   bool Abs;
1190b57cec5SDimitry Andric   bool Neg;
1200b57cec5SDimitry Andric   bool Sext;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric public:
1230b57cec5SDimitry Andric   SDWASrcOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
1240b57cec5SDimitry Andric                  SdwaSel SrcSel_ = DWORD, bool Abs_ = false, bool Neg_ = false,
1250b57cec5SDimitry Andric                  bool Sext_ = false)
1260b57cec5SDimitry Andric       : SDWAOperand(TargetOp, ReplacedOp),
1270b57cec5SDimitry Andric         SrcSel(SrcSel_), Abs(Abs_), Neg(Neg_), Sext(Sext_) {}
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   MachineInstr *potentialToConvert(const SIInstrInfo *TII) override;
1300b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   SdwaSel getSrcSel() const { return SrcSel; }
1330b57cec5SDimitry Andric   bool getAbs() const { return Abs; }
1340b57cec5SDimitry Andric   bool getNeg() const { return Neg; }
1350b57cec5SDimitry Andric   bool getSext() const { return Sext; }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   uint64_t getSrcMods(const SIInstrInfo *TII,
1380b57cec5SDimitry Andric                       const MachineOperand *SrcOp) const;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1410b57cec5SDimitry Andric   void print(raw_ostream& OS) const override;
1420b57cec5SDimitry Andric #endif
1430b57cec5SDimitry Andric };
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric class SDWADstOperand : public SDWAOperand {
1460b57cec5SDimitry Andric private:
1470b57cec5SDimitry Andric   SdwaSel DstSel;
1480b57cec5SDimitry Andric   DstUnused DstUn;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric public:
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   SDWADstOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
1530b57cec5SDimitry Andric                  SdwaSel DstSel_ = DWORD, DstUnused DstUn_ = UNUSED_PAD)
1540b57cec5SDimitry Andric     : SDWAOperand(TargetOp, ReplacedOp), DstSel(DstSel_), DstUn(DstUn_) {}
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric   MachineInstr *potentialToConvert(const SIInstrInfo *TII) override;
1570b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   SdwaSel getDstSel() const { return DstSel; }
1600b57cec5SDimitry Andric   DstUnused getDstUnused() const { return DstUn; }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1630b57cec5SDimitry Andric   void print(raw_ostream& OS) const override;
1640b57cec5SDimitry Andric #endif
1650b57cec5SDimitry Andric };
1660b57cec5SDimitry Andric 
1670b57cec5SDimitry Andric class SDWADstPreserveOperand : public SDWADstOperand {
1680b57cec5SDimitry Andric private:
1690b57cec5SDimitry Andric   MachineOperand *Preserve;
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric public:
1720b57cec5SDimitry Andric   SDWADstPreserveOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
1730b57cec5SDimitry Andric                          MachineOperand *PreserveOp, SdwaSel DstSel_ = DWORD)
1740b57cec5SDimitry Andric       : SDWADstOperand(TargetOp, ReplacedOp, DstSel_, UNUSED_PRESERVE),
1750b57cec5SDimitry Andric         Preserve(PreserveOp) {}
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric   bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   MachineOperand *getPreservedOperand() const { return Preserve; }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1820b57cec5SDimitry Andric   void print(raw_ostream& OS) const override;
1830b57cec5SDimitry Andric #endif
1840b57cec5SDimitry Andric };
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric } // end anonymous namespace
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric INITIALIZE_PASS(SIPeepholeSDWA, DEBUG_TYPE, "SI Peephole SDWA", false, false)
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric char SIPeepholeSDWA::ID = 0;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric char &llvm::SIPeepholeSDWAID = SIPeepholeSDWA::ID;
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric FunctionPass *llvm::createSIPeepholeSDWAPass() {
1950b57cec5SDimitry Andric   return new SIPeepholeSDWA();
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2000b57cec5SDimitry Andric static raw_ostream& operator<<(raw_ostream &OS, SdwaSel Sel) {
2010b57cec5SDimitry Andric   switch(Sel) {
2020b57cec5SDimitry Andric   case BYTE_0: OS << "BYTE_0"; break;
2030b57cec5SDimitry Andric   case BYTE_1: OS << "BYTE_1"; break;
2040b57cec5SDimitry Andric   case BYTE_2: OS << "BYTE_2"; break;
2050b57cec5SDimitry Andric   case BYTE_3: OS << "BYTE_3"; break;
2060b57cec5SDimitry Andric   case WORD_0: OS << "WORD_0"; break;
2070b57cec5SDimitry Andric   case WORD_1: OS << "WORD_1"; break;
2080b57cec5SDimitry Andric   case DWORD:  OS << "DWORD"; break;
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric   return OS;
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric static raw_ostream& operator<<(raw_ostream &OS, const DstUnused &Un) {
2140b57cec5SDimitry Andric   switch(Un) {
2150b57cec5SDimitry Andric   case UNUSED_PAD: OS << "UNUSED_PAD"; break;
2160b57cec5SDimitry Andric   case UNUSED_SEXT: OS << "UNUSED_SEXT"; break;
2170b57cec5SDimitry Andric   case UNUSED_PRESERVE: OS << "UNUSED_PRESERVE"; break;
2180b57cec5SDimitry Andric   }
2190b57cec5SDimitry Andric   return OS;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric LLVM_DUMP_METHOD
2230b57cec5SDimitry Andric void SDWASrcOperand::print(raw_ostream& OS) const {
2240b57cec5SDimitry Andric   OS << "SDWA src: " << *getTargetOperand()
2250b57cec5SDimitry Andric     << " src_sel:" << getSrcSel()
2260b57cec5SDimitry Andric     << " abs:" << getAbs() << " neg:" << getNeg()
2270b57cec5SDimitry Andric     << " sext:" << getSext() << '\n';
2280b57cec5SDimitry Andric }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric LLVM_DUMP_METHOD
2310b57cec5SDimitry Andric void SDWADstOperand::print(raw_ostream& OS) const {
2320b57cec5SDimitry Andric   OS << "SDWA dst: " << *getTargetOperand()
2330b57cec5SDimitry Andric     << " dst_sel:" << getDstSel()
2340b57cec5SDimitry Andric     << " dst_unused:" << getDstUnused() << '\n';
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric LLVM_DUMP_METHOD
2380b57cec5SDimitry Andric void SDWADstPreserveOperand::print(raw_ostream& OS) const {
2390b57cec5SDimitry Andric   OS << "SDWA preserve dst: " << *getTargetOperand()
2400b57cec5SDimitry Andric     << " dst_sel:" << getDstSel()
2410b57cec5SDimitry Andric     << " preserve:" << *getPreservedOperand() << '\n';
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric #endif
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric static void copyRegOperand(MachineOperand &To, const MachineOperand &From) {
2470b57cec5SDimitry Andric   assert(To.isReg() && From.isReg());
2480b57cec5SDimitry Andric   To.setReg(From.getReg());
2490b57cec5SDimitry Andric   To.setSubReg(From.getSubReg());
2500b57cec5SDimitry Andric   To.setIsUndef(From.isUndef());
2510b57cec5SDimitry Andric   if (To.isUse()) {
2520b57cec5SDimitry Andric     To.setIsKill(From.isKill());
2530b57cec5SDimitry Andric   } else {
2540b57cec5SDimitry Andric     To.setIsDead(From.isDead());
2550b57cec5SDimitry Andric   }
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric static bool isSameReg(const MachineOperand &LHS, const MachineOperand &RHS) {
2590b57cec5SDimitry Andric   return LHS.isReg() &&
2600b57cec5SDimitry Andric          RHS.isReg() &&
2610b57cec5SDimitry Andric          LHS.getReg() == RHS.getReg() &&
2620b57cec5SDimitry Andric          LHS.getSubReg() == RHS.getSubReg();
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric static MachineOperand *findSingleRegUse(const MachineOperand *Reg,
2660b57cec5SDimitry Andric                                         const MachineRegisterInfo *MRI) {
2670b57cec5SDimitry Andric   if (!Reg->isReg() || !Reg->isDef())
2680b57cec5SDimitry Andric     return nullptr;
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   MachineOperand *ResMO = nullptr;
2710b57cec5SDimitry Andric   for (MachineOperand &UseMO : MRI->use_nodbg_operands(Reg->getReg())) {
2720b57cec5SDimitry Andric     // If there exist use of subreg of Reg then return nullptr
2730b57cec5SDimitry Andric     if (!isSameReg(UseMO, *Reg))
2740b57cec5SDimitry Andric       return nullptr;
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric     // Check that there is only one instruction that uses Reg
2770b57cec5SDimitry Andric     if (!ResMO) {
2780b57cec5SDimitry Andric       ResMO = &UseMO;
2790b57cec5SDimitry Andric     } else if (ResMO->getParent() != UseMO.getParent()) {
2800b57cec5SDimitry Andric       return nullptr;
2810b57cec5SDimitry Andric     }
2820b57cec5SDimitry Andric   }
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   return ResMO;
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric static MachineOperand *findSingleRegDef(const MachineOperand *Reg,
2880b57cec5SDimitry Andric                                         const MachineRegisterInfo *MRI) {
2890b57cec5SDimitry Andric   if (!Reg->isReg())
2900b57cec5SDimitry Andric     return nullptr;
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric   MachineInstr *DefInstr = MRI->getUniqueVRegDef(Reg->getReg());
2930b57cec5SDimitry Andric   if (!DefInstr)
2940b57cec5SDimitry Andric     return nullptr;
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric   for (auto &DefMO : DefInstr->defs()) {
2970b57cec5SDimitry Andric     if (DefMO.isReg() && DefMO.getReg() == Reg->getReg())
2980b57cec5SDimitry Andric       return &DefMO;
2990b57cec5SDimitry Andric   }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   // Ignore implicit defs.
3020b57cec5SDimitry Andric   return nullptr;
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
3050b57cec5SDimitry Andric uint64_t SDWASrcOperand::getSrcMods(const SIInstrInfo *TII,
3060b57cec5SDimitry Andric                                     const MachineOperand *SrcOp) const {
3070b57cec5SDimitry Andric   uint64_t Mods = 0;
3080b57cec5SDimitry Andric   const auto *MI = SrcOp->getParent();
3090b57cec5SDimitry Andric   if (TII->getNamedOperand(*MI, AMDGPU::OpName::src0) == SrcOp) {
3100b57cec5SDimitry Andric     if (auto *Mod = TII->getNamedOperand(*MI, AMDGPU::OpName::src0_modifiers)) {
3110b57cec5SDimitry Andric       Mods = Mod->getImm();
3120b57cec5SDimitry Andric     }
3130b57cec5SDimitry Andric   } else if (TII->getNamedOperand(*MI, AMDGPU::OpName::src1) == SrcOp) {
3140b57cec5SDimitry Andric     if (auto *Mod = TII->getNamedOperand(*MI, AMDGPU::OpName::src1_modifiers)) {
3150b57cec5SDimitry Andric       Mods = Mod->getImm();
3160b57cec5SDimitry Andric     }
3170b57cec5SDimitry Andric   }
3180b57cec5SDimitry Andric   if (Abs || Neg) {
3190b57cec5SDimitry Andric     assert(!Sext &&
32081ad6265SDimitry Andric            "Float and integer src modifiers can't be set simultaneously");
3210b57cec5SDimitry Andric     Mods |= Abs ? SISrcMods::ABS : 0u;
3220b57cec5SDimitry Andric     Mods ^= Neg ? SISrcMods::NEG : 0u;
3230b57cec5SDimitry Andric   } else if (Sext) {
3240b57cec5SDimitry Andric     Mods |= SISrcMods::SEXT;
3250b57cec5SDimitry Andric   }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric   return Mods;
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric MachineInstr *SDWASrcOperand::potentialToConvert(const SIInstrInfo *TII) {
3310b57cec5SDimitry Andric   // For SDWA src operand potential instruction is one that use register
3320b57cec5SDimitry Andric   // defined by parent instruction
3330b57cec5SDimitry Andric   MachineOperand *PotentialMO = findSingleRegUse(getReplacedOperand(), getMRI());
3340b57cec5SDimitry Andric   if (!PotentialMO)
3350b57cec5SDimitry Andric     return nullptr;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   return PotentialMO->getParent();
3380b57cec5SDimitry Andric }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric bool SDWASrcOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
3410b57cec5SDimitry Andric   // Find operand in instruction that matches source operand and replace it with
3420b57cec5SDimitry Andric   // target operand. Set corresponding src_sel
3430b57cec5SDimitry Andric   bool IsPreserveSrc = false;
3440b57cec5SDimitry Andric   MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
3450b57cec5SDimitry Andric   MachineOperand *SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src0_sel);
3460b57cec5SDimitry Andric   MachineOperand *SrcMods =
3470b57cec5SDimitry Andric       TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
3480b57cec5SDimitry Andric   assert(Src && (Src->isReg() || Src->isImm()));
3490b57cec5SDimitry Andric   if (!isSameReg(*Src, *getReplacedOperand())) {
3500b57cec5SDimitry Andric     // If this is not src0 then it could be src1
3510b57cec5SDimitry Andric     Src = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
3520b57cec5SDimitry Andric     SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src1_sel);
3530b57cec5SDimitry Andric     SrcMods = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric     if (!Src ||
3560b57cec5SDimitry Andric         !isSameReg(*Src, *getReplacedOperand())) {
3570b57cec5SDimitry Andric       // It's possible this Src is a tied operand for
3580b57cec5SDimitry Andric       // UNUSED_PRESERVE, in which case we can either
3590b57cec5SDimitry Andric       // abandon the peephole attempt, or if legal we can
3600b57cec5SDimitry Andric       // copy the target operand into the tied slot
3610b57cec5SDimitry Andric       // if the preserve operation will effectively cause the same
3620b57cec5SDimitry Andric       // result by overwriting the rest of the dst.
3630b57cec5SDimitry Andric       MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
3640b57cec5SDimitry Andric       MachineOperand *DstUnused =
3650b57cec5SDimitry Andric         TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric       if (Dst &&
3680b57cec5SDimitry Andric           DstUnused->getImm() == AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE) {
369349cc55cSDimitry Andric         // This will work if the tied src is accessing WORD_0, and the dst is
3700b57cec5SDimitry Andric         // writing WORD_1. Modifiers don't matter because all the bits that
3710b57cec5SDimitry Andric         // would be impacted are being overwritten by the dst.
3720b57cec5SDimitry Andric         // Any other case will not work.
3730b57cec5SDimitry Andric         SdwaSel DstSel = static_cast<SdwaSel>(
3740b57cec5SDimitry Andric             TII->getNamedImmOperand(MI, AMDGPU::OpName::dst_sel));
3750b57cec5SDimitry Andric         if (DstSel == AMDGPU::SDWA::SdwaSel::WORD_1 &&
3760b57cec5SDimitry Andric             getSrcSel() == AMDGPU::SDWA::SdwaSel::WORD_0) {
3770b57cec5SDimitry Andric           IsPreserveSrc = true;
3780b57cec5SDimitry Andric           auto DstIdx = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
3790b57cec5SDimitry Andric                                                    AMDGPU::OpName::vdst);
3800b57cec5SDimitry Andric           auto TiedIdx = MI.findTiedOperandIdx(DstIdx);
3810b57cec5SDimitry Andric           Src = &MI.getOperand(TiedIdx);
3820b57cec5SDimitry Andric           SrcSel = nullptr;
3830b57cec5SDimitry Andric           SrcMods = nullptr;
3840b57cec5SDimitry Andric         } else {
3850b57cec5SDimitry Andric           // Not legal to convert this src
3860b57cec5SDimitry Andric           return false;
3870b57cec5SDimitry Andric         }
3880b57cec5SDimitry Andric       }
3890b57cec5SDimitry Andric     }
3900b57cec5SDimitry Andric     assert(Src && Src->isReg());
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric     if ((MI.getOpcode() == AMDGPU::V_FMAC_F16_sdwa ||
3930b57cec5SDimitry Andric          MI.getOpcode() == AMDGPU::V_FMAC_F32_sdwa ||
3940b57cec5SDimitry Andric          MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
3950b57cec5SDimitry Andric          MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
3960b57cec5SDimitry Andric          !isSameReg(*Src, *getReplacedOperand())) {
3970b57cec5SDimitry Andric       // In case of v_mac_f16/32_sdwa this pass can try to apply src operand to
3980b57cec5SDimitry Andric       // src2. This is not allowed.
3990b57cec5SDimitry Andric       return false;
4000b57cec5SDimitry Andric     }
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric     assert(isSameReg(*Src, *getReplacedOperand()) &&
4030b57cec5SDimitry Andric            (IsPreserveSrc || (SrcSel && SrcMods)));
4040b57cec5SDimitry Andric   }
4050b57cec5SDimitry Andric   copyRegOperand(*Src, *getTargetOperand());
4060b57cec5SDimitry Andric   if (!IsPreserveSrc) {
4070b57cec5SDimitry Andric     SrcSel->setImm(getSrcSel());
4080b57cec5SDimitry Andric     SrcMods->setImm(getSrcMods(TII, Src));
4090b57cec5SDimitry Andric   }
4100b57cec5SDimitry Andric   getTargetOperand()->setIsKill(false);
4110b57cec5SDimitry Andric   return true;
4120b57cec5SDimitry Andric }
4130b57cec5SDimitry Andric 
4140b57cec5SDimitry Andric MachineInstr *SDWADstOperand::potentialToConvert(const SIInstrInfo *TII) {
4150b57cec5SDimitry Andric   // For SDWA dst operand potential instruction is one that defines register
4160b57cec5SDimitry Andric   // that this operand uses
4170b57cec5SDimitry Andric   MachineRegisterInfo *MRI = getMRI();
4180b57cec5SDimitry Andric   MachineInstr *ParentMI = getParentInst();
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   MachineOperand *PotentialMO = findSingleRegDef(getReplacedOperand(), MRI);
4210b57cec5SDimitry Andric   if (!PotentialMO)
4220b57cec5SDimitry Andric     return nullptr;
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   // Check that ParentMI is the only instruction that uses replaced register
4250b57cec5SDimitry Andric   for (MachineInstr &UseInst : MRI->use_nodbg_instructions(PotentialMO->getReg())) {
4260b57cec5SDimitry Andric     if (&UseInst != ParentMI)
4270b57cec5SDimitry Andric       return nullptr;
4280b57cec5SDimitry Andric   }
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   return PotentialMO->getParent();
4310b57cec5SDimitry Andric }
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric bool SDWADstOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
4340b57cec5SDimitry Andric   // Replace vdst operand in MI with target operand. Set dst_sel and dst_unused
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric   if ((MI.getOpcode() == AMDGPU::V_FMAC_F16_sdwa ||
4370b57cec5SDimitry Andric        MI.getOpcode() == AMDGPU::V_FMAC_F32_sdwa ||
4380b57cec5SDimitry Andric        MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
4390b57cec5SDimitry Andric        MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
4400b57cec5SDimitry Andric       getDstSel() != AMDGPU::SDWA::DWORD) {
4410b57cec5SDimitry Andric     // v_mac_f16/32_sdwa allow dst_sel to be equal only to DWORD
4420b57cec5SDimitry Andric     return false;
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric 
4450b57cec5SDimitry Andric   MachineOperand *Operand = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
4460b57cec5SDimitry Andric   assert(Operand &&
4470b57cec5SDimitry Andric          Operand->isReg() &&
4480b57cec5SDimitry Andric          isSameReg(*Operand, *getReplacedOperand()));
4490b57cec5SDimitry Andric   copyRegOperand(*Operand, *getTargetOperand());
4500b57cec5SDimitry Andric   MachineOperand *DstSel= TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel);
4510b57cec5SDimitry Andric   assert(DstSel);
4520b57cec5SDimitry Andric   DstSel->setImm(getDstSel());
4530b57cec5SDimitry Andric   MachineOperand *DstUnused= TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
4540b57cec5SDimitry Andric   assert(DstUnused);
4550b57cec5SDimitry Andric   DstUnused->setImm(getDstUnused());
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   // Remove original instruction  because it would conflict with our new
4580b57cec5SDimitry Andric   // instruction by register definition
4590b57cec5SDimitry Andric   getParentInst()->eraseFromParent();
4600b57cec5SDimitry Andric   return true;
4610b57cec5SDimitry Andric }
4620b57cec5SDimitry Andric 
4630b57cec5SDimitry Andric bool SDWADstPreserveOperand::convertToSDWA(MachineInstr &MI,
4640b57cec5SDimitry Andric                                            const SIInstrInfo *TII) {
4650b57cec5SDimitry Andric   // MI should be moved right before v_or_b32.
4660b57cec5SDimitry Andric   // For this we should clear all kill flags on uses of MI src-operands or else
4670b57cec5SDimitry Andric   // we can encounter problem with use of killed operand.
4680b57cec5SDimitry Andric   for (MachineOperand &MO : MI.uses()) {
4690b57cec5SDimitry Andric     if (!MO.isReg())
4700b57cec5SDimitry Andric       continue;
4710b57cec5SDimitry Andric     getMRI()->clearKillFlags(MO.getReg());
4720b57cec5SDimitry Andric   }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric   // Move MI before v_or_b32
4750b57cec5SDimitry Andric   auto MBB = MI.getParent();
4760b57cec5SDimitry Andric   MBB->remove(&MI);
4770b57cec5SDimitry Andric   MBB->insert(getParentInst(), &MI);
4780b57cec5SDimitry Andric 
4790b57cec5SDimitry Andric   // Add Implicit use of preserved register
4800b57cec5SDimitry Andric   MachineInstrBuilder MIB(*MBB->getParent(), MI);
4810b57cec5SDimitry Andric   MIB.addReg(getPreservedOperand()->getReg(),
4820b57cec5SDimitry Andric              RegState::ImplicitKill,
4830b57cec5SDimitry Andric              getPreservedOperand()->getSubReg());
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   // Tie dst to implicit use
4860b57cec5SDimitry Andric   MI.tieOperands(AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdst),
4870b57cec5SDimitry Andric                  MI.getNumOperands() - 1);
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric   // Convert MI as any other SDWADstOperand and remove v_or_b32
4900b57cec5SDimitry Andric   return SDWADstOperand::convertToSDWA(MI, TII);
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric 
493bdd1243dSDimitry Andric std::optional<int64_t>
494bdd1243dSDimitry Andric SIPeepholeSDWA::foldToImm(const MachineOperand &Op) const {
4950b57cec5SDimitry Andric   if (Op.isImm()) {
4960b57cec5SDimitry Andric     return Op.getImm();
4970b57cec5SDimitry Andric   }
4980b57cec5SDimitry Andric 
4990b57cec5SDimitry Andric   // If this is not immediate then it can be copy of immediate value, e.g.:
5000b57cec5SDimitry Andric   // %1 = S_MOV_B32 255;
5010b57cec5SDimitry Andric   if (Op.isReg()) {
5020b57cec5SDimitry Andric     for (const MachineOperand &Def : MRI->def_operands(Op.getReg())) {
5030b57cec5SDimitry Andric       if (!isSameReg(Op, Def))
5040b57cec5SDimitry Andric         continue;
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric       const MachineInstr *DefInst = Def.getParent();
5070b57cec5SDimitry Andric       if (!TII->isFoldableCopy(*DefInst))
508bdd1243dSDimitry Andric         return std::nullopt;
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric       const MachineOperand &Copied = DefInst->getOperand(1);
5110b57cec5SDimitry Andric       if (!Copied.isImm())
512bdd1243dSDimitry Andric         return std::nullopt;
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric       return Copied.getImm();
5150b57cec5SDimitry Andric     }
5160b57cec5SDimitry Andric   }
5170b57cec5SDimitry Andric 
518bdd1243dSDimitry Andric   return std::nullopt;
5190b57cec5SDimitry Andric }
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric std::unique_ptr<SDWAOperand>
5220b57cec5SDimitry Andric SIPeepholeSDWA::matchSDWAOperand(MachineInstr &MI) {
5230b57cec5SDimitry Andric   unsigned Opcode = MI.getOpcode();
5240b57cec5SDimitry Andric   switch (Opcode) {
5250b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B32_e32:
5260b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I32_e32:
5270b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B32_e32:
5280b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B32_e64:
5290b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I32_e64:
5300b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B32_e64: {
5310b57cec5SDimitry Andric     // from: v_lshrrev_b32_e32 v1, 16/24, v0
5320b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:WORD_1/BYTE_3
5330b57cec5SDimitry Andric 
5340b57cec5SDimitry Andric     // from: v_ashrrev_i32_e32 v1, 16/24, v0
5350b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:WORD_1/BYTE_3 sext:1
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric     // from: v_lshlrev_b32_e32 v1, 16/24, v0
5380b57cec5SDimitry Andric     // to SDWA dst:v1 dst_sel:WORD_1/BYTE_3 dst_unused:UNUSED_PAD
5390b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
5400b57cec5SDimitry Andric     auto Imm = foldToImm(*Src0);
5410b57cec5SDimitry Andric     if (!Imm)
5420b57cec5SDimitry Andric       break;
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric     if (*Imm != 16 && *Imm != 24)
5450b57cec5SDimitry Andric       break;
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
5480b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
549e8d8bef9SDimitry Andric     if (Src1->getReg().isPhysical() || Dst->getReg().isPhysical())
5500b57cec5SDimitry Andric       break;
5510b57cec5SDimitry Andric 
5520b57cec5SDimitry Andric     if (Opcode == AMDGPU::V_LSHLREV_B32_e32 ||
5530b57cec5SDimitry Andric         Opcode == AMDGPU::V_LSHLREV_B32_e64) {
5548bcb0991SDimitry Andric       return std::make_unique<SDWADstOperand>(
5550b57cec5SDimitry Andric           Dst, Src1, *Imm == 16 ? WORD_1 : BYTE_3, UNUSED_PAD);
5560b57cec5SDimitry Andric     } else {
5578bcb0991SDimitry Andric       return std::make_unique<SDWASrcOperand>(
5580b57cec5SDimitry Andric           Src1, Dst, *Imm == 16 ? WORD_1 : BYTE_3, false, false,
5590b57cec5SDimitry Andric           Opcode != AMDGPU::V_LSHRREV_B32_e32 &&
5600b57cec5SDimitry Andric           Opcode != AMDGPU::V_LSHRREV_B32_e64);
5610b57cec5SDimitry Andric     }
5620b57cec5SDimitry Andric     break;
5630b57cec5SDimitry Andric   }
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B16_e32:
5660b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I16_e32:
5670b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B16_e32:
5680b57cec5SDimitry Andric   case AMDGPU::V_LSHRREV_B16_e64:
5690b57cec5SDimitry Andric   case AMDGPU::V_ASHRREV_I16_e64:
5700b57cec5SDimitry Andric   case AMDGPU::V_LSHLREV_B16_e64: {
5710b57cec5SDimitry Andric     // from: v_lshrrev_b16_e32 v1, 8, v0
5720b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:BYTE_1
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric     // from: v_ashrrev_i16_e32 v1, 8, v0
5750b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:BYTE_1 sext:1
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric     // from: v_lshlrev_b16_e32 v1, 8, v0
5780b57cec5SDimitry Andric     // to SDWA dst:v1 dst_sel:BYTE_1 dst_unused:UNUSED_PAD
5790b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
5800b57cec5SDimitry Andric     auto Imm = foldToImm(*Src0);
5810b57cec5SDimitry Andric     if (!Imm || *Imm != 8)
5820b57cec5SDimitry Andric       break;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
5850b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
5860b57cec5SDimitry Andric 
587e8d8bef9SDimitry Andric     if (Src1->getReg().isPhysical() || Dst->getReg().isPhysical())
5880b57cec5SDimitry Andric       break;
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric     if (Opcode == AMDGPU::V_LSHLREV_B16_e32 ||
5910b57cec5SDimitry Andric         Opcode == AMDGPU::V_LSHLREV_B16_e64) {
5928bcb0991SDimitry Andric       return std::make_unique<SDWADstOperand>(Dst, Src1, BYTE_1, UNUSED_PAD);
5930b57cec5SDimitry Andric     } else {
5948bcb0991SDimitry Andric       return std::make_unique<SDWASrcOperand>(
5950b57cec5SDimitry Andric             Src1, Dst, BYTE_1, false, false,
5960b57cec5SDimitry Andric             Opcode != AMDGPU::V_LSHRREV_B16_e32 &&
5970b57cec5SDimitry Andric             Opcode != AMDGPU::V_LSHRREV_B16_e64);
5980b57cec5SDimitry Andric     }
5990b57cec5SDimitry Andric     break;
6000b57cec5SDimitry Andric   }
6010b57cec5SDimitry Andric 
602e8d8bef9SDimitry Andric   case AMDGPU::V_BFE_I32_e64:
603e8d8bef9SDimitry Andric   case AMDGPU::V_BFE_U32_e64: {
6040b57cec5SDimitry Andric     // e.g.:
6050b57cec5SDimitry Andric     // from: v_bfe_u32 v1, v0, 8, 8
6060b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:BYTE_1
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric     // offset | width | src_sel
6090b57cec5SDimitry Andric     // ------------------------
6100b57cec5SDimitry Andric     // 0      | 8     | BYTE_0
6110b57cec5SDimitry Andric     // 0      | 16    | WORD_0
6120b57cec5SDimitry Andric     // 0      | 32    | DWORD ?
6130b57cec5SDimitry Andric     // 8      | 8     | BYTE_1
6140b57cec5SDimitry Andric     // 16     | 8     | BYTE_2
6150b57cec5SDimitry Andric     // 16     | 16    | WORD_1
6160b57cec5SDimitry Andric     // 24     | 8     | BYTE_3
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
6190b57cec5SDimitry Andric     auto Offset = foldToImm(*Src1);
6200b57cec5SDimitry Andric     if (!Offset)
6210b57cec5SDimitry Andric       break;
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric     MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
6240b57cec5SDimitry Andric     auto Width = foldToImm(*Src2);
6250b57cec5SDimitry Andric     if (!Width)
6260b57cec5SDimitry Andric       break;
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric     SdwaSel SrcSel = DWORD;
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric     if (*Offset == 0 && *Width == 8)
6310b57cec5SDimitry Andric       SrcSel = BYTE_0;
6320b57cec5SDimitry Andric     else if (*Offset == 0 && *Width == 16)
6330b57cec5SDimitry Andric       SrcSel = WORD_0;
6340b57cec5SDimitry Andric     else if (*Offset == 0 && *Width == 32)
6350b57cec5SDimitry Andric       SrcSel = DWORD;
6360b57cec5SDimitry Andric     else if (*Offset == 8 && *Width == 8)
6370b57cec5SDimitry Andric       SrcSel = BYTE_1;
6380b57cec5SDimitry Andric     else if (*Offset == 16 && *Width == 8)
6390b57cec5SDimitry Andric       SrcSel = BYTE_2;
6400b57cec5SDimitry Andric     else if (*Offset == 16 && *Width == 16)
6410b57cec5SDimitry Andric       SrcSel = WORD_1;
6420b57cec5SDimitry Andric     else if (*Offset == 24 && *Width == 8)
6430b57cec5SDimitry Andric       SrcSel = BYTE_3;
6440b57cec5SDimitry Andric     else
6450b57cec5SDimitry Andric       break;
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
6480b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
6490b57cec5SDimitry Andric 
650e8d8bef9SDimitry Andric     if (Src0->getReg().isPhysical() || Dst->getReg().isPhysical())
6510b57cec5SDimitry Andric       break;
6520b57cec5SDimitry Andric 
6538bcb0991SDimitry Andric     return std::make_unique<SDWASrcOperand>(
654e8d8bef9SDimitry Andric           Src0, Dst, SrcSel, false, false, Opcode != AMDGPU::V_BFE_U32_e64);
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric 
6570b57cec5SDimitry Andric   case AMDGPU::V_AND_B32_e32:
6580b57cec5SDimitry Andric   case AMDGPU::V_AND_B32_e64: {
6590b57cec5SDimitry Andric     // e.g.:
6600b57cec5SDimitry Andric     // from: v_and_b32_e32 v1, 0x0000ffff/0x000000ff, v0
6610b57cec5SDimitry Andric     // to SDWA src:v0 src_sel:WORD_0/BYTE_0
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric     MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
6640b57cec5SDimitry Andric     MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
6650b57cec5SDimitry Andric     auto ValSrc = Src1;
6660b57cec5SDimitry Andric     auto Imm = foldToImm(*Src0);
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric     if (!Imm) {
6690b57cec5SDimitry Andric       Imm = foldToImm(*Src1);
6700b57cec5SDimitry Andric       ValSrc = Src0;
6710b57cec5SDimitry Andric     }
6720b57cec5SDimitry Andric 
6730b57cec5SDimitry Andric     if (!Imm || (*Imm != 0x0000ffff && *Imm != 0x000000ff))
6740b57cec5SDimitry Andric       break;
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric     MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
6770b57cec5SDimitry Andric 
678e8d8bef9SDimitry Andric     if (ValSrc->getReg().isPhysical() || Dst->getReg().isPhysical())
6790b57cec5SDimitry Andric       break;
6800b57cec5SDimitry Andric 
6818bcb0991SDimitry Andric     return std::make_unique<SDWASrcOperand>(
6820b57cec5SDimitry Andric         ValSrc, Dst, *Imm == 0x0000ffff ? WORD_0 : BYTE_0);
6830b57cec5SDimitry Andric   }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric   case AMDGPU::V_OR_B32_e32:
6860b57cec5SDimitry Andric   case AMDGPU::V_OR_B32_e64: {
6870b57cec5SDimitry Andric     // Patterns for dst_unused:UNUSED_PRESERVE.
6880b57cec5SDimitry Andric     // e.g., from:
6890b57cec5SDimitry Andric     // v_add_f16_sdwa v0, v1, v2 dst_sel:WORD_1 dst_unused:UNUSED_PAD
6900b57cec5SDimitry Andric     //                           src1_sel:WORD_1 src2_sel:WORD1
6910b57cec5SDimitry Andric     // v_add_f16_e32 v3, v1, v2
6920b57cec5SDimitry Andric     // v_or_b32_e32 v4, v0, v3
6930b57cec5SDimitry Andric     // to SDWA preserve dst:v4 dst_sel:WORD_1 dst_unused:UNUSED_PRESERVE preserve:v3
6940b57cec5SDimitry Andric 
6950b57cec5SDimitry Andric     // Check if one of operands of v_or_b32 is SDWA instruction
696bdd1243dSDimitry Andric     using CheckRetType =
697bdd1243dSDimitry Andric         std::optional<std::pair<MachineOperand *, MachineOperand *>>;
6980b57cec5SDimitry Andric     auto CheckOROperandsForSDWA =
6990b57cec5SDimitry Andric       [&](const MachineOperand *Op1, const MachineOperand *Op2) -> CheckRetType {
7000b57cec5SDimitry Andric         if (!Op1 || !Op1->isReg() || !Op2 || !Op2->isReg())
701bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric         MachineOperand *Op1Def = findSingleRegDef(Op1, MRI);
7040b57cec5SDimitry Andric         if (!Op1Def)
705bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric         MachineInstr *Op1Inst = Op1Def->getParent();
7080b57cec5SDimitry Andric         if (!TII->isSDWA(*Op1Inst))
709bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric         MachineOperand *Op2Def = findSingleRegDef(Op2, MRI);
7120b57cec5SDimitry Andric         if (!Op2Def)
713bdd1243dSDimitry Andric           return CheckRetType(std::nullopt);
7140b57cec5SDimitry Andric 
715bdd1243dSDimitry Andric         return CheckRetType(std::pair(Op1Def, Op2Def));
7160b57cec5SDimitry Andric       };
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric     MachineOperand *OrSDWA = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
7190b57cec5SDimitry Andric     MachineOperand *OrOther = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
7200b57cec5SDimitry Andric     assert(OrSDWA && OrOther);
7210b57cec5SDimitry Andric     auto Res = CheckOROperandsForSDWA(OrSDWA, OrOther);
7220b57cec5SDimitry Andric     if (!Res) {
7230b57cec5SDimitry Andric       OrSDWA = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
7240b57cec5SDimitry Andric       OrOther = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
7250b57cec5SDimitry Andric       assert(OrSDWA && OrOther);
7260b57cec5SDimitry Andric       Res = CheckOROperandsForSDWA(OrSDWA, OrOther);
7270b57cec5SDimitry Andric       if (!Res)
7280b57cec5SDimitry Andric         break;
7290b57cec5SDimitry Andric     }
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric     MachineOperand *OrSDWADef = Res->first;
7320b57cec5SDimitry Andric     MachineOperand *OrOtherDef = Res->second;
7330b57cec5SDimitry Andric     assert(OrSDWADef && OrOtherDef);
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric     MachineInstr *SDWAInst = OrSDWADef->getParent();
7360b57cec5SDimitry Andric     MachineInstr *OtherInst = OrOtherDef->getParent();
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric     // Check that OtherInstr is actually bitwise compatible with SDWAInst = their
7390b57cec5SDimitry Andric     // destination patterns don't overlap. Compatible instruction can be either
7400b57cec5SDimitry Andric     // regular instruction with compatible bitness or SDWA instruction with
7410b57cec5SDimitry Andric     // correct dst_sel
7420b57cec5SDimitry Andric     // SDWAInst | OtherInst bitness / OtherInst dst_sel
7430b57cec5SDimitry Andric     // -----------------------------------------------------
7440b57cec5SDimitry Andric     // DWORD    | no                    / no
7450b57cec5SDimitry Andric     // WORD_0   | no                    / BYTE_2/3, WORD_1
7460b57cec5SDimitry Andric     // WORD_1   | 8/16-bit instructions / BYTE_0/1, WORD_0
7470b57cec5SDimitry Andric     // BYTE_0   | no                    / BYTE_1/2/3, WORD_1
7480b57cec5SDimitry Andric     // BYTE_1   | 8-bit                 / BYTE_0/2/3, WORD_1
7490b57cec5SDimitry Andric     // BYTE_2   | 8/16-bit              / BYTE_0/1/3. WORD_0
7500b57cec5SDimitry Andric     // BYTE_3   | 8/16/24-bit           / BYTE_0/1/2, WORD_0
7510b57cec5SDimitry Andric     // E.g. if SDWAInst is v_add_f16_sdwa dst_sel:WORD_1 then v_add_f16 is OK
7520b57cec5SDimitry Andric     // but v_add_f32 is not.
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric     // TODO: add support for non-SDWA instructions as OtherInst.
7550b57cec5SDimitry Andric     // For now this only works with SDWA instructions. For regular instructions
7560b57cec5SDimitry Andric     // there is no way to determine if the instruction writes only 8/16/24-bit
7570b57cec5SDimitry Andric     // out of full register size and all registers are at min 32-bit wide.
7580b57cec5SDimitry Andric     if (!TII->isSDWA(*OtherInst))
7590b57cec5SDimitry Andric       break;
7600b57cec5SDimitry Andric 
7610b57cec5SDimitry Andric     SdwaSel DstSel = static_cast<SdwaSel>(
762*06c3fb27SDimitry Andric         TII->getNamedImmOperand(*SDWAInst, AMDGPU::OpName::dst_sel));
7630b57cec5SDimitry Andric     SdwaSel OtherDstSel = static_cast<SdwaSel>(
7640b57cec5SDimitry Andric       TII->getNamedImmOperand(*OtherInst, AMDGPU::OpName::dst_sel));
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric     bool DstSelAgree = false;
7670b57cec5SDimitry Andric     switch (DstSel) {
7680b57cec5SDimitry Andric     case WORD_0: DstSelAgree = ((OtherDstSel == BYTE_2) ||
7690b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
7700b57cec5SDimitry Andric                                 (OtherDstSel == WORD_1));
7710b57cec5SDimitry Andric       break;
7720b57cec5SDimitry Andric     case WORD_1: DstSelAgree = ((OtherDstSel == BYTE_0) ||
7730b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_1) ||
7740b57cec5SDimitry Andric                                 (OtherDstSel == WORD_0));
7750b57cec5SDimitry Andric       break;
7760b57cec5SDimitry Andric     case BYTE_0: DstSelAgree = ((OtherDstSel == BYTE_1) ||
7770b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_2) ||
7780b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
7790b57cec5SDimitry Andric                                 (OtherDstSel == WORD_1));
7800b57cec5SDimitry Andric       break;
7810b57cec5SDimitry Andric     case BYTE_1: DstSelAgree = ((OtherDstSel == BYTE_0) ||
7820b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_2) ||
7830b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
7840b57cec5SDimitry Andric                                 (OtherDstSel == WORD_1));
7850b57cec5SDimitry Andric       break;
7860b57cec5SDimitry Andric     case BYTE_2: DstSelAgree = ((OtherDstSel == BYTE_0) ||
7870b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_1) ||
7880b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_3) ||
7890b57cec5SDimitry Andric                                 (OtherDstSel == WORD_0));
7900b57cec5SDimitry Andric       break;
7910b57cec5SDimitry Andric     case BYTE_3: DstSelAgree = ((OtherDstSel == BYTE_0) ||
7920b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_1) ||
7930b57cec5SDimitry Andric                                 (OtherDstSel == BYTE_2) ||
7940b57cec5SDimitry Andric                                 (OtherDstSel == WORD_0));
7950b57cec5SDimitry Andric       break;
7960b57cec5SDimitry Andric     default: DstSelAgree = false;
7970b57cec5SDimitry Andric     }
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric     if (!DstSelAgree)
8000b57cec5SDimitry Andric       break;
8010b57cec5SDimitry Andric 
8020b57cec5SDimitry Andric     // Also OtherInst dst_unused should be UNUSED_PAD
8030b57cec5SDimitry Andric     DstUnused OtherDstUnused = static_cast<DstUnused>(
8040b57cec5SDimitry Andric       TII->getNamedImmOperand(*OtherInst, AMDGPU::OpName::dst_unused));
8050b57cec5SDimitry Andric     if (OtherDstUnused != DstUnused::UNUSED_PAD)
8060b57cec5SDimitry Andric       break;
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric     // Create DstPreserveOperand
8090b57cec5SDimitry Andric     MachineOperand *OrDst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
8100b57cec5SDimitry Andric     assert(OrDst && OrDst->isReg());
8110b57cec5SDimitry Andric 
8128bcb0991SDimitry Andric     return std::make_unique<SDWADstPreserveOperand>(
8130b57cec5SDimitry Andric       OrDst, OrSDWADef, OrOtherDef, DstSel);
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric   }
8160b57cec5SDimitry Andric   }
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric   return std::unique_ptr<SDWAOperand>(nullptr);
8190b57cec5SDimitry Andric }
8200b57cec5SDimitry Andric 
82147395794SDimitry Andric #if !defined(NDEBUG)
82247395794SDimitry Andric static raw_ostream& operator<<(raw_ostream &OS, const SDWAOperand &Operand) {
82347395794SDimitry Andric   Operand.print(OS);
82447395794SDimitry Andric   return OS;
82547395794SDimitry Andric }
82647395794SDimitry Andric #endif
82747395794SDimitry Andric 
8280b57cec5SDimitry Andric void SIPeepholeSDWA::matchSDWAOperands(MachineBasicBlock &MBB) {
8290b57cec5SDimitry Andric   for (MachineInstr &MI : MBB) {
8300b57cec5SDimitry Andric     if (auto Operand = matchSDWAOperand(MI)) {
8310b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Match: " << MI << "To: " << *Operand << '\n');
8320b57cec5SDimitry Andric       SDWAOperands[&MI] = std::move(Operand);
8330b57cec5SDimitry Andric       ++NumSDWAPatternsFound;
8340b57cec5SDimitry Andric     }
8350b57cec5SDimitry Andric   }
8360b57cec5SDimitry Andric }
8370b57cec5SDimitry Andric 
838bdd1243dSDimitry Andric // Convert the V_ADD_CO_U32_e64 into V_ADD_CO_U32_e32. This allows
839bdd1243dSDimitry Andric // isConvertibleToSDWA to perform its transformation on V_ADD_CO_U32_e32 into
840bdd1243dSDimitry Andric // V_ADD_CO_U32_sdwa.
8410b57cec5SDimitry Andric //
8420b57cec5SDimitry Andric // We are transforming from a VOP3 into a VOP2 form of the instruction.
8430b57cec5SDimitry Andric //   %19:vgpr_32 = V_AND_B32_e32 255,
8440b57cec5SDimitry Andric //       killed %16:vgpr_32, implicit $exec
845e8d8bef9SDimitry Andric //   %47:vgpr_32, %49:sreg_64_xexec = V_ADD_CO_U32_e64
8460b57cec5SDimitry Andric //       %26.sub0:vreg_64, %19:vgpr_32, implicit $exec
8470b57cec5SDimitry Andric //  %48:vgpr_32, dead %50:sreg_64_xexec = V_ADDC_U32_e64
8480b57cec5SDimitry Andric //       %26.sub1:vreg_64, %54:vgpr_32, killed %49:sreg_64_xexec, implicit $exec
8490b57cec5SDimitry Andric //
8500b57cec5SDimitry Andric // becomes
851e8d8bef9SDimitry Andric //   %47:vgpr_32 = V_ADD_CO_U32_sdwa
8520b57cec5SDimitry Andric //       0, %26.sub0:vreg_64, 0, killed %16:vgpr_32, 0, 6, 0, 6, 0,
8530b57cec5SDimitry Andric //       implicit-def $vcc, implicit $exec
854bdd1243dSDimitry Andric //  %48:vgpr_32, dead %50:sreg_64_xexec = V_ADDC_U32_e64
855bdd1243dSDimitry Andric //       %26.sub1:vreg_64, %54:vgpr_32, killed $vcc, implicit $exec
8560b57cec5SDimitry Andric void SIPeepholeSDWA::pseudoOpConvertToVOP2(MachineInstr &MI,
8570b57cec5SDimitry Andric                                            const GCNSubtarget &ST) const {
8580b57cec5SDimitry Andric   int Opc = MI.getOpcode();
859e8d8bef9SDimitry Andric   assert((Opc == AMDGPU::V_ADD_CO_U32_e64 || Opc == AMDGPU::V_SUB_CO_U32_e64) &&
860e8d8bef9SDimitry Andric          "Currently only handles V_ADD_CO_U32_e64 or V_SUB_CO_U32_e64");
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric   // Can the candidate MI be shrunk?
8630b57cec5SDimitry Andric   if (!TII->canShrink(MI, *MRI))
8640b57cec5SDimitry Andric     return;
8650b57cec5SDimitry Andric   Opc = AMDGPU::getVOPe32(Opc);
8660b57cec5SDimitry Andric   // Find the related ADD instruction.
8670b57cec5SDimitry Andric   const MachineOperand *Sdst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
8680b57cec5SDimitry Andric   if (!Sdst)
8690b57cec5SDimitry Andric     return;
8700b57cec5SDimitry Andric   MachineOperand *NextOp = findSingleRegUse(Sdst, MRI);
8710b57cec5SDimitry Andric   if (!NextOp)
8720b57cec5SDimitry Andric     return;
8730b57cec5SDimitry Andric   MachineInstr &MISucc = *NextOp->getParent();
874bdd1243dSDimitry Andric 
8750b57cec5SDimitry Andric   // Make sure the carry in/out are subsequently unused.
8760b57cec5SDimitry Andric   MachineOperand *CarryIn = TII->getNamedOperand(MISucc, AMDGPU::OpName::src2);
8770b57cec5SDimitry Andric   if (!CarryIn)
8780b57cec5SDimitry Andric     return;
8790b57cec5SDimitry Andric   MachineOperand *CarryOut = TII->getNamedOperand(MISucc, AMDGPU::OpName::sdst);
8800b57cec5SDimitry Andric   if (!CarryOut)
8810b57cec5SDimitry Andric     return;
8820b57cec5SDimitry Andric   if (!MRI->hasOneUse(CarryIn->getReg()) || !MRI->use_empty(CarryOut->getReg()))
8830b57cec5SDimitry Andric     return;
8840b57cec5SDimitry Andric   // Make sure VCC or its subregs are dead before MI.
8850b57cec5SDimitry Andric   MachineBasicBlock &MBB = *MI.getParent();
8860b57cec5SDimitry Andric   auto Liveness = MBB.computeRegisterLiveness(TRI, AMDGPU::VCC, MI, 25);
8870b57cec5SDimitry Andric   if (Liveness != MachineBasicBlock::LQR_Dead)
8880b57cec5SDimitry Andric     return;
8890b57cec5SDimitry Andric   // Check if VCC is referenced in range of (MI,MISucc].
8900b57cec5SDimitry Andric   for (auto I = std::next(MI.getIterator()), E = MISucc.getIterator();
8910b57cec5SDimitry Andric        I != E; ++I) {
8920b57cec5SDimitry Andric     if (I->modifiesRegister(AMDGPU::VCC, TRI))
8930b57cec5SDimitry Andric       return;
8940b57cec5SDimitry Andric   }
8955ffd83dbSDimitry Andric 
8960b57cec5SDimitry Andric   // Replace MI with V_{SUB|ADD}_I32_e32
8975ffd83dbSDimitry Andric   BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(Opc))
8985ffd83dbSDimitry Andric     .add(*TII->getNamedOperand(MI, AMDGPU::OpName::vdst))
8995ffd83dbSDimitry Andric     .add(*TII->getNamedOperand(MI, AMDGPU::OpName::src0))
9005ffd83dbSDimitry Andric     .add(*TII->getNamedOperand(MI, AMDGPU::OpName::src1))
9015ffd83dbSDimitry Andric     .setMIFlags(MI.getFlags());
9025ffd83dbSDimitry Andric 
9030b57cec5SDimitry Andric   MI.eraseFromParent();
9045ffd83dbSDimitry Andric 
905bdd1243dSDimitry Andric   // Since the carry output of MI is now VCC, update its use in MISucc.
9065ffd83dbSDimitry Andric 
907bdd1243dSDimitry Andric   MISucc.substituteRegister(CarryIn->getReg(), TRI->getVCC(), 0, *TRI);
9080b57cec5SDimitry Andric }
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric bool SIPeepholeSDWA::isConvertibleToSDWA(MachineInstr &MI,
9110b57cec5SDimitry Andric                                          const GCNSubtarget &ST) const {
9120b57cec5SDimitry Andric   // Check if this is already an SDWA instruction
9130b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
9140b57cec5SDimitry Andric   if (TII->isSDWA(Opc))
9150b57cec5SDimitry Andric     return true;
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   // Check if this instruction has opcode that supports SDWA
9180b57cec5SDimitry Andric   if (AMDGPU::getSDWAOp(Opc) == -1)
9190b57cec5SDimitry Andric     Opc = AMDGPU::getVOPe32(Opc);
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   if (AMDGPU::getSDWAOp(Opc) == -1)
9220b57cec5SDimitry Andric     return false;
9230b57cec5SDimitry Andric 
9240b57cec5SDimitry Andric   if (!ST.hasSDWAOmod() && TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
9250b57cec5SDimitry Andric     return false;
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   if (TII->isVOPC(Opc)) {
9280b57cec5SDimitry Andric     if (!ST.hasSDWASdst()) {
9290b57cec5SDimitry Andric       const MachineOperand *SDst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst);
9300b57cec5SDimitry Andric       if (SDst && (SDst->getReg() != AMDGPU::VCC &&
9310b57cec5SDimitry Andric                    SDst->getReg() != AMDGPU::VCC_LO))
9320b57cec5SDimitry Andric         return false;
9330b57cec5SDimitry Andric     }
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric     if (!ST.hasSDWAOutModsVOPC() &&
9360b57cec5SDimitry Andric         (TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) ||
9370b57cec5SDimitry Andric          TII->hasModifiersSet(MI, AMDGPU::OpName::omod)))
9380b57cec5SDimitry Andric       return false;
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   } else if (TII->getNamedOperand(MI, AMDGPU::OpName::sdst) ||
9410b57cec5SDimitry Andric              !TII->getNamedOperand(MI, AMDGPU::OpName::vdst)) {
9420b57cec5SDimitry Andric     return false;
9430b57cec5SDimitry Andric   }
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric   if (!ST.hasSDWAMac() && (Opc == AMDGPU::V_FMAC_F16_e32 ||
9460b57cec5SDimitry Andric                            Opc == AMDGPU::V_FMAC_F32_e32 ||
9470b57cec5SDimitry Andric                            Opc == AMDGPU::V_MAC_F16_e32 ||
9480b57cec5SDimitry Andric                            Opc == AMDGPU::V_MAC_F32_e32))
9490b57cec5SDimitry Andric     return false;
9500b57cec5SDimitry Andric 
9510b57cec5SDimitry Andric   // Check if target supports this SDWA opcode
9520b57cec5SDimitry Andric   if (TII->pseudoToMCOpcode(Opc) == -1)
9530b57cec5SDimitry Andric     return false;
9540b57cec5SDimitry Andric 
9550b57cec5SDimitry Andric   // FIXME: has SDWA but require handling of implicit VCC use
9560b57cec5SDimitry Andric   if (Opc == AMDGPU::V_CNDMASK_B32_e32)
9570b57cec5SDimitry Andric     return false;
9580b57cec5SDimitry Andric 
959e8d8bef9SDimitry Andric   if (MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0)) {
960e8d8bef9SDimitry Andric     if (!Src0->isReg() && !Src0->isImm())
961e8d8bef9SDimitry Andric       return false;
962e8d8bef9SDimitry Andric   }
963e8d8bef9SDimitry Andric 
964e8d8bef9SDimitry Andric   if (MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1)) {
965e8d8bef9SDimitry Andric     if (!Src1->isReg() && !Src1->isImm())
966e8d8bef9SDimitry Andric       return false;
967e8d8bef9SDimitry Andric   }
968e8d8bef9SDimitry Andric 
9690b57cec5SDimitry Andric   return true;
9700b57cec5SDimitry Andric }
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric bool SIPeepholeSDWA::convertToSDWA(MachineInstr &MI,
9730b57cec5SDimitry Andric                                    const SDWAOperandsVector &SDWAOperands) {
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Convert instruction:" << MI);
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   // Convert to sdwa
9780b57cec5SDimitry Andric   int SDWAOpcode;
9790b57cec5SDimitry Andric   unsigned Opcode = MI.getOpcode();
9800b57cec5SDimitry Andric   if (TII->isSDWA(Opcode)) {
9810b57cec5SDimitry Andric     SDWAOpcode = Opcode;
9820b57cec5SDimitry Andric   } else {
9830b57cec5SDimitry Andric     SDWAOpcode = AMDGPU::getSDWAOp(Opcode);
9840b57cec5SDimitry Andric     if (SDWAOpcode == -1)
9850b57cec5SDimitry Andric       SDWAOpcode = AMDGPU::getSDWAOp(AMDGPU::getVOPe32(Opcode));
9860b57cec5SDimitry Andric   }
9870b57cec5SDimitry Andric   assert(SDWAOpcode != -1);
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   const MCInstrDesc &SDWADesc = TII->get(SDWAOpcode);
9900b57cec5SDimitry Andric 
9910b57cec5SDimitry Andric   // Create SDWA version of instruction MI and initialize its operands
9920b57cec5SDimitry Andric   MachineInstrBuilder SDWAInst =
9935ffd83dbSDimitry Andric     BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), SDWADesc)
9945ffd83dbSDimitry Andric     .setMIFlags(MI.getFlags());
9950b57cec5SDimitry Andric 
9960b57cec5SDimitry Andric   // Copy dst, if it is present in original then should also be present in SDWA
9970b57cec5SDimitry Andric   MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
9980b57cec5SDimitry Andric   if (Dst) {
999bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::vdst));
10000b57cec5SDimitry Andric     SDWAInst.add(*Dst);
10010b57cec5SDimitry Andric   } else if ((Dst = TII->getNamedOperand(MI, AMDGPU::OpName::sdst))) {
1002bdd1243dSDimitry Andric     assert(Dst && AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::sdst));
10030b57cec5SDimitry Andric     SDWAInst.add(*Dst);
10040b57cec5SDimitry Andric   } else {
1005bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::sdst));
10060b57cec5SDimitry Andric     SDWAInst.addReg(TRI->getVCC(), RegState::Define);
10070b57cec5SDimitry Andric   }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric   // Copy src0, initialize src0_modifiers. All sdwa instructions has src0 and
10100b57cec5SDimitry Andric   // src0_modifiers (except for v_nop_sdwa, but it can't get here)
10110b57cec5SDimitry Andric   MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
1012bdd1243dSDimitry Andric   assert(Src0 && AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src0) &&
1013bdd1243dSDimitry Andric          AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src0_modifiers));
10140b57cec5SDimitry Andric   if (auto *Mod = TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers))
10150b57cec5SDimitry Andric     SDWAInst.addImm(Mod->getImm());
10160b57cec5SDimitry Andric   else
10170b57cec5SDimitry Andric     SDWAInst.addImm(0);
10180b57cec5SDimitry Andric   SDWAInst.add(*Src0);
10190b57cec5SDimitry Andric 
10200b57cec5SDimitry Andric   // Copy src1 if present, initialize src1_modifiers.
10210b57cec5SDimitry Andric   MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
10220b57cec5SDimitry Andric   if (Src1) {
1023bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src1) &&
1024bdd1243dSDimitry Andric            AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src1_modifiers));
10250b57cec5SDimitry Andric     if (auto *Mod = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers))
10260b57cec5SDimitry Andric       SDWAInst.addImm(Mod->getImm());
10270b57cec5SDimitry Andric     else
10280b57cec5SDimitry Andric       SDWAInst.addImm(0);
10290b57cec5SDimitry Andric     SDWAInst.add(*Src1);
10300b57cec5SDimitry Andric   }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric   if (SDWAOpcode == AMDGPU::V_FMAC_F16_sdwa ||
10330b57cec5SDimitry Andric       SDWAOpcode == AMDGPU::V_FMAC_F32_sdwa ||
10340b57cec5SDimitry Andric       SDWAOpcode == AMDGPU::V_MAC_F16_sdwa ||
10350b57cec5SDimitry Andric       SDWAOpcode == AMDGPU::V_MAC_F32_sdwa) {
10360b57cec5SDimitry Andric     // v_mac_f16/32 has additional src2 operand tied to vdst
10370b57cec5SDimitry Andric     MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
10380b57cec5SDimitry Andric     assert(Src2);
10390b57cec5SDimitry Andric     SDWAInst.add(*Src2);
10400b57cec5SDimitry Andric   }
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric   // Copy clamp if present, initialize otherwise
1043bdd1243dSDimitry Andric   assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::clamp));
10440b57cec5SDimitry Andric   MachineOperand *Clamp = TII->getNamedOperand(MI, AMDGPU::OpName::clamp);
10450b57cec5SDimitry Andric   if (Clamp) {
10460b57cec5SDimitry Andric     SDWAInst.add(*Clamp);
10470b57cec5SDimitry Andric   } else {
10480b57cec5SDimitry Andric     SDWAInst.addImm(0);
10490b57cec5SDimitry Andric   }
10500b57cec5SDimitry Andric 
10510b57cec5SDimitry Andric   // Copy omod if present, initialize otherwise if needed
1052bdd1243dSDimitry Andric   if (AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::omod)) {
10530b57cec5SDimitry Andric     MachineOperand *OMod = TII->getNamedOperand(MI, AMDGPU::OpName::omod);
10540b57cec5SDimitry Andric     if (OMod) {
10550b57cec5SDimitry Andric       SDWAInst.add(*OMod);
10560b57cec5SDimitry Andric     } else {
10570b57cec5SDimitry Andric       SDWAInst.addImm(0);
10580b57cec5SDimitry Andric     }
10590b57cec5SDimitry Andric   }
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric   // Copy dst_sel if present, initialize otherwise if needed
1062bdd1243dSDimitry Andric   if (AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::dst_sel)) {
10630b57cec5SDimitry Andric     MachineOperand *DstSel = TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel);
10640b57cec5SDimitry Andric     if (DstSel) {
10650b57cec5SDimitry Andric       SDWAInst.add(*DstSel);
10660b57cec5SDimitry Andric     } else {
10670b57cec5SDimitry Andric       SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
10680b57cec5SDimitry Andric     }
10690b57cec5SDimitry Andric   }
10700b57cec5SDimitry Andric 
10710b57cec5SDimitry Andric   // Copy dst_unused if present, initialize otherwise if needed
1072bdd1243dSDimitry Andric   if (AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::dst_unused)) {
10730b57cec5SDimitry Andric     MachineOperand *DstUnused = TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
10740b57cec5SDimitry Andric     if (DstUnused) {
10750b57cec5SDimitry Andric       SDWAInst.add(*DstUnused);
10760b57cec5SDimitry Andric     } else {
10770b57cec5SDimitry Andric       SDWAInst.addImm(AMDGPU::SDWA::DstUnused::UNUSED_PAD);
10780b57cec5SDimitry Andric     }
10790b57cec5SDimitry Andric   }
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric   // Copy src0_sel if present, initialize otherwise
1082bdd1243dSDimitry Andric   assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src0_sel));
10830b57cec5SDimitry Andric   MachineOperand *Src0Sel = TII->getNamedOperand(MI, AMDGPU::OpName::src0_sel);
10840b57cec5SDimitry Andric   if (Src0Sel) {
10850b57cec5SDimitry Andric     SDWAInst.add(*Src0Sel);
10860b57cec5SDimitry Andric   } else {
10870b57cec5SDimitry Andric     SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
10880b57cec5SDimitry Andric   }
10890b57cec5SDimitry Andric 
10900b57cec5SDimitry Andric   // Copy src1_sel if present, initialize otherwise if needed
10910b57cec5SDimitry Andric   if (Src1) {
1092bdd1243dSDimitry Andric     assert(AMDGPU::hasNamedOperand(SDWAOpcode, AMDGPU::OpName::src1_sel));
10930b57cec5SDimitry Andric     MachineOperand *Src1Sel = TII->getNamedOperand(MI, AMDGPU::OpName::src1_sel);
10940b57cec5SDimitry Andric     if (Src1Sel) {
10950b57cec5SDimitry Andric       SDWAInst.add(*Src1Sel);
10960b57cec5SDimitry Andric     } else {
10970b57cec5SDimitry Andric       SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
10980b57cec5SDimitry Andric     }
10990b57cec5SDimitry Andric   }
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric   // Check for a preserved register that needs to be copied.
11020b57cec5SDimitry Andric   auto DstUnused = TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
11030b57cec5SDimitry Andric   if (DstUnused &&
11040b57cec5SDimitry Andric       DstUnused->getImm() == AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE) {
11050b57cec5SDimitry Andric     // We expect, if we are here, that the instruction was already in it's SDWA form,
11060b57cec5SDimitry Andric     // with a tied operand.
11070b57cec5SDimitry Andric     assert(Dst && Dst->isTied());
11080b57cec5SDimitry Andric     assert(Opcode == static_cast<unsigned int>(SDWAOpcode));
11090b57cec5SDimitry Andric     // We also expect a vdst, since sdst can't preserve.
11100b57cec5SDimitry Andric     auto PreserveDstIdx = AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::vdst);
11110b57cec5SDimitry Andric     assert(PreserveDstIdx != -1);
11120b57cec5SDimitry Andric 
11130b57cec5SDimitry Andric     auto TiedIdx = MI.findTiedOperandIdx(PreserveDstIdx);
11140b57cec5SDimitry Andric     auto Tied = MI.getOperand(TiedIdx);
11150b57cec5SDimitry Andric 
11160b57cec5SDimitry Andric     SDWAInst.add(Tied);
11170b57cec5SDimitry Andric     SDWAInst->tieOperands(PreserveDstIdx, SDWAInst->getNumOperands() - 1);
11180b57cec5SDimitry Andric   }
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric   // Apply all sdwa operand patterns.
11210b57cec5SDimitry Andric   bool Converted = false;
11220b57cec5SDimitry Andric   for (auto &Operand : SDWAOperands) {
11230b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << *SDWAInst << "\nOperand: " << *Operand);
112481ad6265SDimitry Andric     // There should be no intersection between SDWA operands and potential MIs
11250b57cec5SDimitry Andric     // e.g.:
11260b57cec5SDimitry Andric     // v_and_b32 v0, 0xff, v1 -> src:v1 sel:BYTE_0
11270b57cec5SDimitry Andric     // v_and_b32 v2, 0xff, v0 -> src:v0 sel:BYTE_0
11280b57cec5SDimitry Andric     // v_add_u32 v3, v4, v2
11290b57cec5SDimitry Andric     //
113081ad6265SDimitry Andric     // In that example it is possible that we would fold 2nd instruction into
113181ad6265SDimitry Andric     // 3rd (v_add_u32_sdwa) and then try to fold 1st instruction into 2nd (that
113281ad6265SDimitry Andric     // was already destroyed). So if SDWAOperand is also a potential MI then do
113381ad6265SDimitry Andric     // not apply it.
11340b57cec5SDimitry Andric     if (PotentialMatches.count(Operand->getParentInst()) == 0)
11350b57cec5SDimitry Andric       Converted |= Operand->convertToSDWA(*SDWAInst, TII);
11360b57cec5SDimitry Andric   }
11370b57cec5SDimitry Andric   if (Converted) {
11380b57cec5SDimitry Andric     ConvertedInstructions.push_back(SDWAInst);
11390b57cec5SDimitry Andric   } else {
11400b57cec5SDimitry Andric     SDWAInst->eraseFromParent();
11410b57cec5SDimitry Andric     return false;
11420b57cec5SDimitry Andric   }
11430b57cec5SDimitry Andric 
11440b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nInto:" << *SDWAInst << '\n');
11450b57cec5SDimitry Andric   ++NumSDWAInstructionsPeepholed;
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric   MI.eraseFromParent();
11480b57cec5SDimitry Andric   return true;
11490b57cec5SDimitry Andric }
11500b57cec5SDimitry Andric 
11510b57cec5SDimitry Andric // If an instruction was converted to SDWA it should not have immediates or SGPR
11520b57cec5SDimitry Andric // operands (allowed one SGPR on GFX9). Copy its scalar operands into VGPRs.
11530b57cec5SDimitry Andric void SIPeepholeSDWA::legalizeScalarOperands(MachineInstr &MI,
11540b57cec5SDimitry Andric                                             const GCNSubtarget &ST) const {
11550b57cec5SDimitry Andric   const MCInstrDesc &Desc = TII->get(MI.getOpcode());
11560b57cec5SDimitry Andric   unsigned ConstantBusCount = 0;
11570b57cec5SDimitry Andric   for (MachineOperand &Op : MI.explicit_uses()) {
11580b57cec5SDimitry Andric     if (!Op.isImm() && !(Op.isReg() && !TRI->isVGPR(*MRI, Op.getReg())))
11590b57cec5SDimitry Andric       continue;
11600b57cec5SDimitry Andric 
1161*06c3fb27SDimitry Andric     unsigned I = Op.getOperandNo();
1162bdd1243dSDimitry Andric     if (Desc.operands()[I].RegClass == -1 ||
1163bdd1243dSDimitry Andric         !TRI->isVSSuperClass(TRI->getRegClass(Desc.operands()[I].RegClass)))
11640b57cec5SDimitry Andric       continue;
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric     if (ST.hasSDWAScalar() && ConstantBusCount == 0 && Op.isReg() &&
11670b57cec5SDimitry Andric         TRI->isSGPRReg(*MRI, Op.getReg())) {
11680b57cec5SDimitry Andric       ++ConstantBusCount;
11690b57cec5SDimitry Andric       continue;
11700b57cec5SDimitry Andric     }
11710b57cec5SDimitry Andric 
11728bcb0991SDimitry Andric     Register VGPR = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
11730b57cec5SDimitry Andric     auto Copy = BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
11740b57cec5SDimitry Andric                         TII->get(AMDGPU::V_MOV_B32_e32), VGPR);
11750b57cec5SDimitry Andric     if (Op.isImm())
11760b57cec5SDimitry Andric       Copy.addImm(Op.getImm());
11770b57cec5SDimitry Andric     else if (Op.isReg())
11780b57cec5SDimitry Andric       Copy.addReg(Op.getReg(), Op.isKill() ? RegState::Kill : 0,
11790b57cec5SDimitry Andric                   Op.getSubReg());
11800b57cec5SDimitry Andric     Op.ChangeToRegister(VGPR, false);
11810b57cec5SDimitry Andric   }
11820b57cec5SDimitry Andric }
11830b57cec5SDimitry Andric 
11840b57cec5SDimitry Andric bool SIPeepholeSDWA::runOnMachineFunction(MachineFunction &MF) {
11850b57cec5SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric   if (!ST.hasSDWA() || skipFunction(MF.getFunction()))
11880b57cec5SDimitry Andric     return false;
11890b57cec5SDimitry Andric 
11900b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
11910b57cec5SDimitry Andric   TRI = ST.getRegisterInfo();
11920b57cec5SDimitry Andric   TII = ST.getInstrInfo();
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric   // Find all SDWA operands in MF.
11950b57cec5SDimitry Andric   bool Ret = false;
11960b57cec5SDimitry Andric   for (MachineBasicBlock &MBB : MF) {
11970b57cec5SDimitry Andric     bool Changed = false;
11980b57cec5SDimitry Andric     do {
11990b57cec5SDimitry Andric       // Preprocess the ADD/SUB pairs so they could be SDWA'ed.
12000b57cec5SDimitry Andric       // Look for a possible ADD or SUB that resulted from a previously lowered
12010b57cec5SDimitry Andric       // V_{ADD|SUB}_U64_PSEUDO. The function pseudoOpConvertToVOP2
12020b57cec5SDimitry Andric       // lowers the pair of instructions into e32 form.
12030b57cec5SDimitry Andric       matchSDWAOperands(MBB);
12040b57cec5SDimitry Andric       for (const auto &OperandPair : SDWAOperands) {
12050b57cec5SDimitry Andric         const auto &Operand = OperandPair.second;
12060b57cec5SDimitry Andric         MachineInstr *PotentialMI = Operand->potentialToConvert(TII);
12070b57cec5SDimitry Andric         if (PotentialMI &&
1208e8d8bef9SDimitry Andric            (PotentialMI->getOpcode() == AMDGPU::V_ADD_CO_U32_e64 ||
1209e8d8bef9SDimitry Andric             PotentialMI->getOpcode() == AMDGPU::V_SUB_CO_U32_e64))
12100b57cec5SDimitry Andric           pseudoOpConvertToVOP2(*PotentialMI, ST);
12110b57cec5SDimitry Andric       }
12120b57cec5SDimitry Andric       SDWAOperands.clear();
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric       // Generate potential match list.
12150b57cec5SDimitry Andric       matchSDWAOperands(MBB);
12160b57cec5SDimitry Andric 
12170b57cec5SDimitry Andric       for (const auto &OperandPair : SDWAOperands) {
12180b57cec5SDimitry Andric         const auto &Operand = OperandPair.second;
12190b57cec5SDimitry Andric         MachineInstr *PotentialMI = Operand->potentialToConvert(TII);
12200b57cec5SDimitry Andric         if (PotentialMI && isConvertibleToSDWA(*PotentialMI, ST)) {
12210b57cec5SDimitry Andric           PotentialMatches[PotentialMI].push_back(Operand.get());
12220b57cec5SDimitry Andric         }
12230b57cec5SDimitry Andric       }
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric       for (auto &PotentialPair : PotentialMatches) {
12260b57cec5SDimitry Andric         MachineInstr &PotentialMI = *PotentialPair.first;
12270b57cec5SDimitry Andric         convertToSDWA(PotentialMI, PotentialPair.second);
12280b57cec5SDimitry Andric       }
12290b57cec5SDimitry Andric 
12300b57cec5SDimitry Andric       PotentialMatches.clear();
12310b57cec5SDimitry Andric       SDWAOperands.clear();
12320b57cec5SDimitry Andric 
12330b57cec5SDimitry Andric       Changed = !ConvertedInstructions.empty();
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric       if (Changed)
12360b57cec5SDimitry Andric         Ret = true;
12370b57cec5SDimitry Andric       while (!ConvertedInstructions.empty())
12380b57cec5SDimitry Andric         legalizeScalarOperands(*ConvertedInstructions.pop_back_val(), ST);
12390b57cec5SDimitry Andric     } while (Changed);
12400b57cec5SDimitry Andric   }
12410b57cec5SDimitry Andric 
12420b57cec5SDimitry Andric   return Ret;
12430b57cec5SDimitry Andric }
1244