This is the mail archive of the 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: inlined memcpy/memset degradation in gcc 4.6 or later

On Thu, Oct 4, 2012 at 7:44 PM, Joe Buck <> wrote:
> > Perhaps I'm missing something.  While memcpy is not permitted to assume
> > alignment of its arguments, copy is.  Otherwise, if I wrote
> >
> > void copy(struct foo* f0, struct foo* f1)
> > {
> >     *f0 = *f1;
> > }
> >
> > the compiler would also be forbidden from assuming any alignment.  So,
> > when compiling "copy", do we lack the information that the memcpy call is
> > to the standard ISO memcpy function?  If we know that it is the standard
> > function we should be able to do the copy assuming everything is properly
> > aligned.
On Fri, Oct 05, 2012 at 10:32:55AM +0200, Richard Guenther wrote:
> If we see the above aggregate copy then we should be able to compile
> the function assuming that f0 and f1 are properly aligned for type struct foo.
> If we see C source code using memcpy (f0, f1, sizeof (struct foo)) then
> we cannot assume anything about the alignment of f0 or f1 based on the
> fact that the code uses the ISO memcpy function.

Sorry, that makes no sense at all.  Let's say that I'm a user of the
function "copy", and I don't know if the implementer of "copy" chose to

	*f0 = *f1;

or if she used memcpy.  What you are telling me is that I need to know
that information, because in one case I am permitted to pass in nonsense
pointers that only claim to point to a (struct foo), and in the other
case I have to use proper struct foo's, aligned according to the rules of
that platform.  In fact, if I pass invalid pointers I deserve to lose, and
GCC should not be required to implement pessimistic code to deal with a
possibility that cannot occur.  A (struct foo*) has to point either to a
proper structure or be null.

What I am assuming is that memcpy(f0, f1, sizeof(struct foo)) is
equivalent to *f0 = *f1, because that is what it does: it copies the
structure.  The types of the pointers tell me the required alignment.
If there is language in the standard indicating otherwise then the
standard is defective, because it prevents an obvious optimization.

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