If a function call returns a reference, don't bind it to a temporary.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@81743 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGCXXTemp.cpp b/lib/CodeGen/CGCXXTemp.cpp
index 177c6db..4768556 100644
--- a/lib/CodeGen/CGCXXTemp.cpp
+++ b/lib/CodeGen/CGCXXTemp.cpp
@@ -54,7 +54,13 @@
   assert(!CleanupInfo.EndBlock &&
          "Should not have an end block for temporary cleanup!");
 
-  EmitBlock(Info.DtorBlock);
+  llvm::BasicBlock *CurBB = Builder.GetInsertBlock();
+  if (CurBB && !CurBB->getTerminator() &&
+      Info.DtorBlock->getNumUses() == 0) {
+    CurBB->getInstList().splice(CurBB->end(), Info.DtorBlock->getInstList());
+    delete Info.DtorBlock;
+  } else
+    EmitBlock(Info.DtorBlock);
 
   llvm::BasicBlock *CondEnd = 0;
 
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index a060e47..a6bf82b 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -1778,6 +1778,15 @@
   if (RD->hasTrivialDestructor())
     return Owned(E);
 
+  if (CallExpr *CE = dyn_cast<CallExpr>(E)) {
+    QualType Ty = CE->getCallee()->getType();
+    if (const PointerType *PT = Ty->getAs<PointerType>())
+      Ty = PT->getPointeeType();
+    
+    const FunctionType *FTy = Ty->getAsFunctionType();
+    if (FTy->getResultType()->isReferenceType())
+      return Owned(E);
+  }
   CXXTemporary *Temp = CXXTemporary::Create(Context,
                                             RD->getDestructor(Context));
   ExprTemporaries.push_back(Temp);
diff --git a/test/CodeGenCXX/temp-1.cpp b/test/CodeGenCXX/temp-1.cpp
index 19a6ace..9b97f00 100644
--- a/test/CodeGenCXX/temp-1.cpp
+++ b/test/CodeGenCXX/temp-1.cpp
@@ -63,9 +63,21 @@
 };
 
 // RUN: grep "call void @_ZN1EC1Ev" %t | count 3 &&
-// RUN: grep "call void @_ZN1ED1Ev" %t | count 5 
+// RUN: grep "call void @_ZN1ED1Ev" %t | count 5 &&
 void f5() {
   E() + E();
   !E();
 }
 
+struct F {
+  F();
+  ~F();
+  F& f();
+};
+
+// RUN: grep "call void @_ZN1FC1Ev" %t | count 1 &&
+// RUN: grep "call void @_ZN1FD1Ev" %t | count 1 
+void f6() {
+  F().f();
+}
+