diff options
author | Douglas Gregor <dgregor@apple.com> | 2011-06-22 16:32:26 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2011-06-22 16:32:26 +0000 |
commit | d1bd98a2c239d2a730b07f7c1b2c8c8718eaeac8 (patch) | |
tree | 6748ae1078f490e4e929ce009cebb8628a9d7f4e | |
parent | d7b23167763f43d3e76b0de26141b1e52613ee3c (diff) |
Implement the C++0x move optimization for Automatic Reference Counting
objects, so that we steal the retain count of a temporary __strong
pointer (zeroing out that temporary), eliding a retain/release
pair. Addresses <rdar://problem/9364932>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133621 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/CodeGen/CGObjC.cpp | 25 | ||||
-rw-r--r-- | test/CodeGenObjCXX/arc-move.mm | 63 |
2 files changed, 88 insertions, 0 deletions
diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp index a43f451109..1849dee123 100644 --- a/lib/CodeGen/CGObjC.cpp +++ b/lib/CodeGen/CGObjC.cpp @@ -2268,6 +2268,31 @@ tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { // ultimate opaque expression. const llvm::Type *resultType = 0; + // If we're loading retained from a __strong xvalue, we can avoid + // an extra retain/release pair by zeroing out the source of this + // "move" operation. + if (e->isXValue() && + e->getType().getObjCLifetime() == Qualifiers::OCL_Strong) { + // Emit the lvalue + LValue lv = CGF.EmitLValue(e); + + // Load the object pointer and cast it to the appropriate type. + QualType exprType = e->getType(); + llvm::Value *result = CGF.EmitLoadOfLValue(lv, exprType).getScalarVal(); + + if (resultType) + result = CGF.Builder.CreateBitCast(result, resultType); + + // Set the source pointer to NULL. + llvm::Value *null + = llvm::ConstantPointerNull::get( + cast<llvm::PointerType>(CGF.ConvertType(exprType))); + CGF.EmitStoreOfScalar(null, lv.getAddress(), lv.isVolatileQualified(), + lv.getAlignment(), exprType); + + return TryEmitResult(result, true); + } + while (true) { e = e->IgnoreParens(); diff --git a/test/CodeGenObjCXX/arc-move.mm b/test/CodeGenObjCXX/arc-move.mm new file mode 100644 index 0000000000..87011a622e --- /dev/null +++ b/test/CodeGenObjCXX/arc-move.mm @@ -0,0 +1,63 @@ +// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -fobjc-nonfragile-abi -fblocks -fobjc-arc -O2 -std=c++0x -disable-llvm-optzns -o - %s | FileCheck %s + +// define void @_Z11simple_moveRU8__strongP11objc_objectS2_ +void simple_move(__strong id &x, __strong id &y) { + // CHECK: = load i8** + // CHECK: store i8* null + // CHECK: = load i8** + // CHECK: store i8* + // CHECK-NEXT: call void @objc_release + x = static_cast<__strong id&&>(y); + // CHECK-NEXT: ret void +} + +template<typename T> +struct remove_reference { + typedef T type; +}; + +template<typename T> +struct remove_reference<T&> { + typedef T type; +}; + +template<typename T> +struct remove_reference<T&&> { + typedef T type; +}; + +template<typename T> +typename remove_reference<T>::type&& move(T &&x) { + return static_cast<typename remove_reference<T>::type&&>(x); +} + +// CHECK: define void @_Z12library_moveRU8__strongP11objc_objectS2_ +void library_move(__strong id &x, __strong id &y) { + // CHECK: call i8** @_Z4moveIRU8__strongP11objc_objectEON16remove_referenceIT_E4typeEOS5_ + // CHECK: load i8** + // CHECK: store i8* null, i8** + // CHECK: load i8*** + // CHECK-NEXT: load i8** + // CHECK-NEXT: store i8* + // CHECK-NEXT: call void @objc_release + // CHECK-NEXT: ret void + x = move(y); +} + +// CHECK: define void @_Z12library_moveRU8__strongP11objc_object +void library_move(__strong id &y) { + // CHECK: [[Y:%[a-zA-Z0-9]+]] = call i8** @_Z4moveIRU8__strongP11objc_objectEON16remove_referenceIT_E4typeEOS5_ + // Load the object + // CHECK-NEXT: [[OBJ:%[a-zA-Z0-9]+]] = load i8** [[Y]] + // Null out y + // CHECK-NEXT: store i8* null, i8** [[Y]] + // Initialize x with the object + // CHECK-NEXT: store i8* [[OBJ]], i8** [[X:%[a-zA-Z0-9]+]] + id x = move(y); + + // CHECK-NEXT: store i32 17 + int i = 17; + // CHECK-NEXT: [[OBJ:%[a-zA-Z0-9]+]] = load i8** [[X]] + // CHECK-NEXT: call void @objc_release(i8* [[OBJ]]) + // CHECK-NEXT: ret void +} |