Code Optimization PDF
Code Optimization PDF
Code Optimization PDF
Introduction
● Optimization is the final stage of compiler, though it is optional. This is a
program transformation technique, which tries to improve the code by making
it consume less resources (i.e. CPU, Memory) and deliver high speed. In
optimization, high-level general programming constructs are replaced by very
efficient low-level programming codes.
● It rearranges the tree generated by parser such that to consume fewer
resources and to produces more speed.
● The meaning of the code is not altered.
● It increases the execution speed of the program
● It reduces the storage space
A code optimizing process must follow the three rules
given below:
1. The output code must not, in any way, change the meaning of the program.
2. Optimization should increase the speed of the program and if possible, the
program should demand less number of resources.
3. Optimization should itself be fast and should not delay the overall compiling
process.
Efforts for an optimized code can be made at various
levels of compiling the process.
1. At the beginning, users can change/rearrange the code or use better algorithms
to write the code.
2. After generating intermediate code, the compiler can modify the intermediate
code by address calculations and improving loops.
3. While producing the target machine code, the compiler can make use of
memory hierarchy and CPU registers.
Main Types of Code Optimization are -
2. Constant propagations – Here, expressions which can be evaluated at compile time are identified and replaced with
their values.
3. Jump threading – This involves an optimization of jump directly into a second one. The second condition is eliminated if it
is an inverse or a subset of the first which can be done effortlessly in a single pass through the program. Acyclic chained jumps are
followed till the compiler arrives at a fixed point.
4. Loop invariant code motion – This is also known as hoisting or scalar promotion. A loop invariant contains expressions
that can be taken outside the body of a loop without any impact on the semantics of the program. The above-mentioned movement
is performed automatically by loop invariant code motion.
5. Dead code elimination – Here, as the name indicates, the codes that do not affect the program results are eliminated. It
has a lot of benefits including reduction of program size and running time. It also simplifies the program structure. Dead code
elimination is also known as DCE, dead code removal, dead code stripping, or dead code strip.
6. Strength reduction – This compiler optimization replaces expensive operations with equivalent and more efficient ones,
but less expensive. For example, replace a multiplication within a loop with an addition.
3. Low-level Optimization
1. Register allocation – Here, a big number of target program variables are assigned to a small number of CPU
registers. This can happen over a local register allocation or a global register allocation or an inter-procedural register
allocation.
2. Instruction Scheduling – This is used to improve an instruction level parallelism that in turn improves the
performance of machines with instruction pipelines. It will not change the meaning of the code but rearranges the
order of instructions to avoid pipeline stalls. Semantically ambiguous operations are also avoided.
3. Floating-point units utilization – Floating point units are designed specifically to carry out operations of floating
point numbers like addition, subtraction, etc. The features of these units are utilized in low-level optimizations which
are highly specific to the type of architecture.
4. Branch prediction – Branch prediction techniques help to guess in which way a branch functions even though it is
not known definitively which will be of great help for the betterment of results.
5. Peephole and profile-based optimization – Peephole optimization technique is carried out over small code
sections at a time to transform them by replacing with shorter or faster sets of instructions. This set is called as a
peephole.
Machine Independent and Machine dependent.
This code involves repeated assignment of the identifier item, which if we put this
way:
i. Function Preserving
2. Constant folding
If expression generates a constant value then instead of performing its calculation again and
again we calculate it once and assign it.
3. Copy Propagation
In this propagation a F value is been send to G and G value is been send to H We
can eliminate G variable directly assigning the value of F to H.
T1 = X T2 = T3 + T2
T2 = T3 + T2 T3 = T1
T3 = X
1. Code Motion
Instead of that perform the calculation outside the loop and assign a value in
the calculation.
2. Strength Reduction
The multiplication operator can be easily replaced by left shift operator a<<1
Division can be replaced by a a>>1 operator.
3. Frequency Reduction
Prior to code generation, the front end must be scanned, parsed and
translated into intermediate representation along with necessary type
checking. Therefore, input to code generation is assumed to be error-free
2. Target program:
The output of the code generator is the target program.
It makes use of symbol table, that is, a name in a three-address statement refers
to a symbol table entry for the name.
4. Instruction selection:
The instructions of target machine should be complete and uniform.
Instruction speeds and machine idioms are important factors when efficiency of
target program is considered.
The quality of the generated code is determined by its speed and size.
The former statement can be translated into the latter statement as shown below:
5. 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 :
1. Register allocation - the set of variables that will reside in registers at a point in the
program is selected.
2. Register assignment - the specific register that a value picked
3. Certain machine requires even-odd register pairs for some operands and results.
For example, consider the division instruction of the form :D x, y where, x - dividend
even register in even/odd register pair y-divisor even register holds the remainder odd
register holds the quotient
6. Evaluation order
The order in which the computations are performed can affect the efficiency of the
target code.
Some computation orders require fewer registers to hold intermediate results than
others.