|
15 | 15 |
|
16 | 16 | ;; Optimize icmp-of-icmp. |
17 | 17 | ;; ne(icmp(ty, cc, x, y), 0) == icmp(ty, cc, x, y) |
18 | | -;; e.g. neq(ugt(x, y), 0) == ugt(x, y) |
| 18 | +;; e.g. neq(ugt(x, y), 0) == ugt(x, y) |
19 | 19 | (rule (simplify (ne ty |
20 | 20 | (uextend_maybe _ inner @ (icmp ty _ _ _)) |
21 | 21 | (iconst_u _ 0))) |
22 | 22 | (subsume inner)) |
23 | 23 |
|
24 | 24 | ;; eq(icmp(ty, cc, x, y), 0) == icmp(ty, cc_complement, x, y) |
25 | | -;; e.g. eq(ugt(x, y), 0) == ule(x, y) |
| 25 | +;; e.g. eq(ugt(x, y), 0) == ule(x, y) |
26 | 26 | (rule (simplify (eq ty |
27 | 27 | (uextend_maybe _ (icmp ty cc x y)) |
28 | 28 | (iconst_u _ 0))) |
|
65 | 65 | (iconst_u _ 1))) |
66 | 66 | extend) |
67 | 67 |
|
| 68 | +;; Rewrite loose inequalities to strict inequalities: |
| 69 | +;; ule(x, c) == ult(x, c+1), for c != UMAX. |
| 70 | +(rule (simplify (ule (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 c)))) |
| 71 | + (if-let $false (u64_eq c (ty_umax cty))) |
| 72 | + (ult bty x (iconst cty (imm64 (u64_add c 1))))) |
| 73 | + |
| 74 | +;; uge(x, c) == ugt(x, c-1), for c != 0. |
| 75 | +(rule (simplify (uge (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 c)))) |
| 76 | + (if-let $false (u64_eq c 0)) |
| 77 | + (ugt bty x (iconst cty (imm64 (u64_sub c 1))))) |
| 78 | + |
| 79 | +;; sle(x, c) == slt(x, c+1), for c != SMAX. |
| 80 | +(rule (simplify (sle (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 c)))) |
| 81 | + (if-let $false (u64_eq c (ty_smax cty))) |
| 82 | + (slt bty x (iconst cty (imm64 (u64_add c 1))))) |
| 83 | + |
| 84 | +;; sge(x, c) == sgt(x, c-1), for c != SMIN. |
| 85 | +;; (rule (simplify (sge (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 c)))) |
| 86 | +;; (if-let $false (u64_eq c (ty_smin cty))) |
| 87 | +;; (sgt bty x (iconst cty (imm64 (u64_sub c 1))))) |
| 88 | + |
68 | 89 | ;; Comparisons against largest/smallest signed/unsigned values: |
69 | 90 | ;; ult(x, 0) == false. |
70 | 91 | (rule (simplify (ult (fits_in_64 (ty_int bty)) x zero @ (iconst_u _ 0))) |
71 | 92 | (subsume (iconst_u bty 0))) |
72 | 93 |
|
| 94 | +;; ult(x, 1) == eq(x, 0) |
| 95 | +(rule (simplify (ult (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 0)))) |
| 96 | + (eq bty x (iconst cty (imm64 0)))) |
| 97 | + |
73 | 98 | ;; ule(x, 0) == eq(x, 0) |
74 | 99 | (rule (simplify (ule (fits_in_64 (ty_int bty)) x zero @ (iconst_u _ 0))) |
75 | 100 | (eq bty x zero)) |
|
78 | 103 | (rule (simplify (ugt (fits_in_64 (ty_int bty)) x zero @ (iconst_u _ 0))) |
79 | 104 | (ne bty x zero)) |
80 | 105 |
|
| 106 | +;; ugt(x, UMAX-1) == eq(x, UMAX). |
| 107 | +(rule (simplify (ugt (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 y)))) |
| 108 | + (if-let $true (u64_eq y (u64_sub (ty_umax cty) 1))) |
| 109 | + (eq bty x (iconst cty (imm64 (ty_umax cty))))) |
| 110 | + |
81 | 111 | ;; uge(x, 0) == true. |
82 | 112 | (rule (simplify (uge (fits_in_64 (ty_int bty)) x zero @ (iconst_u _ 0))) |
83 | 113 | (subsume (iconst_u bty 1))) |
|
112 | 142 | (if-let $true (u64_eq y (ty_smin cty))) |
113 | 143 | (eq bty x smin)) |
114 | 144 |
|
| 145 | +;; slt(x, SMIN+1) == eq(x, SMIN). |
| 146 | +(rule (simplify (slt (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 y)))) |
| 147 | + (if-let $true (u64_eq y (u64_add (ty_smin cty) 1))) |
| 148 | + (eq bty x (iconst cty (imm64 (ty_smin cty))))) |
| 149 | + |
115 | 150 | ;; sgt(x, SMIN) == ne(x, SMIN). |
116 | 151 | (rule (simplify (sgt (fits_in_64 (ty_int bty)) x smin @ (iconst_u cty y))) |
117 | 152 | (if-let $true (u64_eq y (ty_smin cty))) |
|
142 | 177 | (if-let $true (u64_eq y (ty_smax cty))) |
143 | 178 | (eq bty x smax)) |
144 | 179 |
|
| 180 | +;; sgt(x, SMAX-1) == eq(x, SMAX). |
| 181 | +(rule (simplify (sgt (fits_in_64 (ty_int bty)) x (iconst cty (u64_from_imm64 y)))) |
| 182 | + (if-let $true (u64_eq y (u64_sub (ty_smax cty) 1))) |
| 183 | + (eq bty x (iconst cty (imm64 (ty_smax cty))))) |
| 184 | + |
145 | 185 | ;; `band`/`bor` of 2 comparisons: |
146 | 186 | (rule (simplify (band (fits_in_64 ty) (icmp ty cc1 x y) (icmp ty cc2 x y))) |
147 | 187 | (if-let signed (intcc_comparable cc1 cc2)) |
|
0 commit comments