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

            Bug ID: 123762
           Summary: missed optimization for exact integer division by a
                    constant
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

Consider the following code.

unsigned long f (unsigned long i)
{
  if (i % 3)
    __builtin_unreachable ();
  return i / 3;
}

With gcc (Debian 20260121-1) 16.0.1 20260121 (experimental) [trunk
r16-6952-g14cd2833b27], using -O3, I get the following code on x86_64:

        movabsq $-6148914691236517205, %rax
        mulq    %rdi
        movq    %rdx, %rax
        shrq    %rax
        ret

However, since

  if (i % 3)
    __builtin_unreachable ();

ensures that i is a multiple of 3, the division by 3 could be done just by
multiplying by the inverse of 3 mod 2^64.
  • [Bug tree-optimization/123762] ... vincent-gcc at vinc17 dot net via Gcc-bugs

Reply via email to