From 1473770494643082619
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,f5d433881cc29cff
X-Google-Attributes: gidf78e5,public
From: "Bradd W. Szonye" <bradds@concentric.net>
Subject: Re: Named return value optimisation- reprise.
Date: 1997/06/18
Message-ID: <01bc7bf8$bb2c9be0$a56dadce@azguard>#1/1
X-Deja-AN: 249363321
References: <33a345bf.7425526@news.ipswich.gil.com.au> <33A478C1.329@acm.org> <u9pvtnuemu.fsf@yorick.cygnus.com> <33A65B20.595D@aom.ericsson.se> <5o644g$3fj$1@toralf.uib.no> <5o7lbs$3a6@mulga.cs.mu.OZ.AU>
X-Original-Date: 18 Jun 1997 15:06:47 GMT
Organization: Concentric Internet Services
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBM6gHmEy4NqrwXLNJAQFUtwH7Bk8THrOoXVXOxFZWOrZ3ArXBEDjzpcBd uPMCk0uRnnDwjf7rGaq48NdZG1b7WCHOfd7mqz00QxPBDGB/sGq64Q== =jaxO
Newsgroups: comp.std.c++
Originator: austern@isolde.mti.sgi.com


Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote in article
<5o7lbs$3a6@mulga.cs.mu.OZ.AU>...
> 
> The rule allows compilers to optimize
> away copies if _either_ the source or the target of the copy is not
> used again.

Okay, I finally looked up the rule in question after seeing these examples.
The trouble with 12.8/15 is that, in the presence of possible aliasing,
it's undecidable whether an object is actually used again. Take, for
example:

    void foo(T t)
    {
        T = tval1;
    }

    int main()
    {
        T t = tval0;
        T * tp = &t;
        foo(t);
        cout << *tp << endl;
    }

Does this print tval0 or tval1? It depends on whether the compiler performs
value-parameter optimization and on whether use of tp is a use of t. I
would argue that, even though it's an aliased use, it's still definitely a
use of t, and behaving otherwise would break reasonable programs. This
example is very simple, however; it's not hard to construct examples where
it's not clear--not even to humans--whether an object is used again.
Practically speaking, except for compiler-generated temporaries and objects
which are otherwise about to go out of scope immediately, it's undecidable
whether "either the original object will never again be used." It's hard
even to come up with a reasonable definition of "used."

I wouldn't mind seeing a non-normative note added to the example in 12.8/15
to the effect that: "Determining whether an object is used again is in
general undecidable, and implementations should be conservative in applying
this optimization. In particular, an implementation should ensure that
pass-by-value has the expected semantics in the presence of aliases." It's
weaselly, true, but it would help discourage implementors from breaking
pass-by-value.

Fortunately, the places where an object can "never again be used" are
decidable for the most useful optimizations: variables returned from
functions immediately before they go out of scope, and temporaries
constructed in the course of initializations. Those are the cases where
Machievelli could probably screw up the works, but Murphy is much less of a
problem.

Don't forget that implementors don't want to break your code. That's why
some like Sun and Microsoft are slower to jump on the "new features"
bandwagon discussed elsewhere. Commercial compilers are much less likely to
optimize away pass-by-value than, say, experimental compilers.
-- 
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]



