https://gcc.gnu.org/g:d47c7fee5148a9ec1226bba9bc85ab9a3366e7e2
commit d47c7fee5148a9ec1226bba9bc85ab9a3366e7e2 Author: Alexandre Oliva <ol...@adacore.com> Date: Thu Jun 27 09:11:54 2024 -0300 Avoid dropping bits from num/den in fixed-point types We used to use an unsigned 128-bit type to hold the numerator and denominator used to represent the delta of a fixed-point type in debug information, but there are cases in which that was not enough, and more significant bits silently overflowed and got omitted from debug information. Introduce a mode in which UI_to_gnu selects a wide-enough unsigned type, and use that to convert numerator and denominator. for gcc/ada/ChangeLog * gcc-interface/cuintp.cc (UI_To_gnu): Add mode that selects a wide enough unsigned type. * gcc-interface/decl.cc (gnat_to_gnu_entity): Use it for numerator and denominator of fixed-point types. Diff: --- gcc/ada/gcc-interface/cuintp.cc | 47 ++++++++++++++++++++++++++--------------- gcc/ada/gcc-interface/decl.cc | 15 +++++++++---- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/gcc/ada/gcc-interface/cuintp.cc b/gcc/ada/gcc-interface/cuintp.cc index cdf6c019750..ad345096282 100644 --- a/gcc/ada/gcc-interface/cuintp.cc +++ b/gcc/ada/gcc-interface/cuintp.cc @@ -35,6 +35,7 @@ #include "tree.h" #include "inchash.h" #include "fold-const.h" +#include "stor-layout.h" #include "ada.h" #include "types.h" @@ -67,7 +68,8 @@ build_cst_from_int (tree type, HOST_WIDE_INT low) /* Similar to UI_To_Int, but return a GCC INTEGER_CST or REAL_CST node, depending on whether TYPE is an integral or real type. Overflow is tested by the constant-folding used to build the node. TYPE is the GCC type of - the resulting node. */ + the resulting node. If TYPE is NULL, an unsigned integer type wide enough + to hold the entire constant is selected. */ tree UI_To_gnu (Uint Input, tree type) @@ -77,8 +79,10 @@ UI_To_gnu (Uint Input, tree type) any such possible value for intermediate computations and then rely on a conversion back to TYPE to perform the bias adjustment when need be. */ tree comp_type - = TREE_CODE (type) == INTEGER_TYPE && TYPE_BIASED_REPRESENTATION_P (type) - ? get_base_type (type) : type; + = (!type ? gnat_type_for_size (32, 0) + : (TREE_CODE (type) == INTEGER_TYPE + && TYPE_BIASED_REPRESENTATION_P (type)) + ? get_base_type (type) : type); tree gnu_ret; if (Input <= Uint_Direct_Last) @@ -88,6 +92,7 @@ UI_To_gnu (Uint Input, tree type) Int Idx = (*Uints_Ptr)[Input - Uint_Table_Start].Loc; Pos Length = (*Uints_Ptr)[Input - Uint_Table_Start].Length; Int First = (*Udigits_Ptr)[Idx]; + tree_code code = First < 0 ? MINUS_EXPR : PLUS_EXPR; tree gnu_base; gcc_assert (Length > 0); @@ -99,26 +104,34 @@ UI_To_gnu (Uint Input, tree type) convert the final result back to the incoming type later on. */ if (!SCALAR_FLOAT_TYPE_P (comp_type) && TYPE_PRECISION (comp_type) < 32) comp_type = gnat_type_for_size (32, 0); + else if (!type && TYPE_UNSIGNED (comp_type)) + /* Choose a signed type, so that we can detect overflow. */ + comp_type = make_signed_type (TYPE_PRECISION (comp_type)); gnu_base = build_cst_from_int (comp_type, Base); gnu_ret = build_cst_from_int (comp_type, First); - if (First < 0) - for (Idx++, Length--; Length; Idx++, Length--) - gnu_ret = fold_build2 (MINUS_EXPR, comp_type, - fold_build2 (MULT_EXPR, comp_type, - gnu_ret, gnu_base), - build_cst_from_int (comp_type, - (*Udigits_Ptr)[Idx])); - else - for (Idx++, Length--; Length; Idx++, Length--) - gnu_ret = fold_build2 (PLUS_EXPR, comp_type, - fold_build2 (MULT_EXPR, comp_type, - gnu_ret, gnu_base), - build_cst_from_int (comp_type, - (*Udigits_Ptr)[Idx])); + for (Idx++, Length--; Length; Idx++, Length--) + for (;;) + { + tree next_ret = fold_build2 (code, comp_type, + fold_build2 (MULT_EXPR, comp_type, + gnu_ret, gnu_base), + build_cst_from_int + (comp_type, (*Udigits_Ptr)[Idx])); + if (!TREE_OVERFLOW (next_ret) || type) + { + gnu_ret = next_ret; + break; + } + comp_type = make_signed_type (TYPE_PRECISION (comp_type) * 2); + gnu_base = convert (comp_type, gnu_base); + gnu_ret = convert (comp_type, gnu_ret); + } } + if (!type) + type = make_unsigned_type (TYPE_PRECISION (comp_type)); gnu_ret = convert (type, gnu_ret); /* We don't need any NOP_EXPR or NON_LVALUE_EXPR on GNU_RET. */ diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc index 5b3a3b4961b..80dd6034a01 100644 --- a/gcc/ada/gcc-interface/decl.cc +++ b/gcc/ada/gcc-interface/decl.cc @@ -1767,14 +1767,21 @@ gnat_to_gnu_entity (Entity_Id gnat_entity, tree gnu_expr, bool definition) /* Use the arbitrary scale factor description. Note that we support a Small_Value whose magnitude is larger than 64-bit even on 32-bit - platforms, so we unconditionally use a (dummy) 128-bit type. */ + platforms. UI_To_gnu chooses a wide-enough integral type. */ else { const Uint gnat_num = Norm_Num (gnat_small_value); const Uint gnat_den = Norm_Den (gnat_small_value); - tree gnu_small_type = make_unsigned_type (128); - tree gnu_num = UI_To_gnu (gnat_num, gnu_small_type); - tree gnu_den = UI_To_gnu (gnat_den, gnu_small_type); + tree gnu_num = UI_To_gnu (gnat_num, NULL_TREE); + tree gnu_den = UI_To_gnu (gnat_den, NULL_TREE); + tree gnu_num_type = TREE_TYPE (gnu_num); + tree gnu_den_type = TREE_TYPE (gnu_den); + tree gnu_small_type = (TYPE_PRECISION (gnu_num_type) + >= TYPE_PRECISION (gnu_den_type) + ? gnu_num_type : gnu_den_type); + + gnu_num = convert (gnu_small_type, gnu_num); + gnu_den = convert (gnu_small_type, gnu_den); scale_factor = build2 (RDIV_EXPR, gnu_small_type, gnu_num, gnu_den);