diff options
author | Craig Topper <craig.topper@sifive.com> | 2021-03-30 16:43:12 -0700 |
---|---|---|
committer | Craig Topper <craig.topper@sifive.com> | 2021-03-30 17:36:50 -0700 |
commit | f59ba0849f7a148265dcc89d36a11717d365f368 (patch) | |
tree | e4a012091a8358945e1efbef592305a559ac55cd /llvm/lib/IR/DataLayout.cpp | |
parent | fb0b19c3de3b49bd6c343d6c02293cbcc77520fb (diff) | |
download | llvm-f59ba0849f7a148265dcc89d36a11717d365f368.zip llvm-f59ba0849f7a148265dcc89d36a11717d365f368.tar.gz llvm-f59ba0849f7a148265dcc89d36a11717d365f368.tar.bz2 |
[StructLayout] Use TrailingObjects to allocate space for MemberOffsets.
MemberOffsets are stored at the end of StructLayout. The class
contains a single entry array to mark the start of the member
offsets. getStructLayout calculates the additional space needed
for additional elements before allocating memory.
This patch converts this to use TrailingObjects. This simplifies
the size computation in getStructLayout and gets rid of the
single entry array.
This is prep work, but to use TypeSize instead of uint64_t for
D98169. The single entry array doesn't work with TypeSize because
TypeSize doesn't have a default constructor. We thought this
change was an improvement by itself so we've separated it out.
Reviewed By: mehdi_amini
Differential Revision: https://reviews.llvm.org/D99608
Diffstat (limited to 'llvm/lib/IR/DataLayout.cpp')
-rw-r--r-- | llvm/lib/IR/DataLayout.cpp | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp index 274ea0a..ecd74449 100644 --- a/llvm/lib/IR/DataLayout.cpp +++ b/llvm/lib/IR/DataLayout.cpp @@ -64,7 +64,7 @@ StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { // Keep track of maximum alignment constraint. StructAlignment = std::max(TyAlign, StructAlignment); - MemberOffsets[i] = StructSize; + getMemberOffsets()[i] = StructSize; // Consume space for this data item StructSize += DL.getTypeAllocSize(Ty).getFixedValue(); } @@ -80,13 +80,13 @@ StructLayout::StructLayout(StructType *ST, const DataLayout &DL) { /// getElementContainingOffset - Given a valid offset into the structure, /// return the structure index that contains it. unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { - const uint64_t *SI = - std::upper_bound(&MemberOffsets[0], &MemberOffsets[NumElements], Offset); - assert(SI != &MemberOffsets[0] && "Offset not in structure type!"); + ArrayRef<uint64_t> MemberOffsets = getMemberOffsets(); + auto SI = llvm::upper_bound(MemberOffsets, Offset); + assert(SI != MemberOffsets.begin() && "Offset not in structure type!"); --SI; assert(*SI <= Offset && "upper_bound didn't work"); - assert((SI == &MemberOffsets[0] || *(SI-1) <= Offset) && - (SI+1 == &MemberOffsets[NumElements] || *(SI+1) > Offset) && + assert((SI == MemberOffsets.begin() || *(SI - 1) <= Offset) && + (SI + 1 == MemberOffsets.end() || *(SI + 1) > Offset) && "Upper bound didn't work!"); // Multiple fields can have the same offset if any of them are zero sized. @@ -94,7 +94,7 @@ unsigned StructLayout::getElementContainingOffset(uint64_t Offset) const { // at the i32 element, because it is the last element at that offset. This is // the right one to return, because anything after it will have a higher // offset, implying that this element is non-empty. - return SI-&MemberOffsets[0]; + return SI - MemberOffsets.begin(); } //===----------------------------------------------------------------------===// @@ -678,9 +678,8 @@ const StructLayout *DataLayout::getStructLayout(StructType *Ty) const { // Otherwise, create the struct layout. Because it is variable length, we // malloc it, then use placement new. - int NumElts = Ty->getNumElements(); - StructLayout *L = (StructLayout *) - safe_malloc(sizeof(StructLayout)+(NumElts-1) * sizeof(uint64_t)); + StructLayout *L = (StructLayout *)safe_malloc( + StructLayout::totalSizeToAlloc<uint64_t>(Ty->getNumElements())); // Set SL before calling StructLayout's ctor. The ctor could cause other // entries to be added to TheMap, invalidating our reference. |