Skip to content
Merged
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
58 changes: 31 additions & 27 deletions cranelift/codegen/src/opts/arithmetic.isle
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
;; x+0 == x.
(rule iadd_x_plus_zero (simplify (iadd ty
x
(iconst_u ty 0)))
(all_zero ty)))
(subsume x))
;; x-0 == x.
(rule (simplify (isub ty
x
(iconst_u ty 0)))
(all_zero ty)))
(subsume x))
;; 0-x == (ineg x).
(rule (simplify (isub ty
(iconst_u ty 0)
(all_zero ty)
x))
(ineg ty x))

Expand Down Expand Up @@ -47,7 +47,7 @@
(subsume inner))

;; x-x == 0.
(rule (simplify (isub ty x x)) (subsume (iconst_u ty 0)))
(rule (simplify (isub ty x x)) (subsume (all_zero ty)))

;; x*1 == x.
(rule (simplify (imul ty
Expand All @@ -58,7 +58,7 @@
;; x*0 == 0.
(rule (simplify (imul ty
_
zero @ (iconst_u ty 0)))
zero @ (all_zero ty)))
(subsume zero))

;; x*-1 == ineg(x).
Expand Down Expand Up @@ -127,9 +127,9 @@
(apply_div_const_magic_s64 (Opcode.Sdiv) x d))

;; x % 1 == 0
(rule (simplify_skeleton (urem x (iconst_u ty 1))) (iconst_u ty 0))
(rule (simplify_skeleton (srem x (iconst_u ty 1))) (iconst_u ty 0))
(rule (simplify_skeleton (srem x (iconst_s ty -1))) (iconst_u ty 0))
(rule (simplify_skeleton (urem x (iconst_u ty 1))) (all_zero ty))
(rule (simplify_skeleton (srem x (iconst_u ty 1))) (all_zero ty))
(rule (simplify_skeleton (srem x (iconst_s ty -1))) (all_zero ty))

