if you want to remove an article from website contact us from top.

    machine independent optimization in compiler design

    Mohammed

    Guys, does anyone know the answer?

    get machine independent optimization in compiler design from screen.

    Code Studio

    NINJA FUN FACT

    67% of programming jobs aren’t in the technology industry.

    स्रोत : www.codingninjas.com

    Machine Independent Code optimization in Compiler Design

    A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

    Machine Independent Code optimization in Compiler Design

    Difficulty Level : Hard

    Last Updated : 16 Sep, 2021

    Read Discuss Practice Video Courses

    Machine Independent code optimization tries to make the intermediate code more efficient by transforming a section of code that doesn’t involve hardware components like CPU registers or any absolute memory location. Generally, it optimizes code by eliminating redundancies, reducing the number of lines in code, eliminating useless code or reducing the frequency of repeated code. Thus can be used on any processor irrespective of machine specifications.

    Machine independent code optimization can be achieved using the following methods:

    Function Preserving  Optimization :

    Function Preserving optimizations deals with the code in a given function in an attempt of reducing the computational time. It can be achieved by following methods:

    Common Subexpression elimination

    Folding

    Dead code elimination

    Copy Propagation

    1. Common Subexpression elimination :

    A common subexpression is the one which was computed and doesn’t change after it last computation, but is often repeated in the program. The compiler evaluates its value even if it does not change. Such evaluations result in wastage of resources and time. Thus it better be eliminated. Consider an example:

    //Code snippet in three address code format

    t1=x*z; t2=a+b; t3=p%t2;

    t4=x*z; //t4 and t1 is same expression

    //but evaluated twice by compiler.

    t5=a-z;

    // after Optimization

    t1=x*z; t2=a+b; t3=p%t2; t5=a-z;

    It is troublesome if a common subexpression is often repeated in a program. Thus it needs to be eliminated.

    2.  Constant Folding :

    Constant Folding is a technique where the expression which is computed at compile time is replaced by its value. Generally, such expressions are evaluated at runtime, but if we replace them with their values they need not be evaluated at runtime, saving time.

    //Code segment int x= 5+7+c; //Folding applied int x=12+c;

    Folding can be applied on boolean, integers as well as on floating point numbers but one should be careful with floating point numbers. Constant folding is often interleaved with constant propagation.

    Constant Propagation :

    If any variable is assigned a constant value and used in further computations, constant propagation suggests using the constant value directly for further computations. Consider the below example

    // Code segment int a = 5; int c = b * 2; int z = a;

    //Applying constant propagation once

    int c = 5 * 2; int z = a;

    //Applying constant propagation second time

    int c = 10; int z = a;

    //Applying constant propagation last time

    int z = a[10];

    3. Dead Code Elimination :

    Dead code is a program snippet that is never executed or never reached in a program. It is a code that can be efficiently removed from the program without affecting any other part of the program. In case, a value is obtained and never used in the future, it is also regarded as dead code. Consider the below dead code:

    //Code

    int x= a+23; //the variable x is never used

    //in the program. Thus it is a dead code.

    z=a+y;

    printf("%d,%d".z,y);

    //After Optimization

    z=a+y;

    printf("%d,%d".z,y);

    Another example of dead code is assign a value to a variable and changing that value just before using it. The previous value assignment statement is dead code. Such dead code needs to be deleted in order to achieve optimization.

    4. Copy Propagation :

    Copy Propagation suggests to use one variable instead of other, in cases where assignments of the form x=y are used. These assignments are copy statements. We can efficiently use y at all required place instead of assign it to x. In short, elimination of copies in the code is Copy Propagation.

    //Code segment ----; a=b; z=a+x; x=z-b; ----;

    //After Optimization

    ----; z=b+x; x=z-b; ----;

    Another kind of optimization, loop optimization deals with reducing the time a program spends inside a loop.

    Loop Optimizations :

    The program spends most of its time inside the loops. Thus the loops determine the time complexity of the program. So, in order to get an optimal and efficient code, loop optimization is required. In order to apply loop optimization, we first need to detect the loop using control flow analysis with the help of program flow graph. A cycle in a program flow graph will indicate presence of a loop. Note that, the code from Intermediate code Generation phase which is in three-address format is given as input to the optimization phase. It is difficult to identify loops in such a format, thus a program flow graph is required.

    The program flow graph consists of basic blocks, which is nothing but the code divided into parts or blocks and show the execution flow of the code,

    स्रोत : www.geeksforgeeks.org

    Machine Independent

    Machine Independent with introduction, Phases, Passes, Bootstrapping, Optimization of DFA, Finite State machine, Formal Grammar, BNF Notation, YACC, Derivation, Parse Tree, Ambiguity, Syntax directed Translation, slr 1 parsing etc.

    Machine-Independent Optimization

    Machine independent optimization attempts to improve the intermediate code to get a better target code. The part of the code which is transformed here does not involve any absolute memory location or any CPU registers.

    The process of intermediate code generation introduces much inefficiency like: using variable instead of constants, extra copies of variable, repeated evaluation of expression. Through the code optimization, you can remove such efficiencies and improves code.

    It can change the structure of program sometimes of beyond recognition like: unrolls loops, inline functions, eliminates some variables that are programmer defined.

    Code Optimization can perform in the following different ways:

    (1) Compile Time Evaluation:

    (a) z = 5*(45.0/5.0)*r

    Perform 5*(45.0/5.0)*r at compile time.

    (b) x = 5.7 y = x/3.6

    Evaluate x/3.6 as 5.7/3.6 at compile time.

    (2) Variable Propagation:

    Before Optimization the code is:

    c = a * b x = a till d = x * b + 4

    After Optimization the code is:

    c = a * b x = a till d = a * b + 4

    Here, after variable propagation a*b and x*b identified as common sub expression.

    (3) Dead code elimination:

    Before elimination the code is:

    c = a * b x = b till d = a * b + 4

    After elimination the code is:

    c = a * b till d = a * b + 4

    Here, x= b is a dead state because it will never subsequently used in the program. So, we can eliminate this state.

    (4) Code Motion:

    It reduces the evaluation frequency of expression.

    It brings loop invariant statements out of the loop.

    do { item = 10;

    valuevalue = value + item;

    } while(value<100);

    //This code can be further optimized as

    item = 10; do {

    valuevalue = value + item;

    } while(value<100);

    (5) Induction Variable and Strength Reduction:

    Strength reduction is used to replace the high strength operator by the low strength.

    An induction variable is used in loop for the following kind of assignment like i = i + constant.

    Before reduction the code is:

    i = 1; while(i<10) { y = i * 4; }

    After Reduction the code is:

    i = 1 t = 4 { while( t<40) y = t; t = t + 4; } ← Prev Next → Learn more

    स्रोत : www.javatpoint.com

    Do you want to see answer or more ?
    Mohammed 14 day ago
    4

    Guys, does anyone know the answer?

    Click For Answer