1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
SSA.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 More Control Flow John Cavazos University.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
CS 267: Automated Verification Lecture 10: Nested Depth First Search, Counter- Example Generation Revisited, Bit-State Hashing, On-The-Fly Model Checking.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Tirgul 11 DFS Properties of DFS Topological sort.
Jeffrey D. Ullman Stanford University Flow Graph Theory.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Lecture 2 Emery Berger University of.
PSUCS322 HM 1 Languages and Compiler Design II Strongly Connected Components Herbert G. Mayer PSU Spring 2010 rev.: 5/28/2010.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Data Flow Analysis Compiler Design Nov. 3, 2005.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
CS344: Lecture 16 S. Muthu Muthukrishnan. Graph Navigation BFS: DFS: DFS numbering by start time or finish time. –tree, back, forward and cross edges.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 14 Strongly connected components Definition and motivation Algorithm Chapter 22.5.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Efficiency of Iterative Algorithms
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
A Fast Algorithm To Determine Minimality of Strongly Connected Digraphs Under the direction of Dr. Robinson By Jianping Zhu.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Announcements & Reading Material
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Properties and Applications of Depth-First Search Trees and Forests
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Machine-Independent Optimizations Ⅳ CS308 Compiler Theory1.
Dominators and CFGs Taken largely from University of Delaware Compiler Notes.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Loops Simone Campanoni
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Breadth-First Search (BFS)
CS 201 Compiler Construction
Graph Algorithms Using Depth First Search
CS 201 Compiler Construction
More Graph Algorithms.
Factored Use-Def Chains and Static Single Assignment Forms
Taken largely from University of Delaware Compiler Notes
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
Optimizing Compilers CISC 673 Spring 2009 More Control Flow
Elementary Graph Algorithms
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Control Flow Analysis (Chapter 7)
EECS 583 – Class 2 Control Flow Analysis
Taken largely from University of Delaware Compiler Notes
Lecture 17: Register Allocation via Graph Colouring
Analysis and design of algorithm
Presentation transcript:

1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis

2 What is a loop ? A subgraph of CFG with the following properties: –Strongly Connected: there is a path from any node in the loop to any other node in the loop; and –Single Entry: there is a single entry into the loop from outside the loop. The entry node of the loop is called the loop header. Loop nodes: 2, 3, 5 Header node: 2 Loop back edge: 5  2 Tail  Head

3 Property Given two loops: they are either disjoint or one is completely nested within the other. Loops {1,2,4} and {5,6} are Disjoint. Loop {5,6} is nested within loop {2,4,5,6}. Loop {5,6} is nested within loop {1,2,3,4,5,6}

Identifying Loops Definitions: Dominates: node n dominates node m iff all paths from start node to node m pass through node n, i.e. to visit node m we must first visit node n. A loop has –A single entry  the entry node dominates all nodes in the loop; and –A back edge, and edge A  B such that B dominates A. B is the head & A is the tail. 4

Identifying Loops Algorithm for finding loops: 1.Compute Dominator Information. 2.Identify Back Edges. 3.Construct Loops corresponding to Back Edges. 5

Dominators: Characteristics 1.Every node dominates itself. 2.Start node dominates every node in the flow graph. 3.If N DOM M and M DOM R then N DOM R. 4.If N DOM M and O DOM M then either N DOM O or O DOM N 5.Set of dominators of a given node can be linearly ordered according to dominator relationships. 6

Dominators: Characteristics 6. Dominator information can be represented by a Dominator Tree. Edges in the dominator tree represent immediate dominator relationships. 7 1 is the immediate dominator of 2, 3 & 4 CFGDominator Tree

Computing Dominator Sets Observation: node m donimates node n iff m dominates all predecessors of n. 8 Let D(n) = set of dominators of n Where Pred(n) is set of immediate predecessors of n in the CFG

Computing Dominator Sets 9 Initial Approximation: D(n o ) = {n o } n o is the start node. D(n) = N, for all n!=n o N is set of all nodes. Iteratively Refine D(n)’s: Algorithm:

Example: Computing Dom. Sets 10 D(1) = {1} D(2) = {2} U D(1) = {1,2} D(3) = {3} U D(1) = {1,3} D(4) = {4} U (D(2) D(3) D(9)) = {1,4} D(5) = {5} U (D(4) D(10)) = {1,4,5} D(6) = {6} U (D(5) D(7)) = {1,4,5,6} D(7) = {7} U D(5) = {1,4,5,7} D(8) = {8} U (D(6) D(10)) = {1,4,5,6,8} D(9) = {9} U D(8) = {1,4,5,6,8,9} D(10)= {10} U D(8) = {1,4,5,6,8,10} Back Edges: 9  4, 10  8, 10  5

Loop Given a back edge N  D Loop corresponding to edge N  D = {D} + {X st X can reach N without going through D} 11 1 dominates 6  6  1 is a back edge Loop of 6  1 = {1} + {3,4,5,6} = {1,3,4,5,6}

Algorithm for Loop Construction Given a Back Edge N  D 12 Stack = empty Loop = {D} Insert(N) While stack not empty do pop m – top element of stack for each p in pred(m) do Insert(p) endfor Endwhile Insert(m) if m not in Loop then Loop = Loop U {m} push m onto Stack endif End Insert

Example Back Edge 7  2 13 Loop = {2} + {7} + {6} + {4} + {5} + {3} Stack = D N

Examples 14 L2  B, S2 L1  A,S1,B,S2 L2 nested in L1 L1  S1,S2,S3,S4 L2  S2,S3,S4 L2 nested in L1 While A do S1 While B do S2 Endwhile ?

Reducible Flow Graph The edges of a reducible flow graph can be partitioned into two disjoint sets: Forward – from an acyclic graph in which every node can be reached from the initial node. Back – edges whose heads (sink) dominate tails (source). Any flow graph that cannot be partitioned as above is a non-reducible or irreducible. 15

Reducible Flow Graph How to check reducibility ? –Remove all back edges and see if the resulting graph is acyclic. 16 Reducible Irreducible 2  3 not a back edge 3  2 not a back edge graph is not acyclic Node Splitting Converts irreducible to reducible

Loop Detection in Reducible Graphs Depth-first Ordering: numbering of nodes in the reverse order in which they were last visited during depth first search. M  N is a back edge iff DFN(M) >= DFN(N) Depth-first Ordering Forward edge M  N (M is descendant of N in DFST) Back edge M  N (N is ancestor of M in DFST)

Algorithm for DFN Computation 18 Mark all nodes as “unvisited” DFST = {} // set of edges of DFST I = # of nodes in the graph; DFS(n o ); DFS(X) { mark X as “visited” for each successor S of X do if S is “unvisited” then add edge X  S to DFST call DFS(S) endif endfor DFN[X] = I; I = I – 1; }

Example 19 CFG DFST Depth First Ordering Back edge Forward edge

20 Sample Problems Control Flow Analysis

21 Dominators 1. For the given control flow graph: (a)Compute the dominator sets and construct the dominator tree; (b)Identify the loops using the dominator information; and (c) Is this control flow graph reducible? If it is so, covert it into a reducible graph

22 Depth First Numbering 2. For the given reducible control flow graph: (a)Compute the depth first numbering; and (a)Identify the loops using the computed information