;; Unsigned `x % d == x & ((1 << ilog2(d)) - 1)` when `d` is a power of two.
(rule (simplify_skeleton (urem x (iconst_u ty (u64_extract_power_of_two d))))
Expand Down Expand Up @@ -340,7 +340,7 @@
(rule (simplify (isub ty (iadd ty y x) (iadd ty z x))) (isub ty y z))

;; (x - y) + (y - x) --> 0
(rule (simplify (iadd ty (isub ty x y) (isub ty y x))) (subsume (iconst_u ty 0)))
(rule (simplify (iadd ty (isub ty x y) (isub ty y x))) (subsume (all_zero ty)))

;; ((x - z) - (y - z)) --> (x - y)
(rule (simplify (isub ty (isub ty x z) (isub ty y z))) (isub ty x y))
Expand Down Expand Up @@ -392,7 +392,7 @@

;; Helper to create a "true" value for a comparison. For scalar integers this is
;; a value of 1 but for vectors this is -1 since each lane is filled with all
;; 1s. We use `(iconst_u ty 0)` for falses for both integers and vector. This is
;; 1s. We use `(all_zero ty)` for falses for both integers and vectors. This is
;; because of the Cranelift semantics:
;; When comparing scalars, the result is 1 if the condition holds, or 0 otherwise.
;; When comparing vectors, the result is -1 (all-ones) if the condition holds, or 0 otherwise.
Expand All @@ -407,18 +407,18 @@
(rule (simplify (eq ty (iadd cty y x) (iadd cty y x))) (cmp_true ty))

;; (x - y) != x --> y != 0
(rule (simplify (ne cty (isub ty x y) x)) (ne cty y (iconst_u ty 0)))
(rule (simplify (ne cty x (isub ty x y))) (ne cty y (iconst_u ty 0)))
(rule (simplify (ne cty (isub ty x y) x)) (ne cty y (all_zero ty)))
(rule (simplify (ne cty x (isub ty x y))) (ne cty y (all_zero ty)))

;; (x - y) == x --> y == 0
(rule (simplify (eq cty (isub ty x y) x)) (eq cty y (iconst_u ty 0)))
(rule (simplify (eq cty x (isub ty x y))) (eq cty y (iconst_u ty 0)))
(rule (simplify (eq cty (isub ty x y) x)) (eq cty y (all_zero ty)))
(rule (simplify (eq cty x (isub ty x y))) (eq cty y (all_zero ty)))

;; (x + y) == y --> x == 0
(rule (simplify (eq cty (iadd ty x y) y)) (eq cty x (iconst_u ty 0)))
(rule (simplify (eq cty (iadd ty y x) y)) (eq cty x (iconst_u ty 0)))
(rule (simplify (eq cty y (iadd ty x y))) (eq cty x (iconst_u ty 0)))
(rule (simplify (eq cty y (iadd ty y x))) (eq cty x (iconst_u ty 0)))
(rule (simplify (eq cty (iadd ty x y) y)) (eq cty x (all_zero ty)))
(rule (simplify (eq cty (iadd ty y x) y)) (eq cty x (all_zero ty)))
(rule (simplify (eq cty y (iadd ty x y))) (eq cty x (all_zero ty)))
(rule (simplify (eq cty y (iadd ty y x))) (eq cty x (all_zero ty)))

;; -x == -y --> x == y
(rule (simplify (eq ty (ineg ty x) (ineg ty y))) (eq ty x y))
Expand Down Expand Up @@ -574,14 +574,14 @@
(rule (simplify (umax ty (umin ty x y) (umax ty y x))) (umax ty x y))

;; x > max(x, y) --> 0
(rule (simplify (sgt ty x (smax ty x y))) (iconst_u ty 0))
(rule (simplify (sgt ty x (smax ty y x))) (iconst_u ty 0))
(rule (simplify (slt ty (smax ty x y) x)) (iconst_u ty 0))
(rule (simplify (slt ty (smax ty y x) x)) (iconst_u ty 0))
(rule (simplify (ugt ty x (umax ty x y))) (iconst_u ty 0))
(rule (simplify (ugt ty x (umax ty y x))) (iconst_u ty 0))
(rule (simplify (ult ty (umax ty x y) x)) (iconst_u ty 0))
(rule (simplify (ult ty (umax ty y x) x)) (iconst_u ty 0))
(rule (simplify (sgt ty x (smax ty x y))) (all_zero ty))
(rule (simplify (sgt ty x (smax ty y x))) (all_zero ty))
(rule (simplify (slt ty (smax ty x y) x)) (all_zero ty))
(rule (simplify (slt ty (smax ty y x) x)) (all_zero ty))
(rule (simplify (ugt ty x (umax ty x y))) (all_zero ty))
(rule (simplify (ugt ty x (umax ty y x))) (all_zero ty))
(rule (simplify (ult ty (umax ty x y) x)) (all_zero ty))
(rule (simplify (ult ty (umax ty y x) x)) (all_zero ty))

;; (-X) * C = X * (-C)
(rule imul_ineg_const (simplify (imul (fits_in_64 ty) (ineg ty x) (iconst ty y))) (imul ty x (iconst ty (imm64_neg ty y))))
Expand Down Expand Up @@ -619,4 +619,8 @@
(rule (simplify (imul ty (umax ty (ineg ty x) x) (umax ty (ineg ty x) x))) (imul ty x x))
(rule (simplify (imul ty (umax ty (ineg ty x) x) (umax ty x (ineg ty x)))) (imul ty x x))
(rule (simplify (imul ty (umax ty x (ineg ty x)) (umax ty (ineg ty x) x))) (imul ty x x))
(rule (simplify (imul ty (umax ty x (ineg ty x)) (umax ty x (ineg ty x)))) (imul ty x x))
(rule (simplify (imul ty (umax ty x (ineg ty x)) (umax ty x (ineg ty x)))) (imul ty x x))

;; x /u (1 << y) --> x >>u y
(rule (simplify_skeleton (udiv x (ishl ty (iconst_u ty 1) y)))
(ushr ty x y))
49 changes: 45 additions & 4 deletions cranelift/codegen/src/opts/bitops.isle
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@
(rule (truthy (popcnt _ x)) x)
(rule (truthy (rotl _ x _)) x)
(rule (truthy (rotr _ x _)) x)
(rule (truthy (select _ x (iconst_u _ (u64_when_non_zero)) (iconst_u _ 0))) x)
(rule (truthy (select _ x (iconst_u _ (u64_when_non_zero)) (all_zero _))) x)
;; (ne ty (iconst 0) v) is also canonicalized into this form via another rule
(rule (truthy (ne _ x (iconst_u _ 0))) x)
(rule (truthy (ne _ x (all_zero _))) x)

;; All of these expressions don't care about their input as long as it is truthy.
;; so we can remove expressions that preserve that property from the input.
(rule (simplify (bmask ty v)) (if-let x (truthy v)) (bmask ty x))
(rule (simplify (select ty v t f)) (if-let c (truthy v)) (select ty c t f))
;; (ne ty (iconst 0) v) is also canonicalized into this form via another rule
(rule (simplify (ne cty v (iconst_u _ 0)))
(rule (simplify (ne cty v (all_zero _)))
(if-let c (truthy v))
(if-let (value_type (ty_int_ref_scalar_64_extract ty)) c)
(ne cty c (iconst_u ty 0)))
(ne cty c (all_zero ty)))



Expand Down Expand Up @@ -790,3 +790,44 @@

;; popcnt(bitrev(x)) == popcnt(x)
(rule (simplify (popcnt ty (bitrev ty x))) (popcnt ty x))

;; (x ^ y) ^ (x ^ z) --> y ^ z.
(rule (simplify (bxor ty (bxor ty x y) (bxor ty x z))) (subsume (bxor ty y z)))
(rule (simplify (bxor ty (bxor ty x y) (bxor ty z x))) (subsume (bxor ty y z)))
(rule (simplify (bxor ty (bxor ty y x) (bxor ty x z))) (subsume (bxor ty y z)))
(rule (simplify (bxor ty (bxor ty y x) (bxor ty z x))) (subsume (bxor ty y z)))

;; (-x) & 1 --> x & 1.
(rule (simplify (band ty (ineg ty x) (iconst_u ty 1))) (band ty x (iconst_u ty 1)))

;; (x & y) & (x | z) --> x & y.
(rule (simplify (band ty (band ty x y) (bor ty x z))) (subsume (band ty x y)))
(rule (simplify (band ty (bor ty x z) (band ty x y))) (subsume (band ty x y)))
(rule (simplify (band ty (band ty x y) (bor ty z x))) (subsume (band ty x y)))
(rule (simplify (band ty (bor ty z x) (band ty x y))) (subsume (band ty x y)))
(rule (simplify (band ty (band ty y x) (bor ty x z))) (subsume (band ty x y)))
(rule (simplify (band ty (bor ty x z) (band ty y x))) (subsume (band ty x y)))
(rule (simplify (band ty (band ty y x) (bor ty z x))) (subsume (band ty x y)))
(rule (simplify (band ty (bor ty z x) (band ty y x))) (subsume (band ty x y)))

;; (x & y) | (x | z) --> x | z.
(rule (simplify (bor ty (band ty x y) (bor ty x z))) (subsume (bor ty x z)))
(rule (simplify (bor ty (bor ty x z) (band ty x y))) (subsume (bor ty x z)))
(rule (simplify (bor ty (band ty x y) (bor ty z x))) (subsume (bor ty x z)))
(rule (simplify (bor ty (bor ty z x) (band ty x y))) (subsume (bor ty x z)))
(rule (simplify (bor ty (band ty y x) (bor ty x z))) (subsume (bor ty x z)))
(rule (simplify (bor ty (bor ty x z) (band ty y x))) (subsume (bor ty x z)))
(rule (simplify (bor ty (band ty y x) (bor ty z x))) (subsume (bor ty x z)))
(rule (simplify (bor ty (bor ty z x) (band ty y x))) (subsume (bor ty x z)))

;; (x & y) | (x ^ y) --> x | y.
(rule (simplify (bor ty (band ty x y) (bxor ty x y))) (bor ty x y))
(rule (simplify (bor ty (bxor ty x y) (band ty x y))) (bor ty x y))
(rule (simplify (bor ty (band ty x y) (bxor ty y x))) (bor ty x y))
(rule (simplify (bor ty (bxor ty y x) (band ty x y))) (bor ty x y))

;; ~((~x) - y) --> x + y.
(rule (simplify (bnot ty (isub ty (bnot ty x) y))) (iadd ty x y))

;; ~((~x) >>s y) --> x >>s y.
(rule (simplify (bnot ty (sshr ty (bnot ty x) y))) (sshr ty x y))
2 changes: 1 addition & 1 deletion cranelift/codegen/src/opts/cprop.isle
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@

(rule (simplify (select ty (iconst_u _ (u64_when_non_zero)) x _))
(subsume x))
(rule (simplify (select ty (iconst_u _ 0) _ y))
(rule (simplify (select ty (all_zero _) _ y))
(subsume y))

(rule (simplify
Expand Down
20 changes: 10 additions & 10 deletions cranelift/codegen/src/opts/extends.isle
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@
(rule (simplify
(slt ty
(uextend $I64 x @ (value_type $I32))
(iconst_u _ 0)))
(subsume (iconst_u ty 0)))
(all_zero _)))
(subsume (all_zero ty)))
(rule (simplify
(sge ty
(uextend $I64 x @ (value_type $I32))
(iconst_u _ 0)))
(all_zero _)))
(subsume (iconst_u ty 1)))

;; Sign-extending can't change whether a number is zero nor how it signed-compares to zero
(rule (simplify (eq _ (sextend _ x@(value_type ty)) (iconst_s _ 0)))
(eq ty x (iconst_s ty 0)))
(rule (simplify (ne _ (sextend _ x@(value_type ty)) (iconst_s _ 0)))
(ne ty x (iconst_s ty 0)))
(rule (simplify (icmp _ cc (sextend _ x@(value_type ty)) (iconst_s _ 0)))
(rule (simplify (eq _ (sextend _ x@(value_type ty)) (all_zero _)))
(eq ty x (all_zero ty)))
(rule (simplify (ne _ (sextend _ x@(value_type ty)) (all_zero _)))
(ne ty x (all_zero ty)))
(rule (simplify (icmp _ cc (sextend _ x@(value_type ty)) (all_zero _)))
(if (signed_cond_code cc))
(icmp ty cc x (iconst_s ty 0)))
(icmp ty cc x (all_zero ty)))

;; A reduction-of-an-extend back to the same original type is the same as not
;; actually doing the extend in the first place.
Expand Down Expand Up @@ -91,7 +91,7 @@
(rule (simplify (ireduce ty (band _ x y))) (band ty (ireduce ty x) (ireduce ty y)))

;; Try to transform an `iconcat` into an i128 into either an sextend or uextend
(rule (simplify (iconcat $I128 x (iconst_u _ 0))) (uextend $I128 x))
(rule (simplify (iconcat $I128 x (all_zero _))) (uextend $I128 x))
(rule (simplify (iconcat $I128 x (sshr _ x (iconst_u _ 63)))) (sextend $I128 x))

;; Select narrowest type
Expand Down
Loading
Loading