forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RISCV][ISel] Remove redundant vmerge for the vwadd. (llvm#78403)
This patch is aiming at resolving the below missed-optimization case. ### Code ``` define <8 x i64> @vwadd_mask_v8i32(<8 x i32> %x, <8 x i64> %y) { %mask = icmp slt <8 x i32> %x, <i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42> %a = select <8 x i1> %mask, <8 x i32> %x, <8 x i32> zeroinitializer %sa = sext <8 x i32> %a to <8 x i64> %ret = add <8 x i64> %sa, %y ret <8 x i64> %ret } ``` ### Before this patch [Compiler Explorer](https://godbolt.org/z/cd1bKTrx6) ``` vwadd_mask_v8i32: li a0, 42 vsetivli zero, 8, e32, m2, ta, ma vmslt.vx v0, v8, a0 vmv.v.i v10, 0 vmerge.vvm v16, v10, v8, v0 vwadd.wv v8, v12, v16 ret ``` ### After this patch ``` vwadd_mask_v8i32: li a0, 42 vsetivli zero, 8, e32, m2, ta, ma vmslt.vx v0, v8, a0 vsetvli zero, zero, e32, m2, tu, mu vwadd.wv v12, v12, v8, v0.t vmv4r.v v8, v12 ret ``` This pattern could be found in a reduction with a widening destination Specifically, we first do a fold like `(vwadd.wv y, (vmerge cond, x, 0)) -> (vwadd.wv y, x, y, cond)`, then do pattern matching on it.
- Loading branch information
1 parent
608d602
commit 3855757
Showing
3 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4 | ||
; RUN: llc -mtriple=riscv32 -mattr=+v -verify-machineinstrs < %s | FileCheck %s --check-prefixes=CHECK | ||
; RUN: llc -mtriple=riscv64 -mattr=+v -verify-machineinstrs < %s | FileCheck %s --check-prefixes=CHECK | ||
|
||
define <8 x i64> @vwadd_wv_mask_v8i32(<8 x i32> %x, <8 x i64> %y) { | ||
; CHECK-LABEL: vwadd_wv_mask_v8i32: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: li a0, 42 | ||
; CHECK-NEXT: vsetivli zero, 8, e32, m2, ta, ma | ||
; CHECK-NEXT: vmslt.vx v0, v8, a0 | ||
; CHECK-NEXT: vsetvli zero, zero, e32, m2, tu, mu | ||
; CHECK-NEXT: vwadd.wv v12, v12, v8, v0.t | ||
; CHECK-NEXT: vmv4r.v v8, v12 | ||
; CHECK-NEXT: ret | ||
%mask = icmp slt <8 x i32> %x, <i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42> | ||
%a = select <8 x i1> %mask, <8 x i32> %x, <8 x i32> zeroinitializer | ||
%sa = sext <8 x i32> %a to <8 x i64> | ||
%ret = add <8 x i64> %sa, %y | ||
ret <8 x i64> %ret | ||
} | ||
|
||
define <8 x i64> @vwaddu_wv_mask_v8i32(<8 x i32> %x, <8 x i64> %y) { | ||
; CHECK-LABEL: vwaddu_wv_mask_v8i32: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: li a0, 42 | ||
; CHECK-NEXT: vsetivli zero, 8, e32, m2, ta, ma | ||
; CHECK-NEXT: vmslt.vx v0, v8, a0 | ||
; CHECK-NEXT: vsetvli zero, zero, e32, m2, tu, mu | ||
; CHECK-NEXT: vwaddu.wv v12, v12, v8, v0.t | ||
; CHECK-NEXT: vmv4r.v v8, v12 | ||
; CHECK-NEXT: ret | ||
%mask = icmp slt <8 x i32> %x, <i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42> | ||
%a = select <8 x i1> %mask, <8 x i32> %x, <8 x i32> zeroinitializer | ||
%sa = zext <8 x i32> %a to <8 x i64> | ||
%ret = add <8 x i64> %sa, %y | ||
ret <8 x i64> %ret | ||
} | ||
|
||
define <8 x i64> @vwaddu_vv_mask_v8i32(<8 x i32> %x, <8 x i32> %y) { | ||
; CHECK-LABEL: vwaddu_vv_mask_v8i32: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: li a0, 42 | ||
; CHECK-NEXT: vsetivli zero, 8, e32, m2, ta, ma | ||
; CHECK-NEXT: vmslt.vx v0, v8, a0 | ||
; CHECK-NEXT: vmv.v.i v12, 0 | ||
; CHECK-NEXT: vmerge.vvm v8, v12, v8, v0 | ||
; CHECK-NEXT: vwaddu.vv v12, v8, v10 | ||
; CHECK-NEXT: vmv4r.v v8, v12 | ||
; CHECK-NEXT: ret | ||
%mask = icmp slt <8 x i32> %x, <i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42> | ||
%a = select <8 x i1> %mask, <8 x i32> %x, <8 x i32> zeroinitializer | ||
%sa = zext <8 x i32> %a to <8 x i64> | ||
%sy = zext <8 x i32> %y to <8 x i64> | ||
%ret = add <8 x i64> %sa, %sy | ||
ret <8 x i64> %ret | ||
} | ||
|
||
define <8 x i64> @vwadd_wv_mask_v8i32_commutative(<8 x i32> %x, <8 x i64> %y) { | ||
; CHECK-LABEL: vwadd_wv_mask_v8i32_commutative: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: li a0, 42 | ||
; CHECK-NEXT: vsetivli zero, 8, e32, m2, ta, ma | ||
; CHECK-NEXT: vmslt.vx v0, v8, a0 | ||
; CHECK-NEXT: vsetvli zero, zero, e32, m2, tu, mu | ||
; CHECK-NEXT: vwadd.wv v12, v12, v8, v0.t | ||
; CHECK-NEXT: vmv4r.v v8, v12 | ||
; CHECK-NEXT: ret | ||
%mask = icmp slt <8 x i32> %x, <i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42> | ||
%a = select <8 x i1> %mask, <8 x i32> %x, <8 x i32> zeroinitializer | ||
%sa = sext <8 x i32> %a to <8 x i64> | ||
%ret = add <8 x i64> %y, %sa | ||
ret <8 x i64> %ret | ||
} | ||
|
||
define <8 x i64> @vwadd_wv_mask_v8i32_nonzero(<8 x i32> %x, <8 x i64> %y) { | ||
; CHECK-LABEL: vwadd_wv_mask_v8i32_nonzero: | ||
; CHECK: # %bb.0: | ||
; CHECK-NEXT: li a0, 42 | ||
; CHECK-NEXT: vsetivli zero, 8, e32, m2, ta, ma | ||
; CHECK-NEXT: vmslt.vx v0, v8, a0 | ||
; CHECK-NEXT: vmv.v.i v10, 1 | ||
; CHECK-NEXT: vmerge.vvm v16, v10, v8, v0 | ||
; CHECK-NEXT: vwadd.wv v8, v12, v16 | ||
; CHECK-NEXT: ret | ||
%mask = icmp slt <8 x i32> %x, <i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42, i32 42> | ||
%a = select <8 x i1> %mask, <8 x i32> %x, <8 x i32> <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1> | ||
%sa = sext <8 x i32> %a to <8 x i64> | ||
%ret = add <8 x i64> %y, %sa | ||
ret <8 x i64> %ret | ||
} |