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.