You are on page 1of 26

PROJECT PRESENTATION-2011

National Institute of Science & Technology

A GENERAL CODE OPTIMIZER

PROJECT ID - 10120

Presented By

Sunil Kumar Patra ROLL NO:200710427

Debadatta Nayak ROLL NO:200710374

Under the guidance of


Mr. Chandan Kumar Behera

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[1]
PROJECT PRESENTATION-2011

AGENDA
National Institute of Science & Technology

 Introduction
 Code Optimization
 Objectives
 Implemented Algorithms
 Elimination Of Blank Lines & Blank Spaces
 Elimination Of Unused Variables
 Elimination Of Dead Functions
 Elimination Of Unused Header File
 Test For Program Termination
 Conclusion
 Recommendation for Future Improvements
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[2]
PROJECT PRESENTATION-2011

INTRDUCTION
National Institute of Science & Technology

 Code optimization involves the application of rules and algorithms to


program code with the goal of making it faster, smaller, more efficient, and so
on.
 Optimizations can be performed at several levels (e.g. source code,
intermediate representations), and by various parties, such as the developer or
the compiler/optimizer.
 There are broadly two types of optimizations, optimizations for speed and
for memory space. Sometimes an optimization does both.

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[3]
PROJECT PRESENTATION-2011

Cont…
National Institute of Science & Technology

Compilation optimization must meet, the following design


objectives:
The optimization must be correct, that is, preserve the
meaning of the compiled program.
The optimization must improve the performance of many
programs
The compilation time must be kept reasonable
The engineering effort required must be manageable.

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[4]
PROJECT PRESENTATION-2011

CODE OPTIMIZATIONS
National Institute of Science & Technology

Optimization is the process of transforming a piece of code to make more


efficient (either in terms of time or space) without changing its output or
side-effects.
The only difference visible to the code’s user should be that it runs faster
and/or consumes less memory.
It is really a misnomer that the name implies we are finding an "optimal“
solution— in truth, optimization aims to improve, not perfect, the result.
Code optimization can be also broadly categorized as platform-dependent
and platform-independent techniques

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[5]
PROJECT PRESENTATION-2011

OBJECTIVES
National Institute of Science & Technology

 Elimination of extra blank spaces, blank lines


 Elimination of the variables which are not being used but declared
 Elimination of dead functions
 Eliminate of header file, which are not being used
 Elimination of infinite loops

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[6]
PROJECT PRESENTATION-2011

Elimination Of Extra Blank Space,blank Lines


National Institute of Science & Technology

//BEFORE OPTIMIZATION //AFTER OPTIMIZATION


1. #include<stdio.h> 1.#include<stdio.h>
2. main() { 2. main() {
3. int a=2,b,c; 3.int a,b,c;
4. if(a ) { 4.if(a){
5. printf(“possible”); 5.printf(“possible”);
6. } 6.}
7. a= b+ c; 7.a= b+ c;

8. 8.printf("\nThis is a test");

9. printf("\nThis is a test"); 9.return 0;

10. return 0; 10.}

11. }
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[7]
PROJECT PRESENTATION-2011

IMPLEMENTED ALGORITHM
National Institute of Science & Technology

STEP 1: Input file:test.c (r-mode) Output File:blank.c (w-mode)

STEP 2:Scan each character(ch) upto EOF

a.if ch!=‘\n’ and ch!=‘\t’ then

check, if ch equals any of ; { } > then

write ch into blank.c

STEP 3: Open blank.c in r-mode Scan each char Upto EOF

a.if ch equals space increment the count

else if count is 1 then write space into blank1.c

b.if ch not equals space write direct into blank1.c

STEP 4: Check the output in blank1.c containing no extra blank space and blank
lines
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[8]
PROJECT PRESENTATION-2011

Elimination Of Variables Which are not being used But


National Institute of Science & Technology

Declared
//BEFORE OPTIMIZATION //AFTER OPTIMIZATION
1.#include<stdio.h> 1.#include<stdio.h>
2.main() { 2.main() {
3. int var1=1, var2=2, var3=3, var4=5; 3. int var1=1, var2=2, var3=3;
4.if(var1){ 4.if(var1){
5.printf(“possible”); 5.printf(“possible”);
6.} 6.}
7. Var1= var2+ var3; 7. Var1= var2+ var3;
8. printf("\n\nthis is a testl......"); 8. printf("\n\nthis is a testl......");
9.return 0; 9.return 0;
10.} 10.}
Sunil Kumar Patra & Debadatta Nayak [9]
Roll No:200710427 & 200710374
PROJECT PRESENTATION-2011

Cont..
National Institute of Science & Technology

Declared Variables Var1 Var2 Var3 var4

Used Variables : Var1 Var1 Var2 Va3

Unused Variable Var4

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[10]
PROJECT PRESENTATION-2011

ALGRITHM TO FIND DECLARED VARIABLE


National Institute of Science & Technology

STEP 1:Input File:test.c Output File :declared.c


STEP 2:Scan Each char ch from test.c upto EOF
a.if ch equals ‘{‘ increment count by 1
b.if ch equals ‘(’ and count >= 1 then Goto EOF
else if ch equals space ‘ ’
assign the file pointer to current position
1.write each ch into sec.c until ‘;’ encounters
STEP 3:Open sec.c in r-mode scan each character upto EOF
a.if ch is any alphabet (Upper and Lower Case) or Numeric(0-9) then
write into declared.c
else write new line char to declared.c
STEP 4: Now all declared variables are in declared.c
STEP 5:END
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[11]
PROJECT PRESENTATION-2011
National Institute of Science & Technology

ALGORITHM TO FIND USED VARIABLE AND COMPARE

STEP 1: Input : test.c


STEP 2: Read each char ch from test.c upto EOF
a. If ch equals ‘=‘ then move file pointer backward upto ‘\n’ and forward
upto ‘;’ then write these into a file
STEP 3: Open that File and scan each char
a. if any alphabet(Upper and Lower Case) and numeric char encounters
then store it to a 2-d char Array i.e usedvar[ ][ ]
STEP 4: Open declared.c in r-mode scan each char upto EOF
and store in a 2-d char Array i.e declared[ ][ ]
STEP 5: Then Compare each string from declared[ ][ ] with usedvar[ ][ ] then
the unmatched strings are Unused Variables .

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[12]
PROJECT PRESENTATION-2011

ELIMINATION OF DEAD FUNCTIONS


National Institute of Science & Technology

 Dead code elimination is a compiler optimization that removes code


that does not affect the program.
 Removing such code has two benefits: it shrinks program size, an
important consideration in some contexts, and it lets the running
program avoid executing irrelevant operations, which reduces its
running time.
 Dead code includes code that can never be executed (unreachable
code), and code that only affects dead variables that is variables that are
irrelevant to the program.

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[13]
PROJECT PRESENTATION-2011

Cont…
National Institute of Science & Technology

// BEFORE OPTIMIZATION // AFTER OPTIMIZATION


1. int foo(void) 1. int foo(void)
2. { 2. {
3. int a = 24; 3. int a = 24;
4. int b = 25; /* Assignment to 4. int c;
5. dead variable */ 5. c = a << 2;
6. int c; 6. return c;
7. c = a << 2; 7. }
8. return c;
9. b = 24; /* Unreachable code */
10. return 0;
11. }
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[14]
PROJECT PRESENTATION-2011

IMPLEMENTED ALGORITHM
National Institute of Science & Technology

STEP 1: Input file:test.c

STEP 2: Scan each character from test.c upto EOF


a. Find the defined variables by traversing backwards from ‘=‘ to ‘\n’ in
every expression and store in defined_var[ ][ ].
b. Find the used variables by traversing forwards from ‘=’ to ‘;’ in every
expression and store in used_var[ ][ ].

STEP 3: Then Compare each string from defined_var[ ][ ] with used_var[ ][ ] so


the unmatched string in defined_var[ ][ ] are the dead variables and store in a 2-
D array

STEP 4: End

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[15]
PROJECT PRESENTATION-2011

Cont…
National Institute of Science & Technology

1. #include <stdio.h>
Defined Variables
2. main()
a b c d k
3. {
4. int a , b=1,c=2,d,k;
Used Variables
5. Char ch; b c b c a b
6. a=b*c;
7. b=b +2;
8. c=c+3;
Dead Variables
9. d=a; d k
10. k=b;
11. return 0; }
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[16]
PROJECT PRESENTATION-2011

ELIMINATION OF UNUSED HEADER FILES


National Institute of Science & Technology

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming and
error-prone.
So finally eliminating the header files which are not being used in the
program but declared can optimize the program with respect to program size
and running time.

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[17]
PROJECT PRESENTATION-2011

IMPLEMENTED ALGRITHM
National Institute of Science & Technology

STEP 1: Input File :Test.c


STEP 2: Scan Each Character ,findout each function used in test.c and write
it into a file fun_list.c
STEP 3: Except functions defined under stdio.h(main(),printf(),scanf()
….etc) and if(),for()…etc write all other function into another file
fun_list1.c
STEP 4: From fun_list1.c eliminate the repeated function and make single
entry of functions into another file FUN_LIST.c
STEP 5:From FUN_LIST.c,for each function findout the corresspnding
header file name by comparing with functions of standard libraries.
STEP 6: Write #include<stdio.h> as default header into include_header.c
STEP 7: Now append the result of STEP 5 into file include_header.c
STEP 8: Hence include_header.c contains all necessery headers, so now add
rest part of source program-test.c i.e except headers into include_header.c
STEP 9:END Result : include_header.c is the optimized code of test.c i.e
without unused header files
Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[18]
PROJECT PRESENTATION-2011

IMPLEMENTED ALGRITHM
National Institute of Science & Technology

1. #include<stdio.h> main strlen strlen


1. #include<stdio.h>
2. #include<string.h> scanf 2. #include<string.h>
Power` power
3. #include<math.h>
strlen 3. #include<math.h>
power
4. #include<stdlib.h>
4. main()
5. main() power
5. {
6. { power 6. int a,b,c;
7. int a,b,c;
printf 7. char str[12];
8. char str[12];
8. scanf("%s",str);
9. scanf("%s",str); AFTER 9. a=strlen(str);
10. a=strlen(str); OPTIMIZATION 10. b=power(a);
11. b=power(a);
11. b=power(a);
12. b=power(a);
12. printf("hi this is sunil ");
13. printf("hi this is sunil ");
13. return 0;
14. return 0;
14.}
15. }

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[19]
PROJECT PRESENTATION-2011

TEST FOR PROGRAM TERMINATION


National Institute of Science & Technology

An infinite loop is a sequence of instructions in a computer program


which loops endlessly.
Typically they are divided into two categories, "for loops" and "while loops“
There are many patterns in code that will result in an infinite loop and different
programming languages have varying approaches.
The main aim of our program is not eliminating the infinite loops, but
detecting the cases whether an infinite loop exits in the source code itself or not.
If the source program contains an infinite loop then it will show a printed
message “Infinite loop is present in the program”

Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374[20]
PROJECT PRESENTATION-2011

ALGORITHM To Test Program Termination


National Institute of Science & Technology

Having ‘for’ Loop


Input file: test.c Output file:forinfinite.c EXAMPLE 1
STEP 1: scan each character (ch)of input file upto 1.unsigned int i;
EOF and store the character in a array 2.for (i = 1; i > 0; i++) {
STEP 2: if ch=='\n' or ch==';' or ch=='(' make the 3. /* loop code */
array null and set i=0 4.}
STEP 3: compare the string with "for"
if true then set the pointer at the current
position and scan upto the character ')' EXAMPLE 2
if character is any of the symbols '>' or '=' or '<'1.int i;
or '+' or '-' then scan upto ';' and store it in a 2.for (i = -1; i <0; i--) {
array 3. /* loop code */
if ch>=48 && ch<=57 then there may be a 4.}
presence of infinite loop
STEP 4: END

Sunil Kumar Patra & Debadatta Nayak [21]


Roll No:200710427 & 200710374
PROJECT PRESENTATION-2011

TEST WITH ‘while’ Loop


National Institute of Science & Technology

Input file: test.c Output file: whileinfinite.c


STEP 1: scan each character (ch)of input file upto EXAMPLE
EOF and store the character in a array 1.#include<stdio.h>
if ch=='\n' or ch==';' or ch=='(' make the array
null and set i=0 2.main(){
STEP 2: compare the string with "while" 3.while(1){
if true then set the pointer at the current position
and scan upto the character ')' 4.printf(“\nthis is a infinite
if ch>48 && ch<=57 loop”);
print a message there may be a presence of infinite
loop 5.}
then again set the pointer at the current position and 6.return 0;
scan upto the character '}'
STEP 3: compare the string with "break" or 7.}
"exit(0)"
if true then infinite loop is not present
STEP 4: END
Sunil Kumar Patra & Debadatta Nayak [22]
Roll No:200710427 & 200710374
PROJECT PRESENTATION-2011

CONCLUSION
National Institute of Science & Technology

Our discussion has focused on elimination of blank lines and blank space,
removing unused variable, dead code elimination, elimination of unused header
files and automatic test for program termination.
To read a file from disk, sort its contents and write the result back out, ought to
be a very doable performance optimization exercise.
Turning on the optimizations in the compiler sometimes exposes new
problems in the source program; thus testing must again be performed on the
optimization code.
Complete optimization is impossible; there is always room left to optimize,
thus it is pointless to sustain too much effort in pursuit of it. Absolutely
optimization is also not a completely unattainable goal.
Sunil Kumar Patra & Debadatta Nayak [23]
Roll No:200710427 & 200710374
PROJECT PRESENTATION-2011

Recommendation for Future Improvements


National Institute of Science & Technology

 We have focused on primary level of dead code removal i.e. dead


variable elimination only. Future work can be done on eliminating dead
functions.
 In our project we have just tested the program termination using certain
infinite loop conditions. Future work can be done on automatic method to
test program termination using mathematical dependency of logic
implemented inside the loop, so as to solve the HALTING problem.

Sunil Kumar Patra & Debadatta Nayak [24]


Roll No:200710427 & 200710374
PROJECT PRESENTATION-2011

REFERENCES
National Institute of Science & Technology

[1] Alfred V Aho , Ravi Sethi and J D Ullman, ”Compilers: Principles,


Techniques, and Tools”,vol-1 PP 125-160,Aug-2010

[2] S.Muchnick,“Advanced Compiler Design and


Implementation”,vol- 2 Kaufmann, 1997

[3] Byron Cook,”Termination” ,Microsoft Research http://research.micr


osoft.com/en-us/um/cambridge/projects/terminator

[4] Cook, B., Gotsman, A., Podelski, A., Rybalchenko, A.AND VARDI
M. Y. “Proving that programs eventually do something good”,
POPL, PP 1-17, 2007

Sunil Kumar Patra & Debadatta Nayak [25]


Roll No:200710427 & 200710374
PROJECT PRESENTATION-2011
National Institute of Science & Technology

THANK YOU

Sunil Kumar Patra & Debadatta Nayak [26]


Roll No:200710427 & 200710374

You might also like