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

Similar documents
Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations

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?

Multi-dimensional Arrays

Local Optimizations #1

Operational Semantics of Cool

Local Optimizations #1

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

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

CODE GENERATION Monday, May 31, 2010

Code Optimization. Code Optimization

Register Allocation. CS 502 Lecture 14 11/25/08

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

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

Intermediate representation

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

More Code Generation and Optimization. Pat Morin COMP 3002

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

Tour of common optimizations

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

CS 406/534 Compiler Construction Putting It All Together

CS143 - Written Assignment 4 Reference Solutions

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

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

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

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Alternatives for semantic processing

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

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

CS153: Compilers Lecture 15: Local Optimization

Code Generation. Lecture 30

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

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

Running class Timing on Java HotSpot VM, 1

Principles of Compiler Design

CSC D70: Compiler Optimization

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

Lecture 3 Local Optimizations, Intro to SSA

Compiler Optimization

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

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Register Allocation. Lecture 38

Language Security. Lecture 40

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

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

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

Compiler construction 2009

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

Optimization Prof. James L. Frankel Harvard University

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Compiler Design Spring 2017

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

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

Compiler Optimization Intermediate Representation

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

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

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

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

Control Flow Analysis & Def-Use. Hwansoo Han

CSE 401/M501 Compilers

Register Allocation 3/16/11. What a Smart Allocator Needs to Do. Global Register Allocation. Global Register Allocation. Outline.

Unoptimized Code Generation

Simplification of Boolean Functions

Lecture 25: Register Allocation

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Compiler construction in4303 lecture 9

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

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

We can emit stack-machine-style code for expressions via recursion

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Intermediate Code Generation

Compiler Optimization Techniques

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

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

An Overview of Compilation

Introduction to Operating Systems

Bottom-Up Parsing. Lecture 11-12

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Control Flow Analysis

Status. We ll do code generation first... Outline

Bottom-Up Parsing. Lecture 11-12

Run-time Environments

Run-time Environments

CS2210: Compiler Construction. Code Generation

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

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

Syntax-Directed Translation. Lecture 14

Introduction to Optimization Local Value Numbering

Induction Variable Identification (cont)

Control flow graphs and loop optimizations. Thursday, October 24, 13

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

CSc 453 Interpreters & Interpretation

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

Transcription:

Lecture Outline Intermediate code Intermediate Code & Local Optimizations Lecture 14 Local optimizations Next time: global optimizations Prof. Aiken CS 143 Lecture 14 1 Prof. Aiken CS 143 Lecture 14 2 Code Generation Summary We have discussed Runtime organization Simple stack machine code generation Improvements to stack machine code generation Our compiler maps AST to assembly language And does not perform optimizations Optimization Optimization is our last compiler phase Most complexity in modern compilers is in the optimizer Also by far the largest phase First, we need to discuss intermediate languages Prof. Aiken CS 143 Lecture 14 3 Prof. Aiken CS 143 Lecture 14 4 Why Intermediate Languages? When should we perform optimizations? On AST Pro: Machine independent Con: Too high level On assembly language Pro: Exposes optimization opportunities Con: Machine dependent Con: Must reimplement optimizations when retargetting On an intermediate language Pro: Machine independent Pro: Exposes optimization opportunities Prof. Aiken CS 143 Lecture 14 5 Intermediate Languages Intermediate language = high-level assembly Uses register names, but has an unlimited number Uses control structures like assembly language Uses opcodes but some are higher level E.g., push translates to several assembly instructions Most opcodes correspond directly to assembly opcodes Prof. Aiken CS 143 Lecture 14 6 1!

