[Bug c/107285] Incorrect code generation when we use __builtin_constant_p built-in function.

alex.curiou at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Oct 17 08:10:23 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107285

--- Comment #3 from Alexey <alex.curiou at gmail dot com> ---
(In reply to Jakub Jelinek from comment #1)
> That is just bogus expectation.
> __builtin_constant_p works that it has a cut-off after certain optimizations
> (for  -O0 already very early), if optimizations before it manage to optimize
> the argument into a constant, it is true.  After the cut-off the remaining
> occurrences are folded into false.
> So, if bar is inlined or say IPA constant propagated, the argument is
> constant, while if it isn't inlined nor IPA constant propagated (etc.), as
> seems to be the case for -Og, it is not constant, thus you get what you
> asked for.
> __builtin_constant_p better should be used as a way to optimize code without
> changing the observable behavior...

For -O2: the generated code and the result should be consistent.
Otherwise the usage of the __builtin_constant_p function is just unpredictable.

But now the code of bar func will calculate 60 and the real return value is 13.

Or just change the explanation for the __builtin_constant_p
(https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html) and write that some
times the branching on the result of this function will have nothing to do with
the final result provided by the compiled code.


More information about the Gcc-bugs mailing list