Optimization of Basic Blocks
Last Updated :
23 Jun, 2023
Optimization is applied to the basic blocks after the intermediate code generation phase of the compiler. Optimization is the process of transforming a program that improves the code by consuming fewer resources and delivering high speed. In optimization, high-level codes are replaced by their equivalent efficient low-level codes. Optimization of basic blocks can be machine-dependent or machine-independent. These transformations are useful for improving the quality of code that will be ultimately generated from basic block.
There are two types of basic block optimizations:
- Structure preserving transformations
- Algebraic transformations

Structure-Preserving Transformations:
The structure-preserving transformation on basic blocks includes:
- Dead Code Elimination
- Common Subexpression Elimination
- Renaming of Temporary variables
- Interchange of two independent adjacent statements
1.Dead Code Elimination:
Dead code is defined as that part of the code that never executes during the program execution. So, for optimization, such code or dead code is eliminated. The code which is never executed during the program (Dead code) takes time so, for optimization and speed, it is eliminated from the code. Eliminating the dead code increases the speed of the program as the compiler does not have to translate the dead code.
Example:
// Program with Dead code
int main()
{
x = 2
if (x > 2)
cout << "code"; // Dead code
else
cout << "Optimization";
return 0;
}
// Optimized Program without dead code
int main()
{
x = 2;
cout << "Optimization"; // Dead Code Eliminated
return 0;
}
2.Common Subexpression Elimination:
In this technique, the sub-expression which are common are used frequently are calculated only once and reused when needed. DAG ( Directed Acyclic Graph ) is used to eliminate common subexpressions.
Example:

