VS.2003, __inline and __forceinline

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I'm having strange issues with inline functions in VS.2003.
In following simple code snippet:

extern
int mytmp1( int v ) { return !v; };

extern int mytmp( void )
{
return mytmp1( -7 );
}

I see mytmp1 expanded inline, despite option /Ob1 (expand only __inline) iin
the project.
Is this normal? Does this depend on C or C++ mode or debug/release mode?

Another question about __inline extensions in pure C mode (MS specific, not
c++):
Are the following statements valid? All they compile clean, but I doubt
whether this can cause "undefined behavour" or is 100% legal:

A. __inline int foo1(...) { ... }

B. int __inline foo2( ... ) { ... }

C. __inline int foo3( ...); // proto
int foo3(...) {... } // separate body

D. static __inline int foo3( ...); // proto
extern int foo3(...) {... } // __inline with static or extern ??

Could somebody explain please?

Best regards,
Pavel
--
 
I'm having strange issues with inline functions in VS.2003.
In following simple code snippet:
...
I see mytmp1 expanded inline, despite option /Ob1 (expand only __inline) iin
the project.

Pavel,

Do you have a small console application that illustrates the issue?

Dave
 
David Lowndes said:
Pavel,

Do you have a small console application that illustrates the issue?

Dave

Thanks David.
I've tried to get a "minimal" example, and found that the compiler
does very aggressive optimization.
It inlines and removes unused code everywhere.
By itself it is not bad. But why it ignores project options?

This is one variant, where everything gets inlined though
in project settings global optimization is off and inline only explicit.
--------
int /*__inline*/ aaa1( int v )
{ return !v; };

extern int tmp99( void )
{
printf("test1\n");
return aaa1( - 7 ) ;
}


int main(int argc, char* argv[])
{
tmp99();
return 0;
}
-------------

My specific question is, why VC syntax in C mode allows
extern __inline and even extern __forceinline ?
Doesn't __inline mean that the function is not visible outside
and can not be extern?

OTOH, if compiler decides to inline a function defined as extern, how
it knows that no another module calls it from outside?

I'm not sure how to post my sample. There is also .vcproj
and other files besides of .c


Regards,
Pavel
 
Pavel A. said:
David Lowndes said:
Pavel,

Do you have a small console application that illustrates the issue?

Dave

Thanks David.
I've tried to get a "minimal" example, and found that the compiler
does very aggressive optimization.
It inlines and removes unused code everywhere.
By itself it is not bad. But why it ignores project options?

This is one variant, where everything gets inlined though
in project settings global optimization is off and inline only
explicit.
--------
int /*__inline*/ aaa1( int v )
{ return !v; };

extern int tmp99( void )
{
printf("test1\n");
return aaa1( - 7 ) ;
}


int main(int argc, char* argv[])
{
tmp99();
return 0;
}

Here is the result I get with your example. Inline /Ob1, global
optimization on

--- c:\documents and settings\...\quick_test.cpp
#include "stdio.h"


int /*__inline*/ aaa1( int v )
{ return !v; };
00401000 xor eax,eax
00401002 cmp dword ptr [esp+4],eax
00401006 sete al
00401009 ret

--- c:\documents and settings\...\quick_test.cpp

extern int tmp99( void )
{
printf("test1\n");
00401010 push offset string "test1\n" (406D4Ch)
00401015 call printf (401038h)
return aaa1( - 7 ) ;
0040101A push 0FFFFFFF9h
0040101C call aaa1 (401000h)
00401021 add esp,8
}
00401024 ret

--- c:\documents and settings\...\quick_test.cpp


int main(int , char* [])
{
tmp99();
00401030 call tmp99 (401010h)
return 0;
00401035 xor eax,eax
}
00401037 ret


Seems to work very well as intended.
-------------

My specific question is, why VC syntax in C mode allows
extern __inline and even extern __forceinline ?
Doesn't __inline mean that the function is not visible outside
and can not be extern?

But 'extern' means that it is. :-)

OTOH, if compiler decides to inline a function defined as extern, how
it knows that no another module calls it from outside?

The linker knows. If you delay code generation until link time, the
system knows almost everything about the code.


Bo Persson
 
This is one variant, where everything gets inlined though
in project settings global optimization is off and inline only explicit.

In your release build project settings, change the optimization
setting to custom rather than the default /O2 or /O1 options. I think
that other optimizations are having an effect.

Dave
 
Back
Top