C/C++ compilers optimize single layer functions with constant parameters (known at compile time) only when using -Os, -O1 and -O2. They do not optimize all the layers. Only -O3 can do that. gcc is WinAVR 4.3.3 which does not support attribute "optimize".
void inner(double value)
{
//operations using value
//...
}
void outer(double value)
{
//few operations using value
//...
inner(value);
}
int main()
{
inner(1); //optimize
outer(1); //only optimize by using -O3
}
What are the possible solutions other than the followings?
- -O3 hold program or file (misuse will blow up the size)
- attribute optimize -O3 for the functions (4.3.3 does not support)
- macro (error prone)
Update:
//inner function
static inline void _delay_us(double __us) __attribute__((always_inline));
//outer function
void f(double);
inline f1(double);
static inline f2(double);
static f3(double);
f1 is optimized but give warning '_delay_us' is static but used in inline function 'f1' which is not static due to static function problem. Others are not optimized.
Solution:
static inline void outer(double) __attribute__((always_inline));
Inline is the key. My outer function is too large for inline. The attribute always_inline forces the function to be inline. This allows the compiler to optimize the function with less compilation cost than trying to figure out the optimization. -O3 is smart enough to do the optimization but not -Os. -Os may need some compiler options. (The keyword static is required because the inner function is also static inline.)