Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

[Cranelift] dual add (#12731)

Hyunbin Kim557ef73037d4
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index ee8af0f4ac..9a63591b06 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -246,4 +246,8 @@
(rule (simplify (iadd ty (ineg ty y) (bor ty y x)))
(band ty x (bnot ty y)))
-
+; x & (x | y) --> x
+(rule (simplify (band ty (bor ty x y) x)) x)
+(rule (simplify (band ty x (bor ty x y))) x)
+(rule (simplify (band ty (bor ty y x) x)) x)
+(rule (simplify (band ty x (bor ty y x))) x)
diff --git a/cranelift/filetests/filetests/egraph/fold-bitops.clif b/cranelift/filetests/filetests/egraph/fold-bitops.clif
index 4c4a9e14ab..a797110d44 100644
--- a/cranelift/filetests/filetests/egraph/fold-bitops.clif
+++ b/cranelift/filetests/filetests/egraph/fold-bitops.clif
@@ -107,3 +107,15 @@ block0(v0: i32, v1: i32):
; return v7
; }
+;; (band ty (bor ty x y) x) -> x
+function %test_band_bor_absorb_x(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = bor v0, v1
+ v3 = band v2, v0
+ return v3
+}
+
+; function %test_band_bor_absorb_x(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; return v0
+; }
\ No newline at end of file
diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif
index b79dc40ada..22265cd9ef 100644
--- a/cranelift/filetests/filetests/runtests/bitops.clif
+++ b/cranelift/filetests/filetests/runtests/bitops.clif
@@ -257,4 +257,17 @@ block0(v0: i32, v1: i32):
; run: %test_iadd_bor_ineg(0, 0) == 0
; run: %test_iadd_bor_ineg(1, 1) == 0
; run: %test_iadd_bor_ineg(2, 1) == 2
-; run: %test_iadd_bor_ineg(0xffffffff, 0) == -1
\ No newline at end of file
+; run: %test_iadd_bor_ineg(0xffffffff, 0) == -1
+
+function %test_band_bor_absorb_x(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = bor v0, v1
+ v3 = band v2, v0
+ return v3
+}
+
+; run: %test_band_bor_absorb_x(0, 0) == 0
+; run: %test_band_bor_absorb_x(1, 0) == 1
+; run: %test_band_bor_absorb_x(1, 1) == 1
+; run: %test_band_bor_absorb_x(2, 1) == 2
+; run: %test_band_bor_absorb_x(0xf0f0f0f0, 0x0f0f0f0f) == 0xf0f0f0f0
\ No newline at end of file