More Code Generation and Optimization. Pat Morin COMP 3002

Similar documents
Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Review. Pat Morin COMP 3002

CODE GENERATION Monday, May 31, 2010

Compiler Optimization Techniques

Code Generation. M.B.Chandak Lecture notes on Language Processing

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

Code Analysis and Optimization. Pat Morin COMP 3002

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2]

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

Intermediate Code & Local Optimizations. Lecture 20

Tour of common optimizations

Compiler Optimization

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Compiler Design and Construction Optimization

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Compiler Code Generation COMP360

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

MIT Spring 2011 Quiz 3

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

Optimization Prof. James L. Frankel Harvard University

Goals of Program Optimization (1 of 2)

Compiler construction in4303 lecture 9

Machine-Independent Optimizations

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

Principles of Compiler Design

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Global Register Allocation

CS 6353 Compiler Construction, Homework #3

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

VIVA QUESTIONS WITH ANSWERS

16.10 Exercises. 372 Chapter 16 Code Improvement. be translated as

Lecture 21 CIS 341: COMPILERS

Global Register Allocation - 2

COP5621 Exam 4 - Spring 2005

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Liveness Analysis and Register Allocation

Global Register Allocation

CS 406/534 Compiler Construction Putting It All Together

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

Intermediate Code & Local Optimizations

Three address representation

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Introduction to Machine-Independent Optimizations - 1

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

CS415 Compilers. Intermediate Represeation & Code Generation

Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013

Code Generation (#')! *+%,-,.)" !"#$%! &$' (#')! 20"3)% +"#3"0- /)$)"0%#"

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

Intermediate representation

LECTURE 17. Expressions and Assignment

Low-level optimization

Lecture 25: Register Allocation

Programming Language Processor Theory

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

register allocation saves energy register allocation reduces memory accesses.

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413

Intermediate Code Generation

Abstract Interpretation Continued

INSTITUTE OF AERONAUTICAL ENGINEERING

Multi-dimensional Arrays

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Running class Timing on Java HotSpot VM, 1

Code generation and local optimization

Code generation and local optimization

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Control-Flow Graph and. Local Optimizations

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

MODEL ANSWERS COMP36512, May 2016

PRINCIPLES OF COMPILER DESIGN

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

IR Optimization. May 15th, Tuesday, May 14, 13

Compiler construction 2009

Register Allocation (via graph coloring) Lecture 25. CS 536 Spring

Compiler Optimization and Code Generation

Global Register Allocation - Part 3

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Compiler Optimization and Code Generation

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Register Allocation. Lecture 38

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Code Generation: Integrated Instruction Selection and Register Allocation Algorithms

Introduction to Optimization Local Value Numbering

Register allocation. TDT4205 Lecture 31

Transcription:

More Code Generation and Optimization Pat Morin COMP 3002

Outline DAG representation of basic blocks Peephole optimization Register allocation by graph coloring 2

Basic Blocks as DAGs 3

Basic Blocks as DAGs Like expressions, we can represent basic blocks as DAGs Create a node for the initial value of each variable that appears in the block For each statement s, create a node N whose children are the nodes defining the operands of s Each node N is labelled by the operation used Each node N has a list of variables that it defines A node N is marked as an output node if its value is used outside the basic block 4

Basic Block Example a := b + c b := a - d c := b + c d := a - d + (c) - (b,d) + (a) b0 c0 d0 5

Basic Block Example Draw the DAG for this basic block a := b + c b := b - d c := c + d e := b + c 6

Dead Code Elimination Dead code is code that computes a value that is never used Inspecting the flow graph we can tell if the value of a variable computed within a basic block is used outside that block These are called live variables or live on exit Algorithm While some node N has no ancestor and no live variable attached Delete N and its outgoing edges 7

Dead Code Elimination - Example Remove dead code assuming c is the only live variable + (c) + (y) + (x) - (b,d) + (a) b0 c0 d0 8

Using Algebraic Identities The DAG representation can allow constant folding When a subexpression involves 2 or more constants its value can sometimes be computed at compile time Commutativity: x * y = y * x When creating a node for x * y we should check if there already exists a node named x * y or a node named y * x Associativity: (a + b) + c = a + (b + c) 9

Associativity An example of using associativity Source Code: a = b + c; e + c + d + b; 3-AI (slow): a := b + c t := c + d e := t + b 3-AI (fast): a := b + c e := a + b 10

Array References Array references are a different kind of operator Assigning to an array location can change any element of the array x = a[i] a[j] = y z = a[i] =[] (x,z) []= (a[j]) WRONG a0 i0 j0 y0 11

Arrays References The right way x = a[i] creates a node with two children (a and i) that defines x a[j] = y creates a node with 3 children It has the side-effect of killing any node whose value depends on a x = a[i] a[j] = y z = a[i] =[] z =[] (x) []= a0 i0 j0 y0 12

Pointer References Pointer references are similar to array references assigning to a pointer kills all nodes changing any variable kills all pointer references x = *p t = a + b z = *p ; maybe p points to t *q = y x = a + b ; maybe q points to a or b 13

Reassembling Basic Blocks After converting a basic block into a DAG and applying optimizations we must reassemble the basic block Rules: 1)The order of instructions must obey the order of the DAG 2)Assigning to an array must follow any previous assignments to the same array 3)Evaluations of an array must follow any previous assignments to the same array 4)Any use of a variable must follow any previous indirect (pointer) assignments 5)Any indirect assignment must follow all previous evaluations of variables 14

