Code Optimization. Code Optimization

Similar documents
Compiler Optimization and Code Generation

Goals of Program Optimization (1 of 2)

Tour of common optimizations

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

Intermediate Code & Local Optimizations

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

Intermediate Code & Local Optimizations. Lecture 20

Data Flow Analysis. Program Analysis

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

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

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

Multi-dimensional Arrays

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

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

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?

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

Introduction to Compilers

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

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

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

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

CS 406/534 Compiler Construction Putting It All Together

Introduction to Optimization Local Value Numbering

Optimization Prof. James L. Frankel Harvard University

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

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

Operational Semantics of Cool

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

Languages and Compiler Design II IR Code Optimization

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

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

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

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

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

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

CS5363 Final Review. cs5363 1

Programming Language Processor Theory

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

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

Compilers. Optimization. Yannis Smaragdakis, U. Athens

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

CoVaC: Compiler Verification by Program Analysis of the Cross-Product. Anna Zaks, Amir Pnueli. May 28, FM 08, Turku, Finland

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

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

DFA&:OPT-METAFrame: A Tool Kit for Program Analysis and Optimization

Just-In-Time Compilers & Runtime Optimizers

Intermediate representation

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

Induction Variable Identification (cont)

EECS 583 Class 8 Classic Optimization

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

Introduction to Machine-Independent Optimizations - 1

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

Machine-Independent Optimizations

Instruction Selection: Peephole Matching. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

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

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

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Local Optimizations #1

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

Data-flow Analysis - Part 2

8. Static Single Assignment Form. Marcus Denker

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

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

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

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

Compiler Code Generation COMP360

Lecture 2. Introduction to Data Flow Analysis

Lecture 3 Local Optimizations, Intro to SSA

Topic I (d): Static Single Assignment Form (SSA)

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

Instruction Selection and Scheduling

ICS 252 Introduction to Computer Design

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

Measuring the User Debugging Experience. Greg Bedwell Sony Interactive Entertainment

Local Optimizations #1

Overview of a Compiler

Compiler Optimization

Programming Languages and Compilers. Jeff Nucciarone AERSP 597B Sept. 20, 2004

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Intra-procedural Data Flow Analysis Introduction

PRINCIPLES OF COMPILER DESIGN

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

Calvin Lin The University of Texas at Austin

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

Introduction to Machine-Independent Optimizations - 6

Data Flow Information. already computed

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

Optimizing Simulink R Models

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Lecture Notes on Loop Optimizations

CSC D70: Compiler Optimization

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Why Global Dataflow Analysis?

CS553 Lecture Generalizing Data-flow Analysis 3

More Code Generation and Optimization. Pat Morin COMP 3002

Transcription:

161 Code Optimization Code Optimization 162 Two steps: 1. Analysis (to uncover optimization opportunities) 2. Optimizing transformation Optimization: must be semantically correct. shall improve program according to some criterion (program rarely optimal under any measure). Note: This demand sounds trivial, but it is not. It is difficult to show that an optimization always improves e.g. performance. Question: Should it be allowed to introduce new statements on some path? What about optimizations which may produce worse results for some inputs? Page 1

Taxonomy of Optimizations (Orthogonal) 163 Scope Local Basic bock: sequential execution, usual simple Example: common subexpression elimination Intraprocedural whole procedure: CFG classical data flow Interprocedural whole program: call graph Machine dependent/independent independent: e.g. dead code elimination, code motion dependent: e.g. latency hiding; managing bounded machine ressources like registers, functional units, caches. Taxonomy of Optimizations 164 Which kind of information? static properties of program runtime information: feedback directed optimization or profile guided optimization Time when optimizer executes compile time: classical» early optimizations constant folding, algebraic simplification» late optimizations peephole optimizations installation time: adaptive optimizations runtime: dynamic optimizations Page 2

Flow Analysis 165 Flow is a fundamental prerequisite for many important types of code improvement. Generally control flow precedes data flow. Control flow (CFA) represents flow of control usually in form of graphs. CFA constructs control flow graph call graph. Data flow (DFA) is the process of ascerting and collecting information prior to program execution about the possible modification, preservation, and use of certain entities (such as values or attributes of variables) in a computer program. Data Flow Analysis (Datenflussanalyse) 166 DFA solves data flow problems by propagating data flow information along the paths of a flow graph. Data flow information is the set of information which is relevant for a data flow problem. Data flow information is propagated through a CFG by setting up and solving data flow equations. (Global) DFA collects data flow information across jump boundaries (considerably more difficult than local ). DFA problems can be classified as forward problems: information propagated in the direction of the control flow backward problems: propagation in the opposite direction of the control flow. Page 3

Classification of Flow Analysis 167 Two orthogonal classifications of flow : Control flow Interprocedural Program Flow Intraprocedural Procedure Data flow Local Basic block Interprocedural optimizations usually require a call graph. Def.: In a call graph each node represents a procedure and an edge from one node to another indicates that one procedure may directly call another. Example of an Optimizer 168 Consider the following Figure: intermediate code generator code optimizer code generator control flow data flow transformations Page 4

Data Flow Problems 169 Reaching definitions. Determine the set of variable definitions that can reach a CFG node, i.e. the definition occurs at least on one path prior to that node (forward problem). Available expressions. Determine the set of expressions that are available at a CFG node, i.e. the expression is evaluated on all paths prior to that node (forward problem). Live/dead variables. Determine the set of variables that are live at a CFG node, i.e. the variable is used after control passes that node at least on one path; if the variable is not used, it is called dead (backward problem). Optimizations based on DFA 170 Common subexpression elimination (based on available expression information). If an expression occurs several times, compute it once and reuse the result. (Partial) redundancy elimination (PRE). Perform code motion to eliminate partially redundant code, i.e. code that is at least on one path redundant. (Partial) dead code elimination (PDE). Perform code motion to eliminate partially dead code, i.e. code that is at least on one path dead. Loop-invariant code motion. Move loop-invariant code out of loops (can be covered by combining PRE and PDE). Constant propagation. Determine the value of a variable, if it is a constant. Page 5