[cfe-commits] r95077 - in /cfe/trunk: lib/CodeGen/CGExprConstant.cpp test/CodeGenCXX/global-init.cpp

John McCall rjmccall at apple.com
Tue Feb 2 00:02:50 PST 2010


Author: rjmccall
Date: Tue Feb  2 02:02:49 2010
New Revision: 95077

URL: http://llvm.org/viewvc/llvm-project?rev=95077&view=rev
Log:
Codegen CXXConstructExprs with trivial constructors as constants.
Eliminates a lot of spurious global initializers, fixing PR6205.


Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/test/CodeGenCXX/global-init.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Tue Feb  2 02:02:49 2010
@@ -668,6 +668,29 @@
     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");
 

Modified: cfe/trunk/test/CodeGenCXX/global-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/global-init.cpp?rev=95077&r1=95076&r2=95077&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/global-init.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/global-init.cpp Tue Feb  2 02:02:49 2010
@@ -7,6 +7,10 @@
 
 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 @@
 // 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() {





More information about the cfe-commits mailing list