[llvm] [llvm] Add KnownBits implementations for avgFloor and avgCeil (PR #86445)

Nhat Nguyen via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 23 20:48:53 PDT 2024


https://github.com/changkhothuychung updated https://github.com/llvm/llvm-project/pull/86445

>From e447f4c15ced2880f520d636ffc239d1368032b7 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 24 Mar 2024 13:54:21 -0400
Subject: [PATCH 01/14] initial attempt

---
 llvm/lib/Support/KnownBits.cpp | 40 ++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index d72355dab6f1d3..07c7ad0882387a 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -762,6 +762,46 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
+KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
+  // (C1 & C2) + (C1 ^ C2).ashr(1)
+  KnownBits andResult = LHS & RHS;
+  KnownBits xorResult = LHS ^ RHS;
+  xorResult.Zero.ashrInPlace(1);
+  xorResult.One.ashrInPlace(1);
+  return computeForSatAddSub(/*Add*/ true, /*Signed*/ true, andResult,
+                             xorResult);
+}
+
+KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
+  // (C1 & C2) + (C1 ^ C2).lshr(1)
+  KnownBits andResult = LHS & RHS;
+  KnownBits xorResult = LHS ^ RHS;
+  xorResult.Zero.lshrInPlace(1);
+  xorResult.One.lshrInPlace(1);
+  return computeForSatAddSub(/*Add*/ true, /*Signed*/ false, andResult,
+                             xorResult);
+}
+
+KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
+  // (C1 | C2) - (C1 ^ C2).ashr(1)
+  KnownBits andResult = LHS & RHS;
+  KnownBits xorResult = LHS ^ RHS;
+  xorResult.Zero.ashrInPlace(1);
+  xorResult.One.ashrInPlace(1);
+  return computeForSatAddSub(/*Add*/ false, /*Signed*/ true, andResult,
+                             xorResult);
+}
+
+KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
+  // (C1 | C2) - (C1 ^ C2).lshr(1)
+  KnownBits andResult = LHS & RHS;
+  KnownBits xorResult = LHS ^ RHS;
+  xorResult.Zero.lshrInPlace(1);
+  xorResult.One.lshrInPlace(1);
+  return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, andResult,
+                             xorResult);
+}
+
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
                          bool NoUndefSelfMultiply) {
   unsigned BitWidth = LHS.getBitWidth();

>From 8b6e8bbec1e782ef1da06bf2dcf69319c9e96813 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 24 Mar 2024 14:02:31 -0400
Subject: [PATCH 02/14] add definitions to header file

---
 llvm/include/llvm/Support/KnownBits.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 73cb01e0644a8d..575362dc18b0cc 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -354,6 +354,18 @@ struct KnownBits {
   /// Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
   static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS);
 
+  /// Compute knownbits resulting from (C1 & C2) + (C1 ^ C2).ashr(1)
+  static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS);
+
+  /// Compute knownbits resulting from (C1 & C2) + (C1 ^ C2).lshr(1)
+  static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS);
+
+  /// Compute knownbits resulting from (C1 & C2) - (C1 ^ C2).ashr(1)
+  static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS);
+
+  /// Compute knownbits resulting from (C1 & C2) - (C1 ^ C2).lshr(1)
+  static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
+
   /// Compute known bits resulting from multiplying LHS and RHS.
   static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
                        bool NoUndefSelfMultiply = false);

>From b0d81007840a354694e0238a0be96b13ce74b119 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Thu, 4 Apr 2024 21:10:27 -0400
Subject: [PATCH 03/14] fix comments

