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

Nhat Nguyen via llvm-commits llvm-commits at lists.llvm.org
Sat May 18 15:07:42 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/33] 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 d72355dab6f1d..07c7ad0882387 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/33] 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 73cb01e0644a8..575362dc18b0c 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/33] 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 575362dc18b0c..93049d05eb0b5 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 1dd0fa49a460f..f6af02ded36a2 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 07c7ad0882387..1ad30a72166d9 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/33] 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 027d6379af26b..74432feea353e 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/33] 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 f6af02ded36a2..24edaf0d4af9b 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 1ad30a72166d9..690bb750e90c3 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/33] 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 24edaf0d4af9b..7884fcbfddad0 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 74432feea353e..77c786dc7c489 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/33] 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 690bb750e90c3..36c2e9e8854ae 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/33] 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 36c2e9e8854ae..4a7796f6c142a 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/33] 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 27bcad7c24c4d..e498a2f60bad3 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/33] 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 4a7796f6c142a..7ca2d5e2d325a 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/33] 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 7ca2d5e2d325a..4a7796f6c142a 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/33] 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 4a7796f6c142a..225b27f389d05 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/33] 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 225b27f389d05..20d77cf04e68b 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/33] 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 20d77cf04e68b..37e126aa26f71 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);
 }
 

>From 61c7db79d63e698f0d59c52e046dc85883bbecaa Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Tue, 23 Apr 2024 23:56:43 -0400
Subject: [PATCH 15/33] clang format

---
 llvm/unittests/Support/KnownBitsTest.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 63f5688491de5..824cf7501fd44 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -502,13 +502,17 @@ TEST(KnownBitsTest, BinaryExhaustive) {
       [](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
       /*CheckOptimality=*/false);
 
-  testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS, APIntOps::avgFloorS, false);
+  testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS, APIntOps::avgFloorS,
+                         false);
 
-  testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU, APIntOps::avgFloorU, false);
+  testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU, APIntOps::avgFloorU,
+                         false);
 
-  testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU, false );
+  testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU,
+                         false);
 
-  testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS, false);
+  testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS,
+                         false);
 }
 
 TEST(KnownBitsTest, UnaryExhaustive) {

>From 857f57acb98a5b3c129c3ff195f638a5fcd74814 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 11 May 2024 20:45:01 -0400
Subject: [PATCH 16/33] try new impl

---
 llvm/lib/Support/KnownBits.cpp | 106 ++++++++++++++++++++++++---------
 1 file changed, 79 insertions(+), 27 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index a8257f4579313..86db9ef3b82b8 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -776,42 +776,94 @@ 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);
+  // 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);
+  KnownBits Known = LHS;
+  KnownBits Known2 = RHS;
+  bool IsCeil = false;
+  bool IsSigned = true;
+  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+  unsigned BitWidth = Known.getBitWidth();
+  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 = Known.extractBits(BitWidth, 1);
+  return Known;
 }
 
 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);
+  // // (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);
+  KnownBits Known = LHS;
+  KnownBits Known2 = RHS;
+  bool IsCeil = false;
+  bool IsSigned = false;
+  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+  unsigned BitWidth = Known.getBitWidth();
+  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 = Known.extractBits(BitWidth, 1);
+  return Known;
 }
 
 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);
+  // // (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);
+  KnownBits Known = LHS;
+  KnownBits Known2 = RHS;
+  bool IsCeil = true;
+  bool IsSigned = true;
+  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+  unsigned BitWidth = Known.getBitWidth();
+  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 = Known.extractBits(BitWidth, 1);
+  return Known;
 }
 
 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);
+  // // (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);
+  KnownBits Known = LHS;
+  KnownBits Known2 = RHS;
+  bool IsCeil = true;
+  bool IsSigned = false;
+  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
+  unsigned BitWidth = Known.getBitWidth();
+  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 = Known.extractBits(BitWidth, 1);
+  return Known;
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From 672566a06b9207ee233b2624ff2b012ceba65a86 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 11 May 2024 21:21:17 -0400
Subject: [PATCH 17/33] remove unused code

---
 llvm/lib/Support/KnownBits.cpp | 36 ----------------------------------
 1 file changed, 36 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 86db9ef3b82b8..e096a89075194 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -775,19 +775,10 @@ 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);
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   bool IsCeil = false;
   bool IsSigned = true;
-  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
   unsigned BitWidth = Known.getBitWidth();
   Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
   Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
@@ -798,19 +789,10 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
 }
 
 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);
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   bool IsCeil = false;
   bool IsSigned = false;
-  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
   unsigned BitWidth = Known.getBitWidth();
   Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
   Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
@@ -821,19 +803,10 @@ 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 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);
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   bool IsCeil = true;
   bool IsSigned = true;
-  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
   unsigned BitWidth = Known.getBitWidth();
   Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
   Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
@@ -844,19 +817,10 @@ 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 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);
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   bool IsCeil = true;
   bool IsSigned = false;
-  // Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
-  // Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
   unsigned BitWidth = Known.getBitWidth();
   Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
   Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);

>From 91e6eb28058330bcefcb6096a8959eecda26b9b0 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 11 May 2024 21:29:55 -0400
Subject: [PATCH 18/33] missing header

---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 36 ++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 libc/test/src/sys/ioctl/linux/ioctl_test.cpp

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
new file mode 100644
index 0000000000000..7f7ea2eb232ec
--- /dev/null
+++ b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
@@ -0,0 +1,36 @@
+//===-- Unittests for ioctl -----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/errno/libc_errno.h"
+#include "src/sys/ioctl/ioctl.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
+
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcIoctlTest, InvalidFileDescriptor) {
+  int fd = 10;
+  unsigned long request = 10;
+  int res = LIBC_NAMESPACE::ioctl(fd, request, NULL);
+  EXPECT_THAT(res, Fails(EBADF, -1));
+}
+
+TEST(LlvmLibcIoctlTest, ValidFileDescriptor) {
+  int fd = open("/dev/null", O_RDWR);
+  int data;
+  int res = LIBC_NAMESPACE::ioctl(fd, FIONREAD, &data);
+  EXPECT_THAT(res, Succeeds());
+}

>From 94ce6cbffc63e7b70224c420be4da4faca86a224 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 11 May 2024 21:31:58 -0400
Subject: [PATCH 19/33] revert

---
 libc/test/src/sys/ioctl/linux/ioctl_test.cpp | 36 --------------------
 1 file changed, 36 deletions(-)
 delete mode 100644 libc/test/src/sys/ioctl/linux/ioctl_test.cpp

diff --git a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp b/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
deleted file mode 100644
index 7f7ea2eb232ec..0000000000000
--- a/libc/test/src/sys/ioctl/linux/ioctl_test.cpp
+++ /dev/null
@@ -1,36 +0,0 @@
-//===-- Unittests for ioctl -----------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-#include "src/errno/libc_errno.h"
-#include "src/sys/ioctl/ioctl.h"
-#include "test/UnitTest/ErrnoSetterMatcher.h"
-#include "test/UnitTest/LibcTest.h"
-#include "test/UnitTest/Test.h"
-
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/syscall.h>
-#include <unistd.h>
-
-using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
-using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
-
-TEST(LlvmLibcIoctlTest, InvalidFileDescriptor) {
-  int fd = 10;
-  unsigned long request = 10;
-  int res = LIBC_NAMESPACE::ioctl(fd, request, NULL);
-  EXPECT_THAT(res, Fails(EBADF, -1));
-}
-
-TEST(LlvmLibcIoctlTest, ValidFileDescriptor) {
-  int fd = open("/dev/null", O_RDWR);
-  int data;
-  int res = LIBC_NAMESPACE::ioctl(fd, FIONREAD, &data);
-  EXPECT_THAT(res, Succeeds());
-}

>From 7825ad7e332fb88b3658aacf9c162c61d6c28cb9 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Mon, 13 May 2024 20:46:12 -0400
Subject: [PATCH 20/33] typo

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

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 6ded7df0e3205..ba4a5f01036ca 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -360,10 +360,10 @@ struct KnownBits {
   /// Compute knownbits resulting from APIntOps::avgFloorU
   static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Compute knownbits resulting from APIntOps::avgCelS
+  /// Compute knownbits resulting from APIntOps::avgCeilS
   static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Compute knownbits resulting from APIntOps::avgCelU
+  /// Compute knownbits resulting from APIntOps::avgCeilU
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
   /// Compute known bits resulting from multiplying LHS and RHS.

>From 0b91c85f88ea04f390b332c874af7bc635447aec Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Thu, 16 May 2024 23:53:57 -0400
Subject: [PATCH 21/33] add helper function

---
 llvm/include/llvm/Support/KnownBits.h |  5 +++
 llvm/lib/Support/KnownBits.cpp        | 46 ++++++---------------------
 2 files changed, 15 insertions(+), 36 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index ba4a5f01036ca..97f5be5398ddc 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -366,6 +366,11 @@ struct KnownBits {
   /// Compute knownbits resulting from APIntOps::avgCeilU
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
+  /// Return the knownbits for computing average floor or ceil
+  static KnownBits avgFloorCeilCompute(const KnownBits &LHS,
+                                       const KnownBits &RHS, bool IsCeil,
+                                       bool IsSigned);
+
   /// Compute known bits resulting from multiplying LHS and RHS.
   static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
                        bool NoUndefSelfMultiply = false);
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index e096a89075194..3e98653c69cca 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -774,11 +774,10 @@ 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) {
+KnownBits avgFloorCeilCompute(const KnownBits &LHS, const KnownBits &RHS,
+                              bool IsCeil, bool IsSigned) {
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
-  bool IsCeil = false;
-  bool IsSigned = true;
   unsigned BitWidth = Known.getBitWidth();
   Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
   Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
@@ -788,46 +787,21 @@ KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
   return Known;
 }
 
+KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
+  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ false, /* IsSigned */ true);
+}
+
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
-  KnownBits Known = LHS;
-  KnownBits Known2 = RHS;
-  bool IsCeil = false;
-  bool IsSigned = false;
-  unsigned BitWidth = Known.getBitWidth();
-  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 = Known.extractBits(BitWidth, 1);
-  return Known;
+  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ false,
+                             /* IsSigned */ false);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
-  KnownBits Known = LHS;
-  KnownBits Known2 = RHS;
-  bool IsCeil = true;
-  bool IsSigned = true;
-  unsigned BitWidth = Known.getBitWidth();
-  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 = Known.extractBits(BitWidth, 1);
-  return Known;
+  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  KnownBits Known = LHS;
-  KnownBits Known2 = RHS;
-  bool IsCeil = true;
-  bool IsSigned = false;
-  unsigned BitWidth = Known.getBitWidth();
-  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 = Known.extractBits(BitWidth, 1);
-  return Known;
+  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ false);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From 489a371990a1ee8cd2cdc105cb3ae9dd4511dbdd Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Thu, 16 May 2024 23:59:15 -0400
Subject: [PATCH 22/33] change function name

---
 llvm/include/llvm/Support/KnownBits.h | 7 +++----
 llvm/lib/Support/KnownBits.cpp        | 4 ++--
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 97f5be5398ddc..55a5f3c01109f 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -366,10 +366,9 @@ struct KnownBits {
   /// Compute knownbits resulting from APIntOps::avgCeilU
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Return the knownbits for computing average floor or ceil
-  static KnownBits avgFloorCeilCompute(const KnownBits &LHS,
-                                       const KnownBits &RHS, bool IsCeil,
-                                       bool IsSigned);
+  /// Return the knownbits for computing average
+  static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
+                              bool IsCeil, bool IsSigned);
 
   /// Compute known bits resulting from multiplying LHS and RHS.
   static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 3e98653c69cca..6a4d0ac0b92d8 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -774,8 +774,8 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
-KnownBits avgFloorCeilCompute(const KnownBits &LHS, const KnownBits &RHS,
-                              bool IsCeil, bool IsSigned) {
+KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil,
+                     bool IsSigned) {
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   unsigned BitWidth = Known.getBitWidth();

>From 10de0a1e52cb304e4a7819aecaf4b489cf3d7483 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 00:01:41 -0400
Subject: [PATCH 23/33] syntax fix

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

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 6a4d0ac0b92d8..53d78131f5957 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -788,20 +788,20 @@ KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil,
 }
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ false, /* IsSigned */ true);
+  return avgCompute(LHS, RHS, /* IsCeil */ false, /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ false,
-                             /* IsSigned */ false);
+  return avgCompute(LHS, RHS, /* IsCeil */ false,
+                    /* IsSigned */ false);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ true);
+  return avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgFloorCeilCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ false);
+  return avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ false);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From 69c1a37f9060227131aec6dee9c873ebe73b63d5 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 00:20:32 -0400
Subject: [PATCH 24/33] syntax fix

---
 llvm/include/llvm/Support/KnownBits.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 55a5f3c01109f..b5c2fbe0a1eda 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -367,7 +367,7 @@ struct KnownBits {
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
   /// Return the knownbits for computing average
-  static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
+  inline KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
                               bool IsCeil, bool IsSigned);
 
   /// Compute known bits resulting from multiplying LHS and RHS.

>From 99e179a8c97f387df9f93626044e5c2631899852 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 00:24:17 -0400
Subject: [PATCH 25/33] syntax fix

---
 llvm/include/llvm/Support/KnownBits.h |  2 +-
 llvm/lib/Support/KnownBits.cpp        | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index b5c2fbe0a1eda..f69f8c3382cea 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -367,7 +367,7 @@ struct KnownBits {
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
   /// Return the knownbits for computing average
-  inline KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
+  KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
                               bool IsCeil, bool IsSigned);
 
   /// Compute known bits resulting from multiplying LHS and RHS.
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 53d78131f5957..c5ae544003bdc 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -774,7 +774,7 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
-KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil,
+KnownBits KnownBits::avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil,
                      bool IsSigned) {
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
@@ -788,20 +788,20 @@ KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil,
 }
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgCompute(LHS, RHS, /* IsCeil */ false, /* IsSigned */ true);
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false, /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgCompute(LHS, RHS, /* IsCeil */ false,
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false,
                     /* IsSigned */ false);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ true);
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ false);
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ false);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From ab00768213f07873ae5c92af69f57954ec0c8dcf Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 00:35:39 -0400
Subject: [PATCH 26/33] some fix

---
 llvm/include/llvm/Support/KnownBits.h |  2 +-
 llvm/lib/Support/KnownBits.cpp        | 31 +++++++++++++++------------
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index f69f8c3382cea..55a5f3c01109f 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -367,7 +367,7 @@ struct KnownBits {
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
   /// Return the knownbits for computing average
-  KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
+  static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
                               bool IsCeil, bool IsSigned);
 
   /// Compute known bits resulting from multiplying LHS and RHS.
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index c5ae544003bdc..7c3b0d322e204 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -18,9 +18,8 @@
 
 using namespace llvm;
 
-static KnownBits computeForAddCarry(
-    const KnownBits &LHS, const KnownBits &RHS,
-    bool CarryZero, bool CarryOne) {
+static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS,
+                                    bool CarryZero, bool CarryOne) {
   assert(!(CarryZero && CarryOne) &&
          "Carry can't be zero and one at the same time");
 
@@ -47,11 +46,12 @@ static KnownBits computeForAddCarry(
   return KnownOut;
 }
 
-KnownBits KnownBits::computeForAddCarry(
-    const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
+KnownBits KnownBits::computeForAddCarry(const KnownBits &LHS,
+                                        const KnownBits &RHS,
+                                        const KnownBits &Carry) {
   assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
-  return ::computeForAddCarry(
-      LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
+  return ::computeForAddCarry(LHS, RHS, Carry.Zero.getBoolValue(),
+                              Carry.One.getBoolValue());
 }
 
 KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, bool NUW,
@@ -475,7 +475,7 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
   Known.Zero.setAllBits();
   Known.One.setAllBits();
   for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
-      ++ShiftAmt) {
+       ++ShiftAmt) {
     // Skip if the shift amount is impossible.
     if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
         (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
@@ -774,8 +774,8 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
-KnownBits KnownBits::avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool IsCeil,
-                     bool IsSigned) {
+KnownBits KnownBits::avgCompute(const KnownBits &LHS, const KnownBits &RHS,
+                                bool IsCeil, bool IsSigned) {
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   unsigned BitWidth = Known.getBitWidth();
@@ -788,20 +788,23 @@ KnownBits KnownBits::avgCompute(const KnownBits &LHS, const KnownBits &RHS, bool
 }
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false, /* IsSigned */ true);
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false,
+                               /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
   return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false,
-                    /* IsSigned */ false);
+                               /* IsSigned */ false);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ true);
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true,
+                               /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true, /* IsSigned */ false);
+  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true,
+                               /* IsSigned */ false);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From d2309974fba39c2b59d9853e832a953ab6af5623 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 09:02:44 -0400
Subject: [PATCH 27/33] some fix

---
 llvm/include/llvm/Support/KnownBits.h |  4 ----
 llvm/lib/Support/KnownBits.cpp        | 20 ++++++++++----------
 2 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/llvm/include/llvm/Support/KnownBits.h b/llvm/include/llvm/Support/KnownBits.h
index 55a5f3c01109f..ba4a5f01036ca 100644
--- a/llvm/include/llvm/Support/KnownBits.h
+++ b/llvm/include/llvm/Support/KnownBits.h
@@ -366,10 +366,6 @@ struct KnownBits {
   /// Compute knownbits resulting from APIntOps::avgCeilU
   static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS);
 
-  /// Return the knownbits for computing average
-  static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
-                              bool IsCeil, bool IsSigned);
-
   /// Compute known bits resulting from multiplying LHS and RHS.
   static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
                        bool NoUndefSelfMultiply = false);
diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 7c3b0d322e204..44e2733e0cd7c 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -774,8 +774,8 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
-KnownBits KnownBits::avgCompute(const KnownBits &LHS, const KnownBits &RHS,
-                                bool IsCeil, bool IsSigned) {
+static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
+                            bool IsCeil, bool IsSigned) {
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
   unsigned BitWidth = Known.getBitWidth();
@@ -788,23 +788,23 @@ KnownBits KnownBits::avgCompute(const KnownBits &LHS, const KnownBits &RHS,
 }
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false,
-                               /* IsSigned */ true);
+  return avgCompute(LHS, RHS, /* IsCeil */ false,
+                    /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgFloorU(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ false,
-                               /* IsSigned */ false);
+  return avgCompute(LHS, RHS, /* IsCeil */ false,
+                    /* IsSigned */ false);
 }
 
 KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true,
-                               /* IsSigned */ true);
+  return avgCompute(LHS, RHS, /* IsCeil */ true,
+                    /* IsSigned */ true);
 }
 
 KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  return KnownBits::avgCompute(LHS, RHS, /* IsCeil */ true,
-                               /* IsSigned */ false);
+  return avgCompute(LHS, RHS, /* IsCeil */ true,
+                    /* IsSigned */ false);
 }
 
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,

>From eca6944f3888845668b03ece2283db4ad3028973 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 09:30:30 -0400
Subject: [PATCH 28/33] revert

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

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 44e2733e0cd7c..4aa83a863c8c8 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -778,6 +778,8 @@ static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
                             bool IsCeil, bool IsSigned) {
   KnownBits Known = LHS;
   KnownBits Known2 = RHS;
+  bool IsCeil = false;
+  bool IsSigned = true;
   unsigned BitWidth = Known.getBitWidth();
   Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
   Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);

>From 2cd048c4babdcb5ab2641606d005382b5fadd1ee Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 09:48:24 -0400
Subject: [PATCH 29/33] make test optimal

---
 llvm/lib/Support/KnownBits.cpp           | 27 ++++++++----------------
 llvm/unittests/Support/KnownBitsTest.cpp | 14 ++++++------
 2 files changed, 15 insertions(+), 26 deletions(-)

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 4aa83a863c8c8..6dfaf73fe0938 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -774,19 +774,15 @@ KnownBits KnownBits::usub_sat(const KnownBits &LHS, const KnownBits &RHS) {
   return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
 }
 
-static KnownBits avgCompute(const KnownBits &LHS, const KnownBits &RHS,
-                            bool IsCeil, bool IsSigned) {
-  KnownBits Known = LHS;
-  KnownBits Known2 = RHS;
-  bool IsCeil = false;
-  bool IsSigned = true;
-  unsigned BitWidth = Known.getBitWidth();
-  Known = IsSigned ? Known.sext(BitWidth + 1) : Known.zext(BitWidth + 1);
-  Known2 = IsSigned ? Known2.sext(BitWidth + 1) : Known2.zext(BitWidth + 1);
+static KnownBits avgCompute(KnownBits LHS, KnownBits RHS, bool IsCeil,
+                            bool IsSigned) {
+  unsigned BitWidth = LHS.getBitWidth();
+  LHS = IsSigned ? LHS.sext(BitWidth + 1) : LHS.zext(BitWidth + 1);
+  RHS = IsSigned ? RHS.sext(BitWidth + 1) : RHS.zext(BitWidth + 1);
   KnownBits Carry = KnownBits::makeConstant(APInt(1, IsCeil ? 1 : 0));
-  Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
-  Known = Known.extractBits(BitWidth, 1);
-  return Known;
+  LHS = KnownBits::computeForAddCarry(LHS, RHS, Carry);
+  LHS = LHS.extractBits(BitWidth, 1);
+  return LHS;
 }
 
 KnownBits KnownBits::avgFloorS(const KnownBits &LHS, const KnownBits &RHS) {
@@ -804,11 +800,6 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
                     /* IsSigned */ true);
 }
 
-KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
-  return avgCompute(LHS, RHS, /* IsCeil */ true,
-                    /* IsSigned */ false);
-}
-
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
                          bool NoUndefSelfMultiply) {
   unsigned BitWidth = LHS.getBitWidth();
@@ -1171,4 +1162,4 @@ void KnownBits::print(raw_ostream &OS) const {
 void KnownBits::dump() const {
   print(dbgs());
   dbgs() << "\n";
-}
+}
\ No newline at end of file
diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index 824cf7501fd44..db1d35c03a958 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -502,17 +502,15 @@ TEST(KnownBitsTest, BinaryExhaustive) {
       [](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
       /*CheckOptimality=*/false);
 
-  testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS, APIntOps::avgFloorS,
-                         false);
+  testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS,
+                         APIntOps::avgFloorS);
 
-  testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU, APIntOps::avgFloorU,
-                         false);
+  testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU,
+                         APIntOps::avgFloorU);
 
-  testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU,
-                         false);
+  testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU);
 
-  testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS,
-                         false);
+  testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS);
 }
 
 TEST(KnownBitsTest, UnaryExhaustive) {

>From 3ae62ff6889210477baefec938ea35e5fe3f1804 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 10:05:43 -0400
Subject: [PATCH 30/33] add avgCeilU back

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

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 6dfaf73fe0938..157b55677f7d6 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -800,6 +800,11 @@ KnownBits KnownBits::avgCeilS(const KnownBits &LHS, const KnownBits &RHS) {
                     /* IsSigned */ true);
 }
 
