https://github.com/RKSimon updated 
https://github.com/llvm/llvm-project/pull/200114

>From 9d5cb0edda8d6a57f9e3ce22953c18addbeca981 Mon Sep 17 00:00:00 2001
From: Eisenwave <[email protected]>
Date: Thu, 28 May 2026 07:21:36 +0200
Subject: [PATCH 1/4] [APInt] Add bitwiseParity, compressBits, and expandBits
 operations

These are necessary to implement portable intrinsics for bit_compress/pext/bext 
and bit_expand/pdep/bdep.
---
 llvm/include/llvm/ADT/APInt.h    | 27 +++++++++++++++
 llvm/lib/Support/APInt.cpp       | 56 ++++++++++++++++++++++++++++++
 llvm/unittests/ADT/APIntTest.cpp | 58 ++++++++++++++++++++++++++++++++
 3 files changed, 141 insertions(+)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 859cbd5c07147..25cb4531e937a 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2489,6 +2489,33 @@ LLVM_ABI APInt clmulr(const APInt &LHS, const APInt 
&RHS);
 /// clmulh(a, b) = clmulr(a, b) >> 1
 LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS);
 
+/// Compute the parity (inclusive prefix XOR) for each bit in V.
+/// This is equivalent to clmul(V, ~0).
+///
+/// Example:
+/// bitwiseParity(i4 0b1010) = 0b0110
+LLVM_ABI APInt bitwiseParity(const APInt &V);
+
+/// Perform a "compress" operation, also known as pext or bext.
+///
+/// Selects the bits from Val at the positions where Mask has a 1-bit, and
+/// packs them contiguously into the least significant bits of the result.
+///
+/// Examples:
+/// (1) compressBits(i8 0b1010'1010, i8 0b1100'1100) = 0b0000'1010
+/// (2) compressBits(i8 0b1111'1111, i8 0b1010'1010) = 0b0000'1111
+LLVM_ABI APInt compressBits(const APInt &Val, const APInt &Mask);
+
+/// Perform an "expand" operation, also known as pdep or bdep.
+///
+/// Places the least significant bits of Val at the positions where Mask has a
+/// 1-bit, and zeros the remaining bits.
+///
+/// Examples:
+/// (1) expandBits(i8 0b0000'1010, i8 0b1100'1100) = 0b1000'1000
+/// (2) expandBits(i8 0b0000'1111, i8 0b1010'1010) = 0b1010'1010
+LLVM_ABI APInt expandBits(const APInt &Val, const APInt &Mask);
+
 } // namespace APIntOps
 
 // See friend declaration above. This additional declaration is required in
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index df3616abd7dcf..24dbe7cdc6f75 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3259,3 +3259,59 @@ APInt llvm::APIntOps::clmulh(const APInt &LHS, const 
APInt &RHS) {
   assert(LHS.getBitWidth() == RHS.getBitWidth());
   return clmulr(LHS, RHS).lshr(1);
 }
