From 7f686e0ea7a161f258d22ec93bfdc5b2fa7110ac Mon Sep 17 00:00:00 2001 From: Pengcheng Wang Date: Sun, 27 Apr 2025 16:25:23 +0800 Subject: [PATCH] [RISCV] Match (ext (op a, b)) to (wop a, b) This suboptimal case was found when trying to optimize ABD/ABDS operation. Adding ISel patterns is the simplest way to optimize. We can add DAGCombine cases for `ISD::SIGN_EXTEND/ISD::ZERO_EXTEND` instead but that may need a lot of manual handlings. --- .../Target/RISCV/RISCVInstrInfoVSDPatterns.td | 11 +++++ llvm/test/CodeGen/RISCV/rvv/abd.ll | 44 +++++++------------ 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td b/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td index aea125c5348dd..55a5109b7ecfa 100644 --- a/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoVSDPatterns.td @@ -467,6 +467,17 @@ multiclass VPatWidenBinarySDNode_VV_VX(instruction_name#"_VX_"#vti.LMul.MX) (wti.Vector (IMPLICIT_DEF)), vti.RegClass:$rs2, GPR:$rs1, vti.AVL, vti.Log2SEW, TA_MA)>; + if !eq(extop1, extop2) then + def : Pat<(wti.Vector (extop1 (op (vti.Vector vti.RegClass:$rs2), + (vti.Vector vti.RegClass:$rs1)))), + (!cast(instruction_name#"_VV_"#vti.LMul.MX) + (wti.Vector (IMPLICIT_DEF)), vti.RegClass:$rs2, + vti.RegClass:$rs1, vti.AVL, vti.Log2SEW, TA_MA)>; + def : Pat<(wti.Vector (extop1 (op (vti.Vector vti.RegClass:$rs2), + (vti.Vector (SplatPat (XLenVT GPR:$rs1)))))), + (!cast(instruction_name#"_VX_"#vti.LMul.MX) + (wti.Vector (IMPLICIT_DEF)), vti.RegClass:$rs2, + GPR:$rs1, vti.AVL, vti.Log2SEW, TA_MA)>; } } } diff --git a/llvm/test/CodeGen/RISCV/rvv/abd.ll b/llvm/test/CodeGen/RISCV/rvv/abd.ll index 583d872238df7..b8d95bd95df8a 100644 --- a/llvm/test/CodeGen/RISCV/rvv/abd.ll +++ b/llvm/test/CodeGen/RISCV/rvv/abd.ll @@ -58,10 +58,8 @@ define @sabd_h_promoted_ops( %a, %a to %b.sext = sext %b to @@ -91,10 +89,8 @@ define @sabd_s_promoted_ops( %a, %a to %b.sext = sext %b to @@ -124,10 +120,8 @@ define @sabd_d_promoted_ops( %a, %a to %b.sext = sext %b to @@ -192,10 +186,8 @@ define @uabd_h_promoted_ops( %a, %a to %b.zext = zext %b to @@ -225,10 +217,8 @@ define @uabd_s_promoted_ops( %a, %a to %b.zext = zext %b to @@ -258,10 +248,8 @@ define @uabd_d_promoted_ops( %a, %a to %b.zext = zext %b to @@ -296,11 +284,9 @@ define @uabd_non_matching_promoted_ops( %a, ; CHECK: # %bb.0: ; CHECK-NEXT: vsetvli a0, zero, e16, m1, ta, ma ; CHECK-NEXT: vzext.vf2 v10, v8 -; CHECK-NEXT: vminu.vv v8, v10, v9 -; CHECK-NEXT: vmaxu.vv v9, v10, v9 -; CHECK-NEXT: vsub.vv v10, v9, v8 -; CHECK-NEXT: vsetvli zero, zero, e32, m2, ta, ma -; CHECK-NEXT: vzext.vf2 v8, v10 +; CHECK-NEXT: vminu.vv v11, v10, v9 +; CHECK-NEXT: vmaxu.vv v10, v10, v9 +; CHECK-NEXT: vwsubu.vv v8, v10, v11 ; CHECK-NEXT: ret %a.zext = zext %a to %b.zext = zext %b to