This is an archive of the discontinued LLVM Phabricator instance.

[LIR] Teach LIR to avoid extending the BE count prior to adding one to it when safe.
ClosedPublic

Authored by chandlerc on Jul 21 2017, 8:13 PM.

Details

Summary

Very often the BE count is the trip count minus one, and the plus one
here should fold with that minus one. But because the BE count might in
theory be UINT_MAX or some such, adding one before we extend could in
some cases wrap to zero and break when we scale things.

This patch checks to see if it would be safe to add one because the
specific case that would cause this is guarded for prior to entering the
preheader. This should handle essentially all of the common loop idioms
coming out of C/C++ code once canonicalized by LLVM.

Before this patch, both forms of loop in the added test cases ended up
subtracting one from the size, extending it, scaling it up by 8 and then
adding 8 back onto it. This is really silly, and it turns out made it
all the way into generated code very often, so this is a surprisingly
important cleanup to do.

Many thanks to Sanjoy for showing me how to do this with scev.

Event Timeline

chandlerc created this revision.Jul 21 2017, 8:13 PM
sanjoy accepted this revision.Jul 21 2017, 8:25 PM

lgtm!

lib/Transforms/Scalar/LoopIdiomRecognize.cpp
790

Not sure what the S at the end of NumBytes is for.

This revision is now accepted and ready to land.Jul 21 2017, 8:25 PM

Wheee!

lib/Transforms/Scalar/LoopIdiomRecognize.cpp
790

It was there in the original code. I'm guessing it means "SCEV"? Anyways, happy to nuke it if you want (in a subsequent patch).

This revision was automatically updated to reflect the committed changes.