This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: missing optimization - don't compute return value not used?
On Thu, Sep 27, 2007 at 01:17:58PM -0500, Aaron W. LaFramboise wrote:
> I think the biggest problem here is that GCC will not elide calls to the
> allocator. This is a subject of some controversy--even though its
> probably difficult to do such optimization anyway. It's not quite clear
> that such optimization is legal under the as-if rule.
> For example, the code you have written will throw std::bad_alloc if your
> system is out of memory. The code that you would like it to be
> optimized to would not do that, which is a behavior change.
The problem with such an argument is that the standard does not say
anything about how much memory you have. Also, since eliding a copy
constructor will, in general, reduce the number of calls to operator new,
we are already doing optimizations that might eliminate a bad_alloc
throw. Of course, eliding copy constructors (even if there are side
effects) is explicitly blessed by the standard.
Considering memory allocation inside a constructor as a side effect
that must be preserved has some interesting and not-very-desirable
effects. Consider
int foo1() {
std::vector<int> unused_v(5);
return 1;
}
int foo2() {
int unused_a[5];
return 1;
}
gcc -Wall will issue a warning about foo2, but not about foo1.
That's because the constructor "does something" to unused_v.
Now, there are some cases where we really do construct an object
just for its side effect, particularly with the "resource allocation
by initialization" pattern.
But it would be cool if there were a way to mark a class somehow
to indicate that it works like a value, meaning that it can be
optimized as such, and that a warning would be appreciated if it
is unused after construction.