Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant...

32
Advanced Compiler Design Early Optimizations

Transcript of Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant...

Advanced Compiler Design

Early Optimizations

Introduction

Constant expression evaluation (constant folding) dataflow independent

Scalar replacement of aggregates dataflow independent

Algebraic simplifications and reassociations dataflow independent

Value numbering dataflow dependent

Copy propagation dataflow dependent

Sparse conditional constant propagation dataflow dependent

Introduction

Usually performed at early phases in the optimization process

Many of these optimizations belong to the group of the most important techniques: Constant folding Algebraic simplifications and reassociations Global value numbering Sparse conditional constant propagation

Introduction

Lexical Analyzer

Parser

Semantic Analyzer

Translator

Optimizer

Final Assembly

Low level intermediate code

Low level intermediate code

Lexical Analyzer

Parser

Semantic Analyzer

Intermediate code generator

Optimizer

Postpass optimizer

Medium level intermediate code

code generator

Medium level intermediate code

Low level model Mixed level model

Introduction

A) Optimizations performed to source code or high-level intermediate code

B-C) Medium or low-level intermediate code (depending on the model: mixed or low-level)

D) Always on low-level, machine depemdent

E) link time, operate on relocatable object code.

IntroductionA) Scalar replacement of array referencesData-cache optimizations

B) Procedure integrationTail-call optimizationScalar replecement of aggregatesSparse conditional constant propagationInterprocedural constant propagationProcedure specialization and cloningSparse conditional constant propagation

C1) Global value numberingLocal and global copy propagationSparse conditional constant propagation

Constant folding

C2

C3

Algebraic simplification

C4 D E

Constant Expression Evaluation

Compile time evaluation of those expressions, whose operands are known to be constant.

Three phases: Determine that the operands are constant Calculate the value of the expression Replace the expression using the calculated

value

Constant Expression Evaluation

Best structured as a subroutine that can be invoked from anywhere in the optimizer

Operations and datatypes used in the evalutation must match those in the target architecture

The effectiveness of constant expression evaluation can be increased by constant propagation

Constant Expression Evaluation

Applicability Integers:

Almost allways applicable Exception: division by zero, overflows

Addressing Arithmetic No problems

Floating point values: Many exceptions, compiler’s arithmetic must match

to the processor

Scalar replacement of aggregates

Makes other optimizations applicable to the components of aggregates

Principle: Determine which aggregate components have

simple scalar values and are not aliased Assign them to tempories, which values match

those in the aggregates

Scalar replacement of aggregatesExample

typedef enum {APPLE, BANANA, ORANGE} VARIETY;

typedef enum {LONG, ROUND} SHAPE;

typedef struct fruit {

VARIETY variety;

SHAPE shape; } FRUIT

Main() {

FRUIT snack;

snack.variety = APPLE;

snack.shape = ROUND;

}

Main() {

VARIETY t1 = APPLE;

SHAPE t2 = ROUND;

}

Scalar replacement of aggregates

Should be performed on very early on in the optimization process

Algebraic simplifications and reassociations

Algebraic simplifications Algebraic properties of the operators are used

to simplify the expressions

Algebraic reassociations Uses specific algebraic properties:

Associativity, commutativity, and distributivity Divides an expression into parts that are

constant, loop-invariant, variable.

Algebraic simplifications and reassociations

Best structured as a subroutine that can be invoked from anywhere in the optimizer

Used from many different phases in the whole optimization process.

Algebraic simplifications and reassociationsExamples

i+0=0+1=i-0=i0-i=-ii*1=1*i=i/1=i-(-i)=ii+(-j)=i-jb v true = true v b = trueb v false = false v b = b

Algebraic simplifications and reassociationsExamples

i^2 = i*i, 2*i = i+ii*5

t:=i shl 2 (shl = shift left) t:=t+i

i*7 t:=i shl 3 t:=t-i

(i-j) + (i-j) + (i-j) + (i-j) = 4*i – 4*j

Algebraic simplifications and reassociationsExamples

j=0, k=1*j, i=i+k*1j=0, k=0, i=i

Algebraic simplifications and reassociations of Adressing Expression

Overflows makes no differences in address computations

The general strategy is canonicalization. Example:var a: array[lo1..hi1, lo2..hi2] of eltype

var i,j: integer

do j=lo2 to hi2 begin

a[i,j] := b + a[i,j]

end a[i,j]: base_a + ((i-lo1)*(hi2-lo2+1)+j-lo2)*w W = sizeof(eltype)

Algebraic reassociations of Adressing Expression

a[i,j]: -(lo1*(hi2-lo2+1)-lo2)*w+base_a

compiletime constant + (hi2-lo2+1)*i*w

loop invariant +j*w

variable

Algebraic reassociations of Floating point Expression

Can be applied only in rare cases

Algebraic reassociationsApplicability

Integers Overflows, exceptions

Adressing Expression Allways

Floating point Expression In rare cases Overflows, exceptions

Value numbering

Determines that two computations are equivalent and eliminates one of them.

Similar optimizations: Sparse conditional constant propagation Common-subexpression evaluation Partial-redundancy evaluation

Value numberingExamples

read(i)

j := i+1

k := i

l := k+1

Value numbering:

read(i)

t := i+1

j := t

k := i

l := t

i:= 2

j:= i*2

k:= i+2

Copy propagation:

i:= 2

j:= 2*2

k:= 2+2

Value numberingAlgorithms

Basic principle: Associates a value to each computation Any two computations with same value always

computes the same resultBasic block

Also extended basic blocksGlobal form (Alpern, Wegman and

Zadeck) Requires that the procedure is in the SSA form

Value numberingGlobal form

Two variables are congruent: If the defining statement has identical operators Operands are congruent

Copy propagation

Transformation that, if we have assignment x := y, replace all the later uses of x with y,

as long as the intervening instructions have not changed the value of x

Similar optimizations Register coalescing (16.3)

Copy propagation

Can be divided into a local and global phases Local phase: operating within individual basic

block Global phase: operating across the whole

flowgraph

Copy propagationExample

b := ac := 4*bc > b

d:=b+2

e:=a + b

NY

b := ac := 4*ac > a

d:=a+2

e:=a + a

NY

Copy propagation result

Copy propagation

There is O(n) algorithm to solve the copy propagation

Input: MIR instructions Block[m][1],…,Block[m][n]

Sparse conditional constant propagation

Constant propagation is a transformation that if we have assignment x := constant replace all the later uses of x with constant,

as long as the intervening instructions have not changed the value of x

Particular important in RISC architectures

Sparse conditional constant propagationExample

b := 3c := 4*bc > b

d:=b+2

e:=a + b

NY

b := 3c := 4*3c > 3

d:=3+2

e:=a + 3

NY

Constant propagation result

Sparse conditional constant propagation

Algorithms Wegman and Zadeck SSA form (more efficient) and without SSA form O(n)