On Mon, 20 Aug 2018, Richard Biener wrote:
On Mon, Aug 20, 2018 at 10:53 AM Andreas Schwab <sch...@suse.de> wrote:
On Aug 20 2018, Richard Biener <richard.guent...@gmail.com> wrote:
Btw, I can't find wording in the standards that nullptr + 1 is
invoking undefined behavior,
that is, that pointer arithmetic is only allowed on pointers pointing
to a valid object.
Any specific pointers?
All of 5.7 talks about pointers pointing to objects (except when adding
0).
Thanks all for the response. Working on a patch introducing infrastructure
for this right now but implementing this we'd need to make sure to not
hoist pointer arithmetic into blocks that might otherwise not be executed.
Like
if (p != 0)
{
q = p + 1;
foo (q);
}
may not be optimized to
q = p + 1;
if (p != 0)
foo (q);
because then we'd elide the p != 0 check. I'm implementing the infrastructure
to assume y != 0 after a stmt like z = x / y; where we'd already avoid
such hoisting
because it may trap at runtime.
Similar "issues" would be exposed when hoisting undefined overflow
stmts and we'd
derive ranges for their operands.
So I'm not entirely sure it's worth the likely trouble.
The opposite direction may be both easier and safer, even if it won't
handle everything:
P p+ N is nonnull if P or N is known to be nonnull
(and something similar for &p->field and others)
--
Marc Glisse