Skip to content

Commit 7d41355

Browse files
authored
[Cranelift] min x y < y => false (#11998)
* [Cranelift] `min x y < y => false` * fix tet * add comm/flipped * remove redundancies after renaming
1 parent 53d3652 commit 7d41355

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

cranelift/codegen/src/opts/selects.isle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,15 @@
102102
(rule (simplify (select ty d a (select ty d _ y))) (select ty d a y))
103103
(rule (simplify (select ty d (select ty d x _) a)) (select ty d x a))
104104

105+
;; min x y < x => false and its commutative versions
106+
(rule (simplify (sgt ty (smin _ x y) x)) (iconst_u ty 0))
107+
(rule (simplify (sgt ty (smin _ x y) y)) (iconst_u ty 0))
108+
(rule (simplify (slt ty x (smin _ x z))) (iconst_u ty 0))
109+
(rule (simplify (slt ty x (smin _ y x))) (iconst_u ty 0))
110+
(rule (simplify (ugt ty (umin _ x y) x)) (iconst_u ty 0))
111+
(rule (simplify (ugt ty (umin _ x y) y)) (iconst_u ty 0))
112+
(rule (simplify (ult ty x (umin _ x z))) (iconst_u ty 0))
113+
(rule (simplify (ult ty x (umin _ y x))) (iconst_u ty 0))
114+
115+
116+

cranelift/filetests/filetests/egraph/selects.clif

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,31 @@ block0(v0: i8):
6161
; return v8
6262
; }
6363

64+
function %test_sgt_select_slt_min(i32, i32) -> i8 fast {
65+
block0(v0: i32, v1: i32):
66+
v2 = icmp slt v1, v0
67+
v3 = select v2, v1, v0
68+
v4 = icmp sgt v3, v0
69+
return v4
70+
}
71+
72+
; function %test_sgt_select_slt_min(i32, i32) -> i8 fast {
73+
; block0(v0: i32, v1: i32):
74+
; v7 = iconst.i8 0
75+
; return v7 ; v7 = 0
76+
; }
77+
78+
function %test_ugt_select_ult_min(i32, i32) -> i8 fast {
79+
block0(v0: i32, v1: i32):
80+
v2 = icmp ult v1, v0
81+
v3 = select v2, v1, v0
82+
v4 = icmp ugt v3, v0
83+
return v4
84+
}
85+
86+
; function %test_ugt_select_ult_min(i32, i32) -> i8 fast {
87+
; block0(v0: i32, v1: i32):
88+
; v7 = iconst.i8 0
89+
; return v7 ; v7 = 0
90+
; }
91+

cranelift/filetests/filetests/runtests/select.clif

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,24 @@ block0(v0: i64, v1: i64):
441441
; run: %simplify_select_ult_ne_i64(7, 9) == 0
442442
; run: %simplify_select_ult_ne_i64(3, 1) == 1
443443
; run: %simplify_select_ult_ne_i64(-1, 42) == 1
444+
445+
function %test_sgt_select_slt_min(i32, i32) -> i8 fast {
446+
block0(v0: i32, v1: i32):
447+
v2 = icmp slt v1, v0 ; (slt y, x)
448+
v3 = select v2, v1, v0 ; select( (y <_s x), y, x) -> smin(y, x)
449+
v4 = icmp sgt v3, v0 ; sgt( min(y, x), x )
450+
return v4
451+
}
452+
453+
; run: %test_sgt_select_slt_min(42, 444) == 0
454+
455+
function %test_ugt_select_ult_min(i32, i32) -> i8 fast {
456+
block0(v0: i32, v1: i32):
457+
v2 = icmp ult v1, v0 ; (slt y, x)
458+
v3 = select v2, v1, v0 ; select( (y <_s x), y, x) -> umin(y, x)
459+
v4 = icmp ugt v3, v0 ; sgt( min(y, x), x )
460+
return v4
461+
}
462+
463+
; run: %test_ugt_select_ult_min(234234, 5544) == 0
464+

0 commit comments

Comments
 (0)