Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 29, 2010 19:48:52

Thomas D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] Optimization Problem?


I think something strange is being produced by -Os
I reduced a confusing large application to the below code.

Compiling with default optimizaton produced an avr-objdump that follows
the C code as I expected.

> avr-gcc -mmcu=atmega16 -c -o err.o err.c
> avr-gcc -mmcu=atmega16 err.o -o err.elf -lm -lc
> avr-objdump -d err.elf
...
000000fa <use_libc>:
fa: ef 92 push r14
fc: ff 92 push r15
fe: 0f 93 push r16
100: 1f 93 push r17
102: df 93 push r29
104: cf 93 push r28
106: cd b7 in r28, 0x3d ; 61
108: de b7 in r29, 0x3e ; 62
10a: 68 97 sbiw r28, 0x18 ; 24
10c: 0f b6 in r0, 0x3f ; 63
10e: f8 94 cli
110: de bf out 0x3e, r29 ; 62
112: 0f be out 0x3f, r0 ; 63
114: cd bf out 0x3d, r28 ; 61
116: 89 e1 ldi r24, 0x19 ; 25
118: 94 e0 ldi r25, 0x04 ; 4
11a: ae e9 ldi r26, 0x9E ; 158
11c: bf e3 ldi r27, 0x3F ; 63
...

However, using -Os, I see strange code at c8, ca, etc. The poly
calculation and the sin call seem to be below here. The stack is
cleaned correctly by the 4 ea pop r0's. Why the strange rcalls???

> avr-gcc -Os -mmcu=atmega16 -c -o err.o err.c
> avr-gcc -Os -mmcu=atmega16 err.o -o err.elf -lm -lc
> avr-objdump -d err.elf
...
000000b8 <use_libc>:
b8: 8f 92 push r8
ba: 9f 92 push r9
bc: af 92 push r10
be: bf 92 push r11
c0: cf 92 push r12
c2: df 92 push r13
c4: ef 92 push r14
c6: ff 92 push r15
c8: 0f 93 push r16
ca: 1f 93 push r17
cc: df 93 push r29
ce: cf 93 push r28
d0: 00 d0 rcall .+0 ; 0xd2 <use_libc+0x1a>
d2: 00 d0 rcall .+0 ; 0xd4 <use_libc+0x1c>
d4: cd b7 in r28, 0x3d ; 61
d6: de b7 in r29, 0x3e ; 62
d8: 0f 2e mov r0, r31
da: f0 e0 ldi r31, 0x00 ; 0
...

tomdean

// optimization problem?????
//
// 20101228 tomdean - initial version

////////////////////////////////////////////////////
// includes
#include <avr/io.h>
#include <math.h>

////////////////////////////////////////////////////
// prototypes
void use_libc(void);

////////////////////////////////////////////////////
// initialize
void initialize(void) {
DDRB |= (_BV(0) | _BV(1) | _BV(4) | _BV(5) | _BV(7));
}

////////////////////////////////////////////////////
// main
int main() {
initialize();
while (1) {
PORTB |= _BV(0);
use_libc();
PORTB &= ~_BV(0);
}

return 0; // never get here
}

////////////////////////////////////////////////////
// prototypes
void use_libc(void);

////////////////////////////////////////////////////
// use-libc
void use_libc(void) {
float x,y,z;
float a = 1.2345;
float b = 4.98778;
float c = 123.9876554;

// 200 iterations
for (x=-1.0; x<= 1.0; x+=0.01) {
// do a poly
y = a*x*x + b*x + c;
// some trig
z=sin(y);
PORTA = *(unsigned char *)&z; // use it so not optmized away...
}
}



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#2 Dec. 29, 2010 19:54:56

Thomas D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] Optimization Problem?


On Wed, 2010-12-29 at 11:48 -0800, Thomas D. Dean wrote:

>
> However, using -Os, I see strange code at c8, ca, etc. The poly
> calculation and the sin call seem to be below here. The stack is
> cleaned correctly by the 4 ea pop r0's. Why the strange rcalls???
>
Sorry, should read d0, d2.
tomdean


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#3 Dec. 29, 2010 19:56:43

