Topic: Named local variable return value optimization


Author: "Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com>
Date: Thu, 7 Jun 2001 16:25:38 GMT
Raw View
John,

Here's the output of the program,

min D max D = D
min D max DC = DC
min D max DC = D
min D max DCC = DC
min D max DCC = D
min D max DCC = DC
min D max DCC = D
min DD max DDCC = DDC
min DD max DDCC = DD

it shows that the Intel C++ 5.0 compiler fails to optimize away the
temporary object when doing direct-initialization. I think this is a bug
since it does proper optimization when doing copy-initialization. I'll bug
Intel support about it. As you can see, it does utilize RVO though.

Bo-Staffan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Pavel Kuznetsov" <pavel_kuznetsov@no.spam.users.sourceforge.net>
Date: Thu, 7 Jun 2001 16:45:34 GMT
Raw View
VC++ 6.0 SP4

min D max D = D        // T t1;
min D max DC = DC      // T t2((0, T()));
min D max DC = D       // T t3 = T();
min D max DCC = D      // T t4(f1());
min D max DCC = D      // T t5 = f1();
min D max DCC = DC     // T t6(f2());
min D max DCC = DC     // T t7 = f2();
min DD max DDCC = DDC  // T t8(f3());
min DD max DDCC = DDC  // T t9 = f3();

Intel 4.5 (616)

min D max D = D        // T t1;
min D max DC = DC      // T t2((0, T()));
min D max DC = D       // T t3 = T();
min D max DCC = DC     // T t4(f1())
min D max DCC = D      // T t5 = f1();
min D max DCC = DC     // T t6(f2());
min D max DCC = D      // T t7 = f2();
min DD max DDCC = DDC  // T t8(f3());
min DD max DDCC = DD   // T t9 = f3();

--
Pavel Kuznetsov
mailto:pavel_kuznetsov@no.spam.users.sourceforge.net
remove "no.spam." from e-mail address


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com>
Date: Thu, 7 Jun 2001 17:06:38 GMT
Raw View
John,

Here's the output of the program,

min D max D = D
min D max DC = DC
min D max DC = D
min D max DCC = DC
min D max DCC = D
min D max DCC = DC
min D max DCC = D
min DD max DDCC = DDC
min DD max DDCC = DD

it shows that the Intel C++ 5.0 compiler fails to optimize away the
temporary object when doing direct-initialization. I think this is a bug
since it does proper optimization when doing copy-initialization. I'll bug
Intel support about it. As you can see, it does utilize RVO though.

Bo-Staffan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Radoslav Getov" <nospam@mai.com>
Date: Tue, 5 Jun 2001 14:41:37 GMT
Raw View
Some previous posts to this and other groups say that the standard allows
named local variable return value optimization (NLVRVO). So, I assume that
in code like:

X func()
{
    X x;
    ...
    return x;
}

it is quite legal that no X copy constructor is called.

I have several questions:

1. Am I all wrong about it?
2. Where does this support read in the Standard?
3. Are there any limitations on this?
4. Which compilers are known to support NLVRVO? In particular, does MSVC60
have it?

Thanks
Radoslav Getov



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Tue, 5 Jun 2001 19:03:30 GMT
Raw View
On Tue,  5 Jun 2001 14:41:37 GMT, "Radoslav Getov" <nospam@mai.com>
wrote:

> Some previous posts to this and other groups say that the standard allows
> named local variable return value optimization (NLVRVO). So, I assume that
> in code like:
>
> X func()
> {
>     X x;
>     ...
>     return x;
> }
>
> it is quite legal that no X copy constructor is called.
>
> I have several questions:
>
> 1. Am I all wrong about it?

No.

> 2. Where does this support read in the Standard?

12.8/15

> 3. Are there any limitations on this?

No.

> 4. Which compilers are known to support NLVRVO?

IBM vasual age.

> In particular, does MSVC60 have it?

No.

A search may find an "NRVO much ado about nothing" thread in
comp.lang.c++.moderated with results for several compilers.  It
includes test code.  Given the above:

X x(func());

