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

            Bug ID: 129143
           Summary: [m68k] callee-saved register and extra move for
                    reassociated idx * 32 + (31 - __builtin_clzl (w))
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shingekinocore at gmail dot com
  Target Milestone: ---

Target: m68k-linux

unsigned long f(unsigned long idx, unsigned long w)
{
        return idx * 32 + (31 - __builtin_clzl(w));
}

GCC 16.2.0, -O2 -m68040 -fomit-frame-pointer:

        movel %d2,%sp@-
        bfffo %sp@(12),0,0,%d0
        movel %sp@(8),%d1
        lsll #5,%d1
        moveq #31,%d2
        addl %d2,%d1
        subl %d0,%d1
        movel %d1,%d0
        movel %sp@+,%d2
        rts

GCC 16.2.0, -O2 -m68040 -fomit-frame-pointer -fno-tree-reassoc:

        bfffo %sp@(8),0,0,%d1
        movel %sp@(4),%d0
        lsll #5,%d0
        subl %d1,%d0
        moveq #31,%d1
        addl %d1,%d0
        rts

The default listing has three instructions more than the
-fno-tree-reassoc listing: the push and the pop of the callee-saved
%d2, which holds the constant 31, and the final copy from %d1 to %d0.
In the optimized dump, the default build computes (idx * 32 + 31) -
clz, and the -fno-tree-reassoc build (idx * 32 - clz) + 31.

Instructions of f, rts included (-O2 -m68040 -fomit-frame-pointer; -Os
gives the same counts):

                                     default   -fno-tree-reassoc
  GCC 8.3, 9.5, 10.5, 11.5              9              7
  GCC 12.5, 13.5, 14.4, 15.3, 16.2     10              7
  master (e8ec1a6b68e6, 2026-11-25)    10              7

GCC 8.3, 9.5, 10.5 and 11.5 emit the push and the pop but not the
final copy.

Expected: 7 instructions with reassoc as well.

Compilers: the releases are the prebuilt cross compilers from
https://mirrors.edge.kernel.org/pub/tools/crosstool/ (aarch64 host);
master was configured with --target=m68k-linux --enable-languages=c.

The expression is the return value of _find_last_bit() in the Linux
kernel (lib/find_bit.c) with the kernel's generic __fls().
  • [Bug target/129143] New: [m68... shingekinocore at gmail dot com via Gcc-bugs

Reply via email to