---
 llvm/include/llvm/Support/KnownBits.h         |  8 ++--
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 39 ++++++++++++++++---
 llvm/lib/Support/KnownBits.cpp                | 13 +++----
 3 files changed, 43 insertions(+), 17 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 575362dc18b0cc..93049d05eb0b5c 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -354,16 +354,16 @@ struct KnownBits {
   /// Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
   static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Compute knownbits resulting from (C1 & C2) + (C1 ^ C2).ashr(1)
+  /// Compute knownbits resulting from APIntOps::avgFloorS
   static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Compute knownbits resulting from (C1 & C2) + (C1 ^ C2).lshr(1)
+  /// Compute knownbits resulting from APIntOps::avgFloorU
   static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Compute knownbits resulting from (C1 & C2) - (C1 ^ C2).ashr(1)
+  /// Compute knownbits resulting from APIntOps::avgCelS
   static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Compute knownbits resulting from (C1 & C2) - (C1 ^ C2).lshr(1)
+  /// Compute knownbits resulting from APIntOps::avgCelU
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
   /// Compute known bits resulting from multiplying LHS and RHS.
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 1dd0fa49a460f8..f6af02ded36a2e 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3419,9 +3419,39 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
       Known = KnownBits::mulhs(Known, Known2);
     break;
   }
-  case ISD::AVGFLOORU:
-  case ISD::AVGCEILU:
-  case ISD::AVGFLOORS:
+  case ISD::AVGFLOORU: {
+    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
+    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
+    Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+    Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
+    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = KnownBits::avgFloorU(Known, Known2);
+    Known = Known.extractBits(BitWidth, 1);
+    break;
+  }
+  case ISD::AVGCEILU: {
+    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
+    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
+    Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+    Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
+    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = KnownBits::avgCeilU(Known, Known2);
+    Known = Known.extractBits(BitWidth, 1);
+    break;
+  }
+  case ISD::AVGFLOORS: {
+    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
+    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
+    Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+    Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
+    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = KnownBits::avgFloorS(Known, Known2);
+    Known = Known.extractBits(BitWidth, 1);
+    break;
+  }
   case ISD::AVGCEILS: {
     bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
     bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
@@ -3429,8 +3459,7 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
     Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
     Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
-    KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
-    Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
+    Known = KnownBits::avgCeilS(Known, Known2);
     Known = Known.extractBits(BitWidth, 1);
     break;
   }
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 07c7ad0882387a..1ad30a72166d9d 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -768,8 +768,7 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
-  return computeForSatAddSub(/*Add*/ true, /*Signed*/ true, andResult,
-                             xorResult);
+  return computeForAddSub(/*Add*/ true, /*Signed*/ true, andResult, xorResult);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
@@ -778,8 +777,7 @@ KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
-  return computeForSatAddSub(/*Add*/ true, /*Signed*/ false, andResult,
-                             xorResult);
+  return computeForAddSub(/*Add*/ true, /*Signed*/ false, andResult, xorResult);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
@@ -788,8 +786,7 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
-  return computeForSatAddSub(/*Add*/ false, /*Signed*/ true, andResult,
-                             xorResult);
+  return computeForAddSub(/*Add*/ false, /*Signed*/ true, andResult, xorResult);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
@@ -798,8 +795,8 @@ KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
-  return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, andResult,
-                             xorResult);
+  return computeForAddSub(/*Add*/ false, /*Signed*/ false, andResult,
+                          xorResult);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From ba43d228509f12093608ea3f75819b9893ea65d2 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Thu, 4 Apr 2024 21:19:58 -0400
Subject: [PATCH 04/14] add tests

