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

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 02:58:45 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;
----------------
RKSimon wrote:

alignTo?

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


More information about the llvm-commits mailing list