Three-Address Intermediate Code Each instruction is of the form x := y op z x := op y y and z are registers or constants Common form of intermediate code The expression x + y * z is translated t 1 := y * z Generating Intermediate Code Similar to assembly code generation But use any number of IL registers to hold intermediate results t 2 := x + t 1 Each subexpression has a name Prof. Aiken CS 143 Lecture 14 7 Prof. Aiken CS 143 Lecture 14 8 Generating Intermediate Code (Cont.) igen(e, t) function generates code to compute the value of e in register t igen(e 1 + e 2, t) = igen(e 1, t 1 ) (t 1 is a fresh register) igen(e 2, t 2 ) (t 2 is a fresh register) t := t 1 + t 2 Unlimited number of registers simple code generation Intermediate Code Notes You should be able to use intermediate code At the level discussed in lecture You are not expected to know how to generate intermediate code Because we won t discuss it But really just a variation on code generation... Prof. Aiken CS 143 Lecture 14 9 Prof. Aiken CS 143 Lecture 14 10 An Intermediate Language Definition. Basic Blocks P S P ε id s are register names S id := id op id Constants can replace id s id := op id id := id Typical operators: +, -, * push id id := pop if id relop id goto L L: jump L Prof. Aiken CS 143 Lecture 14 11 A basic block is a maximal sequence of instructions with: no labels (except at the first instruction), and no jumps (except in the last instruction) Idea: Cannot jump into a basic block (except at beginning) Cannot jump out of a basic block (except at end) A basic block is a single-entry, single-exit, straight-line code segment Prof. Aiken CS 143 Lecture 14 12 2!

Basic Block Example Consider the basic block 1. L: 2. t := 2 * x 3. w := t + x 4. if w > 0 goto L (3) executes only after (2) We can change (3) to w := 3 * x Can we eliminate (2) as well? Definition. Control-Flow Graphs A control-flow graph is a directed graph with Basic blocks as nodes An edge from block A to block B if the execution can pass from the last instruction in A to the first instruction in B E.g., the last instruction in A is jump L B E.g., execution can fall-through from block A to block B Prof. Aiken CS 143 Lecture 14 13 Prof. Aiken CS 143 Lecture 14 14 Example of Control-Flow Graphs Optimization Overview x := 1 i := 1 L: x := x * x i := i + 1 if i < 10 goto L The body of a method (or procedure) can be represented as a controlflow graph There is one initial node All return nodes are terminal Optimization seeks to improve a program s resource utilization Execution time (most often) Code size Network messages sent, etc. Optimization should not alter what the program computes The answer must still be the same Prof. Aiken CS 143 Lecture 14 15 Prof. Aiken CS 143 Lecture 14 16 A Classification of Optimizations For languages like C and Cool there are three granularities of optimizations 1. Local optimizations Apply to a basic block in isolation 2. Global optimizations Apply to a control-flow graph (method body) in isolation 3. Inter-procedural optimizations Apply across method boundaries Most compilers do (1), many do (2), few do (3) Cost of Optimizations In practice, a conscious decision is made not to implement the fanciest optimization known Why? Some optimizations are hard to implement Some optimizations are costly in compilation time Some optimizations have low benefit Many fancy optimizations are all three! Goal: Maximum benefit for minimum cost Prof. Aiken CS 143 Lecture 14 17 Prof. Aiken CS 143 Lecture 14 18 3!

Local Optimizations The simplest form of optimizations No need to analyze the whole procedure body Just the basic block in question algebraic simplification Algebraic Simplification Some statements can be deleted x := x + 0 x := x * 1 Some statements can be simplified x := x * 0 x := 0 y := y ** 2 y := y * y x := x * 8 x := x << 3 x := x * 15 t := x << 4; x := t - x (on some machines << is faster than *; but not on all!) Prof. Aiken CS 143 Lecture 14 19 Prof. Aiken CS 143 Lecture 14 20 Constant Folding Operations on constants can be computed at compile time If there is a statement x := y op z And y and z are constants Then y op z can be computed at compile time x := 2 + 2 x := 4 if 2 < 0 jump L can be deleted When might constant folding be dangerous? Prof. Aiken CS 143 Lecture 14 21 Flow of Control Optimizations Eliminate unreachable basic blocks: Code that is unreachable from the initial block E.g., basic blocks that are not the target of any jump or fall through from a conditional Why would such basic blocks occur? Removing unreachable code makes the program smaller And sometimes also faster Due to memory cache effects (increased spatial locality) Prof. Aiken CS 143 Lecture 14 22 Single Assignment Form Common Subexpression Elimination Some optimizations are simplified if each register occurs only once on the left-hand side of an assignment Rewrite intermediate code in single assignment form x := z + y b := z + y a := x a := b x := 2 * x x := 2 * b (b is a fresh register) More complicated in general, due to loops Prof. Aiken CS 143 Lecture 14 23 If Basic block is in single assignment form A definition x := is the first use of x in a block Then When two assignments have the same rhs, they compute the same value x := y + z x := y + z w := y + z w := x (the values of x, y, and z do not change in the code) Prof. Aiken CS 143 Lecture 14 24 4!

