CS 201 Compiler Construction

Slides:



Advertisements
Similar presentations
Continuing Abstract Interpretation We have seen: 1.How to compile abstract syntax trees into control-flow graphs 2.Lattices, as structures that describe.
Advertisements

Overview Structural Testing Introduction – General Concepts
1 SSA review Each definition has a unique name Each use refers to a single definition The compiler inserts  -functions at points where different control.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
1 CS 201 Compiler Construction Machine Code Generation.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
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.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Early Global Program Optimizations Chapter Mooly Sagiv.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
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.
Data Flow Analysis Compiler Design Nov. 3, 2005.
CS 201 Compiler Construction
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
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.
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...
1 CS 201 Compiler Construction Lecture 9 Static Single Assignment Form.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
1 CS 201 Compiler Construction Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Detecting Equality of Variables in Programs Bowen Alpern, Mark N. Wegman, F. Kenneth Zadeck Presented by: Abdulrahman Mahmoud.
Final Code Generation and Code Optimization.
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.
Definition-Use Chains
Introduction to Optimization
Data Flow Analysis Suman Jana
Efficiently Computing SSA
Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm
Introduction to Optimization
University Of Virginia
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.
CS 201 Compiler Construction
Dataflow analysis.
CS 201 Compiler Construction
CS 201 Compiler Construction
Lectures on Graph Algorithms: searching, testing and sorting
Static Single Assignment Form (SSA)
Optimizations using SSA
Final Code Generation and Code Optimization
Introduction to Optimization
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/4/2019 CPEG421-05S/Topic5.
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/17/2019 CPEG421-05S/Topic5.
The Partitioning Algorithm for Detecting Congruent Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
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.
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
CS 201 Compiler Construction
Presentation transcript:

CS 201 Compiler Construction SSA-Based Sparse Conditional Constant Propagation

Conditional Constant Propagation SSA-based algorithm is faster than the previous algorithm that was presented earlier. Basic Idea: Use SSA-edges for faster propagation of constants from definitions to uses.

Contd.. Handling Executable vs Non-Executable Edges At ϕ-functions non-executable operands have lattice value of top-undef- This allows conditional constant propagation. Process nodes only when they are known to be executable. Non-executable SSA edges will transmit

Contd.. When can we process an SSA edge ? When we know the destination of the edge is executable. Destination statement is executable when at least one of the incoming control flow edge is executable. Justification: In SSA form each use receives value from only one definition. Thus, no matter how we get to the use it will get the value from the same SSA-edge.

Contd.. Maintain two worklists: FlowWorkList (for control flow edges) SSAWorkList (for SSA edges) Initialize: FlowWorkList  Edges leaving the entry node. SSAWorkList  {} ExecutableFlag(FlowEdges)False Lattice cells initialized to UNDEF or top Halt execution when both worklists are empty. Execution may proceed by processing edges from either worklist.

Contd.. 3. if e is CFG-edge from FlowWorkList then if ExecutableFlag(e) = false then ExecutableFlag(e) = true Perform Visitϕ for all ϕ-nodes at destination node. if only one incoming CFG-edges is TRUE (i.e., this is the first visit to the node) then Perform VisitExpression at the node if the node contains one outgoing CFGedge then add the edge to FlowWorkList

Contd.. 4. if e is SSA-edge from SSAWorkList then if destination of e is a ϕ-node then Perform Visitϕ elseif destination of e is an expression then examine ExecutableFlags for the CFG edges reaching that node and if any one flag is true then perform VisitExpression.

Contd.. Visitϕ For each operand in the ϕ-node Examine the ExecutableFlag of the corresponding CFG-edge and set the operand’s lattice value as follows: CFG-edge executable  lattice value same as lattice value at the definition edge of the SSA-edge. CFG-edge non-executable lattice value is UNDEF (top). Compute the lattice value of the variable on the left hand side.

Contd.. VisitExpression Evaluate lattice value of the expression using lattice values of operands from places where they were defined. If lattice value changes then if expression is part of an assignment statement then add SSA-edge starting from left hand side to the SSAWorkList elseif expression is conditional/predicate then based upon the result add appropriate CFG-edges to the FlowWorkList: TRUEtrue edge; FALSEfalse edge; and bottom  both edges.

Def-Use Edges vs SSA-edges We cannot take advantage of non-executable edges if def-use edges are used for propagation. Why? Because def-use chains include def-use edges formed through non-executable edges. executable executable executable

Def-Use Edges vs SSA-edges Algorithm based upon Def-Use chains will be less efficient. SSA-based algorithm: Time spent is proportional to number of SSA-edges. Number of SSA-edges per variable is O(N) where N is the number of statements. Therefore time complexity is O(NxV) where V is the number of variables. Def-Use based algorithm: There can be O(N) definitions and uses of one variable. Thus, number of def-use edges is O(N2) and hence the time complexity is O(N2xV).

