Notes on Basic Blocks - Compiler Design | CS 322, Study notes of Computer Science

Material Type: Notes; Professor: Li; Class: LANG COMPILER DESIGN; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-71v
koofers-user-71v 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Basic Blocks
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Basic Blocks 1/1
Basic Blocks
Abasic block is a sequence of consecutive (three-address) statements with
no branch-in or branch-out statements.
Only the first statement can be a lab el or target of a jump.
Only the last statement can be a jump statement.
A basic block:
(1) i := m-1
(2) j := n
(3) t1 := 4*n
(4) v := a[t1]
Not a basic block:
(1) i := m-1
(2) j := n
(3) t1 := 4*n
(4) v := a[t1]
L1:
(5) i := i+1
(6) t2 := 4*i
(7) t3 := a[t2]
(8) if t3<v goto L1
(9) j := j-1
(10) t4 := 4*j
Jingke Li (Portland State University) CS322 Basic Blocks 2/1
pf3
pf4
pf5

Partial preview of the text

Download Notes on Basic Blocks - Compiler Design | CS 322 and more Study notes Computer Science in PDF only on Docsity!

Basic Blocks

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 Basic Blocks 1 / 1

Basic Blocks

A basic block is a sequence of consecutive (three-address) statements with no branch-in or branch-out statements.

  • Only the first statement can be a label or target of a jump.
  • Only the last statement can be a jump statement.

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

Finding Basic Blocks

Algorithm:

  1. Find the “leaders” (the first statements of basic blocks).
    • The first statement of the program, or
    • The target of a conditional or unconditional branch statement, or
    • The statement following a conditional or unconditional branch statement (this statement is an implicit target).
  2. For each leader, its basic block consists of the leader and all statements up to the next leader or the end of the program.

Example:

(1) a := 0 L1: (2) b := b+ (3) c := c+b (4) a := b* (5) if a<N goto L (6) return c

  1. Find leaders: (1), (2), and (6)
  2. Form basic blocks: B1: (1) B2: (2)--(5) B3: (6)

Jingke Li (Portland State University) CS322 Basic Blocks 3 / 1

Control Flow Graph

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.

Quicksort (cont.)

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

Loops

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

Natural Loops

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

  • a = b, or
  • (^) a is the unique immediate predecessor of b, or
  • a dominates all the immediate predecessors of b.

Jingke Li (Portland State University) CS322 Basic Blocks 9 / 1

Back Edges

We call an edge t → h back edge if h dominates t.

Finding Back Edges:

  • Find a spanning tree of the CFG (e.g. using a depth-first search algorithm).
  • The edges that are not included in the spanning tree are candidates for back edges, check each of them against the dominance relation.

Traces

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:

  • Jumps between blocks that belong to the same trace can be eliminated (e.g. by reversing the jump condition if it’s a conditional jump).
  • This enables many optimizations to apply to a whole trace.

Jingke Li (Portland State University) CS322 Basic Blocks 13 / 1

Trace Examples

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

  • Overlapping traces: Trace 1 — statements from B1, B2, B3, B4, and B5. Trace 2 — statements from B1, B2, B3, B4, and B6.
  • (^) Traces that exactly covers the program: Trace 1 — statements from B1, B2, B3, B4, and B5. Trace 2 — statements from B6.
  • (^) Another set of traces that covers the program: Trace 1 — statements from B1. Trace 2 — statements from B2, B3, B4, and B5. Trace 3 — statements from B6.