Code Optimization in Compiler Design

0
1254

Code Optimization in Compiler Design

In the synthesis process, code optimization is a software transformation technique that aims to optimize intermediate code by making it use less resources (i.e. CPU, Memory) in order to produce faster-running machine code. The compiler optimization process should achieve the following goals:

  • The optimization must be correct; it must not alter the program’s sense in any way.
  • The program’s speed and efficiency should be improved by optimization.
  • Compilation time must be held to a minimum.
  • The optimization method does not cause the compilation process to lag.

When Do You Optimize?
Since it decreases readability and adds code that improves efficiency, optimization of the code is often done at the end of the development stage.

What is the point of optimizing?
The code optimization process does not include the optimization of an algorithm. As a result, the software has been streamlined. It may also include reducing the code’s size. As a result, optimization aids in:

  • Reduce the amount of space used and increase the compilation speed.
  • Analyzing datasets by hand takes a long time. As a result, we use data analysis tools like Tableau. Similarly, manually performing the optimization is time-consuming and should be avoided in favor of using a code optimizer.
  • Reusability is often promoted by optimized code.

Software Optimization Forms – The optimization method can be divided into two categories:

Machine Independent Optimization – In this code optimization process, the aim is to optimize the intermediate code in order to produce a better target code. There are no CPU registers or absolute memory locations involved in the portion of the intermediate code that is transformed here.

Machine-Dependent Optimization – After the target code has been created and transformed according to the target machine architecture, machine-dependent optimization is performed. It makes use of CPU registers and can use absolute rather than relative memory references. Machine-dependent optimizers work hard to maximize the memory hierarchy’s benefits.

Code optimization can be accomplished in a variety of 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. Variable propagation sometimes results in assignment statements being dead code, which must be removed.

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

4. Code Motion :

• Decrease the duration of speech assessments.
• Exclude statements that are loop invariant from 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

• An induction variable is used in a loop for the following type of assignment: I = I + constant.
• The term “power reduction” refers to the method of replacing a high-strength operator with a low-strength.

i = 1;
while (i<10)
{
y = i * 4;
}
//After Reduction
i = 1
t = 4
{
while( t<40)
y = t;
t = t + 4;
}

Where Can Optimization Be Used?
Now that we’ve heard about the importance of optimization and the two forms of optimization, let’s look at where these optimizations can be used.

the program’s source

Changing the algorithm or changing the loop structures is part of optimizing the source program.

The user is the main character in this scene.

Code for Intermediates

Changing the address calculations and transforming the process calls involved in optimizing the intermediate code. The participant in this case is the compiler.

Code for the goal

The compiler is in charge of optimizing the target code. The target code’s optimization includes the use of registers, pick, and transfer instructions.

Optimisation Phases
Optimisation is normally divided into two stages:

Transformations are applied to large software segments, such as functions, procedures, and loops, in order to achieve global optimization.
Optimization on a local level:

Small blocks of statements are subjected to transformations.

Prior to performing global optimization, local optimization is carried out.

Reference – https://en.wikipedia.org/wiki/Optimizing_compiler

LEAVE A REPLY

Please enter your comment!
Please enter your name here