Re: [PATCH 01/12] extarray: define helpers for arrays defined in linker scripts

From: Richard Biener
Date: Wed Oct 19 2016 - 10:24:33 EST


On Wed, 19 Oct 2016, Peter Zijlstra wrote:

> On Wed, Oct 19, 2016 at 10:18:43AM +0200, Richard Biener wrote:
>
> > The commit implements a long-standing failure to optimize trivial pointer
> > comparisons that arise for example from libstdc++. PR65686 contains
> > a simple C example:
> >
> > mytype f(struct S *e)
> > {
> > mytype x;
> > if(&x != e->pu)
> > __builtin_memcpy(&x, e->pu, sizeof(unsigned));
> > return x;
> > }
> >
> > where GCC before the commit could not optimize the &x != e->pu test
> > as trivial false.
>
> Which is fine; x is stack based and could not possibly have been handed
> as the argument to this same function.

Sure, it was just one example.

> This is also an entirely different class of optimizations than the whole
> pointer arithmetic is only valid inside an object thing.

Yes, it is not related to that. I've opened
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78035 to track an
inconsistency in that new optimization.

> The kernel very much relies on unbounded pointer arithmetic, including
> overflow. Sure, C language says its UB, but we know our memory layout,
> and it would be very helpful if we could define it.

It's well-defined and correctly handled if you do the arithmetic
in uintptr_t. No need for knobs.

> Can't we get a knob extending -fno-strict-aliasing to define pointer
> arithmetic outside of objects and overflow? I mean, we already use that,
> we also use -fno-strict-overflow and a whole bunch of others.
>
> At the very least, it would be nice to get a -W flag for when this alias
> analysis stuff kills something so we can at least know when GCC goes and
> defeats us.

What kind of warning do you envision?

"warning: optimized address comparison to always true/false"

? That would trigger all over the place.

Richard.