Control Flow Analysis Compiler Baojian Hua

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.
8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
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)
Optimization Compiler Baojian Hua
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Program Representations Xiangyu Zhang. CS590F Software Reliability Why Program Representations  Initial representations Source code (across languages).
Cpeg421-08S/final-review1 Course Review Tom St. John.
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.
ECE1724F Compiler Primer Sept. 18, 2002.
Parsing Compiler Baojian Hua Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer.
Chair of Software Engineering Fundamentals of Program Analysis Dr. Manuel Oriol.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Code Generation Professor Yihjia Tsai Tamkang University.
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 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
2015/6/24\course\cpeg421-10F\Topic1-b.ppt1 Topic 1b: Flow Analysis Some slides come from Prof. J. N. Amaral
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Precision Going back to constant prop, in what cases would we lose precision?
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
CSE P501 – Compiler Construction
Data Flow Analysis Compiler Baojian Hua
Software (Program) Analysis. Automated Static Analysis Static analyzers are software tools for source text processing They parse the program text and.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
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.
Dominators, control-dependence and SSA form. Organization Dominator relation of CFGs –postdominator relation Dominator tree Computing dominator relation.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 1 Developed By:
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.
LL(k) Parsing Compiler Baojian Hua
A System to Generate Test Data and Symbolically Execute Programs Lori A. Clarke Presented by: Xia Cheng.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
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.
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.
CS 3220: Compilation Techniques for Parallel Systems Spring Pitt CS
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Simone Campanoni CFA Simone Campanoni
Basic Program Analysis
Static Single Assignment
Intermediate Representations Hal Perkins Autumn 2011
Taken largely from University of Delaware Compiler Notes
Control Flow Analysis CS 4501 Baishakhi Ray.
CS 201 Compiler Construction
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Optimizations using SSA
Control Flow Analysis (Chapter 7)
Compiler Baojian Hua Value Numbering Compiler Baojian Hua
Data Flow Analysis Compiler Design
SSA-based Optimizations
Taken largely from University of Delaware Compiler Notes
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

Control Flow Analysis Compiler Baojian Hua

Front End source code abstract syntax tree lexical analyzer parser tokens IR semantic analyzer

Middle End AST translation IR1 asm other IR and translation translation IR2

Intermediate Representation Trees and Dags high-level, program structures 3-address code low-level, closer to ISA Today, control-flow graph (CFG) more refined 3-address code good for optimizations

Control Flow Graph (CFG)

3-address Code: Recap if (x < y){ z = 4; m = 3; } else{ z = 6; m = 5; } Cjmp (x<y, L_1, L_2); L_1: z = 4; m = 3; jmp L_3; L_2: z = 6; m = 5; jmp L_3; L_3:

Control Structure Cjmp (x<y, L_1, L_2); L_1: z = 4; m = 3; jmp L_3; L_2: z = 6; m = 5; jmp L_3; L_3: Cjmp (x<y, L_1, L_2); z = 4; m = 3; jmp L_3; z = 6; m = 5; jmp L_3; …; L_3 L_1 L_2

Moral This graph-based representation is good for many purposes: flow analysis: for many program analysis, the program internal structure is important enable other analysis: such as data-flow analysis (to be discussed later) scheduling: try to minimizing “ jump ” s by rearranging the program structures

Basic Blocks & Control Flow Graph A basic block is a sequence of basic statements, executing from the beginning and exiting at the end can NOT enter the middle can NOT exit the from the middle no interleaving “ jump ” or “ branch ” Control-flow graph is a graph consisting of basic blocks as vertices

Basic blocks and CFG basic blocks Cjmp (x<y, L_1, L_2); z = 4; m = 3; jmp L_3; z = 6; m = 5; jmp L_3; …; L_3 L_1 L_2 block label (name) ending with a “jump” statement edge stands for control transfer

Control Flow Graph Data Structure // Just a refined 3-address code s -> x = v1 + v2 | x = v | x = f (v1, v2, …, vn) j -> Jump L | Cjump (v, L1, L2) | return b -> Label L; s1; s2; …, sn j; f -> b1, …, bn prog -> f1, …, fn

Conversion into CFG One can start directly from AST or HIL: good for language like MiniJava, which has regular control structures Or one can start from 3-adress code or other IRs: may be easier for languages such as C, which have unstructured controls (e.g., goto) Next, we discuss techniques dealing with CFG

CFG Traversal Standard graph traversal algorithms: DFS, BFS, … Important for linearization of nodes: Topo-sort order, quasi-topo-sort order, and reverse top-sort order We leave these operations to your algorithm course, and next we discuss two applications: dead-code eliminations (optimizations) extended basic blocks (EBBs)

#1: Dead code (block) elimination example int f () { int i = 3; while (i<10){ i = i+1; printi(i); continue; printi(i); } return 0; } printi(i) jump L0 i=3 i<10? L1: L2 return 0 L2 printi(i) jump L2 L0 L1 L3

#1: Dead code (block) elimination algorithm // algorithm // input: a CFG g for f // output: a new CFG for // function f dfs (g); for (each node n in g) if (!visited(n)) delete (n); printi(i) jump L0 i=3 i<10? L1: L2 return 0 L2 printi(i) jump L2 L0 L1 L3

#2: Extended basic blocks Extended blocks from a block A is a maximal set of blocks with no join that is, every block (except for A) should have just one predecessor e.g., in the following graph, extended blocks from A are {A, B, C} A B C D

#2: EBBs // Algorithm: give a node n, // calculate EBB for this node. // This is just a variant // of DFS ebb = {}; build_ebb (n: node) ebb \/= {n}; foreach (successor m of n) if (|pred(m)| ==1 && m\not\in ebb) build_ebb (m); A B C D

Dominator

Dominators A node a dominates a node d, iff every path from the entry node s0 to the node d goes through the node a a is a dominator of node d every node dominates itself Dominator relationship is a partial order that is: reflexive, anti-symmetry, transitive leave the proof to you!

Example A node a dominates a node d, iff every path from the entry node s0 to the node d goes through the node a. We write it as: a dom d 1 dom 2 2 dom 4 2 dom 7 4 dom 7 6 dom 7 ??? D[n]={all nodes x | x dom n} D[5] D[6] D[7]

Equation Fix-point algorithm Can be accelerated by first ordering the nodes quasi-topo sort order Or by Tarjan ’ s algorithm (nearly linear time)

Step #1: initialization D[1]={1} D[2]={1, …, 12} D[4]={1, …, 12} D[3]={1, …, 12} D[s0]={s0} D[n]={all nodes} D[5]={1, …, 12} D[6]={1, …, 12} D[7]={1, …, 12} D[8]={1, …, 12} D[9]={1, …, 12} D[10]={1, …, 12} D[11]={1, …, 12} D[12]={1, …, 12}

Step #2: calculate a quasi- topo sort order D[1]={1} D[2]={1, …, 12} D[4]={1, …, 12} D[3]={1, …, 12} D[5]={1, …, 12} D[6]={1, …, 12} D[7]={1, …, 12} D[8]={1, …, 12} D[9]={1, …, 12} D[10]={1, …, 12} D[11]={1, …, 12} D[12]={1, …, 12} quasi top-sort order: 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12

Step #3: calculate fix-point D[1]={1} D[2]={1, …, 12} D[4]={1, …, 12} D[3]={1, …, 12} D[5]={1, …, 12} D[6]={1, …, 12} D[7]={1, …, 12} D[8]={1, …, 12} D[9]={1, …, 12} D[10]={1, …, 12} D[11]={1, …, 12} D[12]={1, …, 12} quasi top-sort order: 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12 {1, 2} {1, 2, 3} {1, 2, 4} {1, 2, 4, 5} {1, 2, 4, 6} {1, 2, 4, 7} {1, 2, 4, 5, 8} {1, 2, 4, 5, 8, 9} {1, 2, 4, 5, 8, 9, 10} {1, 2, 4, 7, 11} {1, 2, 4, 12}

