I took a stab at PR4898[1]. The attached patch improves Clang’s __builtin_constant_p support so that the Linux kernel is happy. With this improvement, Clang can determine if __builtin_constant_p is true or false after inlining.
As an example:
static attribute((always_inline)) int foo(int x) {
if (__builtin_constant_p(x))
return 1;
return 0;
}
static attribute((always_inline)) int mux() {
if (__builtin_constant_p(37))
return 927;
return 0;
}
int bar(int a) {
if (a)
return foo(42);
else
return mux();
}
I took a stab at PR4898[1]. The attached patch improves Clang's
__builtin_constant_p support so that the Linux kernel is happy. With this
improvement, Clang can determine if __builtin_constant_p is true or false
after inlining.
As an example:
static __attribute__((always_inline)) int foo(int x) {
if (__builtin_constant_p(x))
return 1;
return 0;
}
static __attribute__((always_inline)) int mux() {
if (__builtin_constant_p(37))
return 927;
return 0;
}
int bar(int a) {
if (a)
return foo(42);
else
return mux();
}
I actually was working on an updated patch for the LLVM-side of this, also. I was just working on some test cases; I’ll post it soon. It’s somewhat different than yours.
I haven’t touched the clang side yet, but I think it needs to be more complex than what you have there. I think it actually needs to be able to evaluate the intrinsic as a constant false in the front-end in some circumstances, rather than always emitting IR if it cannot tell the answer is true.
I actually was working on an updated patch for the LLVM-side of this, also. I was just working on some test cases; I’ll post it soon. It’s somewhat different than yours.
Oh cool! I’ll look at the Phab entry.
I haven’t touched the clang side yet, but I think it needs to be more complex than what you have there. I think it actually needs to be able to evaluate the intrinsic as a constant false in the front-end in some circumstances, rather than always emitting IR if it cannot tell the answer is true.
It would only be “false” if the function cannot be inlined, right? Otherwise you don’t really know. So at -O0, we can safely mark something as “false”. But in other cases, we would need to be wary.