https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123160
--- Comment #6 from Andrew Macleod <amacleod at redhat dot com> ---
Here's a short testcase which shows that pointer_plus is not being folded very
well, and causes incorrect code at -O2.
struct A
{
char a[10];
char c[10];
} a;
extern void dead1 (void);
extern void dead2 (void);
int
main (void)
{
void *r = &a.a[6];
if (__builtin_object_size (r, 3) != sizeof (a.a) - 6)
dead1 ();
void *r2 = &a.a[4] + 2;
if (__builtin_object_size (r2, 3) != sizeof (a.a) - 6)
dead2 ();
}
Neither dead1() nor dead2() should be called, but the program produced is:
int main ()
{
<bb 2> [local count: 1073741824]:
dead2 ();
return 0;
}
Visiting statement:
r_5 = &a.a[6];
which is likely CONSTANT
Lattice value changed to CONSTANT &a.a[6]. Adding SSA edges to worklist.
marking stmt to be not simulated again
Visiting statement:
_11 = __builtin_object_size (r_5, 3);
which is likely CONSTANT
Lattice value changed to CONSTANT 4.
vs
Visiting statement:
r2_8 = &a.a[4] + 2;
which is likely CONSTANT
Lattice value changed to CONSTANT &MEM <char> [(void *)&a + 6B]. Adding SSA
edges to worklist.
marking stmt to be not simulated again
Visiting statement:
_12 = __builtin_object_size (r2_8, 3);
which is likely CONSTANT
wholesize_for_memref: 20, offset: 6
Lattice value changed to CONSTANT 14.
So something is definitely amok. It appears to be using the full size of 'a'
instead of the size of 'a.a' when pointer_plus is used.
When the forthcoming prange PTA code is checked in, this big is affects
gcc.dg/builtin-object-size-4.c causing it to fail. That means it will
likely become more prevalent.