+
+APInt llvm::APIntOps::bitwiseParity(const APInt &V) {
+  // This computes `clmul(V, ~0)`,
+  // but we don't use `clmul` because this special case has lower complexity
+  // (logarithmic amount of loop iterations instead of linear in BW).
+  unsigned BW = V.getBitWidth();
+  APInt Result = V;
+  for (unsigned I = 1; I < BW; I <<= 1)
+    Result ^= Result.shl(I);
+  return Result;
+}
+
+APInt llvm::APIntOps::compressBits(const APInt &Val, const APInt &Mask) {
+  // See also Hacker's Delight 2nd Edition, §7–4 Compress, or Generalized 
Extract
+  unsigned BW = Val.getBitWidth();
+  assert(BW == Mask.getBitWidth() && "Operand mismatch");
+  APInt V = Val & Mask;
+  APInt M = Mask;
+  APInt Mk = (~M).shl(1);
+  for (unsigned I = 1; I < BW; I <<= 1) {
+    APInt MkParity = bitwiseParity(Mk);
+    APInt Move = MkParity & M;
+    M = (M ^ Move) | Move.lshr(I);
+    APInt T = V & Move;
+    V = (V ^ T) | T.lshr(I);
+    Mk &= ~MkParity;
+  }
+  return V;
+}
+
+APInt llvm::APIntOps::expandBits(const APInt &Val, const APInt &Mask) {
+  // See also Hacker's Delight 2nd Edition, §7–5 Expand, or Generalized Insert
+  unsigned BW = Val.getBitWidth();
+  assert(BW == Mask.getBitWidth() && "Operand mismatch");
+  SmallVector<APInt> MoveArray;
+  unsigned LogN = Log2_32_Ceil(BW);
+  MoveArray.reserve(LogN);
+
+  APInt M = Mask;
+  APInt Mk = (~M).shl(1);
+  for (unsigned I = 0; I < LogN; ++I) {
+    APInt MkParity = bitwiseParity(Mk);
+    APInt Move = MkParity & M;
+    M = (M ^ Move) | Move.lshr(1u << I);
+    MoveArray.push_back(std::move(Move));
+    Mk &= ~MkParity;
+  }
+
+  APInt V = Val;
+  for (unsigned I = LogN; I-- > 0;) {
+    APInt Move = MoveArray[I];
+    APInt T = V.shl(1u << I);
+    V = (V & ~Move) | (T & Move);
+  }
+  return V & Mask;
+}
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 3c0446867b14b..668bef67271e8 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -4017,4 +4017,62 @@ TEST(APIntTest, sqrt) {
   EXPECT_EQ(APInt::getMaxValue(256).sqrt(),
             APInt(256, "340282366920938463463374607431768211456", 10));
 }
+
+TEST(APIntTest, bitwiseParity) {
+  EXPECT_EQ(APIntOps::bitwiseParity(APInt(8, 0)).getZExtValue(), 0U);
+  EXPECT_EQ(APIntOps::bitwiseParity(APInt(8, 1)).getZExtValue(), 0xFFU);
+  EXPECT_EQ(APIntOps::bitwiseParity(APInt(4, 0xAU)).getZExtValue(), 6U);
+  EXPECT_EQ(APIntOps::bitwiseParity(APInt(4, 0xFU)).getZExtValue(), 5U);
+  EXPECT_EQ(APIntOps::bitwiseParity(APInt(8, 0xAAU)).getZExtValue(), 0x66U);
+}
+
+TEST(APIntTest, compressBits) {
+  EXPECT_EQ(APIntOps::compressBits(APInt(8, 0), APInt(8, 
0xAAU)).getZExtValue(),
+            0U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(8, 0x55U), APInt(8, 0xAAU)).getZExtValue(),
+      0U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(8, 0xAAU), APInt(8, 0xAAU)).getZExtValue(),
+      15U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(8, 0xFFU), APInt(8, 0xAAU)).getZExtValue(),
+      15U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(8, 0xFFU), APInt(8, 0)).getZExtValue(), 0U);
+  EXPECT_EQ(APIntOps::compressBits(APInt(4, 0xFU), APInt(4, 
0xAU)).getZExtValue(),
+            3U);
+  EXPECT_EQ(APIntOps::compressBits(APInt(4, 0xAU), APInt(4, 
0xAU)).getZExtValue(),
+            3U);
+  EXPECT_EQ(APIntOps::compressBits(APInt(4, 0x5U), APInt(4, 
0xAU)).getZExtValue(),
+            0U);
+}
+
+TEST(APIntTest, expandBits) {
+  EXPECT_EQ(APIntOps::expandBits(APInt(8, 0), APInt(8, 0xAAU)).getZExtValue(),
+            0U);
+  EXPECT_EQ(
+      APIntOps::expandBits(APInt(8, 15U), APInt(8, 0xAAU)).getZExtValue(),
+      0xAAU);
+  EXPECT_EQ(
+      APIntOps::expandBits(APInt(8, 0xFFU), APInt(8, 0)).getZExtValue(), 0U);
+  EXPECT_EQ(APIntOps::expandBits(APInt(4, 3U), APInt(4, 0xAU)).getZExtValue(),
+            0xAU);
+  EXPECT_EQ(APIntOps::expandBits(APInt(4, 1U), APInt(4, 0xAU)).getZExtValue(),
+            2U);
+  APInt X(8, 0b10110100U);
+  APInt M(8, 0b11001110U);
+  EXPECT_EQ(APIntOps::expandBits(APIntOps::compressBits(X, M), M), X & M);
+}
+
+TEST(APIntTest, compressExpandBitsExhaustive) {
+  for (unsigned V = 0; V < 256; ++V) {
+    for (unsigned Mask = 0; Mask < 256; ++Mask) {
+      APInt Val(8, V), APMask(8, Mask);
+      APInt Compressed = APIntOps::compressBits(Val, APMask);
+      APInt RoundTrip = APIntOps::expandBits(Compressed, APMask);
+      EXPECT_EQ(RoundTrip, Val & APMask);
+    }
+  }
+}
 } // end anonymous namespace

