Bug ID: 84824
           Summary: DCE fails to remove dead code of std::function
           Product: gcc
           Version: 7.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot
          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:
        .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
        movl    $1, (%rsp)
        movq    %rax, 24(%rsp)
        movq    %rax, 16(%rsp)
        xorl    %eax, %eax
        call    __printf_chk@PLT
        movq    16(%rsp), %rax
        testq   %rax, %rax
        je      .L10
        movq    %rsp, %rdi
        movl    $3, %edx
        movq    %rdi, %rsi
        call    *%rax
        xorl    %eax, %eax
        movq    40(%rsp), %rcx
        xorq    %fs:40, %rcx
        jne     .L23
        addq    $48, %rsp
        .cfi_def_cfa_offset 16
        popq    %rbx
        .cfi_def_cfa_offset 8

But clang manage to remove these code
main:                                   # @main
# %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

compiler flags are: -O2 -S

It seems gcc insist on construct std::function<int(int,int)> object on stack.

Reply via email to