partial redundancy elimination and ssa form

25
Topic 5a Topic 5a Topic 5a Topic 5a Partial Redundancy Partial Redundancy Partial Redundancy Partial Redundancy Elimination and SSA Form Elimination and SSA Form Elimination and SSA Form Elimination and SSA Form 2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic- 7b.ppt 1 Elimination and SSA Form Elimination and SSA Form Elimination and SSA Form Elimination and SSA Form

Upload: others

Post on 20-Apr-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Partial Redundancy Elimination and SSA Form

Topic 5aTopic 5aTopic 5aTopic 5a

Partial Redundancy Partial Redundancy Partial Redundancy Partial Redundancy

Elimination and SSA FormElimination and SSA FormElimination and SSA FormElimination and SSA Form

2008-03-26

\course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-

7b.ppt 1

Elimination and SSA FormElimination and SSA FormElimination and SSA FormElimination and SSA Form

Page 2: Partial Redundancy Elimination and SSA Form

References

• Robert Kennedy, Sun Chan, Shin-ming Liu, Raymond Lo, Pend

Tu, Fred Chow. Partial redundancy elimination in SSA Form.

ACM Trans. On Programming Languages and Systems, 21(3),

May 1999: 627-676.

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 2

• Drechsler K. and Stadel M. A variation of Knoop, Ruthing, and

Steffen’s lazy code motion. SIGPLAN Notices, 28(5), May,

1993: 29-38.

• Keith Cooper, Linda Torczon. Engineering a compiler. 2004

Page 3: Partial Redundancy Elimination and SSA Form

Outline

• Partial Redundancy Elimination (PRE)

• SSAPRE (a short summary)

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 3

• SSAPRE (a short summary)

Page 4: Partial Redundancy Elimination and SSA Form

KCC Compiler InfrastructureC++Fortran C

fe90 gfec gfecc

Very High WHIRL

High WHIRL

Middle WHIRL

•Source to IR (Scanner →Parser →RTL →WHIRL)

•VHO (Very High WHIRL Optimizer)•Standalone Inliner•W2C/W2F

•IPA (inter-procedural analysis & opt)•LNO (Loop unrolling/fission/fusion/tiling/peeling etc)•PREOPT (point-to analysis etc)

•WOPT- SSAPRE (Partial Redundancy Elimination)- VNFRE (Value numbering based full redundancy elim.)

Machin

e M

odel

Fro

nt e

nd

Mid

dle

end

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 4

Asm file

Middle WHIRL

Low WHIRL

Very Low WHIRL

CGIR

- VNFRE (Value numbering based full redundancy elim.)• RVI-1 (Register Variable Identification)

•RVI-2

•Some peephole opt

•Cflow (control flow opt)

•EBO (extended block opt.)

•PQS (predicate query system)

•Loop Opt (Unrolling + SWP)

•GCM (global code motion), HB sched (hyperblk schedule)

• GRA/LRA (global/local register alloc)

Machin

e M

odel

Back e

nd

Page 5: Partial Redundancy Elimination and SSA Form

Important Observation

• An operand of a φ function is regarded as

occurring at the end of its corresponding

predecessor block.

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 5

predecessor block.

• The result of a φφφφ function is regarded as

occurring at the beginning of the block

that contains it.

Page 6: Partial Redundancy Elimination and SSA Form

Partial Redundancy

Elimination

B1

B

t←←←←x*y

a ←←←←t

a ←←←←x*y t←←←←x*y

Is the program in SSA form before and after

PRE?

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 6

b ←←←←x*y

b ←←←←t

B2

B3

X*y is partially redundant here

a ←←←←x*y t←←←←x*y

X*y is made redundant here

Page 7: Partial Redundancy Elimination and SSA Form

Problem Formulation for PRE

• For an expression, identify its partially redundant occurrences

• Safely insert occurrences (to new temporaries) at some points

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 7

temporaries) at some points

• Delete the original partially (now fully) redundant occurrences. Replace them by loads from a temporary variable.

Page 8: Partial Redundancy Elimination and SSA Form

PRE Basics

• Availability: An expression, say, x*y, is

available at a program point p if

� EVERY path from entry to p evaluates the

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 8

� EVERY path from entry to p evaluates the

expression before reaching p

� And there are no assignments to x or y after the

(last) evaluation but before p (on all paths).

• Availability implies full redundancy.

Page 9: Partial Redundancy Elimination and SSA Form

PRE Basics (Cont.)

• Partial availability: An expression, say, x*y,

is partially available at a program point p if

� SOME paths from entry to p evaluates the expression

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 9

� SOME paths from entry to p evaluates the expression

before reaching p

� And there are no assignments to x or y after the (last) such

evaluation but before p.

• Partial availability implies partial redundancy.

Page 10: Partial Redundancy Elimination and SSA Form

PRE Basics (Cont.)

• Anticipatability: An expression e is anticipatable (

“down-safe”) at a program point p if it appears

(without redefinition) along ALL paths from

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 10

