Open In App

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 :

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:

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.
  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 is done in the following different ways:

1. Compile Time Evaluation:




(i)   A = 2*(22.0/7.0)*r 
      Perform 2*(22.0/7.0)*r at compile time.
(ii)  x = 12.4
      y = x/2.3 
      Evaluate x/2.3 as 12.4/2.3 at compile time.

2. Variable Propagation:




//Before Optimization 
c = a * b                                               
x = a                                                  
till                                                           
d = x * b + 4 
  
  
//After Optimization 
c = a * b  
x = a
till
d = a * b + 4

3. Constant Propagation:  

Example: 




(i)  A = 2*(22.0/7.0)*r
     Performs 2*(22.0/7.0)*r at compile time.
(ii)  x = 12.4
      y = x/2.3
      Evaluates x/2.3 as 12.4/2.3 at compile time.
(iii) int k=2;
      if(k) go to L3;
      It is evaluated as :
      go to L3 ( Because k = 2 which implies condition is always true)

4. Constant Folding: 

Example: 




#define k 5
x = 2 * k 
y = k + 5
   
This can be computed at compile time and the values of x and y are :
 x = 10
 y = 10

Note: Difference between Constant Propagation and Constant Folding: 

5. Copy Propagation: 

Example : 




//Before Optimization
c = a * b                                              
x = a                                                 
till                                                          
d = x * b + 4
 
 
//After Optimization
c = a * b 
x = a
till
d = a * b + 4

6. Common Sub Expression Elimination: 

7. Dead Code Elimination: 

Example: 




c = a * b                                               
x = a                                               
till                                                         
d = a * b + 4  
 
//After elimination :
c = a * b
till
d = a * b + 4

8. Unreachable Code Elimination: 




#include <iostream>
using namespace std;
 
int main() {
  int num;
  num=10;
    cout << "GFG!";
    return 0;
  cout << num; //unreachable code
}
//after elimination of unreachable code
int main() {
  int num;
  num=10;
    cout << "GFG!";
    return 0;
 }

9. Function Inlining: 

10. Function Cloning: 

11. Induction Variable and Strength Reduction: 

Examples: 




Example 1 :
Multiplication with powers of 2 can be replaced by shift left operator which is less
expensive than multiplication
a=a*16
// Can be modified as :
a = a<<4
 
Example 2 :
i = 1;                                                                     
while (i<10)                                                         
{                                                                            
   y = i * 4;
}
 
 
//After Reduction
i = 1
t = 4
{
  while( t<40)
  y = t;
  t = t + 4;
}

Loop Optimization Techniques: 

1. Code Motion or Frequency Reduction: 

Example: 




a = 200;
 while(a>0)
 {
     b = x + y;
     if (a % b == 0)
     printf(“%d”, a);
   }
 
 
//This code can be further optimized as
a = 200;
b = x + y;
while(a>0)
 {
     if (a % b == 0}
     printf(“%d”, a);
   }

2. Loop Jamming: 

Example: 




// Before loop jamming
for(int k=0;k<10;k++)
{
 x = k*2;
}
 
for(int k=0;k<10;k++)
{
 y = k+3;
}
 
//After loop jamming
for(int k=0;k<10;k++)
{
 x = k*2;
 y = k+3;
}

3. Loop Unrolling: 

Example: 




//Before Loop Unrolling
 
for(int i=0;i<2;i++)
{
  printf("Hello");
}
 
//After Loop Unrolling
 
printf("Hello");
printf("Hello");

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.

Advantages of Code Optimization:

Improved performance: Code optimization can result in code that executes faster and uses fewer resources, leading to improved performance.

Reduction in code size: Code optimization can help reduce the size of the generated code, making it easier to distribute and deploy.

Increased portability: Code optimization can result in code that is more portable across different platforms, making it easier to target a wider range of hardware and software.

Reduced power consumption: Code optimization can lead to code that consumes less power, making it more energy-efficient.

Improved maintainability: Code optimization can result in code that is easier to understand and maintain, reducing the cost of software maintenance.

Disadvantages of Code Optimization:

Increased compilation time: Code optimization can significantly increase the compilation time, which can be a significant drawback when developing large software systems.

Increased complexity: Code optimization can result in more complex code, making it harder to understand and debug.

Potential for introducing bugs: Code optimization can introduce bugs into the code if not done carefully, leading to unexpected behavior and errors.

Difficulty in assessing the effectiveness: It can be difficult to determine the effectiveness of code optimization, making it hard to justify the time and resources spent on the process.


Article Tags :