>From c20bd40adc506a47b0b4419834ba181b106bae4d Mon Sep 17 00:00:00 2001
From: Jan Schultke <[email protected]>
Date: Thu, 28 May 2026 10:20:15 +0200
Subject: [PATCH 2/4] Fix formatting

---
 llvm/include/llvm/ADT/APInt.h    |  8 ++++----
 llvm/lib/Support/APInt.cpp       |  6 ++++--
 llvm/unittests/ADT/APIntTest.cpp | 26 ++++++++++++++------------
 3 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 25cb4531e937a..8987b037ed47c 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2498,8 +2498,8 @@ LLVM_ABI APInt bitwiseParity(const APInt &V);
 
 /// Perform a "compress" operation, also known as pext or bext.
 ///
-/// Selects the bits from Val at the positions where Mask has a 1-bit, and
-/// packs them contiguously into the least significant bits of the result.
+/// Selects the bits from /p Val at the positions where /p Mask has a 1-bit,
+/// and packs them contiguously into the least significant bits of the result.
 ///
 /// Examples:
 /// (1) compressBits(i8 0b1010'1010, i8 0b1100'1100) = 0b0000'1010
@@ -2508,8 +2508,8 @@ LLVM_ABI APInt compressBits(const APInt &Val, const APInt 
&Mask);
 
 /// Perform an "expand" operation, also known as pdep or bdep.
 ///
