Introduction to Machine-Independent Optimizations - 6

Similar documents
The Static Single Assignment Form:

Induction Variable Identification (cont)

Introduction to Machine-Independent Optimizations - 1

Using Static Single Assignment Form

Compiler Construction 2009/2010 SSA Static Single Assignment Form

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

Sparse Conditional Constant Propagation

Sparse Conditional Constant Propagation

Sparse Conditional Constant Propagation

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

Calvin Lin The University of Texas at Austin

Introduction to Machine-Independent Optimizations - 4

Goals of Program Optimization (1 of 2)

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

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

Lecture 3 Local Optimizations, Intro to SSA

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

Intermediate representation

Constant Propagation with Conditional Branches

Loop Unrolling. Source: for i := 1 to 100 by 1 A[i] := A[i] + B[i]; endfor

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

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Static Single Assignment (SSA) Form

CS153: Compilers Lecture 23: Static Single Assignment Form

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

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

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

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Computing Static Single Assignment (SSA) Form. Control Flow Analysis. Overview. Last Time. Constant propagation Dominator relationships

Live Variable Analysis. Work List Iterative Algorithm Rehashed

Data-flow Analysis - Part 2

Topic 9: Control Flow

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

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

A Propagation Engine for GCC

Lecture 9: Loop Invariant Computation and Code Motion

Data Flow Analysis. CSCE Lecture 9-02/15/2018

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

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

Compiler Optimisation

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

Sta$c Single Assignment (SSA) Form

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

EECS 583 Class 8 Classic Optimization

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

Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc.

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

Control Flow Analysis & Def-Use. Hwansoo Han

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

Lecture Notes on Loop Optimizations

SSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

Tour of common optimizations

Compiler Construction 2010/2011 Loop Optimizations

Lecture 23 CIS 341: COMPILERS

Conditional Elimination through Code Duplication

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

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

A Practical and Fast Iterative Algorithm for φ-function Computation Using DJ Graphs

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

Example. Example. Constant Propagation in SSA

Combining Analyses, Combining Optimizations - Summary

Compiler Optimization and Code Generation

Control-Flow Analysis

Control flow and loop detection. TDT4205 Lecture 29

A Sparse Algorithm for Predicated Global Value Numbering

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

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

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

Dataflow analysis (ctd.)

Program analysis for determining opportunities for optimization: 2. analysis: dataow Organization 1. What kind of optimizations are useful? lattice al

Why Global Dataflow Analysis?

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

Single-Pass Generation of Static Single Assignment Form for Structured Languages

More Dataflow Analysis

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

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

register allocation saves energy register allocation reduces memory accesses.

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

Compiler Construction 2016/2017 Loop Optimizations

Lecture 8: Induction Variable Optimizations

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Towards an SSA based compiler back-end: some interesting properties of SSA and its extensions

Outline. Register Allocation. Issues. Storing values between defs and uses. Issues. Issues P3 / 2006

Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA

Code Placement, Code Motion

Data Flow Analysis and Computation of SSA

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

Intermediate Representations Part II

Constant Propagation

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

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

CIS 341 Final Examination 3 May 2011

Lecture 4 Introduction to Data Flow Analysis

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

Intermediate representations IR #1: CPS/L 3. Code example. Advanced Compiler Construction Michel Schinz

Run-time Environments - 2

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

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Transcription:

Introduction to Machine-Independent Optimizations - 6 Machine-Independent Optimization Algorithms Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design Optimization Algorithms

Outline of the Lecture What is code optimization? (in part 1) Illustrations of code optimizations (in part 1) Examples of data-flow analysis (in parts 2,3, and 4) Fundamentals of control-flow analysis (in parts 4 and 5) Algorithms for machine-independent optimizations SSA form and optimizations Optimization Algorithms

Detection of Loop-invariant Computations Mark as invariant, those statements whose operands are all either constant or have all their reaching definitions outside L Repeat { Mark as invariant all those statements not previously so marked all of whose operands are constants, or have all their reaching definitions outside L, or have exactly one reaching definition, and that definition is a statement in L marked invariant } until no new statements are marked invariant Optimization Algorithms

Loop Invariant Code motion Example Optimization Algorithms

