Docsity

Compiler Code Optimization: Storage Allocation, Tree-Walk Generation, and Array Addressing, Study notes of Computer Science

Toggle sidebar
bg1
1
CS5363 Appendix 2
A compiler can implement some source language constructs on a given target machine in
many ways. Code shape refers to the differences and it has a strong impact both on the
behavior of the compiled code and on the ability of the optimizer and back end to
improve it.
Storage allocation
If x is declared locally in procedure p, and
its value is not preserved across distinct invocations of p,
then assign it to procedure-local storage;
if its value is preserved across invocations of p,
then assign it to procedure-static storage
If x is declared as globally visible,
then assign it to global storage.
If x is allocated under program control,
then assign it to the run-time heap.
A local variable can be kept in a register as long as its address is never taken, its value is
never used in a nested procedure, and it is not passed as a call-by-reference parameter to
another procedure. The assignment b = &a creates a second name of a. Neither is kept in
a register unless code between two references does not update a or *b. A value that can
be kept in a register is called an unambiguous value; a variable that can have more than
one name is called an ambiguous value.
Tree-walk Code Generation
Example: x – 2 * y
(postorder walk of the parse tree)
lo adI @x => r1 //load offset
loadAO rarp, r1 => r2 //get x using activation record pointer and offset
loadI 2 => r3
loadI @y => r4
loadAO rarp, r4 => r5 //get y using activation record pointer and offset
mult r3, r5 => r6
sub r2, r6 => r7
There are 8 register used in the above code but the number of registers can be reduced by
sharing offset and variable in one register!
loadI @x => r1
loadAO rarp, r1 => r1
loadI 2 => r2
loadI @y => r3
loadAO rarp, r3 => r3
mult r2, r3 => r2
sub r1, r2 => r2
There is no need to load x before 2*y is computed. Thus, 2*y is first evaluated.
pf3
pf4
pf5

Partial preview of the text

Download Compiler Code Optimization: Storage Allocation, Tree-Walk Generation, and Array Addressing and more Study notes Computer Science in PDF only on Docsity!

CS5363 Appendix 2 A compiler can implement some source language constructs on a given target machine in many ways. Code shape refers to the differences and it has a strong impact both on the behavior of the compiled code and on the ability of the optimizer and back end to improve it.

Storage allocation If x is declared locally in procedure p, and its value is not preserved across distinct invocations of p, then assign it to procedure- local storage; if its value is preserved across invocations of p, then assign it to procedure-static storage If x is declared as globally visible, then assign it to global storage. If x is allocated under program control, then assign it to the run-time heap.

A local variable can be kept in a register as long as its address is never taken, its value is never used in a nested procedure, and it is not passed as a call-by-reference parameter to another procedure. The assignment b = &a creates a second name of a. Neither is kept in a register unless code between two references does not update a or *b. A value that can be kept in a register is called an unambiguous value; a variable that can have more than one name is called an ambiguous value.

Tree-walk Code Generation Example: x – 2 * y (postorder walk of the parse tree)

loadI @x => r1 //load offset loadAO rarp, r1 => r2 //get x using activation record pointer and offset

loadI 2 => r loadI @y => r loadAO rarp, r4 => r5 //get y using activation record pointer and offset mult r3, r5 => r sub r2, r6 => r

There are 8 register used in the above code but the number of registers can be reduced by sharing offset and variable in one register! loadI @x => r loadAO rarp, r1 => r loadI 2 => r loadI @y => r loadAO rarp, r3 => r mult r2, r3 => r sub r1, r2 => r

There is no need to load x before 2y is computed. Thus, 2y is first evaluated.

loadI @y => r loadAO rarp, r1 => r loadI 2 => r mult r2, r1 => r loadI @x => r loadAO rarp, r2 => r sub r2, r1 => r

However, evaluating the right child first is not a general solution!

A call-by-reference parameter passed in the AR requires one additional indirection.

loadI @x => r loadAI rarp, r1=> r load r2 => r