-/// Places the least significant bits of Val at the positions where Mask has a
-/// 1-bit, and zeros the remaining bits.
+/// Places the least significant bits of /p Val at the positions where /p Mask
+/// has a 1-bit, and zeros the remaining bits.
 ///
 /// Examples:
 /// (1) expandBits(i8 0b0000'1010, i8 0b1100'1100) = 0b1000'1000
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 24dbe7cdc6f75..9898bb2bfe30e 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3272,7 +3272,8 @@ APInt llvm::APIntOps::bitwiseParity(const APInt &V) {
 }
 
 APInt llvm::APIntOps::compressBits(const APInt &Val, const APInt &Mask) {
-  // See also Hacker's Delight 2nd Edition, §7–4 Compress, or Generalized 
Extract
+  // See also Hacker's Delight 2nd Edition,
+  // §7–4 Compress, or Generalized Extract
   unsigned BW = Val.getBitWidth();
   assert(BW == Mask.getBitWidth() && "Operand mismatch");
   APInt V = Val & Mask;
@@ -3290,7 +3291,8 @@ APInt llvm::APIntOps::compressBits(const APInt &Val, 
const APInt &Mask) {
 }
 
 APInt llvm::APIntOps::expandBits(const APInt &Val, const APInt &Mask) {
-  // See also Hacker's Delight 2nd Edition, §7–5 Expand, or Generalized Insert
+  // See also Hacker's Delight 2nd Edition,
+  // §7–5 Expand, or Generalized Insert
   unsigned BW = Val.getBitWidth();
   assert(BW == Mask.getBitWidth() && "Operand mismatch");
   SmallVector<APInt> MoveArray;
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 668bef67271e8..6e11dc342a3b1 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -4038,24 +4038,26 @@ TEST(APIntTest, compressBits) {
   EXPECT_EQ(
       APIntOps::compressBits(APInt(8, 0xFFU), APInt(8, 0xAAU)).getZExtValue(),
       15U);
-  EXPECT_EQ(
-      APIntOps::compressBits(APInt(8, 0xFFU), APInt(8, 0)).getZExtValue(), 0U);
-  EXPECT_EQ(APIntOps::compressBits(APInt(4, 0xFU), APInt(4, 
0xAU)).getZExtValue(),
-            3U);
-  EXPECT_EQ(APIntOps::compressBits(APInt(4, 0xAU), APInt(4, 
0xAU)).getZExtValue(),
-            3U);
-  EXPECT_EQ(APIntOps::compressBits(APInt(4, 0x5U), APInt(4, 
0xAU)).getZExtValue(),
+  EXPECT_EQ(APIntOps::compressBits(APInt(8, 0xFFU), APInt(8, 
0)).getZExtValue(),
             0U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(4, 0xFU), APInt(4, 0xAU)).getZExtValue(),
+      3U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(4, 0xAU), APInt(4, 0xAU)).getZExtValue(),
+      3U);
+  EXPECT_EQ(
+      APIntOps::compressBits(APInt(4, 0x5U), APInt(4, 0xAU)).getZExtValue(),
+      0U);
 }
 
 TEST(APIntTest, expandBits) {
   EXPECT_EQ(APIntOps::expandBits(APInt(8, 0), APInt(8, 0xAAU)).getZExtValue(),
             0U);
-  EXPECT_EQ(
-      APIntOps::expandBits(APInt(8, 15U), APInt(8, 0xAAU)).getZExtValue(),
-      0xAAU);
-  EXPECT_EQ(
-      APIntOps::expandBits(APInt(8, 0xFFU), APInt(8, 0)).getZExtValue(), 0U);
+  EXPECT_EQ(APIntOps::expandBits(APInt(8, 15U), APInt(8, 
0xAAU)).getZExtValue(),
+            0xAAU);
+  EXPECT_EQ(APIntOps::expandBits(APInt(8, 0xFFU), APInt(8, 0)).getZExtValue(),
+            0U);
   EXPECT_EQ(APIntOps::expandBits(APInt(4, 3U), APInt(4, 0xAU)).getZExtValue(),
             0xAU);
   EXPECT_EQ(APIntOps::expandBits(APInt(4, 1U), APInt(4, 0xAU)).getZExtValue(),

>From 6d534d191de3fd8152d7fbf5b4dfca5da20fd57e Mon Sep 17 00:00:00 2001
From: Eisenwave <[email protected]>
Date: Thu, 28 May 2026 19:12:01 +0200
Subject: [PATCH 3/4] Implement review comments

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 21 ++-------
 clang/lib/AST/ExprConstant.cpp           | 16 +------
 llvm/include/llvm/ADT/APInt.h            |  7 ---
 llvm/lib/Support/APInt.cpp               | 58 ++++--------------------
 llvm/unittests/ADT/APIntTest.cpp         |  8 ----
 5 files changed, 15 insertions(+), 95 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 8a53ae0937782..ce4e774e399d5 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -19,6 +19,7 @@
 #include "clang/Basic/Builtins.h"
 #include "clang/Basic/TargetBuiltins.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/APInt.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/AllocToken.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -5099,30 +5100,14 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, 
const CallExpr *Call,
   case clang::X86::BI__builtin_ia32_pdep_di:
     return interp__builtin_elementwise_int_binop(
         S, OpPC, Call, [](const APSInt &Val, const APSInt &Mask) {
-          unsigned BitWidth = Val.getBitWidth();
-          APInt Result = APInt::getZero(BitWidth);
-
-          for (unsigned I = 0, P = 0; I != BitWidth; ++I) {
-            if (Mask[I])
-              Result.setBitVal(I, Val[P++]);
-          }
-
-          return Result;
+          return llvm::APIntOps::expandBits(Val, Mask);
         });
 
   case clang::X86::BI__builtin_ia32_pext_si:
   case clang::X86::BI__builtin_ia32_pext_di:
     return interp__builtin_elementwise_int_binop(
         S, OpPC, Call, [](const APSInt &Val, const APSInt &Mask) {
-          unsigned BitWidth = Val.getBitWidth();
-          APInt Result = APInt::getZero(BitWidth);
-
-          for (unsigned I = 0, P = 0; I != BitWidth; ++I) {
-            if (Mask[I])
-              Result.setBitVal(P++, Val[I]);
-          }
-
-          return Result;
+          return llvm::APIntOps::compressBits(Val, Mask);
         });
 
   case clang::X86::BI__builtin_ia32_addcarryx_u32:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 0522d6f1dc944..013264a7c5bef 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -17855,13 +17855,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     if (!EvaluateInteger(E->getArg(0), Val, Info) ||
         !EvaluateInteger(E->getArg(1), Msk, Info))
       return false;
-
-    unsigned BitWidth = Val.getBitWidth();
-    APInt Result = APInt::getZero(BitWidth);
-    for (unsigned I = 0, P = 0; I != BitWidth; ++I)
-      if (Msk[I])
-        Result.setBitVal(I, Val[P++]);
-    return Success(Result, E);
+    return Success(llvm::APIntOps::expandBits(Val, Msk), E);
   }
 
   case clang::X86::BI__builtin_ia32_pext_si:
@@ -17870,13 +17864,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     if (!EvaluateInteger(E->getArg(0), Val, Info) ||
         !EvaluateInteger(E->getArg(1), Msk, Info))
       return false;
-
-    unsigned BitWidth = Val.getBitWidth();
-    APInt Result = APInt::getZero(BitWidth);
-    for (unsigned I = 0, P = 0; I != BitWidth; ++I)
-      if (Msk[I])
-        Result.setBitVal(P++, Val[I]);
-    return Success(Result, E);
+    return Success(llvm::APIntOps::compressBits(Val, Msk), E);
   }
   case X86::BI__builtin_ia32_ptestz128:
   case X86::BI__builtin_ia32_ptestz256:
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 8987b037ed47c..e8d806cf78578 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2489,13 +2489,6 @@ LLVM_ABI APInt clmulr(const APInt &LHS, const APInt 
&RHS);
 /// clmulh(a, b) = clmulr(a, b) >> 1
 LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS);
 
