[llvm-commits] [llvm-gcc-4.2] r65541 - /llvm-gcc-4.2/trunk/gcc/llvm-types.cpp

Duncan Sands baldrick at free.fr
Thu Feb 26 09:39:42 PST 2009


Author: baldrick
Date: Thu Feb 26 11:39:42 2009
New Revision: 65541

URL: http://llvm.org/viewvc/llvm-project?rev=65541&view=rev
Log:
Do not use an array of i32 to pad structs to the same
size as the gcc type if the struct has alignment less
than 4.  Otherwise the very act of adding the padding
will increase the alignment of the struct, which can
result in the size increasing beyond the size of the
gcc type.  Testcase is test/FrontendAda/element_copy.adb
(but only triggers if Ada strings are mapped to [0 x i8]
rather than i8, as is done in a later patch). 

Modified:
    llvm-gcc-4.2/trunk/gcc/llvm-types.cpp

Modified: llvm-gcc-4.2/trunk/gcc/llvm-types.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm-gcc-4.2/trunk/gcc/llvm-types.cpp?rev=65541&r1=65540&r2=65541&view=diff

==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-types.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-types.cpp Thu Feb 26 11:39:42 2009
@@ -1355,16 +1355,22 @@
                            Packed || (!Elements.empty() && AllBitFields));
   }
   
+  /// getAlignmentAsLLVMStruct - Return the alignment of this struct if it were
+  /// converted to an LLVM type.
+  uint64_t getAlignmentAsLLVMStruct() const {
+    if (Packed || AllBitFields) return 1;
+    unsigned MaxAlign = 1;
+    for (unsigned i = 0, e = Elements.size(); i != e; ++i)
+      MaxAlign = std::max(MaxAlign, getTypeAlignment(Elements[i]));
+    return MaxAlign;
+  }
+
   /// getSizeAsLLVMStruct - Return the size of this struct if it were converted
   /// to an LLVM type.  This is the end of last element push an alignment pad at
   /// the end.
   uint64_t getSizeAsLLVMStruct() const {
     if (Elements.empty()) return 0;
-    unsigned MaxAlign = 1;
-    if (!Packed && !AllBitFields)
-      for (unsigned i = 0, e = Elements.size(); i != e; ++i)
-        MaxAlign = std::max(MaxAlign, getTypeAlignment(Elements[i]));
-    
+    unsigned MaxAlign = getAlignmentAsLLVMStruct();
     uint64_t Size = ElementOffsetInBytes.back()+ElementSizeInBytes.back();
     return (Size+MaxAlign-1) & ~(MaxAlign-1);
   }
@@ -2083,7 +2089,9 @@
       if (GCCTypeSize-LLVMLastElementEnd == 1)
         Info->addElement(Type::Int8Ty, 1, 1);
       else {
-        if ( ((GCCTypeSize-LLVMStructSize) % 4) == 0) {
+        if (((GCCTypeSize-LLVMStructSize) % 4) == 0 &&
+            (Info->getAlignmentAsLLVMStruct() %
+             Info->getTypeAlignment(Type::Int32Ty)) == 0) {
           // insert array of i32
           unsigned Int32ArraySize = (GCCTypeSize-LLVMStructSize)/4;
           const Type *PadTy = ArrayType::get(Type::Int32Ty, Int32ArraySize);





More information about the llvm-commits mailing list