please consider following testcase: #include <vector> void f( int ); typedef std::vector< int > V; void g( V const& v, V::size_type n ) { for ( V::size_type i = 0; i < n; i++ ) f( v[ i ] ); } void h( V const& v, V::size_type n ) { for ( V::const_iterator i = v.begin(), e = i + n; i != e; ++i ) f( *i ); }
g() and h() traverse vector in the same way and imho should produces the same assembly code but compiler producess unoptimal code for g(). _Z1gRKSt6vectorIiSaIiEEm: pushq %r12 testq %rsi, %rsi movq %rdi, %r12 pushq %rbp movq %rsi, %rbp pushq %rbx je .L12 xorl %ebx, %ebx .L11: movq (%r12), %rax <=== h() is smarter in this point. movl (%rax,%rbx,4), %edi <=== addq $1, %rbx call _Z1fi cmpq %rbp, %rbx jne .L11 popq %rbx popq %rbp popq %r12 ret _Z1hRKSt6vectorIiSaIiEEm: pushq %rbp pushq %rbx subq $8, %rsp movq (%rdi), %rbx leaq (%rbx,%rsi,4), %rbp cmpq %rbp, %rbx je .L4 .L5: movl (%rbx), %edi addq $4, %rbx call _Z1fi cmpq %rbx, %rbp jne .L5 addq $8, %rsp popq %rbx popq %rbp ret -- Summary: missed optimization of std::vector access. Product: gcc Version: 4.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: pluto at agmk dot net GCC target triplet: x86_64-gnu-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35269