Skip to content

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

[Cranelift] add commutative cases (follow-up of #11642) (#11663)

Bongjun Jang3de15c01380e
diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle
index 6f2d4d346a..3e6c443b03 100644
--- a/cranelift/codegen/src/opts/bitops.isle
+++ b/cranelift/codegen/src/opts/bitops.isle
@@ -203,7 +203,11 @@
(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))
+;; (x ^ ~y) & x --> x & y
(rule (simplify (band ty (bxor ty x (bnot ty y)) x)) (band ty x y))
+(rule (simplify (band ty (bxor ty (bnot ty y) x) x)) (band ty x y))
+(rule (simplify (band ty x (bxor ty x (bnot ty y)))) (band ty x y))
+(rule (simplify (band ty x (bxor ty (bnot ty y) x))) (band ty x y))
; (x & y) + (x ^ y) --> x | y
(rule (simplify (iadd ty (band ty x y) (bxor ty x y))) (bor 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
index f14a5cfc19..8b4e2978ef 100644
--- a/cranelift/filetests/filetests/egraph/fold-and-xor-not.clif
+++ b/cranelift/filetests/filetests/egraph/fold-and-xor-not.clif
@@ -2,7 +2,7 @@ test optimize precise-output
set opt_level=speed
target x86_64
-function %test1(i8, i8) -> i8 {
+function %fold_and_xor_not1(i8, i8) -> i8 {
block0(v0: i8, v1: i8):
v2 = bnot v1
v3 = bxor v0, v2
@@ -10,13 +10,13 @@ block0(v0: i8, v1: i8):
return v4
}
-; function %test1(i8, i8) -> i8 fast {
+; function %fold_and_xor_not1(i8, i8) -> i8 fast {
; block0(v0: i8, v1: i8):
; v5 = band v0, v1
; return v5
; }
-function %test2(i16, i16) -> i16 {
+function %fold_and_xor_not2(i16, i16) -> i16 {
block0(v0: i16, v1: i16):
v2 = bnot v1
v3 = bxor v0, v2
@@ -24,13 +24,13 @@ block0(v0: i16, v1: i16):
return v4
}
-; function %test2(i16, i16) -> i16 fast {
+; function %fold_and_xor_not2(i16, i16) -> i16 fast {
; block0(v0: i16, v1: i16):
; v5 = band v0, v1
; return v5
; }
-function %test3(i32, i32) -> i32 {
+function %fold_and_xor_not3(i32, i32) -> i32 {
block0(v0: i32, v1: i32):
v2 = bnot v1
v3 = bxor v0, v2
@@ -38,13 +38,13 @@ block0(v0: i32, v1: i32):
return v4
}
-; function %test3(i32, i32) -> i32 fast {
+; function %fold_and_xor_not3(i32, i32) -> i32 fast {
; block0(v0: i32, v1: i32):
; v5 = band v0, v1
; return v5
; }
-function %test4(i64, i64) -> i64 {
+function %fold_and_xor_not4_1(i64, i64) -> i64 {
block0(v0: i64, v1: i64):
v2 = bnot v1
v3 = bxor v0, v2
@@ -52,7 +52,50 @@ block0(v0: i64, v1: i64):
return v4
}
-; function %test4(i64, i64) -> i64 fast {
+; function %fold_and_xor_not4_1(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; v5 = band v0, v1
+; return v5
+; }
+
+function %fold_and_xor_not4_2(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bnot v1
+ v3 = bxor v2, v0
+ v4 = band v3, v0
+ return v4
+}
+
+; function %fold_and_xor_not4_2(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; v5 = band v0, v1
+; return v5
+; }
+
+
+function %fold_and_xor_not4_3(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bnot v1
+ v3 = bxor v0, v2
+ v4 = band v0, v3
+ return v4
+}
+
+; function %fold_and_xor_not4_3(i64, i64) -> i64 fast {
+; block0(v0: i64, v1: i64):
+; v5 = band v0, v1
+; return v5
+; }
+
+function %fold_and_xor_not4_4(i64, i64) -> i64 {
+block0(v0: i64, v1: i64):
+ v2 = bnot v1
+ v3 = bxor v2, v0
+ v4 = band v0, v3
+ return v4
+}
+
+; function %fold_and_xor_not4_4(i64, i64) -> i64 fast {
; block0(v0: i64, v1: i64):
; v5 = band v0, v1
; return v5