https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115392
Bug ID: 115392 Summary: Missed optimization: fold `3 / (b ^ a)` to `b < 4` Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: zhiwuyazhe154 at gmail dot com Target Milestone: --- Godbolt example: https://godbolt.org/z/7YE4Eq9a7 Code example: bool m; bool a; void fn1(unsigned int b) { if((b^a)!=0) m = 3 / (b ^ a); // equals to `m = b < 4` } Since the range of a is {0,1}, we can discuss it in different cases: When a is 0, b^a = b^0 = b; When a is 1, b^a = b^1 = {b, b is an even number; b - 1, b is an odd number} In summary, b^a is less than or equal to 3 only when b < 4 GCC -O3: fn1(unsigned int): movzx eax, BYTE PTR a[rip] cmp eax, edi je .L1 xor eax, edi cmp eax, 3 setbe BYTE PTR m[rip] .L1: ret a: .zero 1 m: .zero 1 Expected code(CLANG -O3): fn1(unsigned int): movzx eax, byte ptr [rip + a] cmp eax, edi je .LBB0_2 cmp edi, 4 setb byte ptr [rip + m] .LBB0_2: ret m: .byte 0 a: .byte 0