Could use two copy ctors.  First from the internal x to the return
value and second from there to this x.  Most compilers will remove
the second one.  Either BC or VC does both.  VA removes both.  At
last report, EDG only removes the second.  The results are dated,
but nobody has shown any interest lately.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
Date: Tue, 5 Jun 2001 23:09:18 GMT
Raw View
"Radoslav Getov" <nospam@mai.com> writes:

| Some previous posts to this and other groups say that the standard allows
| named local variable return value optimization (NLVRVO). So, I assume that
| in code like:
|
| X func()
| {
|     X x;
|     ...
|     return x;
| }
|
| it is quite legal that no X copy constructor is called.
|
| I have several questions:
|
| 1. Am I all wrong about it?

No.

| 2. Where does this support read in the Standard?

12.8/15

| 3. Are there any limitations on this?

Normal semantics restrictions still apply, that is the
copy-constructor should be accessible for example.

As an aside, the "return value optimization" isn't really an
optimization, since an optimization should not affect a program
observable behaviour.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Bo-Staffan Lankinen" <bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com>
Date: Tue, 5 Jun 2001 23:12:28 GMT
Raw View
> Some previous posts to this and other groups say that the standard allows
> named local variable return value optimization (NLVRVO). So, I assume that
> in code like:
>
> X func()
> {
>     X x;
>     ...
>     return x;
> }
>
> it is quite legal that no X copy constructor is called.
>
> I have several questions:
>
> 1. Am I all wrong about it?

No, you are correct.

> 2. Where does this support read in the Standard?

12.8/15

> 3. Are there any limitations on this?

12.8/15

> 4. Which compilers are known to support NLVRVO? In particular, does MSVC60
> have it?

AFAIK, VC++6.0 doesn't utilize NLVRVO. I know that the Intel C++ 5.0 fully
supports RVO, since I'm using it myself. I take it that you are using the
VC6.0++ IDE, the Intel C++ 5.0 compiler can be used as a plug-in to the
VC++6.0 IDE. Other benefits of the Intel C++ 5.0 compiler is that it's much
more standard C++ conforming and produces better code, atleast with respect
to execution-speed which is most important for me, than VC++6.0. There is an
evaluation version, that you can try for free up to a month, somewhere at
www.intel.com.

Bo-Staffan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: Wed, 6 Jun 2001 14:30:11 GMT
Raw View
I have included a test program for this optimization (or as Gabby says
permission to change the semantics of a program :).  I am willing to
collect results for current compilers and post a summary.  Mods, please
add a note on whether you would prefer posts or a summary.

On Tue,  5 Jun 2001 23:12:28 GMT, "Bo-Staffan Lankinen"
<bSoP_AsMteSfUfCaKnS_lankinen@hotmail.com> wrote:

> I know that the Intel C++ 5.0 fully
> supports RVO, since I'm using it myself.

That may be a new data point for me.  Here is a test.

#include <iostream>
struct T {
    T () { cout << "D"; }
    T (T const&) { cout << "C"; }
    };
T f1 () { return T(); }
T f2 () { T t; return t; }
T f3 () { T t; t = T(); return t; }
int main () {
    cout << "min D max D = "; T t1; cout << endl;
    cout << "min D max DC = "; T t2((0, T())); cout << endl;
// The "0," is a workaround for gcc-2.95.2, I think.  The parens around
// the T() should make it an expression.
    cout << "min D max DC = "; T t3 = T(); cout << endl;
    cout << "min D max DCC = "; T t4(f1()); cout << endl;
    cout << "min D max DCC = "; T t5 = f1(); cout << endl;
    cout << "min D max DCC = "; T t6(f2()); cout << endl;
    cout << "min D max DCC = "; T t7 = f2(); cout << endl;
    cout << "min DD max DDCC = "; T t8(f3()); cout << endl;
    cout << "min DD max DDCC = "; T t9 = f3(); cout << endl;
    }

Gcc hits min on the first five and adds one extra C on the last four
which test this thing.  It does not provide it.  This is a front end
feature and should not be affected by optimization level.  It may
require a special switch to enable it.

John

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]