[PATCH] D156466: [clang][CGExprConstant] handle implicit widening/narrowing Int-to-Int casts
Nick Desaulniers via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 2 16:58:34 PDT 2023
nickdesaulniers updated this revision to Diff 546645.
nickdesaulniers marked an inline comment as done.
nickdesaulniers added a comment.
- add test case that would have caught SExt bug
- fix SExt bug
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D156466/new/
https://reviews.llvm.org/D156466
Files:
clang/lib/CodeGen/CGExprConstant.cpp
clang/test/CodeGen/global-init.c
Index: clang/test/CodeGen/global-init.c
===================================================================
--- clang/test/CodeGen/global-init.c
+++ clang/test/CodeGen/global-init.c
@@ -49,6 +49,11 @@
// CHECK: @yuv_types ={{.*}} global [4 x [6 x i8]] {{\[}}[6 x i8] c"4:0:0\00", [6 x i8] c"4:2:0\00", [6 x i8] c"4:2:2\00", [6 x i8] c"4:4:4\00"]
char yuv_types[4][6]= {"4:0:0","4:2:0","4:2:2","4:4:4"};
+unsigned long long x = -1000;
+// CHECK: @x ={{.*}} global i64 -1000
+unsigned long long uint_max = 4294967295u;
+// CHECK: @uint_max ={{.*}} global i64 4294967295
+
// NOTE: tentative definitions are processed at the end of the translation unit.
Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1139,6 +1139,24 @@
case CK_IntToOCLSampler:
llvm_unreachable("global sampler variables are not generated");
+ case CK_IntegralCast: {
+ QualType FromType = subExpr->getType();
+ // See also HandleIntToIntCast in ExprConstant.cpp
+ if (FromType->isIntegerType())
+ if (llvm::Constant *C = Visit(subExpr, FromType))
+ if (auto *CI = dyn_cast<llvm::ConstantInt>(C)) {
+ unsigned SrcWidth = CGM.getContext().getIntWidth(FromType);
+ unsigned DstWidth = CGM.getContext().getIntWidth(destType);
+ if (DstWidth == SrcWidth)
+ return CI;
+ llvm::APInt A = FromType->isSignedIntegerType()
+ ? CI->getValue().sextOrTrunc(DstWidth)
+ : CI->getValue().zextOrTrunc(DstWidth);
+ return llvm::ConstantInt::get(CGM.getLLVMContext(), A);
+ }
+ return nullptr;
+ }
+
case CK_Dependent: llvm_unreachable("saw dependent cast!");
case CK_BuiltinFnToFnPtr:
@@ -1191,7 +1209,6 @@
case CK_IntegralComplexToFloatingComplex:
case CK_PointerToIntegral:
case CK_PointerToBoolean:
- case CK_IntegralCast:
case CK_BooleanToSignedIntegral:
case CK_IntegralToPointer:
case CK_IntegralToBoolean:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156466.546645.patch
Type: text/x-patch
Size: 2161 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230802/871c3100/attachment.bin>
More information about the cfe-commits
mailing list