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

            Bug ID: 113105
           Summary: Missing optimzation: fold `div(v, a) * b + rem(v, a)`
                    to `div(v, a) * (b - a) + v`
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xxs_chy at outlook dot com
  Target Milestone: ---

Godbolt example: https://godbolt.org/z/b5va37Tzx

For example:

unsigned char _bin2bcd(unsigned val)
{
        return ((val / 10) << 4) + val % 10;
}

can be folded to:

unsigned char new_bin2bcd(unsigned val)
{
        return val / 10 * 6 + val;
}

This C snippet is extracted from
https://github.com/torvalds/linux/blob/master/lib/bcd.c

Both GCC and LLVM missed it.

Reply via email to