Skip to content

[SelectionDAG]: Deduce KnownNeverZero from SMIN and SMAX #85722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2024

Conversation

AZero13
Copy link
Contributor

@AZero13 AZero13 commented Mar 19, 2024

No description provided.

@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Mar 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 19, 2024

@llvm/pr-subscribers-llvm-regalloc
@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-selectiondag

Author: AtariDreams (AtariDreams)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/85722.diff

1 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+20-3)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 6f6ed4bd45027b..81d2df1db565ea 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4007,8 +4007,6 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
 
     // For SMAX, if CstLow is non-negative we know the result will be
     // non-negative and thus all sign bits are 0.
-    // TODO: There's an equivalent of this for smin with negative constant for
-    // known ones.
     if (IsMax && CstLow) {
       const APInt &ValueLow = CstLow->getAPIntValue();
       if (ValueLow.isNonNegative()) {
@@ -4017,6 +4015,16 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
       }
     }
 
+    // For SMAX, if CstHigh is negative we know the result will be
+    // negative and thus all sign bits are 1.
+    if (!IsMax && CstHigh) {
+      const APInt &ValueHigh = CstHigh->getAPIntValue();
+      if (ValueHigh.isNegative()) {
+        unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
+        Known.One.setHighBits(std::min(SignBits, ValueHigh.getNumSignBits()));
+      }
+    }
+
     break;
   }
   case ISD::UINT_TO_FP: {
@@ -5360,10 +5368,19 @@ bool SelectionDAG::isKnownNeverZero(SDValue Op, unsigned Depth) const {
     return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
            isKnownNeverZero(Op.getOperand(0), Depth + 1);
 
-    // TODO for smin/smax: If either operand is known negative/positive
+    // For smin/smax: If either operand is known negative/positive
     // respectively we don't need the other to be known at all.
   case ISD::SMAX:
+    if (computeKnownBits(Op.getOperand(0), Depth + 1).isStrictlyPositive() ||
+        computeKnownBits(Op.getOperand(1), Depth + 1).isStrictlyPositive())
+      return true;
+    return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
+           isKnownNeverZero(Op.getOperand(0), Depth + 1);
   case ISD::SMIN:
+    if (computeKnownBits(Op.getOperand(0), Depth + 1).isNegative() ||
+        computeKnownBits(Op.getOperand(1), Depth + 1).isNegative())
+      return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
+             isKnownNeverZero(Op.getOperand(0), Depth + 1);
   case ISD::UMIN:
     return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
            isKnownNeverZero(Op.getOperand(0), Depth + 1);

@AZero13 AZero13 force-pushed the ARC-Migrate branch 3 times, most recently from e9514a3 to 4fb62d8 Compare March 19, 2024 03:02
@AZero13 AZero13 force-pushed the ARC-Migrate branch 2 times, most recently from 1a5c93e to c5bdbd2 Compare March 19, 2024 03:54
@jayfoad
Copy link
Contributor

jayfoad commented Mar 19, 2024

Maybe split this into two patches, one for computeKnownBits and one for isKnownNeverZero? Then it is easier to see what is being tested where.

@AZero13 AZero13 force-pushed the ARC-Migrate branch 5 times, most recently from f6933e1 to dd38f1a Compare March 20, 2024 21:46
@AZero13 AZero13 changed the title [SelectionDAG]: Deduce known bits from SMIN and SMAX [SelectionDAG]: Deduce KnownNeverZero from SMIN and SMAX Mar 21, 2024
Copy link
Contributor

@jayfoad jayfoad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM but I haven't been involved too much in the review so I'll leave it for someone else to approve.

Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need better vector tests - the vector CTTZ expansion won't be making use of is never known zero

@AZero13
Copy link
Contributor Author

AZero13 commented Mar 22, 2024

You need better vector tests - the vector CTTZ expansion won't be making use of is never known zero

So, what would then, if I may ask? Just to give me an idea so I could make a test for it

@RKSimon
Copy link
Collaborator

RKSimon commented Mar 22, 2024

simplifySetCCWithCTPOP appears to use isKnownNeverZero for the (ctpop x) eq/ne 1 --> (x & x-1) eq/ne 0 fold - maybe try that?

@AZero13
Copy link
Contributor Author

AZero13 commented Mar 22, 2024

@RKSimon Thank you!

@AZero13 AZero13 requested review from topperc and jayfoad March 22, 2024 21:54
Copy link

✅ With the latest revision this PR passed the Python code formatter.

Copy link

✅ With the latest revision this PR passed the C/C++ code formatter.

RKSimon added a commit that referenced this pull request Mar 24, 2024
Enabled vector coverage as well: i686+SSE2 and x64_64+AVX

Should improve test quality for #85722
Copy link
Collaborator

@RKSimon RKSimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please can you rebase - I've added better target coverage to known-never-zero.ll

@AZero13
Copy link
Contributor Author

AZero13 commented Mar 24, 2024

@RKSimon Done!

@AZero13
Copy link
Contributor Author

AZero13 commented Mar 24, 2024

@RKSimon Done!

@AZero13 AZero13 force-pushed the ARC-Migrate branch 2 times, most recently from 238c1e2 to 268d744 Compare March 25, 2024 02:15
@RKSimon RKSimon merged commit f5a067b into llvm:main Mar 25, 2024
@AZero13 AZero13 deleted the ARC-Migrate branch March 25, 2024 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants