Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Compiler Design  >  Function Preserving Transformations

Function Preserving Transformations | Compiler Design - Computer Science Engineering (CSE) PDF Download

 
2.1 Function-Preserving Transformations 

  • There are a number of ways in which a compiler can improve a program without changing the function it computes.   
  • The transformations  
    o Common sub expression elimination,
    o Copy propagation,
    o  Dead-code elimination, and
    o Constant folding, are common examples of such function-preserving transformations. The other transformations come up primarily when global optimizations are performed.
  • Frequently, a program will include several calculations of the  same value, such as an   offset in an array. Some of the duplicate calculations cannot be avoided by the programmer because they lie below the level of detail accessible within the source language.
     

2.2 Common Sub expressions elimination:

  • An occurrence of an expression E is called a common sub-expression if E was previously computed, and the values of variables in E have not changed since the previous computation. We can avoid recomputing the expression if we can use the previously computed value.    
  • For example   t1: =4*i t2: =a [t1] t3: =4*j t4:=4*i t5: =n t 6: =b [t 4] +t 5

The above code can be optimized using the common sub-expression elimination as t1: =4*i t2: =a [t1] t3: =4*j t5: =n t6: =b [t1] +t5

The common sub expression t 4: =4*i is eliminated as its computation is already in t1. And value of i is not been changed from definition to use.
 

2.3 Copy Propagation:
Assignments of the form f : = g called copy statements, or copies for short. The idea behind the copy-propagation transformation is to use g for f, whenever possible after the copy statement f: = g. Copy propagation means use of one variable instead of another. This may not appear to be an improvement, but as we shall see it gives us an opportunity to eliminate x.

For example: x=Pi;
……
A=x*r*r;

The optimization using copy propagation can be done as follows:

A=Pi*r*r;

Here the variable x is eliminated.
 

2.4 Dead-Code Eliminations:

A variable is live at a point in a program if its value can be used subsequently; otherwise, it is dead at that point. A related idea is dead or useless code, statements that compute values that never get used. While the programmer is unlikely to introduce any dead code intentionally, it may appear as the result of previous transformations. An optimization can be done by eliminating dead code.

Example: i=0; if(i=1)
{
a=b+5; }

Here, ‘if’ statement is dead code because this condition will never get satisfied.

2.5 Constant folding:
o We can eliminate both the test and printing from the object code. More generally, deducing at compile time that the value of an expression is a constant and using the constant instead is known as constant folding. o One advantage of copy propagation is that it often turns the copy statement into dead code. 

For example, a=3.14157/2  can be replaced by a=1.570 there by eliminating a division operation.

2.6 Loop Optimizations:
o We now give a brief introduction to a very important place for optimizations, namely loops, especially the inner loops where programs tend to spend the bulk of their time. The running time of a program may be improved if we decrease the number of instructions in an inner loop, even if we increase the amount of code outside that loop. o Three techniques are important for loop optimization: §

  • code motion, which moves code outside a loop;  
     
  • Induction -variable elimination, which we apply to replace variables from inner loop.  
     
  • Reduction in strength, which replaces and expensive operation by a cheaper one, such as a multiplication by an addition
     

2.7 Code Motion: 

  • An important modification that decreases the amount of code in a loop is code motion. This transformation takes an expression that yields the same result independent of the number of times a loop is executed ( a loop-invariant computation) and places the expression before the loop. Note that the notion “before the loop” assumes the existence of an entry for the loop. For example, evaluation of limit-2 is a loop-invariant computation in the following whilestatement:  

while (i <= limit-2) /* statement does not change Limit*/ Code motion will result in the equivalent of t= limit-2; while (i<=t) /* statement does not change limit or t */

2.8 Induction Variables : 

  • Loops are usually processed inside out. For example consider the loop around B3.    
  • Note that the values of j and t4 remain in lock-step; every time the value of j decreases by 1, that of t4 decreases by 4 because 4*j is assigned to t4. Such identifiers are called induction variables.  
  • When there are two or more induction variables in a loop, it may be possible to get rid of all but one, by the process of induction-variable elimination. For the inner loop around B3 in Fig. we cannot get rid of either j or t4 completely; t4 is used in B3 and j in B4.    
  • However, we can illustrate reduction in strength and illustrate a part of the process of induction-variable elimination. Eventually j will be eliminated when the outer loop of B2 - B5 is considered.  

 
  Example:   

As the relationship t 4:=4*j surely holds after such an assignment to t 4 in Fig. and t4 is not changed elsewhere in the inner loop around B3, it follows that just after the statement j:=j -1 the relationship t4:= 4*j-4 must hold. We may therefore replace the assignment t 4:= 4*j by t4:= t4-4. The only problem is that t 4 does not have a value when we enter block B3 for the first time. Since we must maintain the relationship t4=4*j on entry to the block B3, we place an initializations of t4 at the end of the block where j itself is initialized, shown by the dashed addition to block B1 in second Fig.  

The replacement of a multiplication by a subtraction will speed up the object code if multiplication takes more time than addition or subtraction, as is the case on many machines.
 

2.9 Reduction in Strength: 

  •  Reduction in strength replaces expensive operations by equivalent cheaper ones on the target machine. Certain machine instructions are considerably cheaper than others and can often be used as special cases of more expensive operators.  
     
  • For example, x² is invariably cheaper to implement as x*x than as a call to an exponentiation routine. Fixed-point multiplication or division by a power of two is cheaper to implement as a shift. Floating-point division by a constant can be implemented as multiplication by a constant, which may be cheaper.  

     

 

 

The document Function Preserving Transformations | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Function Preserving Transformations - Compiler Design - Computer Science Engineering (CSE)

1. What are function preserving transformations in computer science engineering?
Ans. Function preserving transformations refer to the transformations that preserve the functionality of software programs. These transformations can be applied to the codebase to improve software quality, reduce redundancy, and optimize performance.
2. What are some examples of function preserving transformations?
Ans. Some examples of function preserving transformations include code refactoring, optimization, and obfuscation. Code refactoring entails restructuring the codebase to make it more readable and maintainable. Optimization involves improving the performance of software programs, while obfuscation refers to obscuring the code to prevent reverse engineering.
3. How do function preserving transformations improve software quality?
Ans. Function preserving transformations improve software quality by reducing code complexity, increasing readability, and improving maintainability. They also optimize performance, reduce redundancy, and ensure the correctness of software programs. By implementing function preserving transformations, software engineers can improve the overall quality of their software products.
4. What are the benefits of using function preserving transformations?
Ans. The benefits of using function preserving transformations include improved software quality, reduced maintenance costs, increased developer productivity, enhanced security, and better performance. By applying function preserving transformations, software engineers can improve the functionality and reliability of their software programs.
5. How do function preserving transformations impact software maintenance?
Ans. Function preserving transformations reduce software maintenance costs by improving the readability and maintainability of the codebase. They also make it easier to implement new features, fix bugs, and update software programs. By optimizing performance and reducing redundancy, function preserving transformations can also improve the overall efficiency of software maintenance.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

Function Preserving Transformations | Compiler Design - Computer Science Engineering (CSE)

,

mock tests for examination

,

practice quizzes

,

Viva Questions

,

study material

,

shortcuts and tricks

,

Free

,

Important questions

,

Objective type Questions

,

ppt

,

pdf

,

Extra Questions

,

video lectures

,

Previous Year Questions with Solutions

,

past year papers

,

Function Preserving Transformations | Compiler Design - Computer Science Engineering (CSE)

,

MCQs

,

Exam

,

Sample Paper

,

Summary

,

Function Preserving Transformations | Compiler Design - Computer Science Engineering (CSE)

;