;; Chained `uextend` and `sextend`. (rule (simplify (uextend ty (uextend _intermediate_ty x))) (uextend ty x)) (rule (simplify (sextend ty (sextend _intermediate_ty x))) (sextend ty x)) ;; Once something has be `uextend`ed, further `sextend`ing is the same as `uextend` (rule (simplify (sextend ty (uextend _intermediate_ty x))) (uextend ty x)) ;; `icmp` is zero or one, so sign-extending is the same as zero-extending (rule (simplify (sextend ty x@(icmp _ _ _ _))) (uextend ty x)) ;; Masking out any of the top bits of the result of `uextend` is a no-op. (This ;; is like a cheap version of known-bits analysis.) (rule (simplify (band wide x @ (uextend _ (value_type narrow)) (iconst _ (u64_from_imm64 mask)))) ; Check that `narrow_mask` has a subset of the bits that `mask` does. (if-let $true (let ((narrow_mask u64 (ty_mask narrow))) (u64_eq narrow_mask (u64_and mask narrow_mask)))) x) ;; Masking out the sign-extended bits of an `sextend` turns it into a `uextend`. (rule (simplify (band wide (sextend _ x @ (value_type narrow)) (iconst _ (u64_from_imm64 mask)))) (if-let $true (u64_eq mask (ty_mask narrow))) (uextend wide x)) ;; 32-bit integers zero-extended to 64-bit integers are never negative (rule (simplify (slt ty (uextend $I64 x @ (value_type $I32)) (iconst_u _ 0))) (iconst_u ty 0)) (rule (simplify (sge ty (uextend $I64 x @ (value_type $I32)) (iconst_u _ 0))) (iconst_u ty 1)) ;; Sign-extending can't change whether a number is zero nor how it signed-compares to zero (rule (simplify (eq _ (sextend _ x@(value_type ty)) (iconst_s _ 0))) (eq ty x (iconst_s ty 0))) (rule (simplify (ne _ (sextend _ x@(value_type ty)) (iconst_s _ 0))) (ne ty x (iconst_s ty 0))) (rule (simplify (icmp _ cc (sextend _ x@(value_type ty)) (iconst_s _ 0))) (if (signed_cond_code cc)) (icmp ty cc x (iconst_s ty 0))) ;; A reduction-of-an-extend back to the same original type is the same as not ;; actually doing the extend in the first place. (rule (simplify (ireduce ty (sextend _ x @ (value_type ty)))) x) (rule (simplify (ireduce ty (uextend _ x @ (value_type ty)))) x)