3

If i declare a variable as volatile and if I dont use it any where in the program, will the compiler optimize that variable ?

What in case of local and global declarations of volatile variables in this case?

tq.

Lakshman
  • 33
  • 3
  • 3
    To emphasize Jim Balter's comment below "The volatile keyword is irrelevant to whether the compiler allocates storage for unused variables.". What makes you think `volatile` would impact the compiler output? – pascal Feb 08 '11 at 14:06

3 Answers3

7

The compiler can, and probably will, eliminate (ignore) the unused volatile variable declaration (but the compiler cannot eliminate an unused global variable definition - it has to assume that some other translation unit (TU) will reference it).

If the variable is local to the function and unused, it can be eliminated by the compiler, regardless of its volatility. It is not clear you can have meaningful local volatile variables, though I suppose you could call a function that passes its address to some code that then arranges for an interrupt handler to write to that variable - achieving volatility (but it is then, clearly, a used variable).

The volatile qualifier controls (influences) how the compiler generates the code that accesses the variable - if the code does not access the variable, there is no need to alter the code that it generates except to avoid generating a reference to the variable. It may as well not exist.


Further thoughts:

If the variable is static volatile and unreferenced in the source code, can it be eliminated?

The answer is (almost certainly) yes. There is no reference to the variable in the source, and the only ways it can be accessed in a portable manner require it to be referenced. Possible unportable hacks would include having multiple such static variables defined, and passing a reference to one of them to some function, and that function then expects to be able to access the other variables by address manipulation. However, such code is at best gruesome and non-portable. The author of such should probably be taken out back somewhere and quietly dissuaded from writing such code again.

So, a global variable definition cannot be eliminated; it might be referenced from another TU. A static variable definition that is unused can be eliminated. A local variable definition that is unused can be eliminated. And this applies whether the variable in question has a volatile qualifier or not.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    I wonder how a compiler can do that. It compiles only one unit at once, and if the variable isn't declared static, it can't know if it's used elsewhere. This looks more like a linker's job. – Sergei Tachenov Feb 08 '11 at 13:31
  • @Sergey: Did the questioner mean 'declare' - as in `extern volatile int variable;` or 'define' - as in `volatile int variable = 0;` (unless something behind the compiler's back changed the value from 0 to something else)? I casually assumed 'declare' meant 'declare'. You're correct; the compiler cannot eliminate a global variable definition - it can ignore an unused variable declaration. – Jonathan Leffler Feb 08 '11 at 14:38
  • @Jonathan, good point here. I haven't noticed that, but if the question really meant to say "declare", then I don't even understand its meaning. What could "optimizing" a declaration mean? You can optimize away a variable definition in the sense of storage usage, but declaration is purely a compile-time thing, what's there to optimize? – Sergei Tachenov Feb 08 '11 at 14:53
  • 1
    "It is not clear you can have meaningful local volatile variables": loop counters used in `for` loop delays - typically in embedded systems - must be `volatile`. For example in `volatile int i; for (i=100; i>=0; --i) {}`. The `for` loop has no logical meaning, but must not be optimized away. The optimizer would do that if it wasn't for `volatile`. (but it is then a used variable). – Gauthier Feb 08 '11 at 15:24
  • @Gauthier: interesting observation. The variable is not volatile in the sense that it will change without the compiler's knowledge, but by specifying that it is volatile, you force the compiler to generate the loop (it must treat it as if something could change it). Thanks! – Jonathan Leffler Feb 08 '11 at 15:43
  • 1
    Also volatile is used to assure restoration of local variables in a routine containing setjmp after longjmp is called. – Jim Balter Feb 08 '11 at 15:46
  • 1
    "The variable is not volatile in the sense that it will change without the compiler's knowledge" -- that's not what volatile is about. It puts limitations on the compiler's ability to do "as if" transformations; those limitations are spelled out in the C standard. – Jim Balter Feb 08 '11 at 15:46
3

volatile is irrelevant to storage allocation -- if the compiler eliminates an unused variable without the volatile keyword, it can and probably will eliminate it with the volatile keyword. If you want to know for sure, check the generated code or symbol table.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
-1

If a variable is not used, that is the most optimum situation of all. Optimizations are done only in the case of operations and calculations.

If no operations are performed on the data, no optimization is needed.

Arjun J Rao
  • 925
  • 1
  • 10
  • 25
  • Compilers can also optimize for program size. Getting rid of an unused variable is an optimization. – Fred Foo Feb 08 '11 at 13:16
  • This answer is largely correct and did not deserve downrating. The fact is that the C standard only specifies how volatile constrains the compiler's semantic model; it says nothing about allocation of storage. The volatile keyword is irrelevant to whether the compiler allocates storage for unused variables. – Jim Balter Feb 08 '11 at 14:03