\"\"
'
,)
.)
.)
-)
.)
..)
-)
,
,)
.
,II j)))
replacement
Scalar
Data-cacheoptimizations)
of array references
Procedure
Tail-call optimization,
integration
including
tail-recursion
elimination
Scalar replacementof aggregates
Sparse
conditional
constant propagation
constant
propagation
Interprocedural
Procedure specializationand
cloning
Sparse conditional constantpropagation)
numbering
Global value
Local and global copy propagation
Sparse conditional
Dead-code elimination)
constant
propagation
----------------\037)
Constant
folding)
Algebraic
simplifications,
including
reassociation)
A)
B)
Cl)
C2)
C3)
Partial-redundancy
elimination)
reduction
C)
Local
and global common-
subexpressionelimination
Loop-invariant
code motion)
elimination
Dead-code
Code hoisting
Induction-variable
Linear-function test replacement
removal
Induction-
Unnecessarybounds-checking
variable
strength
C4)
elimination
Control-flow optimizations)
Order
of Optimizations)
This flowchart representsa recommended
sive optimizing compiler. Other ordersarepossible,and
in Chapter
tions in
several alternatives,
The
letters at the left
21 present
order
though
this
none
appropriate
codelevels
diagram.
the
for
follows:)
is as
corresponding
in
the
optimizations.
for performing
optimizations
of
the
of real-world compilers
examples
them includes all of the optimiza-
to the levels of code
correspond
diagram
The correspondence between letters and
(from box D))
in
an
aggres-
A These optimizations typically
are
applied
either
to source code or to a high-level
code
that
that preserves
has
array
accesses
done very early in
code as it proceeds
loop structure and the
sequence
in which
in essentially their source-code form. Usually,
compilation
since compilation tends to lower the
operations
these
the
from one phase to the next.)))
process,
intermediate
are performedand
are
optimizations
the
level
of
(to constant
folding, algebraic
simplifications,
and
reassociation)
A)
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
I
- - - - - - - - - - - - - - - - I)
,)
expansion
In-line
Leaf-routine optimization
Shrink wrapping
Machine idioms
Tail merging
Branch optimizations and
moves
conditional
Dead-code elimination
Software
pipelining, with loop
expansion,
variable
and hierarchical
unrolling,
renaming,
branch
Basic-blockand
Register allocation by graph
Basic-block and
Intraprocedural I-cache optimization
Instruction
Data prefetching
Branch
prefetching
branch
prediction)
register
reduction
1
scheduling
coloring
2
scheduling
,)
Interprocedural
register allocation
references
Interprocedural I-cacheoptimization)
of global
Aggregation
D)
E
are typically performed on medium-
optimizations
overall
other than
These
depending on the
optimizations
ture), then
optimizations are performed on a medium-level,
ate code and others are performed on low-level
optimizations
organization
those
these
of
in box A (known as the
are performed
then
these optimizations are generally
or
low-level
intermediate
the compiler. If code selectionis
done
\"low-level\"
model
of optimizer
on low-levelcode.If,
on
the other
relatively
code
after
machine-independent
code generation
done
on
the medium-level
before
code,
all
struc-
hand, some
intermedi-
(known as the
interme-
\"mixed\"
model),
diate code.
The branches
essentiallythe
formed less frequently without
a choiceof
data-flow
same
from
Cl
the
optimization
to C2 and C3 representa choiceof
the method
(namely, moving computations to placeswhere
used to perform
are per-
they
of the program). They alsorepresent
changing
the
semantics
used to perform the
analyses
optimization.
optimizations
These
be quite machine-dependent
moregeneral,such
as
are almost always
a low-level
(e.g., a structured assemblylanguage)
done
on
low-level
code used in
the
been turned into the form required by
intermediate
form of code-one that
or
may
that may be somewhat
they require
and because
processor
this
book-because
the
target
require
low-level
control-flow code.
B, C
D
that
addresses
several of
them
have
E These optimizations
Three optimizations,
are in
boxes
they
are
best
connected
structured
are
performed
at
link time, so they
operate
on relocatable
object code.
namely,
constant
to the other phases of
as subroutines that can be invoked
folding,
the
algebraic
optimization
process
simplification, and reassociation,
by dotted lines because
they
20
are needed.
the reader
to guide
whenever
1 and 11 through
A version
of
this diagram appears in Chapters
in
ordering
optimizer
components
in a compiler.)))
Advanced
COlllpiler
Design
and
Illlplelllentation)
Steven S. Muchnick)
M\037\037@)
MORGAN
KAUFMANN
PUBLISHERS)
AN
IMPRINT
OF ACADEMIC
PRESS
A Harcourt Science and Technology Company)
SAN FRANCISCO SAN DIEGO NEW YORK BOSTON)
LONDON SYDNEY TOKYO)))
Senior
Editor
Denise E. M. Penrose
Director
Senior
of Production
and Manufacturing
Production Editor Cheri Palmer
Carron
Jane Elliott
Design
Coordinator
Editorial
Cover Design Ross
Text Design, Composition,and Il/ustration
Copyeditor
Proofreader
Indexer
Printer
Jeff Van
Jennifer McClain
Ty Koontz
Courier Corporation)
Bueren
Yonie
Overton
Windfall
Software
ACADEMICPRESS
A Harcourt
525 B Street,
http://www.academicpress.com)
Science
and Technology Company
92101-4495,
Suite 1900, San Diego, CA
USA
Academic Press
Harcourt Place,32 Jamestown
http://www.academicpress.com)
Road,
London,
NWI 7BY, United
Kingdom
Morgan Kaufmann Publishers
340
Pine Street, Sixth Floor, San Francisco, CA
94104-3205,
USA
http://www.mkp.com
<9 1997
by Academic Press
rights
All
Printed in
reserved
the United
States of America)
04
03)
6)
No
part
of
form or by
in
otherwise-without
any
a retrieval
photocopying,
publisher.)
system, or transmitted
recording, or
this publication may
be
reproduced,
stored in
any means-electronic,
the
written
prior
mechanical,
of the
permission
Library
of Congress Cataloging-in-Publication
Data
Muchnick,
Steven S., date.
Advanced
compiler
design
and implementation / SteveMuchnick.
p.
cm.
Includes bibliographical referencesand
ISBN 1-55860-320-4
1. Compilers (Computer
programs)
science). I. Title.
QA76.76.C65M8
005.4'53-dc21
1997
index.
2. Systems programming (Computer
97-13063
CIP)))
To
Eric,
nihil
sine
quo)))
Foreword)
quality
of
and
development
research
used higher-level
since
language, suc-
its early compilers. John
not give up
language unless the performance
machine
this
that underlie the topics in
of handwritten
programmers
would
in loop optimization and
indexes
time, both researchersand
standing
with more
ask, should there be a new
practi-
effective ones.
as a relatively
in
efficient mappings
that generate
target
to
change,
ever more ambitious
become
the
book
its
ompiler
design
has
been an active topic of
the mid-1950s. Fortran, the
ceeded,in
because
part,
C
Backusand
the detailed designcontrol they
colleagues
first
widely
of the high
at IBM recognized that
assembly
had with
large
his
of
compiled
code was
sufficiently close to the
performance
code. Backus'sgroup invented
book.
methods for
have
tioners
Among
register
local
improved
of
the
In
light
key
them are the treatment of
several
concepts
array
allocation.
Since that
and supplanted them
(repeatedly)
long history of compiler design,and
mature computing technology,
the field? The answer is clear.Compilers
why,
one might
are
tools
from
programs
to machines.
The language designs continue
architectures continue to change,and
the
programs
in
their
level,
same
resources we can bring to bear in
as we zoom in,
and
scale
at a high
the
the computational
increasing. Consequently, modern compilersuse
algorithms than
new and better techniques for
fact, an
computer
solving
of topics in
conventional
architecture.
collection
possible
book
entire
were
this
is
it
complexity. Thus, while the compiler designproblem
continually
changing.
the
compilers
more
time-
and
remains
Furthermore,
are
themselves
space-intensive
invent
to
before. And, of course, researcherscontinue
In
are direct consequences of changesin
design problems.
compiler
This book takes on the
for
reader
and preparesthe
in the future. For example, in Chapter
of symbol
and
tables and local scopestructure
to
scopes as found in Ada, Modula-2,
environments
exported
run-time
model
of contemporary
challenges
the new compiling problems that
3 the book builds on the
languages
will
reader's
and architectures
arise
inevitably
describe
how to deal with
and other modern languages. And,
knowledge
imported
the dynamic semantics of
since
discussion of advanced issuesin
shared
systems
passing
is particularly
modern
objects,
found in
dictated
valuable. That chapter also addresses
some
languages
by modern architectures.)
and the diverse strategies for
source
languages,
in Chapter 5, such as compiling
rich
the
run-time
support
parameter
the
type
vii)))