Professional Documents
Culture Documents
Lexical Analyzer
(scanner)
Syntax Analyzer
(parser)
Front End
Semantic Analyzer
(Analysis)
Intermediate Code
Generator
Code Optimizer
Back End
Code Generator
(Synthesis)
Target Code
Compiler Design Page 2
Outline
Introduction to Dataflow Analysis
Dataflow Algorithms
Liveness Analysis
Reaching Definition Analysis
DU/UD Chains
Generalized Dataflow Algorithm
Available Definition
Available Expression
Optimizations
r4 = 4
r6 = 8
r1 = MEM[r2+0]
r2 = r2 + 1 USE = {r2,r4}
r3 = r1 * r4 DEF = {r1,r2,r3}
r1 = r1 + 5 r2 = 0
USE = {r1,r5} USE = Æ
r3 = r5 – r1 r7 = 23
DEF = {r1,r3,r7} DEF = {r1,r2,r7}
r7 = r3 * 2 r1 = 4
r8 = r7 + 5
USE = {r3,r7}
r1 = r3 – r8
DEF = {r1,r3,r8}
r3 = r1 * 2
IN = {r3,r7}
r8 = r7 + 5
USE = {r3,r7}
r1 = r3 – r8
DEF = {r1,r3,r8}
r3 = r1 * 2
OUT = Æ
5: r6 = r2 + r3
6: r7 = r4 – r5
defs 1, 3, 5, 6 reach this point
defs 2, 4 are killed by 5
4: r1 = r1 + 1 GEN = 4,5
5: r7 = r1 * r2 KILL = 1
GEN = 6 GEN = 7
KILL = 2,7 6: r2 = 0 7: r2 = r2 + 1 KILL = 2,6
8: r4 = r2 + r1 GEN = 8
KILL = Æ
9: r9 = r4 + r8 GEN = 9
KILL = Æ
IN = 1,2,3 à 1,2,3,4,5,6,7,8
4: r1 = r1 + 1 GEN = 4,5
5: r7 = r1 * r2 KILL = 1
Forward-Flow Backward-Flow
Any in[B] = Èp Î pred(B) out[p] out[B] = Ès Î succ(B) in[s]
out[B] = gen[B] È (in[B] - kill[B]) in[B] = gen[B] È (out[B] - kill[B])
path
All in[B] = Çp Î pred(B) out[p] out[B] = Çs Î succ(B) in[s]
out[B] = gen[B] È (in[B] - kill[B]) in[B] = gen[B] È (out[B] - kill[B])
path
1: r1 = r2 + r3 1,2 reach
2: r6 = r4 – r5 1,2 available
1,2 reach 3: r4 = 4
1,2 available 4: r6 = 8
1,3,4 reach
1,3,4 available
5: r6 = r2 + r3
6: r7 = r4 – r5 1,2,3,4 reach
1 available
change = 1
while (change) do
change = 0
for each basic block in procedure, X, do
old_OUT = OUT(X)
IN(X) = Intersect(OUT(Y)) for all predecessors Y of X
OUT(X) = GEN(X) + (IN(X) – KILL(X))
if (old_OUT != OUT(X)) then
change = 1
endif
endfor
endfor
Compiler Design Page 34
Outline
Introduction to Dataflow Analysis
Dataflow Algorithms
Liveness Analysis
Reaching Definition Analysis
DU/UD Chains
Generalized Dataflow Algorithm
Available Definition
Available Expression
Optimizations
r2 = 0 r3 = r3 + 1
r3 = r2 + r1
When is it legal? r1 = r1 + r2 r7 = r1 + r4
SRC: Literal is a hard coded
constant, so never a
problem r8 = r1 + 3
DEST: Must be available
Guaranteed to reach r9 = r1 + r11
May reach not good enough