Whole Program Optimization pratical example...
I wrote those couple lines of code today as a short example of what Whole Program Optimization gives you with VC++ 7.x (although I used 8.0 Beta 1):
Main.cpp:
#include<iostream>
externvoid FunctionInOtherSource( int * ) ;
void wmain()
{
for( int i = 0 ; i < 10 ; )
{
FunctionInOtherSource( & i ) ;
std::wcout << i << std::endl ;
}
}
FunctionInOtherSource.cpp:
void FunctionInOtherSource( int * pi )
{
(*pi)++ ;
}
Without Whole Program Optimization turned on (no /GL), the FunctionInOtherSource code gets called:
00404612 mov dword ptr [esp+4],0
0040461A push edi
0040461B jmp wmain+10h (404620h)
0040461D lea ecx,[ecx]
{
FunctionInOtherSource( & i ) ;
00404620 lea eax,[esp+8]
00404624 push eax
00404625 call FunctionInOtherSource (401000h)
std::wcout << i << std::endl ;
0040462A mov ecx,dword ptr [esp+0Ch]
With Whole Program Optimization turned on (/GL), the FunctionInOtherSource code gets inlined :
for( int i = 0 ; i < 10 ; )
0041599A xor ebp,ebp
{
FunctionInOtherSource( & i ) ;
0041599C add ebp,1
std::wcout << i << std::endl ;
0041599F push ebp
004159A0 call std::basic_ostream<unsigned short,std::char_traits<unsigned short> >::operator<< (401900h)