-/// Compute the parity (inclusive prefix XOR) for each bit in V.
-/// This is equivalent to clmul(V, ~0).
-///
-/// Example:
-/// bitwiseParity(i4 0b1010) = 0b0110
-LLVM_ABI APInt bitwiseParity(const APInt &V);
-
 /// Perform a "compress" operation, also known as pext or bext.
 ///
 /// Selects the bits from /p Val at the positions where /p Mask has a 1-bit,
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 9898bb2bfe30e..86b2eff7ada61 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -3260,60 +3260,22 @@ APInt llvm::APIntOps::clmulh(const APInt &LHS, const 
APInt &RHS) {
   return clmulr(LHS, RHS).lshr(1);
 }
 
-APInt llvm::APIntOps::bitwiseParity(const APInt &V) {
-  // This computes `clmul(V, ~0)`,
-  // but we don't use `clmul` because this special case has lower complexity
-  // (logarithmic amount of loop iterations instead of linear in BW).
-  unsigned BW = V.getBitWidth();
-  APInt Result = V;
-  for (unsigned I = 1; I < BW; I <<= 1)
-    Result ^= Result.shl(I);
-  return Result;
-}
-
 APInt llvm::APIntOps::compressBits(const APInt &Val, const APInt &Mask) {
-  // See also Hacker's Delight 2nd Edition,
-  // §7–4 Compress, or Generalized Extract
   unsigned BW = Val.getBitWidth();
   assert(BW == Mask.getBitWidth() && "Operand mismatch");
-  APInt V = Val & Mask;
-  APInt M = Mask;
-  APInt Mk = (~M).shl(1);
-  for (unsigned I = 1; I < BW; I <<= 1) {
-    APInt MkParity = bitwiseParity(Mk);
-    APInt Move = MkParity & M;
-    M = (M ^ Move) | Move.lshr(I);
-    APInt T = V & Move;
-    V = (V ^ T) | T.lshr(I);
-    Mk &= ~MkParity;
-  }
-  return V;
+  APInt Result = APInt::getZero(BW);
+  for (unsigned I = 0, P = 0; I != BW; ++I)
+    if (Mask[I])
+      Result.setBitVal(P++, Val[I]);
+  return Result;
 }
 
 APInt llvm::APIntOps::expandBits(const APInt &Val, const APInt &Mask) {
-  // See also Hacker's Delight 2nd Edition,
-  // §7–5 Expand, or Generalized Insert
   unsigned BW = Val.getBitWidth();
   assert(BW == Mask.getBitWidth() && "Operand mismatch");
-  SmallVector<APInt> MoveArray;
-  unsigned LogN = Log2_32_Ceil(BW);
-  MoveArray.reserve(LogN);
-
-  APInt M = Mask;
-  APInt Mk = (~M).shl(1);
-  for (unsigned I = 0; I < LogN; ++I) {
-    APInt MkParity = bitwiseParity(Mk);
-    APInt Move = MkParity & M;
-    M = (M ^ Move) | Move.lshr(1u << I);
-    MoveArray.push_back(std::move(Move));
-    Mk &= ~MkParity;
-  }
-
-  APInt V = Val;
-  for (unsigned I = LogN; I-- > 0;) {
-    APInt Move = MoveArray[I];
-    APInt T = V.shl(1u << I);
-    V = (V & ~Move) | (T & Move);
-  }
-  return V & Mask;
+  APInt Result = APInt::getZero(BW);
+  for (unsigned I = 0, P = 0; I != BW; ++I)
+    if (Mask[I])
+      Result.setBitVal(I, Val[P++]);
+  return Result;
 }
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 6e11dc342a3b1..a6cb34376d1cb 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -4018,14 +4018,6 @@ TEST(APIntTest, sqrt) {
             APInt(256, "340282366920938463463374607431768211456", 10));
 }
 
