Loop Optimization in Compiler Design - GeeksforGeeks

21 Nov, 2019
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 Optimization is a machine independent optimization.
Decreasing the number of instructions in an inner loop improves the running time of a program even if the amount of code outside that loop is increased.
Loop Optimization Techniques:
Frequency Reduction (Code Motion):In frequency reduction, the amount of code in loop is decreased. A statement or expression, which can be moved outside the loop body without affecting the semantics of the program, is moved outside the loop.Example:Initial code:

while(i<100)
{
 a = Sin(x)/Cos(x) + i;
 i++;
}

Optimized code:

t = Sin(x)/Cos(x);
while(i<100)
{
 a = t + i;
 i++;
} Loop Unrolling:Loop unrolling is a loop transformation technique that helps to optimize the execution time of a program. We basically remove or reduce iterations. Loop unrolling increases the program’s speed by eliminating loop control instruction and loop test instructions.Example:Initial code:

for (int i=0; i<5; i++)
    printf("Pankaj\n");

Optimized code:

printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n"); Loop Jamming:Loop jamming is the combining the two or more loops in a single loop. It reduces the time taken to compile the many number of loops.Example:Initial Code:

for(int i=0; i<5; i++)
    a = i + 5;
for(int i=0; i<5; i++)
    b = i + 10;

Optimized code:
for(int i=0; i<5; i++)
{
 a = i + 5;
 b = i + 10;
} 
Frequency Reduction (Code Motion):In frequency reduction, the amount of code in loop is decreased. A statement or expression, which can be moved outside the loop body without affecting the semantics of the program, is moved outside the loop.Example:Initial code:

while(i<100)
{
 a = Sin(x)/Cos(x) + i;
 i++;
}

Optimized code:

t = Sin(x)/Cos(x);
while(i<100)
{
 a = t + i;
 i++;
} 
Example:
Initial code:

while(i<100)
{
 a = Sin(x)/Cos(x) + i;
 i++;
}

Optimized code:

t = Sin(x)/Cos(x);
while(i<100)
{
 a = t + i;
 i++;
} 
Loop Unrolling:Loop unrolling is a loop transformation technique that helps to optimize the execution time of a program. We basically remove or reduce iterations. Loop unrolling increases the program’s speed by eliminating loop control instruction and loop test instructions.Example:Initial code:

for (int i=0; i<5; i++)
    printf("Pankaj\n");

Optimized code:

printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n"); 
Example:
Initial code:

for (int i=0; i<5; i++)
    printf("Pankaj\n");

Optimized code:

printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n"); 
Loop Jamming:Loop jamming is the combining the two or more loops in a single loop. It reduces the time taken to compile the many number of loops.Example:Initial Code:

for(int i=0; i<5; i++)
    a = i + 5;
for(int i=0; i<5; i++)
    b = i + 10;

Optimized code:
for(int i=0; i<5; i++)
{
 a = i + 5;
 b = i + 10;
} 
Example:
Initial Code:

for(int i=0; i<5; i++)
    a = i + 5;
for(int i=0; i<5; i++)
    b = i + 10;

Optimized code:
for(int i=0; i<5; i++)
{
 a = i + 5;
 b = i + 10;
} 
Compiler Design
GATE CS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Construction of LL(1) Parsing Table
Introduction of Lexical Analysis
Flex (Fast Lexical Analyzer Generator )
C program to detect tokens in a C program
Introduction of Compiler Design
Layers of OSI Model
ACID Properties in DBMS
Types of Operating Systems
TCP/IP Model
Page Replacement Algorithms in Operating Systems