Compiler construction in4020 – lecture 7 Koen Langendoen Delft University of Technology The Netherlands.

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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Intermediate Code Generation
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.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
1 Compiler Construction Intermediate Code Generation.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
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,
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Compiler construction in4020 – lecture 8 Koen Langendoen Delft University of Technology The Netherlands.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Attribute Grammars Professor Yihjia Tsai Tamkang University.
Tentative Schedule 20/12 Interpreter+ Code Generation 27/12 Code Generation for Control Flow 3/1 Activation Records 10/1 Program Analysis 17/1 Register.
Annotating Abstract Syntax Tree Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
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.
Compiler construction 2002
Compiler Summary Mooly Sagiv html://
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Code Generation Mooly Sagiv html:// Chapter 4.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
Compiler construction in4020 – lecture 11 Koen Langendoen Delft University of Technology The Netherlands.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 CS 201 Compiler Construction Data Flow Analysis.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Data Flow Analysis Compiler Baojian Hua
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
3. Annotating the Abstract Syntax Tree ― the Context From: Chapter 3, Modern Compiler Design, by Dick Grun et al.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Semantic analysis Jakub Yaghob
Data Flow Analysis Suman Jana
A Simple Syntax-Directed Translator
Constructing Precedence Table
CS 3304 Comparative Languages
Topic 10: Dataflow Analysis
University Of Virginia
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.
Data Flow Analysis Compiler Design
Presentation transcript:

Compiler construction in4020 – lecture 7 Koen Langendoen Delft University of Technology The Netherlands

attribute grammars formal attributes per symbol: inherited & synthesized attribute evaluation rule per production dependency graphs evaluation order Summary of lecture 6 S -attributed grammarsL -attributed grammarsmulti-visit grammars

Quiz 3.1 Which of the following items belong to a non-terminal, and which to a production? (a) inherited attribute (b) synthesized attribute (c) attribute evaluation rule (d) dependency graph (e) visiting routine

Overview context handling annotating the AST attribute grammars manual methods symbolic interpretation data-flow equations program text lexical analysis syntax analysis context handling annotated AST tokens AST

Manual methods for analyzing the AST preparing the grounds for code generation constant propagation last-def analysis (reaching definitions) live analysis common subexpression elimination dead-code elimination... we need flow-of-control information

Threading the AST determine the control flow graph that records the successor(s) of AST nodes - * ‘c’‘c’ ‘4’‘4’ * ‘a’‘a’ ‘b’ * first last intermediate code PUSH b MUL PUSH 4 PUSH a MUL PUSH c MUL SUB

Threading the AST result is a post-order traversal of the AST global variable: Last node pointer PROCEDURE Thread binary expression (Expr node pointer): Thread expression (Expr node pointer.left operand); Thread expression (Expr node pointer.right operand); // link this node to the dynamically last node SET Last node pointer.successor TO Expr node pointer; // make this node the new dynamically last node SET Last node pointer TO Expr node pointer; * ‘c’‘c’ ‘4’‘4’ * ‘a’‘a’

Multiple successors problem: threading is built around single Last node pointer IF condition THENELSE first last

Multiple successors problem: threading is built around single Last node pointer solution: introduce join node IF condition THENELSE first last FI

Symbolic interpretation behaviour of code is determined by (the values of) variables simulate run-time execution at compile time attach a stack representation to each arrow in the control flow graph an entry summarizes all compile-time knowledge about the variable/constant

Symbolic interpretation condition IF THEN ELSE FI

Symbolic interpretation condition IF THEN ELSE > y0 y x 5 y x 5 5 y x y x 5 T y x 5 y x 5 y x 5 x = 7; y x 5 7 y x 5 dead code FI merge

Exercise (5 min.) draw the control flow graph for while C do S od propagate initial stack when C represents y>x and S stands for x:=7 y x 5

Answers

condition WHILE BODY y x 5 y x 5 y x 5 y x 5 b y x 5 7

Simple symbolic interpretation used in narrow compiler simple properties + simple control-flow example: detecting the use of uninitialized variables maintain property list advance list through control flow graph int foo(int n) { int first; while (n-- > 0) { if (glob[n] == KEY) first = n; } return first; }

Tracking uninitialized variables parameter declaration: variable declaration: expression: assignment: control statement fork nodes: join nodes: add (ID:Initialized) tuple add (ID:Uninitialized) tuple check status of used variables set tuple to (ID:Initialized) copy list merge lists merge( Init, Init) = Init merge( Uninit, Uninit) = Uninit merge( x, x) = Maybe int foo(int n) { int first; while (n-- > 0) { if (glob[n] == KEY) first = n; } return first; }

Simple symbolic interpretation flow-of-control structures with one entry and one exit point (no GOTOs) the values of the property are severely constrained (see book) example: constant propagation

Constant propagation record the exact value iso Initialized int i = 0; while (condition) { if (i>0) printf("loop reentered\n"); … i++; } simple symbolic interpretation fails // i == 0 // i == 1 // i == {0,1}

Full symbolic interpretation maintain a property list for each entry point (label), initially set to empty traverse flow of control graph L:merge current-list into list-L continue with list-L jump L;merge current-list into list-L continue with the empty list repeat until nothing changed guarantee termination – select suitable property

int i = 0; while (condition) { if (i>0) printf("loop reentered\n"); … i++; } int i = 0; L: if (condition) { if (i>0) printf("loop reentered\n"); … i++; goto L; } Constant propagation full symbolic interpretation property: unknown < value < any // i == 0 // i == 1 // empty // i == ANY // i == 0 // i == ANY

Break

Data flow equations “automated” full symbolic interpretation stack replaced by collection of sets IN(N) OUT(N) semantics are expressed by constant sets KILL(N) GEN(N) equations IN(N) =  M  predecessor[N] OUT(M) OUT(N) = IN(N) \ KILL(N)  GEN(N) n = n-1; IN OUT

Data flow equations solved through iteration (closure algorithm) data-flow nodes may not change the stack individual statements basic blocks example: tracking uninitialized variables properties: I x, M x, U x GEN(x = expr ;) = KILL(x = expr ;) = int foo(int n) { int first; while (n-- > 0) { if (glob[n] == KEY) first = n; } return first; } {I x} {U x, M x} int foo(int n) { int first; L: n = n-1; if (n >= 0) { if (glob[n] == KEY) first = n; goto L; } return first; }

T T Data flow equations solved through iteration (closure algorithm) data-flow nodes may not change the stack individual statements basic blocks example: tracking uninitialized variables properties: I x, M x, U x GEN(x = expr ;) = KILL(x = expr ;) = {I x} {U x, M x} if (glob[n] == KEY) first = n; goto L; return first; L: n = n-1; if (n >= 0) F F

{} IN(N) =  M  predecessor[N] OUT(M) OUT(N) = IN(N) \ KILL(N)  GEN(N) Iterative solution initialization: set all sets to empty iterate from top to bottom {I n, U f} {I n, I f} {I n, M f} T T F return first; 6 F first = n; 4 goto L; 5 L: n = n-1; 1 int foo(int n) { int first; 0 if (glob[n] == KEY) 3 if (n >= 0) 2 statementKILLGEN 0I n U f 1U n M n InIn 4U f M f IfIf

Iterative solution statement iteration 1iteration 2 KILLGENINOUTINOUT 0 I n U f 1U n M n InIn I n U f 2 I n M f 3I n U f I n M f 4U f M f IfIf I n U f I n I f I n M f I n I f 5I n M f 6I n U f I n M f

Efficient data-flow equations limit to (set of) on/off properties set union = bitwise OR set difference = bitwise AND NOT combine statements into basic blocks KILL[S 1 ;S 2 ] = KILL[S 1 ]  KILL[S 2 ] GEN[S 1 ;S 2 ] = GEN[S 2 ]  (GEN[S 1 ] \ KILL[S 2 ]) sort data-flow graph depth-first traversal (break cycles!)

Live analysis a variable is live at node N if the value it holds is used on some path further down the control-flow graph; otherwise it is dead useful information for register allocation information must flow “backwards” (up) through the control-flow graph difficult for symbolic interpretation easy for data flow equations

Solving data-flow equations forwards backwards IN(N) =  M  predecessor[N] OUT(M) OUT(N) = IN(N) \ KILL(N)  GEN(N) if (cond) IN OUT OUT(N) =  M  successor[N] IN(M) IN(N) = OUT(N) \ KILL(N)  GEN(N)

Exercise (5 min.) determine the KILL and GEN sets for the property “V is live here” for the following statements statement SKILLGEN v = a  b v = M[i] M[i] = v f(a 1,...,a n ) v = f(a 1,...,a n ) if a>b then goto L 1 else goto L 2 L: goto L

Answers determine the KILL and GEN sets for the property “V is live here” for the following statements statement SKILLGEN v = a  b v = M[i] M[i] = v f(a 1,...,a n ) v = f(a 1,...,a n ) if a>b then goto L 1 else goto L 2 L: goto L {v} {a,b} \ KILLS[S] {v} {i} \ KILLS[S] {} {i,v} {} { a 1,...,a n } {v} { a 1,...,a n } \ KILLS[S] {} {a,b} {}

Exercise (7 min.) draw the control-flow graph for the following code fragment perform backwards live analysis using the KILL and GEN sets from the previous exercise double average(int n, double v[]) { int i; double sum = 0.0; for (i=0; i<n; i++) { sum += v[i]; } return sum/n; }

Answers OUT(N) =  M  successor[N] IN(M) IN(N) = OUT(N) \ KILL(N)  GEN(N) statementKILLGEN

Answers OUT(N) =  M  successor[N] IN(M) IN(N) = OUT(N) \ KILL(N)  GEN(N) statementKILLGEN sum = 0.0; i = 0; if (i<n) sum += v[i]; i++; return sum/n;

Answers sum = 0.0; i = 0; if (i<n) sum += v[i]; i++; return sum/n; live: n live: n, i, sum live: n, sum live: n, i, sum OUT(N) =  M  successor[N] IN(M) IN(N) = OUT(N) \ KILL(N)  GEN(N) statementKILLGEN 1sum, i 2i, n 3sum, i 4sum, n

Answers stat em ent iteration 1iteration 2iteration 3 KILLGENOUTINOUTINOUTIN 0n 1sum, ii, nni, n, sumn 2i, n i, n, sum 3sum, i i, ni, n, sum 4sum, nn, sumi, n, sum process nodes top to bottom (from 0 to 4) processing nodes in reverse (from 4 to 0) requires only two iterations

Summary manual methods for annotating the AST threading symbolic interpretation data-flow equations symbolic interpretation data-flow equations simplefull methodstack-based simulationIN, OUT, GEN, KILL sets granularityAST nodebasic block algorithmone-passiterative directionforwardsforwards & backwards

Homework study sections: interprocedural data-flow analysis live analysis by symbolic interpretation assignment 1: replace yacc with LLgen deadline April 9 08:59 print handout for next week [blackboard]