https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86318
Bug ID: 86318 Summary: const local aggregates can be assumed not to be modified even when escaped Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Because the local object in all three function is defined const it can be assumed not to change as a result of the call to f() (otherwise the behavior of a program that changed it would be undefined). GCC takes advantage of this constraint when the object is of a basic/fundamental type like in g0() but not when the object is an aggregate like in g1() or g2(), unless the object has a static storage duration. $ cat c.c && gcc -S -O2 -Wall -Wextra -Wpedantic -Wstrict-prototypes -fdump-tree-optimized=/dev/stdout c.c struct S { int i; }; void f (const void*); void g0 (void) { const int i = 1; f (&i); if (i != 1) // folded into false __builtin_abort (); } void g1 (void) { const int a[1] = { 1 }; f (a); if (*a != 1) // not folded but could be __builtin_abort (); } void g2 (void) { const struct { int i; } s = { 1 }; f (&s.i); if (s.i != 1) // not folded but could be __builtin_abort (); } ;; Function g0 (g0, funcdef_no=0, decl_uid=1902, cgraph_uid=1, symbol_order=0) g0 () { const int i; <bb 2> [local count: 1073741825]: i = 1; f (&i); i ={v} {CLOBBER}; return; } ;; Function g1 (g1, funcdef_no=1, decl_uid=1906, cgraph_uid=2, symbol_order=1) g1 () { const int a[1]; int _1; <bb 2> [local count: 1073741825]: a[0] = 1; f (&a); _1 = a[0]; if (_1 != 1) goto <bb 3>; [0.00%] else goto <bb 4>; [99.96%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073312327]: a ={v} {CLOBBER}; return; } ;; Function g2 (g2, funcdef_no=2, decl_uid=1910, cgraph_uid=3, symbol_order=2) g2 () { const struct { int i; } s; int _1; <bb 2> [local count: 1073741825]: s.i = 1; f (&s.i); _1 = s.i; if (_1 != 1) goto <bb 3>; [0.00%] else goto <bb 4>; [99.96%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073312327]: s ={v} {CLOBBER}; return; }