From 699c0a2ed48410bc403a9b8c8f483c16b40400e4 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 16 Aug 2023 14:32:14 -0700 Subject: [PATCH] uint32 -> int32 casting should not produce SIO --- src/Simplify_Cast.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Simplify_Cast.cpp b/src/Simplify_Cast.cpp index 4eebc9f4323e..4e689212aaa0 100644 --- a/src/Simplify_Cast.cpp +++ b/src/Simplify_Cast.cpp @@ -74,14 +74,21 @@ Expr Simplify::visit(const Cast *op, ExprInfo *bounds) { } else if (op->type.is_int() && const_uint(value, &u) && op->type.bits() < value.type().bits()) { - // uint -> int + // uint -> int narrowing // Recursively call mutate just to set the bounds return mutate(make_const(op->type, safe_numeric_cast(u)), bounds); } else if (op->type.is_int() && const_uint(value, &u) && - op->type.bits() >= value.type().bits()) { - // uint -> int with less than or equal to the number of bits + op->type.bits() == value.type().bits()) { + // uint -> int reinterpret + // Recursively call mutate just to set the bounds + return mutate(make_const(op->type, safe_numeric_cast(u)), bounds); + } else if (op->type.is_int() && + const_uint(value, &u) && + op->type.bits() > value.type().bits()) { + // uint -> int widening if (op->type.can_represent(u) || op->type.bits() < 32) { + // If the type can represent the value or overflow is well-defined. // Recursively call mutate just to set the bounds return mutate(make_const(op->type, safe_numeric_cast(u)), bounds); } else {