[Bug c++/51571] No named return value optimization while adding a dummy scope

guillaume.melquiond at inria dot fr gcc-bugzilla@gcc.gnu.org
Sun Jun 2 07:36:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51571

Guillaume Melquiond <guillaume.melquiond at inria dot fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |guillaume.melquiond at inria dot f
                   |                            |r

--- Comment #3 from Guillaume Melquiond <guillaume.melquiond at inria dot fr> ---
I have recently encountered a similar problem, but in a much more general case.

struct A {
    A(int);
    A(A const &);
    ~A();
};

A f(bool b)
{
    if (b) return A(0);
    A a(1);
    return a;
}

All the return statements dominated by variable "a" return "a", so its
construction should happen in-place, hence eliding copy-construction and
destruction. Unfortunately, this is not what happens with g++ 4.8.0.

Interestingly enough, if one uninlines the code by hand, g++ actually generates
the optimal code, so it is possible though cumbersome to work around the missed
optimization:

inline A f2()
{
    A a(1);
    return a;
}

A f1(bool b)
{
    if (b) return A(0);
        return f2();
}

produces

A f1(bool) (bool b)
{
  <bb 2>:
  if (b_2(D) != 0)
    goto <bb 3>;
  else
    goto <bb 4>;

  <bb 3>:
  A::A (_4(D), 0);
  goto <bb 5>;

  <bb 4>:
  A::A (_4(D), 1);

  <bb 5>:
  return _4(D);
}



More information about the Gcc-bugs mailing list