Thomas D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] Optimization Problem?


On Wed, 2010-12-29 at 11:48 -0800, Thomas D. Dean wrote:
Sorry, again. I am really doing good with this post.

>avr-gcc --version
avr-gcc (GCC) 4.3.4
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

>uname -a
Linux asus 2.6.32-26-generic-pae #48-Ubuntu SMP Wed Nov 24 10:31:20 UTC
2010 i686 GNU/Linux

tomdean


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#4 Dec. 29, 2010 21:02:27

larry b.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] Optimization Problem?


Strange rcalls are a fast way to allocate four bytes on the stack for a local...

-----Original Message-----
From: avr-gcc-list-bounces+larry=barello.***@*ongnu.org
On Behalf Of Thomas
D. Dean
Sent: Wednesday, December 29, 2010 11:56 AM
To: avr-gcc-l***@*ongnu.org
Subject: Re: Optimization Problem?

On Wed, 2010-12-29 at 11:48 -0800, Thomas D. Dean wrote:
Sorry, again. I am really doing good with this post.

>avr-gcc --version
avr-gcc (GCC) 4.3.4
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

>uname -a
Linux asus 2.6.32-26-generic-pae #48-Ubuntu SMP Wed Nov 24 10:31:20 UTC
2010 i686 GNU/Linux

tomdean


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-l***@*ongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#5 Dec. 29, 2010 21:26:31

Thomas D.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] Optimization Problem?


On Wed, 2010-12-29 at 13:01 -0800, larry barello wrote:
> Strange rcalls are a fast way to allocate four bytes on the stack for a
> local...

Thanks, Larry.

Looking thru the code, that is what is happening.

tomdean


_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#6 Dec. 31, 2010 05:53:02

Boyapati, A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[avr-gcc-list] Optimization Problem?


>-----Original Message-----
>From: avr-gcc-list-bounces+anitha.boyapati=atmel.***@*ongnu.org
> On
>Behalf Of larry barello
>Sent: Thursday, December 30, 2010 2:31 AM
>To: tomd***@*peakeasy.org; avr-gcc-l***@*ongnu.org
>Subject: RE: Optimization Problem?
>
>Strange rcalls are a fast way to allocate four bytes on the stack for a
>local...
>

Thanks for explanation Tom Dean. I have been wondering about this for some time
although it never struck me as an optimization change.http://lists.nongnu.org/archive/html/avr-libc-dev/2006-06/msg00027.htmlseems to discuss more about this.


Anitha

>-----Original Message-----
>From: avr-gcc-list-bounces+larry=barello.***@*ongnu.org On Behalf Of Thomas D. Dean
>Sent: Wednesday, December 29, 2010 11:56 AM
>To: avr-gcc-l***@*ongnu.org
>Subject: Re: Optimization Problem?
>
>On Wed, 2010-12-29 at 11:48 -0800, Thomas D. Dean wrote:
>Sorry, again. I am really doing good with this post.
>
>>avr-gcc --version
>avr-gcc (GCC) 4.3.4
>Copyright (C) 2008 Free Software Foundation, Inc.
>This is free software; see the source for copying conditions. There is
>NO
>warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
>PURPOSE.
>
>>uname -a
>Linux asus 2.6.32-26-generic-pae #48-Ubuntu SMP Wed Nov 24 10:31:20 UTC
>2010 i686 GNU/Linux
>
>tomdean
>
>
>_______________________________________________
>AVR-GCC-list mailing list
>AVR-GCC-l***@*ongnu.org
>http://lists.nongnu.org/mailman/listinfo/avr-gcc-list>
>
>
>_______________________________________________
>AVR-GCC-list mailing list
>AVR-GCC-l***@*ongnu.org
>http://lists.nongnu.org/mailman/listinfo/avr-gcc-list_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

Board footer

Moderator control

Enjoy the 15th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support