Docsity

Lecture Slides on Dataflow Analysis - Compiler Construction | EECS 483, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Lecture Slides on Dataflow Analysis - Compiler Construction | EECS 483 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Dataflow Analysis/Opti III Classical Optimization

EECS 483 – Lecture 20 University of Michigan Wednesday, November 19, 2003

  • 1 -

Types of Classical Optimizations

Y

Operation-level – 1 operation in isolation » Constant folding, strength reduction » Dead code elimination (global, but 1 op at a time)

Y

Local – Pairs of operations in same BB » May or may not use dataflow analysis

Y

Global – Again pairs of operations » But, operations in different BBs » Dataflow analysis necessary here

Y

Loop – Body of a loop

  • 3 -

Class Problem

Optimize this applying 1. constant folding 2. strength reduction 3. dead code elimination r1 = 0 r4 = r1 | -1 r7 = r1 * 4 r6 = r r3 = 8 / r r3 = 8 * r6 r3 = r3 + r r2 = r2 + r1r6 = r7 * r6r1 = r1 + 1 store (r1, r3)

  • 4 -

Constant Propagation

Y

Forward propagation ofmoves of the form

» rx = L (where L is a literal) » Maximally propagate » Assume no instruction

encoding restrictions

Y

When is it legal?

» SRC: Literal is a hard coded

constant, so never a problem

» DEST: Must be available

y Guaranteed to reach y May reach not good enough r1 = 5 r2 = r1 + r r1 = r1 + r r7 = r1 + r r8 = r1 + 3 r9 = r1 + r

  • 6 -

Global Constant Propagation

Y

Consider 2 ops, X and Y indifferent BBs

» 1. X is a move » 2. src1(X) is a literal » 3. Y consumes dest(X) » 4. X is in adef_IN(BB(Y)) » 5. dest(X) is not modified

between the top of BB(Y) and Y

y Rules 4/5 guarantee X is available

» 6. If dest(X) is SP/FP/..., no

subroutine call between X and Y

r1 = 5 r2 = ‘_x’ r1 = r1 + r r7 = r1 – r r8 = r1 * r r9 = r1 + r Note: checks for subroutine calls whenever SP/FP/etc. are involved is required for all optis. I will omit the check from here on!

  • 7 -

Class Problem

Optimize this applying 1. constant propagation 2. constant folding 3. strength reduction 4. dead code elimination 1: r1 = 0 2: r2 = 10 3: r4 = 1 4: r7 = r1 * 4 5: r6 = 8 6: r2 = 0 7: r3 = r2 / r 8: r3 = r4 * r6 9: r3 = r3 + r 10: r2 = r2 + r111: r6 = r7 * r612: r1 = r1 + 1 13: store (r1, r3)

  • 9 -

Backward Copy Propagation Y

Backward prop. of the LHS of moves

X: r1 = r2 + r

Æ

r4 = r2 + r »

r5 = r1 + r

Æ

r5 = r4 + r »

Y: r4 = r

Æ

noop Y

Rules (ops X and Y in same BB)

dest(X) is a register » dest(X) not live out of BB(X) » Y is a move » dest(Y) is a register » Y consumes dest(X) » dest(Y) not consumed in (X…Y) » dest(Y) not defined in (X…Y) » There are no uses of dest(X) after the firstredefinition of dest(Y)

r1 = r8 + r9 r2 = r9 + r1 r4 = r2 r6 = r2 + 1 r9 = r1 r10 = r6 r5 = r6 + 1 r4 = 0 r8 = r2 + r

  • 10 -

Local Common Subexpression Elimination

Y

Eliminate recomputation of an expr

X: r1 = r2 * r »

Æ

r100 = r »

Y: r4 = r2 * r

Æ

r4 = r Y

Benefits

Reduce work » Moves can get copy propagated Y

Rules (ops X and Y)

X and Y have the same opcode » src(X) = src(Y), for all srcs » for all srcs(X) no defs of srci in [X ... Y) » if X is a load, then there is no store thatmay write to address(X) between X and Y

r1 = r2 + r3r4 = r4 +1r1 = 6r6 = r2 + r3r2 = r1 -1r6 = r4 + 1r7 = r2 + r

  • 12 -

Class Problem

Optimize this applying 1. constant propagation 2. constant folding 3. strength reduction 4. dead code elimination 5. forward copy propagation 6. backward copy propagation 7. CSE r1 = 9 r4 = 4 r5 = 0 r6 = 16 r2 = r3 * r4 r8 = r2 + r r9 = r r7 = load(r2)^ r5 = r9 * r4 r3 = load(r2)^ r10 = r3 / r6^ store (r8, r7) r11 = r r12 = load(r11) store(r12, r3)

  • 13 -

Constant Combining Y

Combine 2 dependent ops into 1by combining the literals

X: r1 = r2 + 4 »

Y: r5 = r1 - 9

Æ

r5 = r2 – 5 Y

First op often becomes dead

Y

Rules (ops X and Y in same BB)

X is of the form rx +- K » dest(X) != src1(X) » Y is of the form ry +- K (comparisonalso ok) » Y consumes dest(X) » src1(X) not modified in (X…Y)

r1 = r2 + 4 r3 = r1 < 0 r2 = r3 + 6 r7 = r1 – 3 r8 = r7 + 5

  • 15 -

Loop Optimizations

Y

The most important set of optimizations » Because programs spend so much time in loops

Y

Optimize given that you know a sequenceof code will be repeatedly executed

Y

Optis » Invariant code removal » Global variable migration » Induction variable strength reduction » Induction variable elimination

  • 16 -

Recall Loop Terminology^ - r1, r4 are basic^ induction variables^ - r7 is a derived^ induction variable

r1 = 3 r2 = 10 loop preheader r4 = r4 + 1r7 = r4 * 3 r2 = 0 r3 = r2 + 1 r1 = r1 + 2 store (r1, r3) loop header exit BB backedge BB

  • 18 -

Invariant Code Removal (2)

Y

Rules

X can be moved » src(X) not modified in loop body » X is the only op to modifydest(X) » for all uses of dest(X), X is in theavailable defs set » for all exit BB, if dest(X) is liveon the exit edge, X is in theavailable defs set on the edge » if X not executed on everyiteration, then X must provablynot cause exceptions » if X is a load or store, then thereare no writes to address(X) inloop r1 = 3 r5 = 0 r4 = load(r5) r7 = r4 * 3 r8 = r2 + 1r7 = r8 * r r3 = r2 + 1 r1 = r1 + r7 store (r1, r3)

  • 19 -

Invariant Code Removal (3)

Y

Can you do LICM w/oavailable defs info?

» Sure – no problem!

Y

Rules that need change

» for all uses of dest(X),

X is in the availabledefs set

» for all exit BB, if

dest(X) is live on theexit edge, X is in theavailable defs set on theedge

Y

First rule approx:

» X dominates all uses

of dest(X)

Y

Second rule approx:

» X dominates all exit

BBs where dest(X) islive

Y

This is how thecompiler that I workon does it..