Rearrangement Order Example Extra (dashed) edges implement rules 2-5 x = a[i] a[j] = y z = a[i] =[] z []= =[] (x) a0 i0 j0 y0 15

Basic Blocks as DAGs - Examples For each of the following lines of C x = a + b * c; x = a / (b+c) - d * (e+f); x = a[i] + 1; a[i] = b[c[i]]; *p++ = *q++; Write a basic block of 3-address instructions Convert the basic block into a DAG Convert the DAG back into a basic block 16

Peephole Optimization 17

Peephole Optimization Peephole optimization is done by looking at a small number of lines of code at a time We are looking at a small piece of code (the peephole) Some rules are applied to simplify or speed up various patterns Can be applied to intermediate code or machine code 18

Redundant Loads and Stores An adjacent pair of load and store operations (to the same variable) is unnecessary and can be eliminated mov x, R0 mov R0, x fload 4 fstore 4 fload 4 pop 19

Algebraic Simplification and Reduction in Strength Peephole optimization may recognize algebraic simplification and reduction in strength x = x + 1 => inc x [machine instruction] x = x + 0 => unnecessary x = x * 1 => unnecessary x = 2 * x => x = x + x To do this in the JVM we need a 3 line peephole 20

Unreachable Code Examining the code surrounding jump statements can often help if debug == 1 goto L1 goto L2 L1: some debug code... L2: L2: if 0!= 1 goto L2 some debug code... L2: if debug!= 1 goto L2 some debug code... goto L2 some debug code... L2: goto L2 L2: 21

Flow of Control Optimization goto L1... L1: goto L2 goto L2... L1: goto L2 if a < b goto L1... L1: goto L2 if a < b goto L2... L1: goto L2 goto L1... L1: if a < b goto L2 L3: if a < b goto L2 goto L3... L3: [saves a goto when a < b] 22

Register Allocation by Graph Coloring 23

Register Allocation By Graph Coloring We have seen how to manage registers within a basic block lazy load and store algorithm spill (or flush) a register back to memory only when necessary We have seen how to assign register variables across blocks (savings calculations) Both these methods are heuristics 24

Register Allocation by Graph Coloring Step 1: Generate code as if we have infinitely many registers R1,R2,R3,... R1, R2, R3,... are just like variable names Step 2: Create the register interference graph Nodes are R1, R2, R3,... Two nodes are adjacent if they are active at the same time Step 3: Properly color the register interference graph with the "colors" 1,2,3,...,k where k is as small as possible 25

Register Allocation by Graph Coloring A virtual register Ri that is colored with color c is assigned to the real register R(c mod r) where r is the number of real registers If k <= r then there are no conflicts and every variable gets its own register Otherwise, conflicts results in extra loads/stores 26

The Register Interference Graph For each register Ri, determine which lines of which basic blocks the register Ri is active in Ri conflicts with Rj if they are active at the same time 27

Coloring a Graph Coloring a graph with the fewest colors is NP-hard The following heuristic is used to color a graph G=(V,E) with the colors 1,2,3,... Find the vertex v with smallest degree (say d) Recursively color the graph G \ {v} Color v with smallest color not used by one of G's neighbours (Note: v will receive color at most d+1) 28

Graph Color Example 29

Graph Coloring Example 30

Graph Coloring Example 31

Notes on Graph Coloring What we are actually trying to find is a balanced ordering on the vertices of G If at most d neighbours of v appear before v in the ordering then v will be colored with color at most d+1 Thorup (1995) has shown that structured programming languages generate register interference graphs with small treewidth Optimally coloring a graph with tree width t can be done in O(f(t) n) where f(t) is a really fast growing function of t. Optimal graph coloring can be done in linear time! 32

Summary We've seen some more code generation and optimization techniques Representing basic blocks as DAGs allows: Common subexpression elimination Dead code elimination Use of algebraic identities Reordering of instructions Peephole optimization allows: Useless code elimination Use of algebraic identities and machine idioms Register allocation by graph color Can do "optimal" register allocation 33