-TEST(APIntTest, bitwiseParity) {
-  EXPECT_EQ(APIntOps::bitwiseParity(APInt(8, 0)).getZExtValue(), 0U);
-  EXPECT_EQ(APIntOps::bitwiseParity(APInt(8, 1)).getZExtValue(), 0xFFU);
-  EXPECT_EQ(APIntOps::bitwiseParity(APInt(4, 0xAU)).getZExtValue(), 6U);
-  EXPECT_EQ(APIntOps::bitwiseParity(APInt(4, 0xFU)).getZExtValue(), 5U);
-  EXPECT_EQ(APIntOps::bitwiseParity(APInt(8, 0xAAU)).getZExtValue(), 0x66U);
-}
-
 TEST(APIntTest, compressBits) {
   EXPECT_EQ(APIntOps::compressBits(APInt(8, 0), APInt(8, 
0xAAU)).getZExtValue(),
             0U);

>From 53e85fd9fd7e536f80992a0fcea24d777d80d9ca Mon Sep 17 00:00:00 2001
From: Eisenwave <[email protected]>
Date: Fri, 29 May 2026 12:23:38 +0200
Subject: [PATCH 4/4] Resolve review comments

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index ce4e774e399d5..fee3d5e2d887a 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -19,7 +19,6 @@
 #include "clang/Basic/Builtins.h"
 #include "clang/Basic/TargetBuiltins.h"
 #include "clang/Basic/TargetInfo.h"
-#include "llvm/ADT/APInt.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/AllocToken.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -5098,17 +5097,13 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, 
const CallExpr *Call,
 
   case clang::X86::BI__builtin_ia32_pdep_si:
   case clang::X86::BI__builtin_ia32_pdep_di:
-    return interp__builtin_elementwise_int_binop(
-        S, OpPC, Call, [](const APSInt &Val, const APSInt &Mask) {
-          return llvm::APIntOps::expandBits(Val, Mask);
-        });
+    return interp__builtin_elementwise_int_binop(S, OpPC, Call,
+                                                 llvm::APIntOps::expandBits);
 
   case clang::X86::BI__builtin_ia32_pext_si:
   case clang::X86::BI__builtin_ia32_pext_di:
-    return interp__builtin_elementwise_int_binop(
-        S, OpPC, Call, [](const APSInt &Val, const APSInt &Mask) {
-          return llvm::APIntOps::compressBits(Val, Mask);
-        });
+    return interp__builtin_elementwise_int_binop(S, OpPC, Call,
+                                                 llvm::APIntOps::compressBits);
 
   case clang::X86::BI__builtin_ia32_addcarryx_u32:
   case clang::X86::BI__builtin_ia32_addcarryx_u64:

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to