https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84824
Bug ID: 84824 Summary: DCE fails to remove dead code of std::function constructor Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: manjian2006 at gmail dot com Target Milestone: --- #include <functional> #include <stdio.h> std::function<int(int, int)> getFunc(int i) { auto f = [=] (int a, int b) { return a + b + i; }; return f; } int main() { printf("%d", getFunc(1)(1, 1)); return 0; } In this example gcc generate code with redundant store operations and exception handling block like: main: .LFB1411: .cfi_startproc .cfi_personality 0x9b,DW.ref.__gxx_personality_v0 .cfi_lsda 0x1b,.LLSDA1411 pushq %rbx .cfi_def_cfa_offset 16 .cfi_offset 3, -16 leaq .LC0(%rip), %rsi movl $3, %edx movl $1, %edi subq $48, %rsp .cfi_def_cfa_offset 64 movq %fs:40, %rax movq %rax, 40(%rsp) xorl %eax, %eax leaq _ZNSt17_Function_handlerIFiiiEZ7getFunciEUliiE_E9_M_invokeERKSt9_Any_dataOiS6_(%rip), %rax movl $1, (%rsp) movq %rax, 24(%rsp) leaq _ZNSt14_Function_base13_Base_managerIZ7getFunciEUliiE_E10_M_managerERSt9_Any_dataRKS3_St18_Manager_operation(%rip), %rax movq %rax, 16(%rsp) xorl %eax, %eax .LEHB0: call __printf_chk@PLT .LEHE0: movq 16(%rsp), %rax testq %rax, %rax je .L10 movq %rsp, %rdi movl $3, %edx movq %rdi, %rsi call *%rax .L10: xorl %eax, %eax movq 40(%rsp), %rcx xorq %fs:40, %rcx jne .L23 addq $48, %rsp .cfi_remember_state .cfi_def_cfa_offset 16 popq %rbx .cfi_def_cfa_offset 8 ret But clang manage to remove these code main: # @main .cfi_startproc # %bb.0: pushq %rax .cfi_def_cfa_offset 16 movl $.L.str, %edi movl $3, %esi xorl %eax, %eax callq printf xorl %eax, %eax popq %rcx retq compiler flags are: -O2 -S It seems gcc insist on construct std::function<int(int,int)> object on stack.