On Thu, 19 Nov 2026, Jakub Jelinek wrote:

> Hi!
> 
> The same PR also complains that we use TYPE_SIZE (TREE_TYPE (f))
> instead of DECL_SIZE (f) in comparisons whether the union member
> occupies the whole union or if there are some padding bits
> after it.  If there are full padding bytes after the whole
> TREE_TYPE, then we already handled it correctly, but if DECL_SIZE
> is smaller we haven't.
> In complete_ctor_at_level_p which is also used by C++ FE we don't
> have access to the ctor nor active member, so we can't do anything
> else there, so this patch just repeats the condition from that
> function for the bit-field case in unions, the rest is using
> DECL_SIZE whenever possible for union members.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
> 
> No testcase, it didn't feel right to test for the padding bytes (which is
> UB), but just watched the reported testcase and we now do clear the
> padding bits and even emit more efficient code (just set the whole 32-bit
> union to a constant instead of oring in some bits into previously
> uninitialized stack slot).
> 
> 2026-11-19  Jakub Jelinek  <[email protected]>
> 
>       PR middle-end/128302
>       * expr.cc (count_type_elements): For unions compare
>       DECL_SIZE (f) rather than TYPE_SIZE (TREE_TYPE (f)).
>       (categorize_ctor_elements_1): Similarly.  Also force
>       p_compate->sparse for ZERO_INIT_PADDING_BITS_UNIONS
>       or above if active union member is a bit-field with
>       padding.
> 
> --- a/gcc/expr.cc     2026-11-17 22:41:36.694193133 +0100
> +++ b/gcc/expr.cc     2026-11-18 16:57:03.432490942 +0100
> @@ -7107,8 +7107,7 @@ count_type_elements (const_tree type, bo
>             m = count_type_elements (TREE_TYPE (f), false);
>             /* If the field doesn't span the whole union, add an extra
>                scalar for the rest.  */
> -           if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
> -                                 TYPE_SIZE (type)) != 1)
> +           if (simple_cst_equal (DECL_SIZE (f), TYPE_SIZE (type)) != 1)
>               m++;
>             if (n < m)
>               n = m;
> @@ -7295,23 +7294,45 @@ categorize_ctor_elements_1 (const_tree c
>       }
>      }
>  
> -  if (!p_complete->sparse && !complete_ctor_at_level_p (TREE_TYPE (ctor),
> -                                             num_fields, elt_type))
> +  if (!p_complete->sparse
> +      && !complete_ctor_at_level_p (TREE_TYPE (ctor),
> +                                 num_fields, elt_type))
>      p_complete->sparse = true;
>    else if (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
>          || TREE_CODE (TREE_TYPE (ctor)) == QUAL_UNION_TYPE)
>      {
> +      tree member = NULL_TREE;
> +      if (num_fields)
> +     {
> +       member = CONSTRUCTOR_ELT (ctor, 0)->index;
> +       if (!member)

Can this really happen?  Shouldn't we fixup frontends here...?

Otherwise LGTM.

Thanks,
Richard.

> +         {
> +           for (member = TYPE_FIELDS (TREE_TYPE (ctor)); member;
> +                member = DECL_CHAIN (member))
> +             if (TREE_CODE (member) == FIELD_DECL)
> +               break;
> +         }
> +       if (member
> +           && DECL_BIT_FIELD (member)
> +           && !p_complete->sparse
> +           && flag_zero_init_padding_bits >= ZERO_INIT_PADDING_BITS_UNIONS
> +           && simple_cst_equal (TYPE_SIZE (TREE_TYPE (ctor)),
> +                                DECL_SIZE (member)) != 1)
> +         p_complete->sparse = true;
> +     }
>        if (!p_complete->sparse
>         && CONSTRUCTOR_ZERO_PADDING_BITS (ctor)
>         && (num_fields
>             ? simple_cst_equal (TYPE_SIZE (TREE_TYPE (ctor)),
> -                               TYPE_SIZE (elt_type)) != 1
> +                               member ? DECL_SIZE (member)
> +                               : TYPE_SIZE (elt_type)) != 1
>             : type_has_padding_at_level_p (TREE_TYPE (ctor))))
>       p_complete->sparse = true;
>        else if (!p_complete->sparse && !p_complete->padded_union
>              && (num_fields
>                  ? simple_cst_equal (TYPE_SIZE (TREE_TYPE (ctor)),
> -                                    TYPE_SIZE (elt_type)) != 1
> +                                    member ? DECL_SIZE (member)
> +                                    : TYPE_SIZE (elt_type)) != 1
>                  : type_has_padding_at_level_p (TREE_TYPE (ctor))))
>       p_complete->padded_union = true;
>      }
> 
>       Jakub
> 
> 

-- 
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to