Test code:

register unsigned char val asm("r4");

void negate(void)
{
    if (val)
        val = ~val;
    else
        val = ~val;
}

Code generated with -Os

.global negate
        .type   negate, @function
negate:
/* prologue: function */
/* frame size = 0 */
        tst r4
        breq .L2
        com r4
        ret
.L2:
        clr r4
        dec r4
        ret
        .size   negate, .-negate

In the "else" branch gcc knows that r4 is zero, and that it should change to
0xff - taking this approach generates longer code than in the first branch.

The same applies when operations like !, ++, -- are used.


-- 
           Summary: [avr] branch optimisation generates worse code
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: k dot kosciuszkiewicz+gcc at gmail dot com
 GCC build triplet: i386-linux-gnu
  GCC host triplet: i386-linux-gnu
GCC target triplet: avr-*-*


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38209

Reply via email to