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
  • You've broken your original code into pieces in a way the optimizer is already forbidden to do it, regardless of any usages of the "volatile" keyword.

    The two parts of the code that really exist are:

    	i = ADC
    and
    	i += 2;
    	write_do_DAC(i)

    The loop around this all has nothing to do with the effect of volatile, because there's one volatile variable (ADC) being used inside the loop.

    The optimizer is already forbidden to remove the call to write_to_DAC() by the simple fact that it's a function call. No volatile needed to explain that.

    The optimizer is forbidden to assume it can pre-compute the value of i to be passed to write_to_DAC because it's computed using a non-local, non-constant-qualified variable: ADC. It doesn't matter at all that ADC is qualified volatile, for that.

    In short: I think you're worrying too much. You really only need "volatile" to tell the compiler "Beware! There's stuff going on with this variable that you don't understand."

Reply
  • You've broken your original code into pieces in a way the optimizer is already forbidden to do it, regardless of any usages of the "volatile" keyword.

    The two parts of the code that really exist are:

    	i = ADC
    and
    	i += 2;
    	write_do_DAC(i)

    The loop around this all has nothing to do with the effect of volatile, because there's one volatile variable (ADC) being used inside the loop.

    The optimizer is already forbidden to remove the call to write_to_DAC() by the simple fact that it's a function call. No volatile needed to explain that.

    The optimizer is forbidden to assume it can pre-compute the value of i to be passed to write_to_DAC because it's computed using a non-local, non-constant-qualified variable: ADC. It doesn't matter at all that ADC is qualified volatile, for that.

    In short: I think you're worrying too much. You really only need "volatile" to tell the compiler "Beware! There's stuff going on with this variable that you don't understand."

Children
No data