Hi
when I compile the following piece of code:
------------------------------------- template< typename T > inline T max( T x1, T x2 ) { return ( x1 >= x2 ) ? x1 : x2; }
int testFunction( int x1 ) { return max( x1, 20 ); } -------------------------------------
The gcc (arm-elf-gcc 3.4.2) do not copy the content of the function max() into testFunction() as expected cause of inlining.
14 _Z12testFunctioni: 21 0000 0DC0A0E1 mov ip, sp 23 0004 00D82DE9 stmfd sp!, {fp, ip, lr, pc} 27 0008 1410A0E3 mov r1, #20 30 000c 04B04CE2 sub fp, ip, #4
34 0010 FEFFFFEB bl _Z3maxIiET_S0_S0_
37 0014 00689DE8 ldmfd sp, {fp, sp, lr} 38 0018 1EFF2FE1 bx lr
45 _Z3maxIiET_S0_S0_: 53 0000 010050E1 cmp r0, r1 54 0004 0100A0B1 movlt r0, r1 58 0008 1EFF2FE1 bx lr
The assembler code above shows that _Z3maxIiET_S0_S0_ is called instead of putting the two lines (cmp & movlt) directly into Z12testFunctioni.
When I remove the template Parameter and change max() to
inline int max( int x1, int x2 ) { return ( x1 >= x2 ) ? x1 : x2; }
the inlining works perfectly.
14 _Z12testFunctioni: 23 0000 140050E3 cmp r0, #20 24 0004 1400A0B3 movlt r0, #20 28 0008 1EFF2FE1 bx lr
12 Bytes of code instead of 40 Bytes code :-/
I looked into the manual and found the option -fno-implicit-inline-templates but both variants (-fno-implicit-inline-templates and -fimplicit-inline-templates) produces the same code.
I compiled with -O2
how can I avoid this non-optimal "feature" of gcc ;-)
regards Martin Kaul