https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114967
Bug ID: 114967 Summary: Missed optimization: std::min((int) f, -a) ==> -a where (bool f, unsigned char a) Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: 652023330028 at smail dot nju.edu.cn Target Milestone: --- Hello, we noticed that the code below can be optimized as stated in the title (std::min((int) f, -a) ==> -a), but gcc -O3 seems to have missed it. Reduced code: https://godbolt.org/z/soWxqTMz8 #include <algorithm> int n; void func(bool f, unsigned char a) { n = std::min((int) f, -a); } GCC -O3: <bb 2> [local count: 1073741824]: # DEBUG BEGIN_STMT _1 = (int) a_5(D); _2 = -_1; _3 = (int) f_7(D); # DEBUG __a => &D.18872 # DEBUG __b => &D.18873 # DEBUG INLINE_ENTRY min if (_2 < _3) goto <bb 3>; [34.00%] else goto <bb 4>; [66.00%] <bb 3> [local count: 365072224]: <bb 4> [local count: 1073741824]: # _4 = PHI <0(2), _2(3)> # DEBUG __a => NULL # DEBUG __b => NULL n = _4; return; func(bool, unsigned char): movzx esi, sil movzx edi, dil xor eax, eax neg esi cmp esi, edi cmovge esi, eax mov DWORD PTR n[rip], esi ret Expected code (Clang): func(bool, unsigned char): # @func(bool, unsigned char) neg esi mov dword ptr [rip + n], esi ret Thank you very much for your time and effort! We look forward to hearing from you.