SDAA552 September   2026 MSPM0C1103 , MSPM0C1103-Q1 , MSPM0C1104 , MSPM0C1104-Q1

 

  1.   1
  2.   Abstract
  3.   Trademarks
  4. 1Introduction
  5. 2C Compiler Optimization
    1. 2.1 Optimization Settings
      1. 2.1.1 Optimization Level
      2. 2.1.2 Optnone
      3. 2.1.3 Link Time Optimization – LTO
    2. 2.2 Runtime Model Options
      1. 2.2.1 Data and Function Sections
      2. 2.2.2 Function Outlining
  6. 3Coding Techniques
    1. 3.1 Use Smallest Possible Types for Variables and Constants
    2. 3.2 Avoid Multiply and Divide
    3. 3.3 Use Lookup Tables Instead of Manual Calculation
    4. 3.4 Use Functions Effectively
    5. 3.5 Implement Down Counting Loops
    6. 3.6 Use the __even_in_range() Intrinsic
    7. 3.7 volatile Keyword
  7. 4Example
    1. 4.1 Optimization Level
    2. 4.2 LTO
    3. 4.3 Runtime Model Options
  8. 5Summary
  9. 6References

volatile Keyword

The volatile keyword is part of the C standard. The tiarmclang compiler supports this keyword in all language modes.

The volatile keyword indicates to the compiler that there is something about how the variable is accessed that requires that the compiler not use overly-clever optimization on expressions involving that variable. For example, the variable can also be accessed by an external program, an interrupt, another thread, or a peripheral device.

The compiler eliminates redundant memory accesses whenever possible, using data flow analysis to figure out when it is legal. However, some memory accesses can be special in some way that the compiler cannot see, and in such cases you should use the volatile keyword to prevent the compiler from optimizing away something important. The compiler does not optimize out any accesses to variables declared volatile. The number of volatile reads and writes are exactly as they appear in the C/C++ code, no more and no less and in the same order.

Any variable that might be modified by something external to the obvious control flow of the program (such as an interrupt service routine) must be declared volatile. This tells the compiler that an interrupt function might modify the value at any time, so the compiler can not perform optimizations which change the number or order of accesses of that variable. This is the primary purpose of the volatile keyword.