0% found this document useful (0 votes)
20 views

Code Optimization-II

The document discusses several techniques for optimizing loops in code: 1) Loop unrolling removes or reduces loop iterations by computing multiple iterations simultaneously. 2) Loop jamming combines two or more loops into a single loop. 3) Loop peeling handles problematic loop iterations separately before the main loop.

Uploaded by

Aakash D.V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Code Optimization-II

The document discusses several techniques for optimizing loops in code: 1) Loop unrolling removes or reduces loop iterations by computing multiple iterations simultaneously. 2) Loop jamming combines two or more loops into a single loop. 3) Loop peeling handles problematic loop iterations separately before the main loop.

Uploaded by

Aakash D.V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Loop Optimization - Loop Unrolling

● remove loop or reduce iterations of the loop

After optimization:

Before optimization: printf("Compiler");


printf("Compiler");
for (i=0; i<5; i++) printf("Compiler");
printf("Compiler"); printf("Compiler");
printf("Compiler");

[email protected] 05/07/2023 13
Loop Optimization - Loop Unrolling
● remove loop or reduce iterations of the loop

After optimization:

Before optimization: for (i=0; i<40; i+=4)


a[i] = 2;
for (i=0; i<40; i++) a[i+1] = 2;
a[i] = 2; a[i+2] = 2;
a[i+3] = 2;

[email protected] 05/07/2023 14
Loop Optimization - Loop Jamming
● combining two or more loops in a single loop

After optimization:
Before optimization:
for(int i=0; i<5; i++)
for(int i=0; i<5; i++)
{
a = i + 5;
a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;
b = i + 10;
}

[email protected] 05/07/2023 15
Loop Optimization - Loop Fusion
● Similar to Jamming but some parts of the expressions are also merged

Before optimization:
int acc = 0; After optimization:
for (int i = 0; i < n; ++i)
{ int acc = 0;
acc += a[i]; for (int i = 0; i < n; ++i)
a[i] = acc; {
} acc += a[i];
for (int i = 0; i < n; ++i) a[i] = acc;
{ b[i] += acc;
b[i] += a[i]; }
} [email protected] 05/07/2023 16
Loop Optimization - Elimination of induction variables
● A variable is called an Induction variable if its value in any loop gets changed
every time

After optimization:
Before optimization:
i=0
i=0
x=0
L1: i = i+1
L1: i = i+1
x = i*3
x = x+3
if x< 10 goto L2
if x< 10 goto L2

● i and x are induction variable


[email protected] 05/07/2023 17
Loop Optimization - Elimination of induction variables
● Example 2
After optimization:
Before optimization:
L1: i = 0
L1: i = 0
s=0
s=0
Source Code: t1 = 0
s=0; goto L3
goto L3
for (i=0; i < 100; i++) L2: t1 = i*4
s += a[i]; L2: t2 = a+t1
t2 = a+t1
t3 = *t2
t3 = *t2
s = s + t3
s = s + t3
i = i+1
i = i+1
t1 is always equal t1 = t1 + 4
to i*4 ! L3: if i < 100 goto L2
L3: if i < 100 goto L2
L4: ...
L4: ...
[email protected] 05/07/2023 18
Loop Optimization - Elimination of induction variables
After optimization After optimization
● Example 2 (stage - 1): (stage - 2):
L1: i = 0
L1: i = 0 s=0
s=0 t1 = 0
t1 = 0 t2 = a
goto L3 goto L3
t2 is always equal L2: t2 = a+t1 L2: t3 = *t2
to a+t1 == a+i*4 ! t3 = *t2 s = s + t3
s = s + t3 i = i+1
i = i+1 t2 = t2 + 4
t1 = t1 + 4 t1 = t1 + 4
L3: if i < 100 goto L2 L3: if i < 100 goto L2
L4: ... L4: ...
[email protected] 05/07/2023 19
Loop Optimization - Elimination of induction variables
● Example 2 After optimization
(stage - 2): After optimization
L1: i = 0 (stage - 3):
s=0 L1: i = 0
t1 = 0 s=0
t2 = a t2 = a
goto L3 goto L3
t1 is not used L2: t3 = *t2 L2: t3 = *t2
s = s + t3 s = s + t3
i = i+1 i = i+1
t2 = t2 + 4 t2 = t2 + 4
t1 = t1 + 4 L3: if i < 100 goto L2
L3: if i < 100 goto L2 L4: ...
L4: ...
[email protected] 05/07/2023 20
Loop Optimization - Loop Fission
● Single loop is divided into multiple - improves locality of reference
○ Locality of reference - accessing same memory location repeatedly in a
short span

Before optimization: After optimization:

for(int i=0; i<5; i++) for(int i=0; i<5; i++)


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

[email protected] 05/07/2023 21
Loop Optimization - Loop Interchange
● In nested loops, inner and outer loops are interchanged - improves locality of
reference

Before optimization: After optimization:

for(int i=0; i<5; i++) for(int j=0; j<5; j++)


for(int j=0; j<5; j++) for(int i=0; i<5; i++)
a[i][j] = i + j a[i][j] = i + j

[email protected] 05/07/2023 22
Loop Optimization - Loop Invariant Removal
● Expression which has computation involved are avoided inside loop

Before optimization:
After optimization:
L1: t = 0
L1: t = 0
t=a+b
L2: i = i + 1
L2: i = i + 1
t=a+b
*i = t
*i = t
if i<N goto L2
if i<N goto L2
L3: x = t
L3: x = t

[email protected] 05/07/2023 23
Loop Optimization - Strength Reduction
● replacing expensive operations with cheaper ones like multiplication is costlier
than addition

After optimization:
Before optimization:
Expensive Cheaper
t= 3 * x+1;
while (x<10) x2 x*x
while (x<10)
{
{ 2*x x+x
y := 3 * x+1;
y=t;
a[y] := a[y]-2; x/2 x * 0.5
a[y]= a[y]-2;
x := x+2;
x=x+2;
}
t=t+6;
}
[email protected] 05/07/2023 24
Loop Optimization - Loop Peeling
● a loop with problematic iteration is resolved separately before entering the
loop

Before optimization: After optimization:

for (int i = 0; i < n; ++i) b[0] = a[0];


{ for (int i = 1; i < n; ++i)
b[i] = (i == 0) ? a[i] : a[i] + b[i-1]; {
} b[i] = a[i] + b[i-1];
}

[email protected] 05/07/2023 25
Principle Sources of Optimization - Causes of Redundancy
● redundancy is a side e ect of having written the program in a high-level
language.
● Consider the code fragment for Quick Sort

[email protected] 05/07/2023 26
Principle Sources of Optimization - Causes of Redundancy
● 3-address Code for the code Fragment of Quick Sort

[email protected] 05/07/2023 27
Principle Sources of Optimization - Causes of Redundancy

● Basic Blocks for the code Fragment


of Quick Sort

[email protected] 05/07/2023 28

You might also like