[cfe-commits] r111779 - in /cfe/trunk: lib/CodeGen/CGExprAgg.cpp lib/CodeGen/CGExprCXX.cpp test/CodeGenCXX/value-init.cpp

Douglas Gregor dgregor at apple.com
Sun Aug 22 09:15:35 PDT 2010


Author: dgregor
Date: Sun Aug 22 11:15:35 2010
New Revision: 111779

URL: http://llvm.org/viewvc/llvm-project?rev=111779&view=rev
Log:
When performing value-initialization for a class with a non-trivial,
implicitly-defined default constructor, zero-initialize the memory
before calling the default constructor. Previously, we would only
zero-initialize in the case of a trivial default constructor.

Also, simplify the hideous logic that determines when we have a
trivial default constructor and, therefore, don't need to emit any
call at all.

Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp
    cfe/trunk/lib/CodeGen/CGExprCXX.cpp
    cfe/trunk/test/CodeGenCXX/value-init.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=111779&r1=111778&r2=111779&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Sun Aug 22 11:15:35 2010
@@ -450,10 +450,6 @@
   if (!Val) // Create a temporary variable.
     Val = CGF.CreateMemTemp(E->getType(), "tmp");
 
-  if (E->requiresZeroInitialization())
-    EmitNullInitializationToLValue(CGF.MakeAddrLValue(Val, E->getType()),
-                                   E->getType());
-
   CGF.EmitCXXConstructExpr(Val, E);
 }
 

Modified: cfe/trunk/lib/CodeGen/CGExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprCXX.cpp?rev=111779&r1=111778&r2=111779&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprCXX.cpp Sun Aug 22 11:15:35 2010
@@ -248,25 +248,18 @@
                                       const CXXConstructExpr *E) {
   assert(Dest && "Must have a destination!");
   const CXXConstructorDecl *CD = E->getConstructor();
-  const ConstantArrayType *Array =
-  getContext().getAsConstantArrayType(E->getType());
-  // For a copy constructor, even if it is trivial, must fall thru so
-  // its argument is code-gen'ed.
-  if (!CD->isCopyConstructor()) {
-    QualType InitType = E->getType();
-    if (Array)
-      InitType = getContext().getBaseElementType(Array);
-    const CXXRecordDecl *RD =
-    cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl());
-    if (RD->hasTrivialConstructor()) {
-      // The constructor is trivial, but we may still need to zero-initialize
-      // the class.
-      if (E->requiresZeroInitialization())
-        EmitNullInitialization(Dest, E->getType());
-      
-      return;
-    }
-  }
+  
+  // If we require zero initialization before (or instead of) calling the
+  // constructor, as can be the case with a non-user-provided default
+  // constructor, emit the zero initialization now.
+  if (E->requiresZeroInitialization())
+    EmitNullInitialization(Dest, E->getType());
+
+  
+  // If this is a call to a trivial default constructor, do nothing.
+  if (CD->isTrivial() && CD->isDefaultConstructor())
+    return;
+  
   // Code gen optimization to eliminate copy constructor and return
   // its first argument instead, if in fact that argument is a temporary 
   // object.
@@ -276,6 +269,9 @@
       return;
     }
   }
+  
+  const ConstantArrayType *Array 
+    = getContext().getAsConstantArrayType(E->getType());
   if (Array) {
     QualType BaseElementTy = getContext().getBaseElementType(Array);
     const llvm::Type *BasePtr = ConvertType(BaseElementTy);

Modified: cfe/trunk/test/CodeGenCXX/value-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/value-init.cpp?rev=111779&r1=111778&r2=111779&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/value-init.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/value-init.cpp Sun Aug 22 11:15:35 2010
@@ -93,4 +93,22 @@
     // CHECK: ret i32
     return S().i;
   }
+
+  struct X0 {
+    X0() { }
+    int x;
+  };
+
+  struct X1 : X0 {
+    int x1;
+    void f();
+  };
+
+  // CHECK: define void @_ZN8zeroinit9testX0_X1Ev
+  void testX0_X1() {
+    // CHECK: call void @llvm.memset.p0i8.i64
+    // CHECK-NEXT: call void @_ZN8zeroinit2X1C1Ev
+    // CHECK-NEXT: call void @_ZN8zeroinit2X11fEv
+    X1().f();
+  }
 }





More information about the cfe-commits mailing list