This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with optimisation of volatiles

Hi there All,

I have a problem which seemed to be defying explanation, but I have come up with a theory. Could I possibly have some feedback on whether the following is likely, plausible, possible, untrue or downright rubbish?

If one reads the contents of a CAN or ADC chip register at a particular address, then the label volatile is placed upon that address to prevent the compiler optimising out repeat readings of the address. If one reads the contents of the address into a variable, then the compiler would automatically treat the contents of this variable with similar care.

Is it possible that there has been an oversight with statements where the contents of a variable depend on the contents of a volatile by way of an if statement, ie...

normal_var=volatile_var;

...is correctly optimised, but...

normal_var=voltile_var;
if (normal_var=0x00)
   {
   another_normal_var+=1;
   }

...is not correctly optimised all of the time, dependant on the surrounding code, unless normal_var itself is declared to be volatile?

For info - am using optimisation level...

OPTIMIZE(3,SPEED)

...and am using version...

C166 COMPILER V4.11

Any thoughts, or is any or all of the above thoughts and understanding way off the mark?


Yours (grateful for any input),


Richard.

Parents
  • A couple of things occur to me here:

    1) I believe that sbit variables are implicitly volatile, although I've had a look in the manual and cannot seem to find anything to back that up. Maybe plain 'bit' variables are also considered volatile?

    2) The compiler would have no difficulty seeing that global_refresh is modified both inside the ISR and in main(). Perhaps it automatically suppresses optimisation in cases like this?


    Optimisation is entirely optional for the compiler.

    In practice, writes to global variables tend not to be optimised out because, in general, it is difficult for the compiler to keep track of where else a variable might be read.

    However, reads of global variables is another matter. In this case SV's code works because it is simply fortunate that the compiler chooses to read the value of global_refresh from memory rather than keeping a copy in a register. With this processor there is probably no benefit in keeping a local copy of a bit, but if global_refresh were to be changed to a char then the code may fail to work as expected. Worse still, the code might work for now, but fail when the compiler is updated!

Reply
  • A couple of things occur to me here:

    1) I believe that sbit variables are implicitly volatile, although I've had a look in the manual and cannot seem to find anything to back that up. Maybe plain 'bit' variables are also considered volatile?

    2) The compiler would have no difficulty seeing that global_refresh is modified both inside the ISR and in main(). Perhaps it automatically suppresses optimisation in cases like this?


    Optimisation is entirely optional for the compiler.

    In practice, writes to global variables tend not to be optimised out because, in general, it is difficult for the compiler to keep track of where else a variable might be read.

    However, reads of global variables is another matter. In this case SV's code works because it is simply fortunate that the compiler chooses to read the value of global_refresh from memory rather than keeping a copy in a register. With this processor there is probably no benefit in keeping a local copy of a bit, but if global_refresh were to be changed to a char then the code may fail to work as expected. Worse still, the code might work for now, but fail when the compiler is updated!

Children
No data