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
diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp
index a43f451..1849dee 100644
--- a/lib/CodeGen/CGObjC.cpp
+++ b/lib/CodeGen/CGObjC.cpp
@@ -2268,6 +2268,31 @@
   // 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();