[clang] [C11] Implement WG14 N1285 (temporary lifetimes) (PR #133472)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 31 12:34:19 PDT 2025
https://github.com/AaronBallman updated https://github.com/llvm/llvm-project/pull/133472
>From f9268b3a331fd8caf2440d742a1f084c0f9648ce Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Fri, 28 Mar 2025 13:01:58 -0400
Subject: [PATCH 1/9] [C11] Implement WG14 N1285 (temporary lifetimes)
This feature largely models the same behavior as in C++11. It is
technically a breaking change between C99 and C11, so the paper is not
being backported to older language modes.
One difference between C++ and C is that things which are rvalues in C
are often lvalues in C++ (such as the result of a ternary operator or a
comma operator).
---
clang/docs/ReleaseNotes.rst | 9 +
clang/lib/CodeGen/CGExpr.cpp | 4 +-
clang/lib/Sema/Sema.cpp | 5 +-
clang/lib/Sema/SemaInit.cpp | 2 +-
clang/test/C/C11/n1285.c | 83 ++++++--
clang/test/C/C11/n1285_1.c | 212 +++++++++++++++++++
clang/test/CodeGenObjC/property-array-type.m | 5 +-
clang/www/c_status.html | 2 +-
8 files changed, 301 insertions(+), 21 deletions(-)
create mode 100644 clang/test/C/C11/n1285_1.c
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b96780cec75d9..c51b3c44f3b3b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -160,6 +160,15 @@ C23 Feature Support
treated as if the compound literal were within the body rather than at file
scope.
+C11 Feature Support
+^^^^^^^^^^^^^^^^^^^
+- Implemented `WG14 N1285 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1285.htm>`_
+ which introduces the notion of objects with a temporary lifetime. When an
+ expression resulting in an rvalue with structure or union type and that type
+ contains a member of array type, the expression result is an automatic storage
+ duration object with temporary lifetime which begins when the expression is
+ evaluated and ends at the evaluation of the containing full expression.
+
Non-comprehensive list of changes in this release
-------------------------------------------------
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 5943ff9294e1a..2328ad0def736 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -387,8 +387,8 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
if (const RecordType *RT =
E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
// Get the destructor for the reference temporary.
- auto *ClassDecl = cast<CXXRecordDecl>(RT->getDecl());
- if (!ClassDecl->hasTrivialDestructor())
+ if (auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl());
+ ClassDecl && !ClassDecl->hasTrivialDestructor())
ReferenceTemporaryDtor = ClassDecl->getDestructor();
}
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 93a2d797679d4..4b573abb8efba 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -741,10 +741,11 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
if (Kind == CK_ArrayToPointerDecay) {
// C++1z [conv.array]: The temporary materialization conversion is applied.
// We also use this to fuel C++ DR1213, which applies to C++11 onwards.
- if (getLangOpts().CPlusPlus && E->isPRValue()) {
+ if ((getLangOpts().C11 || getLangOpts().CPlusPlus) && E->isPRValue()) {
// The temporary is an lvalue in C++98 and an xvalue otherwise.
ExprResult Materialized = CreateMaterializeTemporaryExpr(
- E->getType(), E, !getLangOpts().CPlusPlus11);
+ E->getType(), E,
+ getLangOpts().CPlusPlus && !getLangOpts().CPlusPlus11);
if (Materialized.isInvalid())
return ExprError();
E = Materialized.get();
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 9814c3f456f0d..c47ea71cbafb5 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -7654,7 +7654,7 @@ ExprResult Sema::TemporaryMaterializationConversion(Expr *E) {
// FIXME: This means that AST consumers need to deal with "prvalues" that
// denote materialized temporaries. Maybe we should add another ValueKind
// for "xvalue pretending to be a prvalue" for C++98 support.
- if (!E->isPRValue() || !getLangOpts().CPlusPlus11)
+ if (!E->isPRValue() || (!getLangOpts().CPlusPlus11 && !getLangOpts().C11))
return E;
// C++1z [conv.rval]/1: T shall be a complete type.
diff --git a/clang/test/C/C11/n1285.c b/clang/test/C/C11/n1285.c
index e7a9463e68103..c4fb199e33ae5 100644
--- a/clang/test/C/C11/n1285.c
+++ b/clang/test/C/C11/n1285.c
@@ -1,24 +1,81 @@
-// RUN: %clang_cc1 -verify=wrong -std=c99 %s
-// RUN: %clang_cc1 -verify=wrong -std=c11 %s
-// RUN: %clang_cc1 -verify=cpp -std=c++11 -x c++ %s
+// RUN: %clang_cc1 -fsyntax-only -verify=good -std=c99 %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,c -std=c11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,cpp -std=c++11 -x c++ %s
-/* WG14 N1285: No
+/* WG14 N1285: Clang 21
* Extending the lifetime of temporary objects (factored approach)
*
- * NB: we do not properly materialize temporary expressions in situations where
- * it would be expected; that is why the "no-diagnostics" marking is named
- * "wrong". We do issue the expected diagnostic in C++ mode.
+ * This paper introduced the notion of an object with a temporary lifetime. Any
+ * operation resulting in an rvalue of structure or union type which contains
+ * an array results in an object with temporary lifetime.
*/
-// wrong-no-diagnostics
+// good-no-diagnostics
+
+// C11 6.2.4p8: A non-lvalue expression with structure or union type, where the
+// structure or union contains a member with array type (including,
+// recursively, members of all contained structures and unions) refers to an
+// object with automatic storage duration and temporary lifetime. Its lifetime
+// begins when the expression is evaluated and its initial value is the value
+// of the expression. Its lifetime ends when the evaluation of the containing
+// full expression or full declarator ends. Any attempt to modify an object
+// with temporary lifetime results in undefined behavior.
struct X { int a[5]; };
struct X f(void);
-int foo(void) {
- // FIXME: This diagnostic should be issued in C11 as well (though not in C99,
- // as this paper was a breaking change between C99 and C11).
- int *p = f().a; // cpp-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
- return *p;
+union U { int a[10]; double d; };
+union U g(void);
+
+void sink(int *);
+
+int func_return(void) {
+ int *p = f().a; // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
+ p = f().a; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
+ p = g().a; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
+ sink(f().a); // Ok
+ return *f().a; // Ok
+}
+
+int ternary(void) {
+ int *p = (1 ? (struct X){ 0 } : f()).a; // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
+ int *r = (1 ? (union U){ 0 } : g()).a; // expected-warning {{temporary whose address is used as value of local variable 'r' will be destroyed at the end of the full-expression}}
+ p = (1 ? (struct X){ 0 } : f()).a; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
+ sink((1 ? (struct X){ 0 } : f()).a); // Ok
+
+ // This intentionally gets one diagnostic in C and two in C++. In C, the
+ // compound literal results in an lvalue, not an rvalue as it does in C++. So
+ // only one branch results in a temporary in C but both branches do in C++.
+ int *q = 1 ? (struct X){ 0 }.a : f().a; // expected-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}} \
+ cpp-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}}
+ q = 1 ? (struct X){ 0 }.a : f().a; // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}} \
+ cpp-warning {{object backing the pointer q will be destroyed at the end of the full-expression}}
+ q = 1 ? (struct X){ 0 }.a : g().a; // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}} \
+ cpp-warning {{object backing the pointer q will be destroyed at the end of the full-expression}}
+ sink(1 ? (struct X){ 0 }.a : f().a); // Ok
+ return *(1 ? (struct X){ 0 }.a : f().a); // Ok
}
+int comma(void) {
+ struct X x;
+ int *p = ((void)0, x).a; // c-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
+ p = ((void)0, x).a; // c-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
+ sink(((void)0, x).a); // Ok
+ return *(((void)0, x).a);// Ok
+}
+
+int cast(void) {
+ struct X x;
+ int *p = ((struct X)x).a; // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
+ p = ((struct X)x).a; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
+ sink(((struct X)x).a); // Ok
+ return *(((struct X)x).a); // Ok
+}
+
+int assign(void) {
+ struct X x, s;
+ int *p = (x = s).a; // c-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
+ p = (x = s).a; // c-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
+ sink((x = s).a); // Ok
+ return *((x = s).a); // Ok
+}
diff --git a/clang/test/C/C11/n1285_1.c b/clang/test/C/C11/n1285_1.c
new file mode 100644
index 0000000000000..6a3df58e5a6a0
--- /dev/null
+++ b/clang/test/C/C11/n1285_1.c
@@ -0,0 +1,212 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
+// RUN: %clang_cc1 -std=c99 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C99
+// RUN: %clang_cc1 -std=c11 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C11
+
+// Ensure that codegen for temporary lifetimes makes sense.
+
+struct X { int a[5]; };
+struct X f(void);
+
+// C99-LABEL: define dso_local i32 @func_return(
+// C99-SAME: ) #[[ATTR0:[0-9]+]] {
+// C99-NEXT: [[ENTRY:.*:]]
+// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C99-NEXT: [[TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C99-NEXT: call void @f(ptr dead_on_unwind writable sret([[STRUCT_X]]) align 4 [[TMP]])
+// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[TMP]], i32 0, i32 0
+// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C99-NEXT: ret i32 [[TMP1]]
+//
+// C11-LABEL: define dso_local i32 @func_return(
+// C11-SAME: ) #[[ATTR0:[0-9]+]] {
+// C11-NEXT: [[ENTRY:.*:]]
+// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-NEXT: call void @f(ptr dead_on_unwind writable sret([[STRUCT_X]]) align 4 [[REF_TMP]])
+// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C11-NEXT: ret i32 [[TMP1]]
+//
+int func_return(void) {
+ int *p = f().a;
+ return *p;
+}
+
+// C99-LABEL: define dso_local i32 @ternary(
+// C99-SAME: ) #[[ATTR0]] {
+// C99-NEXT: [[ENTRY:.*:]]
+// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C99-NEXT: [[TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C99-NEXT: [[Q:%.*]] = alloca ptr, align 8
+// C99-NEXT: [[DOTCOMPOUNDLITERAL:%.*]] = alloca [[STRUCT_X]], align 4
+// C99-NEXT: br i1 true, label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
+// C99: [[COND_TRUE]]:
+// C99-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[TMP]], i8 0, i64 20, i1 false)
+// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[TMP]], i32 0, i32 0
+// C99-NEXT: br label %[[COND_END:.*]]
+// C99: [[COND_FALSE]]:
+// C99-NEXT: call void @f(ptr dead_on_unwind writable sret([[STRUCT_X]]) align 4 [[TMP]])
+// C99-NEXT: br label %[[COND_END]]
+// C99: [[COND_END]]:
+// C99-NEXT: [[A1:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[TMP]], i32 0, i32 0
+// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A1]], i64 0, i64 0
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C99-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[DOTCOMPOUNDLITERAL]], i8 0, i64 20, i1 false)
+// C99-NEXT: [[A2:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
+// C99-NEXT: [[A3:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
+// C99-NEXT: [[ARRAYDECAY4:%.*]] = getelementptr inbounds [5 x i32], ptr [[A3]], i64 0, i64 0
+// C99-NEXT: store ptr [[ARRAYDECAY4]], ptr [[Q]], align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C99-NEXT: [[TMP2:%.*]] = load ptr, ptr [[Q]], align 8
+// C99-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
+// C99-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP3]]
+// C99-NEXT: ret i32 [[ADD]]
+//
+// C11-LABEL: define dso_local i32 @ternary(
+// C11-SAME: ) #[[ATTR0]] {
+// C11-NEXT: [[ENTRY:.*:]]
+// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-NEXT: [[Q:%.*]] = alloca ptr, align 8
+// C11-NEXT: [[DOTCOMPOUNDLITERAL:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-NEXT: br i1 true, label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
+// C11: [[COND_TRUE]]:
+// C11-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[REF_TMP]], i8 0, i64 20, i1 false)
+// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-NEXT: br label %[[COND_END:.*]]
+// C11: [[COND_FALSE]]:
+// C11-NEXT: call void @f(ptr dead_on_unwind writable sret([[STRUCT_X]]) align 4 [[REF_TMP]])
+// C11-NEXT: br label %[[COND_END]]
+// C11: [[COND_END]]:
+// C11-NEXT: [[A1:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A1]], i64 0, i64 0
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C11-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[DOTCOMPOUNDLITERAL]], i8 0, i64 20, i1 false)
+// C11-NEXT: [[A2:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
+// C11-NEXT: [[A3:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
+// C11-NEXT: [[ARRAYDECAY4:%.*]] = getelementptr inbounds [5 x i32], ptr [[A3]], i64 0, i64 0
+// C11-NEXT: store ptr [[ARRAYDECAY4]], ptr [[Q]], align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C11-NEXT: [[TMP2:%.*]] = load ptr, ptr [[Q]], align 8
+// C11-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
+// C11-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP3]]
+// C11-NEXT: ret i32 [[ADD]]
+//
+int ternary(void) {
+ int *p;
+ p = (1 ? (struct X){ 0 } : f()).a;
+ int *q = 1 ? (struct X){ 0 }.a : f().a;
+
+ return *p + *q;
+}
+
+// C99-LABEL: define dso_local i32 @comma(
+// C99-SAME: ) #[[ATTR0]] {
+// C99-NEXT: [[ENTRY:.*:]]
+// C99-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[X]], i32 0, i32 0
+// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C99-NEXT: ret i32 [[TMP1]]
+//
+// C11-LABEL: define dso_local i32 @comma(
+// C11-SAME: ) #[[ATTR0]] {
+// C11-NEXT: [[ENTRY:.*:]]
+// C11-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false)
+// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C11-NEXT: ret i32 [[TMP1]]
+//
+int comma(void) {
+ struct X x;
+ int *p = ((void)0, x).a;
+ return *p;
+}
+
+// C99-LABEL: define dso_local i32 @cast(
+// C99-SAME: ) #[[ATTR0]] {
+// C99-NEXT: [[ENTRY:.*:]]
+// C99-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[X]], i32 0, i32 0
+// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C99-NEXT: ret i32 [[TMP1]]
+//
+// C11-LABEL: define dso_local i32 @cast(
+// C11-SAME: ) #[[ATTR0]] {
+// C11-NEXT: [[ENTRY:.*:]]
+// C11-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false)
+// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C11-NEXT: ret i32 [[TMP1]]
+//
+int cast(void) {
+ struct X x;
+ int *p;
+ p = ((struct X)x).a;
+ return *p;
+}
+
+// C99-LABEL: define dso_local i32 @assign(
+// C99-SAME: ) #[[ATTR0]] {
+// C99-NEXT: [[ENTRY:.*:]]
+// C99-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C99-NEXT: [[S:%.*]] = alloca [[STRUCT_X]], align 4
+// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C99-NEXT: [[TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C99-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[X]], ptr align 4 [[S]], i64 20, i1 false)
+// C99-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[TMP]], ptr align 4 [[X]], i64 20, i1 false)
+// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[TMP]], i32 0, i32 0
+// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C99-NEXT: ret i32 [[TMP1]]
+//
+// C11-LABEL: define dso_local i32 @assign(
+// C11-SAME: ) #[[ATTR0]] {
+// C11-NEXT: [[ENTRY:.*:]]
+// C11-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-NEXT: [[S:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
+// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[X]], ptr align 4 [[S]], i64 20, i1 false)
+// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false)
+// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
+// C11-NEXT: ret i32 [[TMP1]]
+//
+int assign(void) {
+ struct X x, s;
+ int *p = (x = s).a;
+ return *p;
+}
diff --git a/clang/test/CodeGenObjC/property-array-type.m b/clang/test/CodeGenObjC/property-array-type.m
index a0ad8db2630fa..3bb1e3c63334e 100644
--- a/clang/test/CodeGenObjC/property-array-type.m
+++ b/clang/test/CodeGenObjC/property-array-type.m
@@ -23,7 +23,8 @@ - (void)viewDidLoad {
}
@end
-// CHECK: [[M:%.*]] = getelementptr inbounds nuw %struct._GLKMatrix4, ptr [[TMP:%.*]], i32 0, i32 0
+// CHECK: [[REF_TEMP:%.*]] = alloca %struct._GLKMatrix4, align 4
+// CHECK: [[M:%.*]] = getelementptr inbounds nuw %struct._GLKMatrix4, ptr [[REF_TEMP:%.*]], i32 0, i32 0
// CHECK: [[ARRAYDECAY:%.*]] = getelementptr inbounds [16 x float], ptr [[M]], i64 0, i64 0
// CHECK: [[SIX:%.*]] = load ptr, ptr @OBJC_SELECTOR_REFERENCES
-// CHECK: call void @objc_msgSend(ptr noundef [[SEVEN:%.*]], ptr noundef [[SIX]], ptr noundef [[ARRAYDECAY]])
+// CHECK: call void @objc_msgSend(ptr noundef [[SEVEN:%.*]], ptr noundef [[SIX]], ptr noundef [[ARRAYDECAY]])
diff --git a/clang/www/c_status.html b/clang/www/c_status.html
index f4f00ac6dd808..0a5de40efe6dc 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -941,7 +941,7 @@ <h2 id="c11">C11 implementation status</h2>
<tr>
<td>Extending the lifetime of temporary objects (factored approach)</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1285.htm">N1285</a></td>
- <td class="none" align="center">No</td>
+ <td class="unreleased" align="center">Clang 21</td>
</tr>
<tr>
<td>Requiring signed char to have no padding bits</td>
>From 54a7a06472d767ffac6ff6730ba8349e0a5a88e4 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 07:42:37 -0400
Subject: [PATCH 2/9] Fix codegen
---
clang/lib/CodeGen/CGDecl.cpp | 11 +++-
clang/lib/CodeGen/CGExpr.cpp | 86 ++++++++++++++---------------
clang/lib/CodeGen/CodeGenFunction.h | 2 +
clang/test/CodeGen/xcore-abi.c | 3 +-
4 files changed, 53 insertions(+), 49 deletions(-)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index eab1ebfb2369b..3ba0b970554a6 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2269,11 +2269,18 @@ void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
cleanupKind & EHCleanup);
}
+ void CodeGenFunction::pushLifetimeExtendedDestroy(
+ QualType::DestructionKind dtorKind, Address addr, QualType type) {
+ CleanupKind cleanupKind = getCleanupKind(dtorKind);
+ pushLifetimeExtendedDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
+ cleanupKind & EHCleanup);
+}
+
void CodeGenFunction::pushDestroy(CleanupKind cleanupKind, Address addr,
QualType type, Destroyer *destroyer,
bool useEHCleanupForArray) {
- pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
- destroyer, useEHCleanupForArray);
+ pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
+ useEHCleanupForArray);
}
// Pushes a destroy and defers its deactivation until its
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index 2328ad0def736..1066d4f79718b 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -383,55 +383,49 @@ pushTemporaryCleanup(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M,
}
}
- CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
- if (const RecordType *RT =
- E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
- // Get the destructor for the reference temporary.
- if (auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl());
- ClassDecl && !ClassDecl->hasTrivialDestructor())
- ReferenceTemporaryDtor = ClassDecl->getDestructor();
- }
+ QualType::DestructionKind DK = E->getType().isDestructedType();
+ if (DK != QualType::DK_none) {
+ switch (M->getStorageDuration()) {
+ case SD_Static:
+ case SD_Thread: {
+ CXXDestructorDecl *ReferenceTemporaryDtor = nullptr;
+ if (const RecordType *RT =
+ E->getType()->getBaseElementTypeUnsafe()->getAs<RecordType>()) {
+ // Get the destructor for the reference temporary.
+ if (auto *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl());
+ ClassDecl && !ClassDecl->hasTrivialDestructor())
+ ReferenceTemporaryDtor = ClassDecl->getDestructor();
+ }
- if (!ReferenceTemporaryDtor)
- return;
+ if (!ReferenceTemporaryDtor)
+ return;
- // Call the destructor for the temporary.
- switch (M->getStorageDuration()) {
- case SD_Static:
- case SD_Thread: {
- llvm::FunctionCallee CleanupFn;
- llvm::Constant *CleanupArg;
- if (E->getType()->isArrayType()) {
- CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper(
- ReferenceTemporary, E->getType(),
- CodeGenFunction::destroyCXXObject, CGF.getLangOpts().Exceptions,
- dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
- CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
- } else {
- CleanupFn = CGF.CGM.getAddrAndTypeOfCXXStructor(
- GlobalDecl(ReferenceTemporaryDtor, Dtor_Complete));
- CleanupArg = cast<llvm::Constant>(ReferenceTemporary.emitRawPointer(CGF));
+ llvm::FunctionCallee CleanupFn;
+ llvm::Constant *CleanupArg;
+ if (E->getType()->isArrayType()) {
+ CleanupFn = CodeGenFunction(CGF.CGM).generateDestroyHelper(
+ ReferenceTemporary, E->getType(), CodeGenFunction::destroyCXXObject,
+ CGF.getLangOpts().Exceptions,
+ dyn_cast_or_null<VarDecl>(M->getExtendingDecl()));
+ CleanupArg = llvm::Constant::getNullValue(CGF.Int8PtrTy);
+ } else {
+ CleanupFn = CGF.CGM.getAddrAndTypeOfCXXStructor(
+ GlobalDecl(ReferenceTemporaryDtor, Dtor_Complete));
+ CleanupArg =
+ cast<llvm::Constant>(ReferenceTemporary.emitRawPointer(CGF));
+ }
+ CGF.CGM.getCXXABI().registerGlobalDtor(
+ CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
+ } break;
+ case SD_FullExpression:
+ CGF.pushDestroy(DK, ReferenceTemporary, E->getType());
+ break;
+ case SD_Automatic:
+ CGF.pushLifetimeExtendedDestroy(DK, ReferenceTemporary, E->getType());
+ break;
+ case SD_Dynamic:
+ llvm_unreachable("temporary cannot have dynamic storage duration");
}
- CGF.CGM.getCXXABI().registerGlobalDtor(
- CGF, *cast<VarDecl>(M->getExtendingDecl()), CleanupFn, CleanupArg);
- break;
- }
-
- case SD_FullExpression:
- CGF.pushDestroy(NormalAndEHCleanup, ReferenceTemporary, E->getType(),
- CodeGenFunction::destroyCXXObject,
- CGF.getLangOpts().Exceptions);
- break;
-
- case SD_Automatic:
- CGF.pushLifetimeExtendedDestroy(NormalAndEHCleanup,
- ReferenceTemporary, E->getType(),
- CodeGenFunction::destroyCXXObject,
- CGF.getLangOpts().Exceptions);
- break;
-
- case SD_Dynamic:
- llvm_unreachable("temporary cannot have dynamic storage duration");
}
}
diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
index ca00a0e8c6cf4..12425d817b5f0 100644
--- a/clang/lib/CodeGen/CodeGenFunction.h
+++ b/clang/lib/CodeGen/CodeGenFunction.h
@@ -2273,6 +2273,8 @@ class CodeGenFunction : public CodeGenTypeCache {
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr,
QualType type, Destroyer *destroyer,
bool useEHCleanupForArray);
+ void pushLifetimeExtendedDestroy(QualType::DestructionKind dtorKind,
+ Address addr, QualType type);
void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
llvm::Value *CompletePtr,
QualType ElementType);
diff --git a/clang/test/CodeGen/xcore-abi.c b/clang/test/CodeGen/xcore-abi.c
index 40e2f418f7304..5a6849cd2e2e4 100644
--- a/clang/test/CodeGen/xcore-abi.c
+++ b/clang/test/CodeGen/xcore-abi.c
@@ -77,7 +77,8 @@ void testva (int n, ...) {
// CHECK: call void @f(ptr noundef [[V5]])
// an unusual aggregate type
- int* v6 = va_arg (ap, int[4]); // expected-warning{{second argument to 'va_arg' is of array type 'int[4]'}}
+ int* v6 = va_arg (ap, int[4]); // expected-warning{{second argument to 'va_arg' is of array type 'int[4]'}} \
+ expected-warning{{temporary whose address is used as value of local variable 'v6' will be destroyed at the end of the full-expression}}
f(v6);
// CHECK: [[I:%[a-z0-9]+]] = load ptr, ptr [[AP]]
// CHECK: [[P:%[a-z0-9]+]] = load ptr, ptr [[I]]
>From 22b87ac21f924975e1ec9d3c8db74019e58008b4 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 07:58:26 -0400
Subject: [PATCH 3/9] Fix formatting; NFC
---
clang/lib/CodeGen/CGDecl.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 3ba0b970554a6..853f448c387a5 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2269,7 +2269,7 @@ void CodeGenFunction::pushDestroy(QualType::DestructionKind dtorKind,
cleanupKind & EHCleanup);
}
- void CodeGenFunction::pushLifetimeExtendedDestroy(
+void CodeGenFunction::pushLifetimeExtendedDestroy(
QualType::DestructionKind dtorKind, Address addr, QualType type) {
CleanupKind cleanupKind = getCleanupKind(dtorKind);
pushLifetimeExtendedDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
>From 15b3f7b1bedb48dc2428ce4ec40488e3784efdf5 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 08:23:01 -0400
Subject: [PATCH 4/9] Back out unnecessary changes
---
clang/lib/Sema/Sema.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 4b573abb8efba..daf48e84f1413 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -741,11 +741,11 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
if (Kind == CK_ArrayToPointerDecay) {
// C++1z [conv.array]: The temporary materialization conversion is applied.
// We also use this to fuel C++ DR1213, which applies to C++11 onwards.
- if ((getLangOpts().C11 || getLangOpts().CPlusPlus) && E->isPRValue()) {
+ if (getLangOpts().CPlusPlus && E->isPRValue()) {
// The temporary is an lvalue in C++98 and an xvalue otherwise.
ExprResult Materialized = CreateMaterializeTemporaryExpr(
E->getType(), E,
- getLangOpts().CPlusPlus && !getLangOpts().CPlusPlus11);
+ !getLangOpts().CPlusPlus11);
if (Materialized.isInvalid())
return ExprError();
E = Materialized.get();
>From 94b396c86549f778f35fcb6100639a4de15d873d Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 08:31:32 -0400
Subject: [PATCH 5/9] Speculative fix for codegen test
---
clang/test/CodeGen/xcore-abi.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CodeGen/xcore-abi.c b/clang/test/CodeGen/xcore-abi.c
index 5a6849cd2e2e4..9f7026648d036 100644
--- a/clang/test/CodeGen/xcore-abi.c
+++ b/clang/test/CodeGen/xcore-abi.c
@@ -27,7 +27,7 @@ void testva (int n, ...) {
va_start(ap,n);
// CHECK: [[AP:%[a-z0-9]+]] = alloca ptr, align 4
// CHECK: [[V5:%[a-z0-9]+]] = alloca %struct.x, align 4
- // CHECK: [[TMP:%[a-z0-9]+]] = alloca [4 x i32], align 4
+ // CHECK: [[TMP:%[a-z0-9\.]+]] = alloca [4 x i32], align 4
// CHECK: call void @llvm.va_start.p0(ptr [[AP]])
char* v1 = va_arg (ap, char*);
>From 59aa0a782c5253b10eb935aab1eeba4609cdc24d Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 08:58:17 -0400
Subject: [PATCH 6/9] Fix formatting, fix test
---
clang/lib/Sema/Sema.cpp | 3 +--
clang/test/CodeGen/xcore-abi.c | 3 +--
2 files changed, 2 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index daf48e84f1413..93a2d797679d4 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -744,8 +744,7 @@ ExprResult Sema::ImpCastExprToType(Expr *E, QualType Ty,
if (getLangOpts().CPlusPlus && E->isPRValue()) {
// The temporary is an lvalue in C++98 and an xvalue otherwise.
ExprResult Materialized = CreateMaterializeTemporaryExpr(
- E->getType(), E,
- !getLangOpts().CPlusPlus11);
+ E->getType(), E, !getLangOpts().CPlusPlus11);
if (Materialized.isInvalid())
return ExprError();
E = Materialized.get();
diff --git a/clang/test/CodeGen/xcore-abi.c b/clang/test/CodeGen/xcore-abi.c
index 9f7026648d036..d06556b9da858 100644
--- a/clang/test/CodeGen/xcore-abi.c
+++ b/clang/test/CodeGen/xcore-abi.c
@@ -77,8 +77,7 @@ void testva (int n, ...) {
// CHECK: call void @f(ptr noundef [[V5]])
// an unusual aggregate type
- int* v6 = va_arg (ap, int[4]); // expected-warning{{second argument to 'va_arg' is of array type 'int[4]'}} \
- expected-warning{{temporary whose address is used as value of local variable 'v6' will be destroyed at the end of the full-expression}}
+ int* v6 = va_arg (ap, int[4]); // expected-warning{{second argument to 'va_arg' is of array type 'int[4]'}}
f(v6);
// CHECK: [[I:%[a-z0-9]+]] = load ptr, ptr [[AP]]
// CHECK: [[P:%[a-z0-9]+]] = load ptr, ptr [[I]]
>From c3a83660ffd8f3e9a8ad336008318f6daa2bde86 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 09:46:13 -0400
Subject: [PATCH 7/9] Add codegen test with lifetime markers
---
clang/test/C/C11/n1285_1.c | 110 +++++++++++++++++++++++++++----------
1 file changed, 82 insertions(+), 28 deletions(-)
diff --git a/clang/test/C/C11/n1285_1.c b/clang/test/C/C11/n1285_1.c
index 6a3df58e5a6a0..bac87c1b48ad5 100644
--- a/clang/test/C/C11/n1285_1.c
+++ b/clang/test/C/C11/n1285_1.c
@@ -1,6 +1,7 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
// RUN: %clang_cc1 -std=c99 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C99
// RUN: %clang_cc1 -std=c11 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C11
+// RUN: %clang_cc1 -std=c11 -O2 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C11-O2
// Ensure that codegen for temporary lifetimes makes sense.
@@ -33,6 +34,16 @@ struct X f(void);
// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C11-NEXT: ret i32 [[TMP1]]
//
+// C11-O2-LABEL: define dso_local i32 @func_return(
+// C11-O2-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// C11-O2-NEXT: [[ENTRY:.*:]]
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4:[0-9]+]]
+// C11-O2-NEXT: call void @f(ptr dead_on_unwind nonnull writable sret([[STRUCT_X]]) align 4 [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3:![0-9]+]]
+// C11-O2-NEXT: ret i32 [[TMP0]]
+//
int func_return(void) {
int *p = f().a;
return *p;
@@ -41,7 +52,6 @@ int func_return(void) {
// C99-LABEL: define dso_local i32 @ternary(
// C99-SAME: ) #[[ATTR0]] {
// C99-NEXT: [[ENTRY:.*:]]
-// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
// C99-NEXT: [[TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
// C99-NEXT: [[Q:%.*]] = alloca ptr, align 8
// C99-NEXT: [[DOTCOMPOUNDLITERAL:%.*]] = alloca [[STRUCT_X]], align 4
@@ -56,13 +66,13 @@ int func_return(void) {
// C99: [[COND_END]]:
// C99-NEXT: [[A1:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[TMP]], i32 0, i32 0
// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A1]], i64 0, i64 0
-// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
// C99-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[DOTCOMPOUNDLITERAL]], i8 0, i64 20, i1 false)
// C99-NEXT: [[A2:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
// C99-NEXT: [[A3:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
// C99-NEXT: [[ARRAYDECAY4:%.*]] = getelementptr inbounds [5 x i32], ptr [[A3]], i64 0, i64 0
// C99-NEXT: store ptr [[ARRAYDECAY4]], ptr [[Q]], align 8
-// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C99-NEXT: [[TMP2:%.*]] = load ptr, ptr [[Q]], align 8
// C99-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
@@ -72,7 +82,6 @@ int func_return(void) {
// C11-LABEL: define dso_local i32 @ternary(
// C11-SAME: ) #[[ATTR0]] {
// C11-NEXT: [[ENTRY:.*:]]
-// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
// C11-NEXT: [[Q:%.*]] = alloca ptr, align 8
// C11-NEXT: [[DOTCOMPOUNDLITERAL:%.*]] = alloca [[STRUCT_X]], align 4
@@ -87,21 +96,31 @@ int func_return(void) {
// C11: [[COND_END]]:
// C11-NEXT: [[A1:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A1]], i64 0, i64 0
-// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
// C11-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[DOTCOMPOUNDLITERAL]], i8 0, i64 20, i1 false)
// C11-NEXT: [[A2:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
// C11-NEXT: [[A3:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
// C11-NEXT: [[ARRAYDECAY4:%.*]] = getelementptr inbounds [5 x i32], ptr [[A3]], i64 0, i64 0
// C11-NEXT: store ptr [[ARRAYDECAY4]], ptr [[Q]], align 8
-// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C11-NEXT: [[TMP2:%.*]] = load ptr, ptr [[Q]], align 8
// C11-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4
// C11-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP3]]
// C11-NEXT: ret i32 [[ADD]]
//
+// C11-O2-LABEL: define dso_local i32 @ternary(
+// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3:[0-9]+]] {
+// C11-O2-NEXT: [[ENTRY:.*:]]
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7:![0-9]+]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
+// C11-O2-NEXT: ret i32 [[TMP0]]
+//
int ternary(void) {
- int *p;
+ extern int *p;
p = (1 ? (struct X){ 0 } : f()).a;
int *q = 1 ? (struct X){ 0 }.a : f().a;
@@ -112,11 +131,10 @@ int ternary(void) {
// C99-SAME: ) #[[ATTR0]] {
// C99-NEXT: [[ENTRY:.*:]]
// C99-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[X]], i32 0, i32 0
// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
-// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
-// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C99-NEXT: ret i32 [[TMP1]]
//
@@ -124,19 +142,29 @@ int ternary(void) {
// C11-SAME: ) #[[ATTR0]] {
// C11-NEXT: [[ENTRY:.*:]]
// C11-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false)
// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
-// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
-// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C11-NEXT: ret i32 [[TMP1]]
//
+// C11-O2-LABEL: define dso_local i32 @comma(
+// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3]] {
+// C11-O2-NEXT: [[ENTRY:.*:]]
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
+// C11-O2-NEXT: ret i32 [[TMP0]]
+//
int comma(void) {
struct X x;
- int *p = ((void)0, x).a;
+ extern int *p;
+ p = ((void)0, x).a;
return *p;
}
@@ -144,11 +172,10 @@ int comma(void) {
// C99-SAME: ) #[[ATTR0]] {
// C99-NEXT: [[ENTRY:.*:]]
// C99-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[X]], i32 0, i32 0
// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
-// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
-// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C99-NEXT: ret i32 [[TMP1]]
//
@@ -156,19 +183,28 @@ int comma(void) {
// C11-SAME: ) #[[ATTR0]] {
// C11-NEXT: [[ENTRY:.*:]]
// C11-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false)
// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
-// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
-// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C11-NEXT: ret i32 [[TMP1]]
//
+// C11-O2-LABEL: define dso_local i32 @cast(
+// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3]] {
+// C11-O2-NEXT: [[ENTRY:.*:]]
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
+// C11-O2-NEXT: ret i32 [[TMP0]]
+//
int cast(void) {
struct X x;
- int *p;
+ extern int *p;
p = ((struct X)x).a;
return *p;
}
@@ -178,14 +214,13 @@ int cast(void) {
// C99-NEXT: [[ENTRY:.*:]]
// C99-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
// C99-NEXT: [[S:%.*]] = alloca [[STRUCT_X]], align 4
-// C99-NEXT: [[P:%.*]] = alloca ptr, align 8
// C99-NEXT: [[TMP:%.*]] = alloca [[STRUCT_X]], align 4
// C99-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[X]], ptr align 4 [[S]], i64 20, i1 false)
// C99-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[TMP]], ptr align 4 [[X]], i64 20, i1 false)
// C99-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[TMP]], i32 0, i32 0
// C99-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
-// C99-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
-// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C99-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
+// C99-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C99-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C99-NEXT: ret i32 [[TMP1]]
//
@@ -194,19 +229,38 @@ int cast(void) {
// C11-NEXT: [[ENTRY:.*:]]
// C11-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
// C11-NEXT: [[S:%.*]] = alloca [[STRUCT_X]], align 4
-// C11-NEXT: [[P:%.*]] = alloca ptr, align 8
// C11-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[X]], ptr align 4 [[S]], i64 20, i1 false)
// C11-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false)
// C11-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
// C11-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
-// C11-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8
-// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8
+// C11-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8
+// C11-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8
// C11-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4
// C11-NEXT: ret i32 [[TMP1]]
//
+// C11-O2-LABEL: define dso_local i32 @assign(
+// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3]] {
+// C11-O2-NEXT: [[ENTRY:.*:]]
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
+// C11-O2-NEXT: ret i32 [[TMP0]]
+//
int assign(void) {
struct X x, s;
- int *p = (x = s).a;
+ extern int *p;
+ p = (x = s).a;
return *p;
}
+//.
+// C11-O2: [[TBAA3]] = !{[[META4:![0-9]+]], [[META4]], i64 0}
+// C11-O2: [[META4]] = !{!"int", [[META5:![0-9]+]], i64 0}
+// C11-O2: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}
+// C11-O2: [[META6]] = !{!"Simple C/C++ TBAA"}
+// C11-O2: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
+// C11-O2: [[META8]] = !{!"p1 int", [[META9:![0-9]+]], i64 0}
+// C11-O2: [[META9]] = !{!"any pointer", [[META5]], i64 0}
+//.
>From 7c264038248e50171fd5a04b6a6ab0ca84962613 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 15:23:44 -0400
Subject: [PATCH 8/9] Disable LLVM passes
---
clang/test/C/C11/n1285_1.c | 138 ++++++++++++++++++++++++++-----------
1 file changed, 98 insertions(+), 40 deletions(-)
diff --git a/clang/test/C/C11/n1285_1.c b/clang/test/C/C11/n1285_1.c
index bac87c1b48ad5..ba320b5f217c9 100644
--- a/clang/test/C/C11/n1285_1.c
+++ b/clang/test/C/C11/n1285_1.c
@@ -1,7 +1,7 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
// RUN: %clang_cc1 -std=c99 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C99
// RUN: %clang_cc1 -std=c11 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C11
-// RUN: %clang_cc1 -std=c11 -O2 -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C11-O2
+// RUN: %clang_cc1 -std=c11 -O2 -disable-llvm-passes -Wno-dangling -emit-llvm -o - %s | FileCheck %s --check-prefix=C11-O2
// Ensure that codegen for temporary lifetimes makes sense.
@@ -35,14 +35,21 @@ struct X f(void);
// C11-NEXT: ret i32 [[TMP1]]
//
// C11-O2-LABEL: define dso_local i32 @func_return(
-// C11-O2-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// C11-O2-SAME: ) #[[ATTR0:[0-9]+]] {
// C11-O2-NEXT: [[ENTRY:.*:]]
+// C11-O2-NEXT: [[P:%.*]] = alloca ptr, align 8
// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4:[0-9]+]]
-// C11-O2-NEXT: call void @f(ptr dead_on_unwind nonnull writable sret([[STRUCT_X]]) align 4 [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3:![0-9]+]]
-// C11-O2-NEXT: ret i32 [[TMP0]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[P]]) #[[ATTR5:[0-9]+]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @f(ptr dead_on_unwind writable sret([[STRUCT_X]]) align 4 [[REF_TMP]])
+// C11-O2-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-O2-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: store ptr [[ARRAYDECAY]], ptr [[P]], align 8, !tbaa [[TBAA3:![0-9]+]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load ptr, ptr [[P]], align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[TBAA8:![0-9]+]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr [[P]]) #[[ATTR5]]
+// C11-O2-NEXT: ret i32 [[TMP1]]
//
int func_return(void) {
int *p = f().a;
@@ -110,14 +117,38 @@ int func_return(void) {
// C11-NEXT: ret i32 [[ADD]]
//
// C11-O2-LABEL: define dso_local i32 @ternary(
-// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3:[0-9]+]] {
+// C11-O2-SAME: ) #[[ATTR0]] {
// C11-O2-NEXT: [[ENTRY:.*:]]
// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7:![0-9]+]]
-// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
-// C11-O2-NEXT: ret i32 [[TMP0]]
+// C11-O2-NEXT: [[Q:%.*]] = alloca ptr, align 8
+// C11-O2-NEXT: [[DOTCOMPOUNDLITERAL:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: br i1 true, label %[[COND_TRUE:.*]], label %[[COND_FALSE:.*]]
+// C11-O2: [[COND_TRUE]]:
+// C11-O2-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[REF_TMP]], i8 0, i64 20, i1 false)
+// C11-O2-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-O2-NEXT: br label %[[COND_END:.*]]
+// C11-O2: [[COND_FALSE]]:
+// C11-O2-NEXT: call void @f(ptr dead_on_unwind writable sret([[STRUCT_X]]) align 4 [[REF_TMP]])
+// C11-O2-NEXT: br label %[[COND_END]]
+// C11-O2: [[COND_END]]:
+// C11-O2-NEXT: [[A1:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-O2-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A1]], i64 0, i64 0
+// C11-O2-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 8, ptr [[Q]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.memset.p0.i64(ptr align 4 [[DOTCOMPOUNDLITERAL]], i8 0, i64 20, i1 false)
+// C11-O2-NEXT: [[A2:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
+// C11-O2-NEXT: [[A3:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[DOTCOMPOUNDLITERAL]], i32 0, i32 0
+// C11-O2-NEXT: [[ARRAYDECAY4:%.*]] = getelementptr inbounds [5 x i32], ptr [[A3]], i64 0, i64 0
+// C11-O2-NEXT: store ptr [[ARRAYDECAY4]], ptr [[Q]], align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[TBAA8]]
+// C11-O2-NEXT: [[TMP2:%.*]] = load ptr, ptr [[Q]], align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 4, !tbaa [[TBAA8]]
+// C11-O2-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP3]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 8, ptr [[Q]]) #[[ATTR5]]
+// C11-O2-NEXT: ret i32 [[ADD]]
//
int ternary(void) {
extern int *p;
@@ -152,14 +183,21 @@ int ternary(void) {
// C11-NEXT: ret i32 [[TMP1]]
//
// C11-O2-LABEL: define dso_local i32 @comma(
-// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3]] {
+// C11-O2-SAME: ) #[[ATTR0]] {
// C11-O2-NEXT: [[ENTRY:.*:]]
-// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7]]
-// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
-// C11-O2-NEXT: ret i32 [[TMP0]]
+// C11-O2-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[X]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false), !tbaa.struct [[TBAA_STRUCT10:![0-9]+]]
+// C11-O2-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-O2-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-O2-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[TBAA8]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[X]]) #[[ATTR5]]
+// C11-O2-NEXT: ret i32 [[TMP1]]
//
int comma(void) {
struct X x;
@@ -193,14 +231,21 @@ int comma(void) {
// C11-NEXT: ret i32 [[TMP1]]
//
// C11-O2-LABEL: define dso_local i32 @cast(
-// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3]] {
+// C11-O2-SAME: ) #[[ATTR0]] {
// C11-O2-NEXT: [[ENTRY:.*:]]
-// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7]]
-// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
-// C11-O2-NEXT: ret i32 [[TMP0]]
+// C11-O2-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[X]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false), !tbaa.struct [[TBAA_STRUCT10]]
+// C11-O2-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-O2-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-O2-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[TBAA8]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[X]]) #[[ATTR5]]
+// C11-O2-NEXT: ret i32 [[TMP1]]
//
int cast(void) {
struct X x;
@@ -240,14 +285,25 @@ int cast(void) {
// C11-NEXT: ret i32 [[TMP1]]
//
// C11-O2-LABEL: define dso_local i32 @assign(
-// C11-O2-SAME: ) local_unnamed_addr #[[ATTR3]] {
+// C11-O2-SAME: ) #[[ATTR0]] {
// C11-O2-NEXT: [[ENTRY:.*:]]
-// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X:%.*]], align 4
-// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: store ptr [[REF_TMP]], ptr @p, align 8, !tbaa [[TBAA7]]
-// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr nonnull [[REF_TMP]]) #[[ATTR4]]
-// C11-O2-NEXT: [[TMP0:%.*]] = load i32, ptr [[REF_TMP]], align 4, !tbaa [[TBAA3]]
-// C11-O2-NEXT: ret i32 [[TMP0]]
+// C11-O2-NEXT: [[X:%.*]] = alloca [[STRUCT_X:%.*]], align 4
+// C11-O2-NEXT: [[S:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-O2-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_X]], align 4
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[X]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[S]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.lifetime.start.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[X]], ptr align 4 [[S]], i64 20, i1 false), !tbaa.struct [[TBAA_STRUCT10]]
+// C11-O2-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 [[REF_TMP]], ptr align 4 [[X]], i64 20, i1 false), !tbaa.struct [[TBAA_STRUCT10]]
+// C11-O2-NEXT: [[A:%.*]] = getelementptr inbounds nuw [[STRUCT_X]], ptr [[REF_TMP]], i32 0, i32 0
+// C11-O2-NEXT: [[ARRAYDECAY:%.*]] = getelementptr inbounds [5 x i32], ptr [[A]], i64 0, i64 0
+// C11-O2-NEXT: store ptr [[ARRAYDECAY]], ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[REF_TMP]]) #[[ATTR5]]
+// C11-O2-NEXT: [[TMP0:%.*]] = load ptr, ptr @p, align 8, !tbaa [[TBAA3]]
+// C11-O2-NEXT: [[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !tbaa [[TBAA8]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[S]]) #[[ATTR5]]
+// C11-O2-NEXT: call void @llvm.lifetime.end.p0(i64 20, ptr [[X]]) #[[ATTR5]]
+// C11-O2-NEXT: ret i32 [[TMP1]]
//
int assign(void) {
struct X x, s;
@@ -257,10 +313,12 @@ int assign(void) {
}
//.
// C11-O2: [[TBAA3]] = !{[[META4:![0-9]+]], [[META4]], i64 0}
-// C11-O2: [[META4]] = !{!"int", [[META5:![0-9]+]], i64 0}
-// C11-O2: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}
-// C11-O2: [[META6]] = !{!"Simple C/C++ TBAA"}
-// C11-O2: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
-// C11-O2: [[META8]] = !{!"p1 int", [[META9:![0-9]+]], i64 0}
-// C11-O2: [[META9]] = !{!"any pointer", [[META5]], i64 0}
+// C11-O2: [[META4]] = !{!"p1 int", [[META5:![0-9]+]], i64 0}
+// C11-O2: [[META5]] = !{!"any pointer", [[META6:![0-9]+]], i64 0}
+// C11-O2: [[META6]] = !{!"omnipotent char", [[META7:![0-9]+]], i64 0}
+// C11-O2: [[META7]] = !{!"Simple C/C++ TBAA"}
+// C11-O2: [[TBAA8]] = !{[[META9:![0-9]+]], [[META9]], i64 0}
+// C11-O2: [[META9]] = !{!"int", [[META6]], i64 0}
+// C11-O2: [[TBAA_STRUCT10]] = !{i64 0, i64 20, [[META11:![0-9]+]]}
+// C11-O2: [[META11]] = !{[[META6]], [[META6]], i64 0}
//.
>From cc0817479f67fc8d1bd340c629a5d397da5c1104 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aaron at aaronballman.com>
Date: Mon, 31 Mar 2025 15:33:39 -0400
Subject: [PATCH 9/9] Remove escape
---
clang/test/CodeGen/xcore-abi.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CodeGen/xcore-abi.c b/clang/test/CodeGen/xcore-abi.c
index d06556b9da858..a8c563c3d2fab 100644
--- a/clang/test/CodeGen/xcore-abi.c
+++ b/clang/test/CodeGen/xcore-abi.c
@@ -27,7 +27,7 @@ void testva (int n, ...) {
va_start(ap,n);
// CHECK: [[AP:%[a-z0-9]+]] = alloca ptr, align 4
// CHECK: [[V5:%[a-z0-9]+]] = alloca %struct.x, align 4
- // CHECK: [[TMP:%[a-z0-9\.]+]] = alloca [4 x i32], align 4
+ // CHECK: [[TMP:%[a-z0-9.]+]] = alloca [4 x i32], align 4
// CHECK: call void @llvm.va_start.p0(ptr [[AP]])
char* v1 = va_arg (ap, char*);
More information about the cfe-commits
mailing list