[cfe-commits] r126628 - in /cfe/trunk: lib/AST/RecordLayoutBuilder.cpp test/CodeGen/union.c

Ken Dyck kd at kendyck.com
Sun Feb 27 18:01:38 PST 2011


Author: kjdyck
Date: Sun Feb 27 20:01:38 2011
New Revision: 126628

URL: http://llvm.org/viewvc/llvm-project?rev=126628&view=rev
Log:
Retry r126357. Use CharUnits for the Size and DataSize calculations when
they are known to be exact multiples of the width of the char type. Add a
test case to CodeGen/union.c that would have caught the problem with the
previous attempt. No change in functionality intended.

Modified:
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/test/CodeGen/union.c

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=126628&r1=126627&r2=126628&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Sun Feb 27 20:01:38 2011
@@ -815,8 +815,8 @@
   assert(DataSize == 0 && "Vtable pointer must be at offset zero!");
 
   // Update the size.
-  setSize(getSizeInBits() + GetVirtualPointersSize(RD));
-  setDataSize(getSizeInBits());
+  setSize(getSize() + Context.toCharUnitsFromBits(GetVirtualPointersSize(RD)));
+  setDataSize(getSize());
 
   CharUnits UnpackedBaseAlign = 
     Context.toCharUnitsFromBits(Context.Target.getPointerAlign(0));
@@ -1108,8 +1108,7 @@
   // If we have an empty base class, try to place it at offset 0.
   if (Base->Class->isEmpty() &&
       EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
-    uint64_t RecordSizeInBits = Context.toBits(Layout.getSize());
-    setSize(std::max(getSizeInBits(), RecordSizeInBits));
+    setSize(std::max(getSize(), Layout.getSize()));
 
     return CharUnits::Zero();
   }
@@ -1124,27 +1123,24 @@
   }
 
   // Round up the current record size to the base's alignment boundary.
-  uint64_t Offset = 
-    llvm::RoundUpToAlignment(getDataSizeInBits(), Context.toBits(BaseAlign));
+  CharUnits Offset = getDataSize().RoundUpToAlignment(BaseAlign);
 
   // Try to place the base.
-  while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, 
-                                          Context.toCharUnitsFromBits(Offset)))
-    Offset += Context.toBits(BaseAlign);
+  while (!EmptySubobjects->CanPlaceBaseAtOffset(Base, Offset))
+    Offset += BaseAlign;
 
   if (!Base->Class->isEmpty()) {
     // Update the data size.
-    setDataSize(Offset + Context.toBits(Layout.getNonVirtualSize()));
+    setDataSize(Offset + Layout.getNonVirtualSize());
 
-    setSize(std::max(getSizeInBits(), getDataSizeInBits()));
+    setSize(std::max(getSize(), getDataSize()));
   } else
-    setSize(std::max(getSizeInBits(), 
-                     Offset + Context.toBits(Layout.getSize())));
+    setSize(std::max(getSize(), Offset + Layout.getSize()));
 
   // Remember max struct/class alignment.
   UpdateAlignment(BaseAlign, UnpackedBaseAlign);
 
-  return Context.toCharUnitsFromBits(Offset);
+  return Offset;
 }
 
 void RecordLayoutBuilder::InitializeLayout(const Decl *D) {
@@ -1233,7 +1229,7 @@
     // We start laying out ivars not at the end of the superclass
     // structure, but at the next byte following the last field.
     setSize(SL.getDataSize());
-    setDataSize(getSizeInBits());
+    setDataSize(getSize());
   }
 
   InitializeLayout(D);
@@ -1487,7 +1483,7 @@
   if (IsUnion)
     setSize(std::max(getSizeInBits(), FieldSizeInBits));
   else
-    setSize(Context.toBits(FieldOffset) + FieldSizeInBits);
+    setSize(FieldOffset + FieldSize);
 
   // Update the data size.
   setDataSize(getSizeInBits());
@@ -1504,17 +1500,18 @@
       // which is not empty but of size 0; such as having fields of
       // array of zero-length, remains of Size 0
       if (RD->isEmpty())
-        setSize(8);
+        setSize(CharUnits::One());
     }
     else
