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

            Bug ID: 91072
           Summary: does not reduce the size of a division by a constant
                    on non-negative int / small unsigned long constant
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

The generated division by a constant is suboptimal when its size could be
reduced, e.g. on non-negative int divided by a small unsigned long constant
(which fits in an int). Consider the following example (the i <= 0 condition is
just there to have a similar source, otherwise this could just be i < 0 when i
is signed).

int f1 (unsigned int i)
{
  return i <= 0 ? 0 : i / 3UL;
}

int f2 (int i)
{
  return i <= 0 ? 0 : i / 3UL;
}

int f3 (int i)
{
  return i <= 0 ? 0 : i / 3L;
}

With GCC 8.3.0 on x86_64, using -O3, f1 and f3 both use a 32-bit multiplication
(mull / imull) by a 32-bit constant, but f2 uses a 64-bit multiplication (mulq)
by a 64-bit constant.

With gcc (Debian 20190628-1) 10.0.0 20190628 (experimental) [trunk revision
272790], the only change is that f1 and f2 both use imulq (but still with a
32-bit constant). Wouldn't this be slower?

On a 64-bit PowerPC, this is similar with GCC 8.3.1 (mulhwu for f1 and f3,
mulhdu for f2).

Reply via email to