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;
58 SmallVector<MachineInstr *, 8> ConvertedInstructions;
60 Optional<int64_t> foldToImm(const MachineOperand &Op) const;
65 SIPeepholeSDWA() : MachineFunctionPass(ID) {
66 initializeSIPeepholeSDWAPass(*PassRegistry::getPassRegistry());
69 bool runOnMachineFunction(MachineFunction &MF) override;
70 void matchSDWAOperands(MachineFunction &MF);
71 bool isConvertibleToSDWA(const MachineInstr &MI) const;
72 bool convertToSDWA(MachineInstr &MI, const SDWAOperandsVector &SDWAOperands);
73 void legalizeScalarOperands(MachineInstr &MI) const;
75 StringRef getPassName() const override { return "SI Peephole SDWA"; }
77 void getAnalysisUsage(AnalysisUsage &AU) const override {
79 MachineFunctionPass::getAnalysisUsage(AU);
85 MachineOperand *Target; // Operand that would be used in converted instruction
86 MachineOperand *Replaced; // Operand that would be replace by Target
89 SDWAOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp)
90 : Target(TargetOp), Replaced(ReplacedOp) {
91 assert(Target->isReg());
92 assert(Replaced->isReg());
95 virtual ~SDWAOperand() {}
97 virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) = 0;
98 virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) = 0;
100 MachineOperand *getTargetOperand() const { return Target; }
101 MachineOperand *getReplacedOperand() const { return Replaced; }
102 MachineInstr *getParentInst() const { return Target->getParent(); }
103 MachineRegisterInfo *getMRI() const {
104 return &getParentInst()->getParent()->getParent()->getRegInfo();
108 using namespace AMDGPU::SDWA;
110 class SDWASrcOperand : public SDWAOperand {
118 SDWASrcOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
119 SdwaSel SrcSel_ = DWORD, bool Abs_ = false, bool Neg_ = false,
121 : SDWAOperand(TargetOp, ReplacedOp), SrcSel(SrcSel_), Abs(Abs_),
122 Neg(Neg_), Sext(Sext_) {}
124 virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) override;
125 virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
127 SdwaSel getSrcSel() const { return SrcSel; }
128 bool getAbs() const { return Abs; }
129 bool getNeg() const { return Neg; }
130 bool getSext() const { return Sext; }
132 uint64_t getSrcMods() const;
135 class SDWADstOperand : public SDWAOperand {
141 SDWADstOperand(MachineOperand *TargetOp, MachineOperand *ReplacedOp,
142 SdwaSel DstSel_ = DWORD, DstUnused DstUn_ = UNUSED_PAD)
143 : SDWAOperand(TargetOp, ReplacedOp), DstSel(DstSel_), DstUn(DstUn_) {}
145 virtual MachineInstr *potentialToConvert(const SIInstrInfo *TII) override;
146 virtual bool convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) override;
148 SdwaSel getDstSel() const { return DstSel; }
149 DstUnused getDstUnused() const { return DstUn; }
152 } // End anonymous namespace.
154 INITIALIZE_PASS(SIPeepholeSDWA, DEBUG_TYPE, "SI Peephole SDWA", false, false)
156 char SIPeepholeSDWA::ID = 0;
158 char &llvm::SIPeepholeSDWAID = SIPeepholeSDWA::ID;
160 FunctionPass *llvm::createSIPeepholeSDWAPass() {
161 return new SIPeepholeSDWA();
166 static raw_ostream& operator<<(raw_ostream &OS, const SdwaSel &Sel) {
168 case BYTE_0: OS << "BYTE_0"; break;
169 case BYTE_1: OS << "BYTE_1"; break;
170 case BYTE_2: OS << "BYTE_2"; break;
171 case BYTE_3: OS << "BYTE_3"; break;
172 case WORD_0: OS << "WORD_0"; break;
173 case WORD_1: OS << "WORD_1"; break;
174 case DWORD: OS << "DWORD"; break;
179 static raw_ostream& operator<<(raw_ostream &OS, const DstUnused &Un) {
181 case UNUSED_PAD: OS << "UNUSED_PAD"; break;
182 case UNUSED_SEXT: OS << "UNUSED_SEXT"; break;
183 case UNUSED_PRESERVE: OS << "UNUSED_PRESERVE"; break;
188 static raw_ostream& operator<<(raw_ostream &OS, const SDWASrcOperand &Src) {
189 OS << "SDWA src: " << *Src.getTargetOperand()
190 << " src_sel:" << Src.getSrcSel()
191 << " abs:" << Src.getAbs() << " neg:" << Src.getNeg()
192 << " sext:" << Src.getSext() << '\n';
196 static raw_ostream& operator<<(raw_ostream &OS, const SDWADstOperand &Dst) {
197 OS << "SDWA dst: " << *Dst.getTargetOperand()
198 << " dst_sel:" << Dst.getDstSel()
199 << " dst_unused:" << Dst.getDstUnused() << '\n';
205 static void copyRegOperand(MachineOperand &To, const MachineOperand &From) {
206 assert(To.isReg() && From.isReg());
207 To.setReg(From.getReg());
208 To.setSubReg(From.getSubReg());
209 To.setIsUndef(From.isUndef());
211 To.setIsKill(From.isKill());
213 To.setIsDead(From.isDead());
217 static bool isSameReg(const MachineOperand &LHS, const MachineOperand &RHS) {
218 return LHS.isReg() &&
220 LHS.getReg() == RHS.getReg() &&
221 LHS.getSubReg() == RHS.getSubReg();
224 static bool isSubregOf(const MachineOperand &SubReg,
225 const MachineOperand &SuperReg,
226 const TargetRegisterInfo *TRI) {
228 if (!SuperReg.isReg() || !SubReg.isReg())
231 if (isSameReg(SuperReg, SubReg))
234 if (SuperReg.getReg() != SubReg.getReg())
237 LaneBitmask SuperMask = TRI->getSubRegIndexLaneMask(SuperReg.getSubReg());
238 LaneBitmask SubMask = TRI->getSubRegIndexLaneMask(SubReg.getSubReg());
239 SuperMask |= ~SubMask;
240 return SuperMask.all();
243 uint64_t SDWASrcOperand::getSrcMods() const {
247 "Float and integer src modifiers can't be set simulteniously");
248 Mods |= Abs ? SISrcMods::ABS : 0;
249 Mods |= Neg ? SISrcMods::NEG : 0;
251 Mods |= SISrcMods::SEXT;
257 MachineInstr *SDWASrcOperand::potentialToConvert(const SIInstrInfo *TII) {
258 // For SDWA src operand potential instruction is one that use register
259 // defined by parent instruction
260 MachineRegisterInfo *MRI = getMRI();
261 MachineOperand *Replaced = getReplacedOperand();
262 assert(Replaced->isReg());
264 MachineInstr *PotentialMI = nullptr;
265 for (MachineOperand &PotentialMO : MRI->use_operands(Replaced->getReg())) {
266 // If this is use of another subreg of dst reg then do nothing
267 if (!isSubregOf(*Replaced, PotentialMO, MRI->getTargetRegisterInfo()))
270 // If there exist use of superreg of dst then we should not combine this
272 if (!isSameReg(PotentialMO, *Replaced))
275 // Check that PotentialMI is only instruction that uses dst reg
276 if (PotentialMI == nullptr) {
277 PotentialMI = PotentialMO.getParent();
278 } else if (PotentialMI != PotentialMO.getParent()) {
286 bool SDWASrcOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
287 // Find operand in instruction that matches source operand and replace it with
288 // target operand. Set corresponding src_sel
290 MachineOperand *Src = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
291 MachineOperand *SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src0_sel);
292 MachineOperand *SrcMods =
293 TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers);
294 assert(Src && (Src->isReg() || Src->isImm()));
295 if (!isSameReg(*Src, *getReplacedOperand())) {
296 // If this is not src0 then it should be src1
297 Src = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
298 SrcSel = TII->getNamedOperand(MI, AMDGPU::OpName::src1_sel);
299 SrcMods = TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers);
301 assert(Src && Src->isReg());
303 if ((MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
304 MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
305 !isSameReg(*Src, *getReplacedOperand())) {
306 // In case of v_mac_f16/32_sdwa this pass can try to apply src operand to
307 // src2. This is not allowed.
311 assert(isSameReg(*Src, *getReplacedOperand()) && SrcSel && SrcMods);
313 copyRegOperand(*Src, *getTargetOperand());
314 SrcSel->setImm(getSrcSel());
315 SrcMods->setImm(getSrcMods());
316 getTargetOperand()->setIsKill(false);
320 MachineInstr *SDWADstOperand::potentialToConvert(const SIInstrInfo *TII) {
321 // For SDWA dst operand potential instruction is one that defines register
322 // that this operand uses
323 MachineRegisterInfo *MRI = getMRI();
324 MachineInstr *ParentMI = getParentInst();
325 MachineOperand *Replaced = getReplacedOperand();
326 assert(Replaced->isReg());
328 for (MachineOperand &PotentialMO : MRI->def_operands(Replaced->getReg())) {
329 if (!isSubregOf(*Replaced, PotentialMO, MRI->getTargetRegisterInfo()))
332 if (!isSameReg(*Replaced, PotentialMO))
335 // Check that ParentMI is the only instruction that uses replaced register
336 for (MachineOperand &UseMO : MRI->use_operands(PotentialMO.getReg())) {
337 if (isSubregOf(UseMO, PotentialMO, MRI->getTargetRegisterInfo()) &&
338 UseMO.getParent() != ParentMI) {
343 // Due to SSA this should be onle def of replaced register, so return it
344 return PotentialMO.getParent();
350 bool SDWADstOperand::convertToSDWA(MachineInstr &MI, const SIInstrInfo *TII) {
351 // Replace vdst operand in MI with target operand. Set dst_sel and dst_unused
353 if ((MI.getOpcode() == AMDGPU::V_MAC_F16_sdwa ||
354 MI.getOpcode() == AMDGPU::V_MAC_F32_sdwa) &&
355 getDstSel() != AMDGPU::SDWA::DWORD) {
356 // v_mac_f16/32_sdwa allow dst_sel to be equal only to DWORD
360 MachineOperand *Operand = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
363 isSameReg(*Operand, *getReplacedOperand()));
364 copyRegOperand(*Operand, *getTargetOperand());
365 MachineOperand *DstSel= TII->getNamedOperand(MI, AMDGPU::OpName::dst_sel);
367 DstSel->setImm(getDstSel());
368 MachineOperand *DstUnused= TII->getNamedOperand(MI, AMDGPU::OpName::dst_unused);
370 DstUnused->setImm(getDstUnused());
372 // Remove original instruction because it would conflict with our new
373 // instruction by register definition
374 getParentInst()->eraseFromParent();
378 Optional<int64_t> SIPeepholeSDWA::foldToImm(const MachineOperand &Op) const {
383 // If this is not immediate then it can be copy of immediate value, e.g.:
384 // %vreg1<def> = S_MOV_B32 255;
386 for (const MachineOperand &Def : MRI->def_operands(Op.getReg())) {
387 if (!isSameReg(Op, Def))
390 const MachineInstr *DefInst = Def.getParent();
391 if (!TII->isFoldableCopy(*DefInst))
394 const MachineOperand &Copied = DefInst->getOperand(1);
398 return Copied.getImm();
405 void SIPeepholeSDWA::matchSDWAOperands(MachineFunction &MF) {
406 for (MachineBasicBlock &MBB : MF) {
407 for (MachineInstr &MI : MBB) {
408 unsigned Opcode = MI.getOpcode();
410 case AMDGPU::V_LSHRREV_B32_e32:
411 case AMDGPU::V_ASHRREV_I32_e32:
412 case AMDGPU::V_LSHLREV_B32_e32: {
413 // from: v_lshrrev_b32_e32 v1, 16/24, v0
414 // to SDWA src:v0 src_sel:WORD_1/BYTE_3
416 // from: v_ashrrev_i32_e32 v1, 16/24, v0
417 // to SDWA src:v0 src_sel:WORD_1/BYTE_3 sext:1
419 // from: v_lshlrev_b32_e32 v1, 16/24, v0
420 // to SDWA dst:v1 dst_sel:WORD_1/BYTE_3 dst_unused:UNUSED_PAD
421 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
422 auto Imm = foldToImm(*Src0);
426 if (*Imm != 16 && *Imm != 24)
429 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
430 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
431 if (TRI->isPhysicalRegister(Src1->getReg()) ||
432 TRI->isPhysicalRegister(Dst->getReg()))
435 if (Opcode == AMDGPU::V_LSHLREV_B32_e32) {
436 auto SDWADst = make_unique<SDWADstOperand>(
437 Dst, Src1, *Imm == 16 ? WORD_1 : BYTE_3, UNUSED_PAD);
438 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWADst << '\n');
439 SDWAOperands[&MI] = std::move(SDWADst);
440 ++NumSDWAPatternsFound;
442 auto SDWASrc = make_unique<SDWASrcOperand>(
443 Src1, Dst, *Imm == 16 ? WORD_1 : BYTE_3, false, false,
444 Opcode == AMDGPU::V_LSHRREV_B32_e32 ? false : true);
445 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
446 SDWAOperands[&MI] = std::move(SDWASrc);
447 ++NumSDWAPatternsFound;
452 case AMDGPU::V_LSHRREV_B16_e32:
453 case AMDGPU::V_ASHRREV_I16_e32:
454 case AMDGPU::V_LSHLREV_B16_e32: {
455 // from: v_lshrrev_b16_e32 v1, 8, v0
456 // to SDWA src:v0 src_sel:BYTE_1
458 // from: v_ashrrev_i16_e32 v1, 8, v0
459 // to SDWA src:v0 src_sel:BYTE_1 sext:1
461 // from: v_lshlrev_b16_e32 v1, 8, v0
462 // to SDWA dst:v1 dst_sel:BYTE_1 dst_unused:UNUSED_PAD
463 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
464 auto Imm = foldToImm(*Src0);
465 if (!Imm || *Imm != 8)
468 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
469 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
471 if (TRI->isPhysicalRegister(Src1->getReg()) ||
472 TRI->isPhysicalRegister(Dst->getReg()))
475 if (Opcode == AMDGPU::V_LSHLREV_B16_e32) {
477 make_unique<SDWADstOperand>(Dst, Src1, BYTE_1, UNUSED_PAD);
478 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWADst << '\n');
479 SDWAOperands[&MI] = std::move(SDWADst);
480 ++NumSDWAPatternsFound;
482 auto SDWASrc = make_unique<SDWASrcOperand>(
483 Src1, Dst, BYTE_1, false, false,
484 Opcode == AMDGPU::V_LSHRREV_B16_e32 ? false : true);
485 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
486 SDWAOperands[&MI] = std::move(SDWASrc);
487 ++NumSDWAPatternsFound;
492 case AMDGPU::V_BFE_I32:
493 case AMDGPU::V_BFE_U32: {
495 // from: v_bfe_u32 v1, v0, 8, 8
496 // to SDWA src:v0 src_sel:BYTE_1
498 // offset | width | src_sel
499 // ------------------------
508 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
509 auto Offset = foldToImm(*Src1);
513 MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
514 auto Width = foldToImm(*Src2);
518 SdwaSel SrcSel = DWORD;
520 if (*Offset == 0 && *Width == 8)
522 else if (*Offset == 0 && *Width == 16)
524 else if (*Offset == 0 && *Width == 32)
526 else if (*Offset == 8 && *Width == 8)
528 else if (*Offset == 16 && *Width == 8)
530 else if (*Offset == 16 && *Width == 16)
532 else if (*Offset == 24 && *Width == 8)
537 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
538 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
540 if (TRI->isPhysicalRegister(Src0->getReg()) ||
541 TRI->isPhysicalRegister(Dst->getReg()))
544 auto SDWASrc = make_unique<SDWASrcOperand>(
545 Src0, Dst, SrcSel, false, false,
546 Opcode == AMDGPU::V_BFE_U32 ? false : true);
547 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
548 SDWAOperands[&MI] = std::move(SDWASrc);
549 ++NumSDWAPatternsFound;
552 case AMDGPU::V_AND_B32_e32: {
554 // from: v_and_b32_e32 v1, 0x0000ffff/0x000000ff, v0
555 // to SDWA src:v0 src_sel:WORD_0/BYTE_0
557 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
558 auto Imm = foldToImm(*Src0);
562 if (*Imm != 0x0000ffff && *Imm != 0x000000ff)
565 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
566 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
568 if (TRI->isPhysicalRegister(Src1->getReg()) ||
569 TRI->isPhysicalRegister(Dst->getReg()))
572 auto SDWASrc = make_unique<SDWASrcOperand>(
573 Src1, Dst, *Imm == 0x0000ffff ? WORD_0 : BYTE_0);
574 DEBUG(dbgs() << "Match: " << MI << "To: " << *SDWASrc << '\n');
575 SDWAOperands[&MI] = std::move(SDWASrc);
576 ++NumSDWAPatternsFound;
584 bool SIPeepholeSDWA::isConvertibleToSDWA(const MachineInstr &MI) const {
585 // Check if this instruction has opcode that supports SDWA
586 return AMDGPU::getSDWAOp(MI.getOpcode()) != -1;
589 bool SIPeepholeSDWA::convertToSDWA(MachineInstr &MI,
590 const SDWAOperandsVector &SDWAOperands) {
592 int SDWAOpcode = AMDGPU::getSDWAOp(MI.getOpcode());
593 assert(SDWAOpcode != -1);
595 const MCInstrDesc &SDWADesc = TII->get(SDWAOpcode);
597 // Create SDWA version of instruction MI and initialize its operands
598 MachineInstrBuilder SDWAInst =
599 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), SDWADesc);
601 // Copy dst, if it is present in original then should also be present in SDWA
602 MachineOperand *Dst = TII->getNamedOperand(MI, AMDGPU::OpName::vdst);
604 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::vdst) != -1);
607 assert(TII->isVOPC(MI));
610 // Copy src0, initialize src0_modifiers. All sdwa instructions has src0 and
611 // src0_modifiers (except for v_nop_sdwa, but it can't get here)
612 MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
615 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src0) != -1 &&
616 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src0_modifiers) != -1);
620 // Copy src1 if present, initialize src1_modifiers.
621 MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
624 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src1) != -1 &&
625 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src1_modifiers) != -1);
629 assert(TII->isVOP1(MI));
632 if (SDWAOpcode == AMDGPU::V_MAC_F16_sdwa ||
633 SDWAOpcode == AMDGPU::V_MAC_F32_sdwa) {
634 // v_mac_f16/32 has additional src2 operand tied to vdst
635 MachineOperand *Src2 = TII->getNamedOperand(MI, AMDGPU::OpName::src2);
641 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::clamp) != -1);
644 // Initialize dst_sel and dst_unused if present
647 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::dst_sel) != -1 &&
648 AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::dst_unused) != -1);
649 SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
650 SDWAInst.addImm(AMDGPU::SDWA::DstUnused::UNUSED_PAD);
653 // Initialize src0_sel
654 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src0_sel) != -1);
655 SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
658 // Initialize src1_sel if present
660 assert(AMDGPU::getNamedOperandIdx(SDWAOpcode, AMDGPU::OpName::src1_sel) != -1);
661 SDWAInst.addImm(AMDGPU::SDWA::SdwaSel::DWORD);
664 // Apply all sdwa operand pattenrs
665 bool Converted = false;
666 for (auto &Operand : SDWAOperands) {
667 // There should be no intesection between SDWA operands and potential MIs
669 // v_and_b32 v0, 0xff, v1 -> src:v1 sel:BYTE_0
670 // v_and_b32 v2, 0xff, v0 -> src:v0 sel:BYTE_0
671 // v_add_u32 v3, v4, v2
673 // In that example it is possible that we would fold 2nd instruction into 3rd
674 // (v_add_u32_sdwa) and then try to fold 1st instruction into 2nd (that was
675 // already destroyed). So if SDWAOperand is also a potential MI then do not
677 if (PotentialMatches.count(Operand->getParentInst()) == 0)
678 Converted |= Operand->convertToSDWA(*SDWAInst, TII);
681 ConvertedInstructions.push_back(SDWAInst);
683 SDWAInst->eraseFromParent();
687 DEBUG(dbgs() << "Convert instruction:" << MI
688 << "Into:" << *SDWAInst << '\n');
689 ++NumSDWAInstructionsPeepholed;
691 MI.eraseFromParent();
695 // If an instruction was converted to SDWA it should not have immediates or SGPR
696 // operands. Copy its scalar operands into VGPRs.
697 void SIPeepholeSDWA::legalizeScalarOperands(MachineInstr &MI) const {
698 const MCInstrDesc &Desc = TII->get(MI.getOpcode());
699 for (unsigned I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) {
700 MachineOperand &Op = MI.getOperand(I);
701 if (!Op.isImm() && !(Op.isReg() && !TRI->isVGPR(*MRI, Op.getReg())))
703 if (Desc.OpInfo[I].RegClass == -1 ||
704 !TRI->hasVGPRs(TRI->getRegClass(Desc.OpInfo[I].RegClass)))
706 unsigned VGPR = MRI->createVirtualRegister(&AMDGPU::VGPR_32RegClass);
707 auto Copy = BuildMI(*MI.getParent(), MI.getIterator(), MI.getDebugLoc(),
708 TII->get(AMDGPU::V_MOV_B32_e32), VGPR);
710 Copy.addImm(Op.getImm());
712 Copy.addReg(Op.getReg(), Op.isKill() ? RegState::Kill : 0,
714 Op.ChangeToRegister(VGPR, false);
718 bool SIPeepholeSDWA::runOnMachineFunction(MachineFunction &MF) {
719 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
722 !AMDGPU::isVI(ST)) { // TODO: Add support for SDWA on gfx9
726 MRI = &MF.getRegInfo();
727 TRI = ST.getRegisterInfo();
728 TII = ST.getInstrInfo();
730 // Find all SDWA operands in MF.
731 matchSDWAOperands(MF);
733 for (const auto &OperandPair : SDWAOperands) {
734 const auto &Operand = OperandPair.second;
735 MachineInstr *PotentialMI = Operand->potentialToConvert(TII);
736 if (PotentialMI && isConvertibleToSDWA(*PotentialMI)) {
737 PotentialMatches[PotentialMI].push_back(Operand.get());
741 for (auto &PotentialPair : PotentialMatches) {
742 MachineInstr &PotentialMI = *PotentialPair.first;
743 convertToSDWA(PotentialMI, PotentialPair.second);
746 PotentialMatches.clear();
747 SDWAOperands.clear();
749 while (!ConvertedInstructions.empty())
750 legalizeScalarOperands(*ConvertedInstructions.pop_back_val());