Copy Propagation If w := x appears in a block, replace subsequent uses of w with uses of x Assumes single assignment form b := z + y b := z + y a := b a := b x := 2 * a x := 2 * b Copy Propagation and Constant Folding a := 5 a := 5 x := 2 * a x := 10 y := x + 6 y := 16 t := x * y t := x << 4 Only useful for enabling other optimizations Constant folding Dead code elimination Prof. Aiken CS 143 Lecture 14 25 Prof. Aiken CS 143 Lecture 14 26 Copy Propagation and Dead Code Elimination Applying Local Optimizations If w := rhs appears in a basic block w does not appear anywhere else in the program Then the statement w := rhs is dead and can be eliminated Dead = does not contribute to the program s result Example: (a is not used anywhere else) x := z + y b := z + y b := z + y a := x a := b x := 2 * b x := 2 * a x := 2 * b Prof. Aiken CS 143 Lecture 14 27 Each local optimization does little by itself Typically optimizations interact Performing one optimization enables another Optimizing compilers repeat optimizations until no improvement is possible The optimizer can also be stopped at any point to limit compilation time Prof. Aiken CS 143 Lecture 14 28 Initial code: a := x ** 2 d := c * c e := b * 2 Algebraic optimization: a := x ** 2 d := c * c e := b * 2 Prof. Aiken CS 143 Lecture 14 29 Prof. Aiken CS 143 Lecture 14 30 5!

Algebraic optimization: d := c * c e := b << 1 Copy propagation: d := c * c e := b << 1 Prof. Aiken CS 143 Lecture 14 31 Prof. Aiken CS 143 Lecture 14 32 Copy propagation: d := x * x e := 3 << 1 Constant folding: d := x * x e := 3 << 1 Prof. Aiken CS 143 Lecture 14 33 Prof. Aiken CS 143 Lecture 14 34 Constant folding: d := x * x Common subexpression elimination: d := x * x Prof. Aiken CS 143 Lecture 14 35 Prof. Aiken CS 143 Lecture 14 36 6!

Common subexpression elimination: d := a Copy propagation: d := a Prof. Aiken CS 143 Lecture 14 37 Prof. Aiken CS 143 Lecture 14 38 Copy propagation: d := a f := a + a g := 6 * f Dead code elimination: d := a f := a + a g := 6 * f Prof. Aiken CS 143 Lecture 14 39 Prof. Aiken CS 143 Lecture 14 40 Dead code elimination: f := a + a g := 6 * f This is the final form Prof. Aiken CS 143 Lecture 14 41 Peephole Optimizations on Assembly Code These optimizations work on intermediate code Target independent But they can be applied on assembly language also Peephole optimization is effective for improving assembly code The peephole is a short sequence of (usually contiguous) instructions The optimizer replaces the sequence with another equivalent one (but faster) Prof. Aiken CS 143 Lecture 14 42 7!

Peephole Optimizations (Cont.) Write peephole optimizations as replacement rules i 1,, i n j 1,, j m where the rhs is the improved version of the lhs move $a $b, move $b $a move $a $b Works if move $b $a is not the target of a jump Peephole Optimizations (Cont.) Many (but not all) of the basic block optimizations can be cast as peephole optimizations Example: addiu $a $b 0 move $a $b Example: move $a $a These two together eliminate addiu $a $a 0 Another example addiu $a $a i, addiu $a $a j addiu $a $a i+j Prof. Aiken CS 143 Lecture 14 43 As for local optimizations, peephole optimizations must be applied repeatedly for maximum effect Prof. Aiken CS 143 Lecture 14 44 Local Optimizations: Notes Intermediate code is helpful for many optimizations Many simple optimizations can still be applied on assembly language Program optimization is grossly misnamed Code produced by optimizers is not optimal in any reasonable sense Program improvement is a more appropriate term Next time: global optimizations Prof. Aiken CS 143 Lecture 14 45 8!