Multi-dimensional Arrays

Similar documents
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?

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

Intermediate Code & Local Optimizations

Intermediate Code & Local Optimizations. Lecture 20

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

Local Optimizations #1

Operational Semantics of Cool

Local Optimizations #1

Register Allocation. Lecture 38

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

Lecture 25: Register Allocation

Code Optimization. Code Optimization

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

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

Code Generation. Lecture 30

CODE GENERATION Monday, May 31, 2010

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

CS143 - Written Assignment 4 Reference Solutions

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

Optimization Prof. James L. Frankel Harvard University

CS153: Compilers Lecture 15: Local Optimization

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

Language Security. Lecture 40

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

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

Tour of common optimizations

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

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

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

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

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

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

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

Midterm II CS164, Spring 2006

Bottom-Up Parsing. Lecture 11-12

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

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

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Solutions to Written Assignment 4

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

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

Compiler Optimization

Handling Assignment Comp 412

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

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

Bottom-Up Parsing. Lecture 11-12

Calvin Lin The University of Texas at Austin

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

Languages and Compiler Design II IR Code Optimization

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

CS 2461: Computer Architecture 1

Compiler Optimization Techniques

Intermediate Code Generation

Running class Timing on Java HotSpot VM, 1

The structure of a compiler

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

More Code Generation and Optimization. Pat Morin COMP 3002

Lecture 3 Local Optimizations, Intro to SSA

Induction Variable Identification (cont)

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

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Intermediate representation

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

Code Generation. Lecture 12

Run-time Environments

The View from 35,000 Feet

Run-time Environments

Intermediate Code Generation (ICG)

CSC D70: Compiler Optimization

Simplification of Boolean Functions

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

Intermediate Representations

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Lecture 4: Instruction Set Design/Pipelining

CS 164, Midterm #2, Spring O, M, C - e1 : Bool O, M, C - e2 : t2 O, M, C - e2 : T3 O, M, C - if e1 then e2 else e3 fi : T2 _ T3

CS558 Programming Languages

CS143 Compilers - Written Assignment 4

Intermediate Representations

Compiler construction 2009

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

Compiling Techniques

Compiler Optimization Intermediate Representation

Code generation and local optimization

Introduction to Optimization Local Value Numbering

Code generation and local optimization

Control-Flow Graph and. Local Optimizations

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

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

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Control Flow Analysis & Def-Use. Hwansoo Han

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

Code Generation. Lecture 19

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

Lecture 15: Iteration and Recursion

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

Structure of a Compiler. We looked at each stage in turn. Exceptions. Language Design and Implementation Issues

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

Data Flow Analysis. Program Analysis

Syntax-Directed Translation. Lecture 14

Transcription:

Multi-dimensional Arrays IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula) A 2D array is a 1D array of 1D arrays Java uses arrays of pointers to arrays for >1D arrays. But if row size constant, for faster access and compactness, may prefer to represent an MxN array as a 1D array of 1D rows (not pointers to rows): row-major order FORTRAN layout is 1D array of 1D columns: column-major order. 4/23/09 Prof. Hilfinger CS 164 Lecture 26 1 4/23/09 Prof. Hilfinger CS 164 Lecture 26 2 IL for 2D Arrays (Row-Major Order) Again, let S be size of one element, so that a row of length N has size NxS. igen(e 1 [e 2,e 3 ], t) = igen(e 1, t 1 ); igen(e 2,t 2 ); igen(e 3,t 3 ) igen(n, t 4 ) (N need not be constant) t 5 := t 4 * t 2 ; t 6 := t 5 + t 3 ; t 7 := t 6 *S; t 8 := t 7 + t 1 t := *t 8 Array Descriptors Calculation of element address for e 1 [e 2,e 3 ] has form VO + S 1 x e 2 + S 2 x e 3, where VO (address of e 1 [0,0]) is the virtual origin S 1 and S 2 are strides All three of these are constant throughout lifetime of array Common to package these up into an array descriptor, which can be passed in lieu of the array itself. 4/23/09 Prof. Hilfinger CS 164 Lecture 26 3 4/23/09 Prof. Hilfinger CS 164 Lecture 26 4 Array Descriptors (II) By judicious choice of descriptor values, can make the same formula work for different kinds of array. For example, if lower bounds of indices are 1 rather than 0, must compute address of e[1,1] + S 1 x (e 2-1) + S 2 x (e 3-1) But some algebra puts this into the form Observation These examples show profligate use of registers. Doesn t matter, because this is Intermediate Code. Rely on later optimization stages to do the right thing. VO + S 1 x e 2 + S 2 x e 3 where VO = address of e[1,1] - S 1 - S 2 4/23/09 Prof. Hilfinger CS 164 Lecture 26 5 4/23/09 Prof. Hilfinger CS 164 Lecture 26 6 1

