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

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

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

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

More Code Generation and Optimization. Pat Morin COMP 3002

Introduction to Machine-Independent Optimizations - 1

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Tour of common optimizations

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

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Machine-Independent Optimizations

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

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

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

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

Lecture Notes on Loop Optimizations

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

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

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

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

Goals of Program Optimization (1 of 2)

Compiler Optimization Techniques

Compiler Optimization

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

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

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

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Intermediate Code & Local Optimizations. Lecture 20

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Compiler Optimization and Code Generation

Compiler Design and Construction Optimization

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)

Code Optimization. Code Optimization

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

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

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

Optimization Prof. James L. Frankel Harvard University

Compiler Construction SMD163. Understanding Optimization: Optimization is not Magic: Goals of Optimization: Lecture 11: Introduction to optimization

Final Exam Review Questions

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

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Compiler Construction 2010/2011 Loop Optimizations

CS202 Compiler Construction

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

Languages and Compiler Design II IR Code Optimization

Loops. Lather, Rinse, Repeat. CS4410: Spring 2013

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

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

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

CODE GENERATION Monday, May 31, 2010

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

Introduction to Machine-Independent Optimizations - 6

Compiler Construction 2016/2017 Loop Optimizations

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

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

Intermediate Code & Local Optimizations

Simone Campanoni Loop transformations

CS 2461: Computer Architecture 1

COMPILER DESIGN - CODE OPTIMIZATION

CS 6353 Compiler Construction, Homework #3

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

Computer Science 160 Translation of Programming Languages

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

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Code Analysis and Optimization. Pat Morin COMP 3002

CS153: Compilers Lecture 15: Local Optimization

Intra-procedural Data Flow Analysis Introduction

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

Code generation and local optimization

Code generation and local optimization

Running class Timing on Java HotSpot VM, 1

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

Lecture 8: Induction Variable Optimizations

Multi-dimensional Arrays

Bentley Rules for Optimizing Work

More Loop Optimizations

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

Compiler Code Generation COMP360

CSC D70: Compiler Optimization

INSTITUTE OF AERONAUTICAL ENGINEERING

CS553 Lecture Generalizing Data-flow Analysis 3

Compiler construction 2009

Resource-aware Computing Lecture 1- Grading, Rules and Introduction

Using Static Single Assignment Form

Code generation and optimization

Lecture 3 Local Optimizations, Intro to SSA

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

DEPARTMENT OF INFORMATION TECHNOLOGY / COMPUTER SCIENCE AND ENGINEERING UNIT -1-INTRODUCTION TO COMPILERS 2 MARK QUESTIONS

VIVA QUESTIONS WITH ANSWERS

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

Compiler Optimization Intermediate Representation

Lecture Notes on Loop-Invariant Code Motion

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur

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

Life Cycle of Source Program - Compiler Design

Intermediate representation

Program Op*miza*on and Analysis. Chenyang Lu CSE 467S

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

UNIT IV INTERMEDIATE CODE GENERATION

Calvin Lin The University of Texas at Austin

8 Optimisation. 8.2 Machine-Independent Optimisation

Transcription:

Code optimization Have we achieved optimal code? Impossible to answer! We make improvements to the code Aim: faster code and/or less space Types of optimization machine-independent In source code or internal form machine dependent In the object program (peephole-optimization) Source code Front-end Internal optimization What are the disadvantages? Debugging is made difficult because of code optimization (eg moving code around) Important to be able to switch off optimization The compiler runs more slowly Unpleasant effects! A:=B*C-D+E A:=B*C+E-D Can lead to overflow The effects of optimization: Register use and choice of instruction Inner loops (locality, 90-10 rule: 90% of the time goes on 10% of the code) Back-end machine code optimization Code transformations Lecture 8 Code optimization Page 215 Lecture 8 Code optimization Page 216 Types of optimizations 1 Algorithm optimization Replace a slow algorithm with a quicker one eg bubble sort quick sort Poor algorithms are the main source of inefficiency but difficult to optimize Machine and compiler independence 2 Intermediate code optimization Performed on intermediate code Examples of optimizations: Local optimization, within basic blocks Loop optimization Address calculations on arrays and records Global optimization Inter-procedural optimization Compiler-dependent but machine-independent Basic block A basic block is a sequence of operations with an entry and an exit No jump instructions may appear within the block (except for the very last instruction) Exercise: Divide the quadruples on the enclosed paper into groups of basic blocks and provide the corresponding control flow graph 3 Peephole optimization Transformations performed on machine code machine-dependent Lecture 8 Code optimization Page 217 Lecture 8 Code optimization Page 218

