On Thu, Apr 5, 2018 at 2:06 PM Linus Torvalds <torva...@linux-foundation.org> wrote: > On Thu, Apr 5, 2018 at 10:46 AM, James Y Knight <jykni...@google.com> wrote: > > > > GCC, however, mixes up the concept of a C "constant expression" with the > > results of running optimization passes such as inlining for its > > definition/implementation of __builtin_constant_p. Clang does not, and quite > > likely will not ever, do that.
> Nothing has ever said that "__builtin_constant_p(x)" means "is x an > integer constant expression" I had actually meant that the __builtin_constant_p **itself** had to be a constant expression, not that its *argument* must be an I-C-E for __builtin_constant_p to return true. But after spending some time on further investigating in order to show an example of how this matters, I must take back my words. I was mistaken about GCC's semantics. Take this example: === int function(void); void useval(int*); int f() { int v = 1 + 2; int array[2][__builtin_constant_p(v) ? 1 : 100]; useval(array[0]); return sizeof(array[function()]) / sizeof(array[0]); } === Build with "gcc -O -std=c99": === f: subq $24, %rsp leaq 8(%rsp), %rdi call useval call function movl $4, %eax addq $24, %rsp ret === Note the fact that "function" is actually *called* indicates that 'array' is a VLA (...and that C99's sizeof(VLA) semantics are bonkers, but that's another story...). Which means that __builtin_constant_p(v) was _not_ evaluated as an integer constant expression by GCC. Instead, it was left as an expression. And, the stack frame being only 24 bytes indicates that the __bcp eventually evaluated to true. I actually think this actually _is_ something clang can implement. Thanks for making me try to prove myself. :)