Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cranelift/codegen/src/opts/bitops.isle
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions cranelift/filetests/filetests/egraph/fold-bitops.clif
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 16 additions & 1 deletion cranelift/filetests/filetests/runtests/bitops.clif
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
; run: %test_band_bor_absorb_x(0xf0f0f0f0, 0x0f0f0f0f) == 0xf0f0f0f0
Loading