1 Constant Propagation for Loops with Factored Use-Def Chains Reporter : Lai, Yen-Chang.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
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.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
SSA.
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.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
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 |
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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,
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
CS 201 Compiler Construction
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 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.
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
CSE P501 – Compiler Construction
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Interference Graphs for Programs in Static Single Information Form are Interval Graphs Philip Brisk Processor Architecture Laboratory (LAP) EPFL Lausanne,
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc. Emery Berger 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.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
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.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Loops Simone Campanoni
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni CFA Simone Campanoni
Static Single Assignment
Princeton University Spring 2016
Topic 10: Dataflow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
Optimizations using SSA
Control Flow Analysis (Chapter 7)
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.
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

1 Constant Propagation for Loops with Factored Use-Def Chains Reporter : Lai, Yen-Chang

2 Outline Introduction Background Constant Propagation Implementation and Experiment

3 Introduction Constant propagation analysis is the optimization based on data-flow analysis which is a static (compiler-time) analysis of providing global information about how a procedure manipulates its data. The solution of constant propagation of one program point is related to the other program points. Constant propagation is a code reduction technique that determines whether all assignments to a particular variable is a constant and provides the same constant value at certain particular points.

Introduction A use of the variable at that point can be replaced by the constant. “Immediate” Ex. Result in faster execution than the case that a register must be used to store the constant. 4

Introduction 5 The number of times that the loop runs depends on the variables a and b. Since variables a and b are both constants, the loop iterates constant times.

Introduction A factored use-def (FUD) chain is the SSA (static single assignment) version of the Use-Def chains which readily lend themselves to demand-driven analysis. Since we want to detect constant values at compile time, it is useful to be able to track constants as they are assigned to variables and then used later. 6

7 Introduction(cont.)

8 A loop is a collection of nodes in a flow graph such that: All nodes in the collection are strongly connected; that is, from any node in the loop there is a directed path to any other node. The collection of nodes has a unique entry point, called the ”header”, through which it is the only possible way to reach a node in the loop from a node outside the loop. There must be at least one way to iterate the loop; ie. there is at least one path that goes back to the header from the nodes in the loop.

Introduction(cont.) Optimizations often require to collect some information before the loop. For simplicity of analysis, compilers usually introduce two predecessors to a loop header, namely a preheader and a postbody. A preheader node is the only one predecessor of the header outside the loop. The source node of unique back edge is a postbody node. 9

10 Introduction(cont.)

11 Introduction(cont.) Reaching Definition This determines which definition of a variable may reach each use of the variable in a procedure, and is not killed by another definition. Dead-Code Elimination Unreachable codes or their removal does not affect the operations of a program, and hence can be eliminated freely.

12 Introduction(cont.) Def-Use link and Use-Def link A Def-Use link for a variable is a list of all uses that can be reached by a given definition of a variable, while A Use-Def link is a list of all definitions that can reach a given use.

13 Background Control Flow Graph The statements of a program are grouped into a set of basic blocks. We write to mean there is a path from X to Y with length greater than zero.(X ≠ Y)

14 Background(cont.) Dominate A node X is said to dominate a node Y in a directed graph if every path from Entry to Y includes X, written as X DOM Y. If X dominates Y and X≠Y, then X strictly dominates Y, written as X SDOM Y. Every node n has a unique immediate dominator m such that m≠n, m IDOM n ?? Entry Y X

15 Background(cont.) Postdominate A node w is said to posdominate node v in a directed graph if every path from v to Exit includes w, written as w PDOM v. If w≠v, then w is said to strictly postdominate v, written as w SPDOM v. v w Exit

16 Background(cont.)

17 Background(cont.)

18 Background(cont.)

19 Background(cont.) Control Dependence Control dependence relations are a more general method to capture the information of essential conditions controlling the execution of code in the program. A node w is control dependent on edge u → v if u is a decision-point that determines whether w will be executed or not. That means if control flows from node u to node v along edges u → v, then it will eventually reach node w. u z v w Exit

Background(cont.) A node w is said to be control dependent on edge (u → v) ∈ E w postdominates v, and if w ≠u, then w does not postdominate u. 20 u z v w Exit

