From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25946 invoked by alias); 9 Dec 2004 19:33:01 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 25900 invoked from network); 9 Dec 2004 19:32:56 -0000 Received: from unknown (HELO palrel12.hp.com) (156.153.255.237) by sourceware.org with SMTP; 9 Dec 2004 19:32:56 -0000 Received: from smtp2.ptp.hp.com (smtp2.ptp.hp.com [15.1.28.240]) by palrel12.hp.com (Postfix) with ESMTP id 0C96A42AA93 for ; Thu, 9 Dec 2004 11:32:56 -0800 (PST) Received: from hpsje.cup.hp.com (hpsje.cup.hp.com [15.244.96.221]) by smtp2.ptp.hp.com (Postfix) with ESMTP id EAAB4567FD for ; Thu, 9 Dec 2004 19:32:55 +0000 (UTC) Received: (from sje@localhost) by hpsje.cup.hp.com (8.9.3 (PHNE_24419+JAGae58098)/8.7.3 TIS Messaging 5.0) id LAA06762 for gcc@gcc.gnu.org; Thu, 9 Dec 2004 11:32:55 -0800 (PST) Date: Thu, 09 Dec 2004 19:33:00 -0000 From: Steve Ellcey Message-Id: <200412091932.LAA06762@hpsje.cup.hp.com> To: gcc@gcc.gnu.org Subject: memcpy / Language Lawyer / optimization question Reply-To: sje@cup.hp.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-SW-Source: 2004-12/txt/msg00332.txt.bz2 I have a question about memcpy and when it is legal to turn it into a simple assignment. The first memcpy in the program below will cause an abort on IA64 when compiled with -O2 because it gets turned into integer assignment and p is not properly aligned. I think this is OK. The last memcpy, where I put in a cast to (char *), will not assume any alignment and this compiles and runs correctly on IA64 with -O2. Again, I think this is OK. My question is about the second memcpy where I cast to (void *). This still results in an abort on IA64 because it is still assuming integer alignment (and thus changing the code to do an integer assignment). Is it legal to do this transformation with the (void *) cast? I ran into this in the libgfortran where I am trying to replace an assignment of a long into a (possibly) unaligned buffer with a call to memcpy. Steve Ellcey sje@cup.hp.com ------------------------------ #include static char buffer[80]; static int *foo(); main () { int i; int *p; i = 0; p = foo(); /* Causes core dump with -O2 */ memcpy(p, &i, sizeof (int)); /* Causes core dump with -O2 */ memcpy((void *) p, (void *) &i, sizeof (int)); /* Works with -O2 */ memcpy((char *) p, (char *) &i, sizeof (int)); } int *foo() { return (int *) &buffer[1]; }