introduction to machine-independent optimizations - 6 ...ramakrishna/compilers-aug14/slides/36-intro...

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

Upload: others

Post on 08-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Introduction toMachine-Independent Optimizations - 6

Machine-Independent Optimization Algorithms

Y.N. Srikant

Department of Computer Science and AutomationIndian Institute of Science

Bangalore 560 012

NPTEL Course on Principles of Compiler Design

Y.N. Srikant Optimization Algorithms

Page 2: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

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 optimizationsSSA form and optimizations

Y.N. Srikant Optimization Algorithms

Page 3: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Detection of Loop-invariant Computations

Mark as “invariant”, those statements whose operands are alleither constant or have all their reaching definitions outside L

Repeat {Mark as “invariant” all those statements not previouslyso marked all of whose operands are constants, or have alltheir reaching definitions outside L, or have exactlyone reaching definition, and that definition is a statementin L marked “invariant”

} until no new statements are marked “invariant”

Y.N. Srikant Optimization Algorithms

Page 4: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Loop Invariant Code motion Example

Y.N. Srikant Optimization Algorithms

Page 5: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Loop-Invariant Code Motion Algorithm

1 Find loop-invariant statements2 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 s3 Move each statement s found in step (1) and satisfying

conditions of step (2) to a newly created preheaderprovided any operands of s that are defined in loop L havepreviously had their definition statements moved to thepreheader

Y.N. Srikant Optimization Algorithms

Page 6: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

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

Y.N. Srikant Optimization Algorithms

Page 7: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

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

Y.N. Srikant Optimization Algorithms

Page 8: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Violation of condition 2(a) - Running Example

Y.N. Srikant Optimization Algorithms

Page 9: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Code Motion - Violation of condition 2(b)

Y.N. Srikant Optimization Algorithms

Page 10: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Violation of condition 2(b) - Running Example

Y.N. Srikant Optimization Algorithms

Page 11: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Code Motion - Violation of condition 2(c)

Y.N. Srikant Optimization Algorithms

Page 12: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Violation of condition 2(c) - Running Example

Y.N. Srikant Optimization Algorithms

Page 13: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

The Static Single Assignment Form:Application to Program Optimizations

Y.N. Srikant

Department of Computer Science and AutomationIndian Institute of Science

Bangalore 560 012

NPTEL Course on Principles of Compiler Design

Y.N. Srikant Program Optimizations and the SSA Form

Page 14: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Outline of the Lecture

SSA form - definition and examplesOptimizations with SSA forms

Dead-code eliminationSimple constant propagationCopy propagationConditional constant propagation and constant folding

Y.N. Srikant Program Optimizations and the SSA Form

Page 15: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

The SSA Form: Introduction

A new intermediate representationIncorporates def-use informationEvery variable has exactly one definition in the programtext

This does not mean that there are no loopsThis is a static single assignment form, and not a dynamicsingle assignment form

Some compiler optimizations perform better on SSA formsConditional constant propagation and global valuenumbering are faster and more effective on SSA forms

A sparse intermediate representationIf a variable has N uses and M definitions, then def-usechains need space and time proportional to N.MBut, the corresponding instructions of uses and definitionsare only N + M in numberSSA form, for most realistic programs, is linear in the size ofthe original program

Y.N. Srikant Program Optimizations and the SSA Form

Page 16: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

A Program in non-SSA Form and its SSA Form

Y.N. Srikant Program Optimizations and the SSA Form

Page 17: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

SSA Form: A Definition

A program is in SSA form, if each use of a variable isreached by exactly one definitionFlow of control remains the same as in the non-SSA formA special merge operator, φ, is used for selection of valuesin join nodesNot every join node needs a φ operator for every variableNo need for a φ operator, if the same definition of thevariable reaches the join node along all incoming edgesOften, an SSA form is augmented with u-d and d-u chainsto facilitate design of faster algorithmsTranslation from SSA to machine code introduces copyoperations, which may introduce some inefficiency

Y.N. Srikant Program Optimizations and the SSA Form

Page 18: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Program 2 in non-SSA and SSA Form

Y.N. Srikant Program Optimizations and the SSA Form

Page 19: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Program 3 in non-SSA and SSA Form

Y.N. Srikant Program Optimizations and the SSA Form

Page 20: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Optimization Algorithms with SSA Forms

Dead-code eliminationVery simple, since there is exactly one definition reachingeach useExamine the du-chain of each variable to see if its use list isemptyRemove such variables and their definition statementsIf a statement such as x = y + z (or x = φ(y1, y2)) isdeleted, care must be taken to remove the deletedstatement from the du-chains of y and z (or y1 and y2)

Simple constant propagationCopy propagationConditional constant propagation and constant foldingGlobal value numbering

Y.N. Srikant Program Optimizations and the SSA Form

Page 21: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

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 = cif S is of the form x = c for some constant c

delete S from the programfor all statements T in the du-chain of x do

substitute c for x in T; simplify TStmtpile = Stmtpile ∪ {T}

}

Copy propagation is similar to constant propagationA single-argument φ-function, x = φ(y), or a copystatement, x = y can be deleted and y substituted forevery use of x

Y.N. Srikant Program Optimizations and the SSA Form

Page 22: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Conditional Constant Propagation - 1

SSA forms along with extra edges corresponding to d-uinformation are used here

Edge from every definition to each of its uses in the SSAform (called henceforth as SSA edges)

Uses both flow graph and SSA edges and maintains twodifferent work-lists, one for each (Flowpile and SSApile ,resp.)Flow graph edges are used to keep track of reachablecode and SSA edges help in propagation of valuesFlow graph edges are added to Flowpile, whenever abranch node is symbolically executed or whenever anassignment node has a single successor

Y.N. Srikant Program Optimizations and the SSA Form

Page 23: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

Conditional Constant Propagation - 2

SSA edges coming out of a node are added to the SSAwork-list whenever there is a change in the value of theassigned variable at the nodeThis ensures that all uses of a definition are processedwhenever a definition changes its lattice value.This algorithm needs much lesser storage compared to itsnon-SSA counterpartConditional expressions at branch nodes are evaluatedand depending on the value, either one of outgoing edges(corresponding to true or false) or both edges(corresponding to ⊥) are added to the worklistHowever, at any join node, the meet operation considersonly those predecessors which are marked executable.

Y.N. Srikant Program Optimizations and the SSA Form

Page 24: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

CCP Algorithm - Example - 1

Y.N. Srikant Program Optimizations and the SSA Form

Page 25: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

CCP Algorithm - Example 1 - Trace 1

Y.N. Srikant Program Optimizations and the SSA Form

Page 26: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

CCP Algorithm - Example 1 - Trace 2

Y.N. Srikant Program Optimizations and the SSA Form

Page 27: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

CCP Algorithm - Example 1 - Trace 3

Y.N. Srikant Program Optimizations and the SSA Form

Page 28: Introduction to Machine-Independent Optimizations - 6 ...ramakrishna/Compilers-Aug14/slides/36-intro … · Introduction to Machine-Independent Optimizations - 6 Machine-Independent

CCP Algorithm - Example 2

Y.N. Srikant Program Optimizations and the SSA Form