[llvm-commits] [PATCH] Teach SCEV about <= comparisons for trip count computation

Tobias von Koch tobias.von.koch at gmail.com
Wed Aug 22 03:17:07 PDT 2012

Dear all,

SCEV currently has a weakness in the loop trip count computation code when
it comes to loops whose bounds are determined by a <= comparison.

Take the example from the attached test case:

int Proc_8 (int arg1)
  int result = 0;
  for (int i = arg1; i <= arg1 + 5; ++i)

  return result;

GCC is able to determine the trip count exactly and translates this whole
function into a single instruction (+ return) on PPC.

LLVM fails on two fronts:
- First of all, it can't deal with the <= comparison because the code to
handle this isn't there. It was added in an earlier version of SCEV years
ago but then reverted alongside a lot of other (buggy) code. The attached
patch fixes this.
- Secondly, even once the patch has been applied, LLVM can only derive a
smax() expression for the trip count of this loop. While this is a lot
better already, it's still not an exact number. I get the impression that
this has something to do with the 'nsw' flag on the original addition not
being propagated through the SCEV analysis, but I'm not entirely sure here.

The patch passes all regression tests and the nightly test suite. I do have
one worry about the line marked with a 'FIXME', though. Is it necessary to
check for overflow here? Can this be done using the same method as
in ScalarEvolution::getBECount, i.e. zero-extend to larger type, add,
compare results?

Thanks for your feedback!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120822/ec1b2d08/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: scev-less-than-equals-opt.patch
Type: application/octet-stream
Size: 6348 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120822/ec1b2d08/attachment.obj>

More information about the llvm-commits mailing list