0% found this document useful (0 votes)
20 views28 pages

Compiler Construction Week 15

Uploaded by

neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views28 pages

Compiler Construction Week 15

Uploaded by

neha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Compiler Constructions

Zulfiqar Ali
UIT University
Week 15 – Code Optimization
• Intermediate Code Optimization
Intermediate Code Optimization
• The code optimization is the synthesis phase is a program
transformation technique, which tries to improve the intermediate
code by making it consume fewer resources (i.e. CPU, Memory) so
that faster-running machine code will result. Compiler optimizing
process should meet the following objectives .
– The optimization must be correct, it must not, in any way, change the
meaning of the program.
– Optimization should increase the speed and performance of the program.
– The compilation time must be kept reasonable.
– The optimization process should not delay the overall compiling process.
Types of Code Optimization:
• The optimization process can be broadly classified into two types:
– Machine Independent Optimization: This code optimization phase attempts to
improve the intermediate code to get a better target code as the output. The
part of the intermediate code which is transformed here does not involve any
CPU registers or absolute memory locations.

– Machine Dependent Optimization: Machine-dependent optimization is done


after the target code has been generated and when the code is transformed
according to the target machine architecture. It involves CPU registers and may
have absolute memory references rather than relative references. Machine-
dependent optimizers put efforts to take maximum advantage of the memory
hierarchy.
Different ways of Code Optimization
• Compile Time Evaluation
– Variable Propagation
– Constant Propagation:
– Constant Folding
– Copy Propagation
• Common Sub Expression Elimination
• Dead Code Elimination
• Unreachable Code Elimination
• Function Inlining
• Function Cloning
• Strength Reduction
• Loop Optimization Techniques
Compile Time Evaluation
Variable Propagation
Constant Propagation:
• If the value of a variable is a constant, then replace the
variable with the constant. The variable may not always be a
constant.
Constant Folding
• Consider an expression : a = b op c and the values b
and c are constants, then the value of a can be
computed at compile time.
Copy Propagation
• It is extension of constant propagation.
• After a is assigned to x, use a to replace x till a is assigned
again to another variable or value or expression.
• It helps in reducing the compile time as it reduces copying.
Common Sub Expression Elimination
• In the following example, a*b and x*b is a
common sub expression.
Dead Code Elimination
• The statements of the code which either never executes or
are unreachable or their output is never used are eliminated.
• In order to find the dead variables, a data flow analysis should
be done.
Code Before Optimization Code After Optimization
i=0;
if (i == 1)
{ i=0;
a=x+5;
}
Function Inlining:
• Here, a function call is replaced by the body of the function
itself.
• This saves a lot of time in copying all the parameters, storing
the return address, etc.
Function Cloning
• Here, specialized codes for a function are
created for different calling parameters.
• Example: Function Overloading
Strength Reduction
• Strength reduction means replacing the high
strength operator with a low strength.
Code Before Optimization Code After Optimization
B=Ax2 B=A+A
Loop Optimization Techniques
• Code Motion or Frequency Reduction
• The evaluation frequency of expression is reduced.
• The loop invariant statements are brought out of the loop.
Target Code Generation
• Target code generation is the final Phase of Compiler.
– Input : Optimized Intermediate Representation.
– Output : Target Code.
– Task Performed : Register allocation methods and
optimization, assembly level code.
– Method : Three popular strategies for register allocation
and optimization.
– Implementation : Algorithms.
• Target code generation deals with assembly language to
convert optimized code into machine understandable format.
Target code can be machine readable code or assembly code.
Each line in optimized code may map to one or more lines in
machine (or) assembly code, hence there is a 1:N mapping
associated with them .
• Computations are generally assumed to be performed
on high speed memory locations, known as registers.
Performing various operations on registers is efficient
as registers are faster than cache memory. This feature
is effectively used by compilers, However registers are
not available in large amount and they are costly.
Therefore we should try to use minimum number of
registers to incur overall low cost.
Optimized code
Register Allocation
• Register allocation is the process of assigning
program variables to registers and reducing the
number of swaps in and out of the registers.
Movement of variables across memory is time
consuming and this is the main reason why registers
are used as they available within the memory and
they are the fastest accessible storage location.
Register Allocation
Reference
• Compilers: Principles, Techniques, and Tools, A. V.
Aho, R. Sethi and J. D. Ullman, Addison-Wesley, 2nd
ed., 2006.
• https://www.geeksforgeeks.org/target-code-
generation-in-compiler-design/
THANK YOU

You might also like