[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
Sun Mar 28 12:24:46 PDT 2021


craig.topper added inline comments.


================
Comment at: llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp:1781-1784
+    // If we didn't find the zero check, we need to assume we'll execute one
+    // iteration of the loop when the input is zero. If the input is zero, we
+    // need to select the initial value incremented/decremented by the count
+    // instruction's immediate operand.
----------------
spatel wrote:
> It's not clear to me what cost model metrics we're using. Does this change the equation?
I think the cost model is if the loop just counts bits and doesn't do anything else, always expand to ctlz since the loop will get deleted. If the loop has other instructions, check if ctlz cost is TCC_Basic (for X86 I think this is is lzcnt or tzcnt intructions enabled). If is then do the transform, the loop will stay but it's trip count will be determine by the ctlz.

If the loop is going to get removed, then the extra select I'm inserting probably doesn't matter. If the loop won't get removed, I'm not sure if the extra select and icmp should be costed. In many cases there probably is a real zero check and other optimizations will remove the extra instructions. I think the original case just counted bits, but it's been a few years since I looked at it. So if we're concerned I can probably handle just the case that the loop will be removed.



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99427/new/

https://reviews.llvm.org/D99427



More information about the llvm-commits mailing list