This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: is portable aliasing possible in C++?
- From: Andy Webber <andy at aligature dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Thu, 4 Sep 2014 13:47:35 -0400
- Subject: Re: is portable aliasing possible in C++?
- Authentication-results: sourceware.org; auth=none
- References: <A76FB9DDEDFA994BAF6B77704A4AF465BC2464 at xchmbbal502 dot ds dot susq dot com> <CADa0DUiuqwF2rbvBcUQOFMN7iVMtedryYLO3i1gCskSRdcOvtg at mail dot gmail dot com> <5408988E dot 2060301 at redhat dot com> <CADa0DUgZ86Z_ZjSX9sPGPeUKWFU3WtOwFk5v6tejO_dR5v9ZUQ at mail dot gmail dot com> <5408A015 dot 5040106 at redhat dot com> <CADa0DUg+cbLjaYtJgwawZccp-M97u5ZzJuvk5_qx-U2SLEbj1A at mail dot gmail dot com>
On 9/4/14, Andy Webber <andy@aligature.com> wrote:
> Know of any way to ask Jason Merrill or Richard Biener to weigh in?
> They seem to be very knowledgable in this area.
>
> On 9/4/14, Andrew Haley <aph@redhat.com> wrote:
>> On 09/04/2014 06:18 PM, Andy Webber wrote:
>>> On 9/4/14, Andrew Haley <aph@redhat.com> wrote:
>>>> On 09/04/2014 05:11 PM, Andy Webber wrote:
>>
>> Regrettably,
>>>>> Our goal is to avoid bugs caused by strict aliasing in our networking
>>>>> libraries. My question is how to guarantee that we're not violating
>>>>> the aliasing rules while also getting the most optimization. I've
>>>>> read through a ton of information about this online and in some gcc
>>>>> discussions, but I don't see a consensus.
>>>>>
>>>>> Memcpy always works, but is dependent on optimization to avoid copies.
>>>>> The union of values is guaranteed to work by C++11, but may involve
>>>>> copies.
>>>>
>>>> Is this a real worry? IME it makes copies when it needs to.
>>>>
>>>>> Each test works when built with -O3 on gcc-4.8.3, but I would like to
>>>>> standardize across compilers and versions. The optimization
>>>>> information generated by -fdump-tree-all is interesting here as it
>>>>> shows slightly different optimization for each case though
>>>>> reinterpret_cast and placement new generate identical code in the end.
>>>>
>>>> The "union trick" has always worked with GCC, and is now hallowed by
>>>> the standard. It's also easy to understand. It generates code as
>>>> efficient as all the other ways of doing it, AFAIAA. It's what we
>>>> have always recommended.
>>>>
>>>> Your test is nice. I suppose we could argue that this is a missed
>>>> optimization:
>>>>
>>>> union_copy():
>>>> movl $2, %eax
>>>> cmpw $2, %ax
>>>> jne .L13
>>>>
>>>> I don't know why we only generate code for one of the tests.
>>>
>>> Thanks for responding. I appreciate any clarity that the gcc devs and
>>> standards experts can give here.
>>>
>>> I'm especially interested in the validity of the placement new
>>> approach. Your recommendation of going through unions causes some
>>> difficulty for us in terms of type abstraction. Specifically,
>>> receiving network bytes directly into a union with all possible
>>> message types present in the union is somewhat less flexible than
>>> determining the correct message type and doing a placement new to
>>> create essentially a memory overlay. Is placement new a suitable
>>> substitute for __may_alias__ in this specific example?
>>
>> I regret that the exact legality of placement new in this context is
>> beyond me. I think it's OK as long as you only do it with POD-types, but
>> I'd have bounce this off someone like Jason Merrill.
>>
>> Andrew.
>>
>>
>>
>
Sorry, didn't mean to top post the last reply.
Know of any way to ask Jason Merrill or Richard Biener to weigh in?
They seem to be very knowledgable in this area.