https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102032
Bug ID: 102032 Summary: missed optimization on 2 equivalent expressions when -fwrapv is not used Product: gcc Version: 11.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: vincent-gcc at vinc17 dot net Target Milestone: --- Consider the following code from bug 30484: int f (int a, int b, int c) { if (b < 0) return a + b + c; else return a + c + b; } On x86_64 with -O3, GCC 11.2.0 gives leal (%rdi,%rdx), %eax addl %esi, %edi addl %edx, %edi addl %esi, %eax testl %esi, %esi cmovs %edi, %eax But x86_64 processors (and most processors, AFAIK) do not care about overflows, so that a + b + c and a + c + b are actually equivalent for the processor and one should get only 2 instructions, corresponding to the 2 additions. Surprisingly, -fwrapv has the effect to allow this optimization, with the following generated code: addl %edx, %edi leal (%rdi,%rsi), %eax