https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107859
Bug ID: 107859 Summary: Fail to optimize rot13 Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: denis.campredon at gmail dot com Target Milestone: --- Compiled with -O2, the following functions produce different assembly although they compute the same things: ---------------------------- unsigned rot13_1(unsigned c) { if(c >= 'A' && c <= 'Z') return 'A' + ((c -'A') + 13)%26; __builtin_unreachable(); } unsigned rot13_2(unsigned c) { if (c >= 'A' && c <= 'M' ) return c + 13; else if (c >= 'N' && c <= 'Z' ) return c - 13; __builtin_unreachable(); } unsigned rot13_3(unsigned c) { if(c >= 'A' && c <= 'Z') return c + (c > 'Z' - 13 ? -13 : 13); __builtin_unreachable(); } unsigned rot13_4(unsigned c) { if(c >= 'A' && c <= 'Z') return c + 13 + (c > 'Z' - 13 ? -26 : 0); __builtin_unreachable(); } ------------------------------ rot13_1(unsigned int): lea edx, [rdi-52] mov rax, rdx imul rdx, rdx, 1321528399 shr rdx, 35 imul edx, edx, 26 sub eax, edx add eax, 65 ret rot13_2(unsigned int): lea edx, [rdi-65] lea eax, [rdi+13] sub edi, 13 cmp edx, 12 cmova eax, edi ret rot13_3(unsigned int): cmp edi, 78 sbb eax, eax and eax, 26 lea eax, [rax-13+rdi] ret rot13_4(unsigned int): cmp edi, 78 sbb eax, eax not eax and eax, -26 lea eax, [rax+13+rdi] ret