From 2738278b7f34df9cf29f1ffffc4c87b00fdd0976 Mon Sep 17 00:00:00 2001 From: Manman Ren Date: Fri, 3 Apr 2015 18:10:29 +0000 Subject: [PATCH] [i386 ABI] expand small C like structs in C++, just like how we handle small C structs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This comes up when we have a function that takes a struct and is defined in a C++ file and used in a C file. Before this commit, we will generate byval for C++ and will expand the struct for C, thus causing difference at IR level. We will use bitcast of function type at the callsite, which causes the inliner to not inline the function. This commit changes how we handle small C like structs at IR level, but at backend, we should generate the same argument passing before and after the commit. Note that the condition for expanding is still over conservative. We should be able to expand type that is spelled with “class” and types that are not C-like. But this commit fixes the inconsistent argument passing between C/C++. Reviewed by John. rdar://20121030 llvm-svn: 234033 --- clang/lib/CodeGen/TargetInfo.cpp | 8 +++++++- clang/test/CodeGenCXX/call-with-static-chain.cpp | 2 +- clang/test/CodeGenCXX/microsoft-abi-sret-and-byval.cpp | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp index 081895f1e9a2..c217273bc0d9 100644 --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -339,9 +339,15 @@ static bool canExpandIndirectArgument(QualType Ty, ASTContext &Context) { // // FIXME: This needs to be generalized to handle classes as well. const RecordDecl *RD = RT->getDecl(); - if (!RD->isStruct() || isa(RD)) + if (!RD->isStruct()) return false; + // We try to expand CLike CXXRecordDecl. + if (const CXXRecordDecl *CXXRD = dyn_cast(RD)) { + if (!CXXRD->isCLike()) + return false; + } + uint64_t Size = 0; for (const auto *FD : RD->fields()) { diff --git a/clang/test/CodeGenCXX/call-with-static-chain.cpp b/clang/test/CodeGenCXX/call-with-static-chain.cpp index 7cf929189f2c..ac1149b52edd 100644 --- a/clang/test/CodeGenCXX/call-with-static-chain.cpp +++ b/clang/test/CodeGenCXX/call-with-static-chain.cpp @@ -21,7 +21,7 @@ A &f4(); void test() { A a; - // CHECK32: call i32 bitcast (i32 (%struct.A*, %struct.A*, %struct.A*, %struct.A*)* @f1 to i32 (i8*, %struct.A*, %struct.A*, %struct.A*, %struct.A*)*)(i8* nest bitcast (i32 (%struct.A*, %struct.A*, %struct.A*, %struct.A*)* @f1 to i8*) + // CHECK32: call i32 bitcast (i32 (i32, i32, i32, i32, i32, i32, i32, i32)* @f1 to i32 (i8*, i32, i32, i32, i32, i32, i32, i32, i32)*)(i8* nest bitcast (i32 (i32, i32, i32, i32, i32, i32, i32, i32)* @f1 to i8*) // CHECK64: call i32 bitcast (i32 (i64, i64, i64, i64, i64, i64, %struct.A*)* @f1 to i32 (i8*, i64, i64, i64, i64, i64, i64, %struct.A*)*)(i8* nest bitcast (i32 (i64, i64, i64, i64, i64, i64, %struct.A*)* @f1 to i8*) __builtin_call_with_static_chain(f1(a, a, a, a), f1); diff --git a/clang/test/CodeGenCXX/microsoft-abi-sret-and-byval.cpp b/clang/test/CodeGenCXX/microsoft-abi-sret-and-byval.cpp index a4eaa1cfbc1b..4c2d8506af70 100644 --- a/clang/test/CodeGenCXX/microsoft-abi-sret-and-byval.cpp +++ b/clang/test/CodeGenCXX/microsoft-abi-sret-and-byval.cpp @@ -101,12 +101,12 @@ Big big_return() { return Big(); } void small_arg(Small s) {} -// LINUX-LABEL: define void @_Z9small_arg5Small(%struct.Small* byval align 4 %s) +// LINUX-LABEL: define void @_Z9small_arg5Small(i32 %s.0) // WIN32: define void @"\01?small_arg@@YAXUSmall@@@Z"(%struct.Small* byval align 4 %s) // WIN64: define void @"\01?small_arg@@YAXUSmall@@@Z"(i32 %s.coerce) void medium_arg(Medium s) {} -// LINUX-LABEL: define void @_Z10medium_arg6Medium(%struct.Medium* byval align 4 %s) +// LINUX-LABEL: define void @_Z10medium_arg6Medium(i32 %s.0, i32 %s.1) // WIN32: define void @"\01?medium_arg@@YAXUMedium@@@Z"(%struct.Medium* byval align 4 %s) // WIN64: define void @"\01?medium_arg@@YAXUMedium@@@Z"(i64 %s.coerce) @@ -229,7 +229,7 @@ class Class { // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUEmptyWithCtor@@@Z"(%class.Class* %this, i8 %s.coerce) void thiscall_method_arg(Small s) {} - // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE5Small(%class.Class* %this, %struct.Small* byval align 4 %s) + // LINUX: define {{.*}} void @_ZN5Class19thiscall_method_argE5Small(%class.Class* %this, i32 %s.0) // WIN32: define {{.*}} void @"\01?thiscall_method_arg@Class@@QAEXUSmall@@@Z"(%class.Class* %this, %struct.Small* byval align 4 %s) // WIN64: define linkonce_odr void @"\01?thiscall_method_arg@Class@@QEAAXUSmall@@@Z"(%class.Class* %this, i32 %s.coerce)