You are on page 1of 11

STUDY MATERIAL

SUBJECT COURSE SEMESTER UNIT STAFF


SYLLABUS

1/11

: : : : :

INTRODUCTION TO COMPILER DESIGN

III-BCA V

V
C. Gayathri

UNIT V
Code generation : issues in design of code generator target machine run-time storage management basic blocks and flow graphs. Code optimization : introduction principle sources of optimization. CODE GENERATING To transform the intermediate code into a form from which more efficient target code can be produced. front end intermediate code code intermediate optimizer code Symbol table ISSUES IN THE DESIGN OF A CODE GENERATOR Input to the Code Generator The input to the code generator consists of the intermediate representation of the source program produced by the front end, together with information in the symbol table that is used to determine the run-time addresses of the data objects denoted by the names in the intermediate representation. Linear representations such as postfix notations. Three-address representations such as quadruples. Virtual machine representations such as syntax trees and dags. To code generation the front end has scanned, parsed and translated the source program into intermediate representation. Its input is free of errors. code target generator program

Source Program

Target Programs The output of the code generator is the target program. The output take variety of forms. Absolute machine language. Re-locatable machine language or assembly language. Producing an absolute machine language program can be placed in a fixed location in a memory and immediately executed.

Producing a re-locatable machine language program allows subprograms to be compiled separately. Asset of re-locatable object can be linked together and loaded for execution by a linking loader. Producing an assembly language program makes the process of code generation. To generate symbolic instructions and use the macro facilities of the assembler to help generate code.

2/11

Memory Management If machine code is being generated, labels in three-address statements have to be converted to addresses of instructions.

Instruction Selection The instruction set of the target machine determines the difficulty of instruction selection. The uniformity and completeness of the insertion set are important factors. If the target machine does not support each data type in a uniform, then each exception to the general rule require special handling. Mov b, R0 For example, the sequence of statements, Add c, R0 a := b + c Mov R0, a d := a + e will be translates Add e, R0 Mov R0, d Register Allocation Instructions involving register operands are shorter and faster than those involving operands in memory. The use of registers is subdivided into two sub problems During register allocation, to select the set of variables that will reside in registers at a pointer in the program. During a subsequent register assignment phase, to pick the specific register that a variable will reside in. NP complete problem, to avoid this problem by generating code for the three-address statements in the order in which they have been produced by the intermediate code generator. The Target Machine Target computer is a byte-addressable machine with four bytes to a word and n general purpose registers R0, R1, , Rn-1. It has two address instructions, op source, destination In which op is an op-code, and source and destination are data fields. These fields are not long enough to hold memory address and these instructions are instructions are specified by continuing registers and memory locations with address modes. The address modes associated with assembly language and costs: NODE Absolute FORM M ADDRESS M ADDED COST 1

Register Indexed Indirect register Indirect indexed Literal Example: Mov R0, M Mov 4(R0), M Mov *4(R0), M Mov #1, R0 Instruction Set

R C(R) *R *C(R) #C

R C + Contents(R) Contents(R) Contents(C + contents (R)) C

0 1 0 1 1

3/11

to store the contents of register R0 into memory location M. to state value contents ( 4 + contents(R0)) into memory location M. to store the value contents ( contents (4 + contents (R0))) into memory location M. to store the constant 1 into register R0.

The cost of an instruction to be one plus the costs associated with the source and destination address modes. This cost corresponds to the length of the instruction. Address modes involving registers have cost zero, while those with a memory location or literal in them have cost one, because such operands have to be stored with the instruction. By minimizing the instruction length to minimize the time taken to perform the instruction

Example : Mov R0, R1 Mov R5, M Add #1, R3 Sub 4(R0), *12(R1) ( contents ( contents destination *12(R1) to copy the contents of register R0 into register R1. to copy contents of register R5 into memory location M. to add the constant 1 to the contents of register R3. to store the value contents ( (12 + contents (R1))) - ( contents (4 + contents (R0))) into the

