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

            Bug ID: 111573
           Summary: lambda functions often not inlined and optimized out
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hubicka at gcc dot gnu.org
  Target Milestone: ---

#include <functional>
using namespace std;
static int dosum(std::function<int(int,int)> fn)
{
   return fn(5,6);
}
int test()
{
  auto sum = [](int a, int b) {
    return a + b;
  };
  int s;
  for (s = 0; s < 10; s++)
    s+= dosum(sum);
  return s;
}
Gets optimized well only with early inlining. compiled with -fno-early-inlining
yields to:

_Z4testv:
.LFB2166:
        .cfi_startproc
        subq    $56, %rsp
        .cfi_def_cfa_offset 64
        xorl    %ecx, %ecx
        .p2align 4,,10
        .p2align 3
.L8:
        leaq    12(%rsp), %rdx
        leaq    8(%rsp), %rsi
        movl    $5, 8(%rsp)
        leaq    16(%rsp), %rdi
        movl    $6, 12(%rsp)
        call   
_ZNSt17_Function_handlerIFiiiEZ4testvEUliiE_E9_M_invokeERKSt9_Any_dataOiS6_
        leal    1(%rcx,%rax), %ecx
        cmpl    $9, %ecx
        jle     .L8
        movl    %ecx, %eax
        addq    $56, %rsp
        .cfi_def_cfa_offset 8
        ret

So we fail to inline since ipa-prop fails to track the constant function
address.  I think this is really common in typical lambda function usage

Reply via email to