Skip to content

Commit 2be8ae4

Browse files
committed
ISLE opts for x ± a == y ± b
1 parent a968b7d commit 2be8ae4

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

cranelift/codegen/src/opts/cprop.isle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@
172172
(rule (simplify (select ty (iconst_u _ 0) _ y))
173173
(subsume y))
174174

175+
;; Similarly, move constants to the right side of an equality comparison.
176+
;; `x + C == y` --> `x == y - C`
177+
(rule (simplify (eq ty1 (iadd ty2 x k@(iconst _ _)) y))
178+
(eq ty1 x (isub ty2 y k)))
179+
;; `x - C == y` --> `x == y + C`
180+
(rule (simplify (eq ty1 (isub ty2 x k@(iconst _ _)) y))
181+
(eq ty1 x (iadd ty2 y k)))
182+
;; `x + C != y` --> `x != y - C`
183+
(rule (simplify (ne ty1 (iadd ty2 x k@(iconst _ _)) y))
184+
(ne ty1 x (isub ty2 y k)))
185+
;; `x - C != y` --> `x != y + C`
186+
(rule (simplify (ne ty1 (isub ty2 x k@(iconst _ _)) y))
187+
(ne ty1 x (iadd ty2 y k)))
188+
175189
;; Replace subtraction by a "negative" constant with addition.
176190
;; Notably, this gives `x - (-1) == x + 1`, so other patterns don't have to
177191
;; match the subtract-negative-one version too.

cranelift/codegen/src/opts/icmp.isle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
(rule (simplify (slt (ty_int ty) x x)) (subsume (iconst_u ty 0)))
1414
(rule (simplify (sle (ty_int ty) x x)) (subsume (iconst_u ty 1)))
1515

16+
;; For integers, adding or subtracting the same thing on both sides of an
17+
;; equality check (or inequality check) doesn't change the result
18+
19+
(rule (simplify (eq ty (iadd _ a k) (iadd _ b k)))
20+
(eq ty a b))
21+
(rule (simplify (eq ty (isub _ a k) (isub _ b k)))
22+
(eq ty a b))
23+
(rule (simplify (ne ty (iadd _ a k) (iadd _ b k)))
24+
(ne ty a b))
25+
(rule (simplify (ne ty (isub _ a k) (isub _ b k)))
26+
(ne ty a b))
27+
1628
;; Optimize icmp-of-icmp.
1729
;; ne(icmp(ty, cc, x, y), 0) == icmp(ty, cc, x, y)
1830
;; e.g. neq(ugt(x, y), 0) == ugt(x, y)

cranelift/filetests/filetests/egraph/cprop.clif

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,32 @@ block0:
231231
; check: v3 = iconst.i8 0
232232
; nextln: return v3
233233

234+
function %icmp_sums_same_addend(i32, i32, i32) -> i8 {
235+
block0(v0: i32, v1: i32, v2: i32):
236+
v3 = iadd v0, v2
237+
v4 = iadd v1, v2
238+
v5 = icmp eq v3, v4
239+
return v5
240+
}
241+
242+
; check: v6 = icmp eq v0, v1
243+
; nextln: return v6
244+
245+
function %icmp_sums_const_addends(i32, i32) -> i8 {
246+
block0(v0: i32, v1: i32):
247+
v3 = iconst.i32 123
248+
v4 = iconst.i32 456
249+
v5 = iadd v0, v3
250+
v6 = iadd v1, v4
251+
v7 = icmp eq v5, v6
252+
return v7
253+
}
254+
255+
; check: v11 = iconst.i32 333
256+
; check: v12 = iadd v1, v11
257+
; check: v15 = icmp eq v0, v12
258+
; nextln: return v15
259+
234260
function %ireduce_iconst() -> i8 {
235261
block0:
236262
v1 = iconst.i16 -10

0 commit comments

Comments
 (0)