This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC warnings for unused global variables


prj@po.cwru.edu (Paul Jarc) writes:

| Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
| > prj@po.cwru.edu (Paul Jarc) writes:
| >| Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
| >|> Fergus Henderson <fjh@cs.mu.OZ.AU> writes:
| >|>        [#3] In the abstract machine, all expressions are  evaluated
| >|>        as  specified  by  the  semantics.  An actual implementation
| >|>        need not evaluate part of an expression  if  it  can  deduce
| >|>        that  its  value is not used and that no needed side effects
| >                                                ^^^^^^^^^
| >|>        are produced (including any caused by calling a function  or
| >|>        accessing a volatile object).
| >|
| >| Accessing a volatile object includes writing, and writing includes
| >| initializing.  So the initialization cannot be optimized out.
| >
| > Why?
| 
| Because, since the object is volatile, it may affect the program in
| ways unknown to the compiler.

This is my favorite peeve against the definition of "volatile".
Does an alpha ray fired from Centauri count as a "way unknown to the
compiler"?  From the C definition, there is nothing that preclude that.  
Then, the compiler can pretend that ways unknown to it affected the
behaviour which is why you're seeing what you're seeing.   

No, I'm not proposing that as an imlementation for GCC.  However, it
is quite a valid interpretation, which shows why the meaning of
"volatile" is fuzzy. 

| > When we request the compiler to optimize a program we are requesting
| > it to transform the program into another one with equivalent
| > semantics, according to some "optmization criteria" (space or speed or
| > whatever).  The compiler has no notion of useful.
| 
| The compiler writers do.

certainly, but the compiler writers' notion of "useful" does not
always match that of all users -- which, in a sense, is why this
thread initially started.

| >| If the programmer marked the variable as volatile, it seems
| >| extremely likely that the variable will be used somehow, and removing
| >| it would cause some sort of breakage.
| >
| > Well, if the side effect is needed then the compiler won't remove
| > it.
| 
| I agree with that precise wording, but note that this is different
| from "if the compiler knows that the side effect is needed, then the
| compiler win't remove it".
| 
| If volatile is used, then the compiler cannot know whether accesses
| and side effects are needed.  (This is exactly what is expressed by
| volatile.)  Do you agree or disagree?

Change "cannot" to "may not" and I agree.

| If the compiler does not know whether accesses and side effects are
| needed, then it must not optimize then away.  Do you agree or
| disagree?

I agree.

-- Gaby


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]