16 marks Q &A

Embed Size (px)

Citation preview

  • 7/31/2019 16 marks Q &A

    1/10Unit V 16 mark Q & A 1

    Om Sakthi

    Adhiparasakthi Engineering College

    Melmaruvathur 603 3 19

    Unit V

    Code Optimization

    1. Explain about the principal sources of optimization in detailA 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=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. i:=m-12. j :=n3. t1:=4*n4. v :=a[t1]5. v:=i+16. t2:=4*i7. t3:=a[t2]8. if t3v goto(%)13. if i>j goto(23)14.t6:=4*i15.x:=a[t6]

    16.t7:=4*i17.t8:4*j18.t9:=a[t8]19.

    a[t7]:=t920.t10:=4*j

    21.a[t10]:=x22.goto(5)23.t11:=4*i24.x:=a[t11]25.t12:=4*i26.t13:=4*n27.t14:=a[t13]28.a[t12]:=t1429.t15:=4*n30.a[t15]:=x

  • 7/31/2019 16 marks Q &A

    2/10Unit V 16 mark Q & A 2

    the construction of basic block initially, without any optimizations is,

    (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

    1) t6 := 4*i2) x := a[t6]3) t7 := 4*i4) t8 := 4*j5) t9 := a[t8]6) a[t7]:=t97) t10 := 4*j8) a[t10] := x9) goto B2

    1) t6 := 4*i2) x := a[t6]3) t8 := 4*j4) t9 := a[t8]5) a[t6] := t96) a[t8] := x7) goto B2

    B5 - Before B5 - After

    i := m-1

    j := n

    t1:=4*n

    v:=a[t1]

    i:=i+1t2:=4*i

    t3:=a[t2]

    if t3=j goto B6

    t6:=4*i

    x:=a[t6]

    t7:=4*i

    t8:=4*j

    t9:=a[t8]

    a[t7]:=t9

    t10:=4*j

    a[t10]:=xgoto B2

    t11:=4*i

    x:=a[t11]

    t12:]4*i

    t13:=4*n

    t14:=a[t1]

    a[t12]:=t14

    t15:=4*n

    a[t15]:=x

    j:=j-1

    t4:=4*j

    t5:=a[t4]

    if t5>v goto B3

    B1

    B2

    B3

    B4

    B5 B6

  • 7/31/2019 16 marks Q &A

    3/10Unit V 16 mark Q & A 3

    The following figure, shows the result of eliminating both global and local common subexpressions

    Copy propogation

    Assignments of the form f:=g are called copy statements (or) copies .

    While eliminating common sub expressions these statements are introduced

    Copies introduced during common sub expression elimination , Consider , block B5

    i :m-1

    j:=n

    t1:=4*n

    v:=a[t1]

    i:=i+1

    t2:=4*i

    t3:=a[t2]

    if t3=j goto B6

    x:=t3

    a[t2]:=t5

    a[4]:=x

    goto B2

    x:=t3

    a[t2]:=t5a[4]:=x

    goto B2

    j:=j-1

    t4:=4*j

    t5:=a[t4]

    if t5>v goto B3

    B6

    B2

    B3

    B5

    B4

    B1

    a:=d+e a:=d+e

    c:=d+e

    t:=d+e

    a:=t

    t :=d+e

    b:=t

    e:=t

  • 7/31/2019 16 marks Q &A

    4/10Unit V 16 mark Q & A 4

    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

    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.

    Here, limit-2 is loop invariant computation

    x:=t3

    a[t2]:=t5

    a[t5]:=x

    goto B2

    x:=t3

    a[t2]:=t5

    a[t4]:t3

    goto B2

    x:=t3

    a[t2]:=t5

    a[t4]:=t3

    goto b

    a[t]2]:=t5

    a[t4]:=t3

    goto B2

    While (i

  • 7/31/2019 16 marks Q &A

    5/10Unit V 16 mark Q & A 5

    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

    Strength reduction applied to 4*j in block B3.

    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.

    B5B5

    i :m-1j:=nt1:=4*n

    v:=a[t1]

    j:=j-1t4:=4*jt5:=a[t4]

    if t5>v goto B3

    if i>=j goto B6

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

    t4 := 4 *

    j:=j-1t4:=4*jt5:=a[t4]

    if t5>v goto B3

    if i>=j goto B6

    B1

    B2

    B3

    B4

    B6 B6

    B4

    B3

    B2

    B1

  • 7/31/2019 16 marks Q &A

    6/10Unit V 16 mark Q & A 6

    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.

    i:=m-1

    j:=n

    t1:=4*n

    v:=a[t1]

    t2:=4*i

    t4:=4*

    t2 :=t2+4

    t3:=a[t2]

    If t3v goto B3

    if t2>=t4 goto B6

    a[t2]:=t5

    a[t4]:=t3

    goto B2

    t14:=a[t1]

    a[t2]:=t14

    a[t1]:=t13

    B6B5

    B3

    B2

    B1

    B4

  • 7/31/2019 16 marks Q &A

    7/10Unit V 16 mark Q & A 7

    ---------------------------------------------------------------------------------------------------------------------3. Explain runtime storage organization in detail.

    Code

    Static Data

    Stack

    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 thatprocedure exited.

    Size of each field can be determined at compile time (Although actual location of theactivation 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.

    return value

    actual parameters

    optional control link

    optional access link

    saved machine status

    local data

    temporaries

    --------------------------------------------------------------------------------------------------------------------

    Memory locations for code aredetermined at compile time.

    Locations of static data can also bedetermined at compile time.

    Data objects allocated at run-time.(Activation Records)

    Other dynamically allocated dataobjects at run-time. (For example,malloc area in C).

    The returned value of the called procedure is returnedin 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 callingprocedure to supply parameters to the called procedure.

    The optional control link points to the activation recordof the caller.

    The optional access link is used to refer to nonlocal dataheld in other activation records.

    The field for saved machine status holds information aboutthe state of the machine before the procedure is called.

    The field of local data holds data that local to an executionof a procedure..

    Temporay variables is stored in the field of temporaries.

  • 7/31/2019 16 marks Q &A

    8/10Unit V 16 mark Q & A 8

    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

    --------------------------------------------------------------------------------------------------------------------

    return value

    actual parameters

    optional control link

    optional access link

    saved machine status

    local data

    temporaries

    Callers Activation Record

    Callers Responsibility

    Callees Activation Record

    Callees Responsibility

  • 7/31/2019 16 marks Q &A

    9/10Unit V 16 mark Q & A 9

    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 textalone 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 activationrecord)

    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 programsS ::= id:=expression | S;S | ifexpression then S else S | do S while expression

    gen[S] = {d}

    kill[S] = Da-{d}.

    Da is the set of all definitions of a.

    out[S] = gen[S] + (in[S]-kill[S])

    S d: a := b + c

    S

    S1

    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]

  • 7/31/2019 16 marks Q &A

    10/10Unit V 16 mark Q & A 10

    -------------------------------------------------------------------------------------------------------------------

    S 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]

    SS

    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]