diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle index 9a63591b06d7..9520a763c325 100644 --- a/cranelift/codegen/src/opts/bitops.isle +++ b/cranelift/codegen/src/opts/bitops.isle @@ -246,6 +246,16 @@ (rule (simplify (iadd ty (ineg ty y) (bor ty y x))) (band ty x (bnot ty y))) +; (x | y) ^ (x | ~y) --> ~x +(rule (simplify (bxor ty (bor ty x y) (bor ty x (bnot ty y)))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty x (bnot ty y)) (bor ty x y))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty x y) (bor ty (bnot ty y) x))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty (bnot ty y) x) (bor ty x y))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty y x) (bor ty x (bnot ty y)))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty x (bnot ty y)) (bor ty y x))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty y x) (bor ty (bnot ty y) x))) (bnot ty x)) +(rule (simplify (bxor ty (bor ty (bnot ty y) x) (bor ty y x))) (bnot ty x)) + ; x & (x | y) --> x (rule (simplify (band ty (bor ty x y) x)) x) (rule (simplify (band ty x (bor ty x y))) x) diff --git a/cranelift/filetests/filetests/egraph/fold-bitops.clif b/cranelift/filetests/filetests/egraph/fold-bitops.clif index a797110d4465..cdf1b9318e07 100644 --- a/cranelift/filetests/filetests/egraph/fold-bitops.clif +++ b/cranelift/filetests/filetests/egraph/fold-bitops.clif @@ -107,6 +107,21 @@ block0(v0: i32, v1: i32): ; return v7 ; } +;; (bxor ty (bor ty x y) (bor ty x (bnot ty y))) -> (bnot ty x) +function %test_bxor_bor_bor_bnot(i32, i32) -> i32 fast { +block0(v0: i32, v1: i32): + v2 = bor v0, v1 + v3 = bnot v1 + v4 = bor v0, v3 + v5 = bxor v2, v4 + return v5 +} + +; function %test_bxor_bor_bor_bnot(i32, i32) -> i32 fast { +; block0(v0: i32, v1: i32): +; v6 = bnot v0 +; return v6 + ;; (band ty (bor ty x y) x) -> x function %test_band_bor_absorb_x(i32, i32) -> i32 fast { block0(v0: i32, v1: i32): diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif index 22265cd9ef5a..1945bffb3bc0 100644 --- a/cranelift/filetests/filetests/runtests/bitops.clif +++ b/cranelift/filetests/filetests/runtests/bitops.clif @@ -259,6 +259,21 @@ block0(v0: i32, v1: i32): ; run: %test_iadd_bor_ineg(2, 1) == 2 ; run: %test_iadd_bor_ineg(0xffffffff, 0) == -1 +function %test_bxor_bor_bor_bnot(i32, i32) -> i32 fast { +block0(v0: i32, v1: i32): + v2 = bor v0, v1 + v3 = bnot v1 + v4 = bor v0, v3 + v5 = bxor v2, v4 + return v5 +} + +; run: %test_bxor_bor_bor_bnot(0, 0) == 0xffffffff +; run: %test_bxor_bor_bor_bnot(1, 0) == 0xfffffffe +; run: %test_bxor_bor_bor_bnot(1, 1) == 0xfffffffe +; run: %test_bxor_bor_bor_bnot(0x12345678, 0x9abcdef0) == 0xedcba987 +; run: %test_bxor_bor_bor_bnot(0xffffffff, 0) == 0 + function %test_band_bor_absorb_x(i32, i32) -> i32 fast { block0(v0: i32, v1: i32): v2 = bor v0, v1 @@ -270,4 +285,4 @@ block0(v0: i32, v1: i32): ; run: %test_band_bor_absorb_x(1, 0) == 1 ; run: %test_band_bor_absorb_x(1, 1) == 1 ; run: %test_band_bor_absorb_x(2, 1) == 2 -; run: %test_band_bor_absorb_x(0xf0f0f0f0, 0x0f0f0f0f) == 0xf0f0f0f0 \ No newline at end of file +; run: %test_band_bor_absorb_x(0xf0f0f0f0, 0x0f0f0f0f) == 0xf0f0f0f0