Basic Block Optimization
Basic Block Optimization
code code
source front intermediate intermediate target
optimiz generat program
program end code code
er or
symbol
table
15. …
Control Flow Graph (CFG)
1. sum = 0
2. i = 1
T
3. if i > n goto 15 15. …
F
4. t1 = addr(a) – 4
5. t2 = i*4
6. t3 = t1[t2] Leader
7. t4 = addr(a) – 4
8. t5 = i*4
9. t6 = t4[t5]
10. t7 = t3*t6
11. t8 = sum + t7
12. sum = t8
13. i = i + 1
14. goto 3
Common Subexpression Elimination
1. sum = 0 1. sum = 0
2. i = 1 2. i = 1
3. if i > n goto 15 3. if i > n goto 15
4. t1 = addr(a) – 4 4. t1 = addr(a) – 4
5. t2 = i*4 5. t2 = i*4
6. t3 = t1[t2] 6. t3 = t1[t2]
7. t4 = addr(a) – 4 7. t4 = addr(a) – 4
8. t5 = i*4 8. t5 = i*4
9. t6 = t4[t5] 9. t6 = t4[t5]
10. t7 = t3*t6 10. t7 = t3*t6
11. t8 = sum + t7 10a t7 = t3*t3
12. sum = t8 11. t8 = sum + t7
13. i = i + 1 11a sum = sum + t7
14. goto 3 12. sum = t8
15. … 13. i = i + 1
14. goto 3
Invariant Code Motion
1. sum = 0 1. sum = 0
2. i = 1 2. i = 1
3. if i > n goto 15 2a t1 = addr(a) - 4
4. t1 = addr(a) – 4 3. if i > n goto 15
5. t2 = i * 4 4. t1 = addr(a) - 4
6. t3 = t1[t2] 5. t2 = i * 4
10a t7 = t3 * t3 6. t3 = t1[t2]
11a sum = sum + t7 10a t7 = t3 * t3
13. i = i + 1 11a sum = sum + t7
14. goto 3 13. i = i + 1
15. … 14. goto 3
15. …
Strength Reduction
1. sum = 0 1. sum = 0
2. i = 1 2. i = 1
2a t1 = addr(a) – 4 2a t1 = addr(a) - 4
3. if i > n goto 15 2b t2 = i * 4
5. t2 = i * 4 3. if i > n goto 15
6. t3 = t1[t2] 5. t2 = i * 4
10a t7 = t3 * t3 6. t3 = t1[t2]
11a sum = sum + t7 10a t7 = t3 * t3
13. i = i + 1 11a sum = sum + t7
14. goto 3 11b t2 = t2 + 4
15. … 13. i = i + 1
14. goto 3
15. …
Test Elision and Induction Variable
Elimination
1. sum = 0 1. sum = 0
2. i = 1 2. i = 1
2a t1 = addr(a) – 4 2a t1 = addr(a) – 4
2b t2 = i * 4 2b t2 = i * 4
3. if i > n goto 15 2c t9 = n * 4
6. t3 = t1[t2] 3. if i > n goto 15
10a t7 = t3 * t3 3a if t2 > t9 goto 15
11a sum = sum + t7 6. t3 = t1[t2]
11b t2 = t2 + 4 10a t7 = t3 * t3
13. i = i + 1 11a sum = sum + t7
14. goto 3 11b t2 = t2 + 4
15. … 13. i = i + 1
14. goto 3a
15. …
Constant Propagation and Dead
Code Elimination
1. sum = 0 1. sum = 0
2. i = 1 2. i = 1
2a t1 = addr(a) – 4 2a t1 = addr(a) - 4
2b t2 = i * 4 2b t2 = i * 4
2c t9 = n * 4 2c t9 = n * 4
3a if t2 > t9 goto 15 2d t2 = 4
6. t3 = t1[t2] 3a if t2 > t9 goto 15
10a t7 = t3 * t3 6. t3 = t1[t2]
11a sum = sum + t7 10a t7 = t3 * t3
11b t2 = t2 + 4 11a sum = sum + t7
14. goto 3a 11b t2 = t2 + 4
15. … 14. goto 3a
15. …
New Control Flow Graph
1. sum = 0
2. t1 = addr(a) - 4
3. t9 = n * 4
4. t2 = 4
T
5. if t2 > t9 goto 11 11. …
F
6. t3 = t1[t2]
7. t7 = t3 * t3
8. sum = sum + t7
9. t2 = t2 + 4
10. goto 5
Building Control Flow Graph
• Partition into basic blocks
1. Determine the leader statements
(i) First program statement
(ii) Targets of conditional or unconditional goto’s
(iii) Any statement following a goto
2. For each leader, its basic block consists of
the leader and all statements up to but not
including the next leader or the end of the
program
Building Control Flow Graph
• Add flow-of-control information
• There is a directed edge from basic block
B1 to block B2 if B2 can immediately
follow B1 in some execution sequence
(i) B2 immediately follows B1 and B1 does not
end in an unconditional jump
(ii) There is a jump from the last statement in B1
to the first statement in B2
Leader Statements and Basic Blocks
1. sum = 0
2. i = 1
3. if i > n goto 15
4. t1 = addr(a) – 4
5. t2 = i*4
6. t3 = t1[t2]
7. t4 = addr(a) – 4
8. t5 = i*4
9. t6 = t5[t5]
10. t7 = t3*t6
11. t8 = sum + t7
12. sum = t8
13. i = i + 1
14. goto 3
15. …
Analysis and optimizing
transformations
• Local optimizations – performed by local
analysis of a basic block
• Global optimizations – requires analysis of
statements outside a basic block
• Local optimizations are performed first,
followed by global optimizations
Local optimizations --- optimizing
transformations of a basic blocks
• Local common subexpression elimination
• Dead code elimination
• Copy propagation
• Constant propagation
• Renaming of compiler-generated
temporaries to share storage
Example 1: Local Common
Subexpression Elimination
1. t1 = 4 * i
2. t2 = a [ t1 ]
3. t3 = 4 * i
4. t4 = b [ t3 ]
5. t5 = t2 * t4
6. t6 = prod * t5
7. prod = t6
8. t7 = i + 1
9. i = t7
10.if i <= 20 goto 1
Example 2: Local Dead Code
Elimination
1. a = y + 2 1’. a = y + 2
2. z = x + w 2’. x = a
3. x = y + 2 3’. z = b + c
4. z = b + c 4’. b = a
5. b = y + 2
Example 3: Local Constant
Propagation
1. t1 = 1 Assuming a, k, t3, and t4 are used beyond:
2. a = t1 1’. a = 1
3. t2 = 1 + a 2’. k = 2
4. k = t2 3’. t4 = 8.2
5. t3 = cvttoreal(k) 4’. t3 = 8.2
6. t4 = 6.2 + t3
7. t3 = t4 D. Gries’ algorithm:
•Process 3-address statements in order
•Check if operand is constant; if so, substitute
•If all operands are constant,
Do operation, and add value to table
associated with LHS
•If not all operands constant
Delete any table entry for LHS
Problems
• Troubles with arrays and pointers. Consider:
x = a[k];
a[j] = y;
z = a[k];
9 10
Four Classical Data-flow Problems
• Reaching definitions (Reach)
• Live uses of variables (Live)
• Available expressions (Avail)
• Very Busy Expressions (VeryB)
• Def-use chains built from Reach, and the dual
Use-def chains, built from Live, play role in many
optimizations
• Avail enables global common subexpression
elimination
• VeryB is used for conservative code motion
Reaching Definitions
• Definition A statement that may change
the value of a variable (e.g., x = i+5)
• A definition of a variable x at node k
reaches node n if there is a path clear of a
definition of x from k to n.
k x = …
x = …
n … = x
• Def and use x = y + z scanf(“%d” , &y)
P1: x = 10
…..
P2: a = x + 20 RD : x=10 p1
P4 …. x = y + z
x = …
n … = x
Def-use Relations
• Use-def chain links an use to a definition
that reaches that use
• Def-use chain links a definition to an use
that it reaches
k x = …
x = …
n … = x
d1 : n=30
if ( x > y )
d2: n =0
p: a = n + b
p2 : x = n *3
n has 2 RD ud chain of n { d1, d2}
n : d1 du chain { p, p2}
d2 du chain { p, p2}
Optimizations Enabled
• Dead code elimination (Def-use)
• Code motion (Use-def)
• Constant propagation (Use-def)
• Strength reduction (Use-def)
• Test elision (Use-def)
• Copy propagation (Def-use)
Dead Code Elimination
1. sum = 0
2. i = 1
T
3. if i > n goto 15
4. t1 = addr(a)–4
5. t2 = i * 4 After strength reduction,
6. i = i + 1
test elision and constant
propagation, the def-use
links from i=1 disappear. It
becomes dead code.
Constant Propagation
1. i = 1
2. i = 1
3. i = 2
4. p = i*2
5. i = 1
6. q = 5*i+3 = 8
Terms
• Control flow graph (CFG)
• Basic block
• Local optimization
• Global optimization
• Data-flow analysis