[Bug tree-optimization/53265] Warn when undefined behavior implies smaller iteration count

jakub at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Apr 30 06:46:00 GMT 2013


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

--- Comment #29 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-04-30 06:45:54 UTC ---
If you want another testcase which doesn't warn and is optimized based on the
assumption that undefined behavior doesn't occur, then say:
http://blog.regehr.org/archives/918#comment-8646
contains:
int a[4];

__attribute__((noinline, noclone)) int
foo (int x)
{
  int i, r = 0, n = x & 31;
  for (i = 0; i < n; i++)
    r += a[i];
  return r;
}

int
main ()
{
  int x = 255;
  __asm volatile ("" : "+r" (x));
  return foo (x);
}

But in both the testcases warning is questionable, in your testcase, if p.r[0]
is non-zero and any of p.r[1] through p.r[8] is zero, then no undefined
behaviour is triggered and the program is valid, and gcc doesn't break it in
any way.
Similarly with the my testcase above and foo being called with x where the low
5 bits are 0 to 4, again, valid in that case (ignore main and the value 255
being hidden from the compiler there).
What would you like to warn about?  That if the loop is invoked with variables
and data that result in undefined behaviour that the behaviour will be
undefined?
The difference between where we know is there the compiler knows that whenever
you enter the loop construct, you will hit the undefined behavior (unless say
one of the called functions in the body throws or exits), so the level of false
positives is low.



More information about the Gcc-bugs mailing list