Title: Early Program Optimizations Chapter 12
1Early Program Optimizations Chapter 12
2Outline
- Constant Folding
- Scalar Replacement of Aggregates
- Algebraic Simplification and Reassociation
- Value Numbering
- Basic blocks
- Procedure based data flow analysis
- Sparse conditional constant propagation
3Constant 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
4Scalar replacement of aggregates
- Replace structure elements by scalars
- Very simple but not common
- Depends on aliasingMakes other optimizations
applicable
5typedef 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-gtvariety) 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 t3Yellow
break case
ORANGE t3Orange 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 t3Yellow
break case
ORANGE t3Orange printf(s\n, t3)
main() printf(s\n, red)
8Algebraic 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
90/1/false/true Simplification
10Simplifying Unary Operators
11Simplifying Shift Operators
12Strength Reduction in Operators
t?i shl 2 t ? t i
t?i 5
t?i shl 3 t ? t - i
t?i 7
13Commutativity and Associativity
14Algebraic Simplifications of Addressing
Expressions
- Cannot raise overflow
- Important for array references
- Use canonizations
- Integrate with other optimizations
15var aarraylo1..hi1, lo2..hi2 of eltype
i, j integer ... for j lo2 to
hi2 do ai,j bai, j end
16Algebraic Simplifications of Floating Point
Expressions
- Almost always impossible
- Removal of type coercions can be done
eps1.0 while eps1.0gt1.0 do oldeps
eps eps 0.5eps od
17Summary
- 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