Skip to content

Commit b8e5e03

Browse files
authored
Cranelift: add/modify mid-end opt rules (#14296)
1 parent 5966757 commit b8e5e03

15 files changed

Lines changed: 809 additions & 142 deletions

File tree

cranelift/codegen/src/opts/arithmetic.isle

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
;; x+0 == x.
88
(rule iadd_x_plus_zero (simplify (iadd ty
99
x
10-
(iconst_u ty 0)))
10+
(all_zero ty)))
1111
(subsume x))
1212
;; x-0 == x.
1313
(rule (simplify (isub ty
1414
x
15-
(iconst_u ty 0)))
15+
(all_zero ty)))
1616
(subsume x))
1717
;; 0-x == (ineg x).
1818
(rule (simplify (isub ty
19-
(iconst_u ty 0)
19+
(all_zero ty)
2020
x))
2121
(ineg ty x))
2222

@@ -47,7 +47,7 @@
4747
(subsume inner))
4848

4949
;; x-x == 0.
50-
(rule (simplify (isub ty x x)) (subsume (iconst_u ty 0)))
50+
(rule (simplify (isub ty x x)) (subsume (all_zero ty)))
5151

5252
;; x*1 == x.
5353
(rule (simplify (imul ty
@@ -58,7 +58,7 @@
5858
;; x*0 == 0.
5959
(rule (simplify (imul ty
6060
_
61-
zero @ (iconst_u ty 0)))
61+
zero @ (all_zero ty)))
6262
(subsume zero))
6363

6464
;; x*-1 == ineg(x).
@@ -127,9 +127,9 @@
127127
(apply_div_const_magic_s64 (Opcode.Sdiv) x d))
128128

129129
;; x % 1 == 0
130-
(rule (simplify_skeleton (urem x (iconst_u ty 1))) (iconst_u ty 0))
131-
(rule (simplify_skeleton (srem x (iconst_u ty 1))) (iconst_u ty 0))
132-
(rule (simplify_skeleton (srem x (iconst_s ty -1))) (iconst_u ty 0))
130+
(rule (simplify_skeleton (urem x (iconst_u ty 1))) (all_zero ty))
131+
(rule (simplify_skeleton (srem x (iconst_u ty 1))) (all_zero ty))
132+
(rule (simplify_skeleton (srem x (iconst_s ty -1))) (all_zero ty))
133133

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

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

345345
;; ((x - z) - (y - z)) --> (x - y)
346346
(rule (simplify (isub ty (isub ty x z) (isub ty y z))) (isub ty x y))
@@ -392,7 +392,7 @@
392392

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

409409
;; (x - y) != x --> y != 0
410-
(rule (simplify (ne cty (isub ty x y) x)) (ne cty y (iconst_u ty 0)))
411-
(rule (simplify (ne cty x (isub ty x y))) (ne cty y (iconst_u ty 0)))
410+
(rule (simplify (ne cty (isub ty x y) x)) (ne cty y (all_zero ty)))
411+
(rule (simplify (ne cty x (isub ty x y))) (ne cty y (all_zero ty)))
412412

413413
;; (x - y) == x --> y == 0
414-
(rule (simplify (eq cty (isub ty x y) x)) (eq cty y (iconst_u ty 0)))
415-
(rule (simplify (eq cty x (isub ty x y))) (eq cty y (iconst_u ty 0)))
414+
(rule (simplify (eq cty (isub ty x y) x)) (eq cty y (all_zero ty)))
415+
(rule (simplify (eq cty x (isub ty x y))) (eq cty y (all_zero ty)))
416416

417417
;; (x + y) == y --> x == 0
418-
(rule (simplify (eq cty (iadd ty x y) y)) (eq cty x (iconst_u ty 0)))
419-
(rule (simplify (eq cty (iadd ty y x) y)) (eq cty x (iconst_u ty 0)))
420-
(rule (simplify (eq cty y (iadd ty x y))) (eq cty x (iconst_u ty 0)))
421-
(rule (simplify (eq cty y (iadd ty y x))) (eq cty x (iconst_u ty 0)))
418+
(rule (simplify (eq cty (iadd ty x y) y)) (eq cty x (all_zero ty)))
419+
(rule (simplify (eq cty (iadd ty y x) y)) (eq cty x (all_zero ty)))
420+
(rule (simplify (eq cty y (iadd ty x y))) (eq cty x (all_zero ty)))
421+
(rule (simplify (eq cty y (iadd ty y x))) (eq cty x (all_zero ty)))
422422

423423
;; -x == -y --> x == y
424424
(rule (simplify (eq ty (ineg ty x) (ineg ty y))) (eq ty x y))
@@ -574,14 +574,14 @@
574574
(rule (simplify (umax ty (umin ty x y) (umax ty y x))) (umax ty x y))
575575

576576
;; x > max(x, y) --> 0
577-
(rule (simplify (sgt ty x (smax ty x y))) (iconst_u ty 0))
578-
(rule (simplify (sgt ty x (smax ty y x))) (iconst_u ty 0))
579-
(rule (simplify (slt ty (smax ty x y) x)) (iconst_u ty 0))
580-
(rule (simplify (slt ty (smax ty y x) x)) (iconst_u ty 0))
581-
(rule (simplify (ugt ty x (umax ty x y))) (iconst_u ty 0))
582-
(rule (simplify (ugt ty x (umax ty y x))) (iconst_u ty 0))
583-
(rule (simplify (ult ty (umax ty x y) x)) (iconst_u ty 0))
584-
(rule (simplify (ult ty (umax ty y x) x)) (iconst_u ty 0))
577+
(rule (simplify (sgt ty x (smax ty x y))) (all_zero ty))
578+
(rule (simplify (sgt ty x (smax ty y x))) (all_zero ty))
579+
(rule (simplify (slt ty (smax ty x y) x)) (all_zero ty))
580+
(rule (simplify (slt ty (smax ty y x) x)) (all_zero ty))
581+
(rule (simplify (ugt ty x (umax ty x y))) (all_zero ty))
582+
(rule (simplify (ugt ty x (umax ty y x))) (all_zero ty))
583+
(rule (simplify (ult ty (umax ty x y) x)) (all_zero ty))
584+
(rule (simplify (ult ty (umax ty y x) x)) (all_zero ty))
585585

586586
;; (-X) * C = X * (-C)
587587
(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))))
@@ -619,4 +619,8 @@
619619
(rule (simplify (imul ty (umax ty (ineg ty x) x) (umax ty (ineg ty x) x))) (imul ty x x))
620620
(rule (simplify (imul ty (umax ty (ineg ty x) x) (umax ty x (ineg ty x)))) (imul ty x x))
621621
(rule (simplify (imul ty (umax ty x (ineg ty x)) (umax ty (ineg ty x) x))) (imul ty x x))
622-
(rule (simplify (imul ty (umax ty x (ineg ty x)) (umax ty x (ineg ty x)))) (imul ty x x))
622+
(rule (simplify (imul ty (umax ty x (ineg ty x)) (umax ty x (ineg ty x)))) (imul ty x x))
623+
624+
;; x /u (1 << y) --> x >>u y
625+
(rule (simplify_skeleton (udiv x (ishl ty (iconst_u ty 1) y)))
626+
(ushr ty x y))

cranelift/codegen/src/opts/bitops.isle

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@
117117
(rule (truthy (popcnt _ x)) x)
118118
(rule (truthy (rotl _ x _)) x)
119119
(rule (truthy (rotr _ x _)) x)
120-
(rule (truthy (select _ x (iconst_u _ (u64_when_non_zero)) (iconst_u _ 0))) x)
120+
(rule (truthy (select _ x (iconst_u _ (u64_when_non_zero)) (all_zero _))) x)
121121
;; (ne ty (iconst 0) v) is also canonicalized into this form via another rule
122-
(rule (truthy (ne _ x (iconst_u _ 0))) x)
122+
(rule (truthy (ne _ x (all_zero _))) x)
123123

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

134134

135135

@@ -790,3 +790,44 @@
790790

