Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Advanced Compiler Design Early Optimizations. Introduction Constant expression evaluation (constant folding)  dataflow independent Scalar replacement."— Presentation transcript:

1 Advanced Compiler Design Early Optimizations

2 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

3 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

4 Introduction Lexical Analyzer Parser Semantic Analyzer Translator Optimizer Final Assembly 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 modelMixed level model

5 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.

6 Introduction A) Scalar replacement of array references Data-cache optimizations B) Procedure integration Tail-call optimization Scalar replecement of aggregates Sparse conditional constant propagation Interprocedural constant propagation Procedure specialization and cloning Sparse conditional constant propagation C1) Global value numbering Local and global copy propagation Sparse conditional constant propagation Constant folding C2 C3 Algebraic simplification C4DE

7 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

8 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

9 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

10 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

11 Scalar replacement of aggregates Example 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; … }

12 Scalar replacement of aggregates Should be performed on very early on in the optimization process

13 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.

14 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.

15 Algebraic simplifications and reassociations Examples i+0=0+1=i-0=i 0-i=-i i*1=1*i=i/1=i -(-i)=i i+(-j)=i-j b v true = true v b = true b v false = false v b = b

16 Algebraic simplifications and reassociations Examples i^2 = i*i, 2*i = i+i i*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

17 Algebraic simplifications and reassociations Examples j=0, k=1*j, i=i+k*1 j=0, k=0, i=i

18 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)

19 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

20 Algebraic reassociations of Floating point Expression Can be applied only in rare cases

21 Algebraic reassociations Applicability Integers  Overflows, exceptions Adressing Expression  Allways Floating point Expression  In rare cases  Overflows, exceptions

22 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

23 Value numbering Examples 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

24 Value numbering Algorithms Basic principle:  Associates a value to each computation  Any two computations with same value always computes the same result Basic block  Also extended basic blocks Global form (Alpern, Wegman and Zadeck)  Requires that the procedure is in the SSA form

25 Value numbering Global form Two variables are congruent:  If the defining statement has identical operators  Operands are congruent

26 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)

27 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

28 Copy propagation Example b := a c := 4*b c > b d:=b+2 e:=a + b NY b := a c := 4*a c > a d:=a+2 e:=a + a NY Copy propagation result

29 Copy propagation There is O(n) algorithm to solve the copy propagation Input: MIR instructions Block[m][1],…,Block[m][n]

30 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

31 Sparse conditional constant propagation Example b := 3 c := 4*b c > b d:=b+2 e:=a + b NY b := 3 c := 4*3 c > 3 d:=3+2 e:=a + 3 NY Constant propagation result

32 Sparse conditional constant propagation Algorithms  Wegman and Zadeck  SSA form (more efficient) and without SSA form  O(n)


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

Similar presentations


Ads by Google