1 //===-- SIPeepholeSDWA.cpp - Peephole optimization for SDWA instructions --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This pass tries to apply several peephole SDWA patterns.
13 /// V_LSHRREV_B32_e32 %vreg0, 16, %vreg1
14 /// V_ADD_I32_e32 %vreg2, %vreg0, %vreg3
15 /// V_LSHLREV_B32_e32 %vreg4, 16, %vreg2
18 /// V_ADD_I32_sdwa %vreg4, %vreg1, %vreg3
19 /// dst_sel:WORD_1 dst_unused:UNUSED_PAD src0_sel:WORD_1 src1_sel:DWORD
21 //===----------------------------------------------------------------------===//
25 #include "AMDGPUSubtarget.h"
26 #include "SIDefines.h"
27 #include "SIInstrInfo.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/CodeGen/MachineFunctionPass.h"
31 #include "llvm/CodeGen/MachineInstrBuilder.h"
32 #include <unordered_map>
33 #include <unordered_set>
37 #define DEBUG_TYPE "si-peephole-sdwa"
39 STATISTIC(NumSDWAPatternsFound, "Number of SDWA patterns found.");
40 STATISTIC(NumSDWAInstructionsPeepholed,
41 "Number of instruction converted to SDWA.");
47 class SIPeepholeSDWA : public MachineFunctionPass {
49 typedef SmallVector<SDWAOperand *, 4> SDWAOperandsVector;
52 MachineRegisterInfo *MRI;
53 const SIRegisterInfo *TRI;
54 const SIInstrInfo *TII;
56 std::unordered_map<MachineInstr *, std::unique_ptr<SDWAOperand>> SDWAOperands;
57 std::unordered_map<MachineInstr *, SDWAOperandsVector> PotentialMatches;
59 Optional<int64_t> foldToImm(const MachineOperand &Op) const;
64 SIPeepholeSDWA() : MachineFunctionPass(ID) {
65 initializeSIPeepholeSDWAPass(*PassRegistry::getPassRegistry());
68 bool runOnMachineFunction(MachineFunction &MF) override;
69 void matchSDWAOperands(MachineFunction &MF);
70 bool isConvertibleToSDWA(const MachineInstr &MI) const;
71 bool convertToSDWA(MachineInstr &MI, const SDWAOperandsVector &SDWAOperands);
73 StringRef getPassName() const override { return "SI Peephole SDWA"; }
75 void getAnalysisUsage(AnalysisUsage &AU) const override {
77 MachineFunctionPass::getAnalysisUsage(AU);
83 MachineOperand *Target; // Operand that would be used in converted instruction
84 MachineOperand *Replaced; // Operand that would be replace by Target
87 SDWAOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp)
88 : Target(TargetOp), Replaced(ReplacedOp) {
89 assert(Target->isReg());
90 assert(Replaced->isReg());
93 virtual ~SDWAOperand() {}
95 virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) = 0;
96 virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) = 0;
98 MachineOperand *getTargetOperand() const { return Target; }
99 MachineOperand *getReplacedOperand() const { return Replaced; }
100 MachineInstr *getParentInst() const { return Target->getParent(); }
101 MachineRegisterInfo *getMRI() const {
102 return &getParentInst()->getParent()->getParent()->getRegInfo();
106 using namespace AMDGPU::SDWA;
108 class SDWASrcOperand : public SDWAOperand {
116 SDWASrcOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
117 SdwaSel SrcSel_ = DWORD, bool Abs_ = false, bool Neg_ = false,
119 : SDWAOperand(TargetOp, ReplacedOp), SrcSel(SrcSel_), Abs(Abs_),
120 Neg(Neg_), Sext(Sext_) {}
122 virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) override;
123 virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
125 SdwaSel getSrcSel() const { return SrcSel; }
126 bool getAbs() const { return Abs; }
127 bool getNeg() const { return Neg; }
128 bool getSext() const { return Sext; }
130 uint64_t getSrcMods() const;
133 class SDWADstOperand : public SDWAOperand {
139 SDWADstOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
140 SdwaSel DstSel_ = DWORD, DstUnused DstUn_ = UNUSED_PAD)
141 : SDWAOperand(TargetOp, ReplacedOp), DstSel(DstSel_), DstUn(DstUn_) {}
143 virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) override;
144 virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
146 SdwaSel getDstSel() const { return DstSel; }
147 DstUnused getDstUnused() const { return DstUn; }
150 } // End anonymous namespace.
152 INITIALIZE_PASS(SIPeepholeSDWA, DEBUG_TYPE, "SI Peephole SDWA", false, false)
154 char SIPeepholeSDWA::ID = 0;
156 char &llvm::SIPeepholeSDWAID = SIPeepholeSDWA::ID;
158 FunctionPass *llvm::createSIPeepholeSDWAPass() {
159 return new SIPeepholeSDWA();
164 static raw_ostream& operator<<(raw_ostream &OS, const SdwaSel &Sel) {
166 case BYTE_0: OS << "BYTE_0"; break;
167 case BYTE_1: OS << "BYTE_1"; break;
168 case BYTE_2: OS << "BYTE_2"; break;
169 case BYTE_3: OS << "BYTE_3"; break;
170 case WORD_0: OS << "WORD_0"; break;
171 case WORD_1: OS << "WORD_1"; break;
172 case DWORD: OS << "DWORD"; break;
177 static raw_ostream& operator<<(raw_ostream &OS, const DstUnused &Un) {
179 case UNUSED_PAD: OS << "UNUSED_PAD"; break;
180 case UNUSED_SEXT: OS << "UNUSED_SEXT"; break;
181 case UNUSED_PRESERVE: OS << "UNUSED_PRESERVE"; break;
186 static raw_ostream& operator<<(raw_ostream &OS, const SDWASrcOperand &Src) {
187 OS << "SDWA src: " << *Src.getTargetOperand()
188 << " src_sel:" << Src.getSrcSel()
189 << " abs:" << Src.getAbs() << " neg:" << Src.getNeg()
190 << " sext:" << Src.getSext() << '\n';
194 static raw_ostream& operator<<(raw_ostream &OS, const SDWADstOperand &Dst) {
195 OS << "SDWA dst: " << *Dst.getTargetOperand()
196 << " dst_sel:" << Dst.getDstSel()
197 << " dst_unused:" << Dst.getDstUnused() << '\n';
203 static void copyRegOperand(MachineOperand &To, const MachineOperand &From) {
204 assert(To.isReg() && From.isReg());
205 To.setReg(From.getReg());
206 To.setSubReg(From.getSubReg());
207 To.setIsUndef(From.isUndef());
209 To.setIsKill(From.isKill());
211 To.setIsDead(From.isDead());
215 static bool isSameReg(const MachineOperand &LHS, const MachineOperand &RHS) {
216 return LHS.isReg() &&
218 LHS.getReg() == RHS.getReg() &&
219 LHS.getSubReg() == RHS.getSubReg();
222 static bool isSubregOf(const MachineOperand &SubReg,
223 const MachineOperand &SuperReg,
224 const TargetRegisterInfo *TRI) {
226 if (!SuperReg.isReg() || !SubReg.isReg())
229 if (isSameReg(SuperReg, SubReg))
232 if (SuperReg.getReg() != SubReg.getReg())
235 LaneBitmask SuperMask = TRI->getSubRegIndexLaneMask(SuperReg.getSubReg());
236 LaneBitmask SubMask = TRI->getSubRegIndexLaneMask(SubReg.getSubReg());
237 SuperMask |= ~SubMask;
238 return SuperMask.all();
241 uint64_t SDWASrcOperand::getSrcMods() const {
245 "Float and integer src modifiers can't be set simulteniously");
246 Mods |= Abs ? SISrcMods::ABS : 0;
247 Mods |= Neg ? SISrcMods::NEG : 0;
249 Mods |= SISrcMods::SEXT;
255 MachineInstr *SDWASrcOperand::potentialToConvert(const SIInstrInfo *TII) {
256 // For SDWA src operand potential instruction is one that use register
257 // defined by parent instruction
258 MachineRegisterInfo *MRI = getMRI();
259 MachineOperand *Replaced = getReplacedOperand();
260 assert(Replaced->isReg());
262 MachineInstr *PotentialMI = nullptr;
263 for (MachineOperand &PotentialMO : MRI->use_operands(Replaced->getReg())) {
264 // If this is use of another subreg of dst reg then do nothing
265 if (!isSubregOf(*Replaced, PotentialMO, MRI->getTargetRegisterInfo()))
268 // If there exist use of superreg of dst then we should not combine this
270 if (!isSameReg(PotentialMO, *Replaced))
273 // Check that PotentialMI is only instruction that uses dst reg
274 if (PotentialMI == nullptr) {
275 PotentialMI = PotentialMO.getParent();
276 } else if (PotentialMI != PotentialMO.getParent()) {
284 bool SDWASrcOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
285 // Find operand in instruction that matches source operand and replace it with
286 // target operand. Set corresponding src_sel
288 MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
289 MachineOperand *SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src0_sel);
290 MachineOperand *SrcMods =
291 TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
292 assert(Src && Src->isReg());
293 if (!isSameReg(*Src, *getReplacedOperand())) {
294 // If this is not src0 then it should be src1
295 Src = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
296 SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src1_sel);
297 SrcMods = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
299 assert(Src && Src->isReg());
301 if ((MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
302 MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
303 !isSameReg(*Src, *getReplacedOperand())) {
304 // In case of v_mac_f16/32_sdwa this pass can try to apply src operand to
305 // src2. This is not allowed.
309 assert(isSameReg(*Src, *getReplacedOperand()) && SrcSel && SrcMods);
311 copyRegOperand(*Src, *getTargetOperand());
312 SrcSel->setImm(getSrcSel());
313 SrcMods->setImm(getSrcMods());
314 getTargetOperand()->setIsKill(false);
318 MachineInstr *SDWADstOperand::potentialToConvert(const SIInstrInfo *TII) {
319 // For SDWA dst operand potential instruction is one that defines register
320 // that this operand uses
321 MachineRegisterInfo *MRI = getMRI();
322 MachineInstr *ParentMI = getParentInst();
323 MachineOperand *Replaced = getReplacedOperand();
324 assert(Replaced->isReg());
326 for (MachineOperand &PotentialMO : MRI->def_operands(Replaced->getReg())) {
327 if (!isSubregOf(*Replaced, PotentialMO, MRI->getTargetRegisterInfo()))
330 if (!isSameReg(*Replaced, PotentialMO))
333 // Check that ParentMI is the only instruction that uses replaced register
334 for (MachineOperand &UseMO : MRI->use_operands(PotentialMO.getReg())) {
335 if (isSubregOf(UseMO, PotentialMO, MRI->getTargetRegisterInfo()) &&
336 UseMO.getParent() != ParentMI) {
341 // Due to SSA this should be onle def of replaced register, so return it
342 return PotentialMO.getParent();
348 bool SDWADstOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
349 // Replace vdst operand in MI with target operand. Set dst_sel and dst_unused
351 if ((MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
352 MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
353 getDstSel() != AMDGPU::SDWA::DWORD) {
354 // v_mac_f16/32_sdwa allow dst_sel to be equal only to DWORD
358 MachineOperand *Operand = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
361 isSameReg(*Operand, *getReplacedOperand()));
362 copyRegOperand(*Operand, *getTargetOperand());
363 MachineOperand *DstSel= TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel);
365 DstSel->setImm(getDstSel());
366 MachineOperand *DstUnused= TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
368 DstUnused->setImm(getDstUnused());
370 // Remove original instruction because it would conflict with our new
371 // instruction by register definition
372 getParentInst()->eraseFromParent();
376 Optional<int64_t> SIPeepholeSDWA::foldToImm(const MachineOperand &Op) const {
381 // If this is not immediate then it can be copy of immediate value, e.g.:
382 // %vreg1<def> = S_MOV_B32 255;
384 for (const MachineOperand &Def : MRI->def_operands(Op.getReg())) {
385 if (!isSameReg(Op, Def))
388 const MachineInstr *DefInst = Def.getParent();
389 if (!TII->isFoldableCopy(*DefInst))
392 const MachineOperand &Copied = DefInst->getOperand(1);
396 return Copied.getImm();
403 void SIPeepholeSDWA::matchSDWAOperands(MachineFunction &MF) {
404 for (MachineBasicBlock &MBB : MF) {
405 for (MachineInstr &MI : MBB) {
406 unsigned Opcode = MI.getOpcode();
408 case AMDGPU::V_LSHRREV_B32_e32:
409 case AMDGPU::V_ASHRREV_I32_e32:
410 case AMDGPU::V_LSHLREV_B32_e32: {
411 // from: v_lshrrev_b32_e32 v1, 16/24, v0
412 // to SDWA src:v0 src_sel:WORD_1/BYTE_3
414 // from: v_ashrrev_i32_e32 v1, 16/24, v0
415 // to SDWA src:v0 src_sel:WORD_1/BYTE_3 sext:1
417 // from: v_lshlrev_b32_e32 v1, 16/24, v0
418 // to SDWA dst:v1 dst_sel:WORD_1/BYTE_3 dst_unused:UNUSED_PAD
419 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
420 auto Imm = foldToImm(*Src0);
424 if (*Imm != 16 && *Imm != 24)
427 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
428 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
429 if (TRI->isPhysicalRegister(Src1->getReg()) ||
430 TRI->isPhysicalRegister(Dst->getReg()))
433 if (Opcode == AMDGPU::V_LSHLREV_B32_e32) {
434 auto SDWADst = make_unique<SDWADstOperand>(
435 Dst, Src1, *Imm == 16 ? WORD_1 : BYTE_3, UNUSED_PAD);
436 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWADst << '\n');
437 SDWAOperands[&MI] = std::move(SDWADst);
438 ++NumSDWAPatternsFound;
440 auto SDWASrc = make_unique<SDWASrcOperand>(
441 Src1, Dst, *Imm == 16 ? WORD_1 : BYTE_3, false, false,
442 Opcode == AMDGPU::V_LSHRREV_B32_e32 ? false : true);
443 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
444 SDWAOperands[&MI] = std::move(SDWASrc);
445 ++NumSDWAPatternsFound;
450 case AMDGPU::V_LSHRREV_B16_e32:
451 case AMDGPU::V_ASHRREV_I16_e32:
452 case AMDGPU::V_LSHLREV_B16_e32: {
453 // from: v_lshrrev_b16_e32 v1, 8, v0
454 // to SDWA src:v0 src_sel:BYTE_1
456 // from: v_ashrrev_i16_e32 v1, 8, v0
457 // to SDWA src:v0 src_sel:BYTE_1 sext:1
459 // from: v_lshlrev_b16_e32 v1, 8, v0
460 // to SDWA dst:v1 dst_sel:BYTE_1 dst_unused:UNUSED_PAD
461 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
462 auto Imm = foldToImm(*Src0);
463 if (!Imm || *Imm != 8)
466 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
467 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
469 if (TRI->isPhysicalRegister(Src1->getReg()) ||
470 TRI->isPhysicalRegister(Dst->getReg()))
473 if (Opcode == AMDGPU::V_LSHLREV_B16_e32) {
475 make_unique<SDWADstOperand>(Dst, Src1, BYTE_1, UNUSED_PAD);
476 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWADst << '\n');
477 SDWAOperands[&MI] = std::move(SDWADst);
478 ++NumSDWAPatternsFound;
480 auto SDWASrc = make_unique<SDWASrcOperand>(
481 Src1, Dst, BYTE_1, false, false,
482 Opcode == AMDGPU::V_LSHRREV_B16_e32 ? false : true);
483 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
484 SDWAOperands[&MI] = std::move(SDWASrc);
485 ++NumSDWAPatternsFound;
490 case AMDGPU::V_BFE_I32:
491 case AMDGPU::V_BFE_U32: {
493 // from: v_bfe_u32 v1, v0, 8, 8
494 // to SDWA src:v0 src_sel:BYTE_1
496 // offset | width | src_sel
497 // ------------------------
506 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
507 auto Offset = foldToImm(*Src1);
511 MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
512 auto Width = foldToImm(*Src2);
516 SdwaSel SrcSel = DWORD;
518 if (*Offset == 0 && *Width == 8)
520 else if (*Offset == 0 && *Width == 16)
522 else if (*Offset == 0 && *Width == 32)
524 else if (*Offset == 8 && *Width == 8)
526 else if (*Offset == 16 && *Width == 8)
528 else if (*Offset == 16 && *Width == 16)
530 else if (*Offset == 24 && *Width == 8)
535 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
536 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
538 if (TRI->isPhysicalRegister(Src0->getReg()) ||
539 TRI->isPhysicalRegister(Dst->getReg()))
542 auto SDWASrc = make_unique<SDWASrcOperand>(
543 Src0, Dst, SrcSel, false, false,
544 Opcode == AMDGPU::V_BFE_U32 ? false : true);
545 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
546 SDWAOperands[&MI] = std::move(SDWASrc);
547 ++NumSDWAPatternsFound;
550 case AMDGPU::V_AND_B32_e32: {
552 // from: v_and_b32_e32 v1, 0x0000ffff/0x000000ff, v0
553 // to SDWA src:v0 src_sel:WORD_0/BYTE_0
555 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
556 auto Imm = foldToImm(*Src0);
560 if (*Imm != 0x0000ffff && *Imm != 0x000000ff)
563 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
564 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
566 if (TRI->isPhysicalRegister(Src1->getReg()) ||
567 TRI->isPhysicalRegister(Dst->getReg()))
570 auto SDWASrc = make_unique<SDWASrcOperand>(
571 Src1, Dst, *Imm == 0x0000ffff ? WORD_0 : BYTE_0);
572 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
573 SDWAOperands[&MI] = std::move(SDWASrc);
574 ++NumSDWAPatternsFound;
582 bool SIPeepholeSDWA::isConvertibleToSDWA(const MachineInstr &MI) const {
583 // Check if this instruction can be converted to SDWA:
584 // 1. Does this opcode support SDWA
585 if (AMDGPU::getSDWAOp(MI.getOpcode()) == -1)
588 // 2. Are all operands - VGPRs
589 for (const MachineOperand &Operand : MI.explicit_operands()) {
590 if (!Operand.isReg() || !TRI->isVGPR(*MRI, Operand.getReg()))
597 bool SIPeepholeSDWA::convertToSDWA(MachineInstr &MI,
598 const SDWAOperandsVector &SDWAOperands) {
600 int SDWAOpcode = AMDGPU::getSDWAOp(MI.getOpcode());
601 assert(SDWAOpcode != -1);
603 const MCInstrDesc &SDWADesc = TII->get(SDWAOpcode);
605 // Create SDWA version of instruction MI and initialize its operands
606 MachineInstrBuilder SDWAInst =
607 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), SDWADesc);
609 // Copy dst, if it is present in original then should also be present in SDWA
610 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
612 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::vdst) != -1);
615 assert(TII->isVOPC(MI));
618 // Copy src0, initialize src0_modifiers. All sdwa instructions has src0 and
619 // src0_modifiers (except for v_nop_sdwa, but it can't get here)
620 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
623 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src0) != -1 &&
624 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src0_modifiers) != -1);
628 // Copy src1 if present, initialize src1_modifiers.
629 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
632 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src1) != -1 &&
633 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src1_modifiers) != -1);
637 assert(TII->isVOP1(MI));
640 if (SDWAOpcode == AMDGPU::V_MAC_F16_sdwa ||
641 SDWAOpcode == AMDGPU::V_MAC_F32_sdwa) {
642 // v_mac_f16/32 has additional src2 operand tied to vdst
643 MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
649 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::clamp) != -1);
652 // Initialize dst_sel and dst_unused if present
655 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::dst_sel) != -1 &&
656 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::dst_unused) != -1);
657 SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
658 SDWAInst.addImm(AMDGPU::SDWA::DstUnused::UNUSED_PAD);
661 // Initialize src0_sel
662 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src0_sel) != -1);
663 SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
666 // Initialize src1_sel if present
668 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src1_sel) != -1);
669 SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
672 // Apply all sdwa operand pattenrs
673 bool Converted = false;
674 for (auto &Operand : SDWAOperands) {
675 // There should be no intesection between SDWA operands and potential MIs
677 // v_and_b32 v0, 0xff, v1 -> src:v1 sel:BYTE_0
678 // v_and_b32 v2, 0xff, v0 -> src:v0 sel:BYTE_0
679 // v_add_u32 v3, v4, v2
681 // In that example it is possible that we would fold 2nd instruction into 3rd
682 // (v_add_u32_sdwa) and then try to fold 1st instruction into 2nd (that was
683 // already destroyed). So if SDWAOperand is also a potential MI then do not
685 if (PotentialMatches.count(Operand->getParentInst()) == 0)
686 Converted |= Operand->convertToSDWA(*SDWAInst, TII);
689 SDWAInst->eraseFromParent();
693 DEBUG(dbgs() << "Convert instruction:" << MI
694 << "Into:" << *SDWAInst << '\n');
695 ++NumSDWAInstructionsPeepholed;
697 MI.eraseFromParent();
701 bool SIPeepholeSDWA::runOnMachineFunction(MachineFunction &MF) {
702 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
705 !AMDGPU::isVI(ST)) { // TODO: Add support for SDWA on gfx9
709 MRI = &MF.getRegInfo();
710 TRI = ST.getRegisterInfo();
711 TII = ST.getInstrInfo();
713 // Find all SDWA operands in MF.
714 matchSDWAOperands(MF);
716 for (const auto &OperandPair : SDWAOperands) {
717 const auto &Operand = OperandPair.second;
718 MachineInstr *PotentialMI = Operand->potentialToConvert(TII);
719 if (PotentialMI && isConvertibleToSDWA(*PotentialMI)) {
720 PotentialMatches[PotentialMI].push_back(Operand.get());
724 for (auto &PotentialPair : PotentialMatches) {
725 MachineInstr &PotentialMI = *PotentialPair.first;
726 convertToSDWA(PotentialMI, PotentialPair.second);
729 PotentialMatches.clear();
730 SDWAOperands.clear();