[cfe-commits] r50769 - in /cfe/trunk: lib/CodeGen/CodeGenTypes.cpp test/CodeGen/bitfield-init.c
Devang Patel
dpatel at apple.com
Tue May 6 12:57:59 PDT 2008
Author: dpatel
Date: Tue May 6 14:57:59 2008
New Revision: 50769
URL: http://llvm.org/viewvc/llvm-project?rev=50769&view=rev
Log:
Fix PR2101 - Codegen crash during bitfield initialization.
Added:
cfe/trunk/test/CodeGen/bitfield-init.c
Modified:
cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
Modified: cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenTypes.cpp?rev=50769&r1=50768&r2=50769&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenTypes.cpp Tue May 6 14:57:59 2008
@@ -521,7 +521,28 @@
unsigned StructAlign = RL.getAlignment();
if (llvmSize % StructAlign) {
unsigned StructPadding = StructAlign - (llvmSize % StructAlign);
- addPaddingFields(llvmSize + StructPadding);
+ bool needStructPadding = true;
+ if (!LLVMFields.empty()) {
+ const llvm::Type *LastFieldType = LLVMFields.back();
+ const llvm::Type *LastFieldDeclType =
+ CGT.ConvertTypeRecursive(FieldDecls.back()->getType());
+ if (LastFieldType != LastFieldDeclType) {
+ unsigned LastFieldTypeSize =
+ CGT.getTargetData().getABITypeSizeInBits(LastFieldType);
+ unsigned LastFieldDeclTypeSize =
+ CGT.getTargetData().getABITypeSizeInBits(LastFieldDeclType);
+ if (LastFieldDeclTypeSize > LastFieldTypeSize
+ && StructPadding == (LastFieldDeclTypeSize - LastFieldTypeSize)) {
+ // Replace last LLVMField with a LastFieldDeclType field will
+ // to avoid extra padding fields.
+ LLVMFields.pop_back();
+ LLVMFields.push_back(LastFieldDeclType);
+ needStructPadding = false;
+ }
+ }
+ }
+ if (needStructPadding)
+ addPaddingFields(llvmSize + StructPadding);
}
STy = llvm::StructType::get(LLVMFields);
@@ -533,8 +554,13 @@
assert(WaterMark >= llvmSize && "Invalid padding Field");
unsigned RequiredBits = WaterMark - llvmSize;
unsigned RequiredBytes = (RequiredBits + 7) / 8;
- for (unsigned i = 0; i != RequiredBytes; ++i)
- addLLVMField(llvm::Type::Int8Ty, true);
+ if (RequiredBytes == 1)
+ // This is a bitfield that is using few bits from this byte.
+ // It is not a padding field.
+ addLLVMField(llvm::Type::Int8Ty, false);
+ else
+ for (unsigned i = 0; i != RequiredBytes; ++i)
+ addLLVMField(llvm::Type::Int8Ty, true);
}
/// addLLVMField - Add llvm struct field that corresponds to llvm type Ty.
Added: cfe/trunk/test/CodeGen/bitfield-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/bitfield-init.c?rev=50769&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/bitfield-init.c (added)
+++ cfe/trunk/test/CodeGen/bitfield-init.c Tue May 6 14:57:59 2008
@@ -0,0 +1,4 @@
+// RUN: clang %s -emit-llvm
+typedef struct { unsigned int i: 1; } c;
+const c d = { 1 };
+
More information about the cfe-commits
mailing list