Hi!

On Tue, May 05, 2020 at 08:41:35PM -0400, Hans-Peter Nilsson wrote:
> On Fri, 1 May 2020, Segher Boessenkool wrote:
> > On Mon, Apr 27, 2020 at 05:01:34PM -0500, will schmidt wrote:
> > > On Mon, 2020-04-27 at 15:53 -0400, Michael Meissner via Gcc-patches wrote:
> > > > +unsigned long
> > > > +load_us_offset1 (unsigned char *p)
> > > > +{
> > > > +  return *(unsigned short *)(p + 1);   /* should generate LHZ.  */
> > > > +}
> >
> > This violates aliasing rules (without -fno-strict-aliasing), unless p
> > points to some "short" object -- in which case it has alignment 2
> > already!
> 
> Ackchyually... that'd be 'unless p+1 points to some "short" object'.

Erm yes.

> You're allowed to do weird things passing char pointers around
> (at least), as long as you point to the right-type object (the
> one used when it was assigned) when you dereference it.

And when you cast it to the "bigger" pointer type, already, in general.
Which isn't a problem for any of the powerpc targets at least, not sure
there is any GCC target where it matters currently?

> Though, I don't know if the same goes if p was something other
> than a "char *" or "unsigned char *".

Sure, it is true always: you can do whatever you want with the address,
cast it (validly) to other pointer types, to integer types, slice it,
dice it, whatever you want; but when you dereference it, *that* is where
the "aliasing rules" apply.

> > So this testcase needs -fno-strict-aliasing, but also, you need to make
> > sure GCC does not assume alignment 2 (after the cast) already.  There
> > are attributes for this.
> 
> FWIW, if it needs -fno-strict-aliasing it's for some other
> reason than the above (we don't see the assigner in the test).

Not to be valid code, no.  But for the testcase to test what it wants
to test, it would help.

The point is that you can do a misaligned "lhz" instruction just fine;
you do not need a prefixed form for it, as with say "ld", where the
offset addressing form always does a multiple of four ("DS-form"), while
"pld" (its prefixed version) can take any integer offset ("D-form").  If
GCC already would assume the memory access is naturally aligned the test
doesn't test anything.

But together with it choosing the prefixed forms for ld and lwa etc.,
it's probably good enough anyway.


Segher

Reply via email to