https://gcc.gnu.org/g:7002faa756a0f60fe28c206620c777112755f94d
commit r17-6487-g7002faa756a0f60fe28c206620c777112755f94d Author: Jakub Jelinek <[email protected]> Date: Fri Nov 20 09:17:29 2026 +0100 gimple-fold: Handle bit-fields in unions correctly during __builtin_clear_padding [PR128302] Clear padding in unions is special, we basically compute bitmask which bits aren't padding (0s) and which are padding (1s) for each union member individually and using clear_padding_flush then and it together because we don't know which union member is active, so only padding bits for all the union members are cleared. This PR reports that I forgot to handle bit-fields in there. This patch fixes it by outlining most of the DECL_BIT_FIELD handling from the RECORD_TYPE loop in clear_padding_type into a new function and using it also in clear_padding_union instead of just calling clear_padding_type as if it wasn't a bit-field. 2026-11-20 Jakub Jelinek <[email protected]> PR middle-end/128302 * gimple-fold.cc (clear_padding_bitfield): New function, mostly moved code from clear_padding_type. (clear_padding_union): Use it to handle DECL_BIT_FIELD inside of a union. (clear_padding_type): Handle DECL_BIT_FIELD using the outlined new clear_padding_bitfield function. * gcc.dg/builtin-clear-padding-3.c: New test. Reviewed-by: Richard Biener <[email protected]> Diff: --- gcc/gimple-fold.cc | 231 +++++++++++++------------ gcc/testsuite/gcc.dg/builtin-clear-padding-3.c | 45 +++++ 2 files changed, 167 insertions(+), 109 deletions(-) diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc index 04c585303270..db3e0e571c31 100644 --- a/gcc/gimple-fold.cc +++ b/gcc/gimple-fold.cc @@ -4720,6 +4720,117 @@ clear_padding_add_padding (clear_padding_struct *buf, static void clear_padding_type (clear_padding_struct *, tree, HOST_WIDE_INT, bool); +/* Clear padding for a bit-field FIELD. Helper function for + clear_padding_type (to handle bit-fields in RECORD_TYPE) and + clear_padding_union (to handle bit-fields in UNION_TYPE). */ + +static HOST_WIDE_INT +clear_padding_bitfield (clear_padding_struct *buf, tree field, + HOST_WIDE_INT pos, HOST_WIDE_INT cur_pos) +{ + HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (field)); + HOST_WIDE_INT bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)); + bpos %= BITS_PER_UNIT; + HOST_WIDE_INT end = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; + if (pos + end > cur_pos) + { + if ((unsigned HOST_WIDE_INT) end + > (unsigned HOST_WIDE_INT) clear_padding_buf_size / 2) + { + if ((unsigned HOST_WIDE_INT) pos + clear_padding_unit + > (unsigned HOST_WIDE_INT) cur_pos) + { + clear_padding_add_padding (buf, + pos + clear_padding_unit - cur_pos); + cur_pos = pos + clear_padding_unit; + } + } + else + { + clear_padding_add_padding (buf, pos + end - cur_pos); + cur_pos = pos + end; + } + } + gcc_assert (cur_pos > pos + && ((unsigned HOST_WIDE_INT) buf->size + >= (unsigned HOST_WIDE_INT) cur_pos - pos)); + unsigned char *p = buf->buf + buf->size - (cur_pos - pos); + HOST_WIDE_INT fldszu = 0; + auto bitfld_adjust = [&] { + memset (p, 0, buf->buf + buf->size - p); + fldszu -= buf->buf + buf->size - p; + clear_padding_flush (buf, false); + while ((unsigned HOST_WIDE_INT) fldszu + > (unsigned HOST_WIDE_INT) 2 * clear_padding_unit) + { + HOST_WIDE_INT this_sz + = MIN ((HOST_WIDE_INT) (clear_padding_buf_size - buf->size), + (HOST_WIDE_INT) (fldszu - 2 * clear_padding_unit)); + memset (buf->buf + buf->size, 0, this_sz); + fldszu -= this_sz; + cur_pos += this_sz; + buf->size += this_sz; + clear_padding_flush (buf, false); + } + clear_padding_add_padding (buf, pos + end - cur_pos); + cur_pos = pos + end; + pos = cur_pos - fldszu - ((fldsz % BITS_PER_UNIT) != 0); + gcc_assert (cur_pos > pos && ((unsigned HOST_WIDE_INT) buf->size + >= (unsigned HOST_WIDE_INT) cur_pos - pos)); + p = buf->buf + buf->size - (cur_pos - pos); + }; + if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) + sorry_at (buf->loc, "PDP11 bit-field handling unsupported" + " in %qs", "__builtin_clear_padding"); + else if (BYTES_BIG_ENDIAN) + { + /* Big endian. */ + if (bpos + fldsz <= BITS_PER_UNIT) + *p &= ~(((1 << fldsz) - 1) << (BITS_PER_UNIT - bpos - fldsz)); + else + { + if (bpos) + { + *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); + p++; + fldsz -= BITS_PER_UNIT - bpos; + } + fldszu = fldsz / BITS_PER_UNIT; + if (cur_pos < pos + end) + bitfld_adjust (); + memset (p, 0, fldszu); + p += fldszu; + fldsz %= BITS_PER_UNIT; + if (fldsz) + *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; + } + } + else + { + /* Little endian. */ + if (bpos + fldsz <= BITS_PER_UNIT) + *p &= ~(((1 << fldsz) - 1) << bpos); + else + { + if (bpos) + { + *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); + p++; + fldsz -= BITS_PER_UNIT - bpos; + } + fldszu = fldsz / BITS_PER_UNIT; + if (cur_pos < pos + end) + bitfld_adjust (); + memset (p, 0, fldszu); + p += fldszu; + fldsz %= BITS_PER_UNIT; + if (fldsz) + *p &= ~((1 << fldsz) - 1); + } + } + return cur_pos; +} + /* Clear padding bits of union type TYPE. */ static void @@ -4780,7 +4891,15 @@ clear_padding_union (clear_padding_struct *buf, tree type, union_buf->off = start_off; union_buf->size = start_size; memset (union_buf->buf, ~0, start_size); - clear_padding_type (union_buf, TREE_TYPE (field), fldsz, for_auto_init); + if (DECL_BIT_FIELD (field)) + { + HOST_WIDE_INT cur_pos = clear_padding_bitfield (union_buf, field, + 0, 0); + gcc_assert (cur_pos == fldsz); + } + else + clear_padding_type (union_buf, TREE_TYPE (field), fldsz, + for_auto_init); clear_padding_add_padding (union_buf, sz - fldsz); clear_padding_flush (union_buf, true); } @@ -4908,8 +5027,7 @@ type_has_padding_at_level_p (tree type) for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f)) if (TREE_CODE (f) != FIELD_DECL || TREE_TYPE (f) == error_mark_node) continue; - else if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)), - TYPE_SIZE (type)) != 1) + else if (simple_cst_equal (DECL_SIZE (f), TYPE_SIZE (type)) != 1) return true; else any_fields = true; @@ -4999,117 +5117,12 @@ clear_padding_type (clear_padding_struct *buf, tree type, HOST_WIDE_INT pos = int_byte_position (field); if (pos >= sz) continue; - HOST_WIDE_INT bpos - = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)); - bpos %= BITS_PER_UNIT; - HOST_WIDE_INT end - = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT; if (pos > cur_pos) { clear_padding_add_padding (buf, pos - cur_pos); cur_pos = pos; } - if (pos + end > cur_pos) - { - if ((unsigned HOST_WIDE_INT) end - > (unsigned HOST_WIDE_INT) clear_padding_buf_size / 2) - { - if ((unsigned HOST_WIDE_INT) pos + clear_padding_unit - > (unsigned HOST_WIDE_INT) cur_pos) - { - clear_padding_add_padding (buf, - pos + clear_padding_unit - - cur_pos); - cur_pos = pos + clear_padding_unit; - } - } - else - { - clear_padding_add_padding (buf, pos + end - cur_pos); - cur_pos = pos + end; - } - } - gcc_assert (cur_pos > pos - && ((unsigned HOST_WIDE_INT) buf->size - >= (unsigned HOST_WIDE_INT) cur_pos - pos)); - unsigned char *p = buf->buf + buf->size - (cur_pos - pos); - HOST_WIDE_INT fldszu = 0; - auto bitfld_adjust = [&] { - memset (p, 0, buf->buf + buf->size - p); - fldszu -= buf->buf + buf->size - p; - clear_padding_flush (buf, false); - while ((unsigned HOST_WIDE_INT) fldszu - > (unsigned HOST_WIDE_INT) 2 * clear_padding_unit) - { - HOST_WIDE_INT this_sz - = MIN ((HOST_WIDE_INT) (clear_padding_buf_size - - buf->size), - (HOST_WIDE_INT) (fldszu - - 2 * clear_padding_unit)); - memset (buf->buf + buf->size, 0, this_sz); - fldszu -= this_sz; - cur_pos += this_sz; - buf->size += this_sz; - clear_padding_flush (buf, false); - } - clear_padding_add_padding (buf, pos + end - cur_pos); - cur_pos = pos + end; - pos = cur_pos - fldszu - ((fldsz % BITS_PER_UNIT) != 0); - gcc_assert (cur_pos > pos - && ((unsigned HOST_WIDE_INT) buf->size - >= (unsigned HOST_WIDE_INT) cur_pos - pos)); - p = buf->buf + buf->size - (cur_pos - pos); - }; - if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) - sorry_at (buf->loc, "PDP11 bit-field handling unsupported" - " in %qs", "__builtin_clear_padding"); - else if (BYTES_BIG_ENDIAN) - { - /* Big endian. */ - if (bpos + fldsz <= BITS_PER_UNIT) - *p &= ~(((1 << fldsz) - 1) - << (BITS_PER_UNIT - bpos - fldsz)); - else - { - if (bpos) - { - *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos); - p++; - fldsz -= BITS_PER_UNIT - bpos; - } - fldszu = fldsz / BITS_PER_UNIT; - if (cur_pos < pos + end) - bitfld_adjust (); - memset (p, 0, fldszu); - p += fldszu; - fldsz %= BITS_PER_UNIT; - if (fldsz) - *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz; - } - } - else - { - /* Little endian. */ - if (bpos + fldsz <= BITS_PER_UNIT) - *p &= ~(((1 << fldsz) - 1) << bpos); - else - { - if (bpos) - { - *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos); - p++; - fldsz -= BITS_PER_UNIT - bpos; - } - fldszu = fldsz / BITS_PER_UNIT; - if (cur_pos < pos + end) - bitfld_adjust (); - memset (p, 0, fldszu); - p += fldszu; - fldsz %= BITS_PER_UNIT; - if (fldsz) - *p &= ~((1 << fldsz) - 1); - } - } + cur_pos = clear_padding_bitfield (buf, field, pos, cur_pos); } else if (DECL_SIZE_UNIT (field) == NULL_TREE) { diff --git a/gcc/testsuite/gcc.dg/builtin-clear-padding-3.c b/gcc/testsuite/gcc.dg/builtin-clear-padding-3.c new file mode 100644 index 000000000000..4093b1069a28 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-clear-padding-3.c @@ -0,0 +1,45 @@ +/* PR middle-end/128302 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +union A { int a : 15; unsigned b : 15; } a1, a2; +#if __BITINT_MAXWIDTH__ >= 951 +union B { _BitInt(951) a : 739; unsigned _BitInt(951) b : 739; } b1, b2; +#else +union B { int a : 5; unsigned b : 5; } b1, b2; +#endif +#if __BITINT_MAXWIDTH__ >= 65535 +union C { _BitInt(65535) a : 65531; unsigned _BitInt(65535) b : 65531; } c1, c2; +#else +union C { int a : 5; unsigned b : 5; } c1, c2; +#endif + +__attribute__((noipa)) void +foo (union A *a, union B *b, union C *c) +{ + a->a = -1361; +#if __BITINT_MAXWIDTH__ >= 951 + b->a = 878212441949088910277769330824589426446069476594189768023517947696484604789122065870316179286548943333278002443986694487060345108557457062615946090733223961247635344029067055511695459419265707468087255861705908585308874670wb; +#else + b->a = 9; +#endif + c->a = 0; +} + +int +main () +{ + __builtin_memset (&a2, -1, sizeof (a2)); + __builtin_memset (&b2, -1, sizeof (b2)); + __builtin_memset (&c2, -1, sizeof (c2)); + foo (&a1, &b1, &c1); + foo (&a2, &b2, &c2); + __builtin_clear_padding (&a2); + __builtin_clear_padding (&b2); + __builtin_clear_padding (&c2); + if (__builtin_memcmp (&a1, &a2, sizeof (a1)) + || __builtin_memcmp (&b1, &b2, sizeof (b1)) + || __builtin_memcmp (&c1, &c2, sizeof (c1))) + __builtin_abort (); + return 0; +}
