Code Optimization and Target Code Generation
Code Optimization and Target Code Generation
Code Optimization and Target Code Generation
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.
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 :
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
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
1) Global Optimization:
Transformations are applied to large program segments that includes
functions, procedures and loops.
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.
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
abstract intermediate
syntax tree form
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/