On Tue, Oct 28, 2025 at 07:56:36AM +0100, Jakub Jelinek wrote:
> On Mon, Oct 27, 2025 at 08:36:51PM -0400, Marek Polacek wrote:
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > This came up in Reflection where an assert fails because we have two
> > different trees for same enumerators.  The reason is that in
> > finish_enum_value_list we copy_node when converting the enumerators.
> > It should be more efficient to share trees for identical enumerators.
> > This fix was proposed by Jakub.
> > 
> > gcc/cp/ChangeLog:
> > 
> >     * decl.cc (finish_enum_value_list): Use wide_int_to_tree instead of
> >     copy_node.
> 
> I wonder if this wouldn't be better
>   value = fold_convert (enumtype, value);
> so that under the hood it does fold_convert_const_int_from_int aka
>   tree arg1_type = TREE_TYPE (arg1);
>   unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
>   return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
>                                                TYPE_SIGN (arg1_type)),
>                          !POINTER_TYPE_P (TREE_TYPE (arg1)),
>                          TREE_OVERFLOW (arg1));
> and thus preserves TREE_OVERFLOW.  fold_convert even will handle
> error_mark_node right.
> 
> The important part is that if there wasn't an overflow, it will do
> wide_int_to_tree and thus share the INTEGER_CST value rather than
> making a copy.

Fair enough.  So like this?  It still obviates the need for the
compare_reflections hack.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
This came up in Reflection where an assert fails because we have two
different trees for same enumerators.  The reason is that in
finish_enum_value_list we copy_node when converting the enumerators.
It should be more efficient to share trees for identical enumerators.
This fix was proposed by Jakub.

gcc/cp/ChangeLog:

        * decl.cc (finish_enum_value_list): Use fold_convert instead of
        copy_node.

Co-authored-by: Jakub Jelinek <[email protected]>
---
 gcc/cp/decl.cc | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index e2c20a34e6f..751ba40fc7f 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -18958,13 +18958,9 @@ finish_enum_value_list (tree enumtype)
         value = perform_implicit_conversion (underlying_type,
                                              DECL_INITIAL (decl),
                                              tf_warning_or_error);
-      /* Do not clobber shared ints.  */
-      if (value != error_mark_node)
-       {
-         value = copy_node (value);
+      /* Do not clobber shared ints.  But do share identical enumerators.  */
+      value = fold_convert (enumtype, value);
 
-         TREE_TYPE (value) = enumtype;
-       }
       DECL_INITIAL (decl) = value;
       if (export_p)
        DECL_MODULE_EXPORT_P (decl) = true;

base-commit: e6322a6c9ac6e3ac20d01336c13e097b0d5cce05
-- 
2.51.0

Reply via email to