Step #3: calculate fix-point D[1]={1} D[2]={1, 2} D[4]={1, 2, 4} D[3]={1, 2, 3} D[5]={1,2,4,5} D[6]={1, 2, 4, 6} D[7]={1, 2, 4, 7} D[8]={1,2,4,5,8} D[9]={1,2,4,5,8,9} D[10]={1,2,4,5,8,9,10} D[11]={1,2,4,7,11} D[12]={1, 2, 4, 12} quasi top-sort order: 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12

Immediate dominator Intuitively, an immediate dominator x for a node n is a node that is most close to n x dom n, x!=n for any y dom n, then y dom x One can prove a theorem stating that for every node n (except for s0), n has just one immediate dominator write n ’ s immediate dominator as idom(n)

Immediate dominator D[1]={1} D[2]={1, 2} D[4]={1, 2, 4} D[3]={1, 2, 3} D[5]={1,2,4,5} D[6]={1, 2, 4, 6} D[7]={1, 2, 4, 7} D[8]={1,2,4,5,8} D[9]={1,2,4,5,8,9} D[10]={1,2,4,5,8,9,10} D[11]={1,2,4,7,11} D[12]={1, 2, 4, 12} quasi top-sort order: 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12

Dominator Tree

Dominator Calculation Revisited In 2005, Cooper et. al, published an interesting paper dominator tree-based, easy to implement Even comparable with Tarjan ’ s algorithm Lesson: careful engineering of well- known slow algorithm may be profitable

Strict dominator Node x is a strict dominator of y, if x dominates y, and x<>y sdom (x) = dom(x)-{x} Dominance frontier of a node x: a set of nodes y such that x dominates a predecessor p of node y, but does not strictly dominates y df(x)=? read the algorithm in Tiger 19.1

Intuition for Dominance Frontier s0 x q p s t

Dominance Frontier df(3)={2} df(10)={5, 12} Walk the dominator tree in post-order: 3, 10, 9, 8, 5, 6, 11, 7, 12, 4, 2, 1 df(9)={5, 12, 8} df(8)={5, 12, 8} df(5)={5, 12, 7} df(6)={7} df(11)={12} df(7)={12} df(12)={} df(4)={2} df(2)={2} df(1)={}

Loops

Natural Loops Given a back edge m->h (for dominance), the natural loop for m->h is all nodes x that dominated by h and can reach m without going through h

Natural Loops Loops(3->2)={2, 3} Loops(4->2)={2, 4} Loops(10->5)={5,8,9,10} Loops(9->8)={8, 9}

Control-Dependency Graph (CDG)

Motivation Suppose we are running this program on a two-core CPU with core C0, C1. Then can we run node 1 on C0 and node2 on C1? (Parallelization!) A[0] = 0 A[1] = Node 1 controls whether or not node 2 will execute. We say node 2 is control- dependent on node 1. Node 2 is control- dependent on node 1, iff 1\in DF(2) in the reverse control flow graph.

Control Dependency Graph A CDG of a CFG G has an edge x->y, iff y is control-dependent on x Algorithm: construct reverse graph G ’ of G calculate the dominator tree for G ’ for each node in G ’, calculate the dominance frontier draw an edge x->y in CDG, for x\in DF(y)

Example e e 7 6 CFGReverse CFG

Example e 7 6 Reverse CFG e 7 6 Dominator tree DF(6)={3} DF(3)={2} DF(5)={3} DF(7)={2} DF(1)={} DF(2)={2} DF(4)={} DF(e)={}

Example e 7 6 CDG e 7 6 Dominator tree DF(6)={3} DF(3)={2} DF(5)={3} DF(7)={2} DF(1)={} DF(2)={2} DF(4)={} DF(e)={}

Example e 7 6 CFG e 7 6 CDG