diff options
author | John McCall <rjmccall@apple.com> | 2010-02-02 08:02:49 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2010-02-02 08:02:49 +0000 |
commit | fb8b69aef3377aaa786d1278aaae7e7b04ac095f (patch) | |
tree | 39d41f571f9024781d92428c981d0a06474684c9 | |
parent | 2e13db8bc64bc99922b0aeb45443c681c1643c82 (diff) |
Codegen CXXConstructExprs with trivial constructors as constants.
Eliminates a lot of spurious global initializers, fixing PR6205.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95077 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/CodeGen/CGExprConstant.cpp | 23 | ||||
-rw-r--r-- | test/CodeGenCXX/global-init.cpp | 10 |
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/CodeGen/CGExprConstant.cpp b/lib/CodeGen/CGExprConstant.cpp index d843d023f7..f4dd8dc337 100644 --- a/lib/CodeGen/CGExprConstant.cpp +++ b/lib/CodeGen/CGExprConstant.cpp @@ -668,6 +668,29 @@ public: return 0; } + llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) { + if (!E->getConstructor()->isTrivial()) + return 0; + + // Only copy and default constructors can be trivial. + + QualType Ty = E->getType(); + + if (E->getNumArgs()) { + assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); + assert(E->getConstructor()->isCopyConstructor() && + "trivial ctor has argument but isn't a copy ctor"); + + Expr *Arg = E->getArg(0); + assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && + "argument to copy ctor is of wrong type"); + + return Visit(E->getArg(0)); + } + + return CGM.EmitNullConstant(Ty); + } + llvm::Constant *VisitStringLiteral(StringLiteral *E) { assert(!E->getType()->isPointerType() && "Strings are always arrays"); diff --git a/test/CodeGenCXX/global-init.cpp b/test/CodeGenCXX/global-init.cpp index b375aef4c8..fd8c159755 100644 --- a/test/CodeGenCXX/global-init.cpp +++ b/test/CodeGenCXX/global-init.cpp @@ -7,6 +7,10 @@ struct A { struct B { B(); ~B(); }; +struct C { void *field; }; + +// CHECK: @c = global %struct.C zeroinitializer, align 8 + // CHECK: call void @_ZN1AC1Ev(%struct.A* @a) // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1AD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @a, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*)) A a; @@ -14,3 +18,9 @@ A a; // CHECK: call void @_ZN1BC1Ev(%struct.A* @b) // CHECK: call i32 @__cxa_atexit(void (i8*)* bitcast (void (%struct.A*)* @_ZN1BD1Ev to void (i8*)*), i8* getelementptr inbounds (%struct.A* @b, i32 0, i32 0), i8* bitcast (i8** @__dso_handle to i8*)) B b; + +// PR6205: this should not require a global initializer +// CHECK-NOT: call void @_ZN1CC1Ev(%struct.C* @c) +C c; + +// CHECK: define internal void @__cxx_global_initialization() { |