https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127080
Bug ID: 127080
Summary: Possible Missed Optimization at O3
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: apowenq at gmail dot com
Target Milestone: ---
cat prog.c
unsigned src(unsigned v0_u8, unsigned v1_u8) {
if (!((4 <= v0_u8) && (v0_u8 <= 5))) __builtin_unreachable();
if (!((3 <= v1_u8) && (v1_u8 <= 4))) __builtin_unreachable();
unsigned i1_u8 = v0_u8 % v1_u8;
return i1_u8;
}
GCC trunk at O3 cannot optimize the C program, while LLVM trunk at O3 can.
Pattern: y < x < 2y. x % y => x - y
Reproducer: https://godbolt.org/z/d1ao3avYE
GCC at O3:
"src":
mov eax, edi
xor edx, edx
div esi
mov eax, edx
ret
LLVM at O3:
src:
mov eax, edi
sub eax, esi
ret