Sample Problem For the given code segment perform conditional constant propagation and folding: (a) Using data flow based algorithm; and (b) Using SSA-form based algorithm. A = 1; W = 1; B = 2; if (A != B) then Z = B + A else Z = B – A endif do W = 2; if (Z < 0) then V = Z + 1 else V = Z - 1 endif; B = B – 1; while (B ≤ 0) Print V+W;

Another Application of SSA Form Global Value Numbering

Global Value Numbering A technique for determining when two computations in a program are equivalent  can be used for redundancy removal. Constant Propagation -- by computing values of two computations they can be shown to be equivalent. Common Subexpression Elimination -- lexically identical expressions can be shown to be equivalent without computing their values by observing that values of variables used have not changed. Value Numbering -- lexically different expressions can be shown to be equivalent without computing their values.

Examples Read A B  A + 2 C  A D  C + 2 Show B==D VN succeeds CP fails CSE fails A  5 B  A + 2 C  A + 1 D  C + 1 Show B==D CP succeeds VN fails CSE fails

Examples If (..) then read X; A  X + 1 --1 else read X; B  X + 1 --2 C  X + 1 --3 VN fails – it concludes all three evaluations of X+1 use different values of X CP fails CSE succeeds If (..) then read X; T  A  X + 1 else read X; T  B  X + 1 C  T

Value Numbering Algorithm Congruence of variables Variables partitioned into congruence classes All variables in a congruence class have the same value Application of identical operators to equivalent arguments produces equivalent results Why SSA? To distinguish values of a variable at different program points we can use different names for them. Renaming is already performed during construction of SSA form.

Treatment of ϕ Nodes If P then A1  5 else A2  3 A3  ϕ (A1, A2) If Q then B1  5 else B2  3 B3  ϕ (B1, B2) Is A3== B3? No -- P and Q may evaluate differently.  Treat ϕ ‘s at different join points as different operators. A3  ϕP (A1, A2) B3  ϕQ (B1, B2)

Congruence Partitioning Algorithm Pessimistic – underestimate partition sizes and merge partitions as equivalences are discovered. Optimistic – overestimate partition sizes and split them as contradictions are discovered. P\iQ – set of variables in P whose ith operand is in Q P/iQ – set of variables in P whose ith operand is not in Q Q properly splits P for i if P\iQ =/= {} and P/iQ =/= {}

Optimistic Algorithm Worklist  {} for each operator f do Cf  {} for each X  f(A,B) do Cf  Cf U {X} Worklist  Worklist U {Cf} While Worklist =/= {} do delete D from Worklist and use it to split all other classes for each class C properly split by D do if C ε Worklist then Worklist  Worklist – {C} U {C\iD, C/iD} else Worklist  Worklist U {C\iD} or Worklist  Worklist U {C/iD}

Example A1input B1A1+1 C1A1 D1C1+1 E1B1+1 F1D1+1 E2D1+1 F2B1+1 Cinput = {A1,C1} C+ = {B1,D1,E1,F1,E2,F2} Cϕ = {E3,F3} Worklist = {Cinput, C+, Cϕ} Remove Cinput & use it to split C+ & Cϕ C+ \1 Cinput = {B1,D1} C+ /1 Cinput = {E1,F1,E2,F2} Worklist = {C+ \1 Cinput, C+ /1 Cinput , Cϕ} Remove C+ \1 Cinput ……. No splitting occurs Remove C+ /1 Cinput ……. No splitting occurs Remove Cϕ ……… No splitting occurs A1=C1 B1=D1 E1=F1=E2=F2 E3=F3 A1input B1A1+1 C1A1 D1C1+1 E1B1+1 F1D1+1 E2D1+1 F2B1+1 E3ϕ(E1,E2) F3ϕ(F1,F2)

Example A1input B1A1+1 C1A1 D1C1+1 A1input B1A1+1 E1B1+1 B1=D1 E1=F1=E2=F2 E3=F3 E1B1+1 F1D1+1 E2D1+1 F2B1+1 E1B1+1 E2B1+1 E3ϕ(E1,E2) E3ϕ(E1,E2) F3ϕ(F1,F2)

Sample Problem input A; input B; X = A + B; Y = B + A; while (…) do Z = 1; P = Z + 1; W = 2; R = Z + 1; if (…) then W = A + 1; Q = Z + 1 else V = A + Z endif; A = A + 1; B = B + 1; X = A + 1; Y = B + 1; endwhile For the given code segment perform global value numbering to identify congruence classes of variables.