[PATCH] D47348: [LoopIdiomRecognize] Only convert loops to ctlz if we can prove that the input is non-negative.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 24 16:00:54 PDT 2018


craig.topper created this revision.
craig.topper added reviewers: spatel, hfinkel, efriedma.
Herald added a reviewer: javed.absar.

Loop idiom recognize tries to convert loops like

  int foo(int x) {
    int cnt = 0;
    while (x) {
      x >>= 1;
      ++cnt;
    }
    return cnt;
  }

into calls to ctlz, but if x is initially negative this loop should be infinite.

It happens that the cases that motivated this change have an absolute value of x before the loop. So this patch restricts the transform to cases where we know x is positive. Note: We are relying on the absolute value of INT_MIN to be undefined so we can assume that the result is always positive.

Fixes PR37479


https://reviews.llvm.org/D47348

Files:
  test/Transforms/LoopIdiom/ARM/ctlz.ll
  test/Transforms/LoopIdiom/X86/ctlz.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47348.148496.patch
Type: text/x-patch
Size: 6244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180524/b46979ed/attachment.bin>


More information about the llvm-commits mailing list