|
124 | 124 | ;; |
125 | 125 | ;; (op k x) --> (op x k) |
126 | 126 |
|
127 | | -(rule (simplify |
128 | | - (iadd ty k @ (iconst ty _) x)) |
129 | | - (iadd ty x k)) |
| 127 | +;; (rule (simplify |
| 128 | +;; (iadd ty k @ (iconst ty _) x)) |
| 129 | +;; (iadd ty x k)) |
130 | 130 | ;; sub is not commutative, but we can flip the args and negate the |
131 | 131 | ;; whole thing. |
132 | | -(rule (simplify |
133 | | - (isub ty k @ (iconst ty _) x)) |
134 | | - (ineg ty (isub ty x k))) |
135 | | -(rule (simplify |
136 | | - (imul ty k @ (iconst ty _) x)) |
137 | | - (imul ty x k)) |
138 | | - |
139 | | -(rule (simplify |
140 | | - (bor ty k @ (iconst ty _) x)) |
141 | | - (bor ty x k)) |
142 | | -(rule (simplify |
143 | | - (band ty k @ (iconst ty _) x)) |
144 | | - (band ty x k)) |
145 | | -(rule (simplify |
146 | | - (bxor ty k @ (iconst ty _) x)) |
147 | | - (bxor ty x k)) |
148 | | - |
149 | | -(rule (simplify |
150 | | - (icmp ty cc k @ (iconst _ _) x)) |
151 | | - (icmp ty (intcc_swap_args cc) x k)) |
| 132 | +;; (rule (simplify |
| 133 | +;; (isub ty k @ (iconst ty _) x)) |
| 134 | +;; (ineg ty (isub ty x k))) |
| 135 | +;; (rule (simplify |
| 136 | +;; (imul ty k @ (iconst ty _) x)) |
| 137 | +;; (imul ty x k)) |
| 138 | + |
| 139 | +;; (rule (simplify |
| 140 | +;; (bor ty k @ (iconst ty _) x)) |
| 141 | +;; (bor ty x k)) |
| 142 | +;; (rule (simplify |
| 143 | +;; (band ty k @ (iconst ty _) x)) |
| 144 | +;; (band ty x k)) |
| 145 | +;; (rule (simplify |
| 146 | +;; (bxor ty k @ (iconst ty _) x)) |
| 147 | +;; (bxor ty x k)) |
| 148 | + |
| 149 | +;; (rule (simplify |
| 150 | +;; (icmp ty cc k @ (iconst _ _) x)) |
| 151 | +;; (icmp ty (intcc_swap_args cc) x k)) |
152 | 152 |
|
153 | 153 | ;; Canonicalize via associativity: reassociate to a right-heavy tree |
154 | 154 | ;; for constants. |
155 | 155 | ;; |
156 | 156 | ;; (op (op x k) k) --> (op x (op k k)) |
157 | 157 |
|
158 | | -(rule (simplify |
159 | | - (iadd ty (iadd ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
160 | | - (iadd ty x (iadd ty k1 k2))) |
| 158 | +;; (rule (simplify |
| 159 | +;; (iadd ty (iadd ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
| 160 | +;; (iadd ty x (iadd ty k1 k2))) |
161 | 161 | ;; sub is not directly associative, but we can flip a sub to an add to |
162 | 162 | ;; make it work: |
163 | 163 | ;; - (sub (sub x k1) k2) -> (sub x (add k1 k2)) |
|
186 | 186 | (iconst ty (u64_from_imm64 k2)))) |
187 | 187 | (isub ty (iconst ty (imm64_masked ty (u64_wrapping_add k1 k2))) x)) |
188 | 188 |
|
189 | | -(rule (simplify |
190 | | - (imul ty (imul ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
191 | | - (imul ty x (imul ty k1 k2))) |
192 | | -(rule (simplify |
193 | | - (bor ty (bor ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
194 | | - (bor ty x (bor ty k1 k2))) |
195 | | -(rule (simplify |
196 | | - (band ty (band ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
197 | | - (band ty x (band ty k1 k2))) |
198 | | -(rule (simplify |
199 | | - (bxor ty (bxor ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
200 | | - (bxor ty x (bxor ty k1 k2))) |
| 189 | +;; (rule (simplify |
| 190 | +;; (imul ty (imul ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
| 191 | +;; (imul ty x (imul ty k1 k2))) |
| 192 | +;; (rule (simplify |
| 193 | +;; (bor ty (bor ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
| 194 | +;; (bor ty x (bor ty k1 k2))) |
| 195 | +;; (rule (simplify |
| 196 | +;; (band ty (band ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
| 197 | +;; (band ty x (band ty k1 k2))) |
| 198 | +;; (rule (simplify |
| 199 | +;; (bxor ty (bxor ty x k1 @ (iconst ty _)) k2 @ (iconst ty _))) |
| 200 | +;; (bxor ty x (bxor ty k1 k2))) |
201 | 201 |
|
202 | 202 | (rule (simplify (select ty (iconst_u _ (u64_when_non_zero)) x _)) |
203 | 203 | (subsume x)) |
|
206 | 206 |
|
207 | 207 | ;; Reassociate across `==`/`!=` when we can simplify a constant |
208 | 208 | ;; `x + K1 == K2` --> `x == K2 - K1` |
209 | | -(rule (simplify (eq ty1 (iadd ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
210 | | - (eq ty1 x (isub ty2 k2 k1))) |
211 | | -(rule (simplify (ne ty1 (iadd ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
212 | | - (ne ty1 x (isub ty2 k2 k1))) |
| 209 | +;; (rule (simplify (eq ty1 (iadd ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
| 210 | +;; (eq ty1 x (isub ty2 k2 k1))) |
| 211 | +;; (rule (simplify (ne ty1 (iadd ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
| 212 | +;; (ne ty1 x (isub ty2 k2 k1))) |
213 | 213 | ;; `x - K1 == K2` --> `x == K2 + K1` |
214 | | -(rule (simplify (eq ty1 (isub ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
215 | | - (eq ty1 x (iadd ty2 k2 k1))) |
216 | | -(rule (simplify (ne ty1 (isub ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
217 | | - (ne ty1 x (iadd ty2 k2 k1))) |
| 214 | +;; (rule (simplify (eq ty1 (isub ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
| 215 | +;; (eq ty1 x (iadd ty2 k2 k1))) |
| 216 | +;; (rule (simplify (ne ty1 (isub ty2 x k1@(iconst _ _)) k2@(iconst _ _))) |
| 217 | +;; (ne ty1 x (iadd ty2 k2 k1))) |
218 | 218 | ;; `x + K1 == y + K2` --> `x == y + (K2 - K1)` |
219 | | -(rule (simplify (eq ty1 (iadd ty2 x k1@(iconst _ _)) (iadd ty3 y k2@(iconst _ _)))) |
220 | | - (eq ty1 x (iadd ty2 y (isub ty3 k2 k1)))) |
221 | | -(rule (simplify (ne ty1 (iadd ty2 x k1@(iconst _ _)) (iadd ty3 y k2@(iconst _ _)))) |
222 | | - (ne ty1 x (iadd ty2 y (isub ty3 k2 k1)))) |
| 219 | +;; (rule (simplify (eq ty1 (iadd ty2 x k1@(iconst _ _)) (iadd ty3 y k2@(iconst _ _)))) |
| 220 | +;; (eq ty1 x (iadd ty2 y (isub ty3 k2 k1)))) |
| 221 | +;; (rule (simplify (ne ty1 (iadd ty2 x k1@(iconst _ _)) (iadd ty3 y k2@(iconst _ _)))) |
| 222 | +;; (ne ty1 x (iadd ty2 y (isub ty3 k2 k1)))) |
223 | 223 | ;; An icmp rule normalizes (eq sub sub), so we don't need to handle it here. |
224 | 224 |
|
225 | 225 | ;; Replace subtraction by a "negative" constant with addition. |
|
228 | 228 | ;; TODO: it would be nice to do this for `x + (-1) == x - 1` as well, but |
229 | 229 | ;; that needs work in lowering first to avoid regressing addressing modes. |
230 | 230 |
|
231 | | -(rule (simplify (isub ty x (iconst_s ty k))) |
232 | | - (if-let true (u64_lt (i64_cast_unsigned (i64_wrapping_neg k)) |
233 | | - (i64_cast_unsigned k))) |
234 | | - (iadd ty x (iconst ty (imm64_masked ty (i64_cast_unsigned (i64_wrapping_neg k)))))) |
| 231 | +;; (rule (simplify (isub ty x (iconst_s ty k))) |
| 232 | +;; (if-let true (u64_lt (i64_cast_unsigned (i64_wrapping_neg k)) |
| 233 | +;; (i64_cast_unsigned k))) |
| 234 | +;; (iadd ty x (iconst ty (imm64_masked ty (i64_cast_unsigned (i64_wrapping_neg k)))))) |
235 | 235 |
|
236 | 236 | ;; A splat of a constant can become a direct `vconst` with the appropriate bit |
237 | 237 | ;; pattern. |
|
260 | 260 | ;; Reassociate nested shifts of constants to put constants together for cprop. |
261 | 261 | ;; |
262 | 262 | ;; ((A shift b) shift C) ==> ((A shift C) shift b) |
263 | | -(rule (simplify (ishl ty (ishl ty a@(iconst _ _) b) c@(iconst _ _))) |
264 | | - (ishl ty (ishl ty a c) b)) |
265 | | -(rule (simplify (ushr ty (ushr ty a@(iconst _ _) b) c@(iconst _ _))) |
266 | | - (ushr ty (ushr ty a c) b)) |
267 | | -(rule (simplify (sshr ty (sshr ty a@(iconst _ _) b) c@(iconst _ _))) |
268 | | - (sshr ty (sshr ty a c) b)) |
| 263 | +;; (rule (simplify (ishl ty (ishl ty a@(iconst _ _) b) c@(iconst _ _))) |
| 264 | +;; (ishl ty (ishl ty a c) b)) |
| 265 | +;; (rule (simplify (ushr ty (ushr ty a@(iconst _ _) b) c@(iconst _ _))) |
| 266 | +;; (ushr ty (ushr ty a c) b)) |
| 267 | +;; (rule (simplify (sshr ty (sshr ty a@(iconst _ _) b) c@(iconst _ _))) |
| 268 | +;; (sshr ty (sshr ty a c) b)) |
269 | 269 |
|
270 | 270 | ;; When we operations that are both commutative and associative, reassociate |
271 | 271 | ;; constants together for cprop: |
272 | 272 | ;; |
273 | 273 | ;; ((a op B) op (c op D)) ==> ((a op c) op (B op D)) |
274 | 274 | ;; |
275 | 275 | ;; Where `op` is one of: `iadd`, `imul`, `band`, `bor`, or `bxor`. |
276 | | -(rule (simplify (iadd ty |
277 | | - (iadd ty a b@(iconst _ _)) |
278 | | - (iadd ty c d@(iconst _ _)))) |
279 | | - (iadd ty (iadd ty a c) (iadd ty b d))) |
280 | | -(rule (simplify (imul ty |
281 | | - (imul ty a b@(iconst _ _)) |
282 | | - (imul ty c d@(iconst _ _)))) |
283 | | - (imul ty (imul ty a c) (imul ty b d))) |
284 | | -(rule (simplify (band ty |
285 | | - (band ty a b@(iconst _ _)) |
286 | | - (band ty c d@(iconst _ _)))) |
287 | | - (band ty (band ty a c) (band ty b d))) |
288 | | -(rule (simplify (bor ty |
289 | | - (bor ty a b@(iconst _ _)) |
290 | | - (bor ty c d@(iconst _ _)))) |
291 | | - (bor ty (bor ty a c) (bor ty b d))) |
292 | | -(rule (simplify (bxor ty |
293 | | - (bxor ty a b@(iconst _ _)) |
294 | | - (bxor ty c d@(iconst _ _)))) |
295 | | - (bxor ty (bxor ty a c) (bxor ty b d))) |
| 276 | +;; (rule (simplify (iadd ty |
| 277 | +;; (iadd ty a b@(iconst _ _)) |
| 278 | +;; (iadd ty c d@(iconst _ _)))) |
| 279 | +;; (iadd ty (iadd ty a c) (iadd ty b d))) |
| 280 | +;; (rule (simplify (imul ty |
| 281 | +;; (imul ty a b@(iconst _ _)) |
| 282 | +;; (imul ty c d@(iconst _ _)))) |
| 283 | +;; (imul ty (imul ty a c) (imul ty b d))) |
| 284 | +;; (rule (simplify (band ty |
| 285 | +;; (band ty a b@(iconst _ _)) |
| 286 | +;; (band ty c d@(iconst _ _)))) |
| 287 | +;; (band ty (band ty a c) (band ty b d))) |
| 288 | +;; (rule (simplify (bor ty |
| 289 | +;; (bor ty a b@(iconst _ _)) |
| 290 | +;; (bor ty c d@(iconst _ _)))) |
| 291 | +;; (bor ty (bor ty a c) (bor ty b d))) |
| 292 | +;; (rule (simplify (bxor ty |
| 293 | +;; (bxor ty a b@(iconst _ _)) |
| 294 | +;; (bxor ty c d@(iconst _ _)))) |
| 295 | +;; (bxor ty (bxor ty a c) (bxor ty b d))) |
296 | 296 |
|
297 | 297 |
|
298 | 298 | ;; Constant fold int-to-float conversions. |
|
0 commit comments