[cfe-commits] r125332 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp lib/AST/RecordLayoutBuilder.cpp lib/CodeGen/CGExprConstant.cpp lib/CodeGen/CGObjCMac.cpp lib/CodeGen/CGRecordLayoutBuilder.cpp

Ken Dyck kd at kendyck.com
Thu Feb 10 17:54:29 PST 2011


Author: kjdyck
Date: Thu Feb 10 19:54:29 2011
New Revision: 125332

URL: http://llvm.org/viewvc/llvm-project?rev=125332&view=rev
Log:
Add a helper function, ASTContext::toBits(), that converts sizes in
CharUnits to sizes in bits, and use it to tidy up the places where the
conversion was done explicitly.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp
    cfe/trunk/lib/CodeGen/CGObjCMac.cpp
    cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=125332&r1=125331&r2=125332&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Thu Feb 10 19:54:29 2011
@@ -1015,6 +1015,9 @@
   /// toCharUnitsFromBits - Convert a size in bits to a size in characters.
   CharUnits toCharUnitsFromBits(int64_t BitSize) const;
 
+  /// toBits - Convert a size in characters to a size in bits.
+  int64_t toBits(CharUnits CharSize) const;
+
   /// getTypeSizeInChars - Return the size of the specified type, in characters.
   /// This method does not work on incomplete types.
   CharUnits getTypeSizeInChars(QualType T) const;

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=125332&r1=125331&r2=125332&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Thu Feb 10 19:54:29 2011
@@ -847,7 +847,7 @@
   case Type::ObjCInterface: {
     const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
     const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
-    Width = Layout.getSize().getQuantity() * getCharWidth();
+    Width = toBits(Layout.getSize());
     Align = Layout.getAlignment();
     break;
   }
@@ -866,7 +866,7 @@
 
     const RecordType *RT = cast<RecordType>(TT);
     const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
-    Width = Layout.getSize().getQuantity() * getCharWidth();
+    Width = toBits(Layout.getSize());
     Align = Layout.getAlignment();
     break;
   }
@@ -923,6 +923,11 @@
   return CharUnits::fromQuantity(BitSize / getCharWidth());
 }
 
+/// toBits - Convert a size in characters to a size in characters.
+int64_t ASTContext::toBits(CharUnits CharSize) const {
+  return CharSize.getQuantity() * getCharWidth();
+}
+
 /// getTypeSizeInChars - Return the size of the specified type, in characters.
 /// This method does not work on incomplete types.
 CharUnits ASTContext::getTypeSizeInChars(QualType T) const {

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=125332&r1=125331&r2=125332&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Thu Feb 10 19:54:29 2011
@@ -616,11 +616,6 @@
       DataSize(0), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0),
       PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
 
-  // FIXME: Remove this.
-  uint64_t toOffset(CharUnits Offset) const {
-    return Offset.getQuantity() * Context.getCharWidth();
-  }
-  
   void Layout(const RecordDecl *D);
   void Layout(const CXXRecordDecl *D);
   void Layout(const ObjCInterfaceDecl *D);
@@ -1091,14 +1086,13 @@
   // If we have an empty base class, try to place it at offset 0.
   if (Base->Class->isEmpty() &&
       EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) {
-    Size = std::max(Size, 
-                    Layout.getSize().getQuantity() * Context.getCharWidth());
+    uint64_t RecordSizeInBits = Context.toBits(Layout.getSize());
+    Size = std::max(Size, RecordSizeInBits);
 
     return CharUnits::Zero();
   }
 
-  unsigned UnpackedBaseAlign = 
-    Layout.getNonVirtualAlign().getQuantity() * Context.getCharWidth();
+  unsigned UnpackedBaseAlign = Context.toBits(Layout.getNonVirtualAlign());
   unsigned BaseAlign = (Packed) ? 8 : UnpackedBaseAlign;
 
   // The maximum field alignment overrides base align.
