[cfe-commits] r139178 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaExpr.cpp test/CodeGenObjCXX/nrvo.mm

Douglas Gregor dgregor at apple.com
Tue Sep 6 13:46:03 PDT 2011


Author: dgregor
Date: Tue Sep  6 15:46:03 2011
New Revision: 139178

URL: http://llvm.org/viewvc/llvm-project?rev=139178&view=rev
Log:
Implement the Named Return Value Optimization (NRVO) for blocks.

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CodeGenObjCXX/nrvo.mm

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=139178&r1=139177&r2=139178&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Tue Sep  6 15:46:03 2011
@@ -1056,6 +1056,7 @@
   Decl *ActOnStartOfFunctionDef(Scope *S, Decl *D);
   void ActOnStartOfObjCMethodDef(Scope *S, Decl *D);
 
+  void computeNRVO(Stmt *Body, sema::FunctionScopeInfo *Scope);
   Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body);
   Decl *ActOnFinishFunctionBody(Decl *Decl, Stmt *Body, bool IsInstantiation);
 

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=139178&r1=139177&r2=139178&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Sep  6 15:46:03 2011
@@ -6626,7 +6626,7 @@
 /// FIXME: Employ a smarter algorithm that accounts for multiple return 
 /// statements and the lifetimes of the NRVO candidates. We should be able to
 /// find a maximal set of NRVO variables.
-static void ComputeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
+void Sema::computeNRVO(Stmt *Body, FunctionScopeInfo *Scope) {
   ReturnStmt **Returns = Scope->Returns.data();
 
   const VarDecl *NRVOCandidate = 0;
@@ -6687,7 +6687,7 @@
       if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(FD))
         MarkVTableUsed(FD->getLocation(), Constructor->getParent());
       
-      ComputeNRVO(Body, getCurFunction());
+      computeNRVO(Body, getCurFunction());
     }
     
     assert(FD == getCurFunctionDecl() && "Function parsing confused");
@@ -6702,7 +6702,7 @@
                                              MD->getResultType(), MD);
       
       if (Body)
-        ComputeNRVO(Body, getCurFunction());
+        computeNRVO(Body, getCurFunction());
     }
     if (ObjCShouldCallSuperDealloc) {
       Diag(MD->getLocEnd(), diag::warn_objc_missing_super_dealloc);

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=139178&r1=139177&r2=139178&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Sep  6 15:46:03 2011
@@ -8755,6 +8755,8 @@
       getCurFunction()->setHasBranchProtectedScope();
   }
 
+  computeNRVO(Body, getCurBlock());
+  
   BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy);
   const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy();
   PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result);

Modified: cfe/trunk/test/CodeGenObjCXX/nrvo.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjCXX/nrvo.mm?rev=139178&r1=139177&r2=139178&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenObjCXX/nrvo.mm (original)
+++ cfe/trunk/test/CodeGenObjCXX/nrvo.mm Tue Sep  6 15:46:03 2011
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -emit-llvm -o - %s -O1 -triple x86_64-apple-darwin10.0.0 | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -o - -fblocks %s -O1 -triple x86_64-apple-darwin10.0.0 | FileCheck %s
 
 // PR10835 / <rdar://problem/10050178>
 struct X {
@@ -19,3 +19,14 @@
   return x;
 }
 @end
+
+X blocksNRVO() {
+  return ^{
+    // CHECK: define internal void @__blocksNRVO_block_invoke_0
+    X x;
+    // CHECK: tail call void @_ZN1XC1Ev
+    // CHECK-NEXT: ret void
+    return x;
+  }() ;
+}
+





More information about the cfe-commits mailing list