Local optimization Is performed within a basic block without information from any other block 1 Constant folding Constant expressions are calculated during compilation Example const NN = 4; i:=2+nn; (* i := 6 *) j:=i*5+a; (* j := 30 + a *) (* constant propagation *) : Constant folding if we know that b = -1 a:=5+b*c-3+d a:=2-c+d 2 Elimination of common sub-expressions is transformed to is transformed to: A[I+1] := B[I+1] T := I+1; A[T] := B[T]; D := D + C*B; A := D + C*B; T := C*B; D := D + T; A := D + T; Lecture 8 Code optimization Page 219 Lecture 8 Code optimization Page 220 3 Reduction in strength Replace an expensive operation by a cheaper one x:=y**2 x:=y*y x:=20*y x:=y+y Loop optimization Loop optimization aims to minimise the time spent in the loop, often by reducing the number of operations in the loop Control flow graph: Concatenation in Snobol L := Length(S1 S2) A number of nodes and edges where the nodes are basic blocks and edges are jumps L := Length(S1)+Length(S2) 1 2 3 4 5 Definition: loop A number of nodes which 1 are strongly connected, ie all nodes in a loop can be reached from the others 2 has a unique entry The loop in the diagram is {2, 3, 4} Lecture 8 Code optimization Page 221 Lecture 8 Code optimization Page 222

Example of loop optimizations: 2 Elimination of induction variables 1 Move loop invariants for i := 1 to 10 do z := i + b/c begin Has the greatest effect on the intermediate form Can remove variables which makes debugging more difficult: What is the value of I? debug> I= "Excuse me, optimizer killed me" can be rewritten t := b/c; for i := 1 to 10 do begin z := i + t; Lecture 8 Code optimization Page 223 Lecture 8 Code optimization Page 224 3 Loop unrolling 4 Loop fusion i:=1; while (i<=50) do begin i:= i + 1 can be written as i:=1; while (i<=50) do begin i:= i + 1; i:= i + 1; Merge several loops to one loop: for i:= 1 to n do for j:= 1 to m do a[i,j]:= 1; can be written as for i:=1 to n*m do {at the internal form level } a[i]:= 1; Reduce the number of tests and jumps by doubling the code: + more efficient in time - increased memory load Lecture 8 Code optimization Page 225 Lecture 8 Code optimization Page 226

Global data flow analysis: on whole procedures A higher level of optimization can be achieved if the whole procedure is analysed (Inter-procedural analysis deals with the whole program) Available expression For example, to manage to eliminate common subexpressions over block borders Concepts: Definition: A := 5 A is defined Use: B := A*C A is used The analysis is performed in two phases: 1 Forwards Reaching definitions Which definitions apply at a point p in a flow graph? A := A := A := 3 B := A B := 3 Lecture 8 Code optimization Page 227 Lecture 8 Code optimization Page 228 2 Backward analysis Live variables A variable v is live at point p if its value is used after p before any new definition of v is made v := A; is there a new definition of v? c := v; Global data flow analysis provides optimization: 1 Remove variables which are never referenced 2 Do not make calculations whose results are not used 3 Remove code which is not called or reachable (dead code elimination) 4 Code motion 5 Find uninitialised variables If variable A is in a register and it is dead (will not be referenced) the register can be released Very busy expressions An expression is very busy if all paths from the expression use it later in the program B := 3 D := B * C F:=B*C+D E:=3+B*C Lecture 8 Code optimization Page 229 Lecture 8 Code optimization Page 230

Examples of other machine-independent optimizations 1 Array-references C := A[I,J] + A[I,J+1] Elements are beside each other in memory Ought to be give me the next element 2 Expand the code for small routines x := sqr(y) x := y * y 4 Fixed references A[3,5] := B[1,5,4] Calculate the addresses during compilation as if: C := D 5 Exploit algebraic manipulations K := -C*(B-A) K := C*(A-B) Eliminate multiplication by 1 and addition with 0 3 Short-circuit evaluation of tests while (a > b) and (c-b < k) and if false the rest does not need to be evaluated Lecture 8 Code optimization Page 231 Lecture 8 Code optimization Page 232 Machine-dependent optimization: Peephole optimization We have a window of 3-4 instructions We try to optimize within the window and then move the window one instruction forwards Several passes over the code are often required Recognize a pattern which is appropriate for a special instruction ADD 1,R0 equivalent to INC A Algebraic simplifications ADD 0,R0 Eliminate! MOV A, R0 ADD 1,R0 Reduction in strength MUL 1,R0 MUL 2,R0 Eliminate! ADD 2,R0 are written SHIFT 1,R0 Improvement of jump over jump Redundant load and store instructions GOTO L1 { is changed to GOTO L2 } L1: GOTO L2 equivalent to Lecture 8 Code optimization Page 233 Lecture 8 Code optimization Page 234