aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFariborz Jahanian <fjahanian@apple.com>2009-08-06 01:02:49 +0000
committerFariborz Jahanian <fjahanian@apple.com>2009-08-06 01:02:49 +0000
commit6904cbb1f21002317387e8fc7b14b7f8c09d198f (patch)
treefbea4d014e5491b560cf155f9be6d1c520efe206
parent213a60edb7d67c2b324e3e9b9349b681f97baf10 (diff)
Patch to optimize away copy constructor call when
appropriate. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78267 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CGCXX.cpp15
-rw-r--r--test/CodeGenCXX/copy-constructor-elim.cpp31
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/CodeGen/CGCXX.cpp b/lib/CodeGen/CGCXX.cpp
index 5324cc622f..0f76266acc 100644
--- a/lib/CodeGen/CGCXX.cpp
+++ b/lib/CodeGen/CGCXX.cpp
@@ -255,7 +255,20 @@ CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
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());
diff --git a/test/CodeGenCXX/copy-constructor-elim.cpp b/test/CodeGenCXX/copy-constructor-elim.cpp
new file mode 100644
index 0000000000..5a1109d7f7
--- /dev/null
+++ b/test/CodeGenCXX/copy-constructor-elim.cpp
@@ -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);
+}