Optimizing Compilers CISC 673 Spring 2009 More Control Flow

Slides:



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

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
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.
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Tirgul 8 Graph algorithms: Strongly connected components.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
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.
ECE1724F Compiler Primer Sept. 18, 2002.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
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.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
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.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
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.
Jan Topological Order and SCC Edge classification Topological order Recognition of strongly connected components.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Loops.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Announcements & Reading Material
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
Control Flow Analysis Compiler Baojian Hua
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment John Cavazos.
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.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Loops Simone Campanoni
Simone Campanoni CFA Simone Campanoni
Basic Program Analysis
CS 201 Compiler Construction
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
EECS 583 – Class 2 Control Flow Analysis
Graph Algorithms Using Depth First Search
CS 201 Compiler Construction
Concurrent Depth-First Search Algorithms
Topic 10: Dataflow Analysis
Taken largely from University of Delaware Compiler Notes
Graph Algorithms – 2 DAGs Topological order
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
Connected Components Minimum Spanning Tree
Graph Algorithms – 2.
Advanced Algorithms Analysis and Design
Elementary Graph Algorithms
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Control Flow Analysis (Chapter 7)
Control Flow Analysis (Chapter 7)
EECS 583 – Class 4 If-conversion
EECS 583 – Class 2 Control Flow Analysis
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1
Presentation transcript:

Optimizing Compilers CISC 673 Spring 2009 More Control Flow John Cavazos University of Delaware

Clarification Leaders are The entry point of the function Any instruction that is a target of a branch Any instruction following a (conditional or unconditional) branch

Additional Clarification Tree edge: in CFG & ST Advancing edge: (v,w) not tree edge but w is descendant of v in ST Back edge: (v,w): v=w or w is proper ancestor of v in ST Cross edge: (v,w): w neither ancestor nor descendant of v in ST

Class Problem: Identify Edges procedure DFST (v) pre(v) = vnum++ InStack(v) = true for w in Succ(v) if not InTree(w) add v→w to TreeEdges InTree(w) = true DFST(w) else if pre(v) < pre(w) add v→w to AdvancingEdges else if InStack(w) add v→w to BackEdges else add v→w to CrossEdges InStack(v) = false for v in V do inTree(v) = false vnum = 0 InTree(root) DFST(root) Work out answer on the board.. Tree Edges: Advancing Edges: Back Edges: Cross Edges: Etc…

Class Problem: Answer procedure DFST (v) pre(v) = vnum++ InStack(v) = true for w in Succ(v) if not InTree(w) add v→w to TreeEdges InTree(w) = true DFST(w) else if pre(v) < pre(w) add v→w to AdvancingEdges else if InStack(w) add v→w to BackEdges else add v→w to CrossEdges InStack(v) = false for v in V do inTree(v) = false vnum = 0 InTree(root) DFST(root) notes

Reducibility Natural loops: Reducible: hierarchical, “well-structured” single entry node: no jumps into middle of loop requires back edge into loop header (single entry point) Reducible: hierarchical, “well-structured” flowgraph reducible iff all loops in it natural

Reducibility Example Some languages only permit procedures with reducible flowgraphs (e.g., Java) “GOTO Considered Harmful”: introduces irreducibility FORTRAN C C++ DFST does not find unique header in irreducible graphs reducible graph irreducible graph

Dominance Node d dominates node i (“d dom i” ) if every path from Entry to i includes d Reflexive: a dom a Transitive: if a dom b and b dom c then a dom c Antisymmetric: if a dom b and b dom a then b=a

Immediate Dominance a idom b iff a dom b there is no c such that a dom c, c dom b (c  a, c  b) Idom’s: each node has unique idom relation forms a dominator tree

Dominance Tree Immediate and other dominators: (excluding Entry) a idom b; a dom a, b, c, d, e, f, g b idom c; b dom b, c, d, e, f, g c idom d; c dom c, d, e, f, g d idom e; d dom d, e, f, g e idom f, e idom g; e dom e, f, g control-flow graph dominator tree

Dominator Tree?

Dominator Tree

Graph is reducible iff … Reducible Graph Test Graph is reducible iff … all back edges are ones whose head dominates its tail

Why is this not a Reducible Graph?

Why is this not a Reducible Graph? Remember: head must dominate tail of back edge.

B (head) does not dominate C (tail) Nonreducible Graph Spanning Tree B (head) does not dominate C (tail)

Reducible Graph? Build Spanning Tree Back edges: head must dominate tail

Natural Loops Now we can find natural loops Given back edge m → n, natural loop is n (loop header) and nodes that can reach m without passing through n

Find Natural Loops?

Natural Loops Back Edge Natural Loop J → G {G,H,J} G → D {D,E,F,G,H,J} D → C {C,D,E,F,G,H,J} H → C {C,D,E,F,G,H,J} I → A {A,B,C,D,E,F,G,H,I,J}

Strongly-Connected Components What about irreducible flowgraphs? Most general loop form = strongly-connected component (SCC): subgraph S such that every node in S reachable from every other node by path including only edges in S Maximal SCC: S is maximal SCC if it is the largest SCC that contains S.

Strongly-connected component SCC Example Entry Maximal strongly-connected component B1 B2 Strongly-connected component B3

Computing Maximal SCCs Tarjan’s algorithm: Computes all maximal SCCs Linear-time (in number of nodes and edges) CLR algorithm: Also linear-time Simpler: Two depth-first searches and one “transpose”: reverse all graph edge

Conclusion Introduced control-flow analysis Basic blocks Control-flow graphs Discussed application of graph algorithms: loops Spanning trees, depth-first spanning trees Reducibility Dominators Dominator tree Strongly-connected components

Next Time Dataflow analysis Read Marlowe and Ryder paper