Skip to content

Commit 7fb8b55

Browse files
authored
[Cranelift] (x < y) & (x ≠ -1) = (x < y) (#11996)
* [Cranelift] `(x < y) & (x ≠ -1) = (x < y)` * fix test
1 parent b570b4f commit 7fb8b55

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

cranelift/codegen/src/opts/icmp.isle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,29 @@
350350
(if-let true (i64_lt x 0))
351351
(iconst_u ty 0))
352352

353+
;; (x < y) & (x ≠ -1) = (x < y)
354+
(rule
355+
(simplify (band ty (ne ty x (iconst_s _ -1)) (ugt ty y x)))
356+
(ult ty x y))
357+
(rule
358+
(simplify (band ty (ne ty x (iconst_s _ -1)) (ult ty x z)))
359+
(ult ty x z))
360+
(rule
361+
(simplify (band ty (ne ty (iconst_s _ -1) x) (ugt ty y x)))
362+
(ult ty x y))
363+
(rule
364+
(simplify (band ty (ne ty (iconst_s _ -1) x) (ult ty x z)))
365+
(ult ty x z))
366+
(rule
367+
(simplify (band ty (ugt ty x y) (ne ty y (iconst_s _ -1))))
368+
(ult ty y x))
369+
(rule
370+
(simplify (band ty (ugt ty x y) (ne ty (iconst_s _ -1) y)))
371+
(ult ty y x))
372+
(rule
373+
(simplify (band ty (ult ty x y) (ne ty x (iconst_s _ -1))))
374+
(ult ty x y))
375+
(rule
376+
(simplify (band ty (ult ty x y) (ne ty (iconst_s _ -1) x)))
377+
(ult ty x y))
378+

cranelift/filetests/filetests/egraph/icmp.clif

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,19 @@ block0(v0: i32):
405405
; return v9 ; v9 = 0
406406
; }
407407

408+
;; (x <u y) & (x != -1) => (x <_u y)
409+
function %test_ult_ne_minus_one(i32, i32) -> i8 fast {
410+
block0(v0: i32, v1: i32):
411+
v2 = icmp ult v0, v1
412+
v3 = iconst.i32 -1
413+
v4 = icmp ne v0, v3
414+
v5 = band v2, v4
415+
return v5
416+
}
417+
418+
; function %test_ult_ne_minus_one(i32, i32) -> i8 fast {
419+
; block0(v0: i32, v1: i32):
420+
; v6 = icmp ult v0, v1
421+
; return v6
422+
; }
423+

cranelift/filetests/filetests/runtests/icmp.clif

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,14 @@ block0(v0: i32):
159159

160160
; run: %test_ult_sgt_neg_const_x(42) == 0
161161

162+
;; (x <u y) & (x != -1) => (x <_u y)
163+
function %test_ult_ne_minus_one(i32, i32) -> i8 {
164+
block0(v0: i32, v1: i32):
165+
v2 = icmp ult v0, v1
166+
v3 = iconst.i32 -1
167+
v4 = icmp ne v0, v3
168+
v5 = band v2, v4
169+
return v5
170+
}
171+
; run: %test_ult_ne_minus_one(3, 5) == 1
172+
; run: %test_ult_ne_minus_one(-1, 1) == 0

0 commit comments

Comments
 (0)