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

Giuseppe D'Angelo <dangelog at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dangelog at gmail dot com

--- Comment #2 from Giuseppe D'Angelo <dangelog at gmail dot com> ---
Still valid as 10.2.

There's just been some discussion related to this missed optimization on the
std-proposals mailing list. It's also worth noticing that neither GCC nor Clang
remove the branch from some slightly different code:

int& test(std::variant<int, bool> &v)
{
    return *std::get_if<int>(&v);
}

GCC 10.2 generates:

test(std::variant<int, double>&):
  xorl %eax, %eax
  cmpb $0, 8(%rdi)
  cmove %rdi, %rax
  ret

Clang 11 generates:

test(std::__1::variant<int, double>&): # @test(std::__1::variant<int, double>&)
  xorl %eax, %eax
  cmpl $0, 8(%rdi)
  cmoveq %rdi, %rax
  retq


If one pushes things a little bit, both compilers remove the branch:

int& test(std::variant<int, double>& v)
{
    auto result = std::get_if<int>(&v);
    if (!result) __builtin_unreachable();
    return *result;
}

GCC:
test(std::variant<int, double>&):
  movq %rdi, %rax
  ret

Clang:
test(std::__1::variant<int, double>&): # @test(std::__1::variant<int, double>&)
  movq %rdi, %rax
  retq


Could you please elaborate on what you mean by "might end up as a security
issue"?

Reply via email to