Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: Joe Mario <mario@zk3.dec.com>
Subject: Re: C compiler optimization of inline memcpy()
Date: 2000/02/23	[thread overview]
Message-ID: <38B40372.3BCEB9EA@zk3.dec.com> (raw)
In-Reply-To: 6cbt5957h8.fsf@rational.com

John Kohl wrote:

> Is this a legitimate optmization, or a compiler gone too far?
>
> This compiler:
>         % cc -V
>         cc  (cc)
>                 Tru64 UNIX Compiler Driver 5.0
>         Compaq C V6.1-013 on Digital UNIX V5.0 (Rev. 910)
>
> with these flags:
>         % cc -migrate -c -fast -tune ev5 -arch ev4 -g3 foo.c
>
> generates different code for inline memcpy() depending on the type of
> the source pointer in the call.  If the type is (unsigned long *), then
> it assumes the pointer value is aligned and can be dereferenced as-is.
> If it's (char *), it uses unaligned loads and bit shifts to effect the
> copy.

The compiler is doing a legitimate optimization.

The basic thing that allows a compiler to assume something about alignment
is the type of the lvalue used to access memory.  In the case of a pointer,
the type used to access memory is the "pointed-to" type.  In the case
of passing a pointer to the memcpy routine, memcpy itself has a prototype
specifying void * as the type of parameters; the type void * specifies
no information at all about the pointed-to type (its alignment or anything
else) - in particular, in contrast to char *, it does NOT specify that
the pointed-to object is aligned only on a char boundary.

So when you invoke memcpy with a pointer-to-long, and the compiler
understands the purpose of memcpy is to access the pointed-to object
(the standard permits compilers to implement standard library functions
with inline code), the alignment of the object is determined by the type
of the pointer expression passed as the actual argument.  The cast to
void * performed by the prototype has no effect on the alignment.  It
might cause a change in representation on a platform where char* pointers
have a different representation than long* pointers, but that doesn't
affect the alignment of the pointed-to object.

Joe Mario
Compaq Tru64 Unix Development Environment




      reply	other threads:[~2000-02-23  5:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-22  5:00 C compiler optimization of inline memcpy() John Kohl
2000-02-23  5:00 ` Joe Mario [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox