Docsity

Control Flow Graph, Dot Product - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Control Flow Graph, Dot Product - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 43

2

Control Flow Graph - CFG

CFG = < V, E, Entry >, where V = vertices or nodes, representing an instruction or basic block (group of statements). E = (V x V) edges, potential flow of control Entry is an element of V, the unique program entry

(^1 2 3 4 )

4

Basic Blocks

 Flow of control only enters at

the beginning

 Flow of control only leaves at

the end

 Variants: single entry/multiple

exit, multiple entry/single exit

5

Generating CFGs

 Partition intermediate code into

basic blocks

 Add edges corresponding to

control flow between blocks

  • Unconditional goto
  • Conditional goto – multiple edges
  • No goto at end – control passes to
first statement of next block

7

Algorithm : Partition into basic blocks

Input.

sequence of three-address

statements

Output.

A list of basic blocks with each

three-address statements in

exactly one block

8

Method.

  1. Determine the set of leaders – the first statements of basic blocks. The rules are

i. The first statement is a leader

ii. Any statement that is the target of a conditional or unconditional goto is a leader

iii. Any statement that immediately follows a goto or conditional goto is a leader

10

Example

Consider the C fragment for

computing dot product a T^ b of two

vectors a and b of length 20

a 1 a 2 ... a 20 b 1 = a 1 b 1 + a 2 b 2

b 2 +.........

. + a 20 b 20

b 20

11

Dot Product

prod = 0;
i = 1;
do {
prod = prod + a[i]*b[i];
i = i + 1;
} while ( i <= 20 );

13

Dot Product

1 prod = 0 2 i = 1 3 t1 = 4i / offset / 4 t2 = a[t1] / a[i] / 5 t3 = 4i 6 t4 = b[t3] /* b[i] / 7 t5 = t2t 8 t6 = prod+t 9 prod = t 10 t7 = i+ 11 i = t 12 if i <= 20 goto (3)

leader

leader

14

Dot Product

1 prod = 0 2 i = 1 3 t1 = 4i 4 t2 = a[t1] 5 t3 = 4i 6 t4 = b[t3] 7 t5 = t2*t 8 t6 = prod+t 9 prod = t 10 t7 = i+ 11 i = t 12 if i <= 20 goto (3)

B 1
B 2

16

Quick Sort

void quicksort(int m, int n) { int i,j,v,x; if( n <= m ) return; i=m-1; j=n; v=a[n]; while(true) { do i=i+1; while( a[i] < v); do j=j-1; while( a[j] > v); i( i >= j ) break; x=a[i]; a[i]=a[j]; a[j]=x; } x=a[i]; a[i]=a[n]; a[n]=x; quicksort(m,j); quicksort(i+1,n); }

17

Quick Sort

void quicksort(int m, int n) { int i,j,v,x; if( n <= m ) return; i=m-1; j=n; v=a[n]; while(true) { do i=i+1; while( a[i] < v); do j=j-1; while( a[j] > v); i( i >= j ) break; x=a[i]; a[i]=a[j]; a[j]=x; } x=a[i]; a[i]=a[n]; a[n]=x; quicksort(m,j); quicksort(i+1,n); }

19

(1) i := m – 1 (16) t7 := 4 * i (2) j := n (17) t8 := 4 * j (3) t1 := 4 * n (18) t9 := a[t8] (4) v := a[t1] (19) a[t7] := t (5) i := i + 1 (20) t10 := 4 * j (6) t2 := 4 * i (21) a[t10] := x (7) t3 := a[t2] (22) goto (5) (8) if t3 < v goto (5) (23) t11 := 4 * i (9) j := j - 1 (24) x := a[t11] (10) t4 := 4 * j (25) t12 := 4 * i (11) t5 := a[t4] (26) t13 := 4 * n (12) If t5 > v goto (9) (27) t14 := a[t13] (13) if i >= j goto (23) (28) a[t12] := t (14) t6 := 4i* (29) t15 := 4 * n (15) x := a[t6] (30) a[t15] := x

20

(1) i := m – 1 (16) t7 := 4 * i (2) j := n (17) t8 := 4 * j (3) t1 := 4 * n (18) t9 := a[t8] (4) v := a[t1] (19) a[t7] := t (5) i := i + 1 (20) t10 := 4 * j (6) t2 := 4 * i (21) a[t10] := x (7) t3 := a[t2] (22) goto (5) (8) if t3 < v goto (5) (23) t11 := 4 * i (9) j := j - 1 (24) x := a[t11] (10) t4 := 4 * j (25) t12 := 4 * i (11) t5 := a[t4] (26) t13 := 4 * n (12) If t5 > v goto (9) (27) t14 := a[t13] (13) if i >= j goto (23) (28) a[t12] := t (14) t6 := 4i* (29) t15 := 4 * n (15) x := a[t6] (30) a[t15] := x