1;; Rewrites of side-effectful instructions.
2;;
3;; These are rules for the `simplify_skeleton` term, rather than `simplify`, and
4;; return a `SkeletonInstSimplification` variant rather than a rewritten-value.
5
6;; Conditional traps that will never trap.
7(rule (simplify_skeleton (trapz (iconst_u _ (u64_when_non_zero)) _))
8      (remove_inst))
9(rule (simplify_skeleton (trapnz (iconst_u _ 0) _))
10      (remove_inst))
11
12;; Constant propagation through `uadd_overflow_trap`.
13(rule (simplify_skeleton (uadd_overflow_trap (iconst_u ty a) (iconst_u ty b) _))
14      (if-let c (checked_add_with_type ty a b))
15      (iconst_u ty c))
16
17;; Summing two zero-extended values cannot overflow.
18(rule (simplify_skeleton (uadd_overflow_trap a @ (uextend ty _) b @ (uextend ty _) _))
19      (iadd ty a b))
20
21;; TODO: We can't simplify into unconditional traps yet. See the comment in
22;; `simplify_skeleton_inst` for more details.
23;;
24;; (rule (simplify_skeleton (trapz (iconst_u _ 0) code))
25;;       (trap code))
26;; (rule (simplify_skeleton (trapnz (iconst_u _ (u64_when_non_zero)) code))
27;;       (trap code))
28;;
29;; (rule (simplify_skeleton (uadd_overflow_trap (iconst_u ty a) (iconst_u ty b) code))
30;;       (if-let true (add_overflows_with_type ty a b))
31;;       (trap code))
32;;
33;; (rule (simplify_skeleton (udiv _ (iconst_u ty 0)))
34;;       (replace_with_val (trap (trap_code_division_by_zero))
35;;                         (iconst_u ty 0)))
36;; (rule (simplify_skeleton (sdiv _ (iconst_s ty 0)))
37;;       (replace_with_val (trap (trap_code_division_by_zero))
38;;                         (iconst_s ty 0)))
39
40(rule
41  (simplify_skeleton (udiv y
42                       (select ty
43                         x
44                         (iconst ty (imm64_power_of_two n))
45                         (iconst ty (imm64_power_of_two m)))))
46  (ushr ty y (select ty x (iconst ty (imm64 n)) (iconst ty (imm64 m)))))
47