This article was originally published on my other public account ‘Embedded Column’.
Original text:Detailed explanation of various optimization options in MDK-ARM, practical applications, and extended content
Many friends have been using MDK-ARM for a long time, but do not know what its optimization options are for, or how to use them.
This article discusses the optimization options of MDK-ARM and related extended knowledge (micro-libraries, practical applications, debugging), hoping to help you in your future development projects.
When we refer to optimization, it mainly involves two aspects:
In MDK-ARM, the configuration options related to optimization are:


Of course, if you choose different compilers (AC5, AC6), the optimization options will also differ (as explained below).
When the MCU capacity is limited in certain projects, in addition to modifying the code (optimizing expenditure), it is also necessary to use optimization options.
For certain projects (like specific algorithms) that require efficient (minimum time) execution, it is necessary to use optimization options.
2Explanation of Optimization Options
This section details the optimization-related options, and there are differences when selecting compilers AC5 and AC6, which will also be discussed separately for AC5 and AC6.
1. Use Cross-Module Optimization:Enable cross-module optimization
Using cross-module optimization can enable linker feedback files (for two compilations), allowing for further code optimization.
A. Do not perform cross-module optimization for library targets.
B. Cross-module optimization will increase the time required to build the project because multiple compilation and linking steps will be automatically executed.
2. Use MicroLIB: Use micro-library
It is a subset of the ISO standard C runtime library (a part of it), providing a trade-off between performance and code size.
MicroLIB is not fully compatible with ANSI, but it is sufficient for most small embedded applications.
3. Use Link-Time Code Generation: Use link-time code generation (optimization)
Before version V5, this optimization option existed, allowing for optimization during multi-file compilation and linking:
-
Function cross-module inlining
-
Removing unused variables and functions
-
Optimizing memory access by rearranging variables
-
Reusing memory whenever possible
4. Execute-only Code:Generate executable-only code
Does not include unused (functions, variables, etc.) code segments.
C, Thumb code
Processors based on Cortex-M3, M4
Compiler version 5.04 and above
5. Optimize for Time:Optimize time
At the cost of larger code size, reduce execution time. For example: using inline functions.
When the compiler is AC6, this option is [Link-Time Optimization], performing inter-module optimization during the linking state.
6. Split Load and Store Multiple:Split load and store multiple
Indicates to the compiler to split LDM and STM instructions into two or more LDM or STM instructions to reduce latency, this option can improve overall system performance.
7. The One ELF Section per Function:Each function has one ELF section
ELF code segments typically contain code for many functions, this option tells the compiler to place all functions into their own ELF sections, allowing the linker to remove unused ELF sections (rather than unused functions).
8. For AC5:Optimization options
<default>: Default value, focused on optimizing performance.
Level 0 (-O0): Turn off most optimizations, except for some simple transformations, the generated code has the best debugging view.
Level 1 (-O1): Apply limited optimizations.
For example: remove unused inline functions and static functions, remove redundant code and reorder instructions, etc. The generated code is reasonably optimized with a good debugging view.
Level 2 (-O2): Highly optimized, the mapping of target code to source code does not necessarily correspond, thus making debugging difficult.
Level 3 (-O3):Maximum level of optimization, level 3 combined with time optimization may generate more code than level 2.
9. For AC6:Optimization options
When the compiler selects AC6, the optimization options differ (with more optimization options):

The first five options in AC6 optimization (default, -O0 ~ 3) have basically the same effect as AC5, but AC6 has three additional options.
-Ofast:Enable all optimizations of -O3, as well as other optimizations that may violate language standards (strict adherence).
-Os balanced:Balance code size and code speed. By default, the compiler performs optimizations to improve performance, but this may increase the size of the image file.
-Oz image size:Optimize for code size.
This section discusses three types of compilation optimizations to achieve the best (smallest code, best performance)
A. If the code size is large (ELF code segments typically contain code for many functions), consider using the One ELF Section per Function option to reduce code size.
B. The AC6 compiler has improved optimization capabilities (the additional three options integrate optimization functions).
Code size optimization (comparison):

2. Optimize Code Performance
-
Optimization: -Ofast
-
Link-Time Optimization
Code performance optimization (comparison):

In this case, while meeting the code size, we should also strive to meet performance.
This is actually a related balance relationship, combining the above two optimization methods based on your actual situation, general optimization considerations are as follows.
Of course, in AC6, the -Os balanced optimization option is smarter.
The optimization features of MDK-ARM need to be optimized based on the actual project situation.For some projects, it can be very helpful, but optimization may also bring some impacts.
1. Using high-level optimization (-O1 and above) may affect debugging (because after optimization, the compiled output does not match the actual code).
2. Specify source file/file group optimization
Some code does not need optimization; we can optimize the specified source files/file groups.

Recommended reading:Keil Series Tutorials
My Zhihu: strongerHuang
My website:www.strongerhuang.com
If you find this article helpful, a simple like or share is also support and encouragement for me.
Follow the public account ‘strongerHuang’ to see more wonderful content in the bottom menu!

Long press to recognize the QR code in the image to follow