1;; `select`/`bitselect`-related rewrites
2
3;; remove select when both choices are the same
4(rule (simplify (select    ty _ x x)) (subsume x))
5(rule (simplify (bitselect ty _ x x)) (subsume x))
6
7;; Push zeroes to the right -- this makes the select `truthy`, as used elsewhere
8;; if icmp { 0 } else { nonzero } => if !icmp { nonzero } else { 0 }
9(rule (simplify (select sty (icmp cty cc x y)
10                        zero @ (iconst_u _ 0)
11                        nonzero @ (iconst_u _ (u64_when_non_zero))))
12    (select sty (icmp cty (intcc_complement cc) x y) nonzero zero))
13
14;; if icmp(x, y) { 1 } else { 0 } => uextend(icmp(x, y))
15(rule (simplify (select ty cmp @ (icmp _ cc x y)
16                        (iconst_u _ 1)
17                        (iconst_u _ 0)))
18    (uextend_maybe ty cmp))
19;; if icmp(x, y) { -1 } else { 0 } => uextend(icmp(x, y))
20(rule (simplify (select ty cmp@(icmp _ cc x y)
21                        (iconst_s _ -1)
22                        (iconst_s _ 0)))
23    (bmask ty cmp))
24
25;; Transform select-of-icmp into {u,s}{min,max} instructions where possible.
26(rule (simplify (select ty (sgt _ x y) x y)) (smax ty x y))
27(rule (simplify (select ty (sge _ x y) x y)) (smax ty x y))
28(rule (simplify (select ty (ugt _ x y) x y)) (umax ty x y))
29(rule (simplify (select ty (uge _ x y) x y)) (umax ty x y))
30(rule (simplify (select ty (slt _ x y) x y)) (smin ty x y))
31(rule (simplify (select ty (sle _ x y) x y)) (smin ty x y))
32(rule (simplify (select ty (ult _ x y) x y)) (umin ty x y))
33(rule (simplify (select ty (ule _ x y) x y)) (umin ty x y))
34
35;; These are the same rules as above, but when the operands for select are swapped
36(rule (simplify (select ty (slt _ x y) y x)) (smax ty x y))
37(rule (simplify (select ty (sle _ x y) y x)) (smax ty x y))
38(rule (simplify (select ty (ult _ x y) y x)) (umax ty x y))
39(rule (simplify (select ty (ule _ x y) y x)) (umax ty x y))
40(rule (simplify (select ty (sgt _ x y) y x)) (smin ty x y))
41(rule (simplify (select ty (sge _ x y) y x)) (smin ty x y))
42(rule (simplify (select ty (ugt _ x y) y x)) (umin ty x y))
43(rule (simplify (select ty (uge _ x y) y x)) (umin ty x y))
44
45;; Transform bitselect-of-icmp into {u,s}{min,max} instructions where possible.
46(rule (simplify (bitselect ty @ (multi_lane _ _) (sgt _ x y) x y)) (smax ty x y))
47(rule (simplify (bitselect ty @ (multi_lane _ _) (sge _ x y) x y)) (smax ty x y))
48(rule (simplify (bitselect ty @ (multi_lane _ _) (ugt _ x y) x y)) (umax ty x y))
49(rule (simplify (bitselect ty @ (multi_lane _ _) (uge _ x y) x y)) (umax ty x y))
50(rule (simplify (bitselect ty @ (multi_lane _ _) (slt _ x y) x y)) (smin ty x y))
51(rule (simplify (bitselect ty @ (multi_lane _ _) (sle _ x y) x y)) (smin ty x y))
52(rule (simplify (bitselect ty @ (multi_lane _ _) (ult _ x y) x y)) (umin ty x y))
53(rule (simplify (bitselect ty @ (multi_lane _ _) (ule _ x y) x y)) (umin ty x y))
54
55;; These are the same rules as above, but when the operands for select are swapped
56(rule (simplify (bitselect ty @ (multi_lane _ _) (slt _ x y) y x)) (smax ty x y))
57(rule (simplify (bitselect ty @ (multi_lane _ _) (sle _ x y) y x)) (smax ty x y))
58(rule (simplify (bitselect ty @ (multi_lane _ _) (ult _ x y) y x)) (umax ty x y))
59(rule (simplify (bitselect ty @ (multi_lane _ _) (ule _ x y) y x)) (umax ty x y))
60(rule (simplify (bitselect ty @ (multi_lane _ _) (sgt _ x y) y x)) (smin ty x y))
61(rule (simplify (bitselect ty @ (multi_lane _ _) (sge _ x y) y x)) (smin ty x y))
62(rule (simplify (bitselect ty @ (multi_lane _ _) (ugt _ x y) y x)) (umin ty x y))
63(rule (simplify (bitselect ty @ (multi_lane _ _) (uge _ x y) y x)) (umin ty x y))
64
65;; (c & x) | (~c & y) -> (bitselect c x y)
66;; These are all the same rule, just with different permutations of the operands
67;;
68;; We currently only match vectors since scalar floats and i128's are not supported
69;; in some backends.
70(rule (simplify (bor (ty_vec128 ty) (band ty c x) (band ty (bnot ty c) y))) (bitselect ty c x y))
71(rule (simplify (bor (ty_vec128 ty) (band ty c x) (band ty y (bnot ty c) ))) (bitselect ty c x y))
72(rule (simplify (bor (ty_vec128 ty) (band ty x c) (band ty (bnot ty c) y))) (bitselect ty c x y))
73(rule (simplify (bor (ty_vec128 ty) (band ty x c) (band ty y (bnot ty c) ))) (bitselect ty c x y))
74(rule (simplify (bor (ty_vec128 ty) (band ty (bnot ty c) y) (band ty c x))) (bitselect ty c x y))
75(rule (simplify (bor (ty_vec128 ty) (band ty (bnot ty c) y) (band ty x c))) (bitselect ty c x y))
76(rule (simplify (bor (ty_vec128 ty) (band ty y (bnot ty c)) (band ty c x))) (bitselect ty c x y))
77(rule (simplify (bor (ty_vec128 ty) (band ty y (bnot ty c)) (band ty x c))) (bitselect ty c x y))
78
79;; Lift an extend operation outside of a `select` if the extend is happening
80;; on both the consequent and the alternative.
81(rule (simplify (select ty cond
82                           (uextend ty a @ (value_type small))
83                           (uextend ty b @ (value_type small))))
84      (uextend ty (select small cond a b)))
85(rule (simplify (select ty cond
86                           (sextend ty a @ (value_type small))
87                           (sextend ty b @ (value_type small))))
88      (sextend ty (select small cond a b)))
89
90(rule (simplify (select ty (sgt _ x (iconst_u ty 0)) x (ineg ty x))) (subsume (iabs ty x)))
91(rule (simplify (select ty (sge _ x (iconst_u ty 0)) x (ineg ty x))) (subsume (iabs ty x)))
92(rule (simplify (select ty (sle _ x (iconst_u ty 0)) (ineg ty x) x)) (subsume (iabs ty x)))
93(rule (simplify (select ty (slt _ x (iconst_u ty 0)) (ineg ty x) x)) (subsume (iabs ty x)))
94
95;; fold add-select to select-add
96(rule (simplify
97        (iadd ty (select ty c (iconst_u ty x) (iconst_u ty y)) (iconst_u ty z)))
98        (select ty c
99            (iconst ty (imm64 (u64_add x z)))
100            (iconst ty (imm64 (u64_add y z)))))
101