Run-Time Storage Management An execution of procedure is kept in a block of storage called an activation record .

STORAGE-ALLOCATION STRATEGIES Static allocation The position of an activation record in a memory is fixed at compile time.

Stack allocation A new activation record is pushed onto the stack for each execution of a procedure. The record is popped when the activation ends. Run-time memory is divided into areas for code, static data, and a stack. Run-time allocation and de-allocation of activation records occurs as part of the procedure. Call and return sequences 1. call 2. return

3. halt, and 4. action , a place holder for other statements. In static allocation, the intermediate code is implemented by a sequence of two target machine instructions, mov # here+20, callee.static-area goto callee.code-area The attributes callee.static-area and callee.code-area are constants referring to the address of the activation record and the first instruction for the called procedure. A return from procedure callee is implemented by goto *callee.static-area which transfers control to the address saved at the beginning of the activation record. In stack allocation, relative addresses in an activation record can be taken as offsets from any known position in the activation record. Example: 1) mov #stackstart, sp /* initialize the stack */ halt /* terminate execution */ 2) add #caller.recordsize, sp mov #here + 16, *sp goto callee.code-area the attribute caller.recordsize represents the size of an activation record. The source # here +16 in saved in the address pointed to by sp. Runtime Address for Names Advantage It makes the compiler more portable. The front end need not be changed even if the compiler is moved to a different machine where a different run-time organization is needed. Generating the specific sequence of access steps while generating intermediate code can be significant advantage in an optimizing compiler. BASIC BLOCKS AND FLOW GRAPHS A graph representation of three-address statements, called a flow graph.

4/11

Basic Blocks A basic block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without halt or possibility of branching except at the end. A name in basic block is said to be live at a given point if its value is used after that point in the program. : : : : To determine the set of leaders, the first statements of basic blocks. Rules: i. The first statement is a leader. ii. Any statement that is the target of a conditional or unconditional Partition into basic blocks A sequence of three-address statements A list of basic blocks with each three address statements in exactly one blocks.

Algorithm Input Output Method 1.

2.

goto is leader. iii. Any statement that immediately follows a goto or conditional goto statement is leader. 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.

5/11

Transformation on Basic Blocks Two basic blocks are said to be equivalent if they compute the same set of expressions A number of transformations can be applied to a basic block without changing the set of expressions computed by the block.

Optimizer To use transformations to rearrange the computations. To reduce the overall running time or space requirement of the final target program. Local transformations that can be applied to basic blocks. The structure preserving transformations and the algebraic transformations.

Structure-Preserving Transformations 1. 2. 3. 4. common sub expression elimination dead-code elimination renaming of temporary variables interchange of two independent adjacent statements a := b b := a c := b d := a + + c d c d a := b + c b := a - d c := b + c d := b

Example : 1.

equivalent to

2. 3. 4.

x := y + z to remove without changing the value of the basic block. t := b + c where, t is a temporary u := b + c where u is a new temporary and change the instances of t to u. t 1 := b + c t 2 := x + y, to interchange the two statements without affecting the value of the block if and only if neither x nor y is t 1 and neither b nor c is t 2 .

Algebraic Translation If can be used to change the set of expressions computed by a basic block into an algebraically equivalent set. Example : x := x + 1 , to eliminate from a basic block without changing the set of expressions it computes.

Flow Graphs The flow of control information to the set of basic blocks making up a program by constructing a directed graph called a flow graph. There is a directed edge from block B 1 to block B 2 if B 2 can immediately follow B 1 in

execution sequence. 1. There is a conditional or unconditional jump from the last statement of B 1 to the first statement of B 2 . 2. B 2 immediately follows B 1 in the order of the program and B 1 does not end in an unconditional jump. B 1 is a processor of B 2 , and B 2 is a successor of B 1 . Representation of Basic Blocks Basic blocks can be represented by a variety of data structures. Each basic block can be represented by a record consisting of the count of the number of quadruples in the block, followed by a pointer to the leader of the block and by the lists of predecessors and successors of the block. An edge of the flow graph from block B to block B does not specify the conditions under which control flows from B to B.

6/11

Loops 1. In a flow graph, a loop is a collection of nodes in a flow graph. i. All nodes in the collection are strongly connected, from any node in the loop to any other, there is a path of length one or more, within the loop. ii. The collection of nodes has a unique entry, a node in the loop such that the only way to reach a node of the loop from a node outside the loop is to first go through the entry. A loop that contains no other loops is called an inner loop. CODE OPTIMIZATION The code produced by compiling algorithms can be made to run faster or take less space or both. This improvement is achieved by program transformations called optimizations. Compilers that apply code-improving transformations are called optimizing compilers. Machine-independent optimizations, program transformations that improve the target code without taking into any properties of the target machine. Machine-dependent optimizations, register allocation and utilization of special machineinstruction sequences.

Criteria for Code Improving Transformations i. A translation must preserve the meaning of the programs. An optimization must not change the output produced by a program for a given input, or cause an error. ii. A translation must, on the average, speedup programs by measurable amount. To reduce the space taken by the compiled code, the size of the code has less than one. Optimization may slow down slightly, on the average it improves transformations. iii. A transformation must be worth the effect. It does not make sense for a compiler writer to expend the effort to implement a codeimproving transformations and to have the compiler expend the additional time compiling source programs if this effort is not repaid when the target programs are executed. Performance To improve the program at all levels, from the source level to the target level.

At each level, the available options fall between the two extreme of finding a better algorithm and of implementing a given algorithm. A compiler can replace a sequence of operations by an algebraic sequence of operations and thereby reduce the running time of a program. A compiler can be relied upon to generate efficient code, then the user can concentrate on writing clear code.

7/11

Source Code

front end

intermediate code compiler can improve loops procedure calls address calculations

code generator

target code compiler can use registers select instruction do peep hole-transformations

User can profile Program change Algorithm transform Loops

An Organization for an Optimizing Compiler Front End Control-flow Analysis Fig. code optimizer Data-flow Analysis code generator Transfor mations

Organization of the code optimizer

The code generator produces the target program from the transformed intermediate code. To analyze and transform a program do not change the level, the transformation of intermediate code using the organization. The code-improvement phase consists of control flow and data flow analysis followed by the application of transformations. In the code optimizer, programs are represented by flow graphs, in which edges indicate the flow of control and nodes represent basic blocks.

Advantages 1. The operations needed to implement high-level constructs are made explicitly in the intermediate code, it is possible to optimize them. 2. the intermediate code can be independent of the target machine, the optimizer does not have to change, if the code generator is replaced by one for a different machine. THE PRINCIPAL SOURCES OF OPTIMIZATION A transformation of a program is called local if it can be performed by looking only at the statements in a basic block, otherwise it is called global. It can be performed at both the local and global levels.

Examples of functions-preserving transformations i. ii. iii. iv. common sub expression elimination copy propagation dead-code elimination constant folding

8/11

i.

Common Sub-expressions An occurrence of an expression E is called a common sub-expression if E was previously computed, and the values of variable in E have not changed since the previous computation. To avoid re-computing the expression if use the previously computed value. For example, = b1 + c1 = b2 * c2 = b1 + c1 = a5 t6 := 4 * I x := a[t6] t7 := 4 * I t8 := 4 * j t9 := a[t8] a[t7] := t9 t10 := 4 * j a[t10] := x (a) after t6 := 4 * i x := a[t6] t8 := 4 * j t9 := a[t8] a[t6] := t9 a[t8] := x goto B2 goto B2 (b) before a[t4] := x goto B2 a[t2] := t14 a[t1] := x

a1 a2 a3 a4

