Upload
rahul-jha
View
220
Download
0
Embed Size (px)
Citation preview
7/26/2019 CD Unit8
1/11
Compiler Design (SHG)
80
Unit-8: Code Optimization and CodeGeneration
8.1 Code Generation
The final phase of a compiler is the generation of target code which consistsof relocatable assembly code or machine code. For each variable used in theprogram memory locations are allocated.
The code generator translates intermediate representation of the sourceprogram into a sequence of machine instructions. The primary goal of acode generator is to produce correct target code which should be efficient.
Code efficiency can be achieved by fully exploiting the resources of the
target machine and the powers of its instruction set. It must also use theavailable registers efficiently.
8.1.1 Problems in Code generation
It might appear that the task of code generation is relatively easy.However, difficulties arise in attempting to perform the computationrepresented by the intermediate language program efficiently, usingthe available instructions of the target machine. There are three mainsources of difficulties:
Deciding what machine instructions to generate,Deciding in what order the computations should be done, andDeciding which registers should be used.
What Instructions To be Generated?
Most machines permit certain computations to be done in a variety ofways. For ex, if our target machine has an add-one-to-storageinstruction (AOS), then for the three-address statement A = A + 1 wemight generate the single instruction
AOS A;
rather than the more obvious sequence of instructions
LOAD AADD 1STORE A
Deciding which machine code sequence is best for a given three
address construct may require extensive knowledge about thecontext in which that construct appears.
7/26/2019 CD Unit8
2/11
Compiler Design (SHG)
81
In What Order Computations to be Performed?
Some computation orders require fewer registers to hold intermediateresults than others. Picking the best order is a very difficult problemin general. Initially, we shall generate code for the three-addressstatements in the order in which they have been produced by thesemantic routines.
What Registers Should be Used?
Deciding the optimal assignment of registers to variables is difficult,even with single-register quantities. The problem becomes furthercomplicated because certain machines requires register-pairs forsome operands and results.
For example, in the IBM System/370 machines, integermultiplication and integer division involves register pairs. Themultiplication instruction is of the form:
M X, Y
where X, the multiplicand, refers to the even register of an even/oddregister pair. The multiplicand itself is taken from the odd register ofthe pair. Y represents the multiplier. The product occupies entireeven/odd register pair.
The division instruction is of the form
D X, Y
where the 64-bit dividend occupies an even/odd register pair whoseeven register is X. Y represents the divisor. After division, the evenregister holds the remainder and the odd register the quotient.
8.2 Forms of Object Code
An object code may take on a variety of forms:
An absolute machine language program, A relocatable machine language program, An assembly language program, or A High Level Language program
Producing an absolute Machine Language program as output has theadvantage that it can be placed in a fixed location in memory andimmediately executed. A small program can be compiled and executed in a
few seconds.
7/26/2019 CD Unit8
3/11
Compiler Design (SHG)
82
Producing a relocatable Machine Language program as output allowssubprograms to be compiled separately. A set of relocatable object modulescan be linked together and loaded for execution by a linking loader.
Producing an Assembly Language program as output makes the process of
code generation somewhat easier. We can generate symbolic instructionsand use the macro facilities of the assembler to help generate code.
Producing a High Level Language program as output simplifies codegeneration even further. For example, FORTRAN is the output of theALTTRAN compiler as well as a variety of structured FORTRAN
preprocessors such as RATFOR.
8.3 Code Optimization
Do it yourself
8.4 Sources of Optimization
Inner loops.
Language implementation details inaccessible to the user.
Further optimizations (Assignment statement)
8.5 Loop Optimization
Consider the following code fragment to compute the dot product of twovectors A & B of length 20:
beginPROD = 0;I = 1;do
beginPROD = PROD + A[I] * B[I];I = I + 1;
endwhile I
7/26/2019 CD Unit8
4/11
Compiler Design (SHG)
83
(6)T4= address(B)4(7)T5= T4[T1] // COMPUTE B[I](8)T6= T3* T5(9)PROD = PROD + T6(10)I = I + 1
(11)
If I
7/26/2019 CD Unit8
5/11
Compiler Design (SHG)
84
B1
B2
To the beginning of the blockwith statements following
8.5.2 Code Motion
The execution time of a program may be improved if we reduce thelength of one of its loops, especially an inner loop. Here the
computation that yields the same result independent of the numberof times through the loop, that is a loop invariant computation, isremoved and is placed before the loop. This is called as code motion.
B1
B3
B2
PROD = 0
I = 1
(3) T1= 4 * I(4) T2= address(A)4(5) T3= T2[T1](6) T4= address(B)4(7) T5= T4[T1](8) T6= T3* T5(9) PROD = PROD + T6(10) I = I + 1(13)If I
7/26/2019 CD Unit8
6/11
Compiler Design (SHG)
85
8.5.3 Induction Variables
Those variables that constitute an arithmetic Progression series areknown as induction variables. For example, I = 1, 2, 3, .. , 20
T1 = 4, 8, 12, . , 20
Here T1can be replaced by T1= 0, T1= T1+ 4.
The process of removal of induction variables will actually reduce thetotal number of instructions as well as speed up the loop. Thisprocess is called as induction variable elimination.
B1
B3
B4
B2
8.5.4 Reduction in Strength
The multiplicity step T1 = 4 * I was replaced by an addition stepT1 = T1 + 4. This replacement would speed up the object code ifaddition takes less time than multiplication, as in case in manymachines. The replacement of an expensive operation by a cheaperone is termed as reduction in strength.
(3) T1= 4 * I(5) T3= T2[T1](7) T5= T4[T1](8) T6= T3* T5(9) PROD = PROD + T6(10) I = I + 111 If I
7/26/2019 CD Unit8
7/11
Compiler Design (SHG)
86
8.6 Register Allocation and Assignment
Basic approachDo yourself
Global register allocation
8.7 Peephole Optimization
Peephole optimization is a technique used in many compilers, in connectionwith the optimization of either an intermediate or an object code. It works bylooking at the intermediate or object code within a small range ofinstructions. The code in the peephole need not be contiguous, althoughthere are some implementations that require this type of optimization.
It is a characteristic of peephole optimization that each improvement mayspawn opportunities for additional improvements. Thus, repeated passesover the code are necessary to get the maximum benefit from the peepholeoptimization.
Redundant LOADS and STORESUnreachable CodeMultiple JumpsAlgebraic Specification as done in class
Reduction in strengthUse of Machine Idioms
7/26/2019 CD Unit8
8/11
Compiler Design (SHG)
87
7/26/2019 CD Unit8
9/11
Compiler Design (SHG)
88
7/26/2019 CD Unit8
10/11
Compiler Design (SHG)
89
7/26/2019 CD Unit8
11/11
Compiler Design (SHG)
90