Skip to content

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

[Cranelift] `(z & x) ^ (z & y) => z & (x ^ y)` (#12000)

Bongjun Jang73fe75e7a8c5
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index 3e6c443b03..8c0666ff8e 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -229,3 +229,7 @@
(rule (simplify (bor ty (band ty x y) (bnot ty x))) (bor ty y (bnot ty x)))
(rule (simplify (bor ty (bnot ty x) (band ty x y))) (bor ty y (bnot ty x)))
+; (z & x) ^ (z & y) => z & (x ^ y)
+(rule (simplify
+ (bxor ty (band ty z x) (band ty z y)))
+ (band ty z (bxor ty x y)))
diff --git a/cranelift/filetests/filetests/egraph/fold-bitops.clif b/cranelift/filetests/filetests/egraph/fold-bitops.clif
index 3026bc98f0..fe1fde54f5 100644
--- a/cranelift/filetests/filetests/egraph/fold-bitops.clif
+++ b/cranelift/filetests/filetests/egraph/fold-bitops.clif
@@ -58,3 +58,19 @@ block0(v0: i32, v1: i32):
; return v2
; }
+;; (bxor ty (band ty z x) (band ty z y)) -> (band ty z (bxor ty x y))
+function %test_bxor_band_band(i32, i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32, v2: i32):
+ v3 = band v0, v1
+ v4 = band v0, v2
+ v5 = bxor v3, v4
+ return v5
+}
+
+; function %test_bxor_band_band(i32, i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32, v2: i32):
+; v6 = bxor v1, v2
+; v7 = band v0, v6
+; return v7
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif
index 9fd88d233f..2b199e97f2 100644
--- a/cranelift/filetests/filetests/runtests/bitops.clif
+++ b/cranelift/filetests/filetests/runtests/bitops.clif
@@ -222,3 +222,13 @@ block0(v0: i32, v1: i32):
; run: %fold_or_and_not(0, 0) == 0xffffffff
+function %test_bxor_band_band(i32, i32, i32) -> i32 fast {
+block0(v0: i32, v1: i32, v2: i32):
+ v3 = band v0, v1
+ v4 = band v0, v2
+ v5 = bxor v3, v4
+ return v5
+}
+
+; run: %test_bxor_band_band(2, 1, 1) == 0
+