Loop-Invariant Code Motion Algorithm 1 Find loop-invariant statements 2 For each statement s defining x found in step (1), check that (a) it is in a block that dominates all exits of L (b) x is not defined elsewhere in L (c) all uses in L of x can only be reached by the definition of x in s 3 Move each statement s found in step (1) and satisfying conditions of step (2) to a newly created preheader provided any operands of s that are defined in loop L have previously had their definition statements moved to the preheader Optimization Algorithms

Code Motion - Violation of condition 2(a)-1 Optimization Algorithms

Code Motion - Violation of condition 2(a)-2 Optimization Algorithms

Violation of condition 2(a) - Running Example Optimization Algorithms

Code Motion - Violation of condition 2(b) Optimization Algorithms

Violation of condition 2(b) - Running Example Optimization Algorithms

Code Motion - Violation of condition 2(c) Optimization Algorithms

Violation of condition 2(c) - Running Example Optimization Algorithms

The Static Single Assignment Form: Application to Program Optimizations Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler Design

Outline of the Lecture SSA form - definition and examples Optimizations with SSA forms Dead-code elimination Simple constant propagation Copy propagation Conditional constant propagation and constant folding

The SSA Form: Introduction A new intermediate representation Incorporates def-use information Every variable has exactly one definition in the program text This does not mean that there are no loops This is a static single assignment form, and not a dynamic single assignment form Some compiler optimizations perform better on SSA forms Conditional constant propagation and global value numbering are faster and more effective on SSA forms A sparse intermediate representation If a variable has N uses and M definitions, then def-use chains need space and time proportional to N.M But, the corresponding instructions of uses and definitions are only N + M in number SSA form, for most realistic programs, is linear in the size of the original program

A Program in non-ssa Form and its SSA Form

SSA Form: A Definition A program is in SSA form, if each use of a variable is reached by exactly one definition Flow of control remains the same as in the non-ssa form A special merge operator, φ, is used for selection of values in join nodes Not every join node needs a φ operator for every variable No need for a φ operator, if the same definition of the variable reaches the join node along all incoming edges Often, an SSA form is augmented with u-d and d-u chains to facilitate design of faster algorithms Translation from SSA to machine code introduces copy operations, which may introduce some inefficiency

Program 2 in non-ssa and SSA Form

Program 3 in non-ssa and SSA Form

Optimization Algorithms with SSA Forms Dead-code elimination Very simple, since there is exactly one definition reaching each use Examine the du-chain of each variable to see if its use list is empty Remove such variables and their definition statements If a statement such as x = y + z (or x = φ(y 1, y 2 )) is deleted, care must be taken to remove the deleted statement from the du-chains of y and z (or y 1 and y 2 ) Simple constant propagation Copy propagation Conditional constant propagation and constant folding Global value numbering

Simple Constant Propagation { Stmtpile = {S S is a statement in the program} while Stmtpile is not empty { S = remove(stmtpile); if S is of the form x = φ(c, c,..., c) for some constant c replace S by x = c if S is of the form x = c for some constant c delete S from the program for all statements T in the du-chain of x do substitute c for x in T; simplify T Stmtpile = Stmtpile {T} } Copy propagation is similar to constant propagation A single-argument φ-function, x = φ(y), or a copy statement, x = y can be deleted and y substituted for every use of x

Conditional Constant Propagation - 1 SSA forms along with extra edges corresponding to d-u information are used here Edge from every definition to each of its uses in the SSA form (called henceforth as SSA edges) Uses both flow graph and SSA edges and maintains two different work-lists, one for each (Flowpile and SSApile, resp.) Flow graph edges are used to keep track of reachable code and SSA edges help in propagation of values Flow graph edges are added to Flowpile, whenever a branch node is symbolically executed or whenever an assignment node has a single successor

Conditional Constant Propagation - 2 SSA edges coming out of a node are added to the SSA work-list whenever there is a change in the value of the assigned variable at the node This ensures that all uses of a definition are processed whenever a definition changes its lattice value. This algorithm needs much lesser storage compared to its non-ssa counterpart Conditional expressions at branch nodes are evaluated and depending on the value, either one of outgoing edges (corresponding to true or false) or both edges (corresponding to ) are added to the worklist However, at any join node, the meet operation considers only those predecessors which are marked executable.

CCP Algorithm - Example - 1

CCP Algorithm - Example 1 - Trace 1

CCP Algorithm - Example 1 - Trace 2

CCP Algorithm - Example 1 - Trace 3

CCP Algorithm - Example 2