Skip to content

Commit f24810a

Browse files
committed
[RISCV] Commute Src in foldVMV_V_V
In llvm#156499 we taught the vmerge peephole to commute operands so that the passthru operands lined up. We can do the same for the vmv.v.v peephole, which allows us fold more vmv.v.vs away. This is needed to prevent a regression in an upcoming patch that adds a combine for vmerge.vvm to vmv.v.v.
1 parent 9bdcb87 commit f24810a

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,23 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
651651
if (!hasSameEEW(MI, *Src))
652652
return false;
653653

654+
std::optional<std::pair<unsigned, unsigned>> NeedsCommute;
655+
654656
// Src needs to have the same passthru as VMV_V_V
655657
MachineOperand &SrcPassthru = Src->getOperand(Src->getNumExplicitDefs());
656658
if (SrcPassthru.getReg().isValid() &&
657-
SrcPassthru.getReg() != Passthru.getReg())
658-
return false;
659+
SrcPassthru.getReg() != Passthru.getReg()) {
660+
// If Src's passthru != Passthru, check if it uses Passthru in another
661+
// operand and try to commute it.
662+
int OtherIdx = Src->findRegisterUseOperandIdx(Passthru.getReg(), TRI);
663+
if (OtherIdx == -1)
664+
return false;
665+
unsigned OpIdx1 = OtherIdx;
666+
unsigned OpIdx2 = Src->getNumExplicitDefs();
667+
if (!TII->findCommutedOpIndices(*Src, OpIdx1, OpIdx2))
668+
return false;
669+
NeedsCommute = {OpIdx1, OpIdx2};
670+
}
659671

660672
// Src VL will have already been reduced if legal (see tryToReduceVL),
661673
// so we don't need to handle a smaller source VL here. However, the
@@ -668,6 +680,13 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
668680
if (!ensureDominates(Passthru, *Src))
669681
return false;
670682

683+
if (NeedsCommute) {
684+
auto [OpIdx1, OpIdx2] = *NeedsCommute;
685+
[[maybe_unused]] bool Commuted =
686+
TII->commuteInstruction(*Src, /*NewMI=*/false, OpIdx1, OpIdx2);
687+
assert(Commuted && "Failed to commute Src?");
688+
}
689+
671690
if (SrcPassthru.getReg() != Passthru.getReg()) {
672691
SrcPassthru.setReg(Passthru.getReg());
673692
// If Src is masked then its passthru needs to be in VRNoV0.

llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,8 @@ define <vscale x 1 x i64> @vmerge(<vscale x 1 x i64> %passthru, <vscale x 1 x i6
249249
define <vscale x 4 x float> @commute_vfmadd(<vscale x 4 x float> %passthru, <vscale x 4 x float> %a, <vscale x 4 x float> %b, iXLen %vl) {
250250
; CHECK-LABEL: commute_vfmadd:
251251
; CHECK: # %bb.0:
252-
; CHECK-NEXT: vsetvli zero, a0, e32, m2, ta, ma
253-
; CHECK-NEXT: vfmadd.vv v10, v12, v8
254-
; CHECK-NEXT: vsetvli zero, zero, e32, m2, tu, ma
255-
; CHECK-NEXT: vmv.v.v v8, v10
252+
; CHECK-NEXT: vsetvli zero, a0, e32, m2, tu, ma
253+
; CHECK-NEXT: vfmacc.vv v8, v12, v10
256254
; CHECK-NEXT: ret
257255
%v = call <vscale x 4 x float> @llvm.riscv.vfmadd(<vscale x 4 x float> %a, <vscale x 4 x float> %b, <vscale x 4 x float> %passthru, iXLen 7, iXLen %vl, iXLen 3)
258256
%w = call <vscale x 4 x float> @llvm.riscv.vmv.v.v(<vscale x 4 x float> %passthru, <vscale x 4 x float> %v, iXLen %vl)

llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ body: |
180180
; CHECK-NEXT: %passthru:vrnov0 = COPY $v8
181181
; CHECK-NEXT: %x:vr = COPY $v9
182182
; CHECK-NEXT: %y:vr = COPY $v10
183-
; CHECK-NEXT: %vfmadd:vrnov0 = nofpexcept PseudoVFMADD_VV_M1_E32 %x, %y, %passthru, 7, %avl, 5 /* e32 */, 3 /* ta, ma */, implicit $frm
184-
; CHECK-NEXT: %vmerge:vrnov0 = PseudoVMV_V_V_M1 %passthru, %vfmadd, %avl, 5 /* e32 */, 0 /* tu, mu */
183+
; CHECK-NEXT: %vfmadd:vrnov0 = nofpexcept PseudoVFMACC_VV_M1_E32 %passthru, %y, %x, 7, %avl, 5 /* e32 */, 0 /* tu, mu */, implicit $frm
185184
%avl:gprnox0 = COPY $x8
186185
%passthru:vrnov0 = COPY $v8
187186
%x:vr = COPY $v9

0 commit comments

Comments
 (0)