










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











February 25, 2015 Reuse Optimization II 1
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
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 +
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
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
More reuse optimization Common subexpression elimination (CSE) Partial redundancy elimination (PRE)
February 25, 2015 Reuse Optimization II 7
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
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...
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 e Avail(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
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
uses defs
February 25, 2015 Reuse Optimization II 13
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
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 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
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
n x + y x + y n x + y x + y
February 25, 2015 Reuse Optimization II 19
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
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
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
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
expr expr delete computation
February 25, 2015 Reuse Optimization II 25
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
Data-flow Equations? February 25, 2015 Reuse Optimization II 26
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?
expr p 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 27 insert computation
expr expr delete computation February 25, 2015 Reuse Optimization II 28
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?
February 25, 2015 Reuse Optimization II 31
Intuition Perform insertion at tops of the chain Perform deletion at the bottoms of the chain Functions insert[n] = ppout[n]
ppout[n]? No Can we omit this clause? February 25, 2015 Reuse Optimization II 32
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
February 25, 2015 Reuse Optimization II 33
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
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