http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49368
Summary: __builtin_constant_p is unable to determine if a union
is constant
Product: gcc
Version: 4.5.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
In the following code GCC seems to be unable to determine that a const union is
actually const when passed as an argument to __builtin_constant_p(). If I use
a struct instead, then it seems to work, so there is only an issue when using a
union.
This is tested on GCC 4.5.1 & GCC 4.1.2
$ cat dummy.c
#include <stdio.h>
union u {
int a;
int b;
};
#define DO_WORK(_arg_) \
do { \
if (__builtin_constant_p(_arg_)) \
{ \
printf("Do optimized code\n"); \
} \
else \
{ \
printf("Do slow code\n"); \
} \
} while (0 == 1)
int main()
{
const union u x = { .a = 0 } ;
DO_WORK(x.b);
return 0;
}
$ gcc -Wall -Os dummy.c
$ ./a.out
Do slow code