Add some minor souper-harvested optimizations (#5735)

I was playing around with souper recently on some wasms I had lying
around and these are some optimization opportunities that popped out
which seemed easy-enough to add to the egraph-based optimizations.
This commit is contained in:
Alex Crichton
2023-02-07 14:06:24 -06:00
committed by GitHub
parent 08403c9915
commit 72962c9f08
3 changed files with 134 additions and 0 deletions

View File

@@ -289,3 +289,47 @@
(rule (simplify (bxor ty x (iconst ty k)))
(if-let -1 (i64_sextend_imm64 ty k))
(bnot ty x))
;; Masking the result of a comparison with 1 always results in the comparison
;; itself. Note that comparisons in wasm may sometimes be hidden behind
;; extensions.
(rule (simplify
(band (ty_int _)
cmp @ (icmp _ _ _ _)
(iconst _ (u64_from_imm64 1))))
cmp)
(rule (simplify
(band (ty_int _)
extend @ (uextend _ (icmp _ _ _ _))
(iconst _ (u64_from_imm64 1))))
extend)
;; `x < 0` is always false for unsigned integers, and `x >= 0` is always true
;; for unsigned integers, along with their reversals.
(rule (simplify
(icmp (fits_in_64 (ty_int ty))
(IntCC.UnsignedLessThan)
_
(iconst _ (u64_from_imm64 0))))
(iconst ty (imm64 0)))
(rule (simplify
(icmp (fits_in_64 (ty_int ty))
(IntCC.UnsignedGreaterThanOrEqual)
_
(iconst _ (u64_from_imm64 0))))
(iconst ty (imm64 1)))
;; 32-bit integers zero-extended to 64-bit integers are never negative
(rule (simplify
(icmp (ty_int ty)
(IntCC.SignedLessThan)
(uextend $I64 x @ (value_type $I32))
(iconst _ (u64_from_imm64 0))))
(iconst ty (imm64 0)))
(rule (simplify
(icmp (ty_int ty)
(IntCC.SignedGreaterThanOrEqual)
(uextend $I64 x @ (value_type $I32))
(iconst _ (u64_from_imm64 0))))
(iconst ty (imm64 1)))

View File

@@ -110,6 +110,10 @@
(bxor ty k @ (iconst ty _) x))
(bxor ty x k))
(rule (simplify
(icmp ty cc k @ (iconst _ _) x))
(icmp ty (intcc_reverse cc) x k))
;; Canonicalize via associativity: reassociate to a right-heavy tree
;; for constants.
;;