Skip to content

Commit

Permalink
[DataLayout] Move operator= to cpp file (NFC) (#102849)
Browse files Browse the repository at this point in the history
`DataLayout` isn't exactly cheap to copy (448 bytes on a 64-bit host).
Move `operator=` to cpp file to improve compilation time. Also move
`operator==` closer to `operator=` and add a couple of FIXMEs.
  • Loading branch information
s-barannikov committed Aug 12, 2024
1 parent f696489 commit 875b652
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
24 changes: 3 additions & 21 deletions llvm/include/llvm/IR/DataLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ class DataLayout {
bool BigEndian;

unsigned AllocaAddrSpace;
MaybeAlign StackNaturalAlign;
unsigned ProgramAddrSpace;
unsigned DefaultGlobalsAddrSpace;

MaybeAlign StackNaturalAlign;
MaybeAlign FunctionPtrAlign;
FunctionPtrAlignType TheFunctionPtrAlignType;

Expand All @@ -139,6 +139,7 @@ class DataLayout {
};
ManglingModeT ManglingMode;

// FIXME: `unsigned char` truncates the value parsed by `parseSpecifier`.
SmallVector<unsigned char, 8> LegalIntWidths;

/// Primitive type alignment data. This is sorted by type and bit
Expand Down Expand Up @@ -201,26 +202,7 @@ class DataLayout {

~DataLayout(); // Not virtual, do not subclass this class

DataLayout &operator=(const DataLayout &DL) {
clear();
StringRepresentation = DL.StringRepresentation;
BigEndian = DL.isBigEndian();
AllocaAddrSpace = DL.AllocaAddrSpace;
StackNaturalAlign = DL.StackNaturalAlign;
FunctionPtrAlign = DL.FunctionPtrAlign;
TheFunctionPtrAlignType = DL.TheFunctionPtrAlignType;
ProgramAddrSpace = DL.ProgramAddrSpace;
DefaultGlobalsAddrSpace = DL.DefaultGlobalsAddrSpace;
ManglingMode = DL.ManglingMode;
LegalIntWidths = DL.LegalIntWidths;
IntAlignments = DL.IntAlignments;
FloatAlignments = DL.FloatAlignments;
VectorAlignments = DL.VectorAlignments;
StructAlignment = DL.StructAlignment;
Pointers = DL.Pointers;
NonIntegralAddressSpaces = DL.NonIntegralAddressSpaces;
return *this;
}
DataLayout &operator=(const DataLayout &Other);

bool operator==(const DataLayout &Other) const;
bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
Expand Down
58 changes: 39 additions & 19 deletions llvm/lib/IR/DataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,45 @@ void DataLayout::reset(StringRef Desc) {
return report_fatal_error(std::move(Err));
}

DataLayout &DataLayout::operator=(const DataLayout &Other) {
clear();
StringRepresentation = Other.StringRepresentation;
BigEndian = Other.BigEndian;
AllocaAddrSpace = Other.AllocaAddrSpace;
ProgramAddrSpace = Other.ProgramAddrSpace;
DefaultGlobalsAddrSpace = Other.DefaultGlobalsAddrSpace;
StackNaturalAlign = Other.StackNaturalAlign;
FunctionPtrAlign = Other.FunctionPtrAlign;
TheFunctionPtrAlignType = Other.TheFunctionPtrAlignType;
ManglingMode = Other.ManglingMode;
LegalIntWidths = Other.LegalIntWidths;
IntAlignments = Other.IntAlignments;
FloatAlignments = Other.FloatAlignments;
VectorAlignments = Other.VectorAlignments;
StructAlignment = Other.StructAlignment;
Pointers = Other.Pointers;
NonIntegralAddressSpaces = Other.NonIntegralAddressSpaces;
return *this;
}

bool DataLayout::operator==(const DataLayout &Other) const {
// NOTE: StringRepresentation might differ, it is not canonicalized.
// FIXME: NonIntegralAddressSpaces isn't compared.
return BigEndian == Other.BigEndian &&
AllocaAddrSpace == Other.AllocaAddrSpace &&
ProgramAddrSpace == Other.ProgramAddrSpace &&
DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace &&
StackNaturalAlign == Other.StackNaturalAlign &&
FunctionPtrAlign == Other.FunctionPtrAlign &&
TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
ManglingMode == Other.ManglingMode &&
LegalIntWidths == Other.LegalIntWidths &&
IntAlignments == Other.IntAlignments &&
FloatAlignments == Other.FloatAlignments &&
VectorAlignments == Other.VectorAlignments &&
StructAlignment == Other.StructAlignment && Pointers == Other.Pointers;
}

Expected<DataLayout> DataLayout::parse(StringRef LayoutDescription) {
DataLayout Layout("");
if (Error Err = Layout.parseSpecifier(LayoutDescription))
Expand Down Expand Up @@ -556,25 +595,6 @@ DataLayout::DataLayout(const Module *M) {

void DataLayout::init(const Module *M) { *this = M->getDataLayout(); }

bool DataLayout::operator==(const DataLayout &Other) const {
bool Ret = BigEndian == Other.BigEndian &&
AllocaAddrSpace == Other.AllocaAddrSpace &&
StackNaturalAlign == Other.StackNaturalAlign &&
ProgramAddrSpace == Other.ProgramAddrSpace &&
DefaultGlobalsAddrSpace == Other.DefaultGlobalsAddrSpace &&
FunctionPtrAlign == Other.FunctionPtrAlign &&
TheFunctionPtrAlignType == Other.TheFunctionPtrAlignType &&
ManglingMode == Other.ManglingMode &&
LegalIntWidths == Other.LegalIntWidths &&
IntAlignments == Other.IntAlignments &&
FloatAlignments == Other.FloatAlignments &&
VectorAlignments == Other.VectorAlignments &&
StructAlignment == Other.StructAlignment &&
Pointers == Other.Pointers;
// Note: getStringRepresentation() might differs, it is not canonicalized
return Ret;
}

static SmallVectorImpl<LayoutAlignElem>::const_iterator
findAlignmentLowerBound(const SmallVectorImpl<LayoutAlignElem> &Alignments,
uint32_t BitWidth) {
Expand Down

0 comments on commit 875b652

Please sign in to comment.