From 6b20496672dfd045487495c81927ef4939e64555 Mon Sep 17 00:00:00 2001 From: myunbin Date: Thu, 5 Mar 2026 13:39:57 +0900 Subject: [PATCH] [Cranelift] bitops add --- cranelift/codegen/src/opts/bitops.isle | 10 +++++++++- .../filetests/filetests/egraph/fold-bitops.clif | 15 +++++++++++++++ .../filetests/filetests/runtests/bitops.clif | 17 ++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cranelift/codegen/src/opts/bitops.isle b/cranelift/codegen/src/opts/bitops.isle index ee8af0f4ac02..25aadb5eeec8 100644 --- a/cranelift/codegen/src/opts/bitops.isle +++ b/cranelift/codegen/src/opts/bitops.isle @@ -246,4 +246,12 @@ (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)) \ No newline at end of file diff --git a/cranelift/filetests/filetests/egraph/fold-bitops.clif b/cranelift/filetests/filetests/egraph/fold-bitops.clif index 4c4a9e14ab20..cfe1b4c659e2 100644 --- a/cranelift/filetests/filetests/egraph/fold-bitops.clif +++ b/cranelift/filetests/filetests/egraph/fold-bitops.clif @@ -107,3 +107,18 @@ 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 +; } \ No newline at end of file diff --git a/cranelift/filetests/filetests/runtests/bitops.clif b/cranelift/filetests/filetests/runtests/bitops.clif index b79dc40ada55..1927cb4bc676 100644 --- a/cranelift/filetests/filetests/runtests/bitops.clif +++ b/cranelift/filetests/filetests/runtests/bitops.clif @@ -257,4 +257,19 @@ block0(v0: i32, v1: i32): ; run: %test_iadd_bor_ineg(0, 0) == 0 ; run: %test_iadd_bor_ineg(1, 1) == 0 ; run: %test_iadd_bor_ineg(2, 1) == 2 -; run: %test_iadd_bor_ineg(0xffffffff, 0) == -1 \ No newline at end of file +; 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 \ No newline at end of file