Patch to optimize away copy constructor call when

appropriate.

llvm-svn: 78267
This commit is contained in:
Fariborz Jahanian 2009-08-06 01:02:49 +00:00
parent 9de556c8b6
commit eb869768f9
2 changed files with 45 additions and 1 deletions

View File

@ -256,6 +256,19 @@ CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest,
if (RD->hasTrivialConstructor()) if (RD->hasTrivialConstructor())
return; 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. // Call the constructor.
EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest,
E->arg_begin(), E->arg_end()); E->arg_begin(), E->arg_end());

View File

@ -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);
}