Skip to content

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

[Cranelift] `(X ^ Y) ^ Y --> X`, `(X & Y) | X --> X` (#11680)

Bongjun Jang76cd1acacd90
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index cf51c4da8b..82b84547f3 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -213,3 +213,11 @@
(rule (simplify (iadd ty (bor ty x y) (band ty x y))) (iadd ty x y))
(rule (simplify (iadd ty (band ty x y) (bor ty x y))) (iadd ty x y))
+; (x & y) | x --> x
+(rule (simplify (bor ty (band ty x y) x)) x)
+(rule (simplify (bor ty x (band ty x y))) x)
+
+; (x ^ y) ^ y --> x
+(rule (simplify (bxor ty (bxor ty x y) y)) x)
+(rule (simplify (bxor ty y (bxor ty x y))) x)
+
diff --git a/cranelift/filetests/filetests/egraph/fold-or-and.clif b/cranelift/filetests/filetests/egraph/fold-or-and.clif
new file mode 100644
index 0000000000..d6acdefd40
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/fold-or-and.clif
@@ -0,0 +1,71 @@
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+function %fold_or_and11(i8, i8) -> i8 {
+block0(v0: i8, v1: i8):
+ v2 = band v0, v1
+ v3 = bor v2, v0
+ return v3
+}
+
+; function %fold_or_and11(i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8):
+; return v0
+; }
+
+
+
+function %fold_or_and12(i16, i16) -> i16 {
+block0(v0: i16, v1: i16):
+ v2 = band v0, v1
+ v3 = bor v2, v0
+ return v3
+}
+
+; function %fold_or_and12(i16, i16) -> i16 fast {
+; block0(v0: i16, v1: i16):
+; return v0
+; }
+
+
+
+function %fold_or_and13(i32, i32) -> i32 {
+block0(v0: i32, v1: i32):
+ v2 = band v0, v1
+ v3 = bor v2, v0
+ return v3
+}
+
+; function %fold_or_and13(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; return v0
+; }
+
+
+
+function %fold_or_and14(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = band v0, v1
+ v3 = bor v2, v0
+ return v3
+}
+
+; function %fold_or_and14(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; return v0
+; }
+
+
+function %fold_or_and14(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = band v0, v1
+ v3 = bor v0, v2
+ return v3
+}
+
+; function %fold_or_and14(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; return v0
+; }
+
diff --git a/cranelift/filetests/filetests/egraph/fold-xor-xor.clif b/cranelift/filetests/filetests/egraph/fold-xor-xor.clif
new file mode 100644
index 0000000000..1eb82d4c5d
--- /dev/null
+++ b/cranelift/filetests/filetests/egraph/fold-xor-xor.clif
@@ -0,0 +1,71 @@
+test optimize precise-output
+set opt_level=speed
+target x86_64
+
+function %fold_xor_xor11(i8, i8) -> i8 {
+block0(v0: i8, v1: i8):
+ v2 = bxor v0, v1
+ v3 = bxor v2, v1
+ return v3
+}
+
+; function %fold_xor_xor11(i8, i8) -> i8 fast {
+; block0(v0: i8, v1: i8):
+; return v0
+; }
+
+
+
+function %fold_xor_xor12(i16, i16) -> i16 {
+block0(v0: i16, v1: i16):
+ v2 = bxor v0, v1
+ v3 = bxor v2, v1
+ return v3
+}
+
+; function %fold_xor_xor12(i16, i16) -> i16 fast {
+; block0(v0: i16, v1: i16):
+; return v0
+; }
+
+
+
+function %fold_xor_xor13(i32, i32) -> i32 {
+block0(v0: i32, v1: i32):
+ v2 = bxor v0, v1
+ v3 = bxor v2, v1
+ return v3
+}
+
+; function %fold_xor_xor13(i32, i32) -> i32 fast {
+; block0(v0: i32, v1: i32):
+; return v0
+; }
+
+
+
+function %fold_xor_xor14(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bxor v0, v1
+ v3 = bxor v2, v1
+ return v3
+}
+
+; function %fold_xor_xor14(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; return v0
+; }
+
+
+function %fold_xor_xor15(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bxor v0, v1
+ v3 = bxor v1, v2
+ return v3
+}
+
+; function %fold_xor_xor15(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; return v0
+; }
+
diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif
index fd89e9efc1..e15a268d0c 100644
--- a/cranelift/filetests/filetests/runtests/bitops.clif
+++ b/cranelift/filetests/filetests/runtests/bitops.clif
@@ -194,3 +194,21 @@ block0(v0: i64, v1: i64):
; run: %fold_add_and_xor4(1, 2) == 3
+function %fold_or_and14(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = band v0, v1
+ v3 = bor v2, v0
+ return v3
+}
+
+; run: %fold_or_and14(0xffff0000, 0x0000ffff) == 0xffff0000
+
+function %fold_xor_xor15(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bxor v0, v1
+ v3 = bxor v1, v2
+ return v3
+}
+
+; run: %fold_xor_xor15(0xdead0000, 0x0000beef) == 0xdead0000
+