From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21734 invoked by alias); 13 Sep 2014 07:23:10 -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 21718 invoked by uid 89); 13 Sep 2014 07:23:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-5.1 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Sat, 13 Sep 2014 07:23:06 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s8D7N5N1020573 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Sat, 13 Sep 2014 03:23:05 -0400 Received: from zebedee.pink ([10.3.113.10]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s8D7N1AB018519; Sat, 13 Sep 2014 03:23:03 -0400 Message-ID: <5413F0D4.5010806@redhat.com> Date: Sat, 13 Sep 2014 07:23:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.8.0 MIME-Version: 1.0 To: haynberg@yahoo.com, "gcc-help@gcc.gnu.org" Subject: Re: is portable aliasing possible in C++? 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> In-Reply-To: <1410562688.66898.YahooMailNeo@web140201.mail.bf1.yahoo.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00076.txt.bz2 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)? Sure. Try it. >>> Putting alignment/padding concerns aside > FYI, I meant assume I'm on x86, or hardware that allows unaligned reads, > and my struct doesn't have padding issues. For example, sending a pragma > packed struct over a socket. > >>> 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’s undefined by the standard? >> >> Yes. It may, and it does. > At some point, I understand GCC began to optimize more heavily in > strict-aliasing opportunities. For future reference, when GCC makes > changes like this, are they always mentioned in the release notes or > someplace else? Not AFAIK. We take the view that GCC is free to optimize as much as possible, subject to the constraints of the language. And, of course, we can't always predict what an optimization might do to every buggy program. Andrew.