gcc 4.8.1, -O3 -march=native -std=c++11
small example program to check what does the gcc 4.8.1 optimizer do with
const std::vector/std::arrays + simple operations
---
#include <vector>
#include <numeric>
#include <array>
#define USE_ARRAY
#if defined(USE_ARRAY)
static int calc(const std::array<int,3> p_ints, const int& p_init)
#else
static int calc(const std::vector<int> p_ints, const int& p_init)
#endif
{
return std::accumulate(p_ints.begin(), p_ints.end(), p_init);
}
int main()
{
const int result = calc({10,20,30},100);
return result;
}
---
gcc produces this code if USE_ARRAY is defined
main:
mov eax, 160
ret
if USE_ARRAY is undefined (and vector is in use) it produces
main:
push rbx
mov edi, 12
call operator new(unsigned long)
mov rdx, QWORD PTR ._81[rip]
mov rdi, rax
mov QWORD PTR [rax], rdx
mov eax, DWORD PTR ._81[rip+8]
mov rsi, rdx
shr rsi, 32
lea ebx, [rsi+100+rdx]
add ebx, eax
test rdi, rdi
mov DWORD PTR [rdi+8], eax
je .L2
call operator delete(void*)
.L2:
mov eax, ebx
pop rbx
ret
._81:
.long 10
.long 20
.long 30
so my questions is - can gcc replace/subtitute the const std::vector by
const std::array in such const situations, to get better
optimizer results or is the STL itself responsible for beeing
optimizeable like that - or does that brake any standard definitions?
btw: clang 3.3 produces much more code for both cases - nearly equal
using array/vector (except new/delete)
main: # @main
movabsq $85899345930, %rax # imm = 0x140000000A
movq %rax, -16(%rsp)
movl $100, %esi
movl $30, -8(%rsp)
xorl %edx, %edx
leaq -16(%rsp), %rcx
movb $1, %al
testb %al, %al
jne .LBB0_1
movd %esi, %xmm1
pxor %xmm0, %xmm0
xorl %eax, %eax
.LBB0_3: # %vector.body.i.i
movdqu (%rsp,%rax,4), %xmm2
paddd %xmm2, %xmm0
movdqu -16(%rsp,%rax,4), %xmm2
paddd %xmm2, %xmm1
addq $8, %rax
cmpq %rax, %rdx
jne .LBB0_3
jmp .LBB0_4
.LBB0_1:
pxor %xmm0, %xmm0
movd %esi, %xmm1
.LBB0_4: # %middle.block.i.i
movl $3, %esi
paddd %xmm1, %xmm0
movdqa %xmm0, %xmm1
movhlps %xmm1, %xmm1 # xmm1 = xmm1[1,1]
paddd %xmm0, %xmm1
phaddd %xmm1, %xmm1
movd %xmm1, %eax
cmpq %rdx, %rsi
je .LBB0_7
addq $-12, %rcx
leaq -16(%rsp), %rdx
.LBB0_6: # %scalar.ph.i.i
addl 12(%rcx), %eax
addq $4, %rcx
cmpq %rcx, %rdx
jne .LBB0_6
.LBB0_7: # %_ZL4calcSt5arrayIiLm3EERKi.exit
ret