Basic Block and Flow Graph
Basic Block and Flow Graph
Denition
sequence of code
control enters at top, exits at bottom
no branch/halt except at end
Construction algorithm (for 3-address code)
1. determine set of leaders
(a) rst statement
(b) target of goto or conditional goto
(c) statement following goto or conditional goto
2. add to basic block all statements following
leader up to next leader or end of program
Example:
A := 0;
if (<cond>) goto L;
A := 1;
B := 1;
L: C := A;
References
John Cocke and Jack Schwartz in
\Programming Languages and Their Compilers;
Preliminary Notes" (1970)
\A Survey of Data Flow Analysis Techniques"
by K.W. Kennedy (in Program Flow Analysis,
N.D. Jones and S.S. Muchnick, editors)
See also Aho, Sethi, and Ullman, pages 292,
293, and 635
AVAIL
hash table keyed by (val, op, val )
Fields: lhsVal,op, rhsval, resultVal, isConstant,
instruction
CONSTANTS (a nit )
table to hold funky, machine-specic values
important in cross-compilation
Fields: val, bits
CPSC 434 Lecture 23, Page 6
Value numbering
for i1 to n
if [ ] is a store then
op i
if is constant then
r
else /* an expression */
if is constant then replace
l [ ] with constant lhs i
create CONSTANTS( [ ] ) l; op i ; r
[ ] \constant ( [ ] )"
op i l op i r
else
if ( [ ] ) 2 AVAIL then
l; op i ; r
else
create AVAIL( [ ] ) l; op i ; r
for i 1 to n
Triples Source
T1: a C4 a 4
T2: i j
T3: T2 + C5
T4: k T3 k i j + 5
T5: C5 a
T6: T5 k
T7: l T6 l 5 a k
T8: m i m i
T9: m j
T10: i a
T11: T9 + T10
T12: b T11 b m j + i a
T13: a T12 a b
Protability
assume that load of constant is cheaper than op
assume that reference (or copy) is cheaper than
op
Opportunity
look at each instruction
linear time, but assumes basic blocks are small
Terminology
this kind of analysis is called data-
ow analysis
it requires a control
ow graph
{ nodes represent basic blocks
{ edges represent possible control
ow paths
{ an algorithm to construct the control
ow
graph