@@ -1117,13 +1111,11 @@
 
   if (!Base->Class->isEmpty()) {
     // Update the data size.
-    DataSize = Offset + 
-      (Layout.getNonVirtualSize().getQuantity() * Context.getCharWidth());
+    DataSize = Offset + Context.toBits(Layout.getNonVirtualSize());
 
     Size = std::max(Size, DataSize);
   } else
-    Size = std::max(Size, 
-            Offset + (Layout.getSize().getQuantity() * Context.getCharWidth()));
+    Size = std::max(Size, Offset + Context.toBits(Layout.getSize()));
 
   // Remember max struct/class alignment.
   UpdateAlignment(BaseAlign, UnpackedBaseAlign);
@@ -1885,7 +1877,7 @@
   RD->dump();
   OS << "\nLayout: ";
   OS << "<ASTRecordLayout\n";
-  OS << "  Size:" << Info.getSize().getQuantity() * getCharWidth() << "\n";
+  OS << "  Size:" << toBits(Info.getSize()) << "\n";
   OS << "  DataSize:" << Info.getDataSize() << "\n";
   OS << "  Alignment:" << Info.getAlignment() << "\n";
   OS << "  FieldOffsets: [";

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=125332&r1=125331&r2=125332&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Thu Feb 10 19:54:29 2011
@@ -394,8 +394,7 @@
   }
 
   // Append tail padding if necessary.
-  AppendTailPadding(
-    Layout.getSize().getQuantity() * CGM.getContext().getCharWidth());
+  AppendTailPadding(CGM.getContext().toBits(Layout.getSize()));
 
   assert(Layout.getSize().getQuantity() == NextFieldOffsetInBytes &&
          "Tail padding mismatch!");

Modified: cfe/trunk/lib/CodeGen/CGObjCMac.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGObjCMac.cpp?rev=125332&r1=125331&r2=125332&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGObjCMac.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGObjCMac.cpp Thu Feb 10 19:54:29 2011
@@ -131,8 +131,7 @@
   // a synthesized ivar can never be a bit-field, so this is safe.
   const ASTRecordLayout &RL =
     CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
-  uint64_t TypeSizeInBits = 
-    RL.getSize().getQuantity() * CGF.CGM.getContext().getCharWidth();
+  uint64_t TypeSizeInBits = CGF.CGM.getContext().toBits(RL.getSize());
   uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
   uint64_t BitOffset = FieldBitOffset % 8;
   uint64_t ContainingTypeAlign = 8;

Modified: cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=125332&r1=125331&r2=125332&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGRecordLayoutBuilder.cpp Thu Feb 10 19:54:29 2011
@@ -300,8 +300,7 @@
                                         uint64_t FieldSize) {
   const RecordDecl *RD = FD->getParent();
   const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD);
-  uint64_t ContainingTypeSizeInBits = 
-    RL.getSize().getQuantity() * Types.getContext().getCharWidth();
+  uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize());
   unsigned ContainingTypeAlign = RL.getAlignment();
 
   return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits,
@@ -689,8 +688,7 @@
   }
   
   // Append tail padding if necessary.
-  AppendTailPadding(
-    Layout.getSize().getQuantity() * Types.getContext().getCharWidth());
+  AppendTailPadding(Types.getContext().toBits(Layout.getSize()));
 
   return true;
 }
@@ -855,8 +853,7 @@
   // Verify that the computed LLVM struct size matches the AST layout size.
   const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D);
 
-  uint64_t TypeSizeInBits = 
-    Layout.getSize().getQuantity() * getContext().getCharWidth();
+  uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize());
   assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) &&
          "Type size mismatch!");
 
@@ -867,7 +864,7 @@
       NonVirtualSize.RoundUpToAlignment(NonVirtualAlign);
 
     uint64_t AlignedNonVirtualTypeSizeInBits = 
-      AlignedNonVirtualTypeSize.getQuantity() * getContext().getCharWidth();
+      getContext().toBits(AlignedNonVirtualTypeSize);
 
     assert(AlignedNonVirtualTypeSizeInBits == 
            getTargetData().getTypeAllocSizeInBits(BaseTy) &&





More information about the cfe-commits mailing list