[PATCH] D92745: [LoopIdiomRecognize] Teach detectShiftUntilZeroIdiom to recognize loops where the counter is decrementing.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 7 00:27:11 PST 2020


craig.topper created this revision.
craig.topper added reviewers: rengolin, joerg, lebedev.ri.
Herald added a subscriber: hiraditya.
craig.topper requested review of this revision.
Herald added a project: LLVM.

This adds support for loops like

unsigned clz(unsigned x) {

  unsigned w = sizeof (x) * 8;
  while (x) {
      w--;
      x >>= 1;
  }
  
  return w;

}

and

unsigned clz(unsigned x) {

  unsigned w = sizeof (x) * 8 - 1;
  while (x >>= 1) {
      w--;
  }
  
  return w;

}

To support these we look for add x, -1 as well as add x, 1 that
we already matched.  If the value was -1 we need to subtract from
the initial counter value instead of adding to it.

Fixes PR48404.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92745

Files:
  llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
  llvm/test/Transforms/LoopIdiom/X86/ctlz.ll
  llvm/test/Transforms/LoopIdiom/X86/cttz.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92745.309819.patch
Type: text/x-patch
Size: 7782 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201207/6132f294/attachment-0001.bin>


More information about the llvm-commits mailing list