https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96900

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> ---
This is also a missed optimization opportunity.  Another test case that shows
both the bogus warning and the suboptimal codegen is the following.  Because
there is no explicit initializer for a.b, fold_nonarray_ctor_reference()
returns a scalar zero, which again triggers the warning and prevents the strlen
call from being folded.  The optimization never worked in this case so that
part is not a regression.

$ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c
struct A { char n, a[4], b[4]; };
const struct A a = { };

int f (void)
{
  const char *p = &a.b[2];
  return __builtin_strlen (p - 2);
}
z.c: In function ‘f’:
z.c:7:10: warning: offset ‘5’ outside bounds of constant string
[-Warray-bounds]
    7 |   return __builtin_strlen (p - 2);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~
z.c:2:16: note: ‘a’ declared here
    2 | const struct A a = { };
      |                ^

;; Function f (f, funcdef_no=0, decl_uid=1935, cgraph_uid=1, symbol_order=1)

f ()
{
  long unsigned int _1;
  int _3;

  <bb 2> [local count: 1073741824]:
  _1 = __builtin_strlen (&MEM <const char> [(void *)&a + 5B]);
  _3 = (int) _1;
  return _3;

}

Reply via email to