Study Notes on Code Optimization

By Mukesh Kumar|Updated : August 5th, 2021

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.

Table of Content

 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
    d = x * b + 4
    /After Optimization
    c = a * b 
    x = a
    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
       d = a * b + 4  
        /After elimination :
        c = a * b
        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;
           b = x + y;
           if (a % b == 0}
                 printf(“%d”, a);
      /This code can be further optimized as
      a = 200;
      b = x + y;
           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

Related Links

GATE CSE Syllabus 2022

GATE CSE Exam Pattern 2022

GATE CSE Question Papers

GATE CSE Books 2022

GATE CSE Study Plan

GATE CSE Exam Analysis 2022

GATE CSE Answer Key 2022

GATE CSE Result 2021

GATE CSE Counselling

BARC CS Syllabus

BARC CS Study Plan

BARC CS Exam Analysis


Prep Smart. Score Better!

Posted by:

Mukesh KumarMukesh KumarMember since Feb 2020
Share this article   |


write a comment
Abhishek Deb

Abhishek DebSep 25, 2018

Pls add me in whttapp GRP 9455692230
Lohith Negalur
Add me 8095913042
Shirfil 333

Shirfil 333Sep 28, 2018

Add me 8281903830
Monu Kumar

Monu KumarSep 29, 2018

Add me 9507121826
Nikhil Kumar
Add me too 8840208074

PraveenOct 22, 2020

Add me to 8121727308

Follow us for latest updates