a1 = b1 + c1 a2 = b2 * c2 a4 = a5

Fig . Local common sub expression elimination B1 i := m-1 j := n t1 := 4 * n v := a[t1] B2 i := I +1 t2 := 4 * I t3 := a[t2]


if t3 < v goto B2

B3 j := j 1 t4 := 4 * j t5 := a[t2] if t5 > v goto B3 B4 if i >= j goto B6 x := t3 B5 a[t2] := t5 B6 x := t3 t14:= a[t1]

The assignments to t7 and t10 have the common sub expressions 4 * i and 4 * j respectively, on the right side in figure(a). They have been eliminated in figure(b), by using t6 instead of t7 and t8 instead of t10. To eliminate both global and local common subexpressions from blocks B5 and B6. After local common sub-expressions are eliminated, B5 still evaluates 4 * i and 4 * j. Both are common sub-expressions, the three statements t8 := 4 * j ; t9 := a[t8];

a[t8] := x in a[t4] := x using t4 B5 can be replaced by computed in block B3. t9 := a[t4]; Fig. B5 and B6 after common sub expression elimination ii. Copy propagation

9/11

To improve by eliminating using transformations Assignments of the form called copy statements or copies. When the common sub expression is eliminated, for example, c:= d + e, the algorithm uses a new variable t to hold the value of d + e. b := b + e t := d + e a := t c := t t := b + e b := t

a := d + e

c := d + e iii. iv.

Fig. Copies during common sub expression elimination The control reach c: = d+e either after the assignment to a or after the assignment to b. The opportunity to eliminate the assignment. Dead-Code Elimination A variable is line at a point is program if its value can be used subsequently , otherwise , it is dead at that point. Deducing at compile time that the value of an expression is a constant and using the constant instead is known as constant folding. One advantage of copy propagation is that it turns the copy statement into dead code. Copy propagation followed by dead-cod elimination removes the assignment and transforms. Loop Optimization The running time of a program may be improved if decrease the number of instructions in an inner loop, even if increase the amount of code outside that loop. The three techniques for loop optimizations: a. code motion which moves code outside a loop. b. induction variable elimination which apply to eliminate from inner loops. c. reduction in strength which replaces an expensive operation by a cheaper one.

a) Code Motion: To decrease the amount of code in a loop is code motion. This transformation takes an expression that yields the same result independent of the number of times a loop is executed is called a loop in varient computation and places the expression before the loop. Note :

The notion before the loop assumes the existence of an entry for the loop.

Example: 1) 2) while ( i < = limit 2 ) t = limit-2 ; while ( i <= t )

10/11
loop-invariant computation. code motion.

b) Induction Variables and Reduction Strength: The value of j decrease by 1 , t4 decreases by 4 because 4 * j is assigned to t4. The identifiers re called induction variables. When there are two or more induction variables in a loop, by the process of inductionvariable elimination. i := m-1 j := n t1 := 4 * n v := a[t1] B1 i := m-1 j := n t1 := 4 * n v := a[t1] t4 := 4 * i B1

B2 B2 B3 j := j 1 B3 t4 := 4 * j t5 := a[t4] if t5 > v goto B3 B4 if i >= j goto B6 B5 (a) B6 before j := j 1 t4 := t4 - 4 t5 := a[t4] if t5 > v goto B3 if i >= j goto B6 B4 B5 (b) after B6

Fig. Strength reduction applied to 4 * j in block i := m-1 j := n t1 := 4 * n v := a[t1] t4 := 4 * j B1

t2 := t2 + 4 B2 t3 := a[t2] If t3 < v goto B2 t4 := t4 4 B3 t5 := a[t4] if t5 > v goto B3 B4 if t2 >= t4 goto B6

a[t2] := t5 a[t4] := t3 goto B2

B5

t14:=a[t1] a[t2] := t14 a[t1] := t3

B6

11/11

Fig. Flow graph after induction variable elimination

You might also like