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

            Bug ID: 124471
           Summary: [AVR] Counterproductive byte copy optimization by
                    multiplication
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dimich.dmb at gmail dot com
  Target Milestone: ---

Simple function to return 16-bit word containing the same low and high byte:

uint16_t clone(uint8_t n)
{
    return n | (n << 8);
}

with "-Wall -Os -mmcu=atmega328p" compiles to 9 instructions:

<clone>:
        21 e0           ldi     r18, 0x01       ; 1
        31 e0           ldi     r19, 0x01       ; 1
        48 2f           mov     r20, r24
        42 9f           mul     r20, r18
        c0 01           movw    r24, r0
        43 9f           mul     r20, r19
        90 0d           add     r25, r0
        11 24           eor     r1, r1
        08 95           ret

Moreover, for targets without hardware multiplier software multiplication
invoked. E.g. with "-mmcu=attiny13a":

<clone>:
        90 e0           ldi     r25, 0x00       ; 0
        61 e0           ldi     r22, 0x01       ; 1
        71 e0           ldi     r23, 0x01       ; 1
        09 d0           rcall   .+18            ; 0x<__mulhi3>
        08 95           ret

5 instructions + 17 instructions of __mulhi3.

Older GCC <= 11.1.0 compile the same function to

<clone>:
        98 2f           mov     r25, r24
        08 95           ret

The issue observed at least since 12.1.0.

$ avr-gcc -v
Using built-in specs.
Reading specs from /usr/lib/gcc/avr/15.2.0/device-specs/specs-avr2
COLLECT_GCC=avr-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/avr/15.2.0/lto-wrapper
Target: avr
Configured with: /home/dimich/work/src/avr/avr-gcc.pkg/src/gcc-15.2.0/configure
--disable-install-libiberty --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-linker-build-id --disable-nls
--disable-werror --disable-__cxa_atexit --enable-checking=release
--enable-clocale=gnu --enable-gnu-unique-object --enable-gold
--enable-languages=c,c++ --enable-ld=default --enable-lto --enable-plugin
--enable-shared --infodir=/usr/share/info --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --prefix=/usr --target=avr
--with-as=/usr/bin/avr-as --with-gnu-as --with-gnu-ld --with-ld=/usr/bin/avr-ld
--with-plugin-ld=ld.gold --with-system-zlib --with-isl
--enable-gnu-indirect-function
Thread model: single
Supported LTO compression algorithms: zlib zstd
gcc version 15.2.0 (GCC)

System: Arch Linux x86_64

Reply via email to