Title: Chapter 13: Redundancy Elimination
1Chapter 13Redundancy Elimination
2Redundancy Elimination
- Removes redundant computations
- Tries either to improve the program performance
or to reduce the space it occupies - Requires data-flow analysis
- Done on either medium-level intermediate code or
low-level intermediate code
3Optimizations Covered
- Common-Subexpression Elimination
- Loop-Invariant Code Motion
- Partial-Redundancy Elimination
- Encompasses Common-Subexpression Elimination,
Loop-Invariant Code Motion and more - Code Hoisting
41. Common-Subexpression Elimination
5Definition Common subexpression
- An occurrence of an expression is a common
subexpression if there is another occurrence of
the expression whose evaluation always precedes
this one in execution order and if the operands
of the expression remain unchanged between the
two evaluations
6Common-Subexpression Elimination (CSE)
- CSE is usually done in two phases
- Local CSE is done within each basic block while
the intermediate code for the basic block is
constructed - Global CSE is done later across an entire
flowgraph representing a procedure
7Local Common-Subexpression Elimination
- (An example, fig. 13.3 - 13.7)
8Global Common-Subexpression Elimination
- (An example, fig. 13.8 - 13.10)
9Global CSE with Copy Propagation
- (An example, fig. 13.12 - 13.14)
10Forward Substitution
- Because CSE creates temporary variables to store
values of expressions, it is a trade-off between
register space and expression compactness - Sometimes we need to do it the other way around,
i.e. replace a copy operation with a reevaluation
of the expression - This is called forward substitution and is
generally easy to do
112. Loop-Invariant Code Motion
12Definition Loop-Invariant Instruction
- An instruction is loop-invariant if, for each of
its operands - The operand is constant or
- All definitions that reach this use of the
operand are located outside the loop or - There is exactly one definition of the operand
that reaches the instruction and that definition
is an instruction inside the loop that is itself
loop-invariant.
13Loop-Invariant Code Motion Example
- (An example, fig. 13.18 - 13.22)
143. Partial-Redundancy Elimination
15Definition Partial Redundancy
- A partial redundancy is a computation that is
done more than once on some path through a
flowgraph. - Partial redundancy elimination inserts and
deletes computations in the flowgraph in such a
way that after the transformation each path
contains no more and, generally,
feweroccurences of any such computation than
before
16Partial Redundancy Elimination
- Computationally optimal, i.e. after PRE the
number of computations on each program path
cannot be reduced anymore by means of safe code
motion - Made more effective by
- Critical edge splitting
- Lazy code motion
17Critical Edge Splitting Example
18Partial Redundancy Elimination Example 1
- (An example, fig. 13.26 - 13.28)
19Partial Redundancy Elimination Example 2
- (An example, fig. 1 - 8)
- (Source Knoop, Ruthing, Steffen Lazy Code
Motion, in Proc. of SIGPLAN 92 Symp. on
Programming Language Design and Implementation
(1992))
204. Code Hoisting
21Code Hoisting
- Finds expressions that are always evaluated
following some point in program and moves them to
the latest point beyond which they would always
be evaluated - These expressions are called very busy
expressions - Almost always reduces space occupied by the
program but may shorten or lengthen the execution
time
22Code Hoisting Example
- (An example fig. 13.33 - 13.35)
23Wrap-Up