Compiler Construction: Global Code Optimizations and Copy Propagation & Elimination - Prof, Study notes of Computer Science

Various global code optimizations techniques in compiler construction, including copy propagation & elimination, constant folding & propagation, strength reduction, partial redundancy elimination, and partial dead code elimination. The concepts and conditions under which these optimizations can be performed.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-pfs
koofers-user-pfs 🇺🇸

8 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
4/12/09
1
1
CS 201
Compiler Construction
Lecture 5
Code Optimizations:
Copy Propagation & Elimination
2
Global Code Optimizations
Copy Propagation & Elimination: copy
assignments are eliminated by replacing lhs
variable with rhs variable.
Constant Folding & Propagation: constant
assignments are exploited by substituting
constant values for variables; if all operands of
an expression are found to be constants, the
expression can be computed at compile time.
Strength Reduction: replaces expensive
operations with cheaper ones – multiplaction
addition; exponentiation multiplication.
pf3
pf4
pf5

Partial preview of the text

Download Compiler Construction: Global Code Optimizations and Copy Propagation & Elimination - Prof and more Study notes Computer Science in PDF only on Docsity!

1

CS 201

Compiler Construction

Lecture 5

Code Optimizations:

Copy Propagation & Elimination

2

Global Code Optimizations

  • Copy Propagation & Elimination: copy assignments are eliminated by replacing lhs variable with rhs variable.
  • Constant Folding & Propagation: constant assignments are exploited by substituting constant values for variables; if all operands of an expression are found to be constants, the expression can be computed at compile time.
  • Strength Reduction: replaces expensive operations with cheaper ones – multiplaction  addition; exponentiation  multiplication.

3

Global Code Optimizations

  • Partial Redundancy Elimination: eliminating repeated evaluation of the same expression. - Loop Invariant Code Motion (full redundancy) - Common Sub-Expression Elimination (full redundancy) - The idea applies to expressions, loads, and even conditional branches.
  • Partial Dead Code Elimination: avoiding execution of statements whose results are not used. - Dead Code Elimination - The idea applies to expressions and stores. 4

Copy Propagation & Elimination

  1. Uses of A replaced by uses of B.
  2. A=B is eliminated because no more uses of A are left.

Data Flow Analysis

• The analysis propagates copies such that a copy

reaches its use only if it can be propagated to

the use.

• However, the copy can only be eliminated if it

can be propagated to all of its uses.

7

Code Transformation Algorithm

for each copy S: X=Y do OK = true for each use u on def-use(X,S) do if (s is in IN[B] where B contains a use of X) and (there is no redinifition of X or Y in B prior to u) then /* can propagate / else OK = false endif / cannot propagate */ endfor if Ok then replace all uses u of X in def-use(X,S) by use of Y; remove S endif endfor 8

9

Example

Eliminated: D=A Not Eliminated: A=B Y=D W=A

A=B

X=A+

W=A D=A

A=3*M

Y=D

Z=D+

W=D+

Assume W,A,X,Y,Z Are live

A=B

X=B+

A=3*M

Y=B

Z=B+

W=B+

W=B