Microcontroller Embedded C Programming Lecture 135| Compiler optimization and flags

  • Post author:
  • Post category:Blog

 

Compiler optimization and flags

 

 

In this article, let’s understand Optimization.

To understand and appreciate the use of the ‘volatile’ type qualifier, we have to first understand what happens when the compiler does optimization on your code.

 

Program optimization

Optimization is a series of actions taken by the compiler on your program’s code generation process to reduce a number of instructions(code space optimization), memory access time (time-space optimization), and Power consumption

If memory access time and the number of instructions come down, then you may get better power consumption. 

  • By default, the compiler doesn’t invoke any optimization on your program. 
  • You can enable the optimization using compiler flags.

 

GCC compiler flags to enable optimization

-O0, -O1, -O2, -O3 are the compiler flags.  These are the different optimization flags that you can supply to the compiler in order to optimize your code.

 

Figure 1. GCC Compiler flags to enable optimization
Figure 1. GCC Compiler flags to enable optimization

 

By default, the compiler uses no optimization. That means the optimization level is zero. 

This is not recommended for productions if you have limited code and  RAM space, because almost no optimization is carried out on your code.

And since the compiler doesn’t invoke any optimization algorithms this actually has the fastest compilation time.

This is debugging friendly because, when you are trying to debug your code there will be a one-to-one map between the instructions generated and the source code that you have written. 

And please note that a code which works with -O0 optimization may not work with -O0+ optimization levels, that is -O1, -O2, -O3. The code needs to be verified again.

 

If you use the -O1 optimization level, then it is kind of introducing moderate optimization to decrease memory access time and code space.

Figure 2. GCC Compiler flags to enable optimization
Figure 2. GCC Compiler flags to enable optimization

 

After that  -O2 optimization level. 

This carries out full optimization. And it results in slow compilation time because now the compiler has to invoke various optimization algorithms on your code. It results in slow compilation time and it is not debugging friendly. So, you cannot be able to debug your code properly.

If you enable a -O2 or -O3 optimization level, because there will not be a one-to-one mapping between the instructions generated and the source code that you have written. 

 

The next optimization level is -O3. 

-O3 means full optimization of -O2+ some more aggressive optimization steps will be taken by the compiler.

This also results in the slowest compilation time. And it may cause bugs in the program, so if you enable -O3 optimization level, then your application may not behave properly, so you have to revisit your code and you may have to rewrite your application or something. So, you should be careful with -O3 optimization level. 

And remember that, you need to work with different optimization levels to find out what works for you. There is no standard guideline that says you should use this optimization for this application.

These are the different optimization levels which you can activate from your IDE,  you can pass all these optimization flags from the IDE itself. So, we’ll explore that in the following article, and we’ll compare our code when we trigger different optimization levels.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply