Skip to content

Commit 65c8fd7

Browse files
committed
gimple-fold: Fix up fold_array_ctor_reference RAW_DATA_CST handling [PR118207]
The following testcases ICE because fold_array_ctor_reference in the RAW_DATA_CST handling just return build_int_cst without actually checking that if type is non-NULL, TREE_TYPE (val) is uselessly convertible to it. By falling through the code after it without *suboff += we get everything we need, the two if conditionals will never be true (we've already checked that size == BITS_PER_UNIT and so can't be 0, and val will be INTEGER_CST), but it will do the important fold_ctor_reference call which will deal with type incompatibilities. 2024-12-28 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/118207 * gimple-fold.cc (fold_array_ctor_reference): For RAW_DATA_CST, just set val to build_int_cst and fall through to the normal element handling code instead of returning build_int_cst right away. * gcc.dg/pr118207.c: New test.
1 parent a326ecf commit 65c8fd7

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

gcc/gimple-fold.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9707,9 +9707,8 @@ fold_array_ctor_reference (tree type, tree ctor,
97079707
constructor_elt *elt = CONSTRUCTOR_ELT (ctor, ctor_idx);
97089708
if (elt->index == NULL_TREE || TREE_CODE (elt->index) != INTEGER_CST)
97099709
return NULL_TREE;
9710-
*suboff += access_index.to_uhwi () * BITS_PER_UNIT;
97119710
unsigned o = (access_index - wi::to_offset (elt->index)).to_uhwi ();
9712-
return build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
9711+
val = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, o));
97139712
}
97149713
if (!size && TREE_CODE (val) != CONSTRUCTOR)
97159714
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* PR tree-optimization/118207 */
2+
/* { dg-do compile } */
3+
/* { dg-options "-O2" } */
4+
5+
struct A { unsigned char a; };
6+
struct B { struct A b; };
7+
static const unsigned char c[] = {
8+
#embed __FILE__
9+
};
10+
struct B d;
11+
12+
void
13+
foo ()
14+
{
15+
const struct B *t = (const struct B *) &c;
16+
d.b = t->b;
17+
}

gcc/testsuite/gcc.dg/pr118207.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* PR tree-optimization/118207 */
2+
/* { dg-do compile } */
3+
/* { dg-options "-O2" } */
4+
5+
struct A { unsigned char a; };
6+
struct B { struct A b; };
7+
static const unsigned char c[160] = {
8+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
9+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
10+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
11+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
12+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
13+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
14+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
15+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
16+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
18+
struct B d;
19+
20+
void
21+
foo ()
22+
{
23+
const struct B *t = (const struct B *) &c;
24+
d.b = t->b;
25+
}

0 commit comments

Comments
 (0)