21 Background(cont.) Static Single Assignment (SSA) SSA is a representation technique which makes a clear correspondence between the use and the definitions of a variable by giving a unique name. The two properties of SSA form can be achieved by two key phases knownas Ф-placement and renaming. At the join nodes or merge definitions, there must add a special pseudo form of assignment called a φ-function. The form of φ-function is

22

23

Background(cont.) In a SSA form, each use is reached by a unique definition, and hence program transformations are simplified. The code motion of a variable use depends primarily on motion of its unique reaching definition. Intuitively, the program has been transformed to the representation of flow of values. 24

Background(cont.) APT Data Structure they are join (or merge) points determine their successor will be executed. 25

Background(cont.) For each use, a variable has a unique reaching definition even in a merge conflicting definitions created by control flow, which is represented by a φ-term. Each use of a variable is marked with a new subscript, while the first one is its label and the others are the definitions that reach this use. 26

Constant Propagation ConstantProp – It is the main procedure that analyzes a procedure starting at the Entry node of the program. VisitPhi – It is performed when a normal φ-function is encountered. The number of times that φ-function is visited is the same as the number of operands within the φ-function. VisitExp – It is called to evaluate the effect of each expression. The two situations that VisitExp is called are: VisitExp is called once when a flow edge of a node is processed first time VisitExp is called once each time an incoming SSA edge is processed. 27

Constant Propagation(cont.) VisitLoop – It is called when a loop-header φ is encountered, then visits all operations in the loop. Check – It does the recursive call to VisitLoop when a operand is never been visited, and hinges on the computation of Lowlink(X ) to determine whether some node are in the same strongly connected components or not. 28

29 Constant Propagation(Cont.)

V is the set of CFG nodes in the program. E is the set of CFG edges in the program. SUCC (v) is the set of successors of CFG node v. Count(SUCC(v)) is the number of successors of node v. Target(s) is the use for a Def-Use link s. Source(s) is the definition of φ-term for a Def-Use link s. CFGWorkList is a work list of CFG edges, initialized as a list of the successor(s) of the Entry node. SSAWorkList is a work list of Def-Use links. It is initially empty. 30

Constant Propagation(Cont.) ReachableEdge(e) is a flag that determines whether a CFG edge e has been marked as being reachable, or executable; it is initially false for all edges. ReachableNode(v) for CFG node v is a flag that tells whether the node v is identified as reachable by our algorithm. It is initially false for all node. 31

32

Constant Propagation(Cont.) N is the global counter for assigning preorder numbers, initialized to zero. LoopStack is a push-down(LIFO) stack of operations, initialized to empty. NPre(P) is the preorder number assigned to each operation as the al- gorithm builds a depth-first spanning forest, initialized as zero for each operation. Lowlink(P) keeps track of whether each operation has a path to a spanning forest predecessor. InStack(P) is a flag to keep track of whether each operation is on theWorkList. The flag is initialized as false. 33

34

Constant Propagation(Cont.) 35

Constant Propagation(Cont.) 36

Constant Propagation(Cont.) 37

Constant Propagation(Cont.) 38

39

40 Constant Propagation(cont.) The techniques of constant propagation support several purposes in optimizing compilers: Expressions evaluated at compile time need not be evaluated at execution time. Code that is never executed can be deleted. Detection of the paths never taken simplifies the control flow of a program. Since many of the parameters to procedures are constants, using constant propagation with procedure integration can avoid the expansion of code (inline) that often results from native implementations of procedure integration.

41 Implementation and Experiment

42 Implementation and Experiment

43

44

45

Conclusions The approach preselted here is based ol three fundamental results col- cerlilg colstalt propagatiol: Wegmal ald Zadeck’s SCC algorithm for conditional constant propagation Algorithm by Wolfe et al. of finding induction variables The framework of FUD chains. Our implementation of constant propagation concentrates on the iterative solver. 46

Conclusions To find the constant-valued parameters passed through procedures is a more aggressive of constant propagation analysis. A destructive solution is treating aly assiglmelt to al array as al assiglmelt of ⊥ regardless of the real content of the array. 47