[PATCH] D99427: [LoopIdiomRecognize] Teach CTLZ/CTTZ idiom recognition to handle not being able to find a pre-loop check for the input being 0.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 26 10:29:52 PDT 2021


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

To find the zero check we look for a branch whose condition is
a zero compare with the same Value* passed to the phi for the loop.
InstCombine knows some tricks that can break this. The one I observed
is when the input to the loop is a llvm.abs intrinsic. InstCombine
rewrites icmp eq (llvm.abs X), 0 to icmp eq X, 0 since the absolute
value doesn't change 0 or produce 0 for any other inputs. I believe
the original motivating case for this tranform featured an absolute
value before the loop so we should make this work.

I didn't want to teach this pass to also look through llvm.abs as
it seemed like we'd inevitably need to teach it more things to look
through. So instead I've inserted a new zero check for this case to
select the count value the loop should produce if there was no
zero check before the loop. This new zero check will hopefully be
optimized by InstCombine and eventually determined to be unneeded
and removed. If not, it's probably still cheaper than the loop.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99427

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99427.333586.patch
Type: text/x-patch
Size: 11047 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210326/5f1e7dd8/attachment.bin>


More information about the llvm-commits mailing list