https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96733
Jakub Jelinek <jakub at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org, | |redi at gcc dot gnu.org --- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> --- I'm afraid there is nothing that the compiler can do to optimize the clamp_builtin into similar code to clamp_minmax, because the optimizer doesn't know an important restriction on std::clamp - "The behavior is undefined if the value of lo is greater than hi". In the IL this isn't expressed in any way, and when the compiler sees (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val it can only optimize (for -Ofast) the (__hi < __val) ? __hi : __val part into min (__hi, __val), but as it needs to assume e.g. __lo could be 10 and __hi 5, it can't emit max.