Skip to content

Commit f6c69b0

Browse files
authored
[Cranelift] (x & y) | ~(x ^ y) => ~(x ^ y) (#12686)
* cranelift: simplify bor(and, not(xor)) to not(xor) * cranelift: extend bor(and, not(xor)) simplify coverage
1 parent 0f28058 commit f6c69b0

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

cranelift/codegen/src/opts/bitops.isle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,13 @@
225225
(rule (simplify
226226
(bxor ty (band ty z x) (band ty z y)))
227227
(band ty z (bxor ty x y)))
228+
229+
; (x & y) | ~(x ^ y) => ~(x ^ y)
230+
(rule (simplify (bor ty (band ty x y) (bnot ty (bxor ty x y)))) (bnot ty (bxor ty x y)))
231+
(rule (simplify (bor ty (bnot ty (bxor ty x y)) (band ty x y))) (bnot ty (bxor ty x y)))
232+
(rule (simplify (bor ty (band ty x y) (bnot ty (bxor ty y x)))) (bnot ty (bxor ty x y)))
233+
(rule (simplify (bor ty (bnot ty (bxor ty y x)) (band ty x y))) (bnot ty (bxor ty x y)))
234+
(rule (simplify (bor ty (band ty y x) (bnot ty (bxor ty x y)))) (bnot ty (bxor ty x y)))
235+
(rule (simplify (bor ty (bnot ty (bxor ty x y)) (band ty y x))) (bnot ty (bxor ty x y)))
236+
(rule (simplify (bor ty (band ty y x) (bnot ty (bxor ty y x)))) (bnot ty (bxor ty x y)))
237+
(rule (simplify (bor ty (bnot ty (bxor ty y x)) (band ty y x))) (bnot ty (bxor ty x y)))

cranelift/filetests/filetests/egraph/fold-bitops.clif

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,19 @@ block0(v0: i32, v1: i32, v2: i32):
7474
; return v7
7575
; }
7676

77+
;; (bor ty (band ty x y) (bnot ty (bxor ty x y))) -> (bnot ty (bxor ty x y))
78+
function %test_bor_band_bnot_bxor(i32, i32) -> i32 fast {
79+
block0(v0: i32, v1: i32):
80+
v2 = band v0, v1
81+
v3 = bxor v0, v1
82+
v4 = bnot v3
83+
v5 = bor v2, v4
84+
return v5
85+
}
86+
87+
; function %test_bor_band_bnot_bxor(i32, i32) -> i32 fast {
88+
; block0(v0: i32, v1: i32):
89+
; v6 = bxor v1, v0
90+
; v7 = bnot v6
91+
; return v7
92+
; }

cranelift/filetests/filetests/runtests/bitops.clif

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,16 @@ block0(v0: i32, v1: i32, v2: i32):
232232

233233
; run: %test_bxor_band_band(2, 1, 1) == 0
234234

235+
function %test_bor_band_bnot_bxor(i32, i32) -> i32 fast {
236+
block0(v0: i32, v1: i32):
237+
v2 = band v0, v1
238+
v3 = bxor v0, v1
239+
v4 = bnot v3
240+
v5 = bor v2, v4
241+
return v5
242+
}
243+
244+
; run: %test_bor_band_bnot_bxor(0, 0) == 0xffffffff
245+
; run: %test_bor_band_bnot_bxor(1, 1) == 0xffffffff
246+
; run: %test_bor_band_bnot_bxor(0, 1) == 0xfffffffe
247+
; run: %test_bor_band_bnot_bxor(2, 1) == 0xfffffffc

0 commit comments

Comments
 (0)