This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Q: About Optimization to Elide Copy Constructors


> If so, then the convention can be adopted that the caller will
> always, when this condition is met, supply a region of local 
> storage in the stack  frame of the caller for the returned object 
> and pass a pointer to this region as a 'hidden' argument of the function.

I think you are misunderstanding the point of the discussion.
The copy we are concerned about is not the copy between the return value and
whatever the caller wants to do with it.  I believe we already
*do* the optimization you suggest.

The copy we want to elide, but currently do not, is a copy
between a local variable in a function, and the return location
in the *same* function:

T foo()
{
  T x;
  ... do stuff with x ...;
  return x;
}

This is (as you suggest) implemented as if it were:

void foo(T& __result)
{
  T x;
  ... do stuff with x ...;
  __result = x;
}

(I am simplifying, and am probably wrong.  I believe that
the __result location is constructed (initialized) by foo,
not be its caller, as implied by this re-write.)

What we want is to have this be replaced by:

void foo(T& __result)
{
  T&x = __result;
  ... do stuff with x ...;
  // `__result = x' is no longer needed.
}

The problem is recognizing that we can alias the storage for x
with the __result location.  Note this aliasing needs to be
done when x is declared, but we cannot know we should do this
until we see the return statement (all of them).  Doing this
optimization is easy in concept, but difficult in practice, given
the way g++ is currently structured.

	--Per Bothner
Cygnus Solutions     bothner@cygnus.com     http://www.cygnus.com/~bothner


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]