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

Chris Lattner sabre at nondot.org
Thu Dec 2 10:29:00 PST 2010


Author: lattner
Date: Thu Dec  2 12:29:00 2010
New Revision: 120722

URL: http://llvm.org/viewvc/llvm-project?rev=120722&view=rev
Log:
fix PR8726 by teaching the aggregate init optimization code to handle 
structs with references in them correctly.


Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.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=120722&r1=120721&r2=120722&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Thu Dec  2 12:29:00 2010
@@ -738,6 +738,39 @@
   if (ILE == 0 || !CGF.getTypes().isZeroInitializable(ILE->getType()))
     return CGF.getContext().getTypeSize(E->getType())/8;
   
+  // InitListExprs for structs have to be handled carefully.  If there are
+  // reference members, we need to consider the size of the reference, not the
+  // referencee.  InitListExprs for unions and arrays can't have references.
+  if (!E->getType()->isUnionType() && !E->getType()->isArrayType()) {
+    RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
+    uint64_t NumNonZeroBytes = 0;
+    
+    unsigned ILEElement = 0;
+    for (RecordDecl::field_iterator Field = SD->field_begin(),
+         FieldEnd = SD->field_end(); Field != FieldEnd; ++Field) {
+      // We're done once we hit the flexible array member or run out of
+      // InitListExpr elements.
+      if (Field->getType()->isIncompleteArrayType() ||
+          ILEElement == ILE->getNumInits())
+        break;
+      if (Field->isUnnamedBitfield())
+        continue;
+
+      const Expr *E = ILE->getInit(ILEElement++);
+      
+      // Reference values are always non-null and have the width of a pointer.
+      if (Field->getType()->isReferenceType()) {
+        NumNonZeroBytes += CGF.getContext().Target.getPointerWidth(0);
+        continue;
+      }      
+      
+      NumNonZeroBytes += GetNumNonZeroBytesInInit(E, CGF);
+    }
+    
+    return NumNonZeroBytes;
+  }
+  
+  
   uint64_t NumNonZeroBytes = 0;
   for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i)
     NumNonZeroBytes += GetNumNonZeroBytesInInit(ILE->getInit(i), CGF);

Modified: cfe/trunk/test/CodeGenCXX/value-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/value-init.cpp?rev=120722&r1=120721&r2=120722&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/value-init.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/value-init.cpp Thu Dec  2 12:29:00 2010
@@ -138,3 +138,16 @@
   // CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
   // CHECK-NEXT: ret void
 }
+
+namespace PR8726 {
+class C;
+struct S {
+  const C &c1;
+  int i;
+  const C &c2;
+};
+void f(const C& c) {
+  S s = {c, 42, c};
+}
+
+}
\ No newline at end of file





More information about the cfe-commits mailing list