SCEV determines that loops with trip count >=2^32 have a trip multiple
of 1 to guard against huge multiples. This patch stregthens this to
instead find the greatest power of 2 divisor that is less than the
threshold.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
We could technically find the largest multiple under the threshold that is not limited to powers of 2, but that is much more computation heavy and the benefits are questionable.
An interesting case where this is beneficial is in the LoopUnroll test. Before, we returned a trip multiple of 1, but this change returns a trip multiple of 2^32. Since its trying to unroll by a factor of 8, and 2^32 is divisible by eight, LoopUnroll knows that the loop epilogue is not required anymore.
llvm/lib/Analysis/ScalarEvolution.cpp | ||
---|---|---|
8248 | If the Result is zero, countTrailingZeros() will return the bitwidth of the integer, I think, which doesn't match the comment. I don't think the wrapping thing can actually happen since D110587 was merged, though, so we shouldn't see a trip count of zero. So maybe update the comment while you're here. (Maybe it's theoretically possible that some combination of folding could end up folding a poison trip count to zero. In that case, though, it wouldn't really matter what multiple we return.) |
llvm/lib/Analysis/ScalarEvolution.cpp | ||
---|---|---|
8248 | You're right, TripCount should never be zero. I added an assertion for this and removed that section of the comment. |
If the Result is zero, countTrailingZeros() will return the bitwidth of the integer, I think, which doesn't match the comment.
I don't think the wrapping thing can actually happen since D110587 was merged, though, so we shouldn't see a trip count of zero. So maybe update the comment while you're here.
(Maybe it's theoretically possible that some combination of folding could end up folding a poison trip count to zero. In that case, though, it wouldn't really matter what multiple we return.)