[llvm-branch-commits] [cfe-branch] r102078 - in /cfe/branches/Apple/williamson: lib/CodeGen/CGRecordLayoutBuilder.cpp test/CodeGenCXX/bitfield-layout.cpp

Daniel Dunbar daniel at zuster.org
Thu Apr 22 10:01:29 PDT 2010


Author: ddunbar
Date: Thu Apr 22 12:01:29 2010
New Revision: 102078

URL: http://llvm.org/viewvc/llvm-project?rev=102078&view=rev
Log:
Unnamed bit-fields in a union should be laid out with a type that doesn't affect alignment.

Modified:
    cfe/branches/Apple/williamson/lib/CodeGen/CGRecordLayoutBuilder.cpp
    cfe/branches/Apple/williamson/test/CodeGenCXX/bitfield-layout.cpp

Modified: cfe/branches/Apple/williamson/lib/CodeGen/CGRecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/williamson/lib/CodeGen/CGRecordLayoutBuilder.cpp?rev=102078&r1=102077&r2=102078&view=diff
==============================================================================
--- cfe/branches/Apple/williamson/lib/CodeGen/CGRecordLayoutBuilder.cpp (original)
+++ cfe/branches/Apple/williamson/lib/CodeGen/CGRecordLayoutBuilder.cpp Thu Apr 22 12:01:29 2010
@@ -332,10 +332,26 @@
     if (FieldSize == 0)
       return 0;
 
+    const llvm::Type *FieldTy;
+
+    if (!Field->getDeclName()) {
+      // This is an unnamed bit-field, which shouldn't affect alignment on the
+      // struct so we use an array of bytes for it.
+
+      FieldTy = llvm::Type::getInt8Ty(Types.getLLVMContext());
+
+      unsigned NumBytesToAppend =
+        llvm::RoundUpToAlignment(FieldSize, 8) / 8;
+
+      if (NumBytesToAppend > 1)
+        FieldTy = llvm::ArrayType::get(FieldTy, NumBytesToAppend);
+    } else 
+      FieldTy = Types.ConvertTypeForMemRecursive(Field->getType());
+    
     // Add the bit field info.
     LLVMBitFields.push_back(
       LLVMBitFieldInfo(Field, ComputeBitFieldInfo(Types, Field, 0, FieldSize)));
-    return Types.ConvertTypeForMemRecursive(Field->getType());
+    return FieldTy;
   }
   
   // This is a regular union field.

Modified: cfe/branches/Apple/williamson/test/CodeGenCXX/bitfield-layout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/Apple/williamson/test/CodeGenCXX/bitfield-layout.cpp?rev=102078&r1=102077&r2=102078&view=diff
==============================================================================
--- cfe/branches/Apple/williamson/test/CodeGenCXX/bitfield-layout.cpp (original)
+++ cfe/branches/Apple/williamson/test/CodeGenCXX/bitfield-layout.cpp Thu Apr 22 12:01:29 2010
@@ -1,9 +1,17 @@
 // RUN: %clang_cc1 %s -triple=x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
 
-// CHECK: = type { i32, [4 x i8] }
+// CHECK: %union.Test1 = type { i32, [4 x i8] }
 union Test1 {
   int a;
   int b: 39;
-};
+} t1;
 
-Test1 t1;
+// CHECK: %union.Test2 = type { i8 }
+union Test2 {
+  int : 6;
+} t2;
+
+// CHECK: %union.Test3 = type { [2 x i8] }
+union Test3 {
+  int : 9;
+} t3;





More information about the llvm-branch-commits mailing list