[cfe-dev] Incongruency in __builtin_constant_p with pointer argument

Abramo Bagnara abramo.bagnara at gmail.com
Thu Apr 28 09:55:27 PDT 2011


Il 28/04/2011 18:47, Jonathan Sauer ha scritto:
> Hello,
> 
>>> From http://gcc.gnu.org/onlinedocs/gcc-4.4.5/gcc/Other-Builtins.html:
>>
>>> You can use the built-in function __builtin_constant_p to determine
>>> if a value is known to be constant at compile-time and hence that GCC
>>> can perform constant-folding on expressions involving that value. The
>>> argument of the function is the value to test. The function returns
>>> the integer 1 if the argument is known to be a compile-time constant
>>> and 0 if it is not known to be a compile-time constant. A return of 0
>>> does not indicate that the value is not a constant, but merely that
>>> GCC cannot prove it is a constant with the specified value of the -O
>>> option.
>>
>> I think that clang is right to consider pointer to static memory region
>> to be constant at compile-time and hence candidate to constant-folding.
>> (note that the documentation does not say "constant with a known value
>> at compile-time", but "value known to be constant at compile time").
> 
> But "candidate to constant-folding" requires the latter, I think. E.g. "a + 2" cannot be
> folded if the value of "a" is not known at compile-time, even if it happens to be constant.

$ cat t.c
int i;

int a[__builtin_constant_p(&i+1) ? 1 : -1];
$ clang -S t.c
$

clang disagrees...

What should be taken in account is that only the "numeric" value of the
pointer is not known at compile time, but its "symbolic" value is known.




More information about the cfe-dev mailing list