Maximizing Compiler Performance: Code Elimination, Loop Optimization, Variable Reduction, , Assignments of Electrical and Electronics Engineering

Various compiler optimization techniques including dead code elimination, forward copy propagation, common subexpression elimination, loop optimizations, induction variable strength reduction, class problem optimization, ilp optimization, rename with copy, tree height reduction, optimizing unrolled loops, register renaming on unrolled loops, accumulator variable expansion, and induction variable expansion. The techniques aim to improve compiler performance by reducing redundancy, increasing parallelism, and optimizing memory usage.

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-bad
koofers-user-bad šŸ‡ŗšŸ‡ø

10 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EECS 583 – Class 10
Classical and ILP Optimizations
University of Michigan
October 8, 2007
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Maximizing Compiler Performance: Code Elimination, Loop Optimization, Variable Reduction, and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS 583 – Class 10 Classical and ILP Optimizations

University of Michigan October 8, 2007

  • 1 -

Reading Material

Today’s class

"Compiler Code Transformations for Superscalar-Based High-Performance Systems", S. Mahlke et al., Supercomputing '92,Nov. 1992, pp. 808-817.

Next class

ā€œMachine Description Driven Compilers for EPIC Processorsā€,B. Rau, V. Kathail, and S. Aditya, HP Technical Report, HPL-98-40, 1998.

  • 3 -

Now Back to Optimization … Last Last Week: Dead Code Elimination

Ā™

Remove any operation who’sresult is never consumed

Ā™

Rules

X can be deleted

y

no stores or branches

DU chain empty or destregister not live

Ā™

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)

  • 4 -

Last Last Week: Global Constant Propagation

Ā™

Consider 2 ops, X and Y indifferent BBs

  1. X is a move
  1. src1(X) is a literal
  1. Y consumes dest(X)
  1. X is in a_in(BB(Y))
  1. Dest(x) is not modified betweenthe top of BB(Y) and Y
  1. No danger betw X and Y

y

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

r1 = 5 r2 = ā€˜_x’

r1 = r1 + r

r7 = r1 – r

r8 = r1 * r

r9 = r1 + r

  • 6 -

CSE – Common Subexpression Elimination

Ā™

Eliminate recomputation of anexpression by reusing the previousresult

Ā»

r1 = r2 * r

Ā»

Ɔ

r100 = r

Ā»

…

Ā»

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

Ā»

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

  • 7 -

Class Problem

Optimize this applying 1. dead code elimination 2. forward copy propagation 3. CSE

r4 = r1 if Truer6 = r15 if Truer2 = r3 * r4 if Truer8 = r2 + r5 if Truer9 = r3 if Truep1,p2 = cmpp(r2, r8) if Truer7 = load(r2) if Truer5 = r9 * r4 if p1r3 = load(r2) if p2r10 = r3 / r6 if p2r11 = r2 if Truer12 = load(r11) if p1store (r8, r7) if p2store(r12, r3) if True

Be careful of the predicates!

  • 9 -

Loop Invariant Code Motion (LICM)

Ā™

Move operations whose sourceoperands do not change withinthe loop to the loop preheader

Execute them only 1x perinvocation of the loop

Be careful with memoryoperations!

Be careful with ops notexecuted every iteration

r1 = 3r5 = 0

r4 = load(r5)

r7 = r4 * 3

r8 = r2 + 1 r7 = r8 * r

r3 = r2 + 1

r1 = r1 + r store (r1, r3)

  • 10 -

LICM (2)

Ā™

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 set onthe edge

Ā»

if X not executed on every iteration, thenX must provably not cause exceptions

Ā»

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)

  • 12 -

Induction Variable Strength Reduction Ā™

Create basic inductionvariables from derivedinduction variables

Induction variable

BIV (i++)

y

DIV (j = i * 4)

y

DIV can be converted into aBIV that is incremented by 4

Issues

Initial and increment vals

Where to place increments

r5 = r4 - 3

r4 = r4 + 1

r7 = r4 * r

r6 = r4 << 2

  • 13 -

Induction Variable Strength Reduction (2)

Ā™

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

Ā™

Transformation

Ā»

Insert the following into the preheader

y

new_reg = RHS(X)

Ā»

If opcode(X) is not add/sub, insert to thebottom 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 update ofsrc1(X)

y

new_reg += new_inc

Ā»

Change X

Ɔ

dest(X) = new_reg

r5 = r4 - 3 r4 = r4 + 1

r7 = r4 * r

r6 = r4 << 2

  • 15 -

ILP Optimization

Traditional optimizations

Redundancy elimination

Reducing operation count

ILP (instruction-level parallelism) optimizations

Increase the amount of parallelism and the ability to overlapoperations

Operation count is secondary, often trade parallelism for extrainstructions (avoid code explosion)

ILP increased by breaking dependences

True or flow = read after write dependence

False or (anti/output) = write after read, write after write

  • 16 -

Register Renaming

Ā™

Remove dependences causedby variable re-use

Re-use of source variables

Re-use of temporaries

Anti, output dependences

Ā™

Create a new variable to holdeach unique life time

Ā™

Very simple transformationwith straight-line code

Make each def a uniqueregister

Substitute new name intosubsequent uses

a: r1 = r2 + r3b: r3 = r4 + r5c: r1 = r7 * r8d: r7 = r1 + r5e: r1 = r3 + 4f: r4 = r7 + 4

a: r1 = r2 + r3b: r

= r4 + r

c: r

= r7 * r

d: r

= r

+ r

e: r

= r

f: r

= r

  • 18 -

Rename with Copy

Ā™

Renaming within a web

The worst case is a web spansall defs/uses

Want to enable some of thedefs within the web to bereordered or executed inparallel

Ā™

Xform

Rename def

Rename uses for which def isthe the only reaching def

Insert copy

y

orig_dest = new_dest

y =

= y

y = = y

= y

y =

= y

= y

  • 19 -

Predicate Promotion

Ā™

Predicate promotion or predicatespeculation

Remove dependence betweenCMPP and predicated operation

Modify predicate of an operationto an ancestor predicate

Operation executes more oftenthan it should, ā€œspeculatedā€

Ā™

x = … if p

Ɔ

if p

Where p2 is an ancestor of p

Legal if x not live on p2 – p

And, op will not cause aspurious exception

r1 = r2 + r3 r7 = 0 p1,p2 = CMPP.UN.UC(r1 < r5) r4 = r5 * r6 if p1 r7 = r8 + r9 if p2 r10 = r4 + 4 if p1 r11 = r7 + 1 if T