I was trying to figure out differences between gcc and clang and how they handle std::vector
. Can anyone with more knowledge explain why gcc and clang produce such different output?
gcc 9.2: https://godbolt.org/z/AFN46d clang 9.0: https://godbolt.org/z/kEkpWE
The program:
#include <vector>
int foo(int a) {
auto vec = std::vector<int>{};
vec.push_back(1);
vec.push_back(2);
return vec[1] * a;
}
int main () {
return foo(5) + foo(4);
}
clang produces pretty understandable assembly. However, gcc produces this weird stuff:
void std::vector<int, std::allocator<int> >::_M_realloc_insert<int>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int&&):
movabs rcx, 2305843009213693951
push r15
push r14
push r13
push r12
push rbp
push rbx
sub rsp, 24
mov r12, QWORD PTR [rdi+8]
mov r8, QWORD PTR [rdi]
mov rax, r12
sub rax, r8
sar rax, 2
cmp rax, rcx
je .L16
mov r15, rdx
mov rdx, rsi
mov rbp, rdi
mov r13, rsi
sub rdx, r8
test rax, rax
je .L11
movabs r14, 9223372036854775804
lea rsi, [rax+rax]
cmp rax, rsi
jbe .L17
Where are these magic numbers coming from?