[llvm] [TargetLowering] Split CTTZ_ELTS when its step vector requires splitting (PR #197623)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu May 14 19:37:30 PDT 2026


================
@@ -13090,6 +13090,28 @@ SDValue TargetLowering::expandCttzElts(SDNode *Node, SelectionDAG &DAG) const {
   bool ZeroIsPoison = Node->getOpcode() == ISD::CTTZ_ELTS_ZERO_POISON;
   auto [Mask, StepVec] =
       getLegalMaskAndStepVector(Node->getOperand(0), ZeroIsPoison, DL, DAG);
+
+  // No legal step vector: split mask in halves and recombine results.
+  // Lo uses the non-poison CTTZ_ELTS so its result is well-defined (== |Lo|
+  // when no active lane), allowing the SETNE comparison.
+  // Result: (ResLo != |Lo|) ? ResLo : (|Lo| + ResHi)
+  if (!StepVec) {
+    EVT ResVT = Node->getValueType(0);
+    auto [MaskLo, MaskHi] = DAG.SplitVector(Node->getOperand(0), DL);
+    SDValue LoNumElts = DAG.getElementCount(
+        DL, ResVT, MaskLo.getValueType().getVectorElementCount());
+    SDValue ResLo = DAG.getNode(ISD::CTTZ_ELTS, DL, ResVT, MaskLo);
+    SDValue ResHi = DAG.getNode(Node->getOpcode(), DL, ResVT, MaskHi);
+    SDValue ResLoNotNumElts = DAG.getSetCC(
+        DL, getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ResVT),
+        ResLo, LoNumElts, ISD::SETNE);
+    // |Lo| + ResHi <= total lane count, fits in ResVT; both operands >= 0.
+    SDValue Sum =
+        DAG.getNode(ISD::ADD, DL, ResVT, LoNumElts, ResHi,
+                    SDNodeFlags::NoUnsignedWrap | SDNodeFlags::NoSignedWrap);
----------------
topperc wrote:

I guess it follows from the "The result is poison if the return type isn't wide enough to hold the maximum number of elements in the input vector."?

Although I think that only guarantees no unsigned wrap.

https://github.com/llvm/llvm-project/pull/197623


More information about the llvm-commits mailing list