from speculative partial redundancy elimination to ...amaral/cascon/cdp04/slides/horsp… ·...

30
From Speculative Partial Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University of Victoria, Canada David Pereira, University of Victoria, Canada in collaboration with Bernhard Scholz, University of Sydney, Australia Jens Knoop, Vienna University of Technology, Austria Slide 1

Upload: others

Post on 18-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

From Speculative Partial Redundancy Elimination to Speculative Partial Dead Code Elimination

Nigel Horspool, University of Victoria, CanadaDavid Pereira, University of Victoria, Canadain collaboration with

Bernhard Scholz, University of Sydney, AustraliaJens Knoop, Vienna University of Technology, Austria

Slide 1

Page 2: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Synopsis

• This is work in progress.• Some experimental results with SPRE were reported in proceedings

of the Conference on Languages, Compilers, and Tools for Embed-ded Systems (LCTES), June 2004.

• We are exploring ideas to generalize SPRE and to make it a practical approach for use in JIT compilers.

Slide 2

Page 3: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

From PRE to SPRE

Partial Redundancy Elimination

... is a generalization of code motion (Morel and Renvoise, 1979)

...= a+b a =

...= a+b

Slide 3

Page 4: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

From PRE to SPRE

Partial Redundancy Elimination

... is a generalization of code motion (Morel and Renvoise, 1979)

...= a+b a =

...= a+b

t1 = a+b... = t1

a = ...t1 = a+b

... = t1

Slide 3

Page 5: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

From PRE to SPRE

Partial Redundancy Elimination

... is a generalization of code motion (Morel and Renvoise, 1979)

1000 1000

500 500500 500

10001000

...= a+b a =

...= a+b

t1 = a+b... = t1

a = ...t1 = a+b

... = t1

1000 1000

1000

Slide 3

Page 6: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

But... Partial Redundancy Elimination

... is also very conservative. An expression e can be inserted at a point P only if every path starting from P uses e. This restriction guarantees:

• safety, and• optimality (no more evaluations of the expression than before).

...= a+b

Slide 4

Page 7: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

But... Partial Redundancy Elimination

... is also very conservative. An expression e can be inserted at a point P only if every path starting from P uses e. This restriction guarantees:

• safety, and• optimality (no more evaluations of the expression than before).

...= a+b ... = t1

t1 = a+b

?

Slide 4

Page 8: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

But... Partial Redundancy Elimination

... is also very conservative. An expression e can be inserted at a point P only if every path starting from P uses e. This restriction guarantees:

• safety, and• optimality (no more evaluations of the expression than before).

...= a+b ... = t1

t1 = a+b

?10000

11

1

10000

1

Slide 4

Page 9: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

But... Partial Redundancy Elimination

... is also very conservative. An expression e can be inserted at a point P only if every path starting from P uses e. This restriction guarantees:

• safety, and• optimality (no more evaluations of the expression than before).

...= a+b ... = t1

t1 = a+b

?10000

11

1

10000

1

Slide 4

Page 10: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Speculative PRE

• An evaluation of e can be inserted anywhere as long as it is safe to do so,

Slide 5

Page 11: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Speculative PRE

• An evaluation of e can be inserted anywhere as long as it is safe to do so, and

• we speculatively compute e in the hope that the value will be useful later.

Slide 5

Page 12: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Speculative PRE

• An evaluation of e can be inserted anywhere as long as it is safe to do so, and

• we speculatively compute e in the hope that the value will be useful later.

• Using probabilistic information (from execution profiles or elsewhere), the optimality goal becomes minimization of the expected number of evaluations.Cai & Xue [CGO 2003] and Scholz, Horspool & Knoop [LCTES 2004] have presented SPRE algorithms which find optimal solutions.We prefer the LCTES 2004 approach because the objective function can combine several different cost measures.

Slide 5

Page 13: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

SPRE Example

..=a+b

a = ... b = ...

..=a+b

5

1 3

5

13

10

1

#evals=15; #occurrences=2

Slide 6

Page 14: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

SPRE Example – optimized for time

...= h

a =...h = a+b

b =...h = a+b h = a+b

...= h

1 1 3

10

5

13

#evals=5; #occurrences=3

5

..=a+b

a = ... b = ...

..=a+b

5

1 3

5

13

10

1

#evals=15; #occurrences=2

Slide 7

Page 15: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

SPRE Example – optimized for space+time

...= h

a = ... b = ...h = a+b

