Docsity

ILP Optimization, Introduction to Code Generation - Fall 2003 | EECS 583, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download ILP Optimization, Introduction to Code Generation - Fall 2003 | EECS 583 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 583 – Lecture 14 ILP Optimization, Intro toCode Generation

University of Michigan March 3, 2003

  • 1 -

ILP Optimization

Y

Traditional optimizations^ »

Redundancy elimination

»^

Reducing operation count

Y

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)

Y

ILP increased by breaking dependences^ »

True or flow = read after write dependence

»^

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

  • 3 -

Predicate Promotion Y

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”

Y

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

  • 4 -

Promote with Copy

r7 = 0 p1,p2 = CMPP.UN.UC(r1 < r5) r7 = load(r8) if p2 r12 = r7 +1 if p2 r10 = r4 + 4 if p1 r11 = r7 + 1 if T

Y

Similar to rename with copy^ »

Promotion alone not legalbecause a live valuedestroyed

Y

Rename destination, canpromote to any ancestor^ »

Might as well choose True »^

Substitute uses for which defis the only reaching def »^

Insert copy of old_dest =new_dest if original_ped »^

Again, must ensure operationwill not cause a spuriousexception

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

  • 6 -

Back Substitution Y

Generation of expressions bycompiler frontends is verysequential^ »

Account for operatorprecedence »^

Apply left-to-right withinsame precedence

Y

Back substitution^ »

Create larger expressions^ y

Iteratively substitute RHSexpression for LHS variable

»^

Note – may correspond tomultiple source statements »^

Enable subsequent optis

Y

Optimization^ »

Re-compute expression in amore favorable manner

y = a + b + c – d + e – f;

r9 = r1 + r2 r10 = r9 + r3 r11 = r10 - r4 r12 = r11 + r5 r13 = r12 – r

Subs r12:

r13 = r11 + r5 – r Subs r11:

r13 = r10 – r4 + r5 – r Subs r

r13 = r9 + r3 – r4 + r5 – r Subs r

r13 = r1 + r2 + r3 – r4 + r5 – r

  • 7 -

Tree Height Reduction

original:

r9 = r1 + r2 r10 = r9 + r3 r11 = r10 - r4 r12 = r11 + r5 r13 = r12 – r

Y

Re-compute expression as abalanced binary tree^ »

Obey precedence rules »^

Essentially re-parenthesize

Y

Effects^ »

Height reduced (n terms)^ y

n-1 (assuming unit latency) y^

ceil(log2(n))

»^

Number of operationsremains constant »^

Cost^ y

Temporary registers “live”longer

»^

Watch out for^ y

Always ok for integerarithmetic y^

Floating-point – may notbe!!

after back subs:

r13 = r1 + r2 + r3 – r4 + r5 – r r1 + r

r3 – r

r5 – r

final code:^ t1 = r1 + r2^ t2 = r3 – r4^ t3 = r5 – r6^ t4 = t1 + t2^ r13 = t4 + t

  • r
  • 9 -

Fancier Tree Height Reduction (2)

after back subs:

Y

Apply distributive property»

a(b+c) = ab + bc »^

Or the reverse »^

Danger^ y

Generate more operations y^

Lots of possibilities

Y

Account for latency inbalancing process»

Want latencies balanced, notthe number of operations »^

multiply = 3, add = 1

Y

Account for operand arrivaltime»

Delay use of late arrivingoperands

r13 = r1r3 + r1r4 + r2r3 + r2r4 r1 * r

r1 * r

r2 * r

r

r2 * r

r1 + r

r3 + r

r

or better yet: r13 = (r1 + r2) * (r3 + r4)

  • 10 -

Class Problem

Assume: + = 1, * = 3

operand arrival times

0 r

0 r

0 r

1 r

2 r

0 r

r10 = r1 * r2 r11 = r10 + r3 r12 = r11 + r4 r13 = r12 – r5 r14 = r13 + r

Back susbstitute Re-express in tree-height reduced form

Account for latency and arrival times

  • 12 -

Register Renaming on Unrolled Loop

loop:

loop:

r1 = load(r2) r3 = load(r4) r5 = r1 * r3 r6 = r6 + r5 r2 = r2 + 4 r4 = r4 + 4

r1 = load(r2) r3 = load(r4) r5 = r1 * r3 r6 = r6 + r5 r2 = r2 + 4 r4 = r4 + 4

iter

iter

r1 = load(r2) r3 = load(r4) r5 = r1 * r3 r6 = r6 + r5 r2 = r2 + 4 r4 = r4 + 4

r11 = load(r2) r13 = load(r4) r15 = r11 * r13 r6 = r6 + r15 r2 = r2 + 4 r4 = r4 + 4

iter

iter

r1 = load(r2) r3 = load(r4) r5 = r1 * r3 r6 = r6 + r5 r2 = r2 + 4 r4 = r4 + 4 if (r4 < 400) goto loop

r21 = load(r2) r23 = load(r4) r25 = r21 * r23 r6 = r6 + r25 r2 = r2 + 4 r4 = r4 + 4 if (r4 < 400) goto loop

iter

iter

  • 13 -

Register Renaming is Not Enough!

Y

Still not much overlap possible

Y

Problems^ »

r2, r4, r6 sequentialize theiterations »^

Need to rename these

Y

2 specialized renaming optis^ »

Accumulator variableexpansion (r6) »^

Induction variable expansion(r2, r4)

loop:

r1 = load(r2) r3 = load(r4) r5 = r1 * r3 r6 = r6 + r5 r2 = r2 + 4 r4 = r4 + 4

iter

r11 = load(r2) r13 = load(r4) r15 = r11 * r13 r6 = r6 + r15 r2 = r2 + 4 r4 = r4 + 4

iter

r21 = load(r2) r23 = load(r4) r25 = r21 * r23 r6 = r6 + r25 r2 = r2 + 4 r4 = r4 + 4 if (r4 < 400) goto loop

iter

  • 15 -

Induction Variable Expansion

r1 = load(r2)r3 = load(r4)r5 = r1 * r3r6 = r6 + r5r2 = r2 + 12r4 = r4 + 12r11 = load(r12)r13 = load(r14)r15 = r11 * r13r16 = r16 + r15r12 = r12 + 12r14 = r14 + 12r21 = load(r22)r23 = load(r24)r25 = r21 * r23r26 = r26 + r25r22 = r22 + 12r24 = r24 + 12if (r4 < 400) goto loop

iter1 iter2 iter

loop:

r12 = r2 + 4, r22 = r2 + 8r14 = r4 + 4, r24 = r4 + 8r16 = r26 = 0 r6 = r6 + r16 + r

Y

Induction variable»

x = x + y or x = x – y »^

where y is loop invariant

Y

Create n-1 additional inductionvariables

Y

Each iteration uses andmodifies a different inductionvariable

Y

Initialize induction variables toinit, init+step, init+2*step, etc.

Y

Step increased to n*originalstep

Y

Now iterations are completelyindependent !!

  • 16 -

Better Induction Variable Expansion

Y

With base+displacementaddressing, often don’t needadditional induction variables»

Just change offsets in eachiterations to reflect step »^

Change final increments to n* original step

r1 = load(r2)r3 = load(r4)r5 = r1 * r3r6 = r6 + r5r11 = load(r2+4)r13 = load(r4+4)r15 = r11 * r13r16 = r16 + r15r21 = load(r2+8)r23 = load(r4+8)r25 = r21 * r23r26 = r26 + r25r2 = r2 + 12r4 = r4 + 12if (r4 < 400) goto loop

iter1 iter2 iter

loop:

r16 = r26 = 0 r6 = r6 + r16 + r

  • 18 -

New Topic: Code Generation

Y

Map optimized “machine-independent” assembly to finalassembly code

Y

Input code^ »

Classical optimizations

»^

ILP optimizations

»^

Formed regions, applied if-conversion

Y

Virtual

Æ

physical binding

»^

2 big steps

»^

1. Scheduling^ y

Determine when every operation executions y Create MultiOps

»^

2. Register allocation^ y

Map virtual

Æ

physical registers

y^

Spill to memory if necessary

  • 19 -

What Do We Need to Schedule Operations?

Y

Information about the processor^ »

Number of resources

»^

Which resources are used by each operation

»^

Operation latencies

»^

Operand encoding limitations

»^

For example:^ y

2 issue slots, 1 memory port, 1 adder/multiplier y load = 2 cycles, add = 1 cycle, mpy = 3 cycles; all fully pipelined y Each operand can be register or 6 bit signed literal

Y

Ordering constraints amongst operations^ »

What order defines correct program execution?

»^

Need a precedence graph – flow, anti, output deps^ y

What about memory deps? control deps? Delay slots?