Docsity

Compiler Optimizations: Dead Code Elimination, Constant Propagation, Loop Optimizations, Exams of Electrical and Electronics Engineering

Partial preview of the text

Download Compiler Optimizations: Dead Code Elimination, Constant Propagation, Loop Optimizations and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Dataflow III: Local, Global and Loop Optimizations

EECS 483 – Lecture 25 University of Michigan Monday, December 4, 2006

  • 1 -

Announcements and Reading

Schedule

» Today 12/4 – Finish optimizations » Wednes 12/6 – Register allocation, Exam 2

review » Mon 12/11 – Exam 2 in class » Wednes 12/13 – No class (Project 3 due)

Reading for today’s class

» 10.

  • 3 -

Class Problem

r1 = 0 r4 = r1 | -1r7 = r1 * 4

r6 = r

r3 = 8 / r

r3 = 8 * r6r3 = r3 + r

r2 = r2 + r1r6 = r7 * r6r1 = r1 + 1 store (r1, r3)

Optimize this applying 1. constant folding 2. strength reduction 3. dead code elimination

  • 4 -

Constant Propagation

Forward propagation ofmoves of the form

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

encoding restrictions

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 = 5r2 = r1 + r

r1 = r1 + r

r7 = r1 + r

r8 = r1 + 3

r9 = r1 + r

  • 6 -

Global Constant Propagation ™

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 = 5r2 = ‘_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

1: r1 = 02: r2 = 10 3: r4 = 1 4: r7 = r1 * 3

5: r6 = 7

6: r2 = 0 7: r3 = r2 / r

8: r3 = r4 * r69: r3 = r3 + r

10: r2 = r2 + r111: r6 = r7 * r612: r1 = r1 + 1 13: store (r1, r3)

Optimize this applying 1. constant propagation 2. constant folding 3. dead code elimination

  • 9 -

Backward Copy Propagation ™

Backward prop. of the LHS of moves^ »

X: r1 = r2 + r

Æ

r4 = r2 + r

»^
»^

r5 = r1 + r

Æ

r5 = r4 + r

»^
»^

Y: r4 = r

Æ

noop

™

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 + r9r2 = r9 + r1r4 = r2r6 = r2 + 1r9 = r1r10 = r6r5 = r6 + 1r4 = 0r8 = r2 + r

  • 10 -

Local Common Subexpression Elimination^ ™

Eliminate recomputation of an expr^ »

X: r1 = r2 * r »^

Æ

r100 = r

»^
»^

Y: r4 = r2 * r

Æ

r4 = r

™

Benefits^ »

Reduce work »^

Moves can get copy propagated

™

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 = 9r4 = 4r5 = 0r6 = 16 r2 = r3 * r4r8 = r2 + r

r9 = r r7 = load(r2)r5 = r9 * r4r3 = load(r2)r10 = r3 / r6store (r8, r7)

r11 = r r12 = load(r11)store(r12, r3)

  • 13 -

Loop Optimizations

The most important set of optimizations

» Because programs spend so much time in

loops

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

Optis

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

  • 15 -

Invariant Code Removal (2)

™

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 = 3r5 = 0 r4 = load(r5)r7 = r4 * 3

r8 = r2 + 1r7 = r8 * r

r3 = r2 + 1

r1 = r1 + r7 store (r1, r3)

  • 16 -

Global Variable Migration ™

Assign a global variabletemporarily to a register forthe duration of the loop^ »

Load in preheader »^

Store at exit points

™

Rules^ »

X is a load or store »^

address(X) not modified inthe loop »^

if X not executed on everyiteration, then X mustprovably not cause anexception »^

All memory ops in loopwhose address can equaladdress(X) must always havethe same address as X

r4 = load(r5)r4 = r4 + 1

r8 = load(r5)r7 = r8 * r

store(r5, r4)

store(r5,r7)

  • 18 -

Induction Variable Strength Reduction

Create basic inductionvariables from derivedinduction variables

Rules

» X is a *, <<, + or –

operation

» src1(X) is a basic ind var » src2(X) is invariant » No other ops modify

dest(X)

» dest(X) != src(X) for all

srcs

» dest(X) is a register

r5 = r4 - 3r4 = r4 + 1

r7 = r4 * r

r6 = r4 << 2

  • 19 -

Induction Variable Strength Reduction (2) ™

Transformation^ »

Insert the following into the bottomof preheader^ y

new_reg = RHS(X)

»^

if opcode(X) is not add/sub, insertto the bottom of the preheader^ y

new_inc = inc(src1(X)) opcode(X)src2(X)

»^

else^ y

new_inc = inc(src1(X))

»^

Insert the following at each updateof src1(X)^ y

new_reg += new_inc

»^

Change X

Æ

dest(X) = new_reg

r5 = r4 - 3r4 = r4 + 1

r7 = r4 * r

r6 = r4 << 2