Code Optimization Chapter 10

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

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 –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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)
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Jeffrey D. Ullman Stanford University Flow Graph Theory.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
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 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
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.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Ben Livshits Based in part of Stanford class slides from
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 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Loops.
Announcements & Reading Material
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
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
Simone Campanoni CFA Simone Campanoni
Basic Program Analysis
High-level optimization Jakub Yaghob
Data Flow Analysis Suman Jana
Simone Campanoni Dependences Simone Campanoni
CS 201 Compiler Construction
EECS 583 – Class 2 Control Flow Analysis
Dataflow Testing G. Rothermel.
CS 201 Compiler Construction
Code Generation Part III
Taken largely from University of Delaware Compiler Notes
University Of Virginia
Control Flow Analysis CS 4501 Baishakhi Ray.
Code Optimization Chapter 9 (1st ed. Ch.10)
Optimizing Compilers CISC 673 Spring 2009 More Control Flow
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
TARGET CODE GENERATION
Topic 4: Flow Analysis Some slides come from Prof. J. N. Amaral
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Control Flow Analysis (Chapter 7)
Final Code Generation and Code Optimization
Static Single Assignment
EECS 583 – Class 2 Control Flow Analysis
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Presentation transcript:

Code Optimization Chapter 10 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007

Control- flow analysis The Code Optimizer Control flow analysis: CFG (Ch. 9) Data-flow analysis Transformations Front end Code optimizer Code generator Control- flow analysis Data- flow analysis Transfor- mations

Determining Loops in Flow Graphs: Dominators Dominators: d dom n Node d of a CFG dominates node n if every path from the initial node of the CFG to n goes through d The loop entry dominates all nodes in the loop The immediate dominator m of a node n is the last dominator on the path from the initial node to n If d  n and d dom n then d dom m

Dominator Trees CFG Dominator tree 1 1 2 2 3 3 4 4 5 6 7 5 6 8 7 9 10

Natural Loops A back edge is is an edge a  b whose head b dominates its tail a Given a back edge n  d The natural loop consists of d plus the nodes that can reach n without going through d The loop header is node d Unless two loops have the same header, they are disjoint or one is nested within the other A nested loop is an inner loop if it contains no other loops

Natural Inner Loops Example 1 1 2 2 3 Natural loop for 3 dom 4 3 4 4 5 6 7 Natural loop for 4 dom 7 5 6 8 7 9 10 8 Natural loop for 7 dom 10 9 10 CFG Dominator tree

Natural Outer Loops Example Natural loop for 1 dom 9 1 1 2 2 3 3 4 4 5 6 7 Natural loop for 3 dom 8 5 6 8 7 9 10 8 9 10 CFG Dominator tree

Pre-Headers To facilitate loop transformations, a compiler often adds a preheader to a loop Code motion, strength reduction, and other loop transformations populate the preheader Preheader Header Header

Reducible Flow Graphs Reducible graph = disjoint partition in forward and back edges such that the forward edges form an acyclic (sub)graph 1 2 3 1 2 3 4 Example of a reducible CFG Example of a nonreducible CFG

Global Data-Flow Analysis To apply global optimizations on basic blocks, data-flow information is collected by solving systems of data-flow equations Suppose we need to determine the reaching definitions for a sequence of statements S out[S] = gen[S]  (in[S] - kill[S]) out[B1] = gen[B1] = {d1, d2} out[B2] = gen[B2]  {d1} = {d1, d3} d1 reaches B2 and B3 and d2 reaches B2, but not B3 because d2 is killed in B2 B1: d1: i := m-1 d2: j := n B2: d3: j := j-1 B3:

Reaching Definitions S d: a:=b+c is of the form d: a:=b+c Then, the data-flow equations for S are: gen[S] = {d} kill[S] = Da - {d} out[S] = gen[S]  (in[S] - kill[S]) where Da = all definitions of a in the region of code

Reaching Definitions S1 S S2 is of the form S2 gen[S] = gen[S2]  (gen[S1] - kill[S2]) kill[S] = kill[S2]  (kill[S1] - gen[S2]) in[S1] = in[S] in[S2] = out[S1] out[S] = out[S2]

