avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Optimization Problem?


From: Thomas D. Dean
Subject: [avr-gcc-list] Optimization Problem?
Date: Wed, 29 Dec 2010 11:48:41 -0800

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...
  }
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]