There are other several minor techniques to be aware of when writing the most efficient C code for the compiler.
Initialize local arrays and structures at declaration time, if possible. Local arrays and structures are initialized optimally by the compiler.
Functions with a large number of parameters will probably have to pass some parameters on the stack causing costly memory accesses. Make sure that frequently called functions pass their parameters in registers. For information on the parameter passing rules for the 56800E C Compiler see the Freescale 56800/E Hybrid Controllers: MC56F83xx/DSP5685x Family Targeting Manual.
Forcing enums as integers (C/C++ Language Panel, "Enums Always Ints") may yield better code since integers are usually handled more efficiently.
Loading frequently used global variables into local temporary variables sometimes has a positive effect on code size and performance, since accessing variables through registers is more efficient that absolute addressing modes.
As an illustration of the final point in the list above, the code in the following listing executes in 98 cycles and 20 program memory words. The same function is performed by the code in Listing: Example 13: Modified Global Structure Example, but it executes in 57 cycles and 13 program memory words. A temporary local variable is used in processing instead of the global variable. Fewer absolute addressing instructions account for the difference.
#define ARRAY_SIZE 5 static struct s1 { unsigned char value_a; unsigned char value_b; unsigned char value_c; } s_s1[ARRAY_SIZE]; unsigned int r1; int main() { int i; for (i = 0; i < ARRAY_SIZE; i++) { r1 += s_s1[i].value_a; r1 += s_s1[i].value_b; r1 += s_s1[i].value_c; } return (r1); }
int main() { int i; unsigned int local_var; local_var = r1; for (i = 0; i < ARRAY_SIZE; i++) { local_var += s_s1[i].value_a; local_var += s_s1[i].value_b; local_var += s_s1[i].value_c; } r1 = local_var; return (r1); }