[Bug c/65752] New: Too strong optimizations int -> pointer casts

gcc at robbertkrebbers dot nl gcc-bugzilla@gcc.gnu.org
Mon Apr 13 14:04:00 GMT 2015


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

            Bug ID: 65752
           Summary: Too strong optimizations int -> pointer casts
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc at robbertkrebbers dot nl

The following program prints "0" instead of the expected "15":

#include <stdio.h>
#include <stdint.h>
#include <limits.h>

int main() {
  int x = 0, *p = 0;
  for (uintptr_t i = 0; ; i++) {
    if (i == (uintptr_t)&x) { p = (int*)i; break; }
  }
  *p = 15;
  printf("%d\n", x);
}

gcc -O2 makes too strict assumptions about non-aliasing here: it removes the
loop entirely (which is perfectly fine), but then assumes that the pointers p
and &x are unrelated.

NB 1: I do not think that DR #260 applies here
NB 2: When compiled with clang, it also optimizes out the loop, but it does
print the expected "15"



More information about the Gcc-bugs mailing list