



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Li; Class: LANG COMPILER DESIGN; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Basic Blocks 1 / 1
A basic block is a sequence of consecutive (three-address) statements with no branch-in or branch-out statements.
A basic block: (1) i := m- (2) j := n (3) t1 := 4*n (4) v := a[t1]
Not a basic block: (1) i := m- (2) j := n (3) t1 := 4n (4) v := a[t1] L1: (5) i := i+ (6) t2 := 4i (7) t3 := a[t2] (8) if t3<v goto L (9) j := j- (10) t4 := 4*j
Algorithm:
Example:
(1) a := 0 L1: (2) b := b+ (3) c := c+b (4) a := b* (5) if a<N goto L (6) return c
Jingke Li (Portland State University) CS322 Basic Blocks 3 / 1
A graph representation of three-address code. Nodes — represent basic blocks Edges — represent (potential) flow of control
Example:
(1) a := 0 L1: (2) b := b+ (3) c := c+b (4) a := b* (5) if a<N goto L (6) return c
a := 0
b := a+ c := c+b a := b* a<N
return c
B
B
B
Control flow graph can be constructed by the basic block partition program.
Basic Blocks:
B1: (1)--(4) B2: (5)--(8) B3: (9)--(12) B4: (13) B5: (14)--(22) B6: (23)--(30)
Control Flow Graph:
i := m- j := n t1 := 4n v := a[t1] i := i+ t2 := 4i t3 := a[t2] if t3<v goto B
j := j- t4 := 4*j t5 := a[t4] if t5>v goto B if i>=j goto B
t6 := 4i x := a[t6] t7 := 4i t8 := 4j t9 := a[t8] a[t7] := t t10 := 4j a[t10] := x goto B
t11 := 4i x := a[t11] t12 := 4i t13 := 4n t14 := a[t13] a[t12] := t t15 := 4n a[t15] := x
B
B
B
B B5 B
Jingke Li (Portland State University) CS322 Basic Blocks 7 / 1
Control flow graphs may contain “loops”. Loops are very important for optimization, because statements in a loop may be executed many times during the execution of the program.
What is a Loop?
Are there one or two loops in each of these CFGs?
1 2 3 4 5 6 1
2
3 4
5
Given a “back edge” t → h, the natural loop of t → h is the subgraph
consisting of the set of nodes containing h and all the nodes that (1) are dominated by h and (2) from which t can be reached without passing through h, and the edge set connecting all the nodes in this node set.
Node h is the loop header, which is the unique entry node to the loop.
Dominance Relation — We say that node d dominates node i, if every possible execution path from CFG entry to i includes d. (e.g. You can’t execute i without executing d first.)
Observation: Node a dominates node b if and only if
Jingke Li (Portland State University) CS322 Basic Blocks 9 / 1
We call an edge t → h back edge if h dominates t.
Finding Back Edges:
A trace is a sequence of statements that could be consecutively executed during the execution of the program. It can include conditional branches.
A program has many different, overlapping traces. We are only interested in the set of traces that exactly covers the program: each block must be in exactly one trace.
Reasons for forming traces:
Jingke Li (Portland State University) CS322 Basic Blocks 13 / 1
i := m- j := nt1 := 4n v := a[t1] i := i+ t2 := 4i t3 := a[t2]if t3<v goto B
j := j-1t4 := 4*j t5 := a[t4]if t5>v goto B
if i>=j goto B t6 := 4i x := a[t6]t7 := 4i t8 := 4jt9 := a[t8] a[t7] := t9t10 := 4j a[t10] := x goto B
t11 := 4ix := a[t11] t12 := 4i t13 := 4nt14 := a[t13] a[t12] := t14t15 := 4n a[t15] := x
B
B
B
B B5 B