Reaching Definitions S S1 S2 is of the form S1 S2 gen[S] = gen[S1]  gen[S2] kill[S] = kill[S1]  kill[S2] in[S1] = in[S] in[S2] = in[S] out[S] = out[S1]  out[S2]

Reaching Definitions S S1 is of the form S1 gen[S] = gen[S1] kill[S] = kill[S1] in[S1] = in[S]  gen[S1] out[S] = out[S1]

Example Reaching Definitions d1: i := m-1; d2: j := n; d3: a := u1; do d4: i := i+1; d5: j := j-1; if e1 then d6: a := u2 else d7: i := u3 while e2 gen={d3,d4,d5,d6,d7} kill={d1,d2} ; gen={d1,d2,d3} kill={d4,d5,d6,d7} ; gen={d1,d2} kill={d4,d5,d7} gen={d3} kill={d6} gen={d4,d5,d6,d7} kill={d1,d2} ; d3 do gen={d4,d5,d6,d7} kill={d1,d2} gen={d1} kill={d4, d7} gen={d2} kill={d5} ; e1 d1 d2 gen={d6,d7} kill={} gen={d4,d5} kill={d1,d2,d7} ; if gen={d4} kill={d1, d7} gen={d5} kill={d2} gen={d6} kill={d3} gen={d7} kill={d1,d4} d4 d5 e1 d6 d7

Using Bit-Vectors to Compute Reaching Definitions d1: i := m-1; d2: j := n; d3: a := u1; do d4: i := i+1; d5: j := j-1; if e1 then d6: a := u2 else d7: i := u3 while e2 0011111 1100000 ; 1110000 0001111 ; 1100000 0001101 ; 0010000 0000010 d3 0001111 1100000 do 0001111 1100000 1000000 0001001 0100000 0000100 ; e1 d1 d2 0001100 1100001 ; if 0000011 0000000 0001000 1000001 d4 0000100 0100000 d5 e1 d6 0000010 0010000 d7 0000001 1001000

Accuracy, Safeness, and Conservative Estimations Conservative: refers to making safe assumptions when insufficient information is available at compile time, i.e. the compiler has to guarantee not to change the meaning of the optimized code Safe: refers to the fact that a superset of reaching definitions is safe (some may be have been killed) Accuracy: the larger the superset of reaching definitions, the less information we have to apply code optimizations

Reaching Definitions are a Conservative (Safe) Estimation Suppose this branch is never taken S1 S2 Estimation: gen[S] = gen[S1]  gen[S2] kill[S] = kill[S1]  kill[S2] Accurate: gen’[S] = gen[S1]  gen[S] kill’[S] = kill[S1]  kill[S]

Reaching Definitions are a Conservative (Safe) Estimation Why gen? S is of the form S1 in[S1] = in[S]  gen[S1] The problem is that in[S1] = in[S]  out[S1] makes more sense, but we cannot solve this directly, because out[S1] depends on in[S1]

Reaching Definitions are a Conservative (Safe) Estimation We have: (1) in[S1] = in[S]  out[S1] (2) out[S1] = gen[S1]  (in[S1] - kill[S1]) Solve in[S1] and out[S1] by estimating in1[S1] using safe but approximate out[S1]=, then re-compute out1[S1] using (2) to estimate in2[S1], etc. in1[S1] =(1) in[S]  out[S1] = in[S] out1[S1] =(2) gen[S1]  (in1[S1] - kill[S1]) = gen[S1]  (in[S] - kill[S1]) in2[S1] =(1) in[S]  out1[S1] = in[S]  gen[S1]  (in[S] - kill[S1]) = in[S]  gen[S1] out2[S1] =(2) gen[S1]  (in2[S1] - kill[S1]) = gen[S1]  (in[S]  gen[S1] - kill[S1]) = gen[S1]  (in[S] - kill[S1]) Because out1[S1] = out2[S1], and therefore in3[S1] = in2[S1], we conclude that in[S1] = in[S]  gen[S1] d: a:=b+c