[ << ] [ >> ] [Top] [Contents] [Index] [ ? ]

5. Optimization Hints

This chapter gives advices and hints to optimize your application.


5.1 Using inline

The GNU compiler can inline functions and methods. When a function is inlined, its code is inserted directly at the place where it is used. This optimization saves a function call and return. In some cases, the compiler can even optimize further the inlined function just because it knows what are the parameters of the function. This optimization is done automatically when the compiler is invoked with `-O3' option. However the use of `-O3' may result in larger code as the compiler can inline small and larger functions.

In many cases a better result is obtained by specifying which function can be inline. The inline optimization can still be performed at `-O1' or `-Os'. You should follow the following rules:

Below is an example of a function that can be inlined by the compiler.

 
static int inline mean(int a, int b)
{
   return (a + b) / 2;
}
int main()
{
   extern int a, b;

   int result = mean(10, 20);
   printf("Result = %d\n", result);
   result = mean(a, b);
   printf("Result = %d\n", result);
   return 0;
}

The first call to `mean' will generate no code: the compiler knows the arguments and since they are constant it will compute itself the result (15). The second call depends on global variables whose values are not known at compile time. However, the compiler will directly use those variables and does not need to push them on the stack, call the function and so on.


5.2 Frame pointer

The frame pointer is used to access local variables as well as function arguments. For 68HC11 and 68HC12, the frame pointer is the address of the beginning of local variables on the stack. When a function is called, a new frame pointer is computed at beginning of the function. The old frame pointer is saved on the stack.

The frame pointer can be eliminated by the compiler with the `-fomit-frame-pointer' option. When it is eliminated it is replaced by the stack pointer plus an offset that depends on the current stack depth.

Elimination of the frame pointer provides better performance, reduces the generated code but it makes debugging with Gdb much harder. Without the frame pointer, Gdb is unable to compute the caller frame and print the stack backtrace.

The frame pointer is not eliminable in some conditions. In general it can't be eliminated when the function uses the builtin alloca or when it allocates a local variable whose size is not known at compile time.


[ << ] [ >> ] [Top] [Contents] [Index] [ ? ]

This document was generated by Stephane Carrez on May, 15 2005 using texi2html>