Index: include/clang/Basic/ObjCRuntime.h =================================================================== --- include/clang/Basic/ObjCRuntime.h +++ include/clang/Basic/ObjCRuntime.h @@ -171,6 +171,68 @@ llvm_unreachable("bad kind"); } + /// Does this runtime provide ARC entrypoints that are likely to be faster + /// than an ordinary message send of the appropriate selector? + + /// The ARC entrypoints are guaranteed to be equivalent to just sending the + /// corresponding message. If the entrypoint is implemented naively as just a + /// message send, using it is a trade-off: it sacrifices a few cycles of + /// overhead to save a small amount of code. However, it's possible for + /// runtimes to detect and special-case classes that use "standard" + /// retain/release behavior; if that's dynamically a large proportion of all + /// retained objects, using the entrypoint will also be faster than using a + /// message send. + + /// When this method returns true, Clang will turn non-super message sends of + /// certain selectors into calls to the correspond entrypoint: + /// retain => objc_retain + /// release => objc_release + bool shouldUseARCFunctionsForRetainRelease() const { + switch (getKind()) { + case FragileMacOSX: return false; + case MacOSX: return getVersion() >= VersionTuple(10, 10); + case iOS: return getVersion() >= VersionTuple(8); + case WatchOS: + return true; + + case GCC: return false; + case GNUstep: return false; + case ObjFW: return false; + } + llvm_unreachable("bad kind"); + } + + + /// Does this runtime provide entrypoints that are likely to be faster + /// than an ordinary message send of the appropriate selector? + + /// The entrypoints are guaranteed to be equivalent to just sending the + /// corresponding message. If the entrypoint is implemented naively as just a + /// message send, using it is a trade-off: it sacrifices a few cycles of + /// overhead to save a small amount of code. However, it's possible for + /// runtimes to detect and special-case classes that use "standard" + /// alloc behavior; if that's dynamically a large proportion of all + /// objects, using the entrypoint will also be faster than using a message + /// send. + + /// When this method returns true, Clang will turn non-super message sends of + /// certain selectors into calls to the correspond entrypoint: + /// alloc => objc_alloc + bool shouldUseRuntimeFunctionsForAlloc() const { + switch (getKind()) { + case FragileMacOSX: return false; + case MacOSX: return getVersion() >= VersionTuple(10, 10); + case iOS: return getVersion() >= VersionTuple(8); + case WatchOS: + return true; + + case GCC: return false; + case GNUstep: return false; + case ObjFW: return false; + } + llvm_unreachable("bad kind"); + } + /// \brief Does this runtime supports optimized setter entrypoints? bool hasOptimizedSetter() const { switch (getKind()) { Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -892,6 +892,8 @@ def fobjc_arc : Flag<["-"], "fobjc-arc">, Group, Flags<[CC1Option]>, HelpText<"Synthesize retain and release calls for Objective-C pointers">; def fno_objc_arc : Flag<["-"], "fno-objc-arc">, Group; +def fno_objc_convert_messages_to_runtime_calls : + Flag<["-"], "fno-objc-convert-messages-to-runtime-calls">, Group, Flags<[CC1Option]>; def fobjc_arc_exceptions : Flag<["-"], "fobjc-arc-exceptions">, Group, Flags<[CC1Option]>, HelpText<"Use EH-safe code when synthesizing retains and releases in -fobjc-arc">; def fno_objc_arc_exceptions : Flag<["-"], "fno-objc-arc-exceptions">, Group; Index: include/clang/Frontend/CodeGenOptions.def =================================================================== --- include/clang/Frontend/CodeGenOptions.def +++ include/clang/Frontend/CodeGenOptions.def @@ -95,6 +95,8 @@ CODEGENOPT(NoZeroInitializedInBSS , 1, 0) ///< -fno-zero-initialized-in-bss. /// \brief Method of Objective-C dispatch to use. ENUM_CODEGENOPT(ObjCDispatchMethod, ObjCDispatchMethodKind, 2, Legacy) +/// Replace certain message sends with calls to ObjC runtime entrypoints +CODEGENOPT(ObjCConvertMessagesToRuntimeCalls , 1, 1) CODEGENOPT(OmitLeafFramePointer , 1, 0) ///< Set when -momit-leaf-frame-pointer is ///< enabled. VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified. Index: lib/CodeGen/CGObjC.cpp =================================================================== --- lib/CodeGen/CGObjC.cpp +++ lib/CodeGen/CGObjC.cpp @@ -460,10 +460,53 @@ Args, method); } else { - result = Runtime.GenerateMessageSend(*this, Return, ResultType, - E->getSelector(), - Receiver, Args, OID, - method); + // Call runtime methods directly if we can. + Optional CallResult = None; + if (method && CGM.getCodeGenOpts().ObjCConvertMessagesToRuntimeCalls) { + auto &Runtime = CGM.getLangOpts().ObjCRuntime; + if (method->getMethodFamily() == OMF_alloc && + Runtime.shouldUseRuntimeFunctionsForAlloc()) { + // Make sure the name is exactly 'alloc'. All methods with that + // prefix are identified as OMF_alloc but we only want to call the + // runtime for this version. + Selector S = method->getSelector(); + if (S.isUnarySelector() && S.getNameForSlot(0) == "alloc" && + ResultType->isObjCIdType()) + CallResult = RValue::get(EmitObjCAlloc(Receiver)); + } else if (CGM.getLangOpts().getGC() == LangOptions::NonGC && + Runtime.shouldUseARCFunctionsForRetainRelease()) { + switch (method->getMethodFamily()) { + case OMF_autorelease: + if (ResultType->isObjCIdType()) + CallResult = RValue::get(EmitARCAutorelease(Receiver)); + break; + + case OMF_retain: + if (ResultType->isObjCIdType()) + CallResult = RValue::get(EmitARCRetainNonBlock(Receiver)); + break; + + case OMF_release: + if (ResultType->isVoidType()) { + EmitARCRelease(Receiver, ARCImpreciseLifetime); + CallResult = RValue::get(nullptr); + } + break; + default: + break; + } + } + } + + // If we didn't convert a message send in to a call, then we should emit + // the message instead. + if (CallResult) + result = CallResult.getValue(); + else + result = Runtime.GenerateMessageSend(*this, Return, ResultType, + E->getSelector(), + Receiver, Args, OID, + method); } // For delegate init calls in ARC, implicitly store the result of @@ -2333,6 +2376,14 @@ return InitRV.getScalarVal(); } +/// Allocate the given objc object. +/// call i8* \@objc_alloc(i8* %value) +llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value) { + return emitARCValueOperation(*this, value, + CGM.getObjCEntrypoints().objc_alloc, + "objc_alloc"); +} + /// Produce the code to do a primitive release. /// [tmp drain]; void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { Index: lib/CodeGen/CodeGenFunction.h =================================================================== --- lib/CodeGen/CodeGenFunction.h +++ lib/CodeGen/CodeGenFunction.h @@ -2799,6 +2799,7 @@ std::pair EmitARCStoreStrong(const BinaryOperator *e, bool ignored); + llvm::Value *EmitObjCAlloc(llvm::Value *value); llvm::Value *EmitObjCThrowOperand(const Expr *expr); llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr); llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr); Index: lib/CodeGen/CodeGenModule.h =================================================================== --- lib/CodeGen/CodeGenModule.h +++ lib/CodeGen/CodeGenModule.h @@ -111,6 +111,9 @@ struct ObjCEntrypoints { ObjCEntrypoints() { memset(this, 0, sizeof(*this)); } + /// void objc_alloc(id); + llvm::Constant *objc_alloc; + /// void objc_autoreleasePoolPop(void*); llvm::Constant *objc_autoreleasePoolPop; Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -4901,6 +4901,12 @@ } + // Allow the user to control whether messages can be converted to runtime + // functions. + if (types::isObjC(InputType) && + Args.getLastArg(options::OPT_fno_objc_convert_messages_to_runtime_calls)) + CmdArgs.push_back("-fno-objc-convert-messages-to-runtime-calls"); + // -fobjc-infer-related-result-type is the default, except in the Objective-C // rewriter. if (rewriteKind != RK_None) Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -609,6 +609,9 @@ } } + if (Args.hasArg(OPT_fno_objc_convert_messages_to_runtime_calls)) + Opts.ObjCConvertMessagesToRuntimeCalls = 0; + Opts.EmulatedTLS = Args.hasFlag(OPT_femulated_tls, OPT_fno_emulated_tls, false); Index: test/CodeGenObjC/convert-messages-to-runtime-calls.m =================================================================== --- /dev/null +++ test/CodeGenObjC/convert-messages-to-runtime-calls.m @@ -0,0 +1,76 @@ +// RUN: %clang_cc1 -fobjc-runtime=macosx-10.10.0 -emit-llvm -o - %s -fno-objc-convert-messages-to-runtime-calls | FileCheck %s --check-prefix=MSGS +// RUN: %clang_cc1 -fobjc-runtime=macosx-10.10.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=CALLS +// RUN: %clang_cc1 -fobjc-runtime=macosx-10.9.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=MSGS +// RUN: %clang_cc1 -fobjc-runtime=macosx-fragile-10.10.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=MSGS +// Make sure we don't do calls to retain/release when using GC. +// RUN: %clang_cc1 -fobjc-runtime=macosx-10.10.0 -emit-llvm -o - %s -fobjc-gc | FileCheck %s --check-prefix=GC +// RUN: %clang_cc1 -fobjc-runtime=ios-8.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=CALLS +// RUN: %clang_cc1 -fobjc-runtime=ios-7.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=MSGS +// Note: This line below is for tvos for which the driver passes through to use the ios9.0 runtime. +// RUN: %clang_cc1 -fobjc-runtime=ios-9.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=CALLS +// RUN: %clang_cc1 -fobjc-runtime=watchos-2.0 -emit-llvm -o - %s | FileCheck %s --check-prefix=CALLS + +@interface NSObject ++ (id)alloc; ++ (id)alloc2; +- (id)init; +- (id)retain; +- (void)release; +- (id)autorelease; +@end + +@interface NSString : NSObject +- (void)retain_self; +- (void)retain_super; +@end + +// CHECK-LABEL: define {{.*}}void @test1 +void test1(id x) { + // MSGS: {{call.*@objc_msgSend}} + // MSGS: {{call.*@objc_msgSend}} + // MSGS: {{call.*@objc_msgSend}} + // MSGS: {{call.*@objc_msgSend}} + // CALLS: {{call.*@objc_alloc}} + // CALLS: {{call.*@objc_retain}} + // CALLS: {{call.*@objc_release}} + // CALLS: {{call.*@objc_autorelease}} + // GC: {{call.*@objc_alloc}} + // GC: {{call.*@objc_msgSend}} + // GC: {{call.*@objc_msgSend}} + // GC: {{call.*@objc_msgSend}} + [NSObject alloc]; + [x retain]; + [x release]; + [x autorelease]; +} + +// CHECK-LABEL: define {{.*}}void @test2 +void test2() { + // MSGS: {{call.*@objc_msgSend}} + // CALLS: {{call.*@objc_msgSend}} + // GC: {{call.*@objc_msgSend}} + // Make sure alloc has the correct name and number of types. + [NSObject alloc2]; +} + +@implementation NSString + +// Make sure we can convert a message to a dynamic receiver to a call +// CHECK-LABEL: define {{.*}}void @retain_self +- (void)retain_self { + // MSGS: {{call.*@objc_msgSend}} + // CALLS: {{call.*@objc_retain}} + // GC: {{call.*@objc_msgSend}} + [self retain]; +} + +// Make sure we never convert a message to super to a call +// CHECK-LABEL: define {{.*}}void @retain_super +- (void)retain_super { + // MSGS: {{call.*@objc_msgSend}} + // CALLS: {{call.*@objc_msgSend}} + // GC: {{call.*@objc_msgSend}} + [super retain]; +} + +@end \ No newline at end of file Index: test/Driver/objc-convert-messages-to-runtime-calls.m =================================================================== --- /dev/null +++ test/Driver/objc-convert-messages-to-runtime-calls.m @@ -0,0 +1,9 @@ +// RUN: %clang %s -### -o %t.o 2>&1 -fsyntax-only -fno-objc-convert-messages-to-runtime-calls -target x86_64-apple-macosx10.10.0 | FileCheck %s --check-prefix=DISABLE +// RUN: %clang %s -### -o %t.o 2>&1 -fsyntax-only -target x86_64-apple-macosx10.10.0 | FileCheck %s --check-prefix=ENABLE + +// Check that we pass fobjc-convert-messages-to-runtime-calls only when supported, and not explicitly disabled. + +// DISABLE: "-fno-objc-convert-messages-to-runtime-calls" +// ENABLE-NOT: "-fno-objc-convert-messages-to-runtime-calls" + +