RE: VC.NET 'Optimization'

Kent Quirk <[email protected]> Mon, 11 Apr 2005 20:59:06 -0400
Newsgroups gmane.games.devel.windows
Message-ID <[email protected]>
I think this is pretty common, actually. Look:

----------------------------------------------------
     timer.Restart();
     int fib = fibonacci( 42 );
     float msTime = timer.GetElapsed();
     printf( "time=%f, result=%d\n", msTime, fib );
----------------------------------------------------

fib is declared locally, and no one ever takes its address. The compiler is 
pretty safe in assuming that no sane code could possibly affect the value 
of fib. Similarly, the fibonacci function only takes a constant argument. 
If it's in the same source file, it might even know that the function has 
no aliasing issues. Consequently, it's reasonable to optimize away the 
creation of the fib variable and simply embed the function call in the 
printf call.

My guess is that what the compiler did was optimize this to:

----------------------------------------------------
     timer.Restart();
     printf( "time=%f, result=%d\n", timer.GetElapsed(), fibonacci(42) );
----------------------------------------------------

MSVC has this:


No aliasing
/Oa:  Assume no aliasing occurs within functions. Alternate: #pragma 
optimize("a")

Intra-func aliasing
/Ow: Assume aliasing occurs across function calls. Alternate: #pragma 
optimize("w")

         Kent


At 06:24 PM 4/11/2005, you wrote:


> > BTW: I'm not entirely sure whether that's a compiler bug or a valid
> > optimisation - it depends what your Restart() function does.
>
>The compiler might be doing type-based aliasing to re-order the
>functions, if one uses float, and the other int, say.
>
>However, I still don't see how the compiler can do this. Timers
>call into functions that are dllimport, and those functions can
>have arbitrary side effects -- especially in a version of the
>DLL that's not released yet. Thus, no amount of alias analysis
>can allow the compiler to re-order the operations like that, so
>I'd say it's a real bug.
>
>Now, if it's really a bug, how come we're not seeing this all
>over the place? Seems like it would cause pretty much nothing at
>all to actually work...
>
>Cheers,
>
>                         / h+

----
Kent Quirk
CTO, CogniToy