[llvm-commits] [llvm-gcc-4.2] r100632 - /llvm-gcc-4.2/trunk/gcc/llvm-types.cpp
Jakob Stoklund Olesen
stoklund at 2pi.dk
Wed Apr 7 09:45:18 PDT 2010
Author: stoklund
Date: Wed Apr 7 11:45:17 2010
New Revision: 100632
URL: http://llvm.org/viewvc/llvm-project?rev=100632&view=rev
Log:
Create larger struct fields for consecutive bitfields.
When creating an LLVM struct type for a struct with bitfields, try to allocate
integer fields that are as large as possible, taking the following bitfields
into account.
For example, this struct:
struct S {
int a, b;
void *c;
unsigned d : 8;
unsigned e : 8;
};
used to be:
%struct.S = type { i32, i32, i8*, i8, i8 },
but now it becomes:
%struct.S = type { i32, i32, i8*, i16 }
This makes it easier for the code generator to shuffle register sized bitfield
groups around in fewer registers. It also means that it is harder for SROA to
isolate bit fields as scalars.
The larger struct fields are only produced when they would be properly aligned
and smaller than the natural machine word size.
This strategy is similar to what clang will be doing. We will see how it works
out.
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=100632&r1=100631&r2=100632&view=diff
==============================================================================
--- llvm-gcc-4.2/trunk/gcc/llvm-types.cpp (original)
+++ llvm-gcc-4.2/trunk/gcc/llvm-types.cpp Wed Apr 7 11:45:17 2010
@@ -1620,7 +1620,8 @@
return ~0U;
}
- void addNewBitField(uint64_t Size, uint64_t FirstUnallocatedByte);
+ void addNewBitField(uint64_t Size, uint64_t Extra,
+ uint64_t FirstUnallocatedByte);
void dump() const;
};
@@ -1628,22 +1629,43 @@
// Add new element which is a bit field. Size is not the size of bit field,
// but size of bits required to determine type of new Field which will be
// used to access this bit field.
-void StructTypeConversionInfo::addNewBitField(uint64_t Size,
+// If possible, allocate a field with room for Size+Extra bits.
+void StructTypeConversionInfo::addNewBitField(uint64_t Size, uint64_t Extra,
uint64_t FirstUnallocatedByte) {
// Figure out the LLVM type that we will use for the new field.
// Note, Size is not necessarily size of the new field. It indicates
// additional bits required after FirstunallocatedByte to cover new field.
- const Type *NewFieldTy;
- if (Size <= 8)
- NewFieldTy = Type::getInt8Ty(Context);
- else if (Size <= 16)
- NewFieldTy = Type::getInt16Ty(Context);
- else if (Size <= 32)
- NewFieldTy = Type::getInt32Ty(Context);
- else {
- assert(Size <= 64 && "Bitfield too large!");
- NewFieldTy = Type::getInt64Ty(Context);
+ const Type *NewFieldTy = 0;
+
+ // First try an ABI-aligned field including (some of) the Extra bits.
+ // This field must satisfy Size <= w && w <= XSize.
+ uint64_t XSize = RoundUpToAlignment(Size + Extra, 8);
+ for (unsigned w = NextPowerOf2(std::min(UINT64_C(64), XSize)/2);
+ w >= Size && w >= 8; w /= 2) {
+ if (TD.isIllegalInteger(w))
+ continue;
+ // Would a w-sized integer field be aligned here?
+ const unsigned a = TD.getABIIntegerTypeAlignment(w);
+ if (FirstUnallocatedByte & (a-1) || a > getGCCStructAlignmentInBytes())
+ continue;
+ // OK, use w-sized integer.
+ NewFieldTy = IntegerType::get(Context, w);
+ break;
+ }
+
+ // Try an integer field that holds Size bits.
+ if (!NewFieldTy) {
+ if (Size <= 8)
+ NewFieldTy = Type::getInt8Ty(Context);
+ else if (Size <= 16)
+ NewFieldTy = Type::getInt16Ty(Context);
+ else if (Size <= 32)
+ NewFieldTy = Type::getInt32Ty(Context);
+ else {
+ assert(Size <= 64 && "Bitfield too large!");
+ NewFieldTy = Type::getInt64Ty(Context);
+ }
}
// Check that the alignment of NewFieldTy won't cause a gap in the structure!
@@ -1987,7 +2009,19 @@
// LLVM struct such that there are no holes in the struct where the bitfield
// is: these holes would make it impossible to statically initialize a global
// of this type that has an initializer for the bitfield.
-
+
+ // We want the integer-typed fields as large as possible up to the machine
+ // word size. If there are more bitfields following this one, try to include
+ // them in the same field.
+
+ // Calculate the total number of bits in the continuous group of bitfields
+ // following this one. This is the number of bits that addNewBitField should
+ // try to include.
+ unsigned ExtraSizeInBits = 0;
+ for (tree f = TREE_CHAIN(Field); f && ExtraSizeInBits < 64 && isBitfield(f);
+ f = TREE_CHAIN(f))
+ ExtraSizeInBits += TREE_INT_CST_LOW(DECL_SIZE(f));
+
// Compute the number of bits that we need to add to this struct to cover
// this field.
uint64_t FirstUnallocatedByte = Info.getEndUnallocatedByte();
@@ -2001,7 +2035,7 @@
// This field starts at byte boundry. Need to allocate space
// for additional bytes not yet allocated.
unsigned NumBitsToAdd = FieldSizeInBits - AvailableBits;
- Info.addNewBitField(NumBitsToAdd, FirstUnallocatedByte);
+ Info.addNewBitField(NumBitsToAdd, ExtraSizeInBits, FirstUnallocatedByte);
return;
}
@@ -2025,7 +2059,7 @@
for (unsigned idx = 0; idx < (prevFieldTypeSizeInBits/8); ++idx)
FirstUnallocatedByte--;
}
- Info.addNewBitField(NumBitsRequired, FirstUnallocatedByte);
+ Info.addNewBitField(NumBitsRequired, ExtraSizeInBits, FirstUnallocatedByte);
// Do this after adding Field.
Info.lastFieldStartsAtNonByteBoundry(true);
return;
@@ -2059,7 +2093,7 @@
}
// Now, Field starts at FirstUnallocatedByte and everything is aligned.
- Info.addNewBitField(FieldSizeInBits, FirstUnallocatedByte);
+ Info.addNewBitField(FieldSizeInBits, ExtraSizeInBits, FirstUnallocatedByte);
}
/// UnionHasOnlyZeroOffsets - Check if a union type has only members with
@@ -2131,7 +2165,7 @@
if (isBitfield(UnionField)) {
unsigned FieldSizeInBits = TREE_INT_CST_LOW(DECL_SIZE(UnionField));
- Info.addNewBitField(FieldSizeInBits, 0);
+ Info.addNewBitField(FieldSizeInBits, 0, 0);
} else {
Info.allFieldsAreNotBitFields();
Info.addElement(UnionTy, 0, Info.getTypeSize(UnionTy));
More information about the llvm-commits
mailing list