public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: memcpy / Language Lawyer / optimization question
@ 2004-12-09 22:54 Paul Schlie
  2004-12-09 23:36 ` Geoffrey Keating
  2004-12-09 23:53 ` Joe Buck
  0 siblings, 2 replies; 11+ messages in thread
From: Paul Schlie @ 2004-12-09 22:54 UTC (permalink / raw)
  To: gcc

> Joseph S. Myers wrote:
>> On Thu, 9 Dec 2004, Steve Ellcey wrote:
>> 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?
>
> The casts in the calls to memcpy are irrelevant: the undefined behavior
> occurs before them, at the point where you cast an unaligned pointer to
> int *.  Casting a pointer to another pointer type for which it doesn't
> have the alignment yields undefined behavior.  The compiler can assume
> that the result of a sequence of pointer casts has the alignment of
> whichever type in the sequence of pointer types has the strictest
> alignment requirements.

As a more general but related question: as C does not define/specify
everything, and even occasionally specifies something as being explicitly
"unspecified", giving license to the complier to express whatever incidental
behavior it may have; has the GCC team adopted the strategy that it will
strive to adopt and document the behavior which seems most generally useful
and/or least-fragile in such circumstances (which it has license to do), as
opposed to justifying any such behavior as being "legal" without regard to
the consideration of adopting a more useful behavior, and/or emit warnings
if the adopted behavior may have undesirable consequences when reasonable
to do so; or is this also "unspecified"? :)


^ permalink raw reply	[flat|nested] 11+ messages in thread
* memcpy / Language Lawyer / optimization question
@ 2004-12-09 19:33 Steve Ellcey
  2004-12-09 19:41 ` Joe Buck
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Steve Ellcey @ 2004-12-09 19:33 UTC (permalink / raw)
  To: gcc

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 <string.h>

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];
}

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-12-10 16:51 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-09 22:54 memcpy / Language Lawyer / optimization question Paul Schlie
2004-12-09 23:36 ` Geoffrey Keating
2004-12-09 23:53 ` Joe Buck
2004-12-10  3:18   ` Paul Schlie
  -- strict thread matches above, loose matches on Subject: below --
2004-12-09 19:33 Steve Ellcey
2004-12-09 19:41 ` Joe Buck
2004-12-09 19:52   ` Paul Jarc
2004-12-09 20:11 ` Gabriel Dos Reis
2004-12-09 20:14 ` Joe Buck
2004-12-09 20:20 ` Joseph S. Myers
2004-12-10 16:51   ` Steve Ellcey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).