Skip to content

Commit 65c1f65

Browse files
authored
Cranelift: Only build iconst for ints <= 64 bits (#5723)
I audited the egraph "algebraic" optimization rules for any which construct an `iconst` on the right-hand side of the rule. In these cases we need to constrain the type passed to `iconst` to be both `fits_in_64` and `ty_int`, because `iconst` is not defined on other types.
1 parent 284fec1 commit 65c1f65

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

cranelift/codegen/src/opts/algebraic.isle

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
(subsume x))
102102

103103
;; x ^ x == 0.
104-
(rule (simplify (bxor (fits_in_64 ty) x x))
104+
(rule (simplify (bxor (fits_in_64 (ty_int ty)) x x))
105105
(subsume (iconst ty (imm64 0))))
106106

107107
;; x ^ not(x) == not(x) ^ x == -1.
@@ -167,6 +167,9 @@
167167
(iadd ty x x))
168168

169169
;; x*c == x<<log2(c) when c is a power of two.
170+
;; Note that the type of `iconst` must be the same as the type of `imul`,
171+
;; so these rules can only fire in situations where it's safe to construct an
172+
;; `iconst` of that type.
170173
(rule (simplify (imul ty x (iconst _ (imm64_power_of_two c))))
171174
(ishl ty x (iconst ty (imm64 c))))
172175
(rule (simplify (imul ty (iconst _ (imm64_power_of_two c)) x))
@@ -252,34 +255,34 @@
252255
;; `x == x` is always true for integers; `x != x` is false. Strict
253256
;; inequalities are false, and loose inequalities are true.
254257
(rule (simplify
255-
(icmp (ty_int ty) (IntCC.Equal) x x))
258+
(icmp (fits_in_64 (ty_int ty)) (IntCC.Equal) x x))
256259
(iconst ty (imm64 1)))
257260
(rule (simplify
258-
(icmp (ty_int ty) (IntCC.NotEqual) x x))
261+
(icmp (fits_in_64 (ty_int ty)) (IntCC.NotEqual) x x))
259262
(iconst ty (imm64 0)))
260263
(rule (simplify
261-
(icmp (ty_int ty) (IntCC.UnsignedGreaterThan) x x))
264+
(icmp (fits_in_64 (ty_int ty)) (IntCC.UnsignedGreaterThan) x x))
262265
(iconst ty (imm64 0)))
263266
(rule (simplify
264-
(icmp (ty_int ty) (IntCC.UnsignedGreaterThanOrEqual) x x))
267+
(icmp (fits_in_64 (ty_int ty)) (IntCC.UnsignedGreaterThanOrEqual) x x))
265268
(iconst ty (imm64 1)))
266269
(rule (simplify
267-
(icmp (ty_int ty) (IntCC.SignedGreaterThan) x x))
270+
(icmp (fits_in_64 (ty_int ty)) (IntCC.SignedGreaterThan) x x))
268271
(iconst ty (imm64 0)))
269272
(rule (simplify
270-
(icmp (ty_int ty) (IntCC.SignedGreaterThanOrEqual) x x))
273+
(icmp (fits_in_64 (ty_int ty)) (IntCC.SignedGreaterThanOrEqual) x x))
271274
(iconst ty (imm64 1)))
272275
(rule (simplify
273-
(icmp (ty_int ty) (IntCC.UnsignedLessThan) x x))
276+
(icmp (fits_in_64 (ty_int ty)) (IntCC.UnsignedLessThan) x x))
274277
(iconst ty (imm64 0)))
275278
(rule (simplify
276-
(icmp (ty_int ty) (IntCC.UnsignedLessThanOrEqual) x x))
279+
(icmp (fits_in_64 (ty_int ty)) (IntCC.UnsignedLessThanOrEqual) x x))
277280
(iconst ty (imm64 1)))
278281
(rule (simplify
279-
(icmp (ty_int ty) (IntCC.SignedLessThan) x x))
282+
(icmp (fits_in_64 (ty_int ty)) (IntCC.SignedLessThan) x x))
280283
(iconst ty (imm64 0)))
281284
(rule (simplify
282-
(icmp (ty_int ty) (IntCC.SignedLessThanOrEqual) x x))
285+
(icmp (fits_in_64 (ty_int ty)) (IntCC.SignedLessThanOrEqual) x x))
283286
(iconst ty (imm64 1)))
284287

285288
;; (x ^ -1) can be replaced with the `bnot` instruction

0 commit comments

Comments
 (0)