1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
Jeffrey D. Ullman Stanford University. 2  Generalizes: 1.Moving loop-invariant computations outside the loop. 2.Eliminating common subexpressions. 3.True.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 Copy Propagation What does it mean? Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
CS 536 Spring Global Optimizations Lecture 23.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 CS 201 Compiler Construction Data Flow Analysis.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
What’s in an optimizing compiler?
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Program Representations. Representing programs Goals.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
Loops Simone Campanoni
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Code Optimization Overview and Examples
Lecture 5 Partial Redundancy Elimination
Machine-Independent Optimization
Code Generation Part III
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Optimizations using SSA
Data Flow Analysis Compiler Design
Static Single Assignment
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Code Optimization.
Presentation transcript:

1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments to x or y. Similar to register coalescing, which eliminates copies from one register to another. When is it performed? –At any level, but usually early in the optimization process. What is the result? –Smaller code

2 Copy Propagation Local copy propagation –Performed within basic blocks –Algorithm sketch: traverse BB from top to bottom maintain table of copies encountered so far modify applicable instructions as you go

3 Copy Propagation Algorithm sketch for a basic block containing instructions i 1, i 2,..., i n for instr = i 1 to i n if instr is of the form 'res = opd1 op opd2' flag = replace(opd1, instr) || replace(opd2, instr); /* no short-circuit */ if instr is of the form 'res = var' flag = replace(var, instr); /* replaces var with var2 */ if flag is true /* then we need to update the table */ if instr is of the form 'res = opd1 op opd2' if the table contains any pairs involving res, remove them if instr is of the form 'res = var' insert {(res, var2)} in the table endfor replace(opd, instr) if you find (opd, x) in table /* use hashing for faster access */ replace the use of opd in instr with x return true return false

4 Copy Propagation step instruction updated instruction table contents 1 b = a b = a {(b,a)} 2 c = b + 1 c = a + 1 {(b,a)} 3 d = b d = a {(b,a), (d,a)} 4 b = d + c b = a + c {(d,a)} 5 b = d b = a {(d,a), (b,a)} Example: Local copy propagation on basic block: b = a c = b + 1 d = b b = d + c b = d Note: if there was a definition of 'a' between 3 and 4, then we would have to remove (b,a) and (d,a) from the table. As a result, we wouldn't be able to perform local copy propagation at instructions 4 and 5. However, this will be taken care of when we perform global copy propagation.

5 Copy Propagation Global copy propagation –Performed on flow graph. –Given copy statement x=y and use w=x, we can replace w=x with w=y only if the following conditions are met: 1. x=y must be the only definition of x reaching w=x –This can be determined through ud-chains there may be no definitions of y on any path from x=y to w=x. –Use iterative data flow analysis to solve this. »Even, better, use iterative data flow analysis to solve both problems at the same time.

6 Copy Propagation Data flow analysis to determine which instructions are candidates for global copy propagation –forward direction –gen[Bi] = {(x,y,i,p) | p is the position of x=y in block Bi and neither x nor y is assigned a value after p} –kill[Bi] = {(x,y,j,p) | x=y, located at position p in block Bj  Bi, is killed due to a definition of x or y in Bi } –in[B]=  out[P] where P is a predecessor –Initialize in[B1]= , in[B]=U for B  B1

7 Copy Propagation entry exit c = a + b d = c e = d * d f = a + c g = e a = g + d a < c h = g + 1 f = d - g f > a b = g * a h < f entry exit c = a + b d = c e = c * c f = a + c g = e a = e + c a < c h = e + 1 f = c - e f > a b = e * a h < f dead code?

8 Copy Propagation Copy propagation will not detect the opportunity to replace x with y in the last block below: Copy propagation may generate code that does not need to be evaluated any longer. –This will be handled by optimizations that perform redundancy elimination. z >0 x = y w = x + z Mini quiz: which optimization can handle this? Answer: If we perform an optimization similar to code hoisting (i.e. one that would move the copy either up or down the graph) then copy propagation will be able to update "w=y+z"

9 Constant Propagation What does it mean? –Given an assignment x = c, where c is a constant, replace later uses of x with uses of c, provided there are no intervening assignments to x. Similar to copy propagation Extra feature: It can analyze constant-value conditionals to determine whether a branch should be executed or not. When is it performed? –Early in the optimization process. What is the result? –Smaller code –Fewer registers

10 Redundancy Elimination Several optimizations deal with locating and appropriately eliminating redundant calculations. These optimizations require data flow analysis They include –common subexpression elimination –loop-invariant code motion –partial-redundancy elimination –code hoisting

11 Common Subexpression Elimination Local common subexpression elimination –Performed within basic blocks –Algorithm sketch: traverse BB from top to bottom maintain table of expressions evaluated so far –if any operand of the expression is redefined, remove it from the table modify applicable instructions as you go –generate temporary variable, store the expression in it and use the variable next time the expression is encountered. x = a + b... y = a + b t = a + b x = t... y = t

