Skip to content

Commit 63c90eb

Browse files
badumbatishkcloudy0717
authored andcommitted
[WebAssembly] Optimize away mask of 63 for sra and srl( zext (and i32 63))) (llvm#170128)
Follow up to llvm#71844 after shl implementation
1 parent 61a5d4d commit 63c90eb

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>;
109109

110110
def : Pat<(shl I64:$lhs, (zext (and I32:$rhs, 63))),
111111
(SHL_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
112+
def : Pat<(sra I64:$lhs, (zext (and I32:$rhs, 63))),
113+
(SHR_S_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
114+
def : Pat<(srl I64:$lhs, (zext (and I32:$rhs, 63))),
115+
(SHR_U_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
112116

113117
defm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond),
114118
(outs), (ins),

llvm/test/CodeGen/WebAssembly/masked-shifts.ll

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ define i32 @sra_i32(i32 %v, i32 %x) {
4646
ret i32 %a
4747
}
4848

49+
define i64 @sra_i64_zext(i64 %v, i32 %x) {
50+
; CHECK-LABEL: sra_i64_zext:
51+
; CHECK: .functype sra_i64_zext (i64, i32) -> (i64)
52+
; CHECK-NEXT: # %bb.0:
53+
; CHECK-NEXT: local.get 0
54+
; CHECK-NEXT: local.get 1
55+
; CHECK-NEXT: i64.extend_i32_u
56+
; CHECK-NEXT: i64.shr_s
57+
; CHECK-NEXT: # fallthrough-return
58+
%m = and i32 %x, 63
59+
%z = zext i32 %m to i64
60+
%a = ashr i64 %v, %z
61+
ret i64 %a
62+
}
63+
4964
define i32 @srl_i32(i32 %v, i32 %x) {
5065
; CHECK-LABEL: srl_i32:
5166
; CHECK: .functype srl_i32 (i32, i32) -> (i32)
@@ -59,6 +74,21 @@ define i32 @srl_i32(i32 %v, i32 %x) {
5974
ret i32 %a
6075
}
6176

77+
define i64 @srl_i64_zext(i64 %v, i32 %x) {
78+
; CHECK-LABEL: srl_i64_zext:
79+
; CHECK: .functype srl_i64_zext (i64, i32) -> (i64)
80+
; CHECK-NEXT: # %bb.0:
81+
; CHECK-NEXT: local.get 0
82+
; CHECK-NEXT: local.get 1
83+
; CHECK-NEXT: i64.extend_i32_u
84+
; CHECK-NEXT: i64.shr_u
85+
; CHECK-NEXT: # fallthrough-return
86+
%m = and i32 %x, 63
87+
%z = zext i32 %m to i64
88+
%a = lshr i64 %v, %z
89+
ret i64 %a
90+
}
91+
6292
define i64 @shl_i64(i64 %v, i64 %x) {
6393
; CHECK-LABEL: shl_i64:
6494
; CHECK: .functype shl_i64 (i64, i64) -> (i64)

0 commit comments

Comments
 (0)