From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,7a26bf34a4749377 X-Google-Attributes: gidfee84,public From: Joe Mario Subject: Re: C compiler optimization of inline memcpy() Date: 2000/02/23 Message-ID: <38B40372.3BCEB9EA@zk3.dec.com>#1/1 X-Deja-AN: 588879866 Content-Transfer-Encoding: 7bit References: <6cbt5957h8.fsf@rational.com> X-Accept-Language: en Content-Type: text/plain; charset=us-ascii X-Complaints-To: iass@lead.zk3.dec.com X-Trace: lead.zk3.dec.com 951321217 16521 16.141.0.18 (23 Feb 2000 15:53:37 GMT) Mime-Version: 1.0 NNTP-Posting-Date: 23 Feb 2000 15:53:37 GMT Newsgroups: comp.unix.osf.osf1 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