[Bug tree-optimization/57199] [4.8/4.9 Regression] Bogus warning: iteration NNNN invokes undefined behavior -Waggressive-loop-optimizations

jakub at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon May 20 14:26:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57199

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
But this isn't any form of the may invoke, the loop certainly unconditionally
invokes undefined behavior, just the whole loop is very unlikely to be ever
executed (in this case if size is supposed to represent the length of an array
with elements bigger than 1, then already the size would need to be invalid,
but that is something the compiler can't understand, for it the size_t field is
likely any other field, and there is no guarantee it won't be -1).

It is in principle no different from say:

void
foo (size_t x)
{
  if (x == (size_t) -1)
    {
      unsigned int a[128];
      int i;

      for (i = 0; i < 128; ++i)     /* { dg-message "note: containing loop" }
*/
        a[i] = i * 0x02000001;      /* { dg-warning "invokes undefined
behavior" } */
      bar (a);
    }
}

where you know you are never going to call foo with (size_t) -1, but the
compiler doesn't know.  How is the above different from say:
void
bar (void)
{
  unsigned int a[128];
  int i;

  for (i = 0; i < 128; ++i)     /* { dg-message "note: containing loop" } */
    a[i] = i * 0x02000001;      /* { dg-warning "invokes undefined behavior" }
*/
  bar (a);
}
...
/* in another CU */
void
baz (size_t x)
{
  if (x == (size_t) -1)
    bar ();
}

In your original testcase, you wouldn't get the warning if size was a signed
integer instead of unsigned one, then the compiler would know it is undefined
behavior if the size wraps and would just optimize the loop away altogether. 
Or perhaps some __builtin_unreachable assert that size isn't (size_t) -1?



More information about the Gcc-bugs mailing list