Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions llvm/lib/Target/RISCV/RISCVVectorPeephole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,11 +651,23 @@ bool RISCVVectorPeephole::foldVMV_V_V(MachineInstr &MI) {
if (!hasSameEEW(MI, *Src))
return false;

std::optional<std::pair<unsigned, unsigned>> NeedsCommute;

// Src needs to have the same passthru as VMV_V_V
MachineOperand &SrcPassthru = Src->getOperand(Src->getNumExplicitDefs());
if (SrcPassthru.getReg().isValid() &&
SrcPassthru.getReg() != Passthru.getReg())
return false;
SrcPassthru.getReg() != Passthru.getReg()) {
// If Src's passthru != Passthru, check if it uses Passthru in another
// operand and try to commute it.
int OtherIdx = Src->findRegisterUseOperandIdx(Passthru.getReg(), TRI);
if (OtherIdx == -1)
return false;
unsigned OpIdx1 = OtherIdx;
unsigned OpIdx2 = Src->getNumExplicitDefs();
if (!TII->findCommutedOpIndices(*Src, OpIdx1, OpIdx2))
return false;
NeedsCommute = {OpIdx1, OpIdx2};
}

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

if (NeedsCommute) {
auto [OpIdx1, OpIdx2] = *NeedsCommute;
[[maybe_unused]] bool Commuted =
TII->commuteInstruction(*Src, /*NewMI=*/false, OpIdx1, OpIdx2);
assert(Commuted && "Failed to commute Src?");
}

if (SrcPassthru.getReg() != Passthru.getReg()) {
SrcPassthru.setReg(Passthru.getReg());
// If Src is masked then its passthru needs to be in VRNoV0.
Expand Down
11 changes: 11 additions & 0 deletions llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.ll
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,14 @@ define <vscale x 1 x i64> @vmerge(<vscale x 1 x i64> %passthru, <vscale x 1 x i6
%b = call <vscale x 1 x i64> @llvm.riscv.vmv.v.v.nxv1i64(<vscale x 1 x i64> %passthru, <vscale x 1 x i64> %a, iXLen %avl)
ret <vscale x 1 x i64> %b
}

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) {
; CHECK-LABEL: commute_vfmadd:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli zero, a0, e32, m2, tu, ma
; CHECK-NEXT: vfmacc.vv v8, v12, v10
; CHECK-NEXT: ret
%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)
%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)
ret <vscale x 4 x float> %w
}
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/RISCV/rvv/vmv.v.v-peephole.mir
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,23 @@ body: |
%x:vrnov0 = PseudoVMERGE_VVM_M1 $noreg, %passthru, $noreg, %mask, 4, 5 /* e32 */
%z:vr = PseudoVMV_V_V_M1 %passthru, %x, 4, 5 /* e32 */, 0 /* tu, mu */
...
---
name: commute_vfmadd
body: |
bb.0:
liveins: $x8, $v0, $v8, $v9, $v10
; CHECK-LABEL: name: commute_vfmadd
; CHECK: liveins: $x8, $v0, $v8, $v9, $v10
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: %avl:gprnox0 = COPY $x8
; CHECK-NEXT: %passthru:vrnov0 = COPY $v8
; CHECK-NEXT: %x:vr = COPY $v9
; CHECK-NEXT: %y:vr = COPY $v10
; CHECK-NEXT: %vfmadd:vrnov0 = nofpexcept PseudoVFMACC_VV_M1_E32 %passthru, %y, %x, 7, %avl, 5 /* e32 */, 0 /* tu, mu */, implicit $frm
%avl:gprnox0 = COPY $x8
%passthru:vrnov0 = COPY $v8
%x:vr = COPY $v9
%y:vr = COPY $v10
%vfmadd:vrnov0 = nofpexcept PseudoVFMADD_VV_M1_E32 %x, %y, %passthru, 7, -1, 5 /* e32 */, 3 /* ta, ma */, implicit $frm
%vmerge:vrnov0 = PseudoVMV_V_V_M1 %passthru, %vfmadd, %avl, 5 /* e32 */, 0 /* tu, mu */
...