Code Optimization Techniques
Code Optimization Techniques
By
VYOM JAIN
15BIT067
i
CODE OPTIMIZATION TECHNIQUES
Seminar
By
VYOM JAIN
15BIT067
Guided By
PROF. MALARAM KUMHAR
[DEPARTMENT OF INFORMATION TECHNOLOGY]
ii
CERTIFICATE
This is to certify that the Seminar entitled “Code optimization Techniques” submitted by Vyom Jain
(15BIT067), towards the partial fulfillment of the requirements for the degree of Bachelor of
Technology in Information Technology of Nirma University is the record of work carried out by him/her
under my supervision and guidance. In my opinion, the submitted work has reached a level required for
being accepted for examination.
iii
ACKNOWLEDGEMENT
I would like to thank Prof. Malaram Kumhar for guiding me at each step every week about
when to do what. This helped me to progress in the correct direction at the correct pace. I would
also like to express my gratitude toward Dr. Ankit Thakkar, who always kept a check on my
progress every week and for forcing me to work only on the seminar and not on any other thing,
during the lab hours. It helped me to focus on the seminar and to give it the attention that it
needed.
iv
ABSTRACT
This report includes to all the progress that I made in order to understand the need and
implementation of the different kinds of code optimization techniques used in the industry. This
includes the improvements done at programmer level, source code level and compiler level.
The basic requirement of the technique is that the output should be exactly the same and thereby
it should offer some positive change in the performance parameters, viz. time and space.
v
CONTENTS
Certificate iii
Acknowledgement iv
Abstract v
Table of Contents vi
Chapter 1 Introduction 1
1.1 Code Optimization
1.2 Rules to be followed
1.4 Efforts before optimization
vi
4.9 Software pipelining
4.10 Loop unswitching
vii
CHAPTER - 1
INTRODUCTION
1
CHAPTER - 2
TYPES OF CODE OPTIMIZATIONS
2
Limited scope optimization techniques, such as macro compression are more effective
when the entire machine code is available for analysis. This is used in that use case.
3
CHAPTER - 3
FACTORS AFFECTING OPTIMIZATION
4
3.3.3 Special purpose use: we can employ heavy optimization techniques as the code is to be
used only on a limited type of devices all whose specifications can be known
beforehand.
3.3.4 Embedded systems: these are common to special usecase in terms of usage, but these
have more priority on system reliability than speed, hence the techniques should try to
reduce the code size at the cost of speed.
5
CHAPTER - 4
LOOP OPTIMIZATION
int A[n];
int A[n];
int B[n];
int B[n];
int i, j, k;
int i;
for(i=0, j=0, k=0;
for(i=0; i<n; i++)
i<n; i1++)
A[i] = B[i];
A[j++]=B[k++];
6
for(I=0; I<N-1;
I++)
{
B[I+1]=C[I]*X +
Y;
for(J=0;J<N;J++) C[I+1]=1/B[I+1];
{ }
A[J]=A[J]+B[J-1];
B[J]=[J-1]*X + Y; for(I=0; I<N-1;
C[J]=1/B[J]; I++)
D[J]=SQRT(C[J]); A[I+1]=A[I+1] +
} B[I];
for (I=0; I<N-1;
I++)
D[I+1]=
SQRT(C[I+1]);
I=N+1;
for (I = 0; I <
300; I++) for (I = 0; I < 300;
A[I] = A[I] + 3; I++){
A[I] = A[I] + 3;
for (I = 0; I < B[I] = B[I] + 4;
300; I++) }
B[I] = B[I] + 4;
7
int i = 0;
int i = 0; if (i++ < 1) {
while (i++ < 1) { do {
//do something //do
} something
} while (i++ < 1);
}
8
4.8 Loop unrolling:
This one duplicates the operations to be performed inside the loop thereby reducing
the number of times the condition is checked and the number of times the jump
instruction is called, thereby improving performance at the pipeline level.
9
if (a)
for (j = 0; j < N; for (j = 0; j < N;
j++) j++)
if (a) A[j] = 0;
A[j] = 0; else
else for (j = 0; j < N;
B[j] = 0; j++)
B[j] = 0;
10
CHAPTER - 5
DATA FLOW OPTIMIZATIONS
int a;
int b;
double c = (a+b)-(a+b)/4
In the above snippet, (a+b) is a common subexpression. The compiler will understand this
and will reuse the previously computed value.
x = 3; x = 3;
y = x+4; y = 7;//propagation
11
5.3 Alias optimization:
Remove unrelated pointers by specifying which variables can alias which variables.
int glob;
void f ()
{ int global;
int a; void f ()
a = 1; //dead store {
glob = 1; //dead store glob = 2;
glob = 2; return;
return; }
glob = 3; //unreachable
}
12
CHAPTER - 6
SSA (STATIC SINGLE ASSIGNMENT) BASED OPTIMIZATIONS
13
CHAPTER - 7
CODE GENERATOR OPTIMIZATIONS
7.5 Trampolines:
Some systems may have relatively smaller subroutine instructions for accessing a low
memory. Space can be saved by calling subroutines in the main function/method of the
code. Jump instruction can use memory routines at any address. This helps to save
space.
14
CHAPTER - 8
FUNCTIONAL LANGUAGE OPTIMIZATIONS
15
CHAPTER - 9
OTHER OPTIMIZATIONS
{
int A[], not_used; {
A = new int[100]; //once checked for
not_used = A[0]; ArrayOutOfBounds other
not_used = A[1]; // statements can be
... optimized
} away.
}
16
9.4 Macro Compression:
if you are running low on space and willing to lose some performance at its cost,
then this is really helpful. Divide your program into subprograms and call them
wherever necessary. This makes the code modular, reusable and saves space.
17
CHAPTER - 10
PROBLEMS WITH OPTIMIZATION
#2. Compilers usually have to make a trade-off for a variety of conflicting objectives such as
space vs. time.
#3. A compiler only deals with a part of the full code, often a single class or c file not the entire
project.
#5. Complex phases of optimization, often make it difficult to find an optimal sequence for
application of these optimization phases.
18
CHAPTER – F
SUMMARY AND CONCLUSION
This report discussed the need and implementation of code optimization. We first came to
define what are the ground rules for a fair optimization technique. Then we classified them in
a broader sense. After having an overview of the techniques, we dived into the factors which
can influence our choice of the techniques that we make.
A detailed description of each technique and its subtypes were discussed with examples
explaining the usage and the actual implementation of most of them. The problems related to it
were discussed after that, which comprises of the challenges and shortcomings of the currently
used practices for code optimization.
As a conclusion, code optimization is a process which requires the actual context of the problem
and the priorities of the intended use case. It requires a tradeoff between space and time
requirements. What looks like an optimized code or compiler for a program may give disastrous
results in some other application. Therefore, it is instrumental to choose wisely.
19
APPENDIX
Below is a list of the useful websites that were used in the making of this project under the
guidance of the concerned faculty.
1. https://en.wikipedia.org/wiki/Optimizing_compiler
2. All the examples of code optimizations were inspired from the website
http://www.compileroptimizations.com
20