]> granicus.if.org Git - llvm/commit
[LIR] Teach LIR to avoid extending the BE count prior to adding one to
authorChandler Carruth <chandlerc@gmail.com>
Tue, 25 Jul 2017 10:48:32 +0000 (10:48 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Tue, 25 Jul 2017 10:48:32 +0000 (10:48 +0000)
commit2dcaf78ab6dc9001e294ffa2467a33a574401e5c
treeda7dd38b9fa55daac07e3c56800fb9190e341454
parent0000a717e90e6de738f8ab1d7eb675df30f8568f
[LIR] Teach LIR to avoid extending the BE count prior to adding one to
it when safe.

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.

Differential Revision: https://reviews.llvm.org/D35758

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308968 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/Scalar/LoopIdiomRecognize.cpp
test/Transforms/LoopIdiom/basic.ll