Constant Propagation: CS 701 Fall 2007
Constant Propagation: CS 701 Fall 2007
Constant Propagation: CS 701 Fall 2007
Constant Propagation
If a variable is known to contain a
particular constant value at a particular
point in the program, replace references to
the variable at that point with that
constant value.
4. Copy Propagation
After the assignment of one variable to
another, a reference to one variable may be
replaced with the value of the other
variable (until one or the other of the
variables is reassigned).
(This may also “set up” dead code
elimination. Why?)
5. Constant Folding
An expression involving constant (literal)
values may be evaluated and simplified to a
constant result value. Particularly useful
when constant propagation is performed.
©
CS 701 Fall 2007 20
6. Dead Code Elimination
Expressions or statements whose values or
effects are unused may be eliminated.
©
CS 701 Fall 2007 21
9. Local Register Allocation
Within a basic block (a straight line
sequence of code) track register contents
and reuse variables and constants from
registers.
©
CS 701 Fall 2007 22
12. Register Targeting
Compute values directly into the intended
target register.
©
CS 701 Fall 2007 23
16. Loop Unrolling
Replace a loop body executed N times with
an expanded loop body consisting of M
copies of the loop body. This expanded loop
body is executed N/M times, reducing loop
overhead and increasing optimization
possibilities within the expanded loop body.
©
CS 701 Fall 2007 24
18. Strength Reduction
Replace an expensive instruction with an
equivalent but cheaper alternative. For
example a division may be replaced by
multiplication of a reciprocal, or a list
append may be replaced by cons
operations.
©
CS 701 Fall 2007 25
20. Instruction Cache Optimizations
Instructions that are repeatedly re-
executed should be accessed from the
instruction cache rather than the
secondary cache or memory. Loops and
“hot” instruction sequences should fit
within the cache.
Temporally close instruction sequences
should not map to conflicting cache
locations.
©
CS 701 Fall 2007 26
Reading Assignment
• Read “Modern Microprocessors—A 90
Minute Guide!,” by Jason Patterson.
©
CS 701 Fall 2007 27
SPARC Overview
• SPARC is an acronym for
Scalable Processor ARChitecture
• SPARCs are load/store RISC processors
Load/store means only loads and
stores access memory directly.
RISC (Reduced Instruction Set
Computer) means the architecture is
simplified with a limited number of
instruction formats and addressing
modes.
©
CS 701 Fall 2007 28
• Instruction format:
add %r1,%r2,%r3
Registers are prefixed with a %
Result is stored into last operand.
ld [adr],%r1
Memory addresses (used only in loads
and stores) are enclosed in brackets
• Distinctive features include Register
Windows and Delayed Branches
©
CS 701 Fall 2007 29
Register Windows
The SPARC provides 32 general-purpose
integer registers, denoted as %r0 through
%r31.
These 32 registers are subdivided into 4
groups:
Globals: %g0 to %g7
In registers: %i0 to %i7
Locals: %l0 to %l7
Out registers: %o0 to %o7
©
CS 701 Fall 2007 30
registers are independent of the register
windows (as are the floating-point
registers).
A register window may be pushed or
popped using SPARC save and restore
instructions.
After a register window push, the “out”
registers become “in” registers and a fresh
set of “local” and “out” registers is created:
Before save:
In Local Out
After save
©
CS 701 Fall 2007 31
Why the overlap between “in” and “out”
registers? It’s a convenient way to pass
parameters—the caller puts parameter
values in his “out” registers. After a call
(and a save) these values are
automatically available as “in” registers in
the newly created register window.
©
CS 701 Fall 2007 32
For example, a leaf procedure—one that
contains no calls—can be compiled without
use of save and restore if it doesn’t
need too many registers. The leaf procedure
must then make do with the caller’s
registers, modifying only those the caller
treats as volatile.
©
CS 701 Fall 2007 33
Register Conventions
Global Registers
%g0 is unique: It always contains 0 and
can never be changed.
%g1 to %g7 have global scope (they are
unaffected by save and restore
instructions)
%g1 to %g4 are volatile across calls;
they may be used between calls.
%g5 to %g7 are reserved for special use
by the SPARC ABI (application binary
interface)
Local Registers
%l0 to %l7
May be freely used; they are unaffected
by deeper calls.
©
CS 701 Fall 2007 34
In Registers
These are also the caller’s out registers;
they are unaffected by deeper calls.
%i0
Contains incoming parameter 1.
Also used to return function value to
caller.
%i1 to %i5
Contain incoming parameters 2 to 6
(if needed); freely usable otherwise.
%i7
Contains return address -8 (offset due
to delay slot); it must be preserved.
©
CS 701 Fall 2007 35
Out Registers
Become the in registers for procedures
called from the current procedure.
%o0
Contains outgoing parameter 1.
It also contains the value returned by
the called procedure.
It is volatile across calls; otherwise it
is freely usable.
%o1 to %o5
Contain outgoing parameters 2 to 6 as
needed.
These are volatile across calls;
otherwise they are freely usable.
©
CS 701 Fall 2007 36
%o6 (also denoted as %sp)
Holds the stack pointer (and becomes
frame pointer of called routines)
It is reserved; it must always be valid
(since TRAPs may modify the stack at
any time).
%o7
Is volatile across calls.
It is loaded with address of caller on a
procedure call.
©
CS 701 Fall 2007 37