 Hello everyone. Welcome to the session on code optimization. Code optimization is one of the phase of the compiler, which is optimizing the code. Usually it enhances, improves the performance of the execution of the code by optimizing the given generated code. The code may be intermediate code or it may be the target code or means any one of these can be optimized and it is usually improving the execution speed of the program. Now at the end of the session the learner will be able to identify the types of general code optimization technique. There are various code optimization techniques. They can identify and if a given block of code is given then they can optimize the given block of code and the block of code might be in the three address code format. Let us come to the formal description of the code optimization. Usually this code optimization is a form of a program transmission transformation technique and it tries to as I told earlier improves the intermediate code by making it fewer resources means it consumes fewer number of resources. The resources may be CPU or it may be the memory, any kind of thing and what is the main objective of that? It improves the speed of the execution of the total final machine code. The objective of the code optimization is it should be correct. It must be correct one. So whenever we are optimizing the code from one form to another one then the code should be correct, the earlier code and the optimized code and it should not change the meaning of the program. So if the program of the it changing its meaning then that will be incorrect. So it should not change the meaning of the program and optimization should improve the performance of the program by that because the main objective of code optimization is improving the performance. So the speed is improving in the form of CPU cycles or it may use the less number of memory space. So that might be the optimization thing and the optimization process should not delay the overall compiling process means it should not happen that it is taking much time in the compiling process to do the code optimization because it may affect the speed of the compiler also. These are the main objective of the code optimization. Let us see what are the types of code optimization techniques. Basically there are two types of code optimization technique called as machine independent optimization and machine dependent optimization. Now a machine independent optimization means it is not considering any target machine for optimizing the code. So it is improving usually the intermediate code which is generated which is not dependent on any target code machine and the code does not involve as it is not dependent on the target code. So it is not involving any CPU registers or the absolute memory locations or any kind of thing. So this kind of optimization is called as a machine independent optimization. Whereas in machine dependent optimization what will happen? It improves basically the target code which is generated because the target code is based on the target machine architecture. So what kind of machine it is based on that? The machine dependent optimization is working. So automatically it is involving the CPU registers. It may have absolute memory references. Whatever the kind of relative things are there based on the machine architecture it is working on that one rather than the relative references. Now let us see what are the various techniques in machine independent optimization. Basically there are these many forms are there. Machine optimization techniques like compile time evaluation, common sub-expression, elimination, variable propagation, dead code elimination, code movement and strength reduction. And in this we can take common sub-expression elimination and variable propagation both in the same format because sometimes common sub-expression elimination is based on the variable propagation also. Let us see one by one. So the first one compile time evaluation. See the name itself is saying it. Consider the example that this example is there A equal to some expression is there which is a floating point calculation and multiplied by R is some value. If this is the statement how this statement can be optimized. You can see that whatever this calculation is there it is not at all changing during the execution time because it is as it is the execution is working based on that. So it is better that whenever we are optimizing the code at the compile time only better to calculate the fixed constant value. So when it performs the actual optimization what the compiler will do or the code optimization phase will do it will calculate the value of this particular sub-expression at the compile time only and during the execution time this will be taken as A equal to and whatever the value of this constant multiplied by R. So again it is not wasting the memory the speed of the CPU time for this one. Another example is here where we are taking x equal to 12.4 and y equal to x divided by 2.3. So in this case what happens the value of x is 12.4 and this is taken as here 12.4 divided by 2.3 and we can see that in between x equal to 12.4 and this y equal to x divided by 2.3 nothing is changing. So this might be again a constant expression like 12.4 divided by 2.3. So automatically y's value is calculated as 12.4 divided by 2.3 at the compile time only. So again during the execution time it is not again compiling it is directly taking that one right. Now the next part is common sub-expression elimination. What do you mean by common sub-expression like any if if a particular statement is there particular expression is there it is having many sub-expression the small parts. So what we are doing in this one is what this technique is working working on is whatever the identical expressions are there in the particular statement or an expression those identical statements are taken as a single evaluation means what if multiple times the same common sub-expression is used in an expression then that will be computed first and that computed value is used many times. Let us see an example here the example is a equal to b multiplied by c plus g some calculation is there and again another another statement is b multiplied by c multiplied by e another calculation is there. Now in this one what will happen is you can see that b multiplied by c and b multiplied by c both are the same expressions and in between these there is not any statement which is changing the value of b or value of c. So what we can do is we can replace this or the code optimization will replace it in this format where temporary is equal to b multiplied by c. So some calculation some temporary variable is taken here and it is calculating only once b multiplied by c and that once calculated value instance is used here as TMP and TMP. So this is what the common sub-expression elimination type is there are various of kind of statements are there expressions are there where usually this is used. So what we can say common sub-expression as it is finding instances of the identical expressions and these are replaced by a single evaluation only like here what we did. Let us go to the next one next one is variable or copy propagation this form we can say as one of the form of the common sub-expression elimination. So here what happens it is identifying some common sub-expression here and that will be taken later. Let us see the example here c equal to a multiplied by b is there x equal to a is there and then d equal to x multiplied by b plus 4 is there. So this is one block of code and if this is the code what we can say is you can see here that c equal to a star b is there and value of x is assigned as a and that x value is used here as some value in this expression. We know that here what will happen the value of a is assigned in x. So both the values are taking the same values so variables are taking the same value. So this can be optimized in this format where it will what will happen is whatever this is there this will be taken and this instead of taking as x equal to a's value it is directly taking instead of this x it is taking directly the value of a here fine. So this is what the copy propagation is copy propagation the value of this variable is copied here but instead of taking this variable here directly we are taking this variable. Let us see what will happen for the statement x equal to a because now we are not using this statement that will be taken as a another part of optimization code optimization. What is there in this now let us see code this is the form of dead code elimination where it is removing the unused code. So earlier we have seen that x equal to a is there which is not at all used so automatically that is removed from this one in the dead code elimination. So the code which is not used by a block of statement that will be removed that is called as a dead code elimination. Code movement in this one what will happen it reduces the evaluation frequency of an expression and brings a loop invariant statements outside the loop. Let us see an example now in this example what will happen some expression is there and then a while loop is there and be equal to x plus y and this statement is there you can observe that this statement in a loop is unnecessarily every time is assigning the same value why because the value either value of x or value of y is changing in the while loop. So why to unnecessarily assigning the same value multiple times what we can do is we can move this code what we can move instead of writing this code inside the loop I am bringing back outside the loop so that only once this is aligned so this is called as code movement so it is the frequency of the evaluation of that expression is now reduces and instead of continuously doing many times in the loop it is going outside the loop. Now one question is there I want to ask you now this is the code which is given to you you have to pause the video and write the optimized code for the given statement and name the code optimization technique what you have applied this is the statement you can pause and see what might be this one this is it can be converted like this where this t1 equal to minus c and t3 equal to minus c both are equal statement so instead of using twice this one we can minimize that so instead of using wherever t3 is there we can use their t1 both so this is one format form of thing and later again the same expression like t2 equal to b star t1 t4 equal to b star t1 can be again taken as a common sub expression sub expression thing so the optimization technique here is common sub expression now the next one is a strength reduction what is this the name itself that it is removing the strength of the particular statement see this example some loop is there and i multiplied by 4 is there how we can reduce the strength we know that visually for the computer program when multiplication is taking place then it will take more time for the execution as compared to addition so what we are doing we are making this change as instead of taking i multiplied by 4 we are converting that as an addition so 4 times the value of i in the in this loop instead of this is changing here it is multiplying so this is called as the strength reduction so these are the types of the machine independent code optimization techniques some references i have used thank you