..=a+b

1 1 3

10

5

13

105

#evals=8; #occurrences=2

..=a+b

a = ... b = ...

..=a+b

5

1 3

5

13

10

1

#evals=15; #occurrences=2

Slide 8

Page 16: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Optimal SPRE

• For each expression, our algorithm maps the optimal SPRE problem to a network flow problem.

• Efficient techniques exist to solve for maximum flow.• The cost function is a linear combination of:

– any static property (size is a useful measure),– execution frequency times any static property

Slide 9

Page 17: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Issues with SPRE and proposed solutions

1. Register pressure

Usually we have too many expressions and too few registers. How can we handle that?

Slide 10

Page 18: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Issues with SPRE and proposed solutions

1. Register pressure

Usually we have too many expressions and too few registers. How can we handle that?

Proposal:

Incorporate a measure of register pressure into the cost function.E.g., lifetime of the value (distance from place where computed to the place where used) can be added into the objective function.Cost of using a saved value =

a × size of the expression +frequency × (b × execution time of original expression +

c × distance from top of block)and there are similar costs elsewhere for keeping the value alive.

Slide 10

Page 19: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Issues with SPRE and proposed solutions

2. Computational cost

Only one expression at a time is analyzed. (It’s not a bit-vector prob-lem.)

Solutions to be tried

• Reduce the size of the constructed network by– applying some basic transformation techniques, and– discarding low frequency regions of the network.

• Incremental analysis (reusing a previous analysis).

Slide 11

Page 20: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Issues with SPRE and proposed solutions

3. Non-reversibility of code transformation

= a+b = h

I IIh = a+b

Slide 12

Page 21: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Issues with SPRE and proposed solutions

3. Non-reversibility of code transformation

• Whether I or II is better depends on the objective function; but we cannot get from II to III using the standard PRE transformations.If the original program is like II, we may want to get to III (to reduce register pressure, to reduce size, ...)

= a+b = a+b = h

I II IIIh = a+b

Slide 12

Page 22: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Partial Dead Code Elimination (PDE)

An expression e is not used on all subsequent execution paths ...

e = a+b

= e a =

a =

Slide 13

Page 23: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Partial Dead Code Elimination (PDE)

An expression e is not used on all subsequent execution paths ...

• we can push the evaluation later to point(s) where the expression is needed on all subsequent paths.(This is assignment sinking.)

e = a+b

= e a = = e a =

e = a+ba =a =

Slide 13

Page 24: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

From PDE to Speculative PDE

• A misnomer because there is no speculation involved.• However, it makes sense to associate costs with expression evalua-

tions which depend on the execution frequency.• Assignment sinking reduces register pressure.• Assignment sinking may increase program size.• Note that optimal solutions will not be found efficiently because of

second order effects – placement of one expression affects the place-ment of another.See example ...

Slide 14

Page 25: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

2nd order effects in SPDE

x = a+ba = c+d

= x= a

= a

5K 10K

5K

3K 3K

5K

Slide 15

Page 26: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

2nd order effects in SPDE

x = a+ba = c+d

= x= a

= a

5K 10K

5K

3K 3K

5K

a = c+d

x = a+ba = c+d= x

a = c+d= a

= a

5K 10K

5K

3K 3K

5KA suboptimal placement of a=c+d must be made before placing x=a+b and achieving the globally optimal solution.

Slide 16

Page 27: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Mapping SPDE to SPRE

e = a+b

= ea =

= e = e

Slide 17

Page 28: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Mapping SPDE to SPRE

• Insert the definition of a variable before each use; then run SPRE to remove redundant evaluations and move them to optimal points.

• If more than one definition reaches a use, we insert the definitions before the point where the control flow paths merge.

• Note: we are ignoring the second-order effects.

e = a+b

= ea =

= e = e

e = a+b

e = a+b = e

a = = ee = a+b = e

Slide 17

Page 29: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

Summary

• Execution profiles permit much better results to be obtained from PRE and PDE optimizations.

• Our cost metric model permits an appropriate trade-off between (esti-mates of)– execution time,– space,– power consumption, and– register pressure.

• Only an implementation in a real compiler will show how well it will work in practice.

Slide 18

Page 30: From Speculative Partial Redundancy Elimination to ...amaral/cascon/CDP04/slides/horsp… · Redundancy Elimination to Speculative Partial Dead Code Elimination Nigel Horspool, University

THANK YOU!

ANY QUESTIONS?