[clang] 5b4071c - [clang][bytecode] Explicitly truncate in IntegralAP::from() (#112683)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 17 03:26:48 PDT 2024


Author: Timm Baeder
Date: 2024-10-17T12:26:44+02:00
New Revision: 5b4071c7554ab4feeae4817e3d41013016308586

URL: https://github.com/llvm/llvm-project/commit/5b4071c7554ab4feeae4817e3d41013016308586
DIFF: https://github.com/llvm/llvm-project/commit/5b4071c7554ab4feeae4817e3d41013016308586.diff

LOG: [clang][bytecode] Explicitly truncate in IntegralAP::from() (#112683)

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

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Integral.h
    clang/lib/AST/ByteCode/IntegralAP.h

Removed: 
    


################################################################################
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) {


        


More information about the cfe-commits mailing list