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

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

RE: [avr-gcc-list] Code size optimization


From: Dave Hansen
Subject: RE: [avr-gcc-list] Code size optimization
Date: Mon, 10 Jan 2005 15:35:41 -0500

From: Antony MATRANGA <address@hidden>
[...]
The compiler is smart enough to insert RET instruction when it's usefull
but not enough when it's useless.

see:
void lcd_interf_RESET (BOOL Level)
{
  if (Level)
    1eac:       88 23           and     r24, r24
    1eae:       11 f0           breq    .+4             ; 0x1eb4
  {
    PORTB |= _BV(PB1);
    1eb0:       29 9a           sbi     0x05, 1 ; 5
    1eb2:       08 95           ret
  }
  else
  {
    PORTB &= ~_BV(PB1);
    1eb4:       29 98           cbi     0x05, 1 ; 5
    1eb6:       08 95           ret
    1eb8:       08 95           ret
  }
}

compiling on :
avr-gcc -gdwarf-2 -Wall -O2 -mmcu=atmega168 -fshort-enums -mtiny-stack -c -o
lcd_interf.o lcd_interf.c

In this case I think the last RET should be removed.
I a 8k application, i'm loosing 12 bytes. And one byte is one byte

I think your'e using the wrong optimization level. I created a small test file from your example:

--- begin included file ---

  C:\Dave>type opt.c
  #include <avr/io.h>
  typedef unsigned char BOOL;

  void lcd_interf_RESET (BOOL Level)
  {
    if (Level)
    {
      PORTB |= _BV(PB1);
    }
    else
    {
      PORTB &= ~_BV(PB1);
    }
  }

  C:\Dave>avr-gcc -mmcu=atmega16 -Os -S opt.c

  C:\Dave>type opt.s
          .file   "opt.c"
          .arch atmega16
  __SREG__ = 0x3f
  __SP_H__ = 0x3e
  __SP_L__ = 0x3d
  __tmp_reg__ = 0
  __zero_reg__ = 1
          .global __do_copy_data
          .global __do_clear_bss
          .text
  .global lcd_interf_RESET
          .type   lcd_interf_RESET, @function
  lcd_interf_RESET:
  /* prologue: frame size=0 */
  /* prologue end (size=0) */
          tst r24
          breq .L2
          sbi 56-0x20,1
          ret
  .L2:
          cbi 56-0x20,1
          ret
  /* epilogue: frame size=0 */
  /* epilogue: noreturn */
  /* epilogue end (size=0) */
  /* function lcd_interf_RESET size 6 (6) */
          .size   lcd_interf_RESET, .-lcd_interf_RESET
/* File "opt.c": code 6 = 0x0006 ( 6), prologues 0, epilogues 0 */

  C:\Dave>avr-gcc -v
  Reading specs from C:/WinAVR/bin/../lib/gcc/avr/3.4.1/specs
Configured with: ../gcc-3.4.1/configure --prefix=e:/avrdev/install --build=mingw
  32 --host=mingw32 --target=avr --enable-languages=c,c++
  Thread model: single
  gcc version 3.4.1

  C:\Dave>

--- end included file ---

The extraneous "ret" is gone.  HTH,
  -=Dave




reply via email to

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