Professional Documents
Culture Documents
16 Marks Q &A
16 Marks Q &A
. Explain about the principal sources of optimization in detail A transformation of the program is called local, if its performed inside the basic block. Otherwise it is said to be global Local usually performed first. Example : Quick sort program C-code for quick sort void quicksort (m,n) int m,n; { int i,j; int v,x; if(n<=m) return; /* fragment begins here*/ 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[j]; a[j]=x; } x=a[i]; a[i]=a[n]; a[n]=x; /* fragment ends here */ quicksort(m,j); quicksort(i+1,n); } Three address code: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. i:=m-1 j :=n t1:=4*n v :=a[t1] v:=i+1 t2:=4*i t3:=a[t2] if t3<v goto (5) j:=j-1 t4:=4*j t5:=a[t4] if t5>v goto(%) if i>j goto(23) t6:=4*i x:=a[t6]
16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.
t7:=4*i t8:4*j t9:=a[t8] a[t7]:=t9 t10:=4*j a[t10]:=x goto(5) t11:=4*i x:=a[t11] t12:=4*i t13:=4*n t14:=a[t13] a[t12]:=t14 t15:=4*n a[t15]:=x Unit V 16 mark Q & A 1
the construction of basic block initially, without any optimizations is, i := m-1 j := n t1:=4*n v:=a[t1]
B1
B2
B3 B4
B5
t6:=4*i x:=a[t6] t7:=4*i t8:=4*j t9:=a[t8] a[t7]:=t9 t10:=4*j a[t10]:=x goto B2
if i>=j goto B6
B6
t11:=4*i x:=a[t11] t12:]4*i t13:=4*n t14:=a[t1] a[t12]:=t14 t15:=4*n a[t15]:=x
(I) FUNCTION PRESERVING TRANSFORMATION Examples: -> common subexpression elimination -> copy propogation -> dead code elimination -> constant folding Common subexpressions An occurrence of an expression ,E is called a common sub-expression, if E was previously computed and the values in the variables, have not changed , since previous computation B5 - Before B5 - After 1) 2) 3) 4) 5) 6) 7) 8) 9) t6 := 4*i x := a[t6] t7 := 4*i t8 := 4*j t9 := a[t8] a[t7]:=t9 t10 := 4*j a[t10] := x goto B2 1) 2) 3) 4) 5) 6) 7) t6 := 4*i x := a[t6] t8 := 4*j t9 := a[t8] a[t6] := t9 a[t8] := x goto B2 Unit V 16 mark Q & A 2
The following figure, shows the result of eliminating both global and local common subexpressions i :m-1 j:=n t1:=4*n v:=a[t1]
B1
B2
B3
B4
if i>=j goto B6
B5
x:=t3 a[t2]:=t5 a[4]:=x goto B2 x:=t3 a[t2]:=t5 a[4]:=x goto B2
B6
Copy propogation Assignments of the form f:=g are called copy statements (or) copies . While eliminating common sub expressions these statements are introduced a:=d+e a:=d+e t:=d+e a:=t t :=d+e b:=t
c:=d+e
e:=t
This may not appear as an improvement , but gives an opportunity to eliminate x Dead code elimination A variable is live at a point ,if its value can be used subsequently, otherwise its dead at a point .A related data is dead (or) useless code, statements that compute values-will never get used. Ex: i=0; if(i=1) { a=(b+5)*3; } Here, if statement is dead code, because this condition will never be satisfied. So if statement can be eliminated. Advantage of copy propagation is that, it often turns copy statement to dead code x:=t3 a[t2]:=t5 a[t4]:=t3 goto b Removes the assignment tox (II) LOOP OPTIMISATION Next we need to concentrate on loops, where programs tend to spend , bulk of their time in inner loops. Three techniques are important for loop optimization: (i) code motion moves code outside the loop (ii) Induction variable elimination eliminate I and j from inner loops B2 and B3 (iii) Reduction in strength replaces expensive operation by cheaper one (ie) multiplication by the addition. Code motion: The transformation take an expression,that yields the same result,independent of nuber of tiomes the loops is executed(loop invariant computation)and places the loop before the expression. While (i<=limit-2) Here, limit-2 is loop invariant computation t = limit-3 While(i<=t) a[t]2]:=t5 a[t4]:=t3 goto B2
Induction variables and reduction in strength Consider, the loop around B3. Only portion of the flowgraph, relevant to the transformations on B3 is shown. Note that,j and t4 are locked together, (ie) everytime ,the value of j decrease by 1,the value of t4 decreases by 4(because 4*j is assigned to t4) Such identifiers are called induction variables. If 2 or more induction variables are there in a loop, we may eliminate all but one, by induction variable elimination
B1
i :m-1 j:=n t1:=4*n v:=a[t1]
B1
B2
B2 B3
j:=j-1 t4:=4*j t5:=a[t4] if t5>v goto B3 j:=j-1 t4:=4*j t5:=a[t4] if t5>v goto B3
B3
B4
if i>=j goto B6 if i>=j goto B6
B4
B5
B6
B5
B6
After,strength in reduction is applied in inner loops B2 and B3,the only use of i and j,is to determine the outcome of the test in block B4. We know that the values of I and t2 satisfy the relationship t2=4*i , while j and t4 satisfy the relationship, t4:=4*j. So,the test t2>=t4 is equivalent to i>=j once ,this replacement is done ,I in block B2 and j in block B3 are dead variables and hence they can be removed.
B1
B2
B3
B4
if t2>=t4 goto B6
B5
a[t2]:=t5 a[t4]:=t3 goto B2
B6
t14:=a[t1] a[t2]:=t14 a[t1]:=t13
True B1 has grown from 4 instructions to 6, but B1 is executed only once in the fragment , so the total running time is very , affected by the size of B1. --------------------------------------------------------------------------------------------------------------------2. Explain about Global Data flow analysis in detail.
Memory locations for code are determined at compile time. Locations of static data can also be determined at compile time. Data objects allocated at run-time. (Activation Records) Other dynamically allocated data objects at run-time. (For example, malloc area in C).
Heap
Activation Records Information needed by a single execution of a procedure is managed using a contiguous block of storage called activation record. An activation record is allocated when a procedure is entered, and it is de-allocated when that procedure exited. Size of each field can be determined at compile time (Although actual location of the activation record is determined at run-time). Except that if the procedure has a local variable and its size depends on a parameter, its size is determined at the run time. The returned value of the called procedure is returned in this field to the calling procedure. In practice, we may use a machine register for the return value. The field for actual parameters is used by the calling procedure to supply parameters to the called procedure. The optional control link points to the activation record of the caller. The optional access link is used to refer to nonlocal data held in other activation records. The field for saved machine status holds information about the state of the machine before the procedure is called. The field of local data holds data that local to an execution of a procedure.. Temporay variables is stored in the field of temporaries.
return value actual parameters optional control link optional access link saved machine status local data temporaries
--------------------------------------------------------------------------------------------------------------------
4. What are the various actions occurs while implementing a procedure? Calling sequence: handles a call to a procedure: loads actual parameters where callee can find them; saves machine state (return address, ); branches to callee; allocates an activation record. Return sequence: handles the return from a procedure call: loads the return value where the caller can find it; deallocates the activation record; restores machine state (saved registers, PC, etc.); branches back to caller.
return value actual parameters optional control link optional access link saved machine status local data temporaries
Callers Responsibility
return value actual parameters optional control link optional access link saved machine status local data temporaries
Callees Responsibility
--------------------------------------------------------------------------------------------------------------------
5. Explain the two types of scope rules. Scope rules of a language determine the treatment of references to nonlocal names. Scope Rules: Lexical Scope (Static Scope) Determines the declaration that applies to a name by examining the program text alone at compile-time. Most-closely nested rule is used. Pascal, C, .. Dynamic Scope Determines the declaration that applies to a name at run-time. Lisp, APL, ... Lexical Scope: The scope of a declaration in a block-structured language is given by the mostly closed rule. Each procedure (block) will have its own activation record. procedure begin-end blocks (treated same as procedure without creating most part of its activation record) A procedure may access to a nonlocal name using: access links in activation records, or displays (an efficient way to access to nonlocal names) -------------------------------------------------------------------------------------------------------------------6. Explain data flow analysis along with the equations for structured programs S ::= id:=expression | S;S | if expression then S else S | do S while expression
d:
a := b + c
gen[S] = {d} kill[S] = Da-{d}. Da is the set of all definitions of a. out[S] = gen[S] + (in[S]-kill[S])
S1 S S2
When S is of the form S1; S2: gen[S] = gen[S2]+(gen[S1]-kill[S2]) kill[S] = kill[S2]+(kill[S1]-gen[S2]) in[S1] = in[S] in[S2] = out[S1] out[S] = out[S2]
S1
S2
When S is of the form if ... then S1 else S2 gen[S]=gen[S1]+gen[S2] kill[S]=kill[S1]kill[S2] in[S1] = in[S] in[S2] = in[S] out[S] = out[S1]+out[S2]
When S is of the form do S1 while ...: gen[S] = gen[S1] kill[S] = kill[S1] in[S1] = in[S]+gen[S1] out[S] = out[S1]
-------------------------------------------------------------------------------------------------------------------