https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123600
Bug ID: 123600
Summary: && unary operator (Labels as Values) issue when using
optimisation level 2 (and higher)
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: colin.i.king at gmail dot com
Target Milestone: ---
Optimisation level O2 and above on the following code returns the same address
for labels l1, l2 and l3, tested on gcc-10 to gcc 15.2, same issue occurs.
Testing with other compilers (clang, pcc, tcc this works OK and returns the
correct addresses)
Tested on x86-64 only.
Reproducer:
$ cat bug.c
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
const uintptr_t *labels(void)
{
static const uintptr_t addrs[] = {
(uintptr_t)&&l1,
(uintptr_t)&&l2,
(uintptr_t)&&l3
};
l1:
printf("%p\n", &&l1);
l2:
printf("%p\n", &&l2);
l3:
return addrs;
}
int main(void)
{
const uintptr_t *ptr = labels();
printf("ptr[0] %" PRIxPTR "\n", ptr[0]);
printf("ptr[1] %" PRIxPTR "\n", ptr[1]);
printf("ptr[2] %" PRIxPTR "\n", ptr[2]);
return 0;
}
Tests:
$ gcc-15 -O0 bug.c
$ ./a.out
0x55555555513d
0x55555555515b
ptr[0] 55555555513d
ptr[1] 55555555515b
ptr[2] 555555555179
$ gcc-15 -O1 bug.c
$ ./a.out
0x55555555513a
0x555555555155
ptr[0] 55555555513a
ptr[1] 555555555155
ptr[2] 555555555169
$ gcc-15 -O2 bug.c
$ ./a.out
0x555555555190
0x555555555190
ptr[0] 555555555190
ptr[1] 555555555190
ptr[2] 555555555190
$ gcc-15 -O3 bug.c
$ ./a.out
0x555555555190
0x555555555190
ptr[0] 555555555190
ptr[1] 555555555190
ptr[2] 555555555190