+KnownBits KnownBits::avgCeilU(const KnownBits &LHS, const KnownBits &RHS) {
+  return avgCompute(LHS, RHS, /* IsCeil */ true,
+                    /* IsSigned */ false);
+}
+
 KnownBits KnownBits::mul(const KnownBits &LHS, const KnownBits &RHS,
                          bool NoUndefSelfMultiply) {
   unsigned BitWidth = LHS.getBitWidth();
@@ -1162,4 +1167,4 @@ void KnownBits::print(raw_ostream &OS) const {
 void KnownBits::dump() const {
   print(dbgs());
   dbgs() << "\n";
-}
\ No newline at end of file
+}

>From 667c7afa9dbfca2dcaa86177aeba1b3b5c2ef6ec Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 12:03:04 -0400
Subject: [PATCH 31/33] remove spurious reformat

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

diff --git a/llvm/lib/Support/KnownBits.cpp b/llvm/lib/Support/KnownBits.cpp
index 157b55677f7d6..97b4498a6e0aa 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -18,8 +18,9 @@
 
 using namespace llvm;
 
-static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS,
-                                    bool CarryZero, bool CarryOne) {
+static KnownBits computeForAddCarry(
+    const KnownBits &LHS, const KnownBits &RHS,
+    bool CarryZero, bool CarryOne) {
   assert(!(CarryZero && CarryOne) &&
          "Carry can't be zero and one at the same time");
 
@@ -46,9 +47,8 @@ static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS,
   return KnownOut;
 }
 
-KnownBits KnownBits::computeForAddCarry(const KnownBits &LHS,
-                                        const KnownBits &RHS,
-                                        const KnownBits &Carry) {
+KnownBits KnownBits::computeForAddCarry(
+    const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
   assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
   return ::computeForAddCarry(LHS, RHS, Carry.Zero.getBoolValue(),
                               Carry.One.getBoolValue());
@@ -475,7 +475,7 @@ KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS,
   Known.Zero.setAllBits();
   Known.One.setAllBits();
   for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
-       ++ShiftAmt) {
+      ++ShiftAmt) {
     // Skip if the shift amount is impossible.
     if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
         (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)

>From 44c0b926ac73b00f6e5dd582c83e394ed860d0c0 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Fri, 17 May 2024 12:04:21 -0400
Subject: [PATCH 32/33] remove spurious reformat

---
 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 97b4498a6e0aa..d6012a8eea8a6 100644
--- a/llvm/lib/Support/KnownBits.cpp
+++ b/llvm/lib/Support/KnownBits.cpp
@@ -50,8 +50,8 @@ static KnownBits computeForAddCarry(
 KnownBits KnownBits::computeForAddCarry(
     const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
   assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
-  return ::computeForAddCarry(LHS, RHS, Carry.Zero.getBoolValue(),
-                              Carry.One.getBoolValue());
+  return ::computeForAddCarry(
+      LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
 }
 
 KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, bool NUW,

>From 22b2aa9cdc21033341ffc11e0a6c4a31d8ad6684 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 18 May 2024 18:07:14 -0400
Subject: [PATCH 33/33] disable optimality check

---
 llvm/unittests/Support/KnownBitsTest.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/llvm/unittests/Support/KnownBitsTest.cpp b/llvm/unittests/Support/KnownBitsTest.cpp
index db1d35c03a958..824cf7501fd44 100644
--- a/llvm/unittests/Support/KnownBitsTest.cpp
+++ b/llvm/unittests/Support/KnownBitsTest.cpp
@@ -502,15 +502,17 @@ TEST(KnownBitsTest, BinaryExhaustive) {
       [](const APInt &N1, const APInt &N2) { return APIntOps::mulhu(N1, N2); },
       /*CheckOptimality=*/false);
 
-  testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS,
-                         APIntOps::avgFloorS);
+  testBinaryOpExhaustive("avgFloorS", KnownBits::avgFloorS, APIntOps::avgFloorS,
+                         false);
 
-  testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU,
-                         APIntOps::avgFloorU);
+  testBinaryOpExhaustive("avgFloorU", KnownBits::avgFloorU, APIntOps::avgFloorU,
+                         false);
 
-  testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU);
+  testBinaryOpExhaustive("avgCeilU", KnownBits::avgCeilU, APIntOps::avgCeilU,
+                         false);
 
-  testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS);
+  testBinaryOpExhaustive("avgCeilS", KnownBits::avgCeilS, APIntOps::avgCeilS,
+                         false);
 }
 
 TEST(KnownBitsTest, UnaryExhaustive) {



More information about the llvm-commits mailing list