[Bug tree-optimization/78528] New: Recursion not optimized in simple case

mawww at kakoune dot org gcc-bugzilla@gcc.gnu.org
Fri Nov 25 16:11:00 GMT 2016


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

            Bug ID: 78528
           Summary: Recursion not optimized in simple case
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mawww at kakoune dot org
  Target Milestone: ---

The following code:

struct Int 
{ 
    constexpr Int(int value) : m_value(value) {} 

    constexpr friend Int operator+(Int lhs, Int rhs) { return {lhs.m_value +
rhs.m_value}; } 

    int m_value; 
}; 

Int strlen(const char* s) 
{ 
    return *s == 0 ? 0 : strlen(s+1) + 1; 
} 


when compiled with `-std=c++11 -O3` generates the following assembly for the
strlen function:

_Z6strlenPKc: 
.LFB4: 
    .cfi_startproc 
    cmpb    $0, (%rdi) 
    jne .L2 
    xorl    %eax, %eax 
    ret 
    .p2align 4,,10 
    .p2align 3 
.L2: 
    cmpb    $0, 1(%rdi) 
    movl    $1, %eax 
    jne .L12 
.L10: 
    ret 
    .p2align 4,,10 
    .p2align 3 
.L12: 
    cmpb    $0, 2(%rdi) 
    movl    $2, %eax 
    je  .L10 
    subq    $8, %rsp 
    .cfi_def_cfa_offset 16 
    addq    $3, %rdi 
    call    _Z6strlenPKc 
    addq    $8, %rsp 
    .cfi_def_cfa_offset 8 
    addl    $3, %eax 
    ret 
    .cfi_endproc 

As we can see, the generated code is still recursive, I think the optimizer
should have optimized that, is it correctly does when we use 'int' instead of
'Int'.


More information about the Gcc-bugs mailing list