[cfe-commits] r78267 - in /cfe/trunk: lib/CodeGen/CGCXX.cpp test/CodeGenCXX/copy-constructor-elim.cpp

Fariborz Jahanian fjahanian at apple.com
Wed Aug 5 18:03:10 PDT 2009


Author: fjahanian
Date: Wed Aug  5 20:02:49 2009
New Revision: 78267

URL: http://llvm.org/viewvc/llvm-project?rev=78267&view=rev
Log:
Patch to optimize away copy constructor call when
appropriate.

Added:
    cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGCXX.cpp

Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=78267&r1=78266&r2=78267&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Wed Aug  5 20:02:49 2009
@@ -255,7 +255,20 @@
   cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
   if (RD->hasTrivialConstructor())
     return;
-  
+
+  // Code gen optimization to eliminate copy constructor and return 
+  // its first argument instead.
+  const CXXConstructorDecl *CDecl = E->getConstructor();
+  if (E->getNumArgs() == 1 &&
+      CDecl->isCopyConstructor(getContext())) {
+    CXXConstructExpr::const_arg_iterator i = E->arg_begin();
+    const Expr *SubExpr = (*i);
+    // FIXME. Any other cases can be optimized away?
+    if (isa<CallExpr>(SubExpr) || isa<CXXTemporaryObjectExpr>(SubExpr)) {
+      EmitAggExpr(SubExpr, Dest, false);
+      return;
+    }
+  }
   // Call the constructor.
   EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, 
                          E->arg_begin(), E->arg_end());

Added: cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp?rev=78267&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp Wed Aug  5 20:02:49 2009
@@ -0,0 +1,31 @@
+// RUN: clang-cc -emit-llvm -o %t %s &&
+// RUN: grep "_ZN1CC1ERK1C" %t | count 0
+
+extern "C" int printf(...);
+
+
+struct C {
+	C() : iC(6) {printf("C()\n"); }
+	C(const C& c) { printf("C(const C& c)\n"); }
+	int iC;
+};
+
+C foo() {
+  return C();
+};
+
+class X { // ...
+public: 
+	X(int) {}
+	X(const X&, int i = 1, int j = 2, C c = foo()) {
+		printf("X(const X&, %d, %d, %d)\n", i, j, c.iC);
+	}
+};
+
+int main()
+{
+	X a(1);
+	X b(a, 2);
+	X c = b;
+	X d(a, 5, 6);
+}





More information about the cfe-commits mailing list