Skip to content

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

[Cranelift] (x & y) | ~(x ^ y) => ~(x ^ y) (#12686)

Hyunbin Kimf6c69b07756e
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index 8d0523f8ae..df947c9205 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -225,3 +225,13 @@
(rule (simplify
(bxor ty (band ty z x) (band ty z y)))
(band ty z (bxor ty x y)))
+
+; (x & y) | ~(x ^ y) => ~(x ^ y)
+(rule (simplify (bor ty (band ty x y) (bnot ty (bxor ty x y)))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (bnot ty (bxor ty x y)) (band ty x y))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (band ty x y) (bnot ty (bxor ty y x)))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (bnot ty (bxor ty y x)) (band ty x y))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (band ty y x) (bnot ty (bxor ty x y)))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (bnot ty (bxor ty x y)) (band ty y x))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (band ty y x) (bnot ty (bxor ty y x)))) (bnot ty (bxor ty x y)))
+(rule (simplify (bor ty (bnot ty (bxor ty y x)) (band ty y x))) (bnot ty (bxor ty x y)))
diff --git a/cranelift/filetests/filetests/egraph/fold-bitops.clif b/cranelift/filetests/filetests/egraph/fold-bitops.clif
index fe1fde54f5..3784c01f90 100644
--- a/cranelift/filetests/filetests/egraph/fold-bitops.clif
+++ b/cranelift/filetests/filetests/egraph/fold-bitops.clif
@@ -74,3 +74,19 @@ block0(v0: i32, v1: i32, v2: i32):
; return v7
; }
+;; (bor ty (band ty x y) (bnot ty (bxor ty x y))) -> (bnot ty (bxor ty x y))
+function %test_bor_band_bnot_bxor(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = band v0, v1
+ v3 = bxor v0, v1
+ v4 = bnot v3
+ v5 = bor v2, v4
+ return v5
+}
+
+; function %test_bor_band_bnot_bxor(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; v6 = bxor v1, v0
+; v7 = bnot v6
+; return v7
+; }
\ No newline at end of file
diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif
index 2b199e97f2..473bf81a6b 100644
--- a/cranelift/filetests/filetests/runtests/bitops.clif
+++ b/cranelift/filetests/filetests/runtests/bitops.clif
@@ -232,3 +232,16 @@ block0(v0: i32, v1: i32, v2: i32):
; run: %test_bxor_band_band(2, 1, 1) == 0
+function %test_bor_band_bnot_bxor(i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32):
+ v2 = band v0, v1
+ v3 = bxor v0, v1
+ v4 = bnot v3
+ v5 = bor v2, v4
+ return v5
+}
+
+; run: %test_bor_band_bnot_bxor(0, 0) == 0xffffffff
+; run: %test_bor_band_bnot_bxor(1, 1) == 0xffffffff
+; run: %test_bor_band_bnot_bxor(0, 1) == 0xfffffffe
+; run: %test_bor_band_bnot_bxor(2, 1) == 0xfffffffc
\ No newline at end of file