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

Nhat Nguyen via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 24 10:55:55 PDT 2024


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

This PR is to address the issue #84640

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



More information about the llvm-commits mailing list