[llvm] [CodeGen] Optimize ctpop expansion using computeKnownBits (PR #221671)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 03:55:52 PDT 2026
================
@@ -10811,6 +10798,25 @@ 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, allowing us to shift the
+ // active bits down if necessary to fit into smaller specialized expansions.
+ KnownBits Known = DAG.computeKnownBits(Op);
+ unsigned LZ = Known.countMinLeadingZeros();
+ unsigned TZ = Known.countMinTrailingZeros();
+ unsigned ShiftedActiveBits = Known.getBitWidth() - (LZ + TZ);
+
+ // If the active bits are not at the low end, shift them down
+ if (ShiftedActiveBits < Len && TZ > 0) {
+ Op = DAG.getNode(ISD::SRL, dl, VT, Op,
+ DAG.getShiftAmountConstant(TZ, VT, dl));
+ }
----------------
milkHongYe wrote:
Done
https://github.com/llvm/llvm-project/pull/221671
More information about the llvm-commits
mailing list