Docsity

Compiler Optimization: Induction Variable Elimination & Predicate Promotion - Prof. Scott , Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Compiler Optimization: Induction Variable Elimination & Predicate Promotion - Prof. Scott and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 583 – Class 12 Finish ILP Optimization Intro to Code Generation

University of Michigan February 16, 2005

  • 1 -

Reading Material

Y

Today’s class

»

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

Y

Material for the next lecture

  • 3 -

From Last Time - Class Problem (continued)

r1 = 0; r2 = 0^ r111 = r5 * 2 r110 = r111 + 2 r109 = r1 << 1 r104 = r109 – 10

r113 = r2 – 1

r1 = 0; r2 = 0^ r111 = r5 * 2 r110 = r111 + 2 r109 = r1 << 1 r104 = r109 – 10

r113 = r2 – 1 r200 = r104 – r r201 = r104 – r110 + 4

apply ind var elimination

r12 = load (r110+0) r3 = load(r110+r201)

r3 = r3 + 1

store(r110+r200, r3)

r7 = r3 << 2 r6 = load(r7+0) r104 = r104 + 2 r113 = r113 + 1 r6, r110, r12, r113 live

r5 = r5 + 1 r111 = r111 + 2 r110 = r110 + 2 r12 = load (r110+0)^ r3 = load(r104+4)

r3 = r3 + 1 store(r104+0, r3)

r7 = r3 << 2 r6 = load(r7+0)

r1 = r1 + 1 r109 = r109 + 2 r104 = r104 + 2

r2 = r2 + 1 r113 = r113 + 1

eliminate r1 – not used eliminate r2 – not used eliminate r5 – not used eliminate r109 – not used eliminate r111 – not used eliminate r104 – replace

with r

Can also simplify preheader – not shown

r6, r110, r12, r113 live

  • 4 -

From Last Time: 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

  • 6 -

From Last Time - Class Problem (continued)

T

p p

p

px

1. Promote everything to its highest predicate w/o renaming 2. Promote any defs of r1, r2 that remain predicated to True

using promotion with renaming

Try to promote each instruction to an ancestor predicate Op 1: already True, no promotion possible Op2: cannot promote CMPPs Op 3: p1’s only ancestor is True, so try to promote to True

Immediately after op3, Is r2 live under T – p1 = px? live(r2) = p2, p

px = null, thus can promote to T

1: r1 = 0 if T 2: p1 = CMPP.UN(r3 < r4) if T 3: r2 = r6 + 3 if p1 4: p2,p3 = CMPP.UN.UC(r5 < r6) if p1 5: r1 = r5 + 1 if p2 6: r10 = r2 + r3 if p2 7: r1 = r3 * 3 if p3 8: r11 = load(r1) if p3 9: store (r1, r10) if T 10: store (r3, r11) if T

  • 7 -

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

  • 9 -

Fancier Tree Height Reduction

after back subs:

Y

Take advantage of literals

Reassociate to maximizeopportunities for combiningliterals at compile time

Reduces amount ofcomputation

r13 = r1 + 4 + r2 - 3 + r3 - 6

r1 + r

r3 – 5

r

reassociate:r13 = r1 + r2 + r3 + (4 - 3 – 6)simplify:r13 = r1 + r2 + r3 - 5balance:

  • 10 -

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)

  • 12 -

Optimizing Unrolled Loops

loop:

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

loop:

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

iter

unroll 3 times

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

iter

Unroll = replicate loop body n-1 times. Hope to enable overlap of operation execution from different iterations Not possible!

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

iter

  • 13 -

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

  • 15 -

Accumulator Variable Expansion

Y

Accumulator variable

x = x + y or x = x – y

where y is loop variant

Y

Create n-1 temporaryaccumulators

Y

Each iteration targets adifferent accumulator

Y

Sum up the accumulatorvariables at the end

Y

May not be safe for floating-point values

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

iter1 iter2 iter

loop:

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

  • 16 -

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 !!

  • 18 -

Class Problem

loop:

loop:

r1 = load(r2) r5 = r6 + 3 r6 = r5 + r1 r2 = r2 + 4

r1 = load(r2) r5 = r6 + 3 r6 = r5 + r1 r2 = r2 + 4 if (r2 < 400) goto loop

r1 = load(r2) r5 = r6 + 3 r6 = r5 + r1 r2 = r2 + 4 r1 = load(r2) r5 = r6 + 3 r6 = r5 + r1 r2 = r2 + 4 if (r2 < 400) goto loop

Optimize the unrolled loop Renaming Ind/Acc expansion

  • 19 -

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

»

  1. Register allocation

y

Map virtual

Æ

physical registers

y

Spill to memory if necessary