170 likes | 438 Vues
Early Program Optimizations Chapter 12. Mooly Sagiv. Outline. Constant Folding Scalar Replacement of Aggregates Algebraic Simplification and Reassociation Value Numbering Basic blocks Procedure based data flow analysis Sparse conditional constant propagation. Constant Folding.
E N D
Early Program Optimizations Chapter 12 Mooly Sagiv
Outline • Constant Folding • Scalar Replacement of Aggregates • Algebraic Simplification and Reassociation • Value Numbering • Basic blocks • Procedure based data flow analysis • Sparse conditional constant propagation
Constant Folding • Evaluate expressions with constant value inside basic blocks • Invoked at all levels • Computations need to be conducted according to target machine • Need to take care of overflow exceptions • Very problematic in floating point arithmetic
Scalar replacement of aggregates • Replace structure elements by scalars • Very simple but not common • Depends on aliasingMakes other optimizations applicable
typedef enum { APPLE, BANANA, ORANGE} VARIETY; typedef enum { LONG, ROUND} SHAPE; typedef struct fruit { VARIETY variety; SHAPE shape ; } FRUIT;
char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; char * color(FRUIT CurrentFruit); { switch (currentFruit->variety) { case APPLE: return Red; break; case BANANA: return Yellow; break; case ORANGE: return Orange; }} main() { FRUIT snack; snack.variety = APPLE; snack.shape = ROUND; printf(“%s\n”, color(&snack));} char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);}
char * Red = “red”; char * Yellow = “yellow”; char * Orange = “orange”; main() { FRUIT snack; VARIETY t1; SHAPE t2; COLOR t3; t1 = APPLE; t2 = ROUND; switch (t1) { case APPLE: t3= Red; break; case BANANA: t3=Yellow; break; case ORANGE: t3=Orange; }} printf(“%s\n”, t3);} main() { printf(“%s\n”, “red”);}
Algebraic Simplification and Reassociations • Use algebraic properties of operators in basic block • Invoked at all levels • Convert (HIR, MIR, LIR) in a basic block into a tree rewrite trees according to rules (in an efficient way) • Mainly for integer and address arithmetic
Strength Reduction in Operators ti shl 2 t t +i ti *5 ti shl 3 t t - i ti *7
Algebraic Simplifications of Addressing Expressions • Cannot raise overflow • Important for array references • Use canonizations • Integrate with other optimizations
var a:array[lo1..hi1, lo2..hi2] of eltype; i, j: integer; ... for j :=lo2 to hi2 do a[i,j] := b+a[i, j]; end
Algebraic Simplifications of Floating Point Expressions • Almost always impossible • Removal of type coercions can be done eps:=1.0 ; while eps+1.0>1.0 do oldeps := eps; eps := 0.5*eps; od
Summary • Algebraic association and constant folding are useful • Compiler need to take into account the exact semantics • Example: Floating point • IEEE Standards, C, Fortran77, Ada, Java