Special Considerations When Using the Optimizer
3-10
3.4 Special Considerations When Using the Optimizer
The optimizer is designed to improve your ANSI-conforming C programs while
maintaining their correctness. However, when you write code for the optimizer,
you should note the following special considerations to ensure that your pro-
gram performs as you intend.
3.4.1 Use Caution With asm Statements in Optimized Code
You must be extremely careful when using asm (inline assembly) statements
in optimized code. The optimizer rearranges code segments, uses registers
freely, and can completely remove variables or expressions. Although the
compiler never optimizes out an asm statement (except when it is
unreachable), the surrounding environment where the assembly code is
inserted can differ significantly from the original C source code. It is usually
safe to use asm statements to manipulate hardware controls such as interrupt
masks, but asm statements that attempt to interface with the C environment
or access C variables can have unexpected results. After compilation, check
the assembly output to make sure your asm statements are correct and main-
tain the integrity of the program.
3.4.2 Use Caution With the Volatile Keyword
The optimizer analyzes data flow to avoid memory accesses whenever possi-
ble. If you have code that depends on memory accesses exactly as written in
the C code, you must
use the volatile keyword to identify these accesses. A
variable qualified with a volatile keyword is allocated to an uninitialized section.
The compiler will not optimize out any references to volatile variables.
In the following example, the loop waits for a location to be read as 0xFF:
unsigned int *ctrl;
while (*ctrl !=0xFF);
In this example,
*ctrl
is a loop-invariant expression, so the loop is optimized
down to a single memory read. To correct this, declare
ctrl
as:
volatile unsigned int *ctrl
Summary of Contents for TMS320C2x
Page 8: ...viii...
Page 69: ...2 47 C Compiler Description...
Page 159: ...6 36...
Page 226: ...8 6...