Compiler Construction

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

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.
Register Usage Keep as many values in registers as possible Register assignment Register allocation Popular techniques – Local vs. global – Graph coloring.
Register allocation Morgensen, Torben. "Register Allocation." Basics of Compiler Design. pp from (
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.
Coalescing Register Allocation CS153: Compilers Greg Morrisett.
Compiler Construction Sohail Aslam Lecture ExampleExample a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2.
COMPILERS Register Allocation hussein suleman uct csc305w 2004.
1 CS 201 Compiler Construction Machine Code Generation.
Graph-Coloring Register Allocation CS153: Compilers Greg Morrisett.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
Lecture 26 Epilogue: Or Everything else you Wanted to Know about Compilers (more accurately Everything else I wanted you to Know) Topics Getreg – Error.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
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.
Code Generation Professor Yihjia Tsai Tamkang University.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
Code Generation for Basic Blocks Introduction Mooly Sagiv html:// Chapter
Register Allocation (via graph coloring)
Register Allocation (via graph coloring). Lecture Outline Memory Hierarchy Management Register Allocation –Register interference graph –Graph coloring.
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
4/29/09Prof. Hilfinger CS164 Lecture 381 Register Allocation Lecture 28 (from notes by G. Necula and R. Bodik)
Code Generation Mooly Sagiv html:// Chapter 4.
Supplementary Lecture – Register Allocation EECS 483 University of Michigan.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Chapter# 5 Intermediate code generation.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Compilers Modern Compiler Design
Final Code Generation and Code Optimization.
Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.
2/22/2016© Hal Perkins & UW CSEP-1 CSE P 501 – Compilers Register Allocation Hal Perkins Winter 2008.
Compiler Principles Fall Compiler Principles Lecture 8: Intermediate Representation Roman Manevich Ben-Gurion University.
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.
Code Analysis and Optimization Pat Morin COMP 3002.
More Code Generation and Optimization Pat Morin COMP 3002.
Chapter 8 Code Generation
Compilation (Semester A, 2013/14)
Code Optimization.
Mooly Sagiv html://
The compilation process
Register Allocation Hal Perkins Autumn 2009
Register Allocation Noam Rinetzky Text book:
Code Generation Part I Chapter 9
More Graph Algorithms.
Register Allocation Hal Perkins Autumn 2011
Unit IV Code Generation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
CS 201 Compiler Construction
Code Generation Part I Chapter 9
Code Optimization Overview and Examples Control Flow Graph
Interval Partitioning of a Flow Graph
Final Code Generation and Code Optimization
Lecture 16: Register Allocation
TARGET CODE -Next Usage
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Lecture 17: Register Allocation via Graph Colouring
Code Generation Part II
Compiler Construction
Fall Compiler Principles Lecture 13: Summary
(via graph coloring and spilling)
Code Optimization.
CS 201 Compiler Construction
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 45 compiler: Bottom-up Parsing

Basic Code Generation Liveness information allows us to keep values in registers if they will be used later (efficiency)

Basic Code Generation Why do we assume all variables are live at the end of blocks? Can we do better?

Basic Code Generation Why do we need to save live variables at the end? We might have to reload them in the next block. Global Data-Flow Analysis

DAG Representations A dag (directed acyclic graph) for a basic block has the following labels for the nodes:

DAG Representations Leaves are labeled by unique identifiers. Interior nodes are labeled by operator symbols. Nodes can have multiple labels since they represent computed values.

DAG Representations BB DAG for x = y op z op x y z

Generating DAGs For statement i: x = y op z if y op z node exists, add x to the label for that node. else add node for y op z. If y or z exist in the dag, point to existing locations else add leaves for y and/or z and have the op node point to them.

Generating DAGs Label the op node with x. If x existed previously as a leaf, subscript that previous entry. If x is associated with other interior nodes, remove them from that list.

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 a + b c

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 t1 * a + b c

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 b + t1 * a + b’ c

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 c * b + t1 * a + b’ c’

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 t2 + * c b + t1 * a + b’ c’

Example 1 a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 + t2 + * c b + t1 * + b’ c’

Example 2 t1 = 4 * i t2 = a[t1] t3 = 4 * i t4 = b[t3] t5 = t2 * t4 t6 = p + t5 p = t6 t7 = i + 1 i = t7 if i <20 goto (1) t6,p + p’ t5 * t4 (1) t2 [ ] [ ] < 20 t1,t3 t7,i * + a b 4 i’ 1

DAGs and optimization Detect common sub-expression elimination. Node with more than one parent is common sub-expression Reordering to reduce number of generated instructions node listing labeling algorithms

Order Matters Order 1 Order 2 t1 = a + b t2 = c + d t2 = c + d - Order 1 t1 = a + b t2 = c + d t3 = e – t2 t4 = t1 – t3 Order 2 t2 = c + d t3 = e – t2 t1 = a + b t4 = t1 – t3

Order #1 with 2 registers t1 = a + b t2 = c + d t3 = e – t2 t4 = t1 – t3 MOV a, R0 ADD b,R0 MOV c, R1 ADD D, R1 MOV R0,t1 MOV e, R0 SUB R1,R0 MOV t1,R1 SUB R0, R1 MOV R1,t4 ; 10 instructions

Order #2 with 2 registers t2 = c + d t3 = e – t2 t1 = a + b t4 = t1 – t3 MOV c, R0 ADD D, R0 MOV e, R1 SUB R0,R1 MOV a,R0 ADD b, R0 MOV R0,t4 ; 7 instructions

Order 1 t1 = a + b t2 = c + d t3 = e – t2 t4 = t1 – t3 Order 2 t2 = c + d t3 = e – t2 t1 = a + b t4 = t1 – t3 Reordering improved code because computation of t4 immediately followed computation of t1, its left operand. t1 must be in a register and it is

Optimal Ordering Algorithms Heuristic ordering algorithm: node listing Ordering when DAG for a block is a tree Both algorithms presented in the book

Register Allocation How to best use the bounded number of registers. Local register allocation: graph coloring problem.

Register Allocation Complications: special purpose registers operators requiring multiple registers.

Algorithm: K registers Compute liveness information. Create interference graph G one node for each variable, an edge connects two variables if one is live at a point where the other is defined

Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} inteference graph

Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a t1 inteference graph

Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a t1 b inteference graph

Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a c t1 b inteference graph

Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a c t1 b t2 inteference graph

