Sunday, October 4, 2015

Compiler Options

A few weeks ago, we learn about gcc compiler and few of it's options, using these options your compiler will change/move your code around to make your code for better performance. I'm going to discuss about GCC compiler options which I researched on for my presentation for my Software Portability and Optimization course.

-ftree-dce is one of the option which I've chose for my research. -ftree-dce performs dead code elimination (DCE) on trees. This flag is enabled by default at -O and higher.

Example: 
int main(){
    int t = 5 + 5; //dead code
    int r = 3 + 3;
    printf(“Result is %d”, r);
}

In this example variable ‘t’ is considered a dead variable because it takes result of two numbers but the value of ‘t’ is never used. It isn't going to affect the final result so, calculating 5 + 5 and assigning the result to a variable will consume resources and space for something that can be avoided. Dead Code Elimination checks for conditions that will never be true and unnecessary calculations on variables which are not affecting the final result and it will remove that block of code before it converts the code into machine language which will provide better performance in your program by eliminating steps that are unnecessary.

Hence compiler option is enabled by default at -O level, so the dead code elimination option is always in use, unless you compile with –fno-tree-dce but still there are other similar options which eliminates dead code.
These are: -fdump-rtl-dce, -fdump-rtl-dce1, -fdump-rtl-dce2 and –ftree-builtin-call-dce which gives warning on unused variables it's default enabled at -O2  

-fmove-loop-invariants is another compiler option which I've chose for my research. -fmove-loop-invariants enables the loop invariant motion pass in the RTL loop optimizer and its enabled at level -O1. Its purpose is to find any value or expression that remains the same on all iterations of the loop. By substituting out the expressions which are unchanged on all iteration the computer will have less number of operations to perform then original.

Example:

Loop invariants transform following code:
             for (int i = 0; i < n; i++) {    
                   x = y + z;
                   array[i] = 6 * i + x * x;
             }
 to:
              Int x = y + z;
              Int t = x * x;

              for (int i = 0; i < n; i++){ 
                    array[i] = 6 * i + t;
              }
Let's calculate number of operation in each code if n is 10. For untransformed code it will be 10 iterations * (2 for x=y+z + 4 for array[i] = 6 * i + x * x ) which is 60 operations and if we look at transformed code it will be 4 for code before loop + 10 iterations * (3 for array[i] = 6 * i + t) which is 34 operations. So, in small code as this we have almost half the number of difference and which means less processing for computer and you could have better performing applications.

No comments:

Post a Comment