Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Code Optimization and Target Code Generation

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Code Optimization &

Target Code Generation


Code Optimization
CODE OPTIMIZATION
The code optimization in 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.
CODE OPTIMIZATION
When to Optimize?
Optimization of the code is often performed at the end of the development stage since it
reduces readability and adds code that is used to increase the performance.

Why Optimize?
Optimizing an algorithm is beyond the scope of the code optimization phase. So the program
is optimized. And it may involve reducing the size of the code.

So optimization helps to:


 Reduce the space consumed and increases the speed of compilation.
 Manually analyzing datasets involves a lot of time. Hence we make use of software like
Tableau for data analysis. Similarly manually performing the optimization is also tedious
and is better done using a code optimizer.
 An optimized code often promotes re-usability
TYPES OF CODE OPTIMIZATION
The optimization process can be broadly classified into two types :

1) 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.
Example:

This code involves repeated It should not only save the CPU cycles,
assignment of the identifier item but can be used on any processor.
TYPES OF CODE OPTIMIZATION
The optimization process can be broadly classified into two types :

(2) 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.
CODE OPTIMIZATION BASICS
Basic Blocks

 Source codes generally have a number of instructions, which are always executed in
sequence and are considered as the basic blocks of the code.

 These basic blocks do not have any jump statements among them, i.e., when the first
instruction is executed, all the instructions in the same basic block will be executed in
their sequence of appearance without losing the flow control of the program.

 A program can have various constructs as basic blocks, like IF-THEN-ELSE, SWITCH-
CASE conditional statements and loops such as DO-WHILE, FOR, and REPEAT-UNTIL,
etc.
CODE OPTIMIZATION BASICS
Basic block identification
We may use the following algorithm to find the basic blocks in a program:

 Search header statements of all the basic blocks from where a basic block starts:
 First statement of a program.
 Statements that are target of any branch (conditional/unconditional).
 Statements that follow any branch statement.
 Header statements and the statements following them form a basic block.
 A basic block does not include any header statement of any other basic block.

Basic blocks are important concepts from both code generation and optimization point of view.

Basic blocks play an important role in identifying variables, which are being used more than once
in a single basic block. If any variable is being used more than once, the register memory
allocated to that variable need not be emptied unless the block finishes execution.
CODE OPTIMIZATION BASICS
Basic block identification
CODE OPTIMIZATION BASICS
Control Flow Graph

 Basic blocks in a program can be represented by means of control flow graphs.


 A control flow graph depicts how the program control is being passed among the
blocks.
 It is a useful tool that helps in optimization by help locating any unwanted loops in the
program.
CODE OPTIMIZATION BASICS
Loop Optimization

Most programs run as a loop in the system. It becomes necessary to optimize the loops in order
to save CPU cycles and memory. Loops can be optimized by the following techniques:

(1) Invariant code : A fragment of code that resides in the loop and computes the same value at each
iteration is called a loop-invariant code. This code can be moved out of the loop by saving it to be
computed only once, rather than with each iteration.

(2) Induction analysis : A variable is called an induction variable if its value is altered within the
loop by a loop-invariant value.

(3) Strength reduction : There are expressions that consume more CPU cycles, time, and memory.
These expressions should be replaced with cheaper expressions without compromising the output of
expression. For example, multiplication (x * 2) is expensive in terms of CPU cycles than (x << 1)
and yields the same result.
CODE OPTIMIZATION BASICS
Dead-code Elimination

Dead code is one or more than one code statements, which are:
 Either never executed or unreachable,
 Or if executed, their output is never used.
Thus, dead code plays no role in any program operation and
therefore it can simply be eliminated.

Partial Redundancy

Redundant expressions are computed more than once in parallel path,


without any change in operands, whereas partial-redundant
expressions are computed more than once in a path, without any
change in operands.
CODE OPTIMIZATION BASICS
Partial Redundancy
Loop-invariant code is partially redundant and can be eliminated by using a Code-Motion
Technique.
Code-Motion Technique:
 Reduce the evaluation frequency of expression and bring loop invariant statements out of
the loop.
We assume that the values of
operands (y and z) are not Here, whether the
changed from assignment of condition is true
variable a to variable c. or false; y OP z
should be
Here, if the condition statement computed only
is true, then y OP z is computed once.
twice, otherwise once.
PHASES OF OPTIMIZATION
There are generally two phases of optimization:

1) Global Optimization:
Transformations are applied to large program segments that includes
functions, procedures and loops.

(2) Local Optimization:


Transformations are applied to small blocks of statements. The local optimization
is done prior to global optimization.
WHERE TO APPLY OPTIMIZATION?
Now that we learned the need for optimization and its two types, now let’s see where to
apply these optimization.

(i) Source program


Optimizing the source program involves making changes to the algorithm or changing
the loop structures. User is the actor here.

(ii) Intermediate Code


Optimizing the intermediate code involves changing the address calculations and
transforming the procedure calls involved. Here compiler is the actor.

(iii) Target Code


Optimizing the target code is done by the compiler. Usage of registers, select and move
instructions is part of optimization involved in the target code.
Target Code Generation
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 .
TARGET CODE GENERATION
 Computations are generally assumed to be
performed on high speed memory locations, 1:N mapping
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.
TARGET CODE GENERATION
Example of Optimized code :
TARGET CODE GENERATION
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.
TARGET CODE GENERATION
Advantages :
Register Allocation :  Fast accessible storage
 Allows computations to be performed on them
 Deterministic as it incurs no miss
 Reduce memory traffic
 Reduces overall computation time

Disadvantages :
• Registers are generally available in small
amount ( up to few hundred Kb )
• Register sizes are fixed and it varies from one
processor to another
• Registers are complicated
• Need to save and restore changes during
context switch and procedure calls
Analysis Synthesis
of input program of output program
(front-end) (back-end)
Where we are….. character
stream
Intermediate
Lexical Analysis Code Generation

token intermediate
stream form

Syntactic Analysis Optimization

abstract intermediate
syntax tree form

Semantic Analysis Code Generation

annotated target
AST language
REFERENCES
Code Optimization:
https://www.tutorialspoint.com/compiler_design/compiler_design_code_optimization.htm
https://www.geeksforgeeks.org/code-optimization-in-compiler-design/

Target Code Generation:


https://www.geeksforgeeks.org/target-code-generation-in-compiler-design/?ref=rp
THE END

You might also like