Skip to content

Commit

Permalink
[DAGCombiner][RISCV] Enable reassociation for VP_FMA in visitFADDForF…
Browse files Browse the repository at this point in the history
…MACombine.

Reviewed By: fakepaper56

Differential Revision: https://reviews.llvm.org/D149911
  • Loading branch information
topperc committed May 5, 2023
1 parent 3b9ed6e commit fe9f557
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15233,10 +15233,10 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) {
SDValue TmpFMA = FMA;
while (E && isFusedOp(TmpFMA) && TmpFMA.hasOneUse()) {
SDValue FMul = TmpFMA->getOperand(2);
if (FMul.getOpcode() == ISD::FMUL && FMul.hasOneUse()) {
if (matcher.match(FMul, ISD::FMUL) && FMul.hasOneUse()) {
SDValue C = FMul.getOperand(0);
SDValue D = FMul.getOperand(1);
SDValue CDE = DAG.getNode(PreferredFusedOpcode, SL, VT, C, D, E);
SDValue CDE = matcher.getNode(PreferredFusedOpcode, SL, VT, C, D, E);
DAG.ReplaceAllUsesOfValueWith(FMul, CDE);
// Replacing the inner FMul could cause the outer FMA to be simplified
// away.
Expand Down
15 changes: 15 additions & 0 deletions llvm/test/CodeGen/RISCV/rvv/fold-vp-fadd-and-vp-fmul.ll
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,18 @@ define <vscale x 1 x double> @fma_nonvp(<vscale x 1 x double> %x, <vscale x 1 x
%2 = call fast <vscale x 1 x double> @llvm.vp.fadd.nxv1f64(<vscale x 1 x double> %1, <vscale x 1 x double> %z, <vscale x 1 x i1> %m, i32 %vl)
ret <vscale x 1 x double> %2
}

define <vscale x 1 x double> @fma_reassociate(<vscale x 1 x double> %a, <vscale x 1 x double> %b, <vscale x 1 x double> %c, <vscale x 1 x double> %d, <vscale x 1 x double> %e, <vscale x 1 x i1> %m, i32 zeroext %vl) {
; CHECK-LABEL: fma_reassociate:
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli zero, a0, e64, m1, ta, mu
; CHECK-NEXT: vfmadd.vv v11, v10, v12, v0.t
; CHECK-NEXT: vfmadd.vv v9, v8, v11, v0.t
; CHECK-NEXT: vmv.v.v v8, v9
; CHECK-NEXT: ret
%1 = call fast <vscale x 1 x double> @llvm.vp.fmul.nxv1f64(<vscale x 1 x double> %a, <vscale x 1 x double> %b, <vscale x 1 x i1> %m, i32 %vl)
%2 = call fast <vscale x 1 x double> @llvm.vp.fmul.nxv1f64(<vscale x 1 x double> %c, <vscale x 1 x double> %d, <vscale x 1 x i1> %m, i32 %vl)
%3 = call fast <vscale x 1 x double> @llvm.vp.fadd.nxv1f64(<vscale x 1 x double> %1, <vscale x 1 x double> %2, <vscale x 1 x i1> %m, i32 %vl)
%4 = call fast <vscale x 1 x double> @llvm.vp.fadd.nxv1f64(<vscale x 1 x double> %3, <vscale x 1 x double> %e, <vscale x 1 x i1> %m, i32 %vl)
ret <vscale x 1 x double> %4
}

0 comments on commit fe9f557

Please sign in to comment.