[Top][All Lists]

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

Re: [avr-chat] Missed Optimisation ?

From: Graham Davies
Subject: Re: [avr-chat] Missed Optimisation ?
Date: Tue, 01 Mar 2011 07:52:55 -0500

bob wrote:

The code is very simple (runs in Port Change interrupt):

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

   result >>= 1;

Bob confirms (off-list) that variable 'result' is declared volatile. My opinion is that this is why the compiler must load and store all 32 bits. Bob, however, disagrees. So, maybe this is another discussion about the exact meaning and effect of the volatile qualifier.

In my opinion, the compiler is not permitted to optimize accesses to volatile variables, either load or store. We typically use the qualifier because these accesses have side effects (they diddle the hardware) or the variable is shared with another thread of execution (in this case, interrupt and non-interrupt code). But the compiler is at liberty to optimize the modification of the variable's value while it is in temporary store (on the stack or in registers). So, in this case the compiler figures out that modifying just the least significant byte will result in the same value as a full 32-bit OR. It's not until the value gets written back to the variable that it begins to matter again that the variable is volatile. The *value* is not volatile, only the variable.

So, I think that the compiler is doing exactly what it should do.

I assume that in the non-interrupt code, interrupts are disabled when accessing this variable, but that has nothing to do with the supposedly missed optimization.


reply via email to

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