1;; Chained `uextend` and `sextend`.
2(rule (simplify (uextend ty (uextend _intermediate_ty x)))
3      (uextend ty x))
4(rule (simplify (sextend ty (sextend _intermediate_ty x)))
5      (sextend ty x))
6
7;; Masking out any of the top bits of the result of `uextend` is a no-op. (This
8;; is like a cheap version of known-bits analysis.)
9(rule (simplify (band wide x @ (uextend _ (value_type narrow)) (iconst _ (u64_from_imm64 mask))))
10      ; Check that `narrow_mask` has a subset of the bits that `mask` does.
11      (if-let $true (let ((narrow_mask u64 (ty_mask narrow))) (u64_eq narrow_mask (u64_and mask narrow_mask))))
12      x)
13
14;; Masking out the sign-extended bits of an `sextend` turns it into a `uextend`.
15(rule (simplify (band wide (sextend _ x @ (value_type narrow)) (iconst _ (u64_from_imm64 mask))))
16      (if-let $true (u64_eq mask (ty_mask narrow)))
17      (uextend wide x))
18
19;; 32-bit integers zero-extended to 64-bit integers are never negative
20(rule (simplify
21       (slt ty
22             (uextend $I64 x @ (value_type $I32))
23             (iconst _ (u64_from_imm64 0))))
24      (iconst ty (imm64 0)))
25(rule (simplify
26       (sge ty
27             (uextend $I64 x @ (value_type $I32))
28             (iconst _ (u64_from_imm64 0))))
29      (iconst ty (imm64 1)))
30
31;; A reduction-of-an-extend back to the same original type is the same as not
32;; actually doing the extend in the first place.
33(rule (simplify (ireduce ty (sextend _ x @ (value_type ty)))) x)
34(rule (simplify (ireduce ty (uextend _ x @ (value_type ty)))) x)
35