https://gcc.gnu.org/bugzilla/show_bug.cgi?id=26724
Matthijs Kooijman <matthijs at stdin dot nl> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |matthijs at stdin dot nl
--- Comment #5 from Matthijs Kooijman <matthijs at stdin dot nl> ---
I also ran into this problem in an embedded project and the workaround also
works for me - thanks!
I had already made a short testcase in godbolt for this before I found this
report. I'll share it here just in case it is useful for testing this problem
later: https://godbolt.org/z/s1eK6a3Pf
Here's the code:
#include <stdio.h>
// I added always_inline to see if that would help - seems to make not
difference
//[[gnu::always_inline]] static inline bool always_true()
__attribute__((always_inline));
static inline bool always_true() { return true; }
static constexpr inline bool constexpr_always_true() { return true; }
int main() {
printf("DIRECT: %d\n", __builtin_constant_p(always_true()));
bool var = always_true();
printf("VIAVAR: %d\n", __builtin_constant_p(var));
printf("CONSTEXPR: %d\n", __builtin_constant_p(constexpr_always_true()));
}
Gcc 12.2 outputs:
DIRECT: 0
VIAVAR: 1
CONSTEXPR: 1
Two additional observations:
- clang seems to behave the same as gcc here
- Adding constexpr to the function definition also fixes the problem without
the workaround (but might not always be useful - constexpr has more strict
requirements than a __builtin_constant_p test).
- Adding always_inline attributes makes no difference.