Related Articles

Related Articles

Need for Intermediate Code and Code Optimization
  • Last Updated : 13 Apr, 2020

Intermediate Code :
Intermediate Code are machine independent code, but they are close to machine instruction. Syntax tree, Postfix notation, 3-address code, DAG can be used as intermediate language.

Need for Intermediate Code:

  1. Suppose we have x no of the source language and y no of the target language:
    1. Without ICG – we have to change each source language into target language directly, So, for each source-target pair we will need a compiler .Hence we need (x*y) compilers, which can be a very large number and which is literally impossible.
    2. With ICG – we will need only x number of compiler to convert each source language into intermediate code. We will also need y compiler to convert the intermediate code into y target languages.
      so, we will need only (x+y) no of the compiler with ICG which is way lesser than x*y no of the compiler.
  2. Re-targeting is facilitated :
    a compiler for a different machine can be created by attaching a back-end(which generate target code) for the new machine to an existing front-end(which generate intermediate code).

  3. Machine independent:
    A Machine independent code-optimizer can be applied to the intermediate code. So this can be run on any machine.

  4. Simplicity:
    Intermediate code is simple enough to be easily converted to any target code. So ICG reduces the overhead of target code generation.



  5. Complexity :
    Intermediate code is Complex enough to represent all complex structure of high-level languages.

  6. Modification:
    We can easily modify our code to get better performance and throughput by applying optimization technique to the Intermediate code.

Code Optimization :
Code Optimization applied on target code(assembly code) is a transformation technique which use to improve the code by consuming fewer resources. Code optimization is of 3 types :

  1. Machine-dependent optimization – Optimization applied after the target code(assembly code) generation is machine-dependent optimization.
  2. Machine-independent – In this, the compiler transforms a part of the intermediate code that does not involve any CPU registers and/or absolute memory locations. e.g, 3-address code.
  3. Local Optimization – The optimization which is performed within the block(sequence of consecutive statements) is local optimization e, g. If-Else, switch-case, conditional statements and loops such as Do-While, For, and repeat-until, etc.

Need for Code optimization:

  1. Loop frequency reduction:
    Loop optimization is the process of optimization within the loop. This reduces the evaluation frequency of the expression and brings loop-invariant statements out of the loop.
  2. Dead code elimination:
    It removes unnecessary instruction without changing the behavior of the code.
  3. Speed:
    Code optimization increases the speed of the program.
  4. Resources:
    After code optimization our program demands less no of resources thus it saves our resource(i.e, cpu, memory) for other programmer.
  5. neat and clean code:
    After removing the common sub expression and redundant code our code becomes the clean code.
  6. Strength reduction:
    Strength reduction means replacing costly operator by simple (cheap/low strength) operator.
  7. To execute less number of iteration we use code optimization.
  8. To combine the bodies of two loops whenever they are sharing same index variable, i.e – for loop jamming or loop fusion is a code optimization.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :