Code Optimization
Code Optimization
Idea
Transform the program to improve efficiency Performance: faster execution Size: smaller executable, smaller memory footprint
Criteria
Criterion of code optimization
Must preserve the semantic equivalence of the programs The algorithm should not be modified Transformation, on average should speed up the execution of the program Worth the effort: Intellectual and compilation effort spend on insignificant improvement.
Transformations are simple enough to have a good effect
Where ?
Optimization can be done in almost all phases of compilation.
Source code
Front end
Inter. code
Code generator
target code
Code Optimizer
Organization of an optimizing compiler
Transformation
Code optimizer
Loop optimization
Machine Architecture
Cache Size and type Cache/Memory transfer rate
Less jumps: jumps interfere with code pre-fetch Code locality: codes executed close together in time is
generated close together in memory increase locality of reference Extract more information about code: More info better code generation
Redundancy elimination
Redundancy elimination = determining that two computations are equivalent and eliminating one. There are several types of redundancy elimination:
Value numbering
Associates symbolic values to computations and identifies expressions that have the same value
Constant/Copy propagation
Identifies variables that have constant/copy values and uses the constants/copies in place of the variables.
Optimizing Transformations
Compile time evaluation Common sub-expression elimination Code motion Strength Reduction Dead code elimination Copy propagation Loop optimization
Induction variables and strength reduction
10
Compile-Time Evaluation
Expressions whose values can be precomputed at the compilation time Two ways:
Constant folding Constant propagation
11
Compile-Time Evaluation
Constant folding: Evaluation of an expression with constant operands to replace the expression with single value
Example:
area := (22.0/7.0) * r ** 2 area := 3.14286 * r ** 2
12
Compile-Time Evaluation
Constant Propagation: Replace a variable with constant which has been assigned to it earlier. Example:
pi := 3.14286
area = pi * r ** 2 area = 3.14286 * r ** 2
13
Constant Propagation
What does it mean?
Given an assignment x = c, where c is a constant, replace later uses of x with uses of c, provided there are no intervening assignments to x.
Similar to copy propagation Extra feature: It can analyze constant-value conditionals to determine whether a branch should be executed or not.
When is it performed?
Early in the optimization process.
14
15
16
18
a:= b
z : = a + b + 10
19
Code Motion
Moving code from one part of the program to other without modifying the algorithm
Reduce size of the program Reduce execution frequency of the code subjected to movement
20
Code Motion
1. Code Space reduction: Similar to common subexpression elimination but with the objective to reduce code size.
Example: Code hoisting
if (a< b) then z := x ** 2 else y := x ** 2 + 10
temp : = x ** 2 if (a< b) then z := temp else y := temp + 10
x ** 2 is computed once in both cases, but the code size in the second case reduces.
21
Code Motion
2 Execution frequency reduction: reduce execution frequency of partially available expressions (expressions available atleast in one path)
22
Code Motion
Move expression out of a loop if the evaluation does not change inside the loop. Example:
while ( i < (max-2) )
Equivalent to:
t := max - 2 while ( i < t )
23
Code Motion
Safety of Code movement
Movement of an expression e from a basic block bi to another block bj, is safe if it does not introduce any new occurrence of e along any path. Example: Unsafe code movement
if (a<b) then z=x*2 else y = 10
24
Strength Reduction
Replacement of an operator with a less costly one.
Example:
for i=1 to 10 do x=i*5 end temp = 5; for i=1 to 10 do x = temp
Typical cases of strength reduction occurs in address calculation of array references. Applies to integer expressions involving induction variables (loop optimization)
25
Examples:
No control flows into a basic block A variable is dead at a point -> its value is not used anywhere in the program An assignment is dead -> assignment assigns a value to a dead variable
26
28
Copy Propagation
What does it mean?
Given an assignment x = y, replace later uses of x with
uses of y, provided there are no intervening assignments to x or y.
When is it performed?
At any level, but usually early in the optimization process.
29
Copy Propagation
f := g are called copy statements or copies Use of g for f, whenever possible after copy statement
Example: x[i] = a; sum = x[i] + a; x[i] = a; sum = a + a;
May not appear to be code improvement, but opens up scope for other optimizations.
30
31
Loop Optimization
Decrease the number of instruction in the inner loop Even if we increase no of instructions in the outer loop Techniques:
Code motion Induction variable elimination Strength reduction
32
Algebraic identities
Worth recognizing single instructions with a constant operand:
A * 1 = A
A * 0 = 0 A / 1 = A A * 2 = A + A
More delicate with floating-point
Strength reduction:
A ^ 2 = A * A
33
A := A / 4;
If unsigned, can replace with shift right But shift right arithmetic is a well-known problem
34
35
Jump to Return
A jump to a return can be replaced by a return
JMP lab1 ... lab1: RET
36
Local Optimization
37
38
43
4
t1 := 4 * i t3 := 4 * i t2 := t1 + t3 + t2
* t1, t3
4 i
49
+ a
- b
+ c
b0
c0
d0
50
d0
a := b + c d := a - d c := d + c
b is not live
b0
Loop Optimization
53
Loop Optimizations
Most important set of optimizations
Programs are likely to spend more time in loops
54
55
56
3 4
Flow Graph
Dominator Tree
57
58
Inner loops
Loops having the same header:
It is difficult to conclude which one of {B1, B2, B3} and {B1, B2, B4} is the inner Loop without detailed analysis of code. Assumption: When two loops have the same header they are treated as a single Loop.
59
B1
B2 B3 B4
60
The cycle (2,3) can be entered at two different places, nodes 2 and 3
Loop Optimization
Loop interchange: exchange inner loops with outer loops
Loop splitting: attempts to simplify a loop or eliminate dependencies by breaking it into multiple loops which have the same bodies but iterate over different contiguous portions of the index range.
63
Loop Optimization
Loop fusion: two adjacent loops would iterate the same number of times, their bodies can be combined as long as they make no reference to each other's data Loop fission: break a loop into multiple loops over the same index range but each taking only a part of the loop's body. Loop unrolling: duplicates the body of the loop multiple times
64
Loop Optimization
Header
Pre-Header:
Targeted to hold statements that are moved out of the loop A basic block which has only the header as successor Control flow that used to enter the loop from outside the loop, through the header, enters the loop from the pre-header
loop L
65
r5 = r4 - 3 r4 = r4 + 1
r7 = r4 *r9
r6 = r4 << 2
66
Increments are equal Initial values are equal x is not live at exit of loop For each BB where x is defined, there is no use of x between the first and the last definition of y
67
r2 = r2 - 1
r9 = r2 + r4
r7 = r1 * r9
r9 = r2 + r4
r7 = r2 * r9
r4 = *(r1) *r2 = r7
r4 = *(r2) *r7 = r2
68
69
70
71
72
1. Reaching Definitions
Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression evaluation.
Definition d of variable v reaches a point p if there exists a path from immediately after d to p such that definition d is not killed along the path. Definition d is killed along a path between two points if there exists an assignment to variable v along the path.
73
Example
d reaches u along path2 & d does not reach u along path1 Since there exists a path from d to u along which d is not killed (i.e., path2), d reaches u.
74
75
IN[B] OUT[B]
d1: X=
76
GEN[B]: Definitions within B that reach the end of B. KILL[B]: Definitions that never reach the end of B due to redefinitions of variables in B.
77
78
Constant Propagation/folding
Copy Propagation
79
2. Available Expressions
An expression is generated at a point if it is computed at that point. An expression is killed by redefinitions of operands of the expression.
An expression A+B is available at a point if every path from the start node to the point evaluates A+B and after the last evaluation of A+B on each path there is no redefinition of either A or B (i.e., A+B is not killed).
80
Available Expressions
Available expressions problem computes: at each program point the set of expressions available at that point.
81
GEN[B]: Expressions computed within B that are available at the end of B. KILL[B]: Expressions whose operands are redefined in B.
82
84
GEN[B]: Variables that are used in B prior to their definition in B. KILL[B]: Variables definitely assigned value in B before any use of that variable in B.
85
86
Compute for each program point the set of very busy expressions at the point.
88
GEN[B]: Expression computed in B and variables used in the expression are not redefined in B prior to expressions evaluation in B. KILL[B]: Expressions that use variables that are redefined in B.
89
90
Summary
May/Union
Forward Reaching Definitions
Backward
Live Variables
91