Docsity

Classical Optimization Techniques in EECS 583 - University of Michigan - Prof. Scott Mahlk, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Classical Optimization Techniques in EECS 583 - University of Michigan - Prof. Scott Mahlk and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 583 – Lecture 12 Classical Optimization

University of Michigan February 17, 2003

  • 1 -

Classical Optimizations

Y

Operation-level – 1 operation in isolation

Constant folding, strength reduction

Y

Dead code elimination (global, but 1 op at a time)

Y

Local/Global – Pairs of operations

Constant propagation

Forward copy propagation

Backward copy propagation

CSE

Constant combining

Operation folding

Y

Loop – Body of a loop

Invariant code removal

Global variable migration

Induction variable strength reduction

Induction variable elimination

  • 3 -

Strength Reduction

Y

Replace expensive ops with cheaper ones

Constant propagation creates opportunities for this

Y

Power of 2 constants

Multiply by power of 2, replace with left shift

y

r1 = r2 * 8

Æ

r1 = r2 << 3

Divide by power of 2, replace with right shift

y

r1 = r2 / 4

Æ

r1 = r2 >> 2

Remainder by power of 2, replace with logical and

y

r1 = r2 REM 16

Æ

r1 = r2 & 15

Y

More exotic

Replace multiply by constant by sequence of shift and adds/subs

y

r1 = r2 * 6

X

r100 = r2 << 2; r101 = r2 << 1; r1 = r100 + r

y

r1 = r2 * 7

X

r100 = r2 << 3; r1 = r100 – r

  • 4 -

Dead Code Elimination

Y

Remove any operation who’sresult is never consumed

Y

Rules

X can be deleted

y

no stores or branches

DU chain empty or destregister not live

Y

This misses some dead code!!

Especially in loops

Critical operation

y

store or branch operation

Any operation that does notdirectly or indirectly feed acritical operation is dead

Trace UD chains backwardsfrom critical operations

Any op not visited is dead

r1 = 3

r2 = 10

r4 = r4 + 1 r7 = r1 * r

r2 = 0

r3 = r3 + 1

r3 = r2 + r store (r1, r3)

  • 6 -

Constant Propagation

Y

Forward propagation of movesof the form

rx = L (where L is a literal)

Maximally propagate

Assume no instructionencoding restrictions

Y

When is it legal?

SRC: Literal is a hard codedconstant, 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

  • 7 -

Local Constant Propagation

Y

Consider 2 ops, X and Y in aBB, X is before Y

  1. X is a move
  1. src1(X) is a literal
  1. Y consumes dest(X)
  1. There is no definition ofdest(X) between X and Y
  1. No danger betw X and Y

y

When dest(X) is a Macroreg, BRL destroys the value

r1 = 5 r2 = ‘_x’ r3 = 7 r4 = r4 + r1 r1 = r1 + r2 r1 = r1 + 1 r3 = 12 r8 = r1 - r2 r9 = r3 + r5 r3 = r2 + 1 r10 = r3 – r

  • 9 -

Class Problem

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

r1 = 0

r2 = 10

r4 = 1

r7 = r1 * 4

r6 = 8

r2 = 0

r3 = r2 / r

r3 = r4 * r r3 = r3 + r

r2 = r2 + r r6 = r7 * r

r1 = r1 + 1

store (r1, r3)

  • 10 -

Forward Copy Propagation

Y

Forward propagation of the RHSof moves

r1 = r

r4 = r1 + 1

Æ

r4 = r2 + 1

Y

Benefits

Reduce chain of dependences

Eliminate the move

Y

Rules (ops X and Y)

X is a move

src1(X) is a register

Y consumes dest(X)

X.dest is an available def at Y

X.src1 is an available expr at Y

r1 = r2 r3 = r

r2 = 0

r6 = r3 + 1

r5 = r2 + r

  • 12 -

CSE – Common Subexpression Elimination

Y

Eliminate recomputation of anexpression by reusing the previousresult

»

r1 = r2 * r

»

Æ

r100 = r

»

…

»

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

»

expr(X) is available at Y

»

if X is a load, then there is no storethat may write to address(X) alongany path between X and Y

r1 = r2 * r

r3 = r4 / r

r2 = r2 + 1

r6 = r3 * 7

r5 = r2 * r

r8 = r4 / r r9 = r3 * 7

if op is a load, call it redundant load elimination rather than CSE

  • 13 -

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 * r r8 = r2 + r

r9 = r

r7 = load(r2)

r5 = r9 * r

r3 = load(r2)

r10 = r3 / r store (r8, r7)

r11 = r

r12 = load(r11)

store(r12, r3)

  • 15 -

Operation Folding

Y

Combine 2 dependent ops into 1complex op

Classic example is MPYADD

r1 = r2 * r

r5 = r1 + r

Æ

r5 = r2 * r3 + r

Y

First op often becomes dead

Y

Borders on machine dependentopti (often it is !! )

Y

Rules (ops X and Y in same BB)

X is an arithmetic operation

dest(X) != any src(X)

Y is an arithmetic operation

Y consumes dest(X)

X and Y can be merged

src(X) not modified in (X…Y)

r1 = r2 & 4 r3 = r1 ^ -1 r2 = r3 < 6 r4 = r2 == 0 r5 = r6 << 1 r7 = r5 + r

  • 16 -

Loop Optimizations

Y

The most important set of optimizations

Because programs spend so much time in loops

Y

Optimize given that you know a sequence of code will berepeatedly executed

Y

Optis

Invariant code removal

Global variable migration

Induction variable strength reduction

Induction variable elimination

  • 18 -

Invariant Code Removal

Y

Move operations whose sourceoperands do not change within the loopto the loop preheader

»

Execute them only 1x per invocation ofthe loop

Y

Rules

»

X can be moved

»

src(X) not modified in loop body

»

X is the only op to modify dest(X)

»

for all uses of dest(X), X is in theavailable defs set

»

for all exit BB, if dest(X) is live on theexit edge, X is in the available defs seton the edge

»

if X not executed on every iteration,then X must provably not causeexceptions

»

if X is a load or store, then there are nowrites to address(X) in loop

r1 = 3r5 = 0

r4 = load(r5)

r7 = r4 * 3

r8 = r2 + 1 r7 = r8 * r

r3 = r2 + 1

r1 = r1 + r store (r1, r3)

  • 19 -

Global Variable Migration

Y

Assign a global variabletemporarily to a register for theduration of the loop

Load in preheader

Store at exit points

Y

Rules

X is a load or store

address(X) not modified in theloop

if X not executed on everyiteration, then X must provablynot cause an exception

All memory ops in loop whoseaddress can equal address(X)must always have the sameaddress as X

r4 = load(r5)

r4 = r4 + 1

r8 = load(r5)

r7 = r8 * r

store(r5, r4)

store(r5,r7)