From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5633 invoked by alias); 2 Nov 2014 23:55:41 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 5618 invoked by uid 89); 2 Nov 2014 23:55:40 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.5 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 X-HELO: nm20.bullet.mail.bf1.yahoo.com Received: from nm20.bullet.mail.bf1.yahoo.com (HELO nm20.bullet.mail.bf1.yahoo.com) (98.139.212.179) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Sun, 02 Nov 2014 23:55:39 +0000 Received: from [98.139.212.150] by nm20.bullet.mail.bf1.yahoo.com with NNFMP; 02 Nov 2014 23:55:37 -0000 Received: from [98.139.215.251] by tm7.bullet.mail.bf1.yahoo.com with NNFMP; 02 Nov 2014 23:55:37 -0000 Received: from [127.0.0.1] by omp1064.mail.bf1.yahoo.com with NNFMP; 02 Nov 2014 23:54:51 -0000 Received: (qmail 12963 invoked by uid 60001); 2 Nov 2014 23:54:51 -0000 Received: from [219.79.98.57] by web165003.mail.bf1.yahoo.com via HTTP; Sun, 02 Nov 2014 15:54:51 PST References: <1410390231.39617.YahooMailNeo@web140202.mail.bf1.yahoo.com> <54115917.1040602@redhat.com> <1410477938.56522.YahooMailNeo@web140205.mail.bf1.yahoo.com> <5412AF85.1080200@redhat.com> Message-ID: <1414972491.23580.YahooMailNeo@web165003.mail.bf1.yahoo.com> Date: Sun, 02 Nov 2014 23:55:00 -0000 From: Hei Chan Reply-To: Hei Chan Subject: Re: is portable aliasing possible in C++? To: Andrew Haley , "gcc-help@gcc.gnu.org" In-Reply-To: <5412AF85.1080200@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2014-11/txt/msg00002.txt.bz2 Hi, I tried to compile your function with g++ 4.6.2: g++ -Wall foo.cpp -c -o foo.o -O3 -std=3Dc++0 and here is objectdump's output: 0: 48 8b 07 mov (%rdi),%rax 3: 48 89 44 24 f0 mov %rax,-0x10(%rsp) 8: f2 0f 10 44 24 f0 movsd -0x10(%rsp),%xmm0 e: c3 retq=20=20=20 Do I need to get a newer gcc to observe the optimization you mentioned? Thanks! On Friday, September 12, 2014 4:32 PM, Andrew Haley wrote: On 12/09/14 00:25, haynberg@yahoo.com wrote: >>> msg *p =3D reinterpret_cast(get_bytes()); >> >> Why are you doing this? >=20 > For efficiency, by preventing a copy (imagine get_bytes() is getting > bytes out of a socket buffer). Firstly, char types alias everything. Secondly, even if you call memcpy(), a compiler doesn't have to do any copies if it can prove that the union you're reading into doesn't escape. Look at this: double kludge(void *p) { union { char bytes[sizeof (double)]; double d; } u; memcpy(u.bytes, p, sizeof u.bytes); return u.d; } which generates kludge: ldr d0, [x0] ret and is completely portable, with no undefined behaviour. > Putting alignment/padding concerns aside, it would be nice if there > was a way to explicitly tell the compiler, I want to do this and, > please don=E2=80=99t reorder stores and loads, or perform other > strict-aliasing optimizations, on the memory pointed to by this > pointer (similar to the effect of a memcpy). I believe the only way > to do this is with the GCC may_alias attribute, or a more > heavy-handed memory clobber. I think the OP wanted to ask the GCC > folks if there was another, possibly more portable, way; for > example, placement new, but that turned out not to be an option. Well, there isn't a more portable way, and we can't ignore alignment. All that GCC can do is provide a way to do it; we can't make anyone else comply. > Another related, maybe more important, question is if GCC sees a > reinterpet_cast like this (without a may_alias type), is it free to > discard code or otherwise drastically change it due to the fact that > it=E2=80=99s undefined by the standard? Yes. It may, and it does. Andrew.