Taken largely from University of Delaware Compiler Notes

Slides:



Advertisements
Similar presentations
Compiler Construction
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.
Course Outline Traditional Static Program Analysis Software Testing
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt.
Topic 3: Flow Analysis José Nelson Amaral
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Jeffrey D. Ullman Stanford University Flow Graph Theory.
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.
CMPUT Compiler Design and Optimization
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
ECE355 Fall 2004Software Reliability1 ECE-355 Tutorial Jie Lian.
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.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Loops.
Compilers Modern Compiler Design
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.
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
 | Website for Students | VTU - Notes - Question Papers Optimization & Code Generation.
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,
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Simone Campanoni CFA Simone Campanoni
High-level optimization Jakub Yaghob
EECS 583 – Class 2 Control Flow Analysis
Factored Use-Def Chains and Static Single Assignment Forms
Taken largely from University of Delaware Compiler Notes
Control Flow Analysis CS 4501 Baishakhi Ray.
Three-Address Implementation
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
Optimizing Compilers CISC 673 Spring 2009 More Control Flow
CS 201 Compiler Construction
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Examples of Basic Blocks
Optimizations using SSA
Control Flow Analysis (Chapter 7)
Interval Partitioning of a Flow Graph
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
EECS 583 – Class 2 Control Flow Analysis
8 Code Generation Topics A simple code generator algorithm
Intermediate Code Generation
TARGET CODE GENERATION
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1
CS 201 Compiler Construction
Presentation transcript:

Taken largely from University of Delaware Compiler Notes Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt

Determine control structure of a program build “control flow graphs” Control flow analysis Interprocedural Program Flow analysis Intraprocedural Procedure Data flow analysis Local Basic block Determine control structure of a program build “control flow graphs” Determine the flow of scalar values (Solution: propagation of data flow information along flow graph.)

Motivation . S1: A 2 (def of A) S2: B 10 (def of B) S3: C A + B determine if C is a constant 12? S4 Do I = 1, C A[I] = B[I] + D[I-1] .

Basic Blocks A basic block is a sequence of consecutive intermediate language statements in which flow of control can only enter at the beginning and leave at the end. Only the last statement of a basic block can be a branch statement and only the first statement of a basic block can be a target of a branch. However, procedure calls MAY need be included within a basic block. [See text, pp 219-20]

Basic Block Partitioning Algorithm 1. Identify leader statements (i.e. the first statements of basic blocks) by using the following rules: (i) The first statement in the program is a leader (ii) Any statement that is the target of a branch statement is a leader (for most IL’s. these are label statements) (iii) Any statement that immediately follows a branch or return statement is a leader 2. The basic block corresponding to a leader consists of the leader, and all statements up to but not including the next leader or up to the end of the program.

Example The following code computes the inner product of two vectors. (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Source code. Three-address code. (AhoSethiUllman, pp. 529)

Example The following code computes the inner product of two vectors. Rule (i) (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Source code. Three-address code.

Example The following code computes the inner product of two vectors. Rule (i) (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Rule (ii) Source code. Three-address code.

Example The following code computes the inner product of two vectors. Rule (i) (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Rule (ii) Rule (iii) Source code. Three-address code.

Example B1 (1) prod := 0 (2) i := 1 B2 (3) t1 := 4 * i (4) t2 := a[t1] (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) Basic Blocks: B3 (13) …

Control Flow Graph (CFG) The basic block whose leader is the first IL statement is called the initial node or start node There is a directed edge from basic block B1 to basic B2 in the CFG if: (1) There is a branch from the last statement of B1 to the first statement of B2, or (2) Control flow can fall through from B1 to B2 because B2 immediately follows B1, and B1 does not end in an unconditional branch

Dominators Node (basic block) D in a CFG dominates node N if every path from the start node to N goes through D. We say that node D is a dominator of node N. Define dom(N) = set of node N’s dominators, or the dominator set for node N. Note: by definition, each node dominates itself i.e., N  dom(N).

An Example Domination relation: { (1, 1), (1, 2), (1, 3), (1, 4) … (2, 3), (2, 4), … (2, 10) } 1 S 2 3 Direct domination: 4 1 <d 2, 2 <d 3, … 5 6 7 DOM: 8 DOM(1) = {1} DOM(2) = {1, 2} DOM(3) = {1, 2, 3} DOM(10) = {1, 2, 10) 9 10

Immediate Dominators and Dominator Tree Node M is the immediate dominator of node N ==> Node M must be the last dominator of N on any path from the start node to N. Therefore, every node other than the start node must have a unique immediate dominator (the start node has no immediate dominator.) What does this mean ?

Dominator Tree A flowgraph (left) and its dominator tree (right) S 1 2 3 4 5 6 7 8 9 10 S 1 2 3 4 5 7 8 6 9 10 A flowgraph (left) and its dominator tree (right)

Question Answer: NO! Example: consider nodes 5 and 8. Assume an immediate dominator n’ of a node n, is n’ necessarily an immediate predecessor in the flow graph? Answer: NO! Example: consider nodes 5 and 8.

An Example (Dominators) 1 2 3 4 5 6 7 8 9 10

Depth-First Search (DFS) An “ordering” of nodes of CFG BBs dfs_nums 1..N Each BB dfs_num represents when that BB first encountered in DFS Write dfs(BB root);

DFS and Depth-First Order CONFUSING: They are NOT the same DFS as defined: order of first visitation during depth-first search Depth-First Order “The depth-first ordering of the nodes is the reverse of the order in which we last visit the nodes in a preorder traversal” (Aho-Sethi-Ullman) Used (sometimes) to identify loops Later …

An Example DFS 1 2 3 4 5 6 7 8 9 10