GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
expr Performance Win

Fix union bit-field handling in constructor element categorization

GCC now correctly compares union member sizes with DECL_SIZE, improving bit-field and padding handling for unions and constructors.

This commit corrects how GCC handles bit-fields within unions during constructor element categorization. It replaces the use of TYPE_SIZE with DECL_SIZE for more accurate size comparisons, ensuring that padding bits are correctly accounted for. This change also leads to more efficient code generation by properly initializing entire unions when needed.

In Details

The expr.cc component of GCC's middle-end handles expression evaluation and type manipulation. This patch corrects categorize_ctor_elements_1 and count_type_elements to use DECL_SIZE instead of TYPE_SIZE when assessing union members, particularly bit-fields. This aligns with how C++ frontends handle constructors and union member sizes, resolving issues with padding byte detection and improving code generation for initialized unions.

For Context
union
A composite data type that allows storing different data types in the same memory location. Only one member of a union can be active at any given time.
bit-field
A data member within a struct or union that is allowed to occupy only a specified number of bits, which may be less than the number of bits in its data type.
TYPE_SIZE
A GCC internal macro that returns the size of a type in bits.
DECL_SIZE
A GCC internal macro that returns the size of a declaration (e.g., a variable or field) in bits.
constructor element categorization
The process by which a compiler determines how to initialize the members of a struct or union, especially when using aggregate initializers (constructors).
Filed Under: c++optimizationbugfix