• p to an exit of the program

B2 a ←←←←x*y b ←←←←x*y B3

B1

X*y is anticipatable

here

Page 11: Partial Redundancy Elimination and SSA Form

Safe Placement

- Safe insertion: If an inserted computation occurs

at a point where the computation is anticipated.

- Unsafe insertion: means that some original path

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 11

- Unsafe insertion: means that some original path

in the program did not contain this computation.

� May increase the execution time of the program

� May cause exceptions

Page 12: Partial Redundancy Elimination and SSA Form

Safe Placement: Example

x*y

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 12

x*ySafe

Not safe

This is called an “critical edge”.

Allowable if we can insert

computations on an edge. Otherwise,

need split

Page 13: Partial Redundancy Elimination and SSA Form

Optimal Placement (Cont’d)

• Computationally optimal

� If no other safe placement can result in

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 13

fewer occurrences of the computation along

any path from entry to exit in the program.

Page 14: Partial Redundancy Elimination and SSA Form

Optimal Placement (cont’d)

• Lifetime optimal

� Minimize the lifetimes of the introduced

temporaries.

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 14

temporaries.

� Intuition: delay an expression to the latest

point (Lazy Code Motion [KnoopEtal92,

DrechslerStadel93])

Page 15: Partial Redundancy Elimination and SSA Form

Outline

• Partial redundancy elimination

(PRE)

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 15

• SSAPRE (a short summary)

Page 16: Partial Redundancy Elimination and SSA Form

SSAPRE

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 16

SSAPRE

Page 17: Partial Redundancy Elimination and SSA Form

SSAPRE: Motivation

• Traditional data flow analysis based

on bit-vectors do not interface well

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 17

with SSA representation

Page 18: Partial Redundancy Elimination and SSA Form

Traditional Partial

Redundancy Elimination

a ←←←←x*y

b ←←←←x*y

B1 B2

B3

SSA form

Before PRE

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 18

t←←←←x*y

a ←←←←tt←←←←x*y

b ←←←←t

B1 B2

B3

Not SSA

form!

After PRE

Page 19: Partial Redundancy Elimination and SSA Form

SSAPRE: Motivation (Cont.)

• Traditional PRE needs a postpass

transform to turn the results into SSA

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 19

form again.

• SSA is based on variables, not on

expressions

Page 20: Partial Redundancy Elimination and SSA Form

SSAPRE: Motivation (Cont.)

• Representative occurrence

� Given a partially redundant occurrence E0,

we define the representative occurrence for

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 20

we define the representative occurrence for

E0 as the nearest to E0 among all

occurrences that dominate E0.

� Unique and well-defined

Page 21: Partial Redundancy Elimination and SSA Form

FRG (Factored Redundancy

Graph)

Control flow

E E E E

E =ΦΦΦΦ(E, E, ⊥⊥⊥⊥)

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 21

Redundancy edge

Without factoring

flow edge

E E

Factored

E E

E =ΦΦΦΦ(E, E, ⊥⊥⊥⊥)

Page 22: Partial Redundancy Elimination and SSA Form

FRG (Factored Redundancy

Graph)

E E

E =ΦΦΦΦ(E, E, ⊥⊥⊥⊥)

t0 ←←←← x*y t1 ←←←← x*y

t2←←←←ΦΦΦΦ(t0, t1 , t3)

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 22

E E

E =ΦΦΦΦ(E, E, ⊥⊥⊥⊥) t2←←←←ΦΦΦΦ(t0, t1 , t3)

←←←← t2 ←←←← t2

Assume E=x*yNote: This is in SSA form

Page 23: Partial Redundancy Elimination and SSA Form

Observation

• Every use-def edge of the temporary

corresponds directly to a redundancy

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 23

edge for the expression.

Page 24: Partial Redundancy Elimination and SSA Form

Intuition for SSAPRE

• Construct FRG for each expression E

• Refine redundant edges to form the use-def

relation for each expression E’s temporary

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 24

relation for each expression E’s temporary

• Transform the program

� For a definition, replace with t←E

� For a use, replace with ←t

� Sometimes need also insert an expression

Page 25: Partial Redundancy Elimination and SSA Form

a1←←←←

a2←φ←φ←φ←φ(a4 , a1)

←←←←a2+b

B1

B2

B

a1←←←←

t1←←←← a1+ b1

t2←φ←φ←φ←φ(t4 , t1)

a2←φ←φ←φ←φ(a4 , a1)

←←←←t2

a ←←←←

B1

B2

An Example

2008-03-26 \course\cpeg421-08s\Topic-5a.ppt\course\cpeg421-07s\Topic-7b.ppt 25

2

a3←←←←

a4←φ←φ←φ←φ(a2, a3)

←←←←a4+b

exit

B3

B4

B5

a3←←←←

t3←←←←a3+ b1

t4←φ←φ←φ←φ(t2, t3)a4←φ←φ←φ←φ(a2, a3)

←←←←t4

exit

B4