Docsity

Partial Redundancy Elimination, Exercises of Compiler Design

Toggle sidebar
bg1
Calvin Lin
The University of Texas at Austin
CS380 C Compilers 1
February 25, 2015 Reuse Optimization II 1
Loop Invariant Code Motion
Last Time
Loop invariant code motion
Value numbering
Today
Finish value numbering
More reuse optimization
Common subexpression elimination
Partial redundancy elimination
Next Time
Something special
February 25, 2015 Reuse Optimization II 2
Comparing Optimistic and Pessimistic Approaches
Differences
Handling of loops
Pessimistic makes worst-case assumptions on back edges
Optimistic requires actual contradiction to split classes
w0= 5
x0= 5
w1=ϕ(w0,w2)
x1=ϕ(x0,x2)
w2= w1+1
x2= x1+1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Partial Redundancy Elimination and more Exercises Compiler Design in PDF only on Docsity!

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 1

Loop Invariant Code Motion

Last Time  Loop invariant code motion  Value numbering Today  Finish value numbering  More reuse optimization  Common subexpression elimination  Partial redundancy elimination Next Time  Something special February 25, 2015 Reuse Optimization II 2

Comparing Optimistic and Pessimistic Approaches

Differences  Handling of loops  Pessimistic makes worst-case assumptions on back edges  Optimistic requires actual contradiction to split classes w 0 = 5 x 0 = 5 w 1 = ϕ (w 0 ,w 2 ) x 1 = ϕ (x 0 ,x 2 ) w 2 = w 1 + x 2 = x 1 +

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 3 Single global result  Variables correspond to values No data flow analysis  Optimistic: Iterate over congruence classes, not CFG nodes  Pessimistic: Visit each assignment once f - functions  Make data-flow merging explicit  Treat like normal functions

Role of SSA

a = b

... a = c ... a = d a 1 = b ... a 2 = c ... a 3 = d a not congruent to anything Congruence classes: {a 1 ,b}, {a 2 ,c},{a 3 ,d} February 25, 2015 Reuse Optimization II 4

Reuse Optimization

More reuse optimization  Common subexpression elimination (CSE)  Partial redundancy elimination (PRE)

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 7

Another CSE Example

Before CSE c := a + b d := m & n e := b + d f := a + b g := - b h := b + a a := j + a k := m & n j := b + d a := - b if m & n goto L Summary 11 instructions 12 variables 9 binary operators After CSE t1 := a + b c := t t2 := m & n d := t t3 := b + d e := t f := t g := - b h := t a := j + a k := t j := t a := - b if t2 goto L Summary 14 instructions 15 variables Which is better? 4 binary operators February 25, 2015 Reuse Optimization II 8

CSE Approach 1

Idea  If block b uses expression e, and e is available  Search backward from b (in CFG) to find the statement on each path that most recently generates e  Insert copy to n after generators  Replace e with n Is this a good approach? ...x+y... ...x+y... b^ ... x+y... x and y not defined along yellow edges e entry ...x+y...

The University of Texas at Austin

Notation  Avail(b) is the set of expressions available at block b  Gen(b) is the set of expressions generated and not killed at block b If we use e and eAvail(b)  Allocate a new name n  Search backward from b (in CFG) to find statement on each path that most recently generates e  Insert copy to n after generators  Replace e with n Problems?  Backward search for each use is expensive  Generates unique name for each use  |names|  |Uses| > |Avail|  Each generator may have many copies b: Example a := b + c t1 := a t2 := a b: e := b + c f := b + c February 25, 2015 Reuse Optimization II 9

CSE Approach 1 (cont)

February 25, 2015 Reuse Optimization II 10 Idea  Reduce number of copies by assigning a unique name to each unique expression Summary  e Name[e] = unassigned  if we use e and e  Avail(b)  if Name[e]=unassigned, allocate new name n and Name[e] = n else n = Name[e]  Replace e with n  In a subsequent traversal of block b, if e  Gen(b) and Name[e]  unassigned, then insert a copy to Name[e] after the generator of e Problem  Requires two passes over the code  May still insert unnecessary copies

CSE Approach 2

uses defs

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 13

Extraneous Copies

Extraneous copies degrade performance Let other transformations deal with them  Dead code elimination  Copy propagation  Greatly simplifies CSE Coalesce assignments to t1 and t2 into a single statement t1 := b + c t2 := t February 25, 2015 Reuse Optimization II 14

Loop Invariant Code Motion

Last Time  Loop invariant code motion  Value numbering Today  Finish value numbering  More reuse optimization  Common subexpression elimination  Partial redundancy elimination Next Time  Something special

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 15 Partial Redundancy  An expression ( e.g., x+y ) is partially redundant at node n if some path from the entry node to n evaluates x+y , and there are no definitions of x or y between the last evaluation of x+y and n Question  Can we remove partially redundant code?  Yes. It’s a three step process

Partial Redundancy Elimination (PRE)

n (^) x + y x + y February 25, 2015 Reuse Optimization II 16 Partial Redundancy  An expression ( e.g., x+y ) is partially redundant at node n if some path from the entry node to n evaluates x+y , and there are no definitions of x or y between the last evaluation of x+y and n Three Steps  Discover partially redundant expressions  Convert them to fully redundant expressions  Remove the redundancy Is this beneficial?  PRE subsumes CSE and loop invariant code motion n x + y x + y x + y

Partial Redundancy Elimination (PRE)

n x + y x + y n x + y x + y

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 19

Local Properties

An expression is locally transparent in block b if its operands are not modified in b An expression is locally available in block b if it is computed at least once and its operands are not modified after its last computation in b An expression is locally anticipated if it is computed at least once and its operands are not modified before its first evaluation Example a := b + c d := a + e Questions? Transparent: Available: Anticipated: { b + c } { b + c, a + e } { b + c } February 25, 2015 Reuse Optimization II 20

Local Properties (cont)

How are these properties useful?  They tell us where we can introduce redundancy  In which direction does anticipation flow? The expression can be redundantly evaluated anywhere after its last evaluation in the block The expression can be redundantly evaluated anywhere in the block Transparent Available a^ =^ b^ +^ c The expression can be redundantly evaluated anywhere before its first evaluation in the block Anticipated (^) a = b + c

The University of Texas at Austin

Example  For each block in the following figure, what are the local properties with respect to expr?  Where can e be computed redundantly? February 25, 2015 Reuse Optimization II 21

Local Properties (cont)

expr expr Big picture  Use local properties ( available and anticipated ) to determine where redundancy can be created within a basic block  Use global analysis (data-flow analysis) to discover where partial redundancy can be converted to full redundancy  Insert code and remove redundant expressions February 25, 2015 Reuse Optimization II 22 insert computation

Implementing PRE

expr expr delete computation

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 25

(Globally) Partially Available

Intuition  An expression is partially available if it is available along some path  If e is partially available at p, then  a path from the entry node to p such that the evaluation of e at p would give the same result as the previous evaluation of e along the path expr p

partially_available_in[n] =  ppred[n] partially_available_out[p]

partially_available_out[n] = locally_available[n] 

(partially_available_in[n]  transparent[n])

Data-flow Equations? February 25, 2015 Reuse Optimization II 26

Globally Anticipated

Intuition  If e is globally anticipated at p, then adding an evaluation of e at p will make e redundant along all paths from p, ie, you’re expecting e to be computed in the future Data-flow Equations?

anticipated_out[n] = ssucc[n] anticipated_in[s]

anticipated_in[n] = locally_anticipated[n] 

(anticipated_out[n]  transparent[n])

expr p expr expr

The University of Texas at Austin

Big picture  Use local properties ( available and anticipated ) to determine where redundancy can be created within a basic block  Use global analysis (data-flow analysis) to discover where partial redundancy can be converted to full redundancy  Insert code and remove redundant expressions February 25, 2015 Reuse Optimization II 27 insert computation

Implementing PRE

expr expr delete computation February 25, 2015 Reuse Optimization II 28

Global Possible Placement

Goal  Convert partial redundancies to full redundancies  Possible Placement is a backwards analysis that identifies locations where such conversions can take place  e  ppin[n] can be placed at entry of n  e  ppout[n] can be placed at exit of n Start with locally anticipated expressions Push Possible Placement backwards as far as possible How?

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 31

Updating Blocks (cont)

Intuition  Perform insertion at tops of the chain  Perform deletion at the bottoms of the chain Functions  insert[n] = ppout[n]

 (ppin[n]   transparent[n])

available_out[n]

 delete[n] = ppin[n]  locally_anticipated[n]

 ppout[n]? No Can we omit this clause? February 25, 2015 Reuse Optimization II 32

Exercise

B1: a := b + c B2: b := b + 1 B3: a := b + c B1 B2 B transparent locally_available locally_anticipated available_in available_out partially_available_in partially_available_out anticipated_out anticipated_in ppout ppin insert delete

The University of Texas at Austin

February 25, 2015 Reuse Optimization II 33

Comparing Redundancy Elimination

Value numbering  Examines values not expressions  Symbolic  Knows nothing about algebraic properties (1+x = x+1) CSE  Examines expressions PRE  Examines expressions  Subsumes CSE and loop invariant code motion  Simpler implementations are now available Constant propagation  Requires that values be statically known February 25, 2015 Reuse Optimization II 34

PRE Summary

What’s so great about PRE?  A modern optimization that subsumes earlier ideas  Composes several simple data-flow analyses to produce a powerful result  Finds earliest and latest points in the CFG at which an expression is anticipated