fINALsEMINAR.ppt Auto Saved]

Embed Size (px)

Citation preview

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    1/26

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    2/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [2]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    AGENDA

    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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    3/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [3]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    INTRDUCTION

    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.

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    4/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [4]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Cont

    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.

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    5/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [5]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    CODE OPTIMIZATIONS

    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 codes 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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    6/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [6]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    OBJECTIVES

    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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    7/26

    //BEFORE OPTIMIZATION

    1. #include

    2. main() {

    3. int a=2,b,c;

    4. if(a ) {

    5. printf(possible);

    6. }

    7. a= b+ c;

    8.

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

    10. return 0;

    11. }N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [7]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Elimination Of Extra Blank Space,blank Lines

    //AFTER OPTIMIZATION

    1.#include

    2. main() {

    3.int a,b,c;

    4.if(a){

    5.printf(possible);

    6.}

    7.a= b+ c;

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

    9.return 0;

    10.}

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    8/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hnolo

    gy

    [8]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    IMPLEMENTED ALGORITHM

    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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    9/26

    N

    ation

    alIns ti

    tuteofSci e

    n

    ce&

    Tec

    hno

    logy

    [9]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Elimination Of Variables Which are not being used ButDeclared

    //BEFORE OPTIMIZATION

    1.#include

    2.main() {

    3. int var1=1, var2=2, var3=3, var4=5;

    4.if(var1){

    5.printf(possible);

    6.}

    7. Var1= var2+ var3;

    8. printf("\n\nthis is a testl......");

    9.return 0;

    10.}

    //AFTER OPTIMIZATION

    1.#include

    2.main() {

    3. int var1=1, var2=2, var3=3;4.if(var1){

    5.printf(possible);

    6.}

    7. Var1= var2+ var3;

    8. printf("\n\nthis is a testl......");

    9.return 0;

    10.}

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    10/26

    N

    ation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [10]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Cont..

    Var1 Var2 Var3 var4Declared Variables

    Var1 Var1 Var2 Va3Used Variables :

    Var4Unused Variable

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    11/26

    N

    ation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [11]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    ALGRITHM TO FIND DECLARED VARIABLE

    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) thenwrite into declared.c

    else write new line char to declared.c

    STEP 4: Now all declared variables are in declared.c

    STEP 5:END

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    12/26

    N

    ation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [12]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    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 chara. 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 .

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    13/26

    N

    ation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [13]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    ELIMINATION OF DEAD FUNCTIONS

    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, animportant 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.

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    14/26

    Nation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [14

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Cont

    // BEFORE OPTIMIZATION

    1. int foo(void)

    2. {

    3. int a = 24;

    4. int b = 25;/*Assignment to

    5. dead variable */

    6. int c;

    7. c = a

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    15/26

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    16/26

    Nation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [16

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Cont

    1. #include

    2. main()

    3. {

    4. int a , b=1,c=2,d,k;5. Char ch;

    6. a=b*c;

    7. b=b +2;

    8. c=c+3;

    9. d=a;

    10. k=b;

    11. return 0; }

    a b c d k

    Defined Variables

    b c b c a b

    Used Variables

    d k

    Dead Variables

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    17/26

    Nation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [17

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    ELIMINATION OF UNUSED HEADER FILES

    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.

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    18/26

    Nation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [18

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    IMPLEMENTED ALGRITHM

    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 filefun_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 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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    19/26

    Nation

    alIns t

    ituteofSci e

    n

    ce&

    Tec

    hno

    logy

    [19

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    IMPLEMENTED ALGRITHM

    1. #include

    2. #include

    3. #include

    4. #include

    5. main()

    6. {7. int a,b,c;

    8. char str[12];

    9. scanf("%s",str);

    10. a=strlen(str);

    11. b=power(a);12. b=power(a);

    13. printf("hi this is sunil ");

    14. return 0;

    15. }

    main

    scanf

    strlen

    power

    power

    printf

    strlen

    Power`

    power

    strlen

    power

    1. #include

    2. #include

    3. #include

    4. main()

    5. {6. int a,b,c;

    7. char str[12];

    8. scanf("%s",str);

    9. a=strlen(str);

    10. b=power(a);

    11. b=power(a);

    12. printf("hi this is sunil ");

    13. return 0;

    14.}

    AFTER

    OPTIMIZATION

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    20/26

    Nation

    alIns t

    ituteofSci en

    ce&

    Tec

    hno

    logy

    [20

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    TEST FOR PROGRAM TERMINATION

    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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    21/26

    Nation

    alIns t

    ituteofSci en

    ce&

    Tec

    hno

    logy

    [21]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    ALGORITHM To Test Program TerminationHaving for Loop

    Input file: test.c Output file:forinfinite.c

    STEP 1: scan each character (ch)of input file upto

    EOF and store the character in a array

    STEP 2: if ch=='\n' or ch==';' or ch=='(' make the

    array null and set i=0STEP 3: compare the string with "for"

    if true then set the pointer at the current

    position and scan upto the character ')'

    if character is any of the symbols '>' or '=' or '

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    22/26

    Nation

    alIns t

    ituteofSci en

    ce&

    Tec

    hno

    logy

    [22]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    TEST WITH while LoopInput file: test.c Output file: whileinfinite.c

    STEP 1: scan each character (ch)of input file upto

    EOF and store the character in a array

    if ch=='\n' or ch==';' or ch=='(' make the array

    null and set i=0

    STEP 2: compare the string with "while"

    if true then set the pointer at the current positionand scan upto the character ')'

    if ch>48 && ch

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    23/26

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    24/26

    Nation

    alIns t

    ituteofSci en

    ce&

    Tec

    hno

    logy

    [24]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    Recommendation for Future Improvements

    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.

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    25/26

    Nation

    alIns t

    ituteofSci en

    ce&

    Tec

    hno

    logy

    [25]

    PROJECT PRESENTATION-2011

    Sunil Kumar Patra & Debadatta Nayak Roll No:200710427 & 200710374

    REFERENCES

    [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

  • 8/6/2019 fINALsEMINAR.ppt Auto Saved]

    26/26

    Nation

    alIns t

    ituteofSci en

    ce&

    Tec

    hno

    logy

    [26]

    PROJECT PRESENTATION-2011

    S il K P t & D b d tt N k R ll N 200710427 & 200710374

    THANK YOU