Jeffrey D. Ullman Stanford University. 2  A set of nodes N and edges E is a region if: 1.There is a header h in N that dominates all nodes in N. 2.If.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Symbolic Analysis. Symbolic analysis tracks the values of variables in programs symbolically as expressions of input variables and other variables, which.
1 Induction Variables Region-Based Analysis Meet and Closure of Transfer Functions Affine Functions of Reference Variables.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
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.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Jeffrey D. Ullman Stanford University. 2  A never-published Stanford technical report by Fran Allen in  Fran won the Turing award in  Flow.
SSA.
Walks, Paths and Circuits Walks, Paths and Circuits Sanjay Jain, Lecturer, School of Computing.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
Approximation, Chance and Networks Lecture Notes BISS 2005, Bertinoro March Alessandro Panconesi University La Sapienza of Rome.
Chapter 6 Linear Programming: The Simplex Method
Program Representations. Representing programs Goals.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Jeffrey D. Ullman Stanford University Flow Graph Theory.
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 |
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
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)
Lecture #17, June 5, 2007 Static Single Assignment phi nodes Dominators Dominance Frontiers Dominance Frontiers when inserting phi nodes.
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
A Data Locality Optimizing Algorithm based on A Data Locality Optimizing Algorithm by Michael E. Wolf and Monica S. Lam.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
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.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
ENE 103 Electrotechnology
UNC Chapel Hill M. C. Lin Point Location Reading: Chapter 6 of the Textbook Driving Applications –Knowing Where You Are in GIS Related Applications –Triangulation.
Structural Data-flow Analysis Algorithms: Allen-Cocke Interval Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
Lecture 05: Theory of Automata:08 Kleene’s Theorem and NFA.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
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.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Loops.
Program Representations. Representing programs Goals.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
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.
Machine-Independent Optimizations Ⅳ CS308 Compiler Theory1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
LINKED LISTS.
Loops Simone Campanoni
Lecture 5 Partial Redundancy Elimination
CS 201 Compiler Construction
Chapter 2 Resistive Circuits
Princeton University Spring 2016
CS 201 Compiler Construction
Control Flow Analysis CS 4501 Baishakhi Ray.
Network Flow 2016/04/12.
TARGET CODE GENERATION
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Optimizations using SSA
Control Flow Analysis (Chapter 7)
Interval Partitioning of a Flow Graph
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Jeffrey D. Ullman Stanford University

2  A set of nodes N and edges E is a region if: 1.There is a header h in N that dominates all nodes in N. 2.If n ≠ h is in N, then all predecessors of n are also in N.  I.e., the header is the sole entry into the region. 3.E consists of all edges between nodes in N, possibly excluding nodes that enter h from somewhere in N.  Note: Every node is a region by itself.

3  For reducible flow graphs, we can “reduce” the graph by two region-creating transformations.  T1: Remove a self-loop from a node.  T2: combine two nodes n and m such that m’s only predecessor is n, and m is not the entry.  Say “n consumes m.” n m

T2

T1

T2

T2

T1

10  As we reduce, each node represents a region of the original flow graph, and each edge represents one or more edges of the original flow graph.  T2: Take the union of the two regions represented by the two nodes, plus all edges represented by the edge between them.  T1: Add (to the region represented by the node) those edges represented by the loop that was removed.

11  Easy inductive proof: every set of nodes and edges constructed is a region.  Key point: When you use T2, the header of the resulting region is the header of the region that consumes the other.

13  First, compute for each region, smallest-to- largest, some transfer functions: 1.For region R constructed by T2 from S consuming T: f R,IN[S] = identity function; f R,IN[T] = meet over paths from the beginning of the header of R to the beginning of the header of T, going only through S. 2.For region R with header h constructed from S by T1: f R,IN[S] = meet over paths within R from h to h. 3.For each region R with exit block ( = has an out- edge not in R) B, f R,OUT[B] = meet over paths within R from header of R to end of B.

U S f U,IN[S] = f 1 f S,OUT[2] = f 2 (f 3 f 2 )* f U,OUT[2] = f 1 f 2 (f 3 f 2 )* f U,OUT[4] = f 1 f 4 Like regular expressions. = f 2 ∧ f 2 f 3 f 2 ∧ f 2 f 3 f 2 f 3 f 2 ∧....

15  We can compute the meet of transfer functions: [f ∧ g](x) = f(x) ∧ g(x).  Needed when we combine transfer functions from header to several exits.  We can compute the closure f* for each transfer function f, given by f* = f 0 ∧ f 1 ∧ f 2 ∧ …  Note f 0 = identity, f 1 = f, f 2 = f composed with f, etc..  Needed when we apply T1 to consume a back edge.

16  Meet of transfer functions f(x) = (x - K) ∪ G and g(x) = (x - K’) ∪ G’ corresponds to the paths of f and g in parallel.  Thus, [f ∧ g](x) = (x – (K ∩ K’)) ∪ (G ∪ G’).  f*(x) = x ∪ f(x) ∪ f(f(x)) ∪ … = x ∪ (x – K) ∪ G ∪ ( (x - K ∪ G) – K) ∪ G … = x ∪ G. f(x) f(f(x))

