[Bug tree-optimization/93051] Wrong optimizations for pointers: `if (p == q) use p` -> `if (p == q) use q`

ch3root at openwall dot com gcc-bugzilla@gcc.gnu.org
Tue Dec 24 17:23:00 GMT 2019


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

--- Comment #3 from Alexander Cherepanov <ch3root at openwall dot com> ---
For completeness, an example with a guessed pointer, based on the testcase from
bug 65752, comment 0, gcc-only (dom2):

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

__attribute__((noipa)) // imagine it in a separate TU
static uintptr_t opaque_to_int(void *p) { return (uintptr_t)p; }

int main()
{
    int x;
    int *p = &x;
    uintptr_t ip = (uintptr_t)p;

    uintptr_t iq = 0;
    while (iq < ip)
        iq++;

    uintptr_t ir = opaque_to_int(p); // hide provenance of p

    if (ir == iq) {
        *p = 1;
        *(int *)ir = 2;
        printf("result: %d\n", *p);
    }
}
----------------------------------------------------------------------
$ gcc -std=c11 -pedantic -Wall -Wextra -O3 test.c && ./a.out
result: 1
----------------------------------------------------------------------
gcc x86-64 version: gcc (GCC) 10.0.0 20191224 (experimental)
----------------------------------------------------------------------


More information about the Gcc-bugs mailing list