Thursday 28 August 2014

Some GCC optimizations

When I first started using GCC, I was impressed by the sheer intelligence of its optimization options (O, O1,O2,O3).  Take the following code as an example:-



The above code, when compiled with gcc with no optimizations takes about 30 ms to execute. However, when compiled with optimization '-O3', the execution takes 0 ms. 

Why, you ask?

This is because the compiler takes the value 1,000,000 and moves it to a register without executing the loop a million times. 
SOO COOOL!

Henceforth, I started looking at the types of optimization options the gcc compiler provides us with on https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html. I have tried to explain a few below:-

1. 'fno-unwind-tables' :- 

Unwind tables are tables in an elf file consisting of information to unwind the call stack in the event of an exception. 
This optimization flag helps reduce the code size by deleting the unwind tables from the elf file. However, deleting such a section can have its own repercussions, such as backtrace() stops working etc.

2. 'fomit-frame-pointer' :-

This optimization flag helps reduce the code execution time by removing the idea of a frame pointer and using the FP register as just another register.
This flag was the one that most interested me as it questioned the idea of a frame pointer that I learnt at school. As far as I can recall, the FP is used for the following 2 functions:-

1. When exiting from a function, it is the value in the FP that is moved to the SP
2. The local variables are always referenced using the FP in assembly code. 

Now, on the otherhand, it is the SP itself that is used to reference the local variables in assembly code. That surely must be a job difficult to implement in the compiler. 

3.  'fprofile-arcs' :-
4. 'floop-optimize' :-


No comments:

Post a Comment