Definition. Basic Blocks Code Optimization: Basic Concepts 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 in a basic block (except at beginning) Cannot jump out of a basic block (except at end) Each instruction in a basic block is executed after all the preceding instructions have been executed 4/23/09 Prof. Hilfinger CS 164 Lecture 26 7 4/23/09 Prof. Hilfinger CS 164 Lecture 26 8 Basic Block Example Consider the basic block 1. L: 2. t := 2 * x 3. w := t + x 4. if w > 0 goto L No way for (3) to be executed without (2) having been executed right before 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 flow 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., the execution can fall-through from block A to block B Frequently abbreviated as CFG 4/23/09 Prof. Hilfinger CS 164 Lecture 26 9 4/23/09 Prof. Hilfinger CS 164 Lecture 26 10 Control-Flow Graphs. Example. 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 utilization of some resource Execution time (most often) Code size Network messages sent Battery power used, etc. Optimization should not alter what the program computes The answer must still be the same 4/23/09 Prof. Hilfinger CS 164 Lecture 26 11 4/23/09 Prof. Hilfinger CS 164 Lecture 26 12 2

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) and very 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 terms of compilation time The fancy optimizations are both hard and costly The goal: maximum improvement with minimum of cost 4/23/09 Prof. Hilfinger CS 164 Lecture 26 13 4/23/09 Prof. Hilfinger CS 164 Lecture 26 14 Local Optimizations The simplest form of optimizations No need to analyze the whole procedure body Just the basic block in question Example: 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!) 4/23/09 Prof. Hilfinger CS 164 Lecture 26 15 4/23/09 Prof. Hilfinger CS 164 Lecture 26 16 Constant Folding Operations on constants can be computed at compile time In general, 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 Example: x := 2 + 2 x := 4 Example: if 2 < 0 jump L can be deleted When might constant folding be dangerous? Flow of Control Optimizations Eliminating unreachable code: Code that is unreachable in the control-flow graph Basic blocks that are not the target of any jump or fall through from a conditional Such basic blocks can be eliminated 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) 4/23/09 Prof. Hilfinger CS 164 Lecture 26 17 4/23/09 Prof. Hilfinger CS 164 Lecture 26 18 3

Single Assignment Form Some optimizations are simplified if each assignment is to a temporary that has not appeared already in the basic block Intermediate code can be rewritten to be in single assignment form x := a + y x := a + y a := x a 1 := x x := a * x x 1 := a 1 * x b := x + a b := x 1 + a 1 (x 1 and a 1 are fresh temporaries) Common Subexpression Elimination Assume Basic block is in single assignment form All assignments with same rhs compute the same value Example: x := y + z x := y + z w := y + z w := x Why is single assignment important here? 4/23/09 Prof. Hilfinger CS 164 Lecture 26 19 4/23/09 Prof. Hilfinger CS 164 Lecture 26 20 Copy Propagation If w := x appears in a block, all subsequent uses of w can be replaced with uses of x Example: b := z + y b := z + y a := b a := b x := 2 * a x := 2 * b This does not make the program smaller or faster but might enable other optimizations Constant folding Dead code elimination Again, single assignment is important here. 4/23/09 Prof. Hilfinger CS 164 Lecture 26 21 Copy Propagation and Constant Folding Example: a := 5 a := 5 x := 2 * a x := 10 y := x + 6 y := 16 t := x * y t := x << 4 4/23/09 Prof. Hilfinger CS 164 Lecture 26 22 Dead Code Elimination 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 Applying Local Optimizations Each local optimization does very little by itself Typically optimizations interact Performing one optimizations enables other opt. Typical optimizing compilers repeatedly perform optimizations until no improvement is possible The optimizer can also be stopped at any time to limit the compilation time 4/23/09 Prof. Hilfinger CS 164 Lecture 26 23 4/23/09 Prof. Hilfinger CS 164 Lecture 26 24 4

Initial code: a := x ** 2 e := b * 2 Algebraic optimization: a := x ** 2 e := b * 2 4/23/09 Prof. Hilfinger CS 164 Lecture 26 25 4/23/09 Prof. Hilfinger CS 164 Lecture 26 26 Algebraic optimization: e := b + b e := b + b 4/23/09 Prof. Hilfinger CS 164 Lecture 26 27 4/23/09 Prof. Hilfinger CS 164 Lecture 26 28 e := 3 + 3 Constant folding: e := 3 + 3 4/23/09 Prof. Hilfinger CS 164 Lecture 26 29 4/23/09 Prof. Hilfinger CS 164 Lecture 26 30 5

Constant folding: Common subexpression elimination: 4/23/09 Prof. Hilfinger CS 164 Lecture 26 31 4/23/09 Prof. Hilfinger CS 164 Lecture 26 32 Common subexpression elimination: 4/23/09 Prof. Hilfinger CS 164 Lecture 26 33 4/23/09 Prof. Hilfinger CS 164 Lecture 26 34 f := a + a g := 6 * f Dead code elimination: f := a + a g := 6 * f 4/23/09 Prof. Hilfinger CS 164 Lecture 26 35 4/23/09 Prof. Hilfinger CS 164 Lecture 26 36 6

Dead code elimination: f := a + a g := 6 * f This is the final form 4/23/09 Prof. Hilfinger CS 164 Lecture 26 37 Peephole Optimizations on Assembly Code The optimizations presented before work on intermediate code They are target independent But they can be applied on assembly language also Peephole optimization is an effective technique for improving assembly code The peephole is a short sequence of (usually contiguous) instructions The optimizer replaces the sequence with another equivalent (but faster) one 4/23/09 Prof. Hilfinger CS 164 Lecture 26 38 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 Examples: move $a $b, move $b $a move $a $b Works if move $b $a is not the target of a jump addiu $a $b k, lw $c ($a) lw $c k($b) - Works if $a not used later (is dead ) 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 Just like for local optimizations, peephole optimizations need to be applied repeatedly to get maximum effect 4/23/09 Prof. Hilfinger CS 164 Lecture 26 39 4/23/09 Prof. Hilfinger CS 164 Lecture 26 40 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 Local Optimizations. Notes (II). Serious problem: what to do with pointers? *t may change even if local variable t does not: Aliasing Arrays are a special case (address calculation) What to do about globals? What to do about calls? Not exactly jumps, because they (almost) always return. Can modify variables used by caller Next: global optimizations 4/23/09 Prof. Hilfinger CS 164 Lecture 26 41 4/23/09 Prof. Hilfinger CS 164 Lecture 26 42 7