Code Optimization in Compiler Design
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.
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.
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 :
- 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.
Code Optimization is done in the following different ways:
1. Compile Time Evaluation:
2. Variable Propagation:
3. 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.
4. 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.
Note: Difference between Constant Propagation and Constant Folding:
- In Constant Propagation, the variable is substituted with its assigned constant where as in Constant Propagation, the variables whose values can be computed at compile time are considered and computed.
5. 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.
6. Common Sub Expression Elimination:
- In the above example, a*b and x*b is a common sub expression.
7. Dead Code Elimination:
- Copy propagation often leads to making assignment statements into dead code.
- A variable is said to be dead if it is never used after its last definition.
- In order to find the dead variables, a data flow analysis should be done.
8. Unreachable Code Elimination:
- First, Control Flow Graph should be constructed.
- The block which does not have an incoming edge is an Unreachable code block.
- After constant propagation and constant folding, the unreachable branches can be eliminated.
9. 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.
10. Function Cloning:
- Here, specialized codes for a function are created for different calling parameters.
- Example: Function Overloading
11. Induction Variable and Strength Reduction:
- An induction variable is used in the loop for the following kind of assignment i = i + constant. It is a kind of Loop Optimization Technique.
- Strength reduction means replacing the high strength operator with a low strength.
Loop Optimization Techniques:
1. Code Motion or Frequency Reduction:
- The evaluation frequency of expression is reduced.
- The loop invariant statements are brought out of the loop.
2. Loop Jamming:
- Two or more loops are combined in a single loop. It helps in reducing the compile time.
3. Loop Unrolling:
- It helps in optimizing the execution time of the program by reducing the iterations.
- It increases the program’s speed by eliminating the loop control and test instructions.
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.
- Source program: Optimizing the source program involves making changes to the algorithm or changing the loop structures. The user is the actor here.
- Intermediate Code: Optimizing the intermediate code involves changing the address calculations and transforming the procedure calls involved. Here compiler is the actor.
- Target Code: Optimizing the target code is done by the compiler. Usage of registers, and select and move instructions are part of the optimization involved in the target code.
- Local Optimization: Transformations are applied to small basic blocks of statements. Techniques followed are Local Value Numbering and Tree Height Balancing.
- Regional Optimization: Transformations are applied to Extended Basic Blocks. Techniques followed are Super Local Value Numbering and Loop Unrolling.
- Global Optimization: Transformations are applied to large program segments that include functions, procedures, and loops. Techniques followed are Live Variable Analysis and Global Code Replacement.
- Interprocedural Optimization: As the name indicates, the optimizations are applied inter procedurally. Techniques followed are Inline Substitution and Procedure Placement.