Compiler optimization and new trends in it!!!
System Programming
{ VIT_TY_IT_B1_ GROUP_5 Home _Assignment}
Code optimization Phase in Compiler Design :-
Compiler consists of 6 phases , each phase play an important role in translation of high-level language(input code)to the machine level language(target code) Compiler Phases are given below ,
In which we are more focusing on the 4th and 5th phase of the compiler i.e. Intermediate code generator and Code optimizer.
Why do we need a code optimizer in the compiler ??
The intermediate code generator output is the intermediate code which is large in terms of number of lines and contains many repetitive parts , some unnecessary code. This code will take too much space in the main memory and also the execution time will increase , decrease in the efficiency.
To solve this problem code optimizers play an important role to optimize the code in terms of decreasing the number of lines , removing the unnecessary code parts (dead code) etc. In this modern era the architecture of processors are very complex and with introduction of embedded system and multi core requires fast output of source code in terms of space and time constraints .That’s why code optimizer is very important in compiler design to enhance the overall performance.
Code optimization phase
It is the 5th phase of compiler design. It mainly involves,
- Removal of unwanted part of code
- Rearrangement of the code
Classical Code optimization techniques:-
1.Local Optimization
2.Global Optimization
3.Inter procedural optimization.
i).Local optimization:-
- The output of 4th phase i.e three address codes are partitioning into the sequence of blocks. The local optimization mainly performs within the particular block of code.
- In Local optimization we will get improvement in the running time of code because of each block will optimized in this process
Some common local optimization techniques are follows,
ii).Dead Code elimination
In the above code block, the below if statement is useless because the value of i is 0 and the if statement will work for i=1 only, so that if statement part is dead part.
2.Global Optimization
In the global optimization , the optimization will be performed across the block of codes. It is mainly based on the data flow analysis..
Some common techniques are,
(a)Eliminating global common sub-expressions.
(b) Dead-code elimination.
(c )Code Motion.
(d) Constant folding.
Constant folding:-
The expressions that contain the operands having constant values at compile time are evaluated.Those expressions are then replaced with their respective results.
Ex:- Area = (22 /7 )r*r
In the above example the value of 22/7 is replaced with 3.14 at the compile time. This process saves the time at run time.
3.Inter Procedural Performance Optimization:-
Inter-procedural optimization (IPO) techniques are applied to programs that contain many frequently used functions. The technique is called inter-procedural because it analyses the entire program, whereas other optimizations such as local optimization or global optimization look at only a single function, or even a single block of code.
(a) Address-token analysis
(b) Array-dimension padding
© Automatic array transposition
(d) Common block splitting
(e) Constant propagation
i)Constant Propagation :-
If some variable has been assigned some constant value, then it replaces that variable with its constant value in the further program during compilation.
Ex:- pi = 3.14 , r =10
Area= pi *r*r.
The pi value and constant value are replaced with corresponding values at the compile time only. So that it will save time in run time.
Optimization new trends
The new trends in code optimization mainly focused on the reduction of code size. However, reducing the code size has become low-priority due to steadily decreasing cost of memory. Still, it is favorable to spend effort on code size reduction instead of wasting memory.
1.Reverse inclining (procedural Abstraction )
This technique is mainly focused on code size reduction for the optimization. Reverse inclining mainly uses the function call to replace the code pattern that is present in the program. Given below example is best explanation of reverse inclining,
This technique is very effective because it reduces code up to 30%.
2.Cross-Linking Optimization :-
This technique mainly focuses on the optimization of switch cases. When there are many switch cases with the same tail end. This tail end can be factored out to reduce the size of code. This techniques can be used within function or across functions.
3)Multiple Memory Access optimization:-
The instructions which load or store two or more registers simultaneously are called Multiple Memory Access (MMA) instructions. Many microprocessors use MMA instructions for reducing code size. MMA instructions include multiple loads and multiple stores where a set of registers are loaded from, or stored to, successive instruction words in memory. For example, the ARM7 has LDM and STM instructions for Load-multiple registers and Store-multiple registers. There is a significant compactness in code size by expressing a large set of registers with few instruction word bits. For example, in a ARM7 processor, the LDM instruction uses only sixteen bits to encode up to sixteen register loads (512 bits without the use of the LDM instruction). ). Effective use of LDM and STM instructions in a ARM7 processor can save up to 480 bits opcode space.
References
- https://www.tutorialspoint.com/compiler_design/compiler_design_code_optimization.htm
- https://www.gatevidyalay.com/code-optimization-techniques/
- https://www.researchgate.net/publication/336771654_New_trends_and_Challenges_in_Source_Code_Optimization
- https://www.researchgate.net/publication/316791432_The_New_Trends_in_Compiler_Analysis_and_Optimizations
- https://www.ijcaonline.org/archives/volume131/number16/23535-2015907609