|
47 | 47 | (subsume inner)) |
48 | 48 |
|
49 | 49 | ;; x-x == 0. |
50 | | -(rule (simplify (isub ty x x)) (subsume (iconst_u ty 0))) |
| 50 | +(rule (simplify (isub (ty_int ty) x x)) (subsume (iconst_u ty 0))) |
51 | 51 |
|
52 | 52 | ;; x*1 == x. |
53 | 53 | (rule (simplify (imul ty |
|
222 | 222 | (rule (simplify (fcvt_from_sint ty (sextend _ val))) |
223 | 223 | (fcvt_from_sint ty val)) |
224 | 224 |
|
225 | | - |
226 | | -;; or(x, C) + (-C) --> and(x, ~C) |
227 | | -(rule |
228 | | - (simplify (iadd ty |
229 | | - (bor ty x (iconst ty n)) |
230 | | - (iconst ty m))) |
231 | | - (if-let m (imm64_neg ty n)) |
232 | | - (band ty x (iconst ty (imm64_not ty n)))) |
233 | | - |
234 | | -;; (x + y) - (x | y) --> x & y |
235 | | -(rule (simplify (isub ty (iadd ty x y) (bor ty x y))) (band ty x y)) |
236 | | - |
237 | | -;; x * (1 << y) == x << y |
238 | | -(rule (simplify (imul ty x (ishl ty (iconst_s ty 1) y))) (ishl ty x y)) |
239 | | - |
240 | | -;; (x - y) + x --> x |
241 | | -(rule (simplify (iadd ty (isub ty x y) y)) x) |
242 | | -(rule (simplify (iadd ty y (isub ty x y))) x) |
243 | | - |
244 | | -;; (x + y) - y --> x |
245 | | -(rule (simplify (isub ty (iadd ty x y) x)) y) |
246 | | -(rule (simplify (isub ty (iadd ty x y) y)) x) |
247 | | - |
248 | | -;; (x - y) - x => -y |
249 | | -(rule (simplify (isub ty (isub ty x y) x))(ineg ty y)) |
250 | | - |
251 | | -;; (x * C) (==/!=) D --> x (==/!=) (D / C) when C is odd and divides D |
252 | | -(rule |
253 | | - (simplify (ne ty (iconst_u ty1 x) (imul ty1 y (iconst_u ty1 z)))) |
254 | | - (if-let 0 (u64_checked_rem x z)) |
255 | | - (if-let 1 (u64_rem z 2)) |
256 | | - (ne ty y (iconst ty1 (imm64 (u64_div x z))))) |
257 | | -(rule |
258 | | - (simplify (ne ty (iconst_u ty1 x) (imul ty1 (iconst_u ty1 y) z))) |
259 | | - (if-let 0 (u64_checked_rem x y)) |
260 | | - (if-let 1 (u64_rem y 2)) |
261 | | - (ne ty z (iconst ty1 (imm64 (u64_div x y))))) |
262 | | -(rule |
263 | | - (simplify (ne ty (imul ty1 x (iconst_u ty1 y)) (iconst_u ty1 z))) |
264 | | - (if-let 0 (u64_checked_rem z y)) |
265 | | - (if-let 1 (u64_rem y 2)) |
266 | | - (ne ty x (iconst ty1 (imm64 (u64_div z y))))) |
267 | | -(rule |
268 | | - (simplify (ne ty (imul ty1 (iconst_u ty1 x) y) (iconst_u ty1 z))) |
269 | | - (if-let 0 (u64_checked_rem z x)) |
270 | | - (if-let 1 (u64_rem x 2)) |
271 | | - (ne ty y (iconst ty1 (imm64 (u64_div z x))))) |
272 | | - |
273 | | - |
274 | | -(rule |
275 | | - (simplify (eq ty (iconst_u ty1 x) (imul ty1 y (iconst_u ty1 z)))) |
276 | | - (if-let 0 (u64_checked_rem x z)) |
277 | | - (if-let 1 (u64_rem z 2)) |
278 | | - (eq ty y (iconst ty1 (imm64 (u64_div x z))))) |
279 | | -(rule |
280 | | - (simplify (eq ty (iconst_u ty1 x) (imul ty1 (iconst_u ty1 y) z))) |
281 | | - (if-let 0 (u64_checked_rem x y)) |
282 | | - (if-let 1 (u64_rem y 2)) |
283 | | - (eq ty z (iconst ty1 (imm64 (u64_div x y))))) |
284 | | -(rule |
285 | | - (simplify (eq ty (imul ty1 x (iconst_u ty1 y)) (iconst_u ty1 z))) |
286 | | - (if-let 0 (u64_checked_rem z y)) |
287 | | - (if-let 1 (u64_rem y 2)) |
288 | | - (eq ty x (iconst ty1 (imm64 (u64_div z y))))) |
289 | | -(rule |
290 | | - (simplify (eq ty (imul ty1 (iconst_u ty1 x) y) (iconst_u ty1 z))) |
291 | | - (if-let 0 (u64_checked_rem z x)) |
292 | | - (if-let 1 (u64_rem x 2)) |
293 | | - (eq ty y (iconst ty1 (imm64 (u64_div z x))))) |
294 | | - |
295 | | -;; (x + y) + (-y) ==> x |
296 | | -;; and equivalent operand-order variants. |
297 | | -(rule (simplify (iadd ty (iadd ty x y) (ineg ty y))) x) |
298 | | -(rule (simplify (iadd ty (ineg ty y) (iadd ty x y))) x) |
299 | | -(rule (simplify (iadd ty (iadd ty y x) (ineg ty y))) x) |
300 | | -(rule (simplify (iadd ty (ineg ty y) (iadd ty y x))) x) |
301 | | - |
302 | | -;; (x | y) - (x & y) ==> (x ^ y) |
303 | | -(rule (simplify (isub ty (bor ty x y) (band ty x y))) (bxor ty x y)) |
304 | | -(rule (simplify (isub ty (bor ty x y) (band ty y x))) (bxor ty x y)) |
305 | | -(rule (simplify (isub ty (bor ty y x) (band ty x y))) (bxor ty x y)) |
306 | | -(rule (simplify (isub ty (bor ty y x) (band ty y x))) (bxor ty x y)) |
307 | | - |
308 | | -;; (x + y) - (x & y) ==> (x | y) |
309 | | -(rule (simplify (isub ty (iadd ty x y) (band ty x y))) (bor ty x y)) |
310 | | -(rule (simplify (isub ty (iadd ty x y) (band ty y x))) (bor ty x y)) |
311 | | -(rule (simplify (isub ty (iadd ty y x) (band ty x y))) (bor ty x y)) |
312 | | -(rule (simplify (isub ty (iadd ty y x) (band ty y x))) (bor ty x y)) |
313 | | - |
314 | | -;; (x | y) - (x ^ y) ==> (x & y) |
315 | | -(rule (simplify (isub ty (bor ty x y) (bxor ty x y))) (band ty x y)) |
316 | | -(rule (simplify (isub ty (bor ty x y) (bxor ty y x))) (band ty x y)) |
317 | | -(rule (simplify (isub ty (bor ty y x) (bxor ty x y))) (band ty x y)) |
318 | | -(rule (simplify (isub ty (bor ty y x) (bxor ty y x))) (band ty x y)) |
319 | | - |
320 | | -;; (~x) + x == -1 |
321 | | -(rule (simplify (iadd ty (bnot ty x) x)) (iconst_s ty -1)) |
322 | | -(rule (simplify (iadd ty x (bnot ty x))) (iconst_s ty -1)) |
323 | | - |
324 | | -;; (x - y) == x --> y == 0 |
325 | | -(rule (simplify (eq cty (isub ty x y) x)) (eq cty y (iconst_u ty 0))) |
326 | | -(rule (simplify (eq cty x (isub ty x y))) (eq cty y (iconst_u ty 0))) |
327 | | - |
328 | | -;; (x + y) == y --> x == 0 |
329 | | -(rule (simplify (eq cty (iadd ty x y) y)) (eq cty x (iconst_u ty 0))) |
330 | | -(rule (simplify (eq cty (iadd ty y x) y)) (eq cty x (iconst_u ty 0))) |
331 | | -(rule (simplify (eq cty y (iadd ty x y))) (eq cty x (iconst_u ty 0))) |
332 | | -(rule (simplify (eq cty y (iadd ty y x))) (eq cty x (iconst_u ty 0))) |
333 | | - |
334 | | -;; -x == -y --> x == y |
335 | | -(rule (simplify (eq ty (ineg ty x) (ineg ty y))) (eq ty x y)) |
336 | | - |
337 | | -;; -((-y) * x) --> x * y |
338 | | -(rule (simplify (ineg ty (imul ty (ineg ty y) x))) (imul ty x y)) |
339 | | -(rule (simplify (ineg ty (imul ty x (ineg ty y)))) (imul ty x y)) |
340 | | - |
341 | | -;; Helper to create a "true" value for a comparison. For scalar integers this is |
342 | | -;; a value of 1 but for vectors this is -1 since each lane is filled with all |
343 | | -;; 1s. We use `(iconst_u ty 0)` for falses for both integers and vector. This is |
344 | | -;; because of the Cranelift semantics: |
345 | | -;; When comparing scalars, the result is 1 if the condition holds, or 0 otherwise. |
346 | | -;; When comparing vectors, the result is -1 (all-ones) if the condition holds, or 0 otherwise. |
347 | | -(decl cmp_true (Type) Value) |
348 | | -(rule 0 (cmp_true (ty_int ty)) (iconst_u ty 1)) |
349 | | -(rule 1 (cmp_true (ty_vec128 ty)) (iconst_s ty -1)) |
350 | | - |
351 | | -;; max(x, y) >= x |
352 | | -(rule (simplify (sge ty (smax ty x y) x)) (cmp_true ty)) |
353 | | -(rule (simplify (sge ty (smax ty y x) x)) (cmp_true ty)) |
354 | | -(rule (simplify (sle ty x (smax ty x y))) (cmp_true ty)) |
355 | | -(rule (simplify (sle ty x (smax ty y x))) (cmp_true ty)) |
356 | | -(rule (simplify (uge ty (umax ty x y) x)) (cmp_true ty)) |
357 | | -(rule (simplify (uge ty (umax ty y x) x)) (cmp_true ty)) |
358 | | -(rule (simplify (ule ty x (umax ty x y))) (cmp_true ty)) |
359 | | -(rule (simplify (ule ty x (umax ty y x))) (cmp_true ty)) |
360 | | - |
361 | | -;; x >= min(x, y) |
362 | | -(rule (simplify (sge ty x (smin ty x y))) (cmp_true ty)) |
363 | | -(rule (simplify (sge ty x (smin ty y x))) (cmp_true ty)) |
364 | | -(rule (simplify (sle ty (smin ty x y) x)) (cmp_true ty)) |
365 | | -(rule (simplify (sle ty (smin ty y x) x)) (cmp_true ty)) |
366 | | -(rule (simplify (uge ty x (umin ty x y))) (cmp_true ty)) |
367 | | -(rule (simplify (uge ty x (umin ty y x))) (cmp_true ty)) |
368 | | -(rule (simplify (ule ty (umin ty x y) x)) (cmp_true ty)) |
369 | | -(rule (simplify (ule ty (umin ty y x) x)) (cmp_true ty)) |
370 | | - |
371 | | -;; min/max(x,x) --> x |
372 | | -(rule (simplify (umin ty x x)) x) |
373 | | -(rule (simplify (smin ty x x)) x) |
374 | | -(rule (simplify (umax ty x x)) x) |
375 | | -(rule (simplify (smax ty x x)) x) |
376 | | - |
377 | | -;; max(max(x, y), x) --> max(x, y) |
378 | | -(rule (simplify (smax ty (smax ty x y) x)) (smax ty x y)) |
379 | | -(rule (simplify (smax ty (smax ty y x) x)) (smax ty x y)) |
380 | | -(rule (simplify (smax ty x (smax ty x y))) (smax ty x y)) |
381 | | -(rule (simplify (smax ty x (smax ty y x))) (smax ty x y)) |
382 | | -(rule (simplify (umax ty (umax ty x y) x)) (umax ty x y)) |
383 | | -(rule (simplify (umax ty (umax ty y x) x)) (umax ty x y)) |
384 | | -(rule (simplify (umax ty x (umax ty x y))) (umax ty x y)) |
385 | | -(rule (simplify (umax ty x (umax ty y x))) (umax ty x y)) |
386 | | - |
387 | | -;; min(min(x, y), x) --> min(x, y) |
388 | | -(rule (simplify (smin ty (smin ty x y) x)) (smin ty x y)) |
389 | | -(rule (simplify (smin ty (smin ty y x) x)) (smin ty x y)) |
390 | | -(rule (simplify (smin ty x (smin ty x y))) (smin ty x y)) |
391 | | -(rule (simplify (smin ty x (smin ty y x))) (smin ty x y)) |
392 | | -(rule (simplify (umin ty (umin ty x y) x)) (umin ty x y)) |
393 | | -(rule (simplify (umin ty (umin ty y x) x)) (umin ty x y)) |
394 | | -(rule (simplify (umin ty x (umin ty x y))) (umin ty x y)) |
395 | | -(rule (simplify (umin ty x (umin ty y x))) (umin ty x y)) |
396 | | - |
397 | | -;; min(max(x, y), y) --> y ; max(min(x, y), y) --> y |
398 | | -(rule (simplify (smin ty (smax ty x y) y)) y) |
399 | | -(rule (simplify (smin ty (smax ty y x) y)) y) |
400 | | -(rule (simplify (smin ty y (smax ty x y))) y) |
401 | | -(rule (simplify (smin ty y (smax ty y x))) y) |
402 | | -(rule (simplify (umin ty (umax ty x y) y)) y) |
403 | | -(rule (simplify (umin ty (umax ty y x) y)) y) |
404 | | -(rule (simplify (umin ty y (umax ty x y))) y) |
405 | | -(rule (simplify (umin ty y (umax ty y x))) y) |
406 | | -(rule (simplify (smax ty (smin ty x y) y)) y) |
407 | | -(rule (simplify (smax ty (smin ty y x) y)) y) |
408 | | -(rule (simplify (smax ty y (smin ty x y))) y) |
409 | | -(rule (simplify (smax ty y (smin ty y x))) y) |
410 | | -(rule (simplify (umax ty (umin ty x y) y)) y) |
411 | | -(rule (simplify (umax ty (umin ty y x) y)) y) |
412 | | -(rule (simplify (umax ty y (umin ty x y))) y) |
413 | | -(rule (simplify (umax ty y (umin ty y x))) y) |
414 | | - |
415 | | -;; min(min(x, y), max(x, y)) --> min(x,y) |
416 | | -(rule (simplify (smin ty (smax ty x y) (smin ty x y))) (smin ty x y)) |
417 | | -(rule (simplify (smin ty (smax ty x y) (smin ty y x))) (smin ty x y)) |
418 | | -(rule (simplify (smin ty (smax ty x y) (umin ty x y))) (umin ty x y)) |
419 | | -(rule (simplify (smin ty (smax ty x y) (umin ty y x))) (umin ty x y)) |
420 | | - |
421 | | -(rule (simplify (smin ty (smin ty x y) (smax ty x y))) (smin ty x y)) |
422 | | -(rule (simplify (smin ty (smin ty x y) (smax ty y x))) (smin ty x y)) |
423 | | -(rule (simplify (smin ty (smin ty x y) (umax ty x y))) (smin ty x y)) |
424 | | -(rule (simplify (smin ty (smin ty x y) (umax ty y x))) (smin ty x y)) |
425 | | - |
426 | | -(rule (simplify (smin ty (umax ty x y) (smin ty x y))) (smin ty x y)) |
427 | | -(rule (simplify (smin ty (umax ty x y) (smin ty y x))) (smin ty x y)) |
428 | | -(rule (simplify (smin ty (umax ty x y) (umin ty x y))) (smin ty x y)) |
429 | | -(rule (simplify (smin ty (umax ty x y) (umin ty y x))) (smin ty x y)) |
430 | | - |
431 | | -(rule (simplify (smin ty (umin ty x y) (smax ty x y))) (umin ty x y)) |
432 | | -(rule (simplify (smin ty (umin ty x y) (smax ty y x))) (umin ty x y)) |
433 | | -(rule (simplify (smin ty (umin ty x y) (umax ty x y))) (smin ty x y)) |
434 | | -(rule (simplify (smin ty (umin ty x y) (umax ty y x))) (smin ty x y)) |
435 | | - |
436 | | -(rule (simplify (umin ty (smax ty x y) (smin ty x y))) (umin ty x y)) |
437 | | -(rule (simplify (umin ty (smax ty x y) (smin ty y x))) (umin ty x y)) |
438 | | -(rule (simplify (umin ty (smax ty x y) (umin ty x y))) (umin ty x y)) |
439 | | -(rule (simplify (umin ty (smax ty x y) (umin ty y x))) (umin ty x y)) |
440 | | - |
441 | | -(rule (simplify (umin ty (smin ty x y) (smax ty x y))) (umin ty x y)) |
442 | | -(rule (simplify (umin ty (smin ty x y) (smax ty y x))) (umin ty x y)) |
443 | | -(rule (simplify (umin ty (smin ty x y) (umax ty x y))) (smin ty x y)) |
444 | | -(rule (simplify (umin ty (smin ty x y) (umax ty y x))) (smin ty x y)) |
445 | | - |
446 | | -(rule (simplify (umin ty (umax ty x y) (smin ty x y))) (smin ty x y)) |
447 | | -(rule (simplify (umin ty (umax ty x y) (smin ty y x))) (smin ty x y)) |
448 | | -(rule (simplify (umin ty (umax ty x y) (umin ty x y))) (umin ty x y)) |
449 | | -(rule (simplify (umin ty (umax ty x y) (umin ty y x))) (umin ty x y)) |
450 | | - |
451 | | -(rule (simplify (umin ty (umin ty x y) (smax ty x y))) (umin ty x y)) |
452 | | -(rule (simplify (umin ty (umin ty x y) (smax ty y x))) (umin ty x y)) |
453 | | -(rule (simplify (umin ty (umin ty x y) (umax ty x y))) (umin ty x y)) |
454 | | -(rule (simplify (umin ty (umin ty x y) (umax ty y x))) (umin ty x y)) |
455 | | - |
456 | | -;; max(min(x, y), max(x, y)) --> max(x, y) |
457 | | -(rule (simplify (smax ty (smax ty x y) (smin ty x y))) (smax ty x y)) |
458 | | -(rule (simplify (smax ty (smax ty x y) (smin ty y x))) (smax ty x y)) |
459 | | -(rule (simplify (smax ty (smax ty x y) (umin ty x y))) (smax ty x y)) |
460 | | -(rule (simplify (smax ty (smax ty x y) (umin ty y x))) (smax ty x y)) |
461 | | - |
462 | | -(rule (simplify (smax ty (smin ty x y) (smax ty x y))) (smax ty x y)) |
463 | | -(rule (simplify (smax ty (smin ty x y) (smax ty y x))) (smax ty x y)) |
464 | | -(rule (simplify (smax ty (smin ty x y) (umax ty x y))) (umax ty x y)) |
465 | | -(rule (simplify (smax ty (smin ty x y) (umax ty y x))) (umax ty x y)) |
466 | | - |
467 | | -(rule (simplify (smax ty (umax ty x y) (smin ty x y))) (umax ty x y)) |
468 | | -(rule (simplify (smax ty (umax ty x y) (smin ty y x))) (umax ty x y)) |
469 | | -(rule (simplify (smax ty (umax ty x y) (umin ty x y))) (smax ty x y)) |
470 | | -(rule (simplify (smax ty (umax ty x y) (umin ty y x))) (smax ty x y)) |
471 | | - |
472 | | -(rule (simplify (smax ty (umin ty x y) (smax ty x y))) (smax ty x y)) |
473 | | -(rule (simplify (smax ty (umin ty x y) (smax ty y x))) (smax ty x y)) |
474 | | -(rule (simplify (smax ty (umin ty x y) (umax ty x y))) (smax ty x y)) |
475 | | -(rule (simplify (smax ty (umin ty x y) (umax ty y x))) (smax ty x y)) |
476 | | - |
477 | | -(rule (simplify (umax ty (smax ty x y) (smin ty x y))) (umax ty x y)) |
478 | | -(rule (simplify (umax ty (smax ty x y) (smin ty y x))) (umax ty x y)) |
479 | | -(rule (simplify (umax ty (smax ty x y) (umin ty x y))) (smax ty x y)) |
480 | | -(rule (simplify (umax ty (smax ty x y) (umin ty y x))) (smax ty x y)) |
481 | | - |
482 | | -(rule (simplify (umax ty (smin ty x y) (smax ty x y))) (umax ty x y)) |
483 | | -(rule (simplify (umax ty (smin ty x y) (smax ty y x))) (umax ty x y)) |
484 | | -(rule (simplify (umax ty (smin ty x y) (umax ty x y))) (umax ty x y)) |
485 | | -(rule (simplify (umax ty (smin ty x y) (umax ty y x))) (umax ty x y)) |
486 | | - |
487 | | -(rule (simplify (umax ty (umax ty x y) (smin ty x y))) (umax ty x y)) |
488 | | -(rule (simplify (umax ty (umax ty x y) (smin ty y x))) (umax ty x y)) |
489 | | -(rule (simplify (umax ty (umax ty x y) (umin ty x y))) (umax ty x y)) |
490 | | -(rule (simplify (umax ty (umax ty x y) (umin ty y x))) (umax ty x y)) |
491 | | - |
492 | | -(rule (simplify (umax ty (umin ty x y) (smax ty x y))) (smax ty x y)) |
493 | | -(rule (simplify (umax ty (umin ty x y) (smax ty y x))) (smax ty x y)) |
494 | | -(rule (simplify (umax ty (umin ty x y) (umax ty x y))) (umax ty x y)) |
495 | | -(rule (simplify (umax ty (umin ty x y) (umax ty y x))) (umax ty x y)) |
496 | | - |
497 | | -;; x > max(x, y) --> 0 |
498 | | -(rule (simplify (sgt ty x (smax ty x y))) (iconst_u ty 0)) |
499 | | -(rule (simplify (sgt ty x (smax ty y x))) (iconst_u ty 0)) |
500 | | -(rule (simplify (slt ty (smax ty x y) x)) (iconst_u ty 0)) |
501 | | -(rule (simplify (slt ty (smax ty y x) x)) (iconst_u ty 0)) |
502 | | -(rule (simplify (ugt ty x (umax ty x y))) (iconst_u ty 0)) |
503 | | -(rule (simplify (ugt ty x (umax ty y x))) (iconst_u ty 0)) |
504 | | -(rule (simplify (ult ty (umax ty x y) x)) (iconst_u ty 0)) |
505 | | -(rule (simplify (ult ty (umax ty y x) x)) (iconst_u ty 0)) |
506 | | - |
507 | | -;; (-X) * C = X * (-C) |
508 | | -(rule (simplify (imul (fits_in_64 ty) (ineg ty x) (iconst ty y))) (imul ty x (iconst ty (imm64_neg ty y)))) |
0 commit comments