diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle index 82b84547f3f3..6f2d4d346a6c 100644 --- a/cranelift/codegen/src/opts/bitops.isle +++ b/cranelift/codegen/src/opts/bitops.isle @@ -221,3 +221,7 @@ (rule (simplify (bxor ty (bxor ty x y) y)) x) (rule (simplify (bxor ty y (bxor ty x y))) x) +; (x & y) | ~x -> y | ~x +(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))) + diff --git a/cranelift/filetests/filetests/egraph/fold-or-and-not.clif b/cranelift/filetests/filetests/egraph/fold-or-and-not.clif new file mode 100644 index 000000000000..024520568696 --- /dev/null +++ b/cranelift/filetests/filetests/egraph/fold-or-and-not.clif @@ -0,0 +1,64 @@ +test optimize precise-output +set opt_level=speed +target x86_64 + +function %test1(i8, i8) -> i8 { +block0(v0: i8, v1: i8): + v2 = band v0, v1 + v3 = bnot v0 + v4 = bor v2, v3 + return v4 +} + +; function %test1(i8, i8) -> i8 fast { +; block0(v0: i8, v1: i8): +; v3 = bnot v0 +; v5 = bor v1, v3 +; return v5 +; } + +function %test2(i16, i16) -> i16 { +block0(v0: i16, v1: i16): + v2 = band v0, v1 + v3 = bnot v0 + v4 = bor v2, v3 + return v4 +} + +; function %test2(i16, i16) -> i16 fast { +; block0(v0: i16, v1: i16): +; v3 = bnot v0 +; v5 = bor v1, v3 +; return v5 +; } + +function %test3(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = band v0, v1 + v3 = bnot v0 + v4 = bor v2, v3 + return v4 +} + +; function %test3(i32, i32) -> i32 fast { +; block0(v0: i32, v1: i32): +; v3 = bnot v0 +; v5 = bor v1, v3 +; return v5 +; } + +function %test4(i64, i64) -> i64 { +block0(v0: i64, v1: i64): + v2 = band v0, v1 + v3 = bnot v0 + v4 = bor v2, v3 + return v4 +} + +; function %test4(i64, i64) -> i64 fast { +; block0(v0: i64, v1: i64): +; v3 = bnot v0 +; v5 = bor v1, v3 +; return v5 +; } + diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif index e15a268d0c28..9fd88d233f24 100644 --- a/cranelift/filetests/filetests/runtests/bitops.clif +++ b/cranelift/filetests/filetests/runtests/bitops.clif @@ -212,3 +212,13 @@ block0(v0: i64, v1: i64): ; run: %fold_xor_xor15(0xdead0000, 0x0000beef) == 0xdead0000 +function %fold_or_and_not(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = band v0, v1 + v3 = bnot v0 + v4 = bor v2, v3 + return v4 +} + +; run: %fold_or_and_not(0, 0) == 0xffffffff +