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

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 01:06:28 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 115c92d8f..e4119d2b7 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -12246,13 +12246,14 @@ void SelectionDAGISel::LowerArguments(const Function &F) {
         }
         if (VT.isInteger()) {
           if (auto CR = Arg.getRange()) {
-            if (!CR->isFullSet() && !CR->isEmptySet() && !CR->isUpperWrapped()) {
+            if (!CR->isFullSet() && !CR->isEmptySet() &&
+                !CR->isUpperWrapped()) {
               APInt Hi = CR->getUnsignedMax();
               unsigned Bits = std::max(Hi.getActiveBits(),
-                                      (unsigned)IntegerType::MIN_INT_BITS);
+                                       (unsigned)IntegerType::MIN_INT_BITS);
               EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), Bits);
               OutVal = DAG.getNode(ISD::AssertZext, dl, OutVal.getValueType(),
-                                  OutVal, DAG.getValueType(SmallVT));
+                                   OutVal, DAG.getValueType(SmallVT));
             }
           }
         }
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index e1fea56f1..1f1dcd031 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -10798,18 +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;
   } else {
     EffectiveLen = Len;
   }
-  
+
   assert(VT.isInteger() && "CTPOP not implemented for this type.");
 
   // TODO: Add support for irregular type lengths.

``````````

</details>


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


More information about the llvm-commits mailing list