[cfe-commits] r101345 - in /cfe/trunk/lib/CodeGen: CGObjCMac.cpp CGRecordLayout.h CGRecordLayoutBuilder.cpp

Daniel Dunbar daniel at zuster.org
Wed Apr 14 22:09:32 PDT 2010


Author: ddunbar
Date: Thu Apr 15 00:09:32 2010
New Revision: 101345

URL: http://llvm.org/viewvc/llvm-project?rev=101345&view=rev
Log:
IRgen: Change CGBitFieldInfo to take the AccessInfo as constructor arguments, it is now an immutable object.

Also, add some checking of various invariants that should hold on the CGBitFieldInfo access.

Modified:
    cfe/trunk/lib/CodeGen/CGObjCMac.cpp
    cfe/trunk/lib/CodeGen/CGRecordLayout.h
    cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp

Modified: cfe/trunk/lib/CodeGen/CGObjCMac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp?rev=101345&r1=101344&r2=101345&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjCMac.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjCMac.cpp Thu Apr 15 00:09:32 2010
@@ -124,13 +124,9 @@
   // layout object. However, this is blocked on other cleanups to the
   // Objective-C code, so for now we just live with allocating a bunch of these
   // objects.
-  CGBitFieldInfo *Info =
-    new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize,
-                                              IvarTy->isSignedIntegerType());
 
   // We always construct a single, possibly unaligned, access for this case.
-  Info->setNumComponents(1);
-  CGBitFieldInfo::AccessInfo &AI = Info->getComponent(0);
+  CGBitFieldInfo::AccessInfo AI;
   AI.FieldIndex = 0;
   AI.FieldByteOffset = 0;
   AI.FieldBitStart = BitOffset;
@@ -139,6 +135,10 @@
   AI.TargetBitOffset = 0;
   AI.TargetBitWidth = BitFieldSize;
 
+  CGBitFieldInfo *Info =
+    new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
+                                              IvarTy->isSignedIntegerType());
+
   // FIXME: We need to set a very conservative alignment on this, or make sure
   // that the runtime is doing the right thing.
   return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());

Modified: cfe/trunk/lib/CodeGen/CGRecordLayout.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayout.h?rev=101345&r1=101344&r2=101345&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayout.h (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayout.h Thu Apr 15 00:09:32 2010
@@ -94,8 +94,33 @@
   bool IsSigned : 1;
 
 public:
-  CGBitFieldInfo(unsigned Size, bool IsSigned)
-    : Size(Size), IsSigned(IsSigned) {}
+  CGBitFieldInfo(unsigned Size, unsigned NumComponents, AccessInfo *_Components,
+                 bool IsSigned) : Size(Size), NumComponents(NumComponents),
+                                  IsSigned(IsSigned) {
+    assert(NumComponents <= 3 && "invalid number of components!");
+    for (unsigned i = 0; i != NumComponents; ++i)
+      Components[i] = _Components[i];
+
+    // Check some invariants.
+    unsigned AccessedSize = 0;
+    for (unsigned i = 0, e = getNumComponents(); i != e; ++i) {
+      const AccessInfo &AI = getComponent(i);
+      AccessedSize += AI.TargetBitWidth;
+
+      // We shouldn't try to load 0 bits.
+      assert(AI.TargetBitWidth > 0);
+
+      // We can't load more bits than we accessed.
+      assert(AI.FieldBitStart + AI.TargetBitWidth <= AI.AccessWidth);
+
+      // We shouldn't put any bits outside the result size.
+      assert(AI.TargetBitWidth + AI.TargetBitOffset <= Size);
+    }
+
+    // Check that the total number of target bits matches the total bit-field
+    // size.
+    assert(AccessedSize == Size && "Total size does not match accessed size!");
+  }
 
 public:
   /// \brief Check whether this bit-field access is (i.e., should be sign
@@ -109,19 +134,11 @@
   /// @{
 
   unsigned getNumComponents() const { return NumComponents; }
-  void setNumComponents(unsigned Value) {
-    assert(Value < 4 && "Invalid number of components!");
-    NumComponents = Value;
-  }
 
   const AccessInfo &getComponent(unsigned Index) const {
     assert(Index < getNumComponents() && "Invalid access!");
     return Components[Index];
   }
-  AccessInfo &getComponent(unsigned Index) {
-    assert(Index < getNumComponents() && "Invalid access!");
-    return Components[Index];
-  }
 
   /// @}
 

Modified: cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=101345&r1=101344&r2=101345&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp Thu Apr 15 00:09:32 2010
@@ -155,18 +155,19 @@
 
   unsigned StartBit = FieldOffset % TypeSizeInBits;
   bool IsSigned = FD->getType()->isSignedIntegerType();
-  CGBitFieldInfo BFI(FieldSize, IsSigned);
 
   // The current policy is to always access the bit-field using the source type
   // of the bit-field. With the C bit-field rules, this implies that we always
   // use either one or two accesses, and two accesses can only occur with a
   // packed structure when the bit-field straddles an alignment boundary.
+  CGBitFieldInfo::AccessInfo Components[2];
+
   unsigned LowBits = std::min(FieldSize, TypeSizeInBits - StartBit);
   bool NeedsHighAccess = LowBits != FieldSize;
-  BFI.setNumComponents(1 + NeedsHighAccess);
+  unsigned NumComponents = 1 + NeedsHighAccess;
 
   // FIXME: This access policy is probably wrong on big-endian systems.
-  CGBitFieldInfo::AccessInfo &LowAccess = BFI.getComponent(0);
+  CGBitFieldInfo::AccessInfo &LowAccess = Components[0];
   LowAccess.FieldIndex = 0;
   LowAccess.FieldByteOffset =
     TypeSizeInBytes * ((FieldOffset / 8) / TypeSizeInBytes);
@@ -178,7 +179,7 @@
   LowAccess.TargetBitWidth = LowBits;
 
   if (NeedsHighAccess) {
-    CGBitFieldInfo::AccessInfo &HighAccess = BFI.getComponent(1);
+    CGBitFieldInfo::AccessInfo &HighAccess = Components[1];
     HighAccess.FieldIndex = 0;
     HighAccess.FieldByteOffset = LowAccess.FieldByteOffset + TypeSizeInBytes;
     HighAccess.FieldBitStart = 0;
@@ -189,7 +190,7 @@
     HighAccess.TargetBitWidth = FieldSize - LowBits;
   }
 
-  return BFI;
+  return CGBitFieldInfo(FieldSize, NumComponents, Components, IsSigned);
 }
 
 void CGRecordLayoutBuilder::LayoutBitField(const FieldDecl *D,





More information about the cfe-commits mailing list