https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106420
Bug ID: 106420 Summary: Missed optimization for comparisons Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: zero at smallinteger dot com Target Milestone: --- Created attachment 53339 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53339&action=edit Sample code When comparing different variables to the same constants, in some cases the compiler could first combine the variables and then do a single compare. In the sample given, two variables are compared against 7. In the slow path, GCC produces the following with -O2. cmp edi, 7 setg al cmp esi, 7 setg dl or eax, edx movzx eax, al ret In the fast path, GCC produces this instead. or edi, esi xor eax, eax cmp edi, 7 setg al ret Although the expression a > 7 || b > 7 is the same as (a | b) > 7, the latter is better because it results in fewer instructions. A quick experiment shows the latter also runs quite faster. Verified with Godbolt for GCC trunk. Clang, ICC, and MSVC latest versions also miss this opportunity as per Godbolt.