public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/59174] New: [avr] Suboptimal multiplication when indexing an array
@ 2013-11-18 16:29 ambrop7 at gmail dot com
  0 siblings, 0 replies; only message in thread
From: ambrop7 at gmail dot com @ 2013-11-18 16:29 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59174

            Bug ID: 59174
           Summary: [avr] Suboptimal multiplication when indexing an array
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ambrop7 at gmail dot com

When indexing an array, gcc will sometimes perform a multiplication using
shifts and adds, instead of using the more efficient multiplication
instructions. This program demonstrates the issue.

#include <stdint.h>
struct Foo { char a[12]; } foo[10];
struct Foo * test (uint8_t i) { return &foo[i]; }

Compile with:
avr-gcc -mmcu=atmega2560 -O2 -S test.c -o -

Result:

test:
        mov r18,r24
        ldi r19,0
        movw r24,r18
        lsl r24
        rol r25
        add r24,r18
        adc r25,r19
        lsl r24
        rol r25
        lsl r24
        rol r25
        subi r24,lo8(-(foo))
        sbci r25,hi8(-(foo))
        ret

On the other hand, if the struct size is changed to 13, much more efficient
assembly is produced:

test:
        ldi r18,lo8(13)
        mul r24,r18
        movw r24,r0
        clr __zero_reg__
        subi r24,lo8(-(foo))
        sbci r25,hi8(-(foo))
        ret

The first assembly takes 13 cycles, the second just 7. I don't see any reason
why the second assembly wouldn't work for size 12.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2013-11-18 16:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-18 16:29 [Bug target/59174] New: [avr] Suboptimal multiplication when indexing an array ambrop7 at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).