Example live a = b + c {a} t1 = a * a {t1,a} b = t1 + a {b,t1} c = t1 * b {b,c} t2 = c + b {b,c,t2} a = t2 + t2 {a,b,c} a c t1 b t2 inteference graph

3. Simplify - For any node m with less than K neighbors, remove it from the graph and push it onto a stack. If G - m can be colored with K colors, so can G. If we reduce the entire graph, goto step 5.

4. Spill - If we get to the point where we are left with only nodes with degree >= K, mark some node for potential spilling (to memory). Remove and push onto stack. Back to step 3.

5. Assign colors - Starting with empty graph, rebuild graph by popping elements off the stack and assigning a color different from neighbors.

5. (continued) Potential spill nodes may or may not be colorable 5. (continued) Potential spill nodes may or may not be colorable. Process may require iterations and rewriting of some of the code to create more temporaries.

Example, k = 3 Assume k = 3 a c t1 b t2 Remove t1 t1

Example Assume k = 3 a c b t2 a Remove a t1

Example Assume k = 3 c b t2 b a Remove b t1

Example Assume k = 3 c c t2 b a Remove c t1

Example Assume k = 3 t2 c b a Remove t2 t1

Rebuild Graph Assume k = 3 t2 c b a t1

Example Assume k = 3 c c b t2 b R1 a t1

Example Assume k = 3 a c R2 b t2 b R1 a t1

Example Assume k = 3 a c R2 t1 b t2 R3 R1 a t1

Example Assume k = 3 R1 a c R2 t1 b t2 R3 R1 t1

Example Assume k = 3 R1 a c R2 R2 t1 b t2 R3 R1 chromatic number is 3

Compiler Construction End compiler: Bottom-up Parsing