Re: machdep.conspeed sysctl

From: Chuck Swiger <cswiger_at_mac.com>
Date: Fri, 08 Apr 2005 15:37:36 -0400
Peter Jeremy wrote:
> On Thu, 2005-Apr-07 21:12:10 -0600, M. Warner Losh wrote:
[ ... ]
> bde made it volatile in 1.162 though it's not clear to me why the
> volatile is needed here.  I probably need to go and have a close study
> of exactly what a compiler is permitted to optimise away in the
> absence of 'volatile'.

Page 586 of _Compilers: Principles, Techniques, and Tools_ by Aho, Sethi, and 
Ullman states:  "First, a transformation must preserve the meaning of 
programs.  That is, an 'optimization' must not change the output produced by a 
program for a given input, or cause an error, such as a division by zero, that 
was not present in the original program."

The simple answer is that the compiler is permitted to optimize away *any* 
code [1], so long as doing so does not change the output of the program.  Dead 
code elimination and common subexpression elimination can be iterated using 
fixed points until the code is as small and fast as possible, although many 
compilers only do a few passes.

A simple example:

int a = 1, b = 2, c = 3;
a = b + c; /* this line can be removed as dead code, since nothing uses a
            before a is assigned a new value.  Ditto for "a = 1" above. */
a = b * c;

-- 
-Chuck

[1]: With obvious exceptions-- notably the volatile keyword-- as you already 
know.  Basicly, when a variable is declared volatile, the compiler treats that 
variable as always being live, and it refuses to try to apply CSE or DCE to 
that variable, so that the code the compiler produces references the variable 
exactly as the original source code specifies.
Received on Fri Apr 08 2005 - 17:39:03 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:31 UTC