[llvm] [WIP][DAG] Improve the knownbits of CTPOP/LZ/TZ based off the min/max counts of the input value (PR #107085)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 3 05:15:21 PDT 2024


================
@@ -3747,26 +3747,24 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
   case ISD::CTTZ:
   case ISD::CTTZ_ZERO_UNDEF: {
     Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-    // If we have a known 1, its position is our upper bound.
-    unsigned PossibleTZ = Known2.countMaxTrailingZeros();
-    unsigned LowBits = llvm::bit_width(PossibleTZ);
-    Known.Zero.setBitsFrom(LowBits);
+    APInt LowerCount(BitWidth, Known2.countMinTrailingZeros());
+    APInt UpperCount(BitWidth, 1 + Known2.countMaxTrailingZeros());
+    Known = ConstantRange(LowerCount, UpperCount).toKnownBits();
----------------
nikic wrote:

```suggestion
    APInt LowerCount(BitWidth, Known2.countMinTrailingZeros());
    APInt UpperCount(BitWidth, Known2.countMaxTrailingZeros());
    Known = ConstantRange::getNonEmpty(LowerCount, UpperCount + 1).toKnownBits();
```


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


More information about the llvm-commits mailing list