[cfe-commits] r103363 - in /cfe/trunk: include/clang/AST/RecordLayout.h lib/AST/RecordLayoutBuilder.cpp lib/AST/RecordLayoutBuilder.h

Anders Carlsson andersca at mac.com
Sat May 8 16:06:26 PDT 2010


Author: andersca
Date: Sat May  8 18:06:26 2010
New Revision: 103363

URL: http://llvm.org/viewvc/llvm-project?rev=103363&view=rev
Log:
Actually compute the empty subobject sizes. No functionality change yet.

Modified:
    cfe/trunk/include/clang/AST/RecordLayout.h
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/lib/AST/RecordLayoutBuilder.h

Modified: cfe/trunk/include/clang/AST/RecordLayout.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecordLayout.h?rev=103363&r1=103362&r2=103363&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/RecordLayout.h (original)
+++ cfe/trunk/include/clang/AST/RecordLayout.h Sat May  8 18:06:26 2010
@@ -228,6 +228,11 @@
     return CXXInfo->VBaseOffsets[VBase];
   }
   
+  uint64_t getSizeOfLargestEmptySubobject() const {
+    assert(CXXInfo && "Record layout does not have C++ specific info!");
+    return CXXInfo->SizeOfLargestEmptySubobject;
+  }
+
   primary_base_info_iterator primary_base_begin() const {
     assert(CXXInfo && "Record layout does not have C++ specific info!");
   

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=103363&r1=103362&r2=103363&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Sat May  8 18:06:26 2010
@@ -24,8 +24,8 @@
 ASTRecordLayoutBuilder::ASTRecordLayoutBuilder(ASTContext &Context)
   : Context(Context), Size(0), Alignment(8), Packed(false), 
   UnfilledBitsInLastByte(0), MaxFieldAlignment(0), DataSize(0), IsUnion(false),
-  SizeOfLargestEmptySubobject(0), NonVirtualSize(0), NonVirtualAlignment(8),
-  FirstNearlyEmptyVBase(0) { }
+  NonVirtualSize(0), NonVirtualAlignment(8), FirstNearlyEmptyVBase(0),
+  SizeOfLargestEmptySubobject(0) { }
 
 /// IsNearlyEmpty - Indicates when a class has a vtable pointer, but
 /// no other data.
@@ -39,6 +39,56 @@
   return false;
 }
 
+void 
+ASTRecordLayoutBuilder::ComputeEmptySubobjectSizes(const CXXRecordDecl *RD) {
+  // Check the bases.
+  for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
+       E = RD->bases_end(); I != E; ++I) {
+    const CXXRecordDecl *BaseDecl =
+      cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
+
+    uint64_t EmptySize = 0;
+    const ASTRecordLayout &Layout = Context.getASTRecordLayout(BaseDecl);
+    if (BaseDecl->isEmpty()) {
+      // If the class decl is empty, get its size.
+      EmptySize = Layout.getSize();
+    } else {
+      // Otherwise, we get the largest empty subobject for the decl.
+      EmptySize = Layout.getSizeOfLargestEmptySubobject();
+    }
+    
+    SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject, 
+                                           EmptySize);
+  }
+
+  // Check the fields.
+  for (CXXRecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
+       I != E; ++I) {
+    const FieldDecl *FD = *I;
+    
+    const RecordType *RT = 
+      Context.getBaseElementType(FD->getType())->getAs<RecordType>();
+    
+    // We only care about record types.
+    if (!RT)
+      continue;
+
+    uint64_t EmptySize = 0;
+    const CXXRecordDecl *MemberDecl = cast<CXXRecordDecl>(RT->getDecl());
+    const ASTRecordLayout &Layout = Context.getASTRecordLayout(MemberDecl);
+    if (MemberDecl->isEmpty()) {
+      // If the class decl is empty, get its size.
+      EmptySize = Layout.getSize();
+    } else {
+      // Otherwise, we get the largest empty subobject for the decl.
+      EmptySize = Layout.getSizeOfLargestEmptySubobject();
+    }
+      
+   SizeOfLargestEmptySubobject = std::max(SizeOfLargestEmptySubobject, 
+                                          EmptySize);
+  }
+}
+
 void ASTRecordLayoutBuilder::IdentifyPrimaryBases(const CXXRecordDecl *RD) {
   const ASTRecordLayout::PrimaryBaseInfo &BaseInfo =
     Context.getASTRecordLayout(RD).getPrimaryBaseInfo();
@@ -522,8 +572,10 @@
 
   // If this is a C++ class, lay out the vtable and the non-virtual bases.
   const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D);
-  if (RD)
+  if (RD) {
+    ComputeEmptySubobjectSizes(RD);
     LayoutNonVirtualBases(RD);
+  }
 
   LayoutFields(D);
 

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.h?rev=103363&r1=103362&r2=103363&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.h (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.h Sat May  8 18:06:26 2010
@@ -77,7 +77,7 @@
   /// VisitedVirtualBases - A set of all the visited virtual bases, used to
   /// avoid visiting virtual bases more than once.
   llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
-  
+
   /// SizeOfLargestEmptySubobject - When laying out C++ classes, this holds the
   /// size of the largest empty subobject (either a base or a member).
   /// Will be zero if the record being built doesn't contain any empty classes.
@@ -99,6 +99,10 @@
   void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize);
   void LayoutBitField(const FieldDecl *D);
 
+  /// ComputeEmptySubobjectSizes - Compute the size of the largest base or 
+  /// member subobject that is empty.
+  void ComputeEmptySubobjectSizes(const CXXRecordDecl *RD);
+
   /// DeterminePrimaryBase - Determine the primary base of the given class.
   void DeterminePrimaryBase(const CXXRecordDecl *RD);
 





More information about the cfe-commits mailing list