Download presentation
Presentation is loading. Please wait.
1
Early Program Optimizations Chapter 12 Mooly Sagiv
2
Outline Constant Folding Scalar Replacement of Aggregates Algebraic Simplification and Reassociation Value Numbering –Basic blocks –Procedure based data flow analysis Sparse conditional constant propagation
3
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
4
Scalar replacement of aggregates Replace structure elements by scalars Very simple but not common Depends on aliasing Makes other optimizations applicable
5
typedef enum { APPLE, BANANA, ORANGE} VARIETY; typedef enum { LONG, ROUND} SHAPE; typedef struct fruit { VARIETY variety; SHAPE shape ; } FRUIT;
6
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);}
7
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”);}
8
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
9
0/1/false/true Simplification
10
Simplifying Unary Operators
11
Simplifying Shift Operators
12
Strength Reduction in Operators t i *5 t i shl 2 t t +i t i *7 t i shl 3 t t - i
13
Commutativity and Associativity
14
Algebraic Simplifications of Addressing Expressions Cannot raise overflow Important for array references Use canonizations Integrate with other optimizations
15
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
16
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
17
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.