3.Renaming of Temporary Variables:
Statements containing instances of a temporary variable can be changed to instances of a new temporary variable without changing the basic block value.
Example: Statement t = a + b can be changed to x = a + b where t is a temporary variable and x is a new temporary variable without changing the value of the basic block.
4.Interchange of Two Independent Adjacent Statements:
If a block has two adjacent statements which are independent can be interchanged without affecting the basic block value.
Example:
t1 = a + b
t2 = c + d
These two independent statements of a block can be interchanged without affecting the value of the block.
Algebraic Transformation:
Countless algebraic transformations can be used to change the set of expressions computed by a basic block into an algebraically equivalent set. Some of the algebraic transformation on basic blocks includes:
- Constant Folding
- Copy Propagation
- Strength Reduction
1. Constant Folding:
Solve the constant terms which are continuous so that compiler does not need to solve this expression.
Example:
x = 2 * 3 + y ⇒ x = 6 + y (Optimized code)
2. Copy Propagation:
It is of two types, Variable Propagation, and Constant Propagation.
Variable Propagation:
x = y ⇒ z = y + 2 (Optimized code)
z = x + 2
Constant Propagation:
x = 3 ⇒ z = 3 + a (Optimized code)
z = x + a
3. Strength Reduction:
Replace expensive statement/ instruction with cheaper ones.
x = 2 * y (costly) ⇒ x = y + y (cheaper)
x = 2 * y (costly) ⇒ x = y << 1 (cheaper)
Loop Optimization:
Loop optimization includes the following strategies:
- Code motion & Frequency Reduction
- Induction variable elimination
- Loop merging/combining
- Loop Unrolling
1. Code Motion & Frequency Reduction
Move loop invariant code outside of the loop.
// Program with loop variant inside loop
int main()
{
for (i = 0; i < n; i++) {
x = 10;
y = y + i;
}
return 0;
}
// Program with loop variant outside loop
int main()
{
x = 10;
for (i = 0; i < n; i++)
y = y + i;
return 0;
}
2. Induction Variable Elimination:
Eliminate various unnecessary induction variables used in the loop.
// Program with multiple induction variables
int main()
{
i1 = 0;
i2 = 0;
for (i = 0; i < n; i++) {
A[i1++] = B[i2++];
}
return 0;
}
// Program with one induction variable
int main()
{
for (i = 0; i < n; i++) {
A[i] = B[i]; // Only one induction variable
}
return 0;
}
3. Loop Merging/Combining:
If the operations performed can be done in a single loop then, merge or combine the loops.
// Program with multiple loops
int main()
{
for (i = 0; i < n; i++)
A[i] = i + 1;
for (j = 0; j < n; j++)
B[j] = j - 1;
return 0;
}
// Program with one loop when multiple loops are merged
int main()
{
for (i = 0; i < n; i++) {
A[i] = i + 1;
B[i] = i - 1;
}
return 0;
}
4. Loop Unrolling:
If there exists simple code which can reduce the number of times the loop executes then, the loop can be replaced with these codes.
// Program with loops
int main()
{
for (i = 0; i < 3; i++)
cout << "Cd";
return 0;
}
// Program with simple code without loops
int main()
{
cout << "Cd";
cout << "Cd";
cout << "Cd";
return 0;
}
Similar Reads
Optimization of Basic Blocks
Optimization is applied to the basic blocks after the intermediate code generation phase of the compiler. Optimization is the process of transforming a program that improves the code by consuming fewer resources and delivering high speed. In optimization, high-level codes are replaced by their equiv
5 min read
Basic Blocks in Compiler Design
Basic Block is a straight line code sequence that has no branches in and out branches except to the entry and at the end respectively. Basic Block is a set of statements that always executes one after other, in a sequence. The first task is to partition a sequence of three-address codes into basic b
3 min read
Basic Blocks in Compiler Design
Basic Block is a straight line code sequence that has no branches in and out branches except to the entry and at the end respectively. Basic Block is a set of statements that always executes one after other, in a sequence. The first task is to partition a sequence of three-address codes into basic b
3 min read
Loop Optimization in Compiler Design
Loop Optimization is the process of increasing execution speed and reducing the overheads associated with loops. It plays an important role in improving cache performance and making effective use of parallel processing capabilities. Most execution time of a scientific program is spent on loops. Loop
4 min read
Code Optimization in Compiler Design
Code optimization is a crucial phase in compiler design aimed at enhancing the performance and efficiency of the executable code. By improving the quality of the generated machine code optimizations can reduce execution time, minimize resource usage, and improve overall system performance. This proc
9 min read
Real-Life Applications of Mathematical Optimization
Mathematical Optimization is one of the many branches in mathematics which help us optimize any objective function by systematically analyzing decision variables and constraints to find the most efficient or effective solution. In this article, we will discuss all the relevant application of Mathema
4 min read
Constraint Propagation in AI
Artificial Intelligence (AI) encompasses a variety of methods and techniques to solve complex problems efficiently. One such technique is constraint propagation, which plays a crucial role in areas like scheduling, planning, and resource allocation. This article explores the concept of constraint pr
10 min read
Maximize Optimization using Scipy
In this post, we'll talk about the Python Scipy module and the idea of linear programming problems, including how to maximize the objective function and obtain the best solution. Linear Programming Linear Programming consists of an objective function (Z) and some constraints. According to the situat
5 min read
What is a Block in Programming?
In programming, a block is a set of statements that are grouped and treated as a single unit. Blocks are used to define the scope of variables, control the flow of execution in conditional statements and loops, and encapsulate code in functions, methods, or classes. Table of Content What is a Block
6 min read
CSES Solutions - Book Shop
You are in a book shop which sells N different books. You know the price and number of pages of each book. You have decided that the total price of your purchases will be at most X. What is the maximum number of pages you can buy? You can buy each book at most once. Examples: Input: N = 4, X = 10, p
7 min read
Blocks in Objective-C
Blocks in Objective-C are a potent tool that can simplify the development process. Blocks are an alternative to traditional functions, allowing you to write code that is more concise and efficient. They are used for many different purposes, including as callbacks and iterators. Blocks can also be us
6 min read
Block Diagram Algebra
In this article, We will discuss about block diagram and its components. We will also discuss about the various rules involved in block diagram algebra along with its equivalent block diagram. In addition to these we will also discuss about the application, advantages and disadvantages. Table of Con
8 min read
Graphical Solution of Linear Programming Problems
Linear programming is the simplest way of optimizing a problem. Through this method, we can formulate a real-world problem into a mathematical model. There are various methods for solving Linear Programming Problems and one of the easiest and most important methods for solving LPP is the graphical m
13 min read
Assign Campus Bikes to Workers II
On a 2D grid representing a campus, there are n workers and m bikes, with 1 <= n <= m <= 10. Each worker and bike is assigned a 2D coordinate on this grid. The tasks is to assign one unique bike to each worker in a way that minimizes the sum of the Manhattan distances between each worker an
9 min read
Using Instance Blocks in Java
The instance block can be defined as the name-less method in java inside which we can define logic and they possess certain characteristics as follows. They can be declared inside classes but not inside any method. Instance block logic is common for all the objects. Instance block will be executed o
3 min read
Block Diagram Reduction - Control System
A control system may consist of several components. To show the function performed by each component in control engineering, we commonly use a diagram called the block diagram. A block diagram of a system is a pictorial representation of the functions performed by each component and of the flow of s
8 min read
Block Diagram Reduction Rules
As we know a complex control system is difficult to analyze as various factors are associated with it. In this article, we will see how to easily analyze a control system, and it is only possible by using block diagram reduction rules. This representation of a system involves summing points, functio
7 min read
Find Maximum Profit for Products within Budget
Given a 2D integer array product, where product[i] = [costi, profiti] represents the cost and profit of a product, respectively. Additionally, you have been given a 0-indexed integer array called budget[]. For each budget[i], the task is to find the maximum profit of a product whose cost is less tha
11 min read
Branch and Bound meaning in DSA
Branch and bound is an algorithmic technique used in computer science to solve optimization problems. Branch and bound is a systematic way of exploring all possible solutions to a problem by dividing the problem space into smaller sub-problems and then applying bounds or constraints to eliminate cer
3 min read