This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: memcpy / Language Lawyer / optimization question


Steve Ellcey <sje@cup.hp.com> writes:

| 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?

There is an explicit permission of copying an object into a possibly
non-aligned array of char or unsigned, with say memcpy(), and copy it
back with no problem.  

I believe the rule

       [#26] A pointer to void shall have the  same  representation
       and  alignment  requirements  as  a  pointer  to a character
       type.39)  Similarly, pointers to  qualified  or  unqualified
       versions   of   compatible   types   shall   have  the  same
       representation and alignment requirements.  All pointers  to
       structure  types  shall  have  the  same  representation and
       alignment requirements as each other.  All pointers to union
       types  shall  have  the  same  representation  and alignment
       requirements as each other.  Pointers to  other  types  need
       not  have the same representation or alignment requirements.

implies that any alignment derivation we make on char* shall also hold
for void*.  In particular the use of void* should inhibit the
transformation of memcpy into an object assignment.

-- Gaby


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]