Need for Intermediate Code and Code Optimization
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:
- Suppose we have x no of the source language and y no of the target language:
- 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.
- 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.
- 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).
- Machine independent:
A Machine independent code-optimizer can be applied to the intermediate code. So this can be run on any machine.
Intermediate code is simple enough to be easily converted to any target code. So ICG reduces the overhead of target code generation.
- Complexity :
Intermediate code is Complex enough to represent all complex structure of high-level languages.
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 :
- Machine-dependent optimization – Optimization applied after the target code(assembly code) generation is machine-dependent optimization.
- 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.
- 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:
- 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.
- Dead code elimination:
It removes unnecessary instruction without changing the behavior of the code.
Code optimization increases the speed of the program.
After code optimization our program demands less no of resources thus it saves our resource(i.e, cpu, memory) for other programmer.
- neat and clean code:
After removing the common sub expression and redundant code our code becomes the clean code.
- Strength reduction:
Strength reduction means replacing costly operator by simple (cheap/low strength) operator.
- To execute less number of iteration we use code optimization.
- 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. Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.
Learn all GATE CS concepts with Free Live Classes on our youtube channel.