Re: VC.NET 'Optimization'

"yogiwp" <[email protected]> Wed, 13 Apr 2005 01:51:54 +0700
Newsgroups gmane.games.devel.windows
Message-ID <[email protected]>
----- Original Message ----- 
From: Kent Quirk 

> 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) );
> ----------------------------------------------------

Yes, this is exactly what happened (disassembly below).
Sigh. I must say that this makes my worry.

Btw, #pragma optimize("a",off) and/or ("w",off) have no effect; it's
still generating the exact same code. Declaring msTime as 'volatile'
does fix it.

-------------------------------------------------------------------------------
; 5215 :    timer.Restart();

    lea ecx, DWORD PTR _timer$[esp+148]
    call    ?Restart@CRealTimer@Torture@@QAEXXZ ; Torture::CRealTimer::Restart

; 5216 :    int fib = fibonacci( 42 );
; 5217 :    float msTime = timer.GetElapsed();

    lea ecx, DWORD PTR _timer$[esp+148]
    call    ?GetElapsed@CRealTimer@Torture@@QAEMXZ  ; Torture::CRealTimer::GetElapsed
    push    41                  ; 00000029H
    call    ?fibonacci@@YAHH@Z          ; fibonacci
    push    40                  ; 00000028H
    mov edx, eax
    call    ?fibonacci@@YAHH@Z          ; fibonacci
    add esp, 8
    add edx, eax

; 5218 :    printf( "msTime=%f, result=%d\n", msTime, fib );

    push    edx
    sub esp, 8
    fstp    QWORD PTR [esp]
    push    OFFSET FLAT:??_C@_0BG@BEAEGCGA@msTime?$DN?$CFf?0?5result?$DN?$CFd?6?$AA@
    call    _printf
    add esp, 16                 ; 00000010H
-------------------------------------------------------------------------------
(There are 2 fibonacci() calls because of inlining -- fibonacci() is a
recursive function calling itself twice.)


Yogi Wahyu Prasidha



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Gamedevlists-windows mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gamedevlists-windows
Archives:
http://sourceforge.net/mailarchive/forum.php?forum_id=555