[llvm] [KnownBits] Add KnownBits::add and KnownBits::sub helper wrappers. (PR #99468)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 03:45:29 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Simon Pilgrim (RKSimon)

<details>
<summary>Changes</summary>

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
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);

``````````

</details>


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


More information about the llvm-commits mailing list