12 Common Subexpression Elimination 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 go to L t1 = a + b c = t1 t2 = m * n d = t2 t3 = b + d e = t3 f = t1 g = -b h = t1 /* commutative */ a = j + a k = t2 j = t3 a = -b if t2 go to L the table contains quintuples: (pos, opd1, opr, opd2, tmp)

13 Common Subexpression Elimination Global common subexpression elimination –Performed on flow graph –Requires available expression information In addition to finding what expressions are available at the endpoints of basic blocks, we need to know where each of those expressions was most recently evaluated (which block and which position within that block).

14 Common Subexpression Elimination Global common subexpression elimination –Algorithm sketch: For each block B and each statement x=y+z, s.t. {y+z}  in[B] i.Find the evaluation of y+z that reaches B, say w=x+y ii.Create temporary variable t iii.Replace [w=y+z] with [t=y+z; w=t] iv.Replace [x=y+z] with [x=t] –Notes: This method will miss the fact that b and d have the same value: a = x+y c = x+y b = a * z d = c * z Mini quiz: which optimization can handle this? Answer: Value Numbering

15 Common Subexpression Elimination entry exit c = a + b d = a * c e = d * d f = a + b c = c * 2 c > d g = a * cg = d * d g > 10 entry exit t1 = a + b c = t1 d = a * c t2 = d * d e = t2 f = t1 c = c * 2 c > d g = a * cg = t2 g > 10

16 Loop-Invariant Code Motion What does it mean? –Computations that are performed in a loop and have the same value at every iteration are moved outside the loop. Before we go on: What is a loop? –A set of basic blocks with a single entry point called the header, which dominates all the other blocks in the set and at least one way to iterate (i.e. go back to the header) –Block Bi dominates block Bj if every path from the flow graph entry to Bj goes through Bi –A loop can be identified by finding an flow graph edge Bj  Bi (called a back edge) s.t. Bi dominates Bj and then finding all blocks that can reach Bj without going through Bi

17 (Loops) B1 B2 B3 B4 B5B6 B7 B8 B9B10 entry exit The dominator tree shows the dominator relation: each node in the tree is the immediate dominator of its children. Example: B7 is dominated by B1, B3, and B4, but its immediate (closest) dominator is B4 Note: B5 does not dominate B7 because we can go from the entry to B7 through the B6 path. B1 B2B3 B4 B5B6B7 B8 B9B10

18 (Loops) back edge: B9  B1 loop: {B9, B8, B7, B10, B6, B5, B4, B3, B2, B1} B1 B2 B3 B4 B5B6 B7 B8 B9B10 entry exit back edge: B10  B7 loop: {B10, B8, B7} back edge: B8  B3 loop: {B8, B7, B10, B6, B5, B4, B3 } back edge: B7  B4 loop: {B7, B10, B6, B5, B8, B4} back edge: B4  B3 loop: {B4, B7, B10, B8, B6, B5, B3}

19 Loop-Invariant Code Motion How do we identify loop-invariant computations? –Easy: use ud-chains –But also: If an computation i depends on a loop-invariant computation j, then i is also loop-invariant. This gives rise to an inductive definition of loop-invariant computations An instruction is loop-invariant if, for each operand: 1. The operand is constant, OR 2. All definitions of that operand that reach the instruction are outside the loop, OR 3. There is exactly one in-loop definition of the operand that reaches the instruction, and that definition is loop invariant

20 Loop-Invariant Code Motion Algorithm sketch: 1.Find all loop-invariant instructions 2.For each instruction i: x=y+z found in step 1, check i.that its block dominates all exits of the loop ii.that x is not defined anywhere else in the loop iii.that all uses of x in the loop can be reached only by i (i.e. its block dominates all uses of x) 3.Move each instruction i that satisfies the requirements in step 2 to a newly created pre-header of the loop, making certain that any operands (such as y, z) have already had their definitions moved to the pre-header. Note: –When applying loop-invariant code motion to nested loops, work from the innermost loop outwards.

21 Loop-Invariant Code Motion entry exit b = 2 i = 1 d = a + d e = 1 + d d = -c f = 1 + a i = i+1 a < 2 a = b+1 c = 2 i mod 2 = 0 Mini quiz: What happens if you perform constant propagation followed by constant folding after the loop-invariant code motion in this loop? T F

22 Loop-Invariant Code Motion entry exit b = 2 i = 1 d = a + d e = 1 + d d = -c f = 1 + a i = i+1 a < 2 a = b+1 c = 2 i mod 2 = 0 entry exit b = 2 i = 1 a = b+1 c = 2 t1 = a<2 d = a + d e = 1 + d d = -c f = 1 + a i = i+1 t1 i mod 2 = 0 T F T F

23 after constant propagation and constant folding entry exit b = 2 i = 1 a = b+1 c = 2 t1 = a<2 d = a + d e = 1 + d d = -c f = 1 + a i = i+1 t1 i mod 2 = 0 T F entry exit b = 2 i = 1 a = 3 c = 2 t1 = false d = a + d e = 1 + d d = -c f = 1 + a i = i+1 t1 i mod 2 = 0 F