PR3609

Mark Mitchell mark@codesourcery.com
Thu Aug 9 14:23:00 GMT 2001


> But casts are uninteresting too. Simply doing strlen((char
> *)(((long)"foo") + 100)) is as bad as doing strlen("foo" + 100).

In what sense?

Let's be clear about what we're talking about; I don't think I've done
a good job yet.  First, do you think that code has undefined behavior?
Or implementation defined behavior?  I think it has implementation-defined
behavior, which is generally supposed to mean "works in a way that makes
sense for the target".

Second, assuming I'm right that it's implementation-defined, what
should we do?  One possibility is to generate:

  (offset > 3) ? strlen (offset + 3) : (3 - offset)

Is there a fencepost error there?  Need to check, but you get the idea.
That still gets most of the win (elimination of the function call,
and the containing loop).  I'd be amazed if the difference between this
and just `3 - offset' was big enough to matter in any real code.

I'd also like to know if you have any evidence that the time saved
by this optimization over the life of the compiler will be greater
than the time it takes us to debate the issue.  I can believe
either answer, but without numbers I'm suspicious that this
optimization isn't worth it.  I'd also like to know what other
compilers do.  Do you know?

If, however, this is really *undefined* behavior, then I agree; fix
the kernel.

For 3.0.1, though, we are going to lose this optimization if that fixes
the problem because I think that's the right tradeoff.

I'm happy to be overruled by the SC, though, of course.

--
Mark Mitchell                   mark@codesourcery.com
CodeSourcery, LLC               http://www.codesourcery.com



More information about the Gcc-patches mailing list