6

I was trying to come up with an answer for a question I had a while ago (Automatically determine if user-defined function is equivalent to the implicit one). My thought was that I would compile in and out the copy constructor, and then disassemble the code, and check that they were identical. The code:

struct A
{
    int B;
    A() : B(0) { }
#ifdef COPY_CONSTRUCTOR
    A(const A& a) : B(a.B) { }
#endif
};

#include <stdio.h>
int main()
{
    A a;
    A b(a);
    printf("%d", b.B);
}

Compiled with (cygwin gcc v4.9.3):

gcc -o a1.exe main.cpp -std=c++11 -g -O0 -DCOPY_CONSTRUCTOR
gcc -o a2.exe main.cpp -std=c++11 -g -O0 -fno-elide-constructors

And then dumping the disassembly the the copy constructors (_ZN1AC1ERKS_ is the mangled name of A's copy constructor):

gdb -batch -ex 'file a1.exe' -ex 'disass _ZN1AC1ERKS_'

Produces:

Dump of assembler code for function A::A(A const&):
   0x0000000100401770 <+0>:     push   %rbp
   0x0000000100401771 <+1>:     mov    %rsp,%rbp
   0x0000000100401774 <+4>:     mov    %rcx,0x10(%rbp)
   0x0000000100401778 <+8>:     mov    %rdx,0x18(%rbp)
   0x000000010040177c <+12>:    mov    0x18(%rbp),%rax
   0x0000000100401780 <+16>:    mov    (%rax),%edx
   0x0000000100401782 <+18>:    mov    0x10(%rbp),%rax
   0x0000000100401786 <+22>:    mov    %edx,(%rax)
   0x0000000100401788 <+24>:    pop    %rbp
   0x0000000100401789 <+25>:    retq
End of assembler dump.

And:

gdb -batch -ex 'file a2.exe' -ex 'disass _ZN1AC1ERKS_'

Produces:

Dump of assembler code for function A::A(A const&):
   0x0000000100401770 <+0>:     push   %rbp
   0x0000000100401771 <+1>:     mov    %rsp,%rbp
   0x0000000100401774 <+4>:     mov    %rcx,0x10(%rbp)
   0x0000000100401778 <+8>:     mov    %rdx,0x18(%rbp)
   0x000000010040177c <+12>:    mov    0x10(%rbp),%rax
   0x0000000100401780 <+16>:    mov    0x18(%rbp),%rdx
   0x0000000100401784 <+20>:    mov    (%rdx),%edx
   0x0000000100401786 <+22>:    mov    %edx,(%rax)
   0x0000000100401788 <+24>:    pop    %rbp
   0x0000000100401789 <+25>:    retq
End of assembler dump.

They are the same, except that the line mov 0x10(%rbp),%rax occurs in a different order. In this case, it's innocuous, but obviously makes equivalence detection harder. What is the explanation for the difference?

4
  • 3
    So, it "makes equivalence detection harder"? why, who told you that equivalence detection at the assembly level was going to be easy?
    – Mike Nakis
    Mar 6, 2017 at 18:07
  • 1
    A compiler is not required to generate identical code for a default constructor as if it was spelled out, using explicit C++ code. More than likely, gcc runs dedicated code that generates the default copy-constructor code, while the code for a constructor, copy or otherwise, gets handled by general purpose code. Mar 6, 2017 at 18:11
  • 3
    Generated machine code will probably be identical if you bump up the optimization level. Mar 6, 2017 at 18:17
  • @NikolaiFetissov: yes - if I compile with -O3 (and add fno-inlines), then they become equivalent (and much shorter). Seems like the answer here is an combination of these three comments. Mar 6, 2017 at 18:22

1 Answer 1

0

Your question got me curious so I had to see for myself. You can check here for version 1 and here for version 2 to see that the results are actually identical.

Both variants are optimized away!

This is the disassembly:

.LC0:
        .string "%d"
Test():
        push    0
        push    OFFSET FLAT:.LC0
        call    printf
        pop     eax
        pop     edx
        push    0
        push    OFFSET FLAT:.LC0
        call    printf
        pop     ecx
        pop     eax
        ret

To understand why the non-optimized versions differ, you would probably have to check the sources of gcc.

Visual Studio also gives identical results:

int main()
{
    A a;
    A b(a);
    printf("%d", b.B);
00B41000  push        0  
00B41002  push        0B499A0h  
00B41007  call        printf (0B4102Dh)  
    printf("%d", a.B);
00B4100C  push        0  
00B4100E  push        0B499A4h  
00B41013  call        printf (0B4102Dh)  
00B41018  add         esp,10h  
    return 0;
0127101B  xor         eax,eax  
}
0127101D  ret  
2
  • Your test is showing the disassembly for the Test function, not the copy constructor of A. So, this doesn't really answer the question. Apr 20, 2017 at 0:45
  • @MuertoExcobito well, the idea is that since both get optimized away, both are identical, no?
    – Beginner
    Apr 20, 2017 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.