egraphs: Add bmask bit pattern optimization rule (#6196)

* egraphs: Add a bmask bit pattern optimization

* egraphs: Add more `ineg` rules

* egraphs: Add sshr rule

* egraphs: Simplify bmask rule

* egraphs: Add comutative version of bmask rule

* egraphs: Add more testcases

* egraphs: Cleanup rule comments

* egraphs: Add more `ineg` optimizations
This commit is contained in:
Afonso Bordado
2023-04-14 19:50:48 +01:00
committed by GitHub
parent 2d25db047f
commit 9e1ff9726c
7 changed files with 183 additions and 0 deletions

View File

@@ -67,6 +67,25 @@
(if-let -1 (i64_sextend_imm64 ty c))
(ineg ty x))
;; (!x) + 1 == 1 + (!x) == !(x) - (-1) == ineg(x)
(rule (simplify (iadd ty (bnot ty x) (iconst ty (u64_from_imm64 1))))
(ineg ty x))
(rule (simplify (iadd ty (iconst ty (u64_from_imm64 1)) (bnot ty x)))
(ineg ty x))
(rule (simplify (isub ty (bnot ty x) (iconst ty c)))
(if-let -1 (i64_sextend_imm64 ty c))
(ineg ty x))
;; !(x - 1) == !(x + (-1)) == !((-1) + x) == ineg(x)
(rule (simplify (bnot ty (isub ty x (iconst ty (u64_from_imm64 1)))))
(ineg ty x))
(rule (simplify (bnot ty (iadd ty x (iconst ty c))))
(if-let -1 (i64_sextend_imm64 ty c))
(ineg ty x))
(rule (simplify (bnot ty (iadd ty (iconst ty c) x)))
(if-let -1 (i64_sextend_imm64 ty c))
(ineg ty x))
;; x/1 == x.
(rule (simplify (sdiv ty
x

View File

@@ -92,3 +92,15 @@
(rule (simplify (bxor ty x (iconst ty k)))
(if-let -1 (i64_sextend_imm64 ty k))
(bnot ty x))
;; sshr((x | -x), N) == bmask(x) where N = ty_bits(ty) - 1.
;;
;; (x | -x) sets the sign bit to 1 if x is nonzero, and 0 if x is zero. sshr propagates
;; the sign bit to the rest of the value.
(rule (simplify (sshr ty (bor ty x (ineg ty x)) (iconst ty (u64_from_imm64 shift_amt))))
(if-let $true (u64_eq shift_amt (u64_sub (ty_bits ty) 1)))
(bmask ty x))
(rule (simplify (sshr ty (bor ty (ineg ty x) x) (iconst ty (u64_from_imm64 shift_amt))))
(if-let $true (u64_eq shift_amt (u64_sub (ty_bits ty) 1)))
(bmask ty x))

View File

@@ -78,3 +78,8 @@
(if-let (u64_from_imm64 shift_u64) shift)
(if-let $true (u64_le shift_u64 (u64_sub (ty_bits_u64 wide) (ty_bits_u64 narrow))))
x)
;; ineg(ushr(x, k)) == sshr(x, k) when k == ty_bits - 1.
(rule (simplify (ineg ty (ushr ty x sconst @ (iconst ty (u64_from_imm64 shift_amt)))))
(if-let $true (u64_eq shift_amt (u64_sub (ty_bits ty) 1)))
(sshr ty x sconst))