Many linkage conventions pass the first few parameters in registers. To evaluate a function call, the compiler simply generates the calling sequence needed to invoke the function and emits the code necessary to move the returned value to a register. The function may have side effects that modify the values of variables used in the expression.

Code motion takes an expression that yields the same result independent of the number of times a loop is executed (a loop-invariant computation).

while ( i <= limit -2) /* statement does not change limit */ Code motion will result in the equivalent of

t = limit -2; while ( i <= t) /* statement does not change limit or t */

The result of evaluation on the right- hand side of an assignment as an rvlaue and the result of evaluation of the left-hand side of an assignment as an lvalue.

The compiler should use commutativity and associativity to improve the quality of code it generates. However, d ue to limitations in precision, floating-point numbers on a computer represent only a subset of the real numbers, and floating-point operations do not preserve associativity. As a result, compilers should not reorder floating-point computations unless the language definition specifically allows it.

Example: we can assign floating-point values to x, y, and z such that x, y < z, z- x = z, and z-y=z, but z-(x+y) <> z.

Numerical encoding and positional encoding are used to evaluate Boolean expressions. Numerical encoding assigns true or false that are operands for Boolean instructions: and, or, not. Positional encoding avoids assigning an actual value to the expression until and

loadI @V => r@V // get V’s starting address subI ri, rlow => r1 // offset multI r1, rw => r2 // get offset add r@V, r2 => r3 // address of V[i] load r3 => r4 // value of V[i]

The calculation can be optimized using false zero. @V + (i – low) w = @V + iw – low *w = (@V – low w) + iw Let @V0 = @V – low *w (this is a constant!), called false zero. If the w is 4, the above code can be optimized as follows:

loadI @V0 => r@V lshiftI ri, 2 => r1 // i * w loadAO r@V0, r1 => rv // value of V[i]

Array Storage Layout: row-major order (C), column-major order (Fortran) and indirection vectors (BCPL and Java). Row- major order stores array elements such as A[1, 1], A[1, 2], …,A[2, 1], A[2, 2], …,A[3,3]. Column-major order stores array elements such as A[1, 1], A[2, 1], A[3, 1], A[1, 2], A[2, 2], A[3, 2], A[1, 3],…, A[3, 3]. Indirection vectors, each row has its own contiguous storage. Within a row, elements are addressed as in a vector.

Referencing an Multi-dimens ional Array Element Let A[low1...high1, low2…high2] be a two-dimensional array and stored in row- major order. We want to calculate the address of A[ i, j]. The len1 = high1- low1+1 and len2 = high2- low2+1.

The offset of a[i, j] = (i- low1)len2w+(j- low2)w. The address of A[i, j] is @A+ (i- low1)len2w+(j-low2)w.

@A+ (i-low1)len2 w+(j-low2)w = @A – low1len2w –low2w + ilen2w + jw Let @A0 = @A – low1len2 w-low2w be the false zero. The address of A[i, j] = @A0 + (ilen2 + j)w.

The code to access A[i, j] is loadI @A0 => r@A0 // false zero multI ri, len2 => r1 // ilen add r1, j => r2 // i len2 + j lshiftI r2, 2 => r3 // assume w = 4 loadAO r@A0, r3 => rA // value of A[i, j]

Indirection vectors To access B[i, j, k], the compiler uses the starting address of B and i to find the vector for the subarray B[i, *, *]. Then it uses the results and j to find the address of B[i, j, *] and finally uses k and element length w to find B[i, j, k].

loadI @B0 => r@B0 // starting address of B

multI ri, 4 => r1 // assume pointer length is 4 loadAO r@B0, r1 => r2 //get @B[i, *, *] multI rj, 4 => r loadAO r2, r3 => r4 // get @B[i, j, *] multI rk, 4 => r5 // element length is 4 loadAO r4, r5 => rB // value of B[i, j, k]

Array is typically passed to a called procedure through call-by-reference. This will reduce cost in the callee’s AR. However, the dimension of an array has to be collected and passed to the callee. A parameter slot in callee’s AR is used to point to a descriptor called dope vector. The dope vector contains the starting address of the array (typically false zero), and length of each dimension.