Skip to content

[KnownBits] Add KnownBits::add and KnownBits::sub helper wrappers. #99468

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 1 commit into from
Aug 12, 2024

Conversation

RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented Jul 18, 2024

No description provided.

@RKSimon RKSimon requested review from jayfoad and goldsteinn July 18, 2024 10:44
@RKSimon RKSimon requested a review from nikic as a code owner July 18, 2024 10:44
@llvmbot llvmbot added backend:X86 llvm:support llvm:analysis Includes value tracking, cost tables and constant folding labels Jul 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 18, 2024

@llvm/pr-subscribers-llvm-globalisel
@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-backend-arm
@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-support

Author: Simon Pilgrim (RKSimon)

Changes

Assumes NSW = NUW = false

Simplifies uses in callbacks by avoiding the need for a custom KnownBits::computeForAddSub wrapper


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

4 Files Affected:

  • (modified) llvm/include/llvm/Support/KnownBits.h (+13)
  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+4-14)
  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+7-8)
  • (modified) llvm/unittests/Support/KnownBitsTest.cpp (+8)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 7ed3d525bd8fb..c49b0395b268b 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -329,6 +329,19 @@ struct KnownBits {
   static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
                                        const KnownBits &Borrow);
 
+  /// Compute knownbits resulting from addition of LHS and RHS.
+  /// NSW and NUW flags are assumed to be false.
+  static KnownBits add(const KnownBits& LHS, const KnownBits& RHS) {
+    return computeForAddSub(/*Add=*/true, /*NSW=*/false, /*NUW=*/false, LHS,
+                            RHS);
+  }
+  /// Compute knownbits resulting from subtraction of LHS and RHS.
+  /// NSW and NUW flags are assumed to be false.
+  static KnownBits sub(const KnownBits& LHS, const KnownBits& RHS) {
+    return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
+                            RHS);
+  }
+
   /// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
   static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS);
 
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 535a248a5f1a2..64386f6c11c08 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1800,13 +1800,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
       case Intrinsic::x86_ssse3_phadd_w_128:
       case Intrinsic::x86_avx2_phadd_d:
       case Intrinsic::x86_avx2_phadd_w: {
-        Known = computeKnownBitsForHorizontalOperation(
-            I, DemandedElts, Depth, Q,
-            [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
-              return KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
-                                                 /*NUW=*/false, KnownLHS,
-                                                 KnownRHS);
-            });
+        Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+                                                       Q, KnownBits::add);
         break;
       }
       case Intrinsic::x86_ssse3_phadd_sw_128:
@@ -1819,13 +1814,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
       case Intrinsic::x86_ssse3_phsub_w_128:
       case Intrinsic::x86_avx2_phsub_d:
       case Intrinsic::x86_avx2_phsub_w: {
-        Known = computeKnownBitsForHorizontalOperation(
-            I, DemandedElts, Depth, Q,
-            [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
-              return KnownBits::computeForAddSub(/*Add=*/false, /*NSW=*/false,
-                                                 /*NUW=*/false, KnownLHS,
-                                                 KnownRHS);
-            });
+        Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+                                                       Q, KnownBits::sub);
         break;
       }
       case Intrinsic::x86_ssse3_phsub_sw_128:
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 881e06e5f78b4..16d32d5fe040c 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -37511,15 +37511,14 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
     }
     break;
   }
-  case X86ISD::HADD:
+  case X86ISD::HADD: {
+    Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+                                                   KnownBits::add);
+    break;
+  }
   case X86ISD::HSUB: {
-    Known = computeKnownBitsForHorizontalOperation(
-        Op, DemandedElts, Depth, DAG,
-        [Opc](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
-          return KnownBits::computeForAddSub(
-              /*Add=*/Opc == X86ISD::HADD, /*NSW=*/false, /*NUW=*/false,
-              KnownLHS, KnownRHS);
-        });
+    Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+                                                   KnownBits::sub);
     break;
   }
   case ISD::INTRINSIC_WO_CHAIN: {
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 84882550117d8..9d2fcf77b29cc 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -300,6 +300,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
         return Known1 ^ Known2;
       },
       [](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
+  testBinaryOpExhaustive(
+      "add", KnownBits::add,
+      [](const APInt &N1, const APInt &N2) { return N1 + N2; },
+      /*CheckOptimality=*/false);
+  testBinaryOpExhaustive(
+      "sub", KnownBits::sub,
+      [](const APInt &N1, const APInt &N2) { return N1 - N2; },
+      /*CheckOptimality=*/false);
   testBinaryOpExhaustive("umax", KnownBits::umax, APIntOps::umax);
   testBinaryOpExhaustive("umin", KnownBits::umin, APIntOps::umin);
   testBinaryOpExhaustive("smax", KnownBits::smax, APIntOps::smax);

@llvmbot
Copy link
Member

llvmbot commented Jul 18, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Simon Pilgrim (RKSimon)

Changes

Assumes NSW = NUW = false

Simplifies uses in callbacks by avoiding the need for a custom KnownBits::computeForAddSub wrapper


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

4 Files Affected:

  • (modified) llvm/include/llvm/Support/KnownBits.h (+13)
  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+4-14)
  • (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+7-8)
  • (modified) llvm/unittests/Support/KnownBitsTest.cpp (+8)
diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 7ed3d525bd8fb..c49b0395b268b 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -329,6 +329,19 @@ struct KnownBits {
   static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
                                        const KnownBits &Borrow);
 
+  /// Compute knownbits resulting from addition of LHS and RHS.
+  /// NSW and NUW flags are assumed to be false.
+  static KnownBits add(const KnownBits& LHS, const KnownBits& RHS) {
+    return computeForAddSub(/*Add=*/true, /*NSW=*/false, /*NUW=*/false, LHS,
+                            RHS);
+  }
+  /// Compute knownbits resulting from subtraction of LHS and RHS.
+  /// NSW and NUW flags are assumed to be false.
+  static KnownBits sub(const KnownBits& LHS, const KnownBits& RHS) {
+    return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
+                            RHS);
+  }
+
   /// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
   static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS);
 
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 535a248a5f1a2..64386f6c11c08 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1800,13 +1800,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
       case Intrinsic::x86_ssse3_phadd_w_128:
       case Intrinsic::x86_avx2_phadd_d:
       case Intrinsic::x86_avx2_phadd_w: {
-        Known = computeKnownBitsForHorizontalOperation(
-            I, DemandedElts, Depth, Q,
-            [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
-              return KnownBits::computeForAddSub(/*Add=*/true, /*NSW=*/false,
-                                                 /*NUW=*/false, KnownLHS,
-                                                 KnownRHS);
-            });
+        Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+                                                       Q, KnownBits::add);
         break;
       }
       case Intrinsic::x86_ssse3_phadd_sw_128:
@@ -1819,13 +1814,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
       case Intrinsic::x86_ssse3_phsub_w_128:
       case Intrinsic::x86_avx2_phsub_d:
       case Intrinsic::x86_avx2_phsub_w: {
-        Known = computeKnownBitsForHorizontalOperation(
-            I, DemandedElts, Depth, Q,
-            [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
-              return KnownBits::computeForAddSub(/*Add=*/false, /*NSW=*/false,
-                                                 /*NUW=*/false, KnownLHS,
-                                                 KnownRHS);
-            });
+        Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
+                                                       Q, KnownBits::sub);
         break;
       }
       case Intrinsic::x86_ssse3_phsub_sw_128:
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 881e06e5f78b4..16d32d5fe040c 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -37511,15 +37511,14 @@ void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
     }
     break;
   }
-  case X86ISD::HADD:
+  case X86ISD::HADD: {
+    Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+                                                   KnownBits::add);
+    break;
+  }
   case X86ISD::HSUB: {
-    Known = computeKnownBitsForHorizontalOperation(
-        Op, DemandedElts, Depth, DAG,
-        [Opc](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
-          return KnownBits::computeForAddSub(
-              /*Add=*/Opc == X86ISD::HADD, /*NSW=*/false, /*NUW=*/false,
-              KnownLHS, KnownRHS);
-        });
+    Known = computeKnownBitsForHorizontalOperation(Op, DemandedElts, Depth, DAG,
+                                                   KnownBits::sub);
     break;
   }
   case ISD::INTRINSIC_WO_CHAIN: {
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 84882550117d8..9d2fcf77b29cc 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -300,6 +300,14 @@ TEST(KnownBitsTest, BinaryExhaustive) {
         return Known1 ^ Known2;
       },
       [](const APInt &N1, const APInt &N2) { return N1 ^ N2; });
+  testBinaryOpExhaustive(
+      "add", KnownBits::add,
+      [](const APInt &N1, const APInt &N2) { return N1 + N2; },
+      /*CheckOptimality=*/false);
+  testBinaryOpExhaustive(
+      "sub", KnownBits::sub,
+      [](const APInt &N1, const APInt &N2) { return N1 - N2; },
+      /*CheckOptimality=*/false);
   testBinaryOpExhaustive("umax", KnownBits::umax, APIntOps::umax);
   testBinaryOpExhaustive("umin", KnownBits::umin, APIntOps::umin);
   testBinaryOpExhaustive("smax", KnownBits::smax, APIntOps::smax);

Copy link

github-actions bot commented Jul 18, 2024

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

@@ -329,6 +329,19 @@ struct KnownBits {
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS,
const KnownBits &Borrow);

/// Compute knownbits resulting from addition of LHS and RHS.
/// NSW and NUW flags are assumed to be false.
static KnownBits add(const KnownBits& LHS, const KnownBits& RHS) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not call it operator+?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mainly because I loathe operators for this kind of stuff :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Well we already use them for &, | and ^...

Copy link
Contributor

Choose a reason for hiding this comment

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

*cough* and we shouldn't *cough*

@goldsteinn
Copy link
Contributor

I'm opposed to no option for NUW/NSW. I think this will probably become the defacto API and think this basically encourages people, especially new contributors, to forgetting/ignoring the flags.

Can you add them as default false params?

@RKSimon
Copy link
Collaborator Author

RKSimon commented Aug 9, 2024

I've added NSW/NUW arguments that default to false - although this means we can't easily use these in llvm::function_ref callbacks anymore

@goldsteinn
Copy link
Contributor

I've added NSW/NUW arguments that default to false - although this means we can't easily use these in llvm::function_ref callbacks anymore

You could add a wrapper addNoWrap and subNoWrap for the callback case.

@goldsteinn
Copy link
Contributor

LGTM

@RKSimon RKSimon merged commit 11ba72e into llvm:main Aug 12, 2024
12 checks passed
@RKSimon RKSimon deleted the knownbits_addsub branch August 12, 2024 09:21
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.

5 participants