[cfe-commits] r78267 - in /cfe/trunk: lib/CodeGen/CGCXX.cpp test/CodeGenCXX/copy-constructor-elim.cpp
Douglas Gregor
dgregor at apple.com
Thu Aug 6 11:10:17 PDT 2009
On Aug 5, 2009, at 6:03 PM, Fariborz Jahanian wrote:
> 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;
> + }
> + }
The intent of CXXConstructorExpr::isElidable() was to have Sema
compute all of the cases where a copy constructor is elidable (based
on its understanding of the language semantics). Then, CodeGen would
just check isElidable() and elide in those cases. That way, CodeGen
doesn't have to know anything about the language to do the right thing.
- Doug
More information about the cfe-commits
mailing list