]> granicus.if.org Git - clang/commitdiff
Cut down unnecessary zero'ing when value-initializing arrays of C++ objects.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 28 Apr 2011 22:57:55 +0000 (22:57 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 28 Apr 2011 22:57:55 +0000 (22:57 +0000)
-C++ objects with user-declared constructor don't need zero'ing.
-We can zero-initialize arrays of C++ objects in "bulk" now, in which case don't zero-initialize each object again.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130453 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CGExprCXX.cpp
test/CodeGenCXX/value-init.cpp

index 8747562ce5f33b30285338d26fbdfee034ddf3b6..87d3819e489f00415e9383aee14ce46ab380e074 100644 (file)
@@ -811,7 +811,16 @@ static void CheckAggExprForMemSetUse(AggValueSlot &Slot, const Expr *E,
   // If the slot is already known to be zeroed, nothing to do.  Don't mess with
   // volatile stores.
   if (Slot.isZeroed() || Slot.isVolatile() || Slot.getAddr() == 0) return;
-  
+
+  // C++ objects with a user-declared constructor don't need zero'ing.
+  if (CGF.getContext().getLangOptions().CPlusPlus)
+    if (const RecordType *RT = CGF.getContext()
+                       .getBaseElementType(E->getType())->getAs<RecordType>()) {
+      const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
+      if (RD->hasUserDeclaredConstructor())
+        return;
+    }
+
   // If the type is 16-bytes or smaller, prefer individual stores over memset.
   std::pair<CharUnits, CharUnits> TypeInfo =
     CGF.getContext().getTypeInfoInChars(E->getType());
index 94f95053425ee94cb72aa56ccc514aee3c26c78c..2f3e4680a79ad746fd92dbedd15f787744750636 100644 (file)
@@ -370,8 +370,9 @@ CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E,
   
   // 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())
+  // constructor, emit the zero initialization now, unless destination is
+  // already zeroed.
+  if (E->requiresZeroInitialization() && !Dest.isZeroed())
     EmitNullInitialization(Dest.getAddr(), E->getType());
   
   // If this is a call to a trivial default constructor, do nothing.
index 890b144de3322f497edf3ae11a4d0d57ad19f572..a5a0b67d57d8161525327e6d5c569914a4a16760 100644 (file)
@@ -101,23 +101,37 @@ struct Test3 : public Test { };
 
 // CHECK: define void @_ZN6PR98011fEv
 void f() {
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98014TestC1Ei
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98014TestC1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98014TestC1Ev
   Test partial[3] = { 1 };
 
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98014TestC1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98014TestC1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98014TestC1Ev
   Test empty[3] = {};
 
+  // CHECK: call void @llvm.memset.p0i8.i64
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98015Test2C1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98015Test2C1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98015Test2C1Ev
   Test2 empty2[3] = {};
 
+  // CHECK: call void @llvm.memset.p0i8.i64
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98015Test3C1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98015Test3C1Ev
+  // CHECK-NOT: call void @llvm.memset.p0i8.i64
   // CHECK: call void @_ZN6PR98015Test3C1Ev
   Test3 empty3[3] = {};
 }