[all-commits] [llvm/llvm-project] 25067f: [LoopIdiomRecognize] Teach detectShiftUntilZeroIdi...

Craig Topper via All-commits all-commits at lists.llvm.org
Mon Dec 14 14:25:52 PST 2020


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 25067f179f33ba1b764ac7a7d83385c8fd73801f
      https://github.com/llvm/llvm-project/commit/25067f179f33ba1b764ac7a7d83385c8fd73801f
  Author: Craig Topper <craig.topper at sifive.com>
  Date:   2020-12-14 (Mon, 14 Dec 2020)

  Changed paths:
    M llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp
    M llvm/test/Transforms/LoopIdiom/X86/ctlz.ll
    M llvm/test/Transforms/LoopIdiom/X86/cttz.ll

  Log Message:
  -----------
  [LoopIdiomRecognize] Teach detectShiftUntilZeroIdiom to recognize loops where the counter is decrementing.

This adds support for loops like

unsigned clz(unsigned x) {
    unsigned w = sizeof (x) * CHAR_BIT;
    while (x) {
        w--;
        x >>= 1;
    }

    return w;
}

and

unsigned clz(unsigned x) {
    unsigned w = sizeof (x) * CHAR_BIT - 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.

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




More information about the All-commits mailing list