Thread: generated assembly

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    generated assembly

    I'm playing around with link time optimization in Visual C++, whole program optimization (WPO) its called there. Specifically checking if MSVC inlines a one-liner function in another module.

    so I have this function

    Code:
    int Bla::GetBla() { return m_x; }
    and this main in another module, pretty simple

    Code:
    int main(int argc, char **argv)
    {
    	Bla bla(std::rand());
    
    	int x = bla.GetBla();
    
    	cout << x << endl;
    
    	return 0;
    }
    When I turn on optimizations but no WPO I get the following assembler for the call to GetBla.

    ; 35 : int x = bla.GetBla();
    lea ecx, DWORD PTR _bla$[esp+8]
    call ?GetBla@Bla@@QAEHXZ; Bla::GetBla
    push eax

    asm for GetBla itself:
    ?GetBla@Bla@@QAEHXZ PROC; Bla::GetBla, COMDAT;
    ; 10 : return m_x;
    mov eax, DWORD PTR [ecx]


    With WPO turned on I get this:

    ; 35 : int x = bla.GetBla();
    call ?GetBla@Bla@@QAEHXZ ; Bla::GetBla
    push eax

    asm for GetBla itself:

    ?GetBla@Bla@@QAEHXZ PROC; Bla::GetBla, COMDAT;

    ; 10 : return m_x;
    mov eax, DWORD PTR [eax]


    I don't understand the lea instruction before the call to GetBla in the version with WPO turned OFF. What's the use of that. And why doesn't it get emitted in the version with WPO turned ON?

    Does anyone know?

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I don't have a clue and I don't plan to encounter this kind of ugly syntax ever. Someone here can help. Dropping this link couldn't hurt. Lots of Masm and Visual C++ going on. Masm is Microsoft ... Masm32 brought it out of the stone-age. Seach keyword C++. The MASM Forum - Index

  3. #3
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    With WPO turned off your main module has no knowledge of the Bla module and so the optimizer can't do certain things like find the best use of registers across module boundaries.

    Since you're invoking a non-static class method, the calling convention is thiscall and Microsoft implements thiscall by passing the "this" pointer in register ecx. This is what the lea instruction is doing in your code; It's fulfilling the caller's obligation of a thiscall.

    Without WPO your main module can't make any assumptions about what the method does and so it plays nice.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since bla is an object, and objects need a this pointer, the compiler takes the address of the object (since it's on the stack) and passes it to the function via the ecx register.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two basic questions about generated assembly
    By George2 in forum Windows Programming
    Replies: 2
    Last Post: 07-16-2008, 03:19 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM