From: Anders Carlsson Date: Mon, 31 Oct 2011 16:27:11 +0000 (+0000) Subject: In x86_64, when calling an Objective-C method that returns a _Complex long double... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eea64802558cc398571938b1f28cda1d4fa79ec3;p=clang In x86_64, when calling an Objective-C method that returns a _Complex long double, make sure to use the objc_msgSend_fp2ret function which ensures that the return value will be {0, 0} if the receiver is nil. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@143350 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Builtins.def b/include/clang/Basic/Builtins.def index d603d5303c..0f63aaf77e 100644 --- a/include/clang/Basic/Builtins.def +++ b/include/clang/Basic/Builtins.def @@ -698,6 +698,8 @@ LIBBUILTIN(objc_msgSend, "GGH.", "f", "objc/message.h", OBJC_LANG) // long double objc_msgSend_fpret(id self, SEL op, ...) LIBBUILTIN(objc_msgSend_fpret, "LdGH.", "f", "objc/message.h", OBJC_LANG) +// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) +LIBBUILTIN(objc_msgSend_fp2ret, "XLdGH.", "f", "objc/message.h", OBJC_LANG) // id objc_msgSend_stret (id, SEL, ...) LIBBUILTIN(objc_msgSend_stret, "GGH.", "f", "objc/message.h", OBJC_LANG) // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) diff --git a/include/clang/Basic/TargetInfo.h b/include/clang/Basic/TargetInfo.h index a87af2fbbc..39d4fe9c02 100644 --- a/include/clang/Basic/TargetInfo.h +++ b/include/clang/Basic/TargetInfo.h @@ -91,6 +91,7 @@ protected: unsigned HasAlignMac68kSupport : 1; unsigned RealTypeUsesObjCFPRet : 3; + unsigned ComplexLongDoubleUsesFP2Ret : 1; // TargetInfo Constructor. Default initializes all fields. TargetInfo(const std::string &T); @@ -327,6 +328,12 @@ public: return RealTypeUsesObjCFPRet & (1 << T); } + /// \brief Check whether _Complex long double should use the "fp2ret" flavor + /// of Obj-C message passing on this target. + bool useObjCFP2RetForComplexLongDouble() const { + return ComplexLongDoubleUsesFP2Ret; + } + ///===---- Other target property query methods --------------------------===// /// getTargetDefines - Appends the target-specific #define values for this diff --git a/lib/Basic/TargetInfo.cpp b/lib/Basic/TargetInfo.cpp index 593db2b901..694121545a 100644 --- a/lib/Basic/TargetInfo.cpp +++ b/lib/Basic/TargetInfo.cpp @@ -74,6 +74,9 @@ TargetInfo::TargetInfo(const std::string &T) : Triple(T) { // Default to no types using fpret. RealTypeUsesObjCFPRet = 0; + // Default to not using fp2ret for __Complex long double + ComplexLongDoubleUsesFP2Ret = false; + // Default to using the Itanium ABI. CXXABI = CXXABI_Itanium; diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp index 60642b383a..da32c93254 100644 --- a/lib/Basic/Targets.cpp +++ b/lib/Basic/Targets.cpp @@ -2245,6 +2245,9 @@ public: // Use fpret only for long double. RealTypeUsesObjCFPRet = (1 << TargetInfo::LongDouble); + // Use fp2ret for _Complex long double. + ComplexLongDoubleUsesFP2Ret = true; + // x86-64 has atomics up to 16 bytes. // FIXME: Once the backend is fixed, increase MaxAtomicInlineWidth to 128 // on CPUs with cmpxchg16b diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index f9eaedab18..6e3b70cef8 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -611,6 +611,17 @@ bool CodeGenModule::ReturnTypeUsesFPRet(QualType ResultType) { return false; } +bool CodeGenModule::ReturnTypeUsesFP2Ret(QualType ResultType) { + if (const ComplexType *CT = ResultType->getAs()) { + if (const BuiltinType *BT = CT->getElementType()->getAs()) { + if (BT->getKind() == BuiltinType::LongDouble) + return getContext().getTargetInfo().useObjCFP2RetForComplexLongDouble(); + } + } + + return false; +} + llvm::FunctionType *CodeGenTypes::GetFunctionType(GlobalDecl GD) { const CGFunctionInfo &FI = getFunctionInfo(GD); diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp index 4730416dd1..84277f6827 100644 --- a/lib/CodeGen/CGObjCMac.cpp +++ b/lib/CodeGen/CGObjCMac.cpp @@ -99,6 +99,22 @@ private: } + /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) + /// + /// The messenger used when the return value is returned in two values on the + /// x87 floating point stack; without a special entrypoint, the nil case + /// would be unbalanced. Only used on 64-bit X86. + llvm::Constant *getMessageSendFp2retFn() const { + llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; + llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); + llvm::Type *resultType = + llvm::StructType::get(longDoubleType, longDoubleType, NULL); + + return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, + params, true), + "objc_msgSend_fp2ret"); + } + /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) /// /// The messenger used for super calls, which have different dispatch @@ -391,6 +407,14 @@ public: return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); } + llvm::Constant *getSendFp2retFn(bool IsSuper) const { + return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); + } + + llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { + return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); + } + ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); ~ObjCCommonTypesHelper(){} }; @@ -1590,6 +1614,9 @@ CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) : ObjCTypes.getSendFpretFn(IsSuper); + } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { + Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) + : ObjCTypes.getSendFp2retFn(IsSuper); } else { Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) : ObjCTypes.getSendFn(IsSuper); diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index dbc6a87bd1..ea2e177605 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -715,10 +715,14 @@ public: /// as a return type. bool ReturnTypeUsesSRet(const CGFunctionInfo &FI); - /// ReturnTypeUsesSret - Return true iff the given type uses 'fpret' when used - /// as a return type. + /// ReturnTypeUsesFPRet - Return true iff the given type uses 'fpret' when + /// used as a return type. bool ReturnTypeUsesFPRet(QualType ResultType); + /// ReturnTypeUsesFP2Ret - Return true iff the given type uses 'fp2ret' when + /// used as a return type. + bool ReturnTypeUsesFP2Ret(QualType ResultType); + /// ConstructAttributeList - Get the LLVM attributes and calling convention to /// use for a particular function type. /// diff --git a/test/CodeGenObjC/fp2ret.m b/test/CodeGenObjC/fp2ret.m new file mode 100644 index 0000000000..9c956aec9b --- /dev/null +++ b/test/CodeGenObjC/fp2ret.m @@ -0,0 +1,28 @@ +// RUN: %clang_cc1 -triple i386-apple-darwin9 -fobjc-fragile-abi -emit-llvm -o - %s | \ +// RUN: FileCheck --check-prefix=CHECK-X86_32 %s +// +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-fragile-abi -emit-llvm -o - %s | \ +// RUN: FileCheck --check-prefix=CHECK-X86_64 %s +// +// RUN: %clang_cc1 -triple armv7-apple-darwin10 -fobjc-fragile-abi -emit-llvm -target-abi apcs-gnu -o - %s | \ +// RUN: FileCheck --check-prefix=CHECK-ARMV7 %s + +@interface A +-(_Complex long double) complexLongDoubleValue; +@end + + +// CHECK-X86_32: define void @t0() +// CHECK-X86_32: call void bitcast {{.*}} @objc_msgSend_stret to +// CHECK-X86_32: } +// +// CHECK-X86_64: define void @t0() +// CHECK-X86_64: call { x86_fp80, x86_fp80 } bitcast {{.*}} @objc_msgSend_fp2ret to +// CHECK-X86_64: } +// +// CHECK-ARMV7: define void @t0() +// CHECK-ARMV7: call i128 bitcast {{.*}} @objc_msgSend to +// CHECK-ARMV7: } +void t0() { + [(A*)0 complexLongDoubleValue]; +}