https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/112683

Add Integral::toAPInt(), which truncates to the given BitWidth, similar to the 
toAPSInt() we already have.

>From eeef15e2121a4bfa520f93037b4f04eb1e3dffa1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com>
Date: Thu, 17 Oct 2024 11:28:50 +0200
Subject: [PATCH] [clang][bytecode] Explicitly truncate in IntegralAP::from()

Add Integral::toAPInt(), which truncates to the given BitWidth,
similar to the toAPSInt() we already have.
---
 clang/lib/AST/ByteCode/Integral.h   | 9 ++++++---
 clang/lib/AST/ByteCode/IntegralAP.h | 7 +------
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Integral.h 
b/clang/lib/AST/ByteCode/Integral.h
index e06ec1669259da..be537d22d5af1b 100644
--- a/clang/lib/AST/ByteCode/Integral.h
+++ b/clang/lib/AST/ByteCode/Integral.h
@@ -122,11 +122,14 @@ template <unsigned Bits, bool Signed> class Integral 
final {
   APSInt toAPSInt() const {
     return APSInt(APInt(Bits, static_cast<uint64_t>(V), Signed), !Signed);
   }
-  APSInt toAPSInt(unsigned NumBits) const {
+  APSInt toAPSInt(unsigned BitWidth) const { return APSInt(toAPInt(BitWidth)); 
}
+  APInt toAPInt(unsigned BitWidth) const {
     if constexpr (Signed)
-      return APSInt(toAPSInt().sextOrTrunc(NumBits), !Signed);
+      return APInt(Bits, static_cast<uint64_t>(V), Signed)
+          .sextOrTrunc(BitWidth);
     else
-      return APSInt(toAPSInt().zextOrTrunc(NumBits), !Signed);
+      return APInt(Bits, static_cast<uint64_t>(V), Signed)
+          .zextOrTrunc(BitWidth);
   }
   APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
 
diff --git a/clang/lib/AST/ByteCode/IntegralAP.h 
b/clang/lib/AST/ByteCode/IntegralAP.h
index 252d7243bee73e..f8aeaaca398fe8 100644
--- a/clang/lib/AST/ByteCode/IntegralAP.h
+++ b/clang/lib/AST/ByteCode/IntegralAP.h
@@ -112,12 +112,7 @@ template <bool Signed> class IntegralAP final {
 
   template <unsigned Bits, bool InputSigned>
   static IntegralAP from(Integral<Bits, InputSigned> I, unsigned BitWidth) {
-    // TODO: Avoid implicit trunc?
-    // See https://github.com/llvm/llvm-project/issues/112510.
-    APInt Copy = APInt(BitWidth, static_cast<uint64_t>(I), InputSigned,
-                       /*implicitTrunc=*/true);
-
-    return IntegralAP<Signed>(Copy);
+    return IntegralAP<Signed>(I.toAPInt(BitWidth));
   }
 
   static IntegralAP zero(int32_t BitWidth) {

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to