Skip to content

Commit 7c999a1

Browse files
committed
cranelift: Add inverted forms of min/max rules
1 parent 87d1139 commit 7c999a1

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

cranelift/codegen/src/opts/algebraic.isle

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,32 @@
374374
(umin ty x y))
375375

376376

377+
;; These are the same rules as above, but when the operands for select are swapped
378+
(rule (simplify
379+
(select ty (icmp _ (IntCC.SignedLessThan) x y) y x))
380+
(smax ty x y))
381+
(rule (simplify
382+
(select ty (icmp _ (IntCC.SignedLessThanOrEqual) x y) y x))
383+
(smax ty x y))
384+
(rule (simplify
385+
(select ty (icmp _ (IntCC.UnsignedLessThan) x y) y x))
386+
(umax ty x y))
387+
(rule (simplify
388+
(select ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) y x))
389+
(umax ty x y))
390+
(rule (simplify
391+
(select ty (icmp _ (IntCC.SignedGreaterThan) x y) y x))
392+
(smin ty x y))
393+
(rule (simplify
394+
(select ty (icmp _ (IntCC.SignedGreaterThanOrEqual) x y) y x))
395+
(smin ty x y))
396+
(rule (simplify
397+
(select ty (icmp _ (IntCC.UnsignedGreaterThan) x y) y x))
398+
(umin ty x y))
399+
(rule (simplify
400+
(select ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) y x))
401+
(umin ty x y))
402+
377403
;; Transform vselect-of-icmp into {u,s}{min,max} instructions where possible.
378404
(rule (simplify
379405
(vselect ty (icmp _ (IntCC.SignedGreaterThan) x y) x y))
@@ -400,7 +426,31 @@
400426
(vselect ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) x y))
401427
(umin ty x y))
402428

403-
429+
;; These are the same rules as above, but when the operands for select are swapped
430+
(rule (simplify
431+
(vselect ty (icmp _ (IntCC.SignedLessThan) x y) y x))
432+
(smax ty x y))
433+
(rule (simplify
434+
(vselect ty (icmp _ (IntCC.SignedLessThanOrEqual) x y) y x))
435+
(smax ty x y))
436+
(rule (simplify
437+
(vselect ty (icmp _ (IntCC.UnsignedLessThan) x y) y x))
438+
(umax ty x y))
439+
(rule (simplify
440+
(vselect ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) y x))
441+
(umax ty x y))
442+
(rule (simplify
443+
(vselect ty (icmp _ (IntCC.SignedGreaterThan) x y) y x))
444+
(smin ty x y))
445+
(rule (simplify
446+
(vselect ty (icmp _ (IntCC.SignedGreaterThanOrEqual) x y) y x))
447+
(smin ty x y))
448+
(rule (simplify
449+
(vselect ty (icmp _ (IntCC.UnsignedGreaterThan) x y) y x))
450+
(umin ty x y))
451+
(rule (simplify
452+
(vselect ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) y x))
453+
(umin ty x y))
404454

405455
;; For floats convert fcmp lt into pseudo_min and gt into pseudo_max
406456
;;

cranelift/filetests/filetests/egraph/select.clif

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ block0(v0: i32, v1: i32):
1818
; check: return v4
1919

2020

21+
; This tests an inverted select, where the operands are swapped.
22+
function %select_sgt_to_smax_inverse(i32, i32) -> i32 {
23+
block0(v0: i32, v1: i32):
24+
v2 = icmp sgt v0, v1
25+
v3 = select v2, v1, v0
26+
return v3
27+
}
28+
29+
; check: block0(v0: i32, v1: i32):
30+
; check: v4 = smin v0, v1
31+
; check: return v4
32+
33+
2134
function %select_sge_to_smax(i32, i32) -> i32 {
2235
block0(v0: i32, v1: i32):
2336
v2 = icmp sge v0, v1

cranelift/filetests/filetests/egraph/vselect.clif

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ block0(v0: i32x4, v1: i32x4):
1717
; check: return v4
1818

1919

20+
; This tests an inverted vselect, where the operands are swapped.
21+
function %vselect_sgt_to_smax(i32x4, i32x4) -> i32x4 {
22+
block0(v0: i32x4, v1: i32x4):
23+
v2 = icmp sgt v0, v1
24+
v3 = vselect v2, v1, v0
25+
return v3
26+
}
27+
28+
; check: block0(v0: i32x4, v1: i32x4):
29+
; check: v4 = smin v0, v1
30+
; check: return v4
31+
32+
33+
2034
function %vselect_sge_to_smax(i32x4, i32x4) -> i32x4 {
2135
block0(v0: i32x4, v1: i32x4):
2236
v2 = icmp sge v0, v1

0 commit comments

Comments
 (0)