---
 llvm/unittests/Support/KnownBitsTest.cpp | 36 ++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 027d6379af26b0..74432feea353ec 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -559,6 +559,42 @@ TEST(KnownBitsTest, BinaryExhaustive) {
       KnownBits::mulhu,
       [](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
       checkCorrectnessOnlyBinary);
+
+  testBinaryOpExhaustive(
+      [](const KnownBits &Known1, const KnownBits &Known2) {
+        return KnownBits::avgFloorS(Known1, Known2);
+      },
+      [](const APInt &N1, const APInt &N2) {
+        return APIntOps::avgFloorS(N1, N2);
+      },
+      checkCorrectnessOnlyBinary);
+
+  testBinaryOpExhaustive(
+      [](const KnownBits &Known1, const KnownBits &Known2) {
+        return KnownBits::avgFloorU(Known1, Known2);
+      },
+      [](const APInt &N1, const APInt &N2) {
+        return APIntOps::avgFloorU(N1, N2);
+      },
+      checkCorrectnessOnlyBinary);
+
+  testBinaryOpExhaustive(
+      [](const KnownBits &Known1, const KnownBits &Known2) {
+        return KnownBits::avgCeilU(Known1, Known2);
+      },
+      [](const APInt &N1, const APInt &N2) {
+        return APIntOps::avgCeilU(N1, N2);
+      },
+      checkCorrectnessOnlyBinary);
+
+  testBinaryOpExhaustive(
+      [](const KnownBits &Known1, const KnownBits &Known2) {
+        return KnownBits::avgCeilS(Known1, Known2);
+      },
+      [](const APInt &N1, const APInt &N2) {
+        return APIntOps::avgCeilS(N1, N2);
+      },
+      checkCorrectnessOnlyBinary);
 }
 
 TEST(KnownBitsTest, UnaryExhaustive) {

>From af3a08cb9362517a9f3126868f0567c8b7283ae2 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Thu, 4 Apr 2024 21:26:12 -0400
Subject: [PATCH 05/14] remove opcode check

---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 24 +++++++------------
 llvm/lib/Support/KnownBits.cpp                | 13 ++++++----
 2 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index f6af02ded36a2e..24edaf0d4af9b4 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3420,45 +3420,37 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
     break;
   }
   case ISD::AVGFLOORU: {
-    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
-    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
-    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = Known.zext(BitWidth + 1);
+    Known2 = Known2.zext(BitWidth + 1);
     Known = KnownBits::avgFloorU(Known, Known2);
     Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::AVGCEILU: {
-    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
-    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
-    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = Known.zext(BitWidth + 1);
+    Known2 = Known2.zext(BitWidth + 1);
     Known = KnownBits::avgCeilU(Known, Known2);
     Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::AVGFLOORS: {
-    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
-    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
-    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = Known.sext(BitWidth + 1);
+    Known2 = Known2.sext(BitWidth + 1);
     Known = KnownBits::avgFloorS(Known, Known2);
     Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::AVGCEILS: {
-    bool IsCeil = Opcode == ISD::AVGCEILU || Opcode == ISD::AVGCEILS;
-    bool IsSigned = Opcode == ISD::AVGFLOORS || Opcode == ISD::AVGCEILS;
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
-    Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+    Known = Known.sext(BitWidth + 1);
+    Known2 = Known2.sext(BitWidth + 1);
     Known = KnownBits::avgCeilS(Known, Known2);
     Known = Known.extractBits(BitWidth, 1);
     break;
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 1ad30a72166d9d..690bb750e90c3d 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -768,7 +768,8 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
-  return computeForAddSub(/*Add*/ true, /*Signed*/ true, andResult, xorResult);
+  return computeForAddSub(/*Add*/ true, /* NSW */ true, /* NUW */ false,
+                          andResult, xorResult);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
@@ -777,7 +778,8 @@ KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
-  return computeForAddSub(/*Add*/ true, /*Signed*/ false, andResult, xorResult);
+  return computeForAddSub(/*Add*/ true, /* NSW */ false, /* NUW */ true,
+                          andResult, xorResult);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
@@ -786,7 +788,8 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
-  return computeForAddSub(/*Add*/ false, /*Signed*/ true, andResult, xorResult);
+  return computeForAddSub(/*Add*/ false, /* NSW */ true, /* NUW */ false,
+                          andResult, xorResult);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
@@ -795,8 +798,8 @@ KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
-  return computeForAddSub(/*Add*/ false, /*Signed*/ false, andResult,
-                          xorResult);
+  return computeForAddSub(/*Add*/ false, /* NSW */ false, /* NUW */ true,
+                          andResult, xorResult);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From d5ded0ec89c29324af29f07bed3400f7e8aee780 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 6 Apr 2024 09:57:02 -0400
Subject: [PATCH 06/14] fix test

---
 .../lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 12 ------
 llvm/unittests/Support/KnownBitsTest.cpp      | 40 ++++---------------
 2 files changed, 8 insertions(+), 44 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 24edaf0d4af9b4..7884fcbfddad00 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -3422,37 +3422,25 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
   case ISD::AVGFLOORU: {
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = Known.zext(BitWidth + 1);
-    Known2 = Known2.zext(BitWidth + 1);
     Known = KnownBits::avgFloorU(Known, Known2);
-    Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::AVGCEILU: {
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = Known.zext(BitWidth + 1);
-    Known2 = Known2.zext(BitWidth + 1);
     Known = KnownBits::avgCeilU(Known, Known2);
-    Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::AVGFLOORS: {
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = Known.sext(BitWidth + 1);
-    Known2 = Known2.sext(BitWidth + 1);
     Known = KnownBits::avgFloorS(Known, Known2);
-    Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::AVGCEILS: {
     Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
     Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
-    Known = Known.sext(BitWidth + 1);
-    Known2 = Known2.sext(BitWidth + 1);
     Known = KnownBits::avgCeilS(Known, Known2);
-    Known = Known.extractBits(BitWidth, 1);
     break;
   }
   case ISD::SELECT:
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 74432feea353ec..77c786dc7c4892 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -560,41 +560,17 @@ TEST(KnownBitsTest, BinaryExhaustive) {
       [](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
       checkCorrectnessOnlyBinary);
 
-  testBinaryOpExhaustive(
-      [](const KnownBits &Known1, const KnownBits &Known2) {
-        return KnownBits::avgFloorS(Known1, Known2);
-      },
-      [](const APInt &N1, const APInt &N2) {
-        return APIntOps::avgFloorS(N1, N2);
-      },
-      checkCorrectnessOnlyBinary);
+  testBinaryOpExhaustive(KnownBits::avgFloorS, APIntOps::avgFloorS,
+                         checkCorrectnessOnlyBinary);
 
-  testBinaryOpExhaustive(
-      [](const KnownBits &Known1, const KnownBits &Known2) {
-        return KnownBits::avgFloorU(Known1, Known2);
-      },
-      [](const APInt &N1, const APInt &N2) {
-        return APIntOps::avgFloorU(N1, N2);
-      },
-      checkCorrectnessOnlyBinary);
+  testBinaryOpExhaustive(KnownBits::avgFloorU, APIntOps::avgFloorU,
+                         checkCorrectnessOnlyBinary);
 
-  testBinaryOpExhaustive(
-      [](const KnownBits &Known1, const KnownBits &Known2) {
-        return KnownBits::avgCeilU(Known1, Known2);
-      },
-      [](const APInt &N1, const APInt &N2) {
-        return APIntOps::avgCeilU(N1, N2);
-      },
-      checkCorrectnessOnlyBinary);
+  testBinaryOpExhaustive(KnownBits::avgCeilU, APIntOps::avgCeilU,
+                         checkCorrectnessOnlyBinary);
 
-  testBinaryOpExhaustive(
-      [](const KnownBits &Known1, const KnownBits &Known2) {
-        return KnownBits::avgCeilS(Known1, Known2);
-      },
-      [](const APInt &N1, const APInt &N2) {
-        return APIntOps::avgCeilS(N1, N2);
-      },
-      checkCorrectnessOnlyBinary);
+  testBinaryOpExhaustive(KnownBits::avgCeilS, APIntOps::avgCeilS,
+                         checkCorrectnessOnlyBinary);
 }
 
 TEST(KnownBitsTest, UnaryExhaustive) {

>From e9c8c6c225d702120d20e0dbd431abddad4a14c2 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 7 Apr 2024 20:31:56 -0400
Subject: [PATCH 07/14] fix or operation

---
 llvm/lib/Support/KnownBits.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 690bb750e90c3d..36c2e9e8854aed 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -784,7 +784,7 @@ KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).ashr(1)
-  KnownBits andResult = LHS & RHS;
+  KnownBits andResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
@@ -794,7 +794,7 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).lshr(1)
-  KnownBits andResult = LHS & RHS;
+  KnownBits andResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);

>From 529ebe7c0a5b6d1436b9c9b41b792bd84937ad69 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Mon, 8 Apr 2024 12:56:43 -0400
Subject: [PATCH 08/14] fix variable name

---
 llvm/lib/Support/KnownBits.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 36c2e9e8854aed..4a7796f6c142a6 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -784,22 +784,22 @@ KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).ashr(1)
-  KnownBits andResult = LHS | RHS;
+  KnownBits orResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
   return computeForAddSub(/*Add*/ false, /* NSW */ true, /* NUW */ false,
-                          andResult, xorResult);
+                          orResult, xorResult);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).lshr(1)
-  KnownBits andResult = LHS | RHS;
+  KnownBits orResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
   xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
   return computeForAddSub(/*Add*/ false, /* NSW */ false, /* NUW */ true,
-                          andResult, xorResult);
+                          orResult, xorResult);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From cecb3c9b4e2ef699a620ee95ed02a921a9bb36ed Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Tue, 9 Apr 2024 23:24:46 -0400
Subject: [PATCH 09/14] fix test

---
 llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp b/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp
index 27bcad7c24c4db..e498a2f60bad35 100644
--- a/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp
+++ b/llvm/unittests/CodeGen/AArch64SelectionDAGTest.cpp
@@ -826,7 +826,7 @@ TEST_F(AArch64SelectionDAGTest,
   EXPECT_EQ(KnownAVGFLOORU.One, Ones);
 
   SDValue AVGFLOORS =
-      DAG->getNode(ISD::AVGFLOORU, Loc, Int16Vec8VT, ZextOp0, ZextOp1);
+      DAG->getNode(ISD::AVGFLOORS, Loc, Int16Vec8VT, ZextOp0, ZextOp1);
   KnownBits KnownAVGFLOORS = DAG->computeKnownBits(AVGFLOORS);
   EXPECT_EQ(KnownAVGFLOORS.Zero, Zeroes);
   EXPECT_EQ(KnownAVGFLOORS.One, Ones);

>From 0ad452fd318eaefb6a3470368dbfcda0d6510cc6 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 12 Apr 2024 00:36:02 -0400
Subject: [PATCH 10/14] try not touch zeros

---
 llvm/lib/Support/KnownBits.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 4a7796f6c142a6..7ca2d5e2d325a4 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -766,7 +766,7 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 & C2) + (C1 ^ C2).ashr(1)
   KnownBits andResult = LHS & RHS;
   KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.ashrInPlace(1);
+  // xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
   return computeForAddSub(/*Add*/ true, /* NSW */ true, /* NUW */ false,
                           andResult, xorResult);
@@ -776,7 +776,7 @@ KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 & C2) + (C1 ^ C2).lshr(1)
   KnownBits andResult = LHS & RHS;
   KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.lshrInPlace(1);
