[llvm] [DAG] visitCTPOP - if only the upper half of the ctpop operand is zero then see if its profitable to only count the lower half. (PR #80473)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 5 02:19:02 PST 2024


================
@@ -11142,11 +11142,29 @@ SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
 SDValue DAGCombiner::visitCTPOP(SDNode *N) {
   SDValue N0 = N->getOperand(0);
   EVT VT = N->getValueType(0);
+  unsigned NumBits = VT.getScalarSizeInBits();
   SDLoc DL(N);
 
   // fold (ctpop c1) -> c2
   if (SDValue C = DAG.FoldConstantArithmetic(ISD::CTPOP, DL, VT, {N0}))
     return C;
+
+  // If the upper bits are known to be zero, then see if its profitable to
+  // only count the lower bits.
+  if (VT.isScalarInteger() && NumBits > 8 && (NumBits & 1) == 0) {
+    EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), NumBits / 2);
+    if (TLI.isTruncateFree(N0, HalfVT) && TLI.isZExtFree(HalfVT, VT) &&
+        TLI.isTypeDesirableForOp(ISD::CTPOP, HalfVT) &&
+        hasOperation(ISD::CTPOP, HalfVT)) {
----------------
arsenm wrote:

Check these two conditions first?

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


More information about the llvm-commits mailing list