[comp.sys.mac.programmer] compiler-generated code quality

urlichs@smurf.sub.org (Matthias Urlichs) (12/19/90)

In comp.sys.mac.programmer, article <1990Dec17.233507.16372@phri.nyu.edu>,
  roy@phri.nyu.edu (Roy Smith) writes:
< 
< 	a () { int x; x = 1; x = x + 1; }
< 	b () { int x; x = 1; x++; }
< 
< [ table deleted ]

Note that a real compiler will generate _no_ code for the above procedures
(other than procedure entry/exit stuff), because the value isn't used
anywhere. Similarly, if you try to fix that by saying
 	a () { int x; x = 1; x = x + 1; xyzzy(x); }

gcc (which, for the purposes of this discussion, certainly counts as a real
compiler) will push the value 2 onto the stack without bothering to create
and increment a variable.

-- 
Matthias Urlichs -- urlichs@smurf.sub.org -- urlichs@smurf.ira.uka.de     /(o\
Humboldtstrasse 7 - 7500 Karlsruhe 1 - FRG -- +49+721+621127(0700-2330)   \o)/

hpoppe@ncar.ucar.edu (Herb Poppe) (12/20/90)

In article <fn9-g2.go1@smurf.sub.org> urlichs@smurf.sub.org (Matthias 
Urlichs) writes:
> In comp.sys.mac.programmer, article 
<1990Dec17.233507.16372@phri.nyu.edu>,
>   roy@phri.nyu.edu (Roy Smith) writes:
> < 
> <       a () { int x; x = 1; x = x + 1; }
> <       b () { int x; x = 1; x++; }
> < 
> < [ table deleted ]
> 
> Note that a real compiler will generate _no_ code for the above 
procedures
> (other than procedure entry/exit stuff), because the value isn't used
> anywhere. Similarly, if you try to fix that by saying
>         a () { int x; x = 1; x = x + 1; xyzzy(x); }
> 
> gcc (which, for the purposes of this discussion, certainly counts as a 
real
> compiler) will push the value 2 onto the stack without bothering to 
create
> and increment a variable.

If that is the case, if I were to "debug" this routine with a symbolic 
debugger, what would it show as the value of "x"?


Herb Poppe             hpoppe@ncar.ucar.edu
NCAR                      (303) 497-1296
1850 Table Mesa Dr.
Boulder, CO  80307-3000

urlichs@smurf.sub.org (Matthias Urlichs) (12/20/90)

In comp.sys.mac.programmer, article <9611@ncar.ucar.edu>,
  hpoppe@ncar.ucar.edu (Herb Poppe) writes:
< In article <fn9-g2.go1@smurf.sub.org> urlichs@smurf.sub.org (Matthias 
< Urlichs) writes:

< >         a () { int x; x = 1; x = x + 1; xyzzy(x); }
< > 
< > gcc (which, for the purposes of this discussion, certainly counts as a real
< > compiler) will push the value 2 onto the stack without bothering to create
< > and increment a variable.
< 
< If that is the case, if I were to "debug" this routine with a symbolic 
< debugger, what would it show as the value of "x"?
< 
Nothing -- because that variable is optimized out, the symbol "x" is
undefined in that context. (I just checked.)

"Normal" C compilers don't allow you to use both symbols and optimizations at
the same time... doing this with gcc can lead to some interesting effects,
especially when it starts unrolling small loops, partially combining case
statements, or optimizing function calls out of your program (you can tell
gcc that a procedure doesn't have side effects).

-- 
Matthias Urlichs -- urlichs@smurf.sub.org -- urlichs@smurf.ira.uka.de     /(o\
Humboldtstrasse 7 - 7500 Karlsruhe 1 - FRG -- +49+721+621127(0700-2330)   \o)/

chewy@apple.com (Paul Snively) (12/21/90)

In article <9611@ncar.ucar.edu> hpoppe@ncar.ucar.edu (Herb Poppe) writes:
> > Note that a real compiler will generate _no_ code for the above 
> procedures
> > (other than procedure entry/exit stuff), because the value isn't used
> > anywhere. Similarly, if you try to fix that by saying
> >         a () { int x; x = 1; x = x + 1; xyzzy(x); }
> > 
> > gcc (which, for the purposes of this discussion, certainly counts as a 
> real
> > compiler) will push the value 2 onto the stack without bothering to 
> create
> > and increment a variable.
> 
> If that is the case, if I were to "debug" this routine with a symbolic 
> debugger, what would it show as the value of "x"?

From "Using and Porting GNU CC," page 14, I quote:

"Unlike most other C compilers, GNU CC allows you to use '-g' with '-O'.  
The shortcuts taken by optimized code may occassionally produce surprising 
results: some variables you declared may not exist at all..."

__________________________________________________________________________
                                Paul Snively
                      Macintosh Developer Technical Support
                             Apple Computer, Inc.

chewy@apple.com

Just because I work for Apple Computer, Inc. doesn't mean that I believe 
what they believe, or vice-versa.
__________________________________________________________________________