From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5827 invoked by alias); 13 Sep 2014 11:45:11 -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 5810 invoked by uid 89); 13 Sep 2014 11:45:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.9 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mailout04.t-online.de Received: from mailout04.t-online.de (HELO mailout04.t-online.de) (194.25.134.18) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sat, 13 Sep 2014 11:45:09 +0000 Received: from fwd36.aul.t-online.de (fwd36.aul.t-online.de [172.20.26.137]) by mailout04.t-online.de (Postfix) with SMTP id 02A1243C51F; Sat, 13 Sep 2014 13:45:05 +0200 (CEST) Received: from [192.168.0.106] (bNru6EZ1rhah1pekR2gsJcsfqUVDXp5LVoIQeJUU0QlIGEle-Ah9lwvZ1Hs4rONZLG@[79.241.195.243]) by fwd36.t-online.de with (TLSv1:ECDHE-RSA-AES256-SHA encrypted) esmtp id 1XSllH-0WARiy0; Sat, 13 Sep 2014 13:45:03 +0200 Message-ID: <1410608702.2384.33.camel@yam-132-YW-E178-FTW> Subject: Re: is portable aliasing possible in C++? From: Oleg Endo To: Andrew Haley Cc: haynberg@yahoo.com, "gcc-help@gcc.gnu.org" Date: Sat, 13 Sep 2014 11:45:00 -0000 In-Reply-To: <5413F0D4.5010806@redhat.com> 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> <1410562688.66898.YahooMailNeo@web140201.mail.bf1.yahoo.com> <5413F0D4.5010806@redhat.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Mime-Version: 1.0 X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00077.txt.bz2 On Sat, 2014-09-13 at 08:23 +0100, Andrew Haley wrote: > On 12/09/14 23:58, haynberg@yahoo.com wrote: > >> Firstly, char types alias everything. > > (I'm not sure why you wrote that?) > > Because the specification says so. 6.3.2.3, Pointers, in C9X. > > > I know if you cast to char type, > > it can, but I'm going from a char type. > > > >> 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. > > If the compiler can optimize away memcpy, why memcpy into a union, why > > not just to the type in question? Or is memcpy into a union special? > > You can copy the bytes from one object to another, and it has the > same effect. I can't guarantee it generates the same code as a > union in all cases. > > > In other words, if I write: > > > > msg p; > > memcpy(&p, get_bytes(), sizeof p); // assume the size OK > > if (p.i) > > // ... > > > > Can the memcpy be optimized away, making it similar to the cast version > > (but not undefined)? I ran into a similar thing a while ago. See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59478 for some experiments. Probably the optimizations will look differently on other targets than SH. On strict alignment targets there's also this memcpy issue https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50417 which might be related. Cheers, Oleg