Goals of Program Optimization (1 of 2)

Similar documents
CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

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

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

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

Control-Flow Analysis

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

Compiler Construction 2016/2017 Loop Optimizations

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

Compiler Construction 2010/2011 Loop Optimizations

Compiler Optimization Techniques

Code Optimization. Code Optimization

Induction Variable Identification (cont)

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

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

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

Introduction to Machine-Independent Optimizations - 6

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

Tour of common optimizations

Example (cont.): Control Flow Graph. 2. Interval analysis (recursive) 3. Structural analysis (recursive)

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

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

Control Flow Graphs. (From Matthew S. Hetch. Flow Analysis of Computer Programs. North Holland ).

Intermediate representation

Statements or Basic Blocks (Maximal sequence of code with branching only allowed at end) Possible transfer of control

Intermediate Representation (IR)

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

Lecture 9: Loop Invariant Computation and Code Motion

Why Global Dataflow Analysis?

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

Lecture 3 Local Optimizations, Intro to SSA

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

Control Flow Analysis & Def-Use. Hwansoo Han

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

Lecture 2: Control Flow Analysis

CODE GENERATION Monday, May 31, 2010

Lecture Notes on Loop Optimizations

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Advanced Compiler Construction

Control Flow Analysis

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

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

CS 426 Fall Machine Problem 4. Machine Problem 4. CS 426 Compiler Construction Fall Semester 2017

EECS 583 Class 8 Classic Optimization

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

Compiler Optimization and Code Generation

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

Calvin Lin The University of Texas at Austin

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Introduction to Compilers

Control flow and loop detection. TDT4205 Lecture 29

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

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

Introduction to Optimization Local Value Numbering

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

Optimization Prof. James L. Frankel Harvard University

A Sparse Algorithm for Predicated Global Value Numbering

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

Code Merge. Flow Analysis. bookkeeping

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

Compiler Optimisation

Data Flow Analysis. Program Analysis

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

Control Flow Analysis

COP5621 Exam 4 - Spring 2005

CS 406/534 Compiler Construction Putting It All Together

CS553 Lecture Dynamic Optimizations 2

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

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

Topic 9: Control Flow

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

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

Introduction to Optimization. CS434 Compiler Construction Joel Jones Department of Computer Science University of Alabama

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

Computer Science 160 Translation of Programming Languages

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

More Code Generation and Optimization. Pat Morin COMP 3002

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

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

CFG (Control flow graph)

Languages and Compiler Design II IR Code Optimization

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

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

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

Using Static Single Assignment Form

ELEC 876: Software Reengineering

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

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

ICS 252 Introduction to Computer Design

Code generation and local optimization

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

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

Review; questions Basic Analyses (2) Assign (see Schedule for links)

Code generation and local optimization

Code Optimization Using Graph Mining

Intermediate Code Generation (ICG)

Module 14: Approaches to Control Flow Analysis Lecture 27: Algorithm and Interval. The Lecture Contains: Algorithm to Find Dominators.

Intermediate Code & Local Optimizations

Transcription:

Goals of Program Optimization (1 of 2) Goal: Improve program performance within some constraints Ask Three Key Questions for Every Optimization 1. Is it legal? 2. Is it profitable? 3. Is it compile-time cost justified? (1) Is it legal? Must preserve the semantics of the program It is sufficient to preserve externally observable results This is a language-dependent property E.g., exceptions in C vs. exceptions in Java May need even more flexibility Reordering floating point operations Topic 9: Optimization Basics p.1/17

Goals of Program Optimization (2 of 2) (2) Is it profitable? Improve performance of average or common case Limit negative impact in cases where performance is reduced Predicting performance impact is often non-trivial Choosing profitable optimization sequences is a major challenge (3) Is its compile-time cost justified? The list of possible optimizations is huge It is easy to go overboard: try everything Must be justified by performance gain E.g., whole-program (interprocedural) optimizations usually O4 Topic 9: Optimization Basics p.2/17

Classifying Optimizations (1 of 2) (1) By scope We can classify optimizations along 3 axes Local within a single basic block Peephole on a window of instructions Loop-level on one or more loops or loop nests Global for an entire procedure Interprocedural across multiple procedures or whole program (2) By machine information used Machine-independent uses no machine-specific information Machine-dependent otherwise Topic 9: Optimization Basics p.3/17

Classifying Optimizations (2 of 2) (3) By effect on structure of program: Algebraic transformations uses algebraic properties E.g., identities, commutativity, constant folding... Code simplification transformations simplify complex code sequences Control-flow simplification simplify branch structure Computation simplification replace expensive instructions with cheaper ones (e.g., constant propagation) Code elimination transformations eliminates unnecessary computations DCE, Unreachable code elimination Redundancy elimination transformations eliminate repetition Local or global CSE, LICM, Value Numbering, PRE Reordering transformations changes the order of computations Loop transformations change loop structure Code scheduling reorder machine instructions Topic 9: Optimization Basics p.4/17

Topics in Program Optimization 1. A catalog of local and peephole optimizations 2. Control flow graph and loop structure 3. Global dataflow analysis 2 example dataflow problems (a) reaching definitions (b) available expressions (c) live variables (d) def-use and use-def chains 4. Some key global optimizations Sparse Conditional Constant Propagation (SCCP) Loop-Invariant Code Motion (LICM) Global Common Subexpression Elimination (GCSE) focus on What, not How Topic 9: Optimization Basics p.5/17

Local and Peephole Optimizations (1 of 3) (1) Unreachable code elimination Code after an unconditional jump and with no branches to it Code in a branch never taken (Often eliminated during global constant propagation) (2) Flow-of-control optimizations If simplification: constant conditions, nested equivalent conditions Straightening: merge basic blocks that are always consecutive Branch folding: unconditional jump to unconditional jump conditional jump to unconditional jump unconditional jump to conditional jump Topic 9: Optimization Basics p.6/17

Local and Peephole Optimizations (2 of 3) (3) Algebraic simplifications exploit algebraic identities, commutativity,... x + 0, y 1, 18 z 14 (4) Redundant instruction elimination Redundant loads and stores: LD a R0 ST R0 a Usually caused by compiler-generated code Conditional branch always taken Usually caused by global constant propagation Topic 9: Optimization Basics p.7/17

Local and Peephole Optimizations (3 of 3) (5) Reduction in strength Replace x 2 by x x Replace 2 n x by x << n Replace x/4 by x 0.25 if integer x if real division (6) Machine idioms and Instruction Combining (Or could be done during Instruction Selection, e.g., with Burg) Multiply-Add instruction: r3 r3 + r1 r2 Auto-increment or auto-decrement addressing modes Conditional move instructions Predicated instructions See Section 18.1.1 in Muchnick s book for some strange idioms Topic 9: Optimization Basics p.8/17

Flow Graphs Definitions A fundamental representation for global optimizations. Flow Graph: A triple G=(N,A,s), where (N,A) is a (finite) directed graph, s N is a designated initial node, and there is a path from node s to every node n N. Entry node: A node with no predecessors. Exit node: A node with no successors. Properties There is a unique entry node, which must be s (Reachability assumption) Assumption is safe: can delete unreachable code Assumption may be conservative: some branches never taken. Control Flow Graphs are usually sparse. That is, A = O( N ). In fact, if only binary branching is allowed A 2 N. Topic 9: Optimization Basics p.9/17

Control Flow Graphs Definitions Review slides on Control Flow Graphs in the IR lecture CFG Construction : Read Section 8.4.1 of Aho et al. for the algorithm to partition a procedure into basic blocks. This is required material. Topic 9: Optimization Basics p.10/17

Dominance in Flow Graphs Let d, d 1, d 2, d 3, n be nodes in G. Definitions d dominates n (write d dom n ) iff every path in G from s to n contains d. d properly dominates n if d dominates n and d n. Properties s dom d, nodes d in G. Partial Ordering: The dominance relation of a flow graph G is a partial ordering: Reflexive : n dom n is true n. Antisymmetric : If d dom n, then n dom d cannot hold. Transitive : d 1 dom d 2 d 2 dom d 3 = d 1 dom d3 Topic 9: Optimization Basics p.11/17

Immediate Dominance More Properties The dominators of a node form a chain: If d 1 dom n and d 2 dom n and d 1 d 2, then: it must hold that d 1 dom d 2 or d 2 dom d 1. = Last dominator for every node n s (on any path) is unique Definition d is the immediate dominator of n (write d idom n ) if d is the last dominator on any path from initial node to n, d n Topic 9: Optimization Basics p.12/17

Dominator Tree Drawing Dominators as A Graph Given a flow graph G, construct a new graph with the same nodes as G, and an edge n 1 n 2 iff n 1 dom n 2. Q. What is the shape of this graph? Given a flow graph G, construct a new graph with the same nodes as G, and an edge n 1 n 2 iff n 1 idom n 2. Q. What is the shape of this graph? Definition The latter graph defined above is called the Dominator Tree. Properties The root of the Dominator Tree is s. Every node except s has exactly one immediate dominator its parent in the Dominator Tree Topic 9: Optimization Basics p.13/17

Defining loops in Flow Graphs Naive definitions of a loop in a control flow graph A cycle identifies a loop? A strongly connected component (SCC) identifies a loop? Why Defining Loops is Challenging Easy case: Structured nested loops: FOR or WHILE Harder case: Arbitrary flow and exits in loop body, but unique loop entry Hardest case: No unique loop entry ( irreducible loops ) Topic 9: Optimization Basics p.14/17

Defining Loops Idea: Use dominance to define Natural Loops Back Edge : An edge n d where d dom n Natural Loop : Given a back edge, n d, the natural loop corresponding to n d is the set of nodes {d + all nodes that can reach n without going through d} Loop Header: A node d that dominates all nodes in the loop Header is unique for each natural loop d is the unique entry point into the loop Uniqueness is very useful for many optimizations Why? Topic 9: Optimization Basics p.15/17

Preheader: An optimization convenience The Idea If a loop has multiple incoming edges to the header, moving code out of the loop safely is complicated Preheader gives a safe place to move code before a loop The Transformation Introduce a pre-header p for each loop (let loop header be d): 1. Insert node p with one out edge: p d 2. All edges that previously entered d should now enter p Topic 9: Optimization Basics p.16/17

Reducible and Irreducible Flow Graphs Reducible flow graph: A flow graph G is called reducible iff we can partition the edges into 2 sets: 1. forward edges: should form a DAG in which every node is reachable from initial node 2. other edges must be back edges: i.e., only those edges n d where d dom n Otherwise graph is called irreducible. Idea: Every cycle has at least one back edge All cycles in a reducible graph are natural loops Not true in an irreducible graph! Topic 9: Optimization Basics p.17/17