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.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

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.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
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.
Control Flow Analysis (Chapter 7) Mooly Sagiv (with Contributions by Hanne Riis Nielson)
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
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.
Introduction to Optimizations
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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 More data flow analysis Emery Berger.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
CMPUT Compiler Design and Optimization
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA Emery Berger University.
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.
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 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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 Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
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.
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)
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
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.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Control Flow Analysis Compiler Baojian Hua
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
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.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Loops Simone Campanoni
Basic Program Analysis
CS 201 Compiler Construction
EECS 583 – Class 2 Control Flow Analysis
Graph Algorithms Using Depth First Search
CS 201 Compiler Construction
Topic 10: Dataflow Analysis
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
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)
EECS 583 – Class 2 Control Flow Analysis
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Presentation transcript:

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 Massachusetts, Amherst

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 2 Control-Flow Analysis Motivating example: identifying loops majority of runtime  focus optimization on loop bodies! remove redundant code, replace expensive operations ) speed up program Finding loops: easy… for i = 1 to 1000 for j = 1 to 1000 for k = 1 to 1000 do something 1 i = 1; j = 1; k = 1; 2 A1: if i > 1000 goto L1; 3 A2: if j > 1000 goto L2; 4 A3: if k > 1000 goto L3; 5 do something 6 k = k + 1; goto A3; 7 L3: j = j + 1; goto A2; 8 L2: i = i + 1; goto A1; 9 L1: halt or harder (GOTOs)

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 3 Steps to Finding Loops 1. Identify basic blocks 2. Build control-flow graph 3. Analyze CFG to find loops

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 4 Control-Flow Graphs Control-flow graph: Node: an instruction or sequence of instructions (a basic block) Two instructions i, j in same basic block iff execution of i guarantees execution of j Directed edge: potential flow of control Distinguished start node Entry First instruction in program

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 5 Identifying Basic Blocks Input: sequence of instructions instr(i) Identify leaders: first instruction of basic block Iterate: add subsequent instructions to basic block until we reach another leader

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 6 Basic Block Partition Algorithm leaders = 1// start of program for i = 1 to |n|// all instructions if instr(i) is a branch leaders = leaders [ targets of instr(i) worklist = leaders While worklist not empty x = first instruction in worklist worklist = worklist – {x} block(x) = {x} for i = x + 1; i <= |n| && i not in leaders; i++ block(x) = block(x) [ {i}

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 7 Basic Block Example 1 A = 4 2 t1 = A * B 3 L1: t2 = t1/C 4 if t2 < W goto L2 5 M = t1 * k 6 t3 = M + I 7 L2: H = I 8 M = t3 – H 9 if t3 >= 0 goto L3 10 goto L1 11 L3: halt Leaders Basic blocks

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 8 Control-Flow Edges Basic blocks = nodes Edges: Add directed edge between B1 and B2 if: Branch from last statement of B1 to first statement of B2 (B2 is a leader), or B2 immediately follows B1 in program order and B1 does not end with unconditional branch (goto)

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 9 Control-Flow Edge Algorithm Input: block(i), sequence of basic blocks Output: CFG where nodes are basic blocks for i = 1 to the number of blocks x = last instruction of block(i) if instr(x) is a branch for each target y of instr(x), create edge block i ! block y if instr(x) is not unconditional branch, create edge block i ! block i+1

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 10 CFG Edge Example 1 A = 4 2 t1 = A * B 3 L1: t2 = t1/C 4 if t2 < W goto L2 5 M = t1 * k 6 t3 = M + I 7 L2: H = I 8 M = t3 – H 9 if t3 >= 0 goto L3 10 goto L1 11 L3: halt Leaders Basic blocks

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 11 Steps to Finding Loops 1. Identify basic blocks 2. Build control-flow graph 3. Analyze CFG to find loops Spanning trees, depth-first spanning trees Reducibility Dominators Dominator tree Strongly-connected components

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 12 Spanning Tree Build a tree containing every node and some edges from CFG procedure Span (v) for w in Succ(v) if not InTree(w) add w, v ! w to ST InTree(w) = true Span(w) for v in V do inTree = false InTree(root) = true Span(root) A B C D E FG

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 13 CFG Edge Classification 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 A B C D E FG loop

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 14 Depth-first spanning tree 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 = false vnum = 0 DFST(root) A B C D E FG

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 15 Reducibility Natural loops: no jumps into middle of loop entirely disjoint or nested Reducible: hierarchical, “well-structured” flowgraph reducible iff all loops in it natural

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 16 Reducibility Example reducible graphirreducible graph 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 17 Dominance Node d dominates node i (“d dom i” ) if every path from Entry to i includes d Reflexive:a dom a Transitive:a dom b, b dom c ! a dom c Antisymmetric:a dom b, b dom a ! b=a Immediate dominance: a idom b iff a dom b Æ no c such that a dom c, c dom b (c  a, c  b) Idom’s: each node has unique idom relation forms tree

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 18 Dominance Example Immediate and other dominators: (excluding Entry) a idom b;a dom a, c, d, e, f, g b idom c;b dom b, d, e, f, g c idom d;c dom c, e, f, g d idom e;d dom d, f, g e idom f, e idom g; e dom e control-flow graphdominator tree

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 19 Dominance and Loops Redefine back edge as one whose head dominates its tail Slightly more restrictive definition Now we can (finally) find natural loops! for back edge m ! n, natural loop is subgraph of nodes containing n (loop header) and nodes from which m can be reached without passing through n + connecting edges

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 20 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. Now: Loops = all maximal SCCs

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 21 SCC Example Entry B1 B2 B3 Strongly-connected component Maximal strongly- connected component

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 22 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 edges Unlike DFST, neither distinguishes inner loops

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 23 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

U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE 24 Next Time Dataflow analysis Read ACDI Chapter 8, pp photocopies should be available soon