hamburger

Study Notes on Code Optimization

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Code Optimization: The code optimization in the synthesis phase of a compiler is a program transformation methodology, that aims to improve the efficiency of the intermediate code by transforming the program optimally so that it consumes fewer resources (i.e. Memory, CPU) to make the machine code execution faster. The compiler code optimizing phase should stand on the following criteria:

  • The transformation must be correct, i.e. it should not change the meaning of the code in any way.
  • Code optimization should maximize the speed and performance of the program.
  • The compilation time should be reasonable.
  • The overall compiling process should not be delayed due to the optimization process.

Code Optimization is often performed at the end of the development stage since this transformation reduces readability and adds code that is used to increase performance and efficiency.

 Types of Code Optimization

The code optimization process is mainly classified into two different 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
     
    Hence, after variable propagation, a*b and x*b will be identified as common sub-expression.
  3. Dead code elimination : Variable propagation often leads to making assignment statement into dead code
       c = a * b

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

  4. Code Motion :
    1. Reduce the evaluation frequency of expression.
    2. 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 as
      a = 200;
      b = x + y;
      while(a>0)
       {
           if (a % b == 0}
           printf(“%d”, a);
         }
  5. Induction Variable and Strength Reduction :
    1.  An induction variable is used in the loop for the following kind of assignment i = i + constant.
    2. Strength reduction means replacing the high strength operator with the low strength.
      i = 1;
      while (i<10)                                                         
      {                                                                            
          y = i * 4;
      }
       
      /After Reduction
      i = 1
      t = 4
      {
         while( t<40)
         y = t;
         t = t + 4;
      }

So this was about code optimization. 

You can go with the detailed champion study plan for GATE CS 2021 from the following link:

Detailed GATE CSE 2021 Champion Study Plan

Candidates can also practice 110+ Mock tests for exams like GATE, ISRO, DRDO, BARC, NIELIT, etc. with the BYJU’S Exam Prep Test Series check the following link:

Click Here to Avail GATE CSE Test Series! (100+ Mock Tests)

Get unlimited access to 21+ structured Live Courses, all 112+ mock tests with the Online Classroom Program for GATE CS & PSU Exams:

Click here to avail Online Classroom Program for Computer Science Engineering

Thanks

#DreamStriveSucceed

Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium