Skip to content

Commit 3670c70

Browse files
rguenthRichard Biener
authored andcommitted
middle-end/115641 - invalid address construction
fold_truth_andor_1 via make_bit_field_ref builds an address of a CALL_EXPR which isn't valid GENERIC and later causes an ICE. The following simply avoids the folding for f ().a != 1 || f ().b != 2 as it is a premature optimization anyway. The alternative would have been to build a TARGET_EXPR around the call. To get this far f () has to be const as otherwise the two calls are not semantically equivalent for the optimization. PR middle-end/115641 * fold-const.cc (decode_field_reference): If the inner reference isn't something we can take the address of, fail. * gcc.dg/torture/pr115641.c: New testcase.
1 parent ebac11a commit 3670c70

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

gcc/fold-const.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,6 +5003,9 @@ decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
50035003
|| *pbitsize < 0
50045004
|| offset != 0
50055005
|| TREE_CODE (inner) == PLACEHOLDER_EXPR
5006+
/* We eventually want to build a larger reference and need to take
5007+
the address of this. */
5008+
|| (!REFERENCE_CLASS_P (inner) && !DECL_P (inner))
50065009
/* Reject out-of-bound accesses (PR79731). */
50075010
|| (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
50085011
&& compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* { dg-do run } */
2+
3+
typedef struct {
4+
char hours, day, month;
5+
short year;
6+
} T;
7+
8+
T g (void)
9+
{
10+
T now;
11+
now.hours = 1;
12+
now.day = 2;
13+
now.month = 3;
14+
now.year = 4;
15+
return now;
16+
}
17+
18+
__attribute__((const)) T f (void)
19+
{
20+
T virk = g ();
21+
return virk;
22+
}
23+
24+
int main ()
25+
{
26+
if (f ().hours != 1 || f ().day != 2 || f ().month != 3 || f ().year != 4)
27+
__builtin_abort ();
28+
return 0;
29+
}

0 commit comments

Comments
 (0)