https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109205

            Bug ID: 109205
           Summary: vector.resize( v.size() + 100 ) does unnecessary
                    comparison
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joerg.rich...@pdv-fs.de
  Target Milestone: ---

This function:

#include <vector>

void testResize( std::vector<char> & v )
{
  v.resize( v.size() + 100 );
}


Will compile (-O2) to something like this:

        mov     rcx, QWORD PTR [rdi+8]
        mov     rax, QWORD PTR [rdi]
        mov     rdx, rcx
        sub     rdx, rax
        lea     rsi, [rdx+100]
        cmp     rdx, rsi
        jb      .L39
        add     rax, rsi
        cmp     rcx, rax
        je      .L36
        mov     QWORD PTR [rdi+8], rax
.L36:
        ret
.L39:
        mov     esi, 100
        jmp     std::vector<char, std::allocator<char>
>::_M_default_append(unsigned long)

The call to _M_default_append is guarded by a cmp.  This seems unnecessary, as
the argument passed to resize is always bigger than size.  

Doing the addition with a signed type does not change the result.

See: https://godbolt.org/z/xY77fsz4z

Reply via email to