[WebAssembly] Optimize away mask of 63 for sra and srl( zext (and i32 63))) (#170128)

Follow up to #71844 after shl implementation
This commit is contained in:
Jasmine Tang 2025-12-02 18:23:17 +00:00 committed by GitHub
parent 23a22d0497
commit e0db7f347c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 0 deletions

View file

@ -109,6 +109,10 @@ def : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>;
def : Pat<(shl I64:$lhs, (zext (and I32:$rhs, 63))),
(SHL_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
def : Pat<(sra I64:$lhs, (zext (and I32:$rhs, 63))),
(SHR_S_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
def : Pat<(srl I64:$lhs, (zext (and I32:$rhs, 63))),
(SHR_U_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
defm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond),
(outs), (ins),

View file

@ -46,6 +46,21 @@ define i32 @sra_i32(i32 %v, i32 %x) {
ret i32 %a
}
define i64 @sra_i64_zext(i64 %v, i32 %x) {
; CHECK-LABEL: sra_i64_zext:
; CHECK: .functype sra_i64_zext (i64, i32) -> (i64)
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: local.get 0
; CHECK-NEXT: local.get 1
; CHECK-NEXT: i64.extend_i32_u
; CHECK-NEXT: i64.shr_s
; CHECK-NEXT: # fallthrough-return
%m = and i32 %x, 63
%z = zext i32 %m to i64
%a = ashr i64 %v, %z
ret i64 %a
}
define i32 @srl_i32(i32 %v, i32 %x) {
; CHECK-LABEL: srl_i32:
; CHECK: .functype srl_i32 (i32, i32) -> (i32)
@ -59,6 +74,21 @@ define i32 @srl_i32(i32 %v, i32 %x) {
ret i32 %a
}
define i64 @srl_i64_zext(i64 %v, i32 %x) {
; CHECK-LABEL: srl_i64_zext:
; CHECK: .functype srl_i64_zext (i64, i32) -> (i64)
; CHECK-NEXT: # %bb.0:
; CHECK-NEXT: local.get 0
; CHECK-NEXT: local.get 1
; CHECK-NEXT: i64.extend_i32_u
; CHECK-NEXT: i64.shr_u
; CHECK-NEXT: # fallthrough-return
%m = and i32 %x, 63
%z = zext i32 %m to i64
%a = lshr i64 %v, %z
ret i64 %a
}
define i64 @shl_i64(i64 %v, i64 %x) {
; CHECK-LABEL: shl_i64:
; CHECK: .functype shl_i64 (i64, i64) -> (i64)