[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
Thu Jul 27 10:58:42 PDT 2023
nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Consider the following statements:
long x = 1;
short y = 1;
With the following AST:
|-VarDecl 0x55d289973730 <x.c:1:1, col:10> col:6 x 'long' cinit
| `-ImplicitCastExpr 0x55d289973800 <col:10> 'long' <IntegralCast>
| `-IntegerLiteral 0x55d2899737e0 <col:10> 'int' 1
`-VarDecl 0x55d289973830 <line:2:1, col:11> col:7 y 'short' cinit
`-ImplicitCastExpr 0x55d2899738b8 <col:11> 'short' <IntegralCast>
`-IntegerLiteral 0x55d289973898 <col:11> 'int' 1
Sign extend or truncate based on the destination width.
Repository:
rG LLVM Github Monorepo
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,9 @@
// 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
+
// 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,22 @@
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 = CI->getValue().sextOrTrunc(DstWidth);
+ return llvm::ConstantInt::get(CGM.getLLVMContext(), A);
+ }
+ return nullptr;
+ }
+
case CK_Dependent: llvm_unreachable("saw dependent cast!");
case CK_BuiltinFnToFnPtr:
@@ -1191,7 +1207,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.544859.patch
Type: text/x-patch
Size: 1926 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230727/27919fd9/attachment.bin>
More information about the cfe-commits
mailing list