Open
Description
Reproducer
// test.c
#include <stdio.h>
int x;
__attribute__((noinline))
int test(int * restrict ptr) {
*ptr = 1;
if (ptr == &x) {
*ptr = 2;
}
return *ptr;
}
int main() {
printf("%d\n", test(&x));
}
$ clang -O3 test.c
$ ./a.out
1
Tested with clang 15.0.0 and trunk
The issue appears to be that Clang's constant propagation replaces *ptr = 2
with x = 2
, which breaks the noalias based on analysis and allows the return *ptr
to be simplified to return 1
.