Skip to content

Commit edd1856

Browse files
authored
[WebAssembly] Optimize away mask of 63 for shl ( zext (and i32 63))) (#152397)
Fixes #71844
1 parent 0e721b7 commit edd1856

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ def : Pat<(i64 (WebAssemblyWrapperREL texternalsym:$addr)),
460460
include "WebAssemblyInstrMemory.td"
461461
include "WebAssemblyInstrCall.td"
462462
include "WebAssemblyInstrControl.td"
463-
include "WebAssemblyInstrInteger.td"
464463
include "WebAssemblyInstrConv.td"
464+
include "WebAssemblyInstrInteger.td"
465465
include "WebAssemblyInstrFloat.td"
466466
include "WebAssemblyInstrAtomics.td"
467467
include "WebAssemblyInstrSIMD.td"

llvm/lib/Target/WebAssembly/WebAssemblyInstrInteger.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def : Pat<(rotr I32:$lhs, (and I32:$rhs, 31)), (ROTR_I32 I32:$lhs, I32:$rhs)>;
107107
def : Pat<(rotl I64:$lhs, (and I64:$rhs, 63)), (ROTL_I64 I64:$lhs, I64:$rhs)>;
108108
def : Pat<(rotr I64:$lhs, (and I64:$rhs, 63)), (ROTR_I64 I64:$lhs, I64:$rhs)>;
109109

110+
def : Pat<(shl I64:$lhs, (zext (and I32:$rhs, 63))),
111+
(SHL_I64 I64:$lhs, (I64_EXTEND_U_I32 I32:$rhs))>;
112+
110113
defm SELECT_I32 : I<(outs I32:$dst), (ins I32:$lhs, I32:$rhs, I32:$cond),
111114
(outs), (ins),
112115
[(set I32:$dst, (select I32:$cond, I32:$lhs, I32:$rhs))],

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ define i32 @shl_i32(i32 %v, i32 %x) {
1818
ret i32 %a
1919
}
2020

21+
define i64 @shl_i64_zext(i64 %v, i32 %x) {
22+
; CHECK-LABEL: shl_i64_zext:
23+
; CHECK: .functype shl_i64_zext (i64, i32) -> (i64)
24+
; CHECK-NEXT: # %bb.0:
25+
; CHECK-NEXT: local.get 0
26+
; CHECK-NEXT: local.get 1
27+
; CHECK-NEXT: i64.extend_i32_u
28+
; CHECK-NEXT: i64.shl
29+
; CHECK-NEXT: # fallthrough-return
30+
%m = and i32 %x, 63
31+
%z = zext i32 %m to i64
32+
%a = shl i64 %v, %z
33+
ret i64 %a
34+
}
35+
2136
define i32 @sra_i32(i32 %v, i32 %x) {
2237
; CHECK-LABEL: sra_i32:
2338
; CHECK: .functype sra_i32 (i32, i32) -> (i32)

0 commit comments

Comments
 (0)