https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127992

            Bug ID: 127992
           Summary: [Missed Optimization] x/(x*x) => 1 when x=1 otherwise
                    0 for unsigned x
           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: ---

reproducer: https://godbolt.org/z/5oMP46MPx

$cat prog.c
unsigned src(unsigned v2) {
  if (!((1 <= v2) && (v2 <= 65535))) __builtin_unreachable();
  unsigned i1 = v2 * v2;
  unsigned i2 = v2 / i1;
  return i2;
}

GCC at O3:
"src":
        mov     ecx, edi
        mov     eax, edi
        xor     edx, edx
        imul    ecx, edi
        div     ecx
        ret

LLVM at O3:
src:
        xor     eax, eax
        cmp     edi, 1
        sete    al
        ret

Reply via email to