U V f U,OUT[2] = f 1 f 2 (f 3 f 2 )* f U,OUT[4] = f 1 f 4 f V,IN[5] = f 1 f 4 ∧ f 1 f 2 (f 3 f 2 )* Region U consumes node 5 to make region V.

 If R is a region consisting of a single node (block), and f is the transfer function for that block, then f R,OUT[R] = f.  Note: we use the same name for the block and the region consisting of only that block. 18

 Apply T2: region R consumes region S to make T. 1.f T,IN(R) = the identity function. 2.f T,IN(S) = meet of f R,OUT[B] for all predecessors B of the header of S. 3.For blocks B in R, f T,OUT[B] = f R,OUT[B]. 4.For blocks B in S, f T,OUT[B] = f T,IN(S) f S,OUT[B]. 19

 Apply T1: add back edges to region S to make R. 1.Compute g = meet of f S,OUT[B] for all predecessors B of the header of S. 2.f R,IN[S] = g*.  Notice that the headers of R and S are the same node, but there are paths within R that are not in S, yet take you to the header of S. 3.For all blocks B in S (and therefore in R) let f R,OUT[B] = f R,IN[S] f S,OUT[B]. 20

 Let e be the header of the region that is the entire flow graph.  Start with IN(e) = v ENTRY.  Proceed inward, from larger regions to smaller. 1.If R was constructed from S by T1, and both have header h, replace IN(h) by f R,IN(S) (IN(h)). 2.If T, with header h’ was constructed from R and S, with R consuming S, and S has header h, initialize IN(h) to f T,IN(S) (IN(h’). 21

R IN[1] = f R,IN[V] (v ENTRY ) = f* V,OUT[5] ] (v ENTRY ) U V IN[5] = f V,IN[5] (IN(1))

23  Values V are mappings from variables to expressions involving reference variables.  Example: m(a) = 2i; m(b) = i + j + 1.  i and j are the reference variables.  We are going to deal with two complicated types of functions: 1.Mappings from variables to expressions, as above. 2.Transfer functions from mappings (in) to mappings (out).

24  Reference variables are basic induction variables = counts of the number of times around some loop.  This count is well-defined for “natural loops.”  Set of values V for the framework = affine mappings = each variable is mapped to either: 1.A linear function of the reference variables, or 2.NAA = “Not an affine” mapping, or 3.UNDEF = top element = “nothing known.”

25 m(a) = 2i + 3j + 4 m(b) = NAA m(c) = 4i + 1 m(d) = UNDEF

26 UNDEF All affine functions of reference variables NAA Less Ignorance Note: The lattice for V is the product of one of these for each variable.

27  Natural application of region-based analysis.  Each loop is a region, and its induction variables can be discovered by a framework based on affine mappings.

28  Let f be the transfer function associated with a block containing only a = a+10.  Let m be the input mapping for the block; then f(m) = m’, where:  m’(a) = m(a)  m’(x) = m(x) for all x ≠ a.  Book uses the notation f(m)(a) = m(a) + 10, etc., to avoid having to name f(m). a = a + 10 m m' Question: What if m(a) is NAA or UNDEF?

29  (f ∧ g)(m)(x) =  f(m)(x) if f(m)(x) = g(m)(x).  f(m)(x) if g(m)(x) = UNDEF.  g(m)(x) if f(m)(x) = UNDEF.  NAA otherwise.

30 a=b+3 b=a+2 a=a+5 B1 B5 B2 B4 B3 f B3 (m) (a) = m(a) (b) = m(a)+2 f B2 (m) (a) = m(a)+5 (b) = m(b) f B4 (m) (a) = m(b)+3 (b) = m(b) R f R,OUT[B4] (m) (a) = m(a)+5 (b) = m(a)+2

31 a=b+3 b=a+2 a=a+5 B1 B5 B2 B4 B3 f B2 (m) (a) = m(a)+5 (b) = m(b) R f R,OUT[B4] (m) (a) = m(a)+5 (b) = m(a)+2 S f S,OUT[B5] (m) (a) = m(a)+5 (b) = NAA

32  Treat the iteration count i as a basic induction variable.  If f(m)(x) = m(x)+c, then f i (m)(x) = m(x)+ci.  Some other cases, e.g., where m(x) is an affine function of basic induction variables, in book.

33 a=b+3 b=a+2 a=a+5 B1 B5 B2 B4 B3 S f S,OUT[B5] (m) (a) = m(a)+5 (b) = NAA T f S,OUT[B5] (m) (a) = m(a)+5i (b) = NAA i OUT i [B3](m) (a) = m(a)+5i (b) = m(a)+5i+2 OUT i [B4](m) (a) = m(a)+5i+5 (b) = m(a)+5i+2 OUT i [B2](m) (a) = m(a)+5i+5 (b) = NAA OUT i [B5](m) (a) = m(a)+5i+5 (b) = NAA

34  Replace the loop counter variable by one of the induction variables (variables that are mapped to an affine expression of the loop count at the point were the loop count is tested).

35 i<10? a=b+3 b=a+2 a=a+5 i=i+1 B1 B5 B2 B4 B3 i=0 a=30 Here, the value of a is m(a)+5i, where m(a) is the value of a before entering the loop, and i is the number of iterations of the loop so far. Since a=30 when i=0, replace “i<10?” by “a<80?”. If i is dead on exit from the loop, delete “i=i+1” and “i=0”. And since i is an induction variable, if not dead we can just assign i = 10 at the exit.

36 a<80? a=b+3 b=a+2 a=a+5 B1 B5 B2 B4 B3 a=30