Skip to content

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

[Cranelift] fold `(and (xor (not ..) ..) ..)` (#11642)

Bongjun Janga740e343d18b
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index a29b2ac337..9af24741c4 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -202,3 +202,5 @@
(rule (simplify (band ty (band ty x y) y)) (band ty x y))
(rule (simplify (band ty x (band ty x y))) (band ty x y))
(rule (simplify (band ty y (band ty x y))) (band ty x y))
+
+(rule (simplify (band ty (bxor ty x (bnot ty y)) x)) (band ty x y))
diff --git a/cranelift/filetests/filetests/egraph/fold-and-xor-not.clif b/cranelift/filetests/filetests/egraph/fold-and-xor-not.clif
new file mode 100644
index 0000000000..f14a5cfc19
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/fold-and-xor-not.clif
@@ -0,0 +1,60 @@
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+function %test1(i8, i8) -> i8 {
+block0(v0: i8, v1: i8):
+ v2 = bnot v1
+ v3 = bxor v0, v2
+ v4 = band v3, v0
+ return v4
+}
+
+; function %test1(i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8):
+; v5 = band v0, v1
+; return v5
+; }
+
+function %test2(i16, i16) -> i16 {
+block0(v0: i16, v1: i16):
+ v2 = bnot v1
+ v3 = bxor v0, v2
+ v4 = band v3, v0
+ return v4
+}
+
+; function %test2(i16, i16) -> i16 fast {
+; block0(v0: i16, v1: i16):
+; v5 = band v0, v1
+; return v5
+; }
+
+function %test3(i32, i32) -> i32 {
+block0(v0: i32, v1: i32):
+ v2 = bnot v1
+ v3 = bxor v0, v2
+ v4 = band v3, v0
+ return v4
+}
+
+; function %test3(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; v5 = band v0, v1
+; return v5
+; }
+
+function %test4(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bnot v1
+ v3 = bxor v0, v2
+ v4 = band v3, v0
+ return v4
+}
+
+; function %test4(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; v5 = band v0, v1
+; return v5
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif
index e8c3b3a4b3..31d30b028c 100644
--- a/cranelift/filetests/filetests/runtests/bitops.clif
+++ b/cranelift/filetests/filetests/runtests/bitops.clif
@@ -164,3 +164,13 @@ block0(v0: i32, v1: i32):
; run: %test_and_and4(4, 3) == 0
+function %fold_and_xor_not(i32, i32) -> i32 {
+block0(v0: i32, v1: i32):
+ v2 = bnot v1
+ v3 = bxor v0, v2
+ v4 = band v3, v0
+ return v4
+}
+
+; run: %fold_and_xor_not(4, 5) == 4
+