Skip to content

Miscompilation with noalias and pointer equality #59679

Open
@carbotaniuman

Description

@carbotaniuman

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions