Skip to content

Commit

Permalink
[WebAssembly] Add more combine pattern for vector shift
Browse files Browse the repository at this point in the history
After change with D144169, the codegen generates redundant instructions
like and and wrap. This fixes it.

Differential Revision: https://reviews.llvm.org/D144360
  • Loading branch information
junparser committed Feb 22, 2023
1 parent c339f9e commit e9d7f96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions llvm/lib/Target/WebAssembly/WebAssemblyInstrSIMD.td
Expand Up @@ -971,6 +971,12 @@ def : Pat<(wasm_shr_s (v4i32 V128:$lhs), (and I32:$rhs, 31)),
def : Pat<(wasm_shr_u (v4i32 V128:$lhs), (and I32:$rhs, 31)),
(SHR_U_I32x4 V128:$lhs, I32:$rhs)>;

def : Pat<(wasm_shl (v2i64 V128:$lhs), (and I32:$rhs, 63)),
(SHL_I64x2 V128:$lhs, I32:$rhs)>;
def : Pat<(wasm_shr_s (v2i64 V128:$lhs), (and I32:$rhs, 63)),
(SHR_S_I64x2 V128:$lhs, I32:$rhs)>;
def : Pat<(wasm_shr_u (v2i64 V128:$lhs), (and I32:$rhs, 63)),
(SHR_U_I64x2 V128:$lhs, I32:$rhs)>;
def : Pat<(wasm_shl (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
(SHL_I64x2 V128:$lhs, (I32_WRAP_I64 I64:$rhs))>;
def : Pat<(wasm_shr_s (v2i64 V128:$lhs), (trunc (and I64:$rhs, 63))),
Expand Down
49 changes: 49 additions & 0 deletions llvm/test/CodeGen/WebAssembly/masked-shifts.ll
Expand Up @@ -502,3 +502,52 @@ define <2 x i64> @lshr_v2i64_late(<2 x i64> %v, i64 %x) {
%a = lshr <2 x i64> %v, %m
ret <2 x i64> %a
}

define <2 x i64> @shl_v2i64_i32(<2 x i64> %v, i32 %x) {
; CHECK-LABEL: shl_v2i64_i32:
; CHECK: .functype shl_v2i64_i32 (v128, i32) -> (v128)
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: local.get 0
; CHECK-NEXT: local.get 1
; CHECK-NEXT: i64x2.shl
; CHECK-NEXT: # fallthrough-return
%z = and i32 %x, 63
%m = zext i32 %z to i64
%t = insertelement <2 x i64> undef, i64 %m, i32 0
%s = shufflevector <2 x i64> %t, <2 x i64> undef, <2 x i32> <i32 0, i32 0>
%a = shl <2 x i64> %v, %s
ret <2 x i64> %a
}

define <2 x i64> @ashr_v2i64_i32(<2 x i64> %v, i32 %x) {
; CHECK-LABEL: ashr_v2i64_i32:
; CHECK: .functype ashr_v2i64_i32 (v128, i32) -> (v128)
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: local.get 0
; CHECK-NEXT: local.get 1
; CHECK-NEXT: i64x2.shr_s
; CHECK-NEXT: # fallthrough-return
%z = and i32 %x, 63
%m = zext i32 %z to i64
%t = insertelement <2 x i64> undef, i64 %m, i32 0
%s = shufflevector <2 x i64> %t, <2 x i64> undef, <2 x i32> <i32 0, i32 0>
%a = ashr <2 x i64> %v, %s
ret <2 x i64> %a
}

define <2 x i64> @lshr_v2i64_i32(<2 x i64> %v, i32 %x) {
; CHECK-LABEL: lshr_v2i64_i32:
; CHECK: .functype lshr_v2i64_i32 (v128, i32) -> (v128)
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: local.get 0
; CHECK-NEXT: local.get 1
; CHECK-NEXT: i64x2.shr_u
; CHECK-NEXT: # fallthrough-return
%z = and i32 %x, 63
%m = zext i32 %z to i64
%t = insertelement <2 x i64> undef, i64 %m, i32 0
%s = shufflevector <2 x i64> %t, <2 x i64> undef, <2 x i32> <i32 0, i32 0>
%a = lshr <2 x i64> %v, %s
ret <2 x i64> %a
}

0 comments on commit e9d7f96

Please sign in to comment.