avr-chat
[Top][All Lists]
Advanced

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

RE: [avr-chat] Missed Optimisation ?


From: Boyapati, Anitha
Subject: RE: [avr-chat] Missed Optimisation ?
Date: Wed, 2 Mar 2011 13:04:04 +0800

>I have a very time-critcal routine where a 32-bit value is read serially
>(bit-shift).
>The code is very simple (runs in Port Change interrupt):
>
>    if ((PINA & _BV(A2DDATA)) != 0)
>       result |= 0x80000000;
>
>    result >>= 1;
>
>The disassembly of the OR  however is:
>
>
>    if ((PINA & _BV(A2DDATA)) != 0)
> 622:  01 9b           sbis    0x00, 1 ; 0
> 624:  11 c0           rjmp    .+34            ; 0x648 <__vector_4+0x42>
>
>       result |= 0x80000000;
> 626:  80 91 5a 02     lds     r24, 0x025A
> 62a:  90 91 5b 02     lds     r25, 0x025B
> 62e:  a0 91 5c 02     lds     r26, 0x025C
> 632:  b0 91 5d 02     lds     r27, 0x025D
> 636:  b0 68           ori     r27, 0x80       ; 128
> 638:  80 93 5a 02     sts     0x025A, r24
> 63c:  90 93 5b 02     sts     0x025B, r25
> 640:  a0 93 5c 02     sts     0x025C, r26
> 644:  b0 93 5d 02     sts     0x025D, r27
>
>The compiler has loaded all 4 bytes of the uint32_t, but only operated
>on one of them, so it seems 'clever' enough to see that the immediate
>value is full of '0' and not bother to OR the lower bytes but still
>loads and saves them.
...

Unless I am missing something in this conversation, I think it is the 
right-hand side value (rvalue) that should be considered while doing an 'OR' 
operation. Since it is all zeros, compiler optimized it away.

If we change the value from 80000000 to some other value and valid code is 
generated. For e.g.,

if ((PINA & _BV(A2DDATA)) != 0)
        result |= 0x80000001;

result |= 0x80000001;
0000005A  LDD R24,Y+1           Load indirect with displacement 
0000005B  LDD R25,Y+2           Load indirect with displacement 
0000005C  LDD R26,Y+3           Load indirect with displacement 
0000005D  LDD R27,Y+4           Load indirect with displacement 
0000005E  ORI R24,0x01          Logical OR with immediate 
0000005F  ORI R27,0x80          Logical OR with immediate 
00000060  STD Y+1,R24           Store indirect with displacement 
00000061  STD Y+2,R25           Store indirect with displacement 
00000062  STD Y+3,R26           Store indirect with displacement 
00000063  STD Y+4,R27           Store indirect with displacement


The above code was generated with -O2. (I have taken a quick glance at the 
remaining conversations on this thread. I think nothing was discussed about the 
rvalue)

Anitha



reply via email to

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