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

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

Re: [avr-gcc-list] Optimization - what is bad on this code ?


From: Ruud Vlaming
Subject: Re: [avr-gcc-list] Optimization - what is bad on this code ?
Date: Sun, 4 Oct 2009 22:59:21 +0200
User-agent: KMail/1.9.1

On Sunday 04 October 2009 22:34, Carl Hamilton wrote:
> I can't say why the compiler has thrown out their bodies (I haven't looked
> at the code that closely), but the "while" loops in both functions will
> never exit. 
Well that is enough reason for the compiler to throw away the bodies.
If a function never exits, all code below can never be reached, so is 
removed. The peice above is local stuff, and, according to the compiler,
not able to have any side effect to the rest of the code, and can
therefore be deleted ass well. What remains is an infinite loop .... 

I want to point out an interresting point however. Probably the idea
of Vaclav was, that the loop will terminate when icnt reaches 0x00, 
subsequently icnt--; will make this 0xFF, and while(icnt+1) will
turn this into while(0x00) again, thereby terminating the loop.

This works, IF you use 8 bit arithmetic. So if you compile with
-mint8, the body will stay probably there. But standard C uses 16 bit 
arithmetic and the world is different. All chars are promoted to
16 bit before the operators act and casted afterwards. The 8
bit rollover trick does not work in 16 bit wide arithmetic.
The compiler 'knows' this, and therefore correctly concludes
as you mentioned.

Instead of -mint8, it is also possible to change 
  unsigned char icnt = 4;
into 
   unsigned int icnt = 4;
and now your argument is not valid anymore, since 
in 16 bit the rollever trick still works. Thererfore i expect
that the compiler will not remove the code after this change,
even though "icnt+1" seems always be true.

Give it a try.

Ruud




> Since you have declared "icnt" as unsigned, "icnt + 1" will 
> always be true. I'm surprised the the compiler didn't issue a warning along
> these lines. What warning level are you compiling with?
>  - Carl
> 
> On Sun, Oct 4, 2009 at 12:33 PM, Vaclav Peroutka <address@hidden> wrote:
> 
> > Hello all,
> >
> > I am sorry, but maybe somebody has expierience why the following code is
> > deleted during optimization. I have two functions for printing of numbers
> > (uart_puts() just send string to UART) :
> > void uart_putux( unsigned int aInt)
> > {
> >  unsigned char buf[5];
> >  unsigned char icnt = 4;
> >
> >  buf[icnt--] = 0x00;
> >  while(icnt+1) {
> >    buf[icnt] = (aInt&0x0f) + 0x30;
> >    if (buf[icnt] > 0x39) {
> >      buf[icnt] += 0x07;
> >    }
> >    aInt >>= 4;
> >    icnt--;
> >  }
> >  uart_puts( buf);
> > }
> >
> > void uart_putlx( unsigned long aInt)
> > {
> >  unsigned char buf[9];
> >  unsigned char icnt = 8;
> >
> >  buf[icnt--] = 0x00;
> >  while(icnt+1) {
> >    buf[icnt] = (aInt&0x0f) + 0x30;
> >    if (buf[icnt] > 0x39) {
> >      buf[icnt] += 0x07;
> >    }
> >    aInt >>= 4;
> >    icnt--;
> >  }
> >  uart_puts( buf);
> > }
> >
> > After I compile and link the code, in .LST I see following:
> > void uart_putux( unsigned int aInt)
> > {
> >  35a:   ff cf           rjmp    .-2             ; 0x35a <uart_putux>
> >
> > 0000035c <uart_putlx>:
> >  }
> >  uart_puts( buf);
> > }
> >
> > void uart_putlx( unsigned long aInt)
> > {
> >  35c:   ff cf           rjmp    .-2             ; 0x35c <uart_putlx>
> >
> > For compilation I use following:
> > /opt/avr-gcc.4.3.4/bin/avr-gcc -g -mmcu=atmega16 -Wall -Wstrict-prototypes
> > -Os -mcall-prologues -I /opt/avr-gcc.4.3.4/bin/include
> > -fno-inline-small-functions -fno-reorder-blocks
> > -fno-reorder-blocks-and-partition -fno-reorder-functions
> > -fno-toplevel-reorder -fno-move-loop-invariants   -c -o hal.o hal.c
> >
> > If I remove "-Os", the code is there. But if I understand correctly, gcc
> > sees the code unusable. What is bad inside ? The code worked in older
> > avr-gcc...
> >
> > Thank you for help,
> > Vaclav
> >
> >
> > _______________________________________________
> > AVR-GCC-list mailing list
> > address@hidden
> > http://lists.nongnu.org/mailman/listinfo/avr-gcc-list
> >
> 




reply via email to

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