[llvm] [CodeGen] Optimize ctpop expansion using computeKnownBits (PR #221671)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 04:38:42 PDT 2026


================
@@ -10798,6 +10798,18 @@ SDValue TargetLowering::expandCTPOP(SDNode *Node, SelectionDAG &DAG) const {
   EVT ShVT = getShiftAmountTy(VT, DAG.getDataLayout());
   SDValue Op = Node->getOperand(0);
   unsigned Len = VT.getScalarSizeInBits();
+
+  // Compute effective bit width from known bits
+  KnownBits Known = DAG.computeKnownBits(Op);
+  unsigned EffectiveLen = Known.countMaxActiveBits();
+
+  // Round up to 8-bit boundary for byte-oriented SWAR algorithm
+  if (EffectiveLen > 0 && EffectiveLen < Len) {
+    EffectiveLen = (EffectiveLen + 7) & ~7u;
----------------
milkHongYe wrote:

I've updated the code to use TZ/LZ  from `computeKnownBits`, following a similar approach to X86.


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


More information about the llvm-commits mailing list