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

            Bug ID: 127497
           Summary: Wrong code with -fwrapv-pointer at -O1/2
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kyuwoncho18 at gmail dot com
  Target Milestone: ---

fwrapv-pointer allows pointer overflow as defined behavior, but the
optimization generates wrong code when it does.

Reproducer:
#include <stdio.h>

#pragma GCC optimize ("wrapv-pointer") // or -fwrapv-pointer

__attribute__((noinline)) unsigned long
f (char *p, unsigned long len)
{
  char *q = p + len;
  unsigned long n = 0;
  do
    {
      n++;
      p += 4;
    }
  while (p < q);
  return n;
}

int
main (void)
{
  printf ("%lu\n", f ((char *) (__UINTPTR_TYPE__) -3, 8));
  return 0;
}

Output:
O0/3/g: 2
O1/2/s/z: 1

Reply via email to