Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* C compiler optimization of inline memcpy()
@ 2000-02-22  5:00 John Kohl
  2000-02-23  5:00 ` Joe Mario
  0 siblings, 1 reply; 2+ messages in thread
From: John Kohl @ 2000-02-22  5:00 UTC (permalink / raw)


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. (See the annotated assembly output below.)

The manual declares memcpy() as:
  void *memcpy(
          void *s1,
          const void *s2,
          size_t n);
and says memcpy() conforms to ISO C, XPG4, and XPG4-UNIX.

Is it legitimate for the compiler to generate ldq instructions to
dereference the pointer?

Witness:

#include <string.h>

extern void
bar(unsigned long **x);

unsigned long
foo_long(char *abc)
{
    unsigned long *foo;
    unsigned long retval;

    bar(&foo);
    memcpy(&retval, foo, sizeof(unsigned long));
    return retval;
}

unsigned long
foo_char(char *abc)
{
    unsigned long *foo;
    unsigned long retval;

    bar(&foo);
    memcpy(&retval, (char *)foo, sizeof(unsigned long));
    return retval;
}

Yields this assembly:
% dis foo.o
        foo_long:
  [foo.c:   7] 0x0:     27bb0001        ldah    gp, 1(t12)
  [foo.c:   7] 0x4:     2ffe0000        ldq_u   zero, 0(sp)
  [foo.c:   7] 0x8:     23bd80a0        lda     gp, -32608(gp)
  [foo.c:   7] 0xc:     2ffe0000        ldq_u   zero, 0(sp)
  [foo.c:  12] 0x10:    a77d8010        ldq     t12, -32752(gp)
  [foo.c:   7] 0x14:    23defff0        lda     sp, -16(sp)
  [foo.c:   7] 0x18:    b75e0000        stq     ra, 0(sp)
  [foo.c:  12] 0x1c:    221e0008        lda     a0, 8(sp)
  [foo.c:  12] 0x20:    6b5b4000        jsr     ra, (t12), bar
  [foo.c:  13] 0x24:    a41e0008        ldq     v0, 8(sp)
  [foo.c:  12] 0x28:    27ba0001        ldah    gp, 1(ra)
  [foo.c:  15] 0x2c:    a75e0000        ldq     ra, 0(sp)
  [foo.c:  13] 0x30:    a4000000        ldq     v0, 0(v0)	<<<<<
  [foo.c:  12] 0x34:    23bd807c        lda     gp, -32644(gp)
  [foo.c:  15] 0x38:    23de0010        lda     sp, 16(sp)
  [foo.c:  15] 0x3c:    6bfa8001        ret     zero, (ra), 1
        foo_char:
  [foo.c:  18] 0x40:    27bb0001        ldah    gp, 1(t12)
  [foo.c:  18] 0x44:    2ffe0000        ldq_u   zero, 0(sp)
  [foo.c:  18] 0x48:    23bd8060        lda     gp, -32672(gp)
  [foo.c:  18] 0x4c:    2ffe0000        ldq_u   zero, 0(sp)
  [foo.c:  23] 0x50:    a77d8010        ldq     t12, -32752(gp)
  [foo.c:  18] 0x54:    23defff0        lda     sp, -16(sp)
  [foo.c:  18] 0x58:    b75e0000        stq     ra, 0(sp)
  [foo.c:  23] 0x5c:    221e0008        lda     a0, 8(sp)
  [foo.c:  23] 0x60:    6b5b4000        jsr     ra, (t12), bar
  [foo.c:  24] 0x64:    a41e0008        ldq     v0, 8(sp)
  [foo.c:  23] 0x68:    27ba0001        ldah    gp, 1(ra)
  [foo.c:  26] 0x6c:    a75e0000        ldq     ra, 0(sp)
  [foo.c:  24] 0x70:    2c400000        ldq_u   t1, 0(v0)	<<<<<
  [foo.c:  23] 0x74:    23bd803c        lda     gp, -32708(gp)
  [foo.c:  24] 0x78:    2c600007        ldq_u   t2, 7(v0)	<<<<<
  [foo.c:  24] 0x7c:    484006c2        extql   t1, v0, t1	<<<<<
  [foo.c:  24] 0x80:    48600f43        extqh   t2, v0, t2	<<<<<
  [foo.c:  24] 0x84:    44430400        bis     t1, t2, v0	<<<<<
  [foo.c:  26] 0x88:    23de0010        lda     sp, 16(sp)
  [foo.c:  26] 0x8c:    6bfa8001        ret     zero, (ra), 1

--
John Kohl <jtk@rational.com>
working for but not speaking for: Rational Software <http://www.rational.com>



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

* Re: C compiler optimization of inline memcpy()
  2000-02-22  5:00 C compiler optimization of inline memcpy() John Kohl
@ 2000-02-23  5:00 ` Joe Mario
  0 siblings, 0 replies; 2+ messages in thread
From: Joe Mario @ 2000-02-23  5:00 UTC (permalink / raw)


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




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

end of thread, other threads:[~2000-02-23  5:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-02-22  5:00 C compiler optimization of inline memcpy() John Kohl
2000-02-23  5:00 ` Joe Mario

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