Top Banner
Advanced Compiler Design Early Optimizations
32

Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding) dataflow independent Scalar replacement.

Jan 03, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Advanced Compiler Design

Early Optimizations

Page 2: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 3: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 4: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 5: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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.

Page 6: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 7: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 8: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 9: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 10: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 11: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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;

}

Page 12: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Scalar replacement of aggregates

Should be performed on very early on in the optimization process

Page 13: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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.

Page 14: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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.

Page 15: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 16: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 17: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Algebraic simplifications and reassociationsExamples

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

Page 18: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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)

Page 19: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 20: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Algebraic reassociations of Floating point Expression

Can be applied only in rare cases

Page 21: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Algebraic reassociationsApplicability

Integers Overflows, exceptions

Adressing Expression Allways

Floating point Expression In rare cases Overflows, exceptions

Page 22: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 23: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 24: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 25: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Value numberingGlobal form

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

Page 26: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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)

Page 27: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 28: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 29: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Copy propagation

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

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

Page 30: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 31: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

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

Page 32: Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement.

Sparse conditional constant propagation

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