Examples of Basic Blocks Following sequence of threeaddress
Examples of Basic Blocks Following sequence of three-address statements forms a basic block: t 1 : = a * a + t 7 t 4 + * t 5 t 2 : = a * b t 1 t 3 : = 2 * t 2 * * b b t 4 : = t 1+ t 3 2 * t 2 a a t 5 : = b * b a b t 6 : = t 4 + t 5 It computes: a*a + 2*a*b + b*b Given 3 -AS x : = y+z: -it defines X -it uses (reference) y and z t 6 t 7 Live name: A name is a basic block is said to be live at a given point if its value is used after that point in the program, perhaps in another basic block.
Examples of 3 address code translation C-code void quicksort(m, n) int m, n { int i, j; int v, x; if (n<=m) return; /* fragment begins */ i: =m-1; j=n ; v=a[n]; while(1) { do i=i+1; while(a[i]<v) do j=j-1; while(a[j]>v) if (i>=j) break; x=a[i]; a[i]=a[n]; a[n]=x; } x=a[i]; a[i]=a[n]; a[n]=x; /* fragment ends */ quicksort( m , j); quicksort(l+1, n); } 3 Address Code. (1) (5) (8) (9) (12) (13) (14) (16) (18) (20) (22) (23) i = m-1; j = n ; v = a[n]; i : = m-l; j: =n; t 1 : = 4*n; v : = a[t 1]; while(1) { do i = i + 1; while(a[i]<v) ; i : = i+1; t 2 : = 4*i; t 3 : = a[t 2]; if t 3<v goto(5) do j = j-1; while (a[j] > v); j: =j+1; t 4: =4*j ; t 5: =a[t 4]; if t 5 >v goto (9) if( i >=j) goto (23) x = a[i]; a[i] = a[n]; a[n]=x t 6 : = 4*i; x: =a[t 6]; t 7 : = 4*i; x: =a[t 6]; t 9: = a[t 8]; a[t 7]: =t 9; t 10: = 4*j; a[t 10]: = x goto(5). . .
Example of Flow graphs i: =m-1 j: =n t 1=4*n v: = a[t 1] I=i+1 t 2: =4*i t 3: =a[t 2] if( t 3<v) goto B 2 j: =j-1 t 4: = 4 * j t 5: = a[t 4] if( t 5 >v ) goto B 3 if(i>=j) goto B 6 t 6: =4*i x: = a[t 6] t 7 : = 4 * i t 8 : = 4 * j t 9: = a[t 8] a[t 7]: = t 9 t 10: = 4 * j a[t 10]: =x goto B 2 B 5 B 1 B 2 B 3 B 4 t 11: = 4* i X= a[t 11] t 12: = 4 * i t 13: = 4*n t 14 : = a[t 13] a[t 12]: = t 14 t 15 : = 4 * n a[t 15]: = x B 6
Partition into Basic Blocks algorithm Input : A sequence of 3 address statements Output: A sequence of basic blocks with each 3 A Statement in exactly one block. Method: (1) First determine a set of leaders, the 1 st statement of basic blocks: a) The first statement is a leader b) Any statement that is a target of a conditional or unconditional goto is a leader c) Any statement that immediately follows a goto, or conditional goto statement is a leader. (2) For each leader its basic block consists of: a) The leader b) All statements upto but not including the next leader or the end of the program.
Example of Partition into Basic Blocks (1) prod : = 0 (2) i : = 1 (3) t 1 : = 4 * i (4) t 2 : = a[t 1] (5) t 3 : = 4 * i (6) t 4 : = b[t 3] (7) t 5 : = t 2 * t 4 (8) t 6 : = prod + t 5 (9) prod : = t 6 (10) t 7 : = i+1 (11) i : = t 7 (12) if( i <=20) goto (3)
Example of Partition into Basic Blocks (1) prod : = 0 B 1 (2) i : = 1 (3) t 1 : = 4 * i B 2 (4) t 2 : = a[t 1] (5) t 3 : = 4 * i (6) t 4 : = b[t 3] (7) t 5 : = t 2 * t 4 (8) t 6 : = prod + t 5 (9) prod : = t 6 (10) t 7 : = i+1 (11) i : = t 7 (12) if( i <=20) goto (3) A leader by rule 1. a A block by rule 2 A leader by rule 1. b A block by rule 2 A leader by rule 1. c
- Slides: 6