+  // xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
   return computeForAddSub(/*Add*/ true, /* NSW */ false, /* NUW */ true,
                           andResult, xorResult);
@@ -786,7 +786,7 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).ashr(1)
   KnownBits orResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.ashrInPlace(1);
+  // xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
   return computeForAddSub(/*Add*/ false, /* NSW */ true, /* NUW */ false,
                           orResult, xorResult);
@@ -796,7 +796,7 @@ KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).lshr(1)
   KnownBits orResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.lshrInPlace(1);
+  // xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
   return computeForAddSub(/*Add*/ false, /* NSW */ false, /* NUW */ true,
                           orResult, xorResult);

>From 87367c0453f83ee819c2e16d740b9b778e8ec21e Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 12 Apr 2024 01:14:53 -0400
Subject: [PATCH 11/14] revert back

---
 llvm/lib/Support/KnownBits.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 7ca2d5e2d325a4..4a7796f6c142a6 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -766,7 +766,7 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 & C2) + (C1 ^ C2).ashr(1)
   KnownBits andResult = LHS & RHS;
   KnownBits xorResult = LHS ^ RHS;
-  // xorResult.Zero.ashrInPlace(1);
+  xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
   return computeForAddSub(/*Add*/ true, /* NSW */ true, /* NUW */ false,
                           andResult, xorResult);
