Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices...

31
Compiler Compiler Construction Construction Sohail Aslam Lecture 43

Transcript of Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices...

Page 1: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

Compiler Compiler ConstructionConstruction

Compiler Compiler ConstructionConstruction

Sohail Aslam

Lecture 43

Page 2: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

2

Control Flow Graph - CFGControl Flow Graph - CFGControl Flow Graph - CFGControl Flow Graph - CFGCFG = < 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 5

Page 3: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

3

Basic BlocksBasic BlocksBasic BlocksBasic Blocks

A basic block is a sequence of consecutive statements with single entry/single exit

Page 4: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

4

Basic BlocksBasic BlocksBasic BlocksBasic 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

Page 5: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

5

Generating CFGsGenerating CFGsGenerating CFGsGenerating 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

Page 6: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

6

Generating CFGsGenerating CFGsGenerating CFGsGenerating 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

Page 7: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

7

AlgorithmAlgorithm: Partition into basic blocks: Partition into basic blocksAlgorithmAlgorithm: Partition into basic blocks: 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

Page 8: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic 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

Page 9: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

9

Method.

2. For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the program

Page 10: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

10

ExampleExampleExampleExampleConsider the C fragment for computing dot product aT b of two vectors a and b of length 20

a1 a2 ... a20 b1 = a1b1 + a2b2

b2 +......... . + a20b20

.b20

Page 11: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

11

Dot ProductDot ProductDot ProductDot Productprod = 0;

i = 1;

do {

prod = prod + a[i]*b[i];

i = i + 1;

} while ( i <= 20 );

Page 12: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

12

Dot ProductDot ProductDot ProductDot Product1 prod = 02 i = 13 t1 = 4*i /* offset */4 t2 = a[t1] /* a[i] */5 t3 = 4*i6 t4 = b[t3] /* b[i] */7 t5 = t2*t48 t6 = prod+t59 prod = t610 t7 = i+111 i = t712 if i <= 20 goto (3)

Page 13: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

13

Dot ProductDot ProductDot ProductDot Product1 prod = 02 i = 13 t1 = 4*i /* offset */4 t2 = a[t1] /* a[i] */5 t3 = 4*i6 t4 = b[t3] /* b[i] */7 t5 = t2*t48 t6 = prod+t59 prod = t610 t7 = i+111 i = t712 if i <= 20 goto (3)

leader

leader

Page 14: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

14

Dot ProductDot ProductDot ProductDot Product1 prod = 02 i = 13 t1 = 4*i 4 t2 = a[t1]5 t3 = 4*i6 t4 = b[t3]7 t5 = t2*t48 t6 = prod+t59 prod = t610 t7 = i+111 i = t712 if i <= 20 goto (3)

B1

B2

Page 15: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

15

prod = 0i = 1

t1 = 4*it2 = a[t1] t3 = 4*it4 = b[t3]t5 = t2*t4t6 = prod+t5prod = t6t7 = i+1i = t7if i <= 20 goto B2

B1

B2

Page 16: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

16

Quick SortQuick SortQuick SortQuick Sortvoid 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);

}

Page 17: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

17

Quick SortQuick SortQuick SortQuick Sortvoid 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);

}

Page 18: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

18

(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] := t9

(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] := t14

(14) t6 := 4*i (29) t15 := 4 * n

(15) x := a[t6] (30) a[t15] := x

Page 19: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

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] := t9

(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] := t14

(14) t6 := 4*i (29) t15 := 4 * n

(15) x := a[t6] (30) a[t15] := x

Page 20: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

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] := t9

(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] := t14

(14) t6 := 4*i (29) t15 := 4 * n

(15) x := a[t6] (30) a[t15] := x

Page 21: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

21

j = j-1t4 = 4*jt5 = a[t4]if t5 > v goto B3

i = m-1j = nt1 = 4*nv = a[t1]

i = i+1t2 = 4*it3 = a[t2]if t3 < v goto B2

t11 = 4*ix = a[t11]t12 = 4*it13 = 4*nt14 = a[t13]a[t12] = t14t15 = 4*na[t15] = x

t6 = 4*ix = a[t6]t7 = 4*it8 = 4*jt9 = a[t8]a[t7] = t9t10 = 4*ja[t10] = xgoto B2

if i >= j goto B6

B6

B1

B2

B3

B4

B5

Page 22: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

22

Basic Block Code GenerationBasic Block Code GenerationBasic Block Code GenerationBasic Block Code Generation

Algorithms: Basic - using liveness

information Using DAGS - node

numbering Register Allocation

Page 23: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

23

Basic Code GenerationBasic Code GenerationBasic Code GenerationBasic Code Generation Deal with each basic block

individually.

Generate code for the block using liveness information.

At end, generate code that saves any live values left in registers.

Page 24: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

24

Basic Code GenerationBasic Code GenerationBasic Code GenerationBasic Code Generation Deal with each basic block

individually.

Generate code for the block using liveness information.

At end, generate code that saves any live values left in registers.

Page 25: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

25

Basic Code GenerationBasic Code GenerationBasic Code GenerationBasic Code Generation Deal with each basic block

individually.

Generate code for the block using liveness information.

At end, generate code that saves any live values left in registers.

Page 26: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

26

Computing Live/Next Use Computing Live/Next Use InformationInformationComputing Live/Next Use Computing Live/Next Use InformationInformation

For the statement: x = y + z

x has a next use if there is a statement s that references x and there is some way for control to flow from the original statement to s.

Page 27: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

27

Computing Live/Next Use Computing Live/Next Use InformationInformationComputing Live/Next Use Computing Live/Next Use InformationInformation

x = y + z ............

s t1 = x – t3 next use

Page 28: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

28

Computing Live/Next Use Computing Live/Next Use InformationInformationComputing Live/Next Use Computing Live/Next Use InformationInformation

A variable is live at a given point in time if it has a next use.

Liveness tells us whether we care about the value held by a variable.

Page 29: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

29

Computing Live/Next Use Computing Live/Next Use InformationInformationComputing Live/Next Use Computing Live/Next Use InformationInformation

x = y + z ............

s t1 = x – t3

live!

Page 30: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

30

Computing live statusComputing live statusComputing live statusComputing live statusInput:

A basic block.

Output: For each statement, set of live variables

Page 31: Compiler Construction Sohail Aslam Lecture 43. 2 Control Flow Graph - CFG CFG =, where V = vertices or nodes, representing an instruction or basic block.

31

Method:

1. Initially all non-temporary variables go into live set.

2. for i = last statement to first statement:

for statement i: x = y op z

• attach to statement i, current live set.

• remove x from set.

• add y and z to set.