This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [C PATCH] Discard P - (P + CST) optimization in pointer_diff (PR c/61240)
- From: Marek Polacek <polacek at redhat dot com>
- To: Richard Biener <rguenther at suse dot de>
- Cc: GCC Patches <gcc-patches at gcc dot gnu dot org>, "Joseph S. Myers" <joseph at codesourcery dot com>, Jakub Jelinek <jakub at redhat dot com>
- Date: Mon, 4 Aug 2014 14:01:11 +0200
- Subject: Re: [C PATCH] Discard P - (P + CST) optimization in pointer_diff (PR c/61240)
- Authentication-results: sourceware.org; auth=none
- References: <20140804101856 dot GB24292 at redhat dot com> <alpine dot DEB dot 2 dot 11 dot 1408041239590 dot 1638 at laptop-mg dot saclay dot inria dot fr> <alpine dot LSU dot 2 dot 11 dot 1408041249420 dot 20733 at zhemvz dot fhfr dot qr>
On Mon, Aug 04, 2014 at 12:51:06PM +0200, Richard Biener wrote:
> On Mon, 4 Aug 2014, Marc Glisse wrote:
>
> > On Mon, 4 Aug 2014, Marek Polacek wrote:
> >
> > > +/* { dg-do compile } */
> > > +
> > > +void
> > > +foo (void)
> > > +{
> > > + volatile __PTRDIFF_TYPE__ t;
> > > + int i;
> > > + int *p = &i;
> > > + int *q = &i + 1;
> > > + t = q - (q - 1);
> > > + t = (q - 1) - q; /* { dg-warning "integer overflow in expression" } */
> > > + t = p - (p - 1);
> > > + t = (p - 1) - p ; /* { dg-warning "integer overflow in expression" } */
> > > +}
> >
> > Why do you want a warning for (q - 1) - q ? It looks like a perfectly correct
> > way to say -1 to me (ptrdiff_t is a signed type for a reason).
It's not that I want the warning there. I can probably drop the two
lines from the test.
> But computing &object - 1 does not result in a valid pointer,
> so p - 1 is what we want to warn about? OTOH (q - 1) - q is fine.
>
> Indeed that POINTER_PLUS_EXPR has an unsigned offset is an implementation
> detail.
I'm inclined to think that we shouldn't issue the warning at all, note
how it talks about "integer overflow", but this is pointer arithmetic.
We can't really decide at that point whether the pointer is valid I'm
afraid.
Marek