@@ -776,7 +776,7 @@ KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 & C2) + (C1 ^ C2).lshr(1)
   KnownBits andResult = LHS & RHS;
   KnownBits xorResult = LHS ^ RHS;
-  // xorResult.Zero.lshrInPlace(1);
+  xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
   return computeForAddSub(/*Add*/ true, /* NSW */ false, /* NUW */ true,
                           andResult, xorResult);
@@ -786,7 +786,7 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).ashr(1)
   KnownBits orResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
-  // xorResult.Zero.ashrInPlace(1);
+  xorResult.Zero.ashrInPlace(1);
   xorResult.One.ashrInPlace(1);
   return computeForAddSub(/*Add*/ false, /* NSW */ true, /* NUW */ false,
                           orResult, xorResult);
@@ -796,7 +796,7 @@ KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).lshr(1)
   KnownBits orResult = LHS | RHS;
   KnownBits xorResult = LHS ^ RHS;
-  // xorResult.Zero.lshrInPlace(1);
+  xorResult.Zero.lshrInPlace(1);
   xorResult.One.lshrInPlace(1);
   return computeForAddSub(/*Add*/ false, /* NSW */ false, /* NUW */ true,
                           orResult, xorResult);

>From 6505a122a1bb50c3afe90d1f02af4610f72465e4 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 12 Apr 2024 16:05:22 -0400
Subject: [PATCH 12/14] change impl

---
 llvm/lib/Support/KnownBits.cpp | 48 +++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 4a7796f6c142a6..225b27f389d05b 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -764,42 +764,42 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 & C2) + (C1 ^ C2).ashr(1)
-  KnownBits andResult = LHS & RHS;
-  KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.ashrInPlace(1);
-  xorResult.One.ashrInPlace(1);
-  return computeForAddSub(/*Add*/ true, /* NSW */ true, /* NUW */ false,
-                          andResult, xorResult);
+  unsigned BitWidth = LHS.getBitWidth();
+  KnownBits tmpLHS = LHS.sext(BitWidth + 1);
+  KnownBits tmpRHS = RHS.sext(BitWidth + 1);
+  KnownBits Carry = KnownBits::makeConstant(APInt(1, 1));
+  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
+  return tmpLHS.extractBits(BitWidth, 1);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 & C2) + (C1 ^ C2).lshr(1)
-  KnownBits andResult = LHS & RHS;
-  KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.lshrInPlace(1);
-  xorResult.One.lshrInPlace(1);
-  return computeForAddSub(/*Add*/ true, /* NSW */ false, /* NUW */ true,
-                          andResult, xorResult);
+  unsigned BitWidth = LHS.getBitWidth();
+  KnownBits tmpLHS = LHS.zext(BitWidth + 1);
+  KnownBits tmpRHS = RHS.zext(BitWidth + 1);
+  KnownBits Carry = KnownBits::makeConstant(APInt(1, 0));
+  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
+  return tmpLHS.extractBits(BitWidth, 1);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).ashr(1)
-  KnownBits orResult = LHS | RHS;
-  KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.ashrInPlace(1);
-  xorResult.One.ashrInPlace(1);
-  return computeForAddSub(/*Add*/ false, /* NSW */ true, /* NUW */ false,
-                          orResult, xorResult);
+  unsigned BitWidth = LHS.getBitWidth();
+  KnownBits tmpLHS = LHS.sext(BitWidth + 1);
+  KnownBits tmpRHS = RHS.sext(BitWidth + 1);
+  KnownBits Carry = KnownBits::makeConstant(APInt(1, 1));
+  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
+  return tmpLHS.extractBits(BitWidth, 1);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
   // (C1 | C2) - (C1 ^ C2).lshr(1)
-  KnownBits orResult = LHS | RHS;
-  KnownBits xorResult = LHS ^ RHS;
-  xorResult.Zero.lshrInPlace(1);
-  xorResult.One.lshrInPlace(1);
-  return computeForAddSub(/*Add*/ false, /* NSW */ false, /* NUW */ true,
-                          orResult, xorResult);
+  unsigned BitWidth = LHS.getBitWidth();
+  KnownBits tmpLHS = LHS.zext(BitWidth + 1);
+  KnownBits tmpRHS = RHS.zext(BitWidth + 1);
+  KnownBits Carry = KnownBits::makeConstant(APInt(1, 0));
+  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
+  return tmpLHS.extractBits(BitWidth, 1);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From 1390e862ccb614f5be9cb8eeaee5995223cc59b0 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 14 Apr 2024 23:31:54 -0400
Subject: [PATCH 13/14] change impl to use APInt

---
 llvm/lib/Support/KnownBits.cpp | 40 ++++++++++------------------------
 1 file changed, 12 insertions(+), 28 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 225b27f389d05b..20d77cf04e68b6 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -763,43 +763,27 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
 }
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
-  // (C1 & C2) + (C1 ^ C2).ashr(1)
-  unsigned BitWidth = LHS.getBitWidth();
-  KnownBits tmpLHS = LHS.sext(BitWidth + 1);
-  KnownBits tmpRHS = RHS.sext(BitWidth + 1);
-  KnownBits Carry = KnownBits::makeConstant(APInt(1, 1));
-  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
-  return tmpLHS.extractBits(BitWidth, 1);
+  APInt Zero = APIntOps::avgFloorS(LHS.Zero, RHS.Zero);
+  APInt One = APIntOps::avgFloorS(LHS.One, RHS.One);
+  return KnownBits(Zero, One);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
-  // (C1 & C2) + (C1 ^ C2).lshr(1)
-  unsigned BitWidth = LHS.getBitWidth();
-  KnownBits tmpLHS = LHS.zext(BitWidth + 1);
-  KnownBits tmpRHS = RHS.zext(BitWidth + 1);
-  KnownBits Carry = KnownBits::makeConstant(APInt(1, 0));
-  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
-  return tmpLHS.extractBits(BitWidth, 1);
+  APInt Zero = APIntOps::avgFloorS(LHS.Zero, RHS.Zero);
+  APInt One = APIntOps::avgFloorS(LHS.One, RHS.One);
+  return KnownBits(Zero, One);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
-  // (C1 | C2) - (C1 ^ C2).ashr(1)
-  unsigned BitWidth = LHS.getBitWidth();
-  KnownBits tmpLHS = LHS.sext(BitWidth + 1);
-  KnownBits tmpRHS = RHS.sext(BitWidth + 1);
-  KnownBits Carry = KnownBits::makeConstant(APInt(1, 1));
-  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
-  return tmpLHS.extractBits(BitWidth, 1);
+  APInt Zero = APIntOps::avgCeilS(LHS.Zero, RHS.Zero);
+  APInt One = APIntOps::avgCeilS(LHS.One, RHS.One);
+  return KnownBits(Zero, One);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  // (C1 | C2) - (C1 ^ C2).lshr(1)
-  unsigned BitWidth = LHS.getBitWidth();
-  KnownBits tmpLHS = LHS.zext(BitWidth + 1);
-  KnownBits tmpRHS = RHS.zext(BitWidth + 1);
-  KnownBits Carry = KnownBits::makeConstant(APInt(1, 0));
-  tmpLHS = KnownBits::computeForAddCarry(tmpLHS, tmpRHS, Carry);
-  return tmpLHS.extractBits(BitWidth, 1);
+  APInt Zero = APIntOps::avgCeilU(LHS.Zero, RHS.Zero);
+  APInt One = APIntOps::avgCeilU(LHS.One, RHS.One);
+  return KnownBits(Zero, One);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From 1dffaa42017ab51fecd49ff26b8eaa5f67079092 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Mon, 15 Apr 2024 00:47:23 -0400
Subject: [PATCH 14/14] fix syntax

---
 llvm/lib/Support/KnownBits.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 20d77cf04e68b6..37e126aa26f710 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -769,8 +769,8 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
-  APInt Zero = APIntOps::avgFloorS(LHS.Zero, RHS.Zero);
-  APInt One = APIntOps::avgFloorS(LHS.One, RHS.One);
+  APInt Zero = APIntOps::avgFloorU(LHS.Zero, RHS.Zero);
+  APInt One = APIntOps::avgFloorU(LHS.One, RHS.One);
   return KnownBits(Zero, One);
 }
 



More information about the llvm-commits mailing list