-      setSize(8);
+      setSize(CharUnits::One());
   }
   // Finally, round the size of the record up to the alignment of the
   // record itself.
   uint64_t UnpaddedSize = getSizeInBits() - UnfilledBitsInLastByte;
-  uint64_t UnpackedSize = 
+  uint64_t UnpackedSizeInBits = 
     llvm::RoundUpToAlignment(getSizeInBits(), 
                              Context.toBits(UnpackedAlignment));
+  CharUnits UnpackedSize = Context.toCharUnitsFromBits(UnpackedSizeInBits);
   setSize(llvm::RoundUpToAlignment(getSizeInBits(), Context.toBits(Alignment)));
 
   unsigned CharBitNum = Context.Target.getCharWidth();
@@ -1536,7 +1533,7 @@
     // Warn if we packed it unnecessarily. If the alignment is 1 byte don't
     // bother since there won't be alignment issues.
     if (Packed && UnpackedAlignment > CharUnits::One() && 
-        getSizeInBits() == UnpackedSize)
+        getSize() == UnpackedSize)
       Diag(D->getLocation(), diag::warn_unnecessary_packed)
           << Context.getTypeDeclType(RD);
   }
@@ -1713,17 +1710,15 @@
     bool IsPODForThePurposeOfLayout = cast<CXXRecordDecl>(D)->isPOD();
 
     // FIXME: This should be done in FinalizeLayout.
-    uint64_t DataSize =
-      IsPODForThePurposeOfLayout ? Builder->Size : Builder->DataSize;
-    CharUnits NonVirtualSize =
-      IsPODForThePurposeOfLayout ? 
-        toCharUnitsFromBits(DataSize) : Builder->NonVirtualSize;
+    CharUnits DataSize =
+      IsPODForThePurposeOfLayout ? Builder->getSize() : Builder->getDataSize();
+    CharUnits NonVirtualSize = 
+      IsPODForThePurposeOfLayout ? DataSize : Builder->NonVirtualSize;
 
-    CharUnits RecordSize = toCharUnitsFromBits(Builder->Size);
     NewEntry =
-      new (*this) ASTRecordLayout(*this, RecordSize, 
+      new (*this) ASTRecordLayout(*this, Builder->getSize(), 
                                   Builder->Alignment,
-                                  toCharUnitsFromBits(DataSize), 
+                                  DataSize, 
                                   Builder->FieldOffsets.data(),
                                   Builder->FieldOffsets.size(),
                                   NonVirtualSize,
@@ -1736,12 +1731,10 @@
     RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
     Builder.Layout(D);
 
-    CharUnits RecordSize = toCharUnitsFromBits(Builder.Size);
-
     NewEntry =
-      new (*this) ASTRecordLayout(*this, RecordSize, 
+      new (*this) ASTRecordLayout(*this, Builder.getSize(), 
                                   Builder.Alignment,
-                                  toCharUnitsFromBits(Builder.Size),
+                                  Builder.getSize(),
                                   Builder.FieldOffsets.data(),
                                   Builder.FieldOffsets.size());
   }
@@ -1797,12 +1790,10 @@
   RecordLayoutBuilder Builder(*this, /*EmptySubobjects=*/0);
   Builder.Layout(D);
 
-  CharUnits RecordSize = toCharUnitsFromBits(Builder.Size);
-
   const ASTRecordLayout *NewEntry =
-    new (*this) ASTRecordLayout(*this, RecordSize, 
+    new (*this) ASTRecordLayout(*this, Builder.getSize(), 
                                 Builder.Alignment,
-                                toCharUnitsFromBits(Builder.DataSize),
+                                Builder.getDataSize(),
                                 Builder.FieldOffsets.data(),
                                 Builder.FieldOffsets.size());
 

Modified: cfe/trunk/test/CodeGen/union.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/union.c?rev=126628&r1=126627&r2=126628&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/union.c (original)
+++ cfe/trunk/test/CodeGen/union.c Sun Feb 27 20:01:38 2011
@@ -42,3 +42,5 @@
 // PR6164
 typedef union T0 { unsigned int : 0; } T0;
 T0 t0;
+
+union { int large_bitfield: 31; char c } u2;





More information about the cfe-commits mailing list