Code Optimization in Compiler Design - GeeksforGeeks

03 Jul, 2020
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.
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:
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.
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 :
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.Variable Propagation ://Before Optimization c = a * b                                               x = a                                                  till                                                           d = x * b + 4   //After Optimization c = a * b  x = atilld = a * b + 4Hence, after variable propagation, a*b and x*b will be identified as common sub-expression.Dead code elimination : Variable propagation often leads to making assignment statement into dead codec = a * b                                                x = a                                                till                                                          d = a * b + 4    //After elimination :c = a * btilld = a * b + 4Code Motion :• Reduce the evaluation frequency of expression.• Bring loop invariant statements out of the loop.a = 200; while(a>0) {     b = x + y;     if (a % b == 0}     printf(“%d”, a);   }  //This code can be further optimized asa = 200;b = x + y;while(a>0) {     if (a % b == 0}     printf(“%d”, a);   }Induction Variable and Strength Reduction :• An induction variable is used in loop for the following kind of assignment i = i + constant.• Strength reduction means replacing the high strength operator by the low strength.i = 1;                                                                      while (i<10)                                                          {                                                                                 y = i * 4; }    //After Reductioni = 1t = 4{    while( t<40)    y = t;    t = t + 4;}
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.
(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.
Variable Propagation ://Before Optimization c = a * b                                               x = a                                                  till                                                           d = x * b + 4   //After Optimization c = a * b  x = atilld = a * b + 4Hence, after variable propagation, a*b and x*b will be identified as common sub-expression.
//Before Optimization c = a * b                                               x = a                                                  till                                                           d = x * b + 4   //After Optimization c = a * b  x = atilld = a * b + 4
Hence, after variable propagation, a*b and x*b will be identified as common sub-expression.
Dead code elimination : Variable propagation often leads to making assignment statement into dead codec = a * b                                                x = a                                                till                                                          d = a * b + 4    //After elimination :c = a * btilld = a * b + 4
c = a * b                                                x = a                                                till                                                          d = a * b + 4    //After elimination :c = a * btilld = a * b + 4
Code Motion :• Reduce the evaluation frequency of expression.• Bring loop invariant statements out of the loop.a = 200; while(a>0) {     b = x + y;     if (a % b == 0}     printf(“%d”, a);   }  //This code can be further optimized asa = 200;b = x + y;while(a>0) {     if (a % b == 0}     printf(“%d”, a);   }
a = 200; while(a>0) {     b = x + y;     if (a % b == 0}     printf(“%d”, a);   }  //This code can be further optimized asa = 200;b = x + y;while(a>0) {     if (a % b == 0}     printf(“%d”, a);   }
Induction Variable and Strength Reduction :• An induction variable is used in loop for the following kind of assignment i = i + constant.• Strength reduction means replacing the high strength operator by the low strength.i = 1;                                                                      while (i<10)                                                          {                                                                                 y = i * 4; }    //After Reductioni = 1t = 4{    while( t<40)    y = t;    t = t + 4;}
i = 1;                                                                      while (i<10)                                                          {                                                                                 y = i * 4; }    //After Reductioni = 1t = 4{    while( t<40)    y = t;    t = t + 4;}
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 programOptimizing the source program involves making changes to the algorithm or changing the loop structures.User is the actor here.
Intermediate CodeOptimizing the intermediate code involves changing the address calculations and transforming the procedure calls involved. Here compiler is the actor.
Target CodeOptimizing the target code is done by the compiler. Usage of registers,select and move instructions is part of optimization involved in the target code.
Global Optimization:Transformations are applied to large program segments that includes functions,procedures and loops.
Local Optimization:Transformations are applied to small blocks of statements.The local optimization is done prior to global optimization.
Reference – https://en.wikipedia.org/wiki/Optimizing_compiler
This article is contributed by Priyamvada Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Palak Jain 5
aleenajoseph993
singhdrishti1001
Compiler Design
GATE CS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Construction of LL(1) Parsing Table
Directed Acyclic graph in Compiler Design (with examples)
Types of Parsers in Compiler Design
Difference between Compiler and Interpreter
Peephole Optimization in Compiler Design
Layers of OSI Model
ACID Properties in DBMS
TCP/IP Model
Types of Operating Systems
Normal Forms in DBMS