791791
;; popcnt(bitrev(x)) == popcnt(x)
792792
(rule (simplify (popcnt ty (bitrev ty x))) (popcnt ty x))
793+
794+
;; (x ^ y) ^ (x ^ z) --> y ^ z.
795+
(rule (simplify (bxor ty (bxor ty x y) (bxor ty x z))) (subsume (bxor ty y z)))
796+
(rule (simplify (bxor ty (bxor ty x y) (bxor ty z x))) (subsume (bxor ty y z)))
797+
(rule (simplify (bxor ty (bxor ty y x) (bxor ty x z))) (subsume (bxor ty y z)))
798+
(rule (simplify (bxor ty (bxor ty y x) (bxor ty z x))) (subsume (bxor ty y z)))
799+
800+
;; (-x) & 1 --> x & 1.
801+
(rule (simplify (band ty (ineg ty x) (iconst_u ty 1))) (band ty x (iconst_u ty 1)))
802+
803+
;; (x & y) & (x | z) --> x & y.
804+
(rule (simplify (band ty (band ty x y) (bor ty x z))) (subsume (band ty x y)))
805+
(rule (simplify (band ty (bor ty x z) (band ty x y))) (subsume (band ty x y)))
806+
(rule (simplify (band ty (band ty x y) (bor ty z x))) (subsume (band ty x y)))
807+
(rule (simplify (band ty (bor ty z x) (band ty x y))) (subsume (band ty x y)))
808+
(rule (simplify (band ty (band ty y x) (bor ty x z))) (subsume (band ty x y)))
809+
(rule (simplify (band ty (bor ty x z) (band ty y x))) (subsume (band ty x y)))
810+
(rule (simplify (band ty (band ty y x) (bor ty z x))) (subsume (band ty x y)))
811+
(rule (simplify (band ty (bor ty z x) (band ty y x))) (subsume (band ty x y)))
812+
813+
;; (x & y) | (x | z) --> x | z.
814+
(rule (simplify (bor ty (band ty x y) (bor ty x z))) (subsume (bor ty x z)))
815+
(rule (simplify (bor ty (bor ty x z) (band ty x y))) (subsume (bor ty x z)))
816+
(rule (simplify (bor ty (band ty x y) (bor ty z x))) (subsume (bor ty x z)))
817+
(rule (simplify (bor ty (bor ty z x) (band ty x y))) (subsume (bor ty x z)))
818+
(rule (simplify (bor ty (band ty y x) (bor ty x z))) (subsume (bor ty x z)))
819+
(rule (simplify (bor ty (bor ty x z) (band ty y x))) (subsume (bor ty x z)))
820+
(rule (simplify (bor ty (band ty y x) (bor ty z x))) (subsume (bor ty x z)))
821+
(rule (simplify (bor ty (bor ty z x) (band ty y x))) (subsume (bor ty x z)))
822+
823+
;; (x & y) | (x ^ y) --> x | y.
824+
(rule (simplify (bor ty (band ty x y) (bxor ty x y))) (bor ty x y))
825+
(rule (simplify (bor ty (bxor ty x y) (band ty x y))) (bor ty x y))
826+
(rule (simplify (bor ty (band ty x y) (bxor ty y x))) (bor ty x y))
827+
(rule (simplify (bor ty (bxor ty y x) (band ty x y))) (bor ty x y))
828+
829+
;; ~((~x) - y) --> x + y.
830+
(rule (simplify (bnot ty (isub ty (bnot ty x) y))) (iadd ty x y))
831+
832+
;; ~((~x) >>s y) --> x >>s y.
833+
(rule (simplify (bnot ty (sshr ty (bnot ty x) y))) (sshr ty x y))

cranelift/codegen/src/opts/cprop.isle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@
226226

227227
(rule (simplify (select ty (iconst_u _ (u64_when_non_zero)) x _))
228228
(subsume x))
229-
(rule (simplify (select ty (iconst_u _ 0) _ y))
229+
(rule (simplify (select ty (all_zero _) _ y))
230230
(subsume y))
231231

232232
(rule (simplify

cranelift/codegen/src/opts/extends.isle

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@
2828
(rule (simplify
2929
(slt ty
3030
(uextend $I64 x @ (value_type $I32))
31-
(iconst_u _ 0)))
32-
(subsume (iconst_u ty 0)))
31+
(all_zero _)))
32+
(subsume (all_zero ty)))
3333
(rule (simplify
3434
(sge ty
3535
(uextend $I64 x @ (value_type $I32))
36-
(iconst_u _ 0)))
36+
(all_zero _)))
3737
(subsume (iconst_u ty 1)))
3838

3939
;; Sign-extending can't change whether a number is zero nor how it signed-compares to zero
40-
(rule (simplify (eq _ (sextend _ x@(value_type ty)) (iconst_s _ 0)))
41-
(eq ty x (iconst_s ty 0)))
42-
(rule (simplify (ne _ (sextend _ x@(value_type ty)) (iconst_s _ 0)))
43-
(ne ty x (iconst_s ty 0)))
44-
(rule (simplify (icmp _ cc (sextend _ x@(value_type ty)) (iconst_s _ 0)))
40+
(rule (simplify (eq _ (sextend _ x@(value_type ty)) (all_zero _)))
41+
(eq ty x (all_zero ty)))
42+
(rule (simplify (ne _ (sextend _ x@(value_type ty)) (all_zero _)))
43+
(ne ty x (all_zero ty)))
44+
(rule (simplify (icmp _ cc (sextend _ x@(value_type ty)) (all_zero _)))
4545
(if (signed_cond_code cc))
46-
(icmp ty cc x (iconst_s ty 0)))
46+
(icmp ty cc x (all_zero ty)))
4747

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

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

9797
;; Select narrowest type

0 commit comments

Comments
 (0)