https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91029
Bug ID: 91029 Summary: missed optimization regarding value of modulo operation Product: gcc Version: 9.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: bruno at clisp dot org Target Milestone: --- GCC's optimizers apparently don't know that, for b an integer >= 0, a % b > 0 implies that a >= 0 and a % b < 0 implies that a <= 0. Test case: ============= foo.c ============= int xx; void f (int i) { if ((i % 7) == 3) xx = (i < 0); } ================================= $ gcc -O2 -m32 -S foo.c && fgrep -v .cfi foo.s .file "foo.c" .text .p2align 4 .globl f .type f, @function f: .LFB0: movl 4(%esp), %ecx movl $-1840700269, %edx movl %ecx, %eax imull %edx movl %ecx, %eax sarl $31, %eax addl %ecx, %edx sarl $2, %edx subl %eax, %edx leal 0(,%edx,8), %eax subl %edx, %eax movl %ecx, %edx subl %eax, %edx cmpl $3, %edx je .L4 ret .p2align 4,,10 .p2align 3 .L4: shrl $31, %ecx movl %ecx, xx ret .LFE0: .size f, .-f .comm xx,4,4 .ident "GCC: (GNU) 9.1.0" .section .note.GNU-stack,"",@progbits The two instructions after .L4 could be optimized to movl $0, xx by observing that for i < 0, i % 7 is <= 0 and therefore never == 3.