Skip to content

[DataLayout] Add helper predicates to sort specifications (NFC) #104417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions llvm/lib/IR/DataLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ bool DataLayout::PointerSpec::operator==(const PointerSpec &Other) const {
IndexBitWidth == Other.IndexBitWidth;
}

namespace {
/// Predicate to sort primitive specs by bit width.
struct LessPrimitiveBitWidth {
bool operator()(const DataLayout::PrimitiveSpec &LHS,
unsigned RHSBitWidth) const {
return LHS.BitWidth < RHSBitWidth;
}
};

/// Predicate to sort pointer specs by address space number.
struct LessPointerAddrSpace {
bool operator()(const DataLayout::PointerSpec &LHS,
unsigned RHSAddrSpace) const {
return LHS.AddrSpace < RHSAddrSpace;
}
};
} // namespace

const char *DataLayout::getManglingComponent(const Triple &T) {
if (T.isOSBinFormatGOFF())
return "-m:l";
Expand Down Expand Up @@ -581,15 +599,6 @@ Error DataLayout::parseSpecifier(StringRef Desc) {
return Error::success();
}

static SmallVectorImpl<DataLayout::PrimitiveSpec>::const_iterator
findPrimitiveSpecLowerBound(
const SmallVectorImpl<DataLayout::PrimitiveSpec> &Specs,
uint32_t BitWidth) {
return partition_point(Specs, [BitWidth](const DataLayout::PrimitiveSpec &E) {
return E.BitWidth < BitWidth;
});
}

Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
Align ABIAlign, Align PrefAlign) {
// AlignmentsTy::ABIAlign and AlignmentsTy::PrefAlign were once stored as
Expand Down Expand Up @@ -620,9 +629,7 @@ Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
break;
}

auto I = partition_point(*Specs, [BitWidth](const PrimitiveSpec &E) {
return E.BitWidth < BitWidth;
});
auto I = lower_bound(*Specs, BitWidth, LessPrimitiveBitWidth());
if (I != Specs->end() && I->BitWidth == BitWidth) {
// Update the abi, preferred alignments.
I->ABIAlign = ABIAlign;
Expand All @@ -637,10 +644,7 @@ Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
const DataLayout::PointerSpec &
DataLayout::getPointerSpec(uint32_t AddrSpace) const {
if (AddrSpace != 0) {
auto I = lower_bound(PointerSpecs, AddrSpace,
[](const PointerSpec &Spec, uint32_t AddrSpace) {
return Spec.AddrSpace < AddrSpace;
});
auto I = lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
if (I != PointerSpecs.end() && I->AddrSpace == AddrSpace)
return *I;
}
Expand All @@ -658,10 +662,7 @@ Error DataLayout::setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth,
if (IndexBitWidth > BitWidth)
return reportError("Index width cannot be larger than pointer width");

auto I = lower_bound(PointerSpecs, AddrSpace,
[](const PointerSpec &A, uint32_t AddrSpace) {
return A.AddrSpace < AddrSpace;
});
auto I = lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
if (I == PointerSpecs.end() || I->AddrSpace != AddrSpace) {
PointerSpecs.insert(I, PointerSpec{AddrSpace, BitWidth, ABIAlign, PrefAlign,
IndexBitWidth});
Expand All @@ -676,7 +677,7 @@ Error DataLayout::setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth,

Align DataLayout::getIntegerAlignment(uint32_t BitWidth,
bool abi_or_pref) const {
auto I = findPrimitiveSpecLowerBound(IntSpecs, BitWidth);
auto I = lower_bound(IntSpecs, BitWidth, LessPrimitiveBitWidth());
// If we don't have an exact match, use alignment of next larger integer
// type. If there is none, use alignment of largest integer type by going
// back one element.
Expand Down Expand Up @@ -792,7 +793,7 @@ Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
case Type::FP128TyID:
case Type::X86_FP80TyID: {
unsigned BitWidth = getTypeSizeInBits(Ty).getFixedValue();
auto I = findPrimitiveSpecLowerBound(FloatSpecs, BitWidth);
auto I = lower_bound(FloatSpecs, BitWidth, LessPrimitiveBitWidth());
if (I != FloatSpecs.end() && I->BitWidth == BitWidth)
return abi_or_pref ? I->ABIAlign : I->PrefAlign;

Expand All @@ -807,7 +808,7 @@ Align DataLayout::getAlignment(Type *Ty, bool abi_or_pref) const {
case Type::FixedVectorTyID:
case Type::ScalableVectorTyID: {
unsigned BitWidth = getTypeSizeInBits(Ty).getKnownMinValue();
auto I = findPrimitiveSpecLowerBound(VectorSpecs, BitWidth);
auto I = lower_bound(VectorSpecs, BitWidth, LessPrimitiveBitWidth());
if (I != VectorSpecs.end() && I->BitWidth == BitWidth)
return abi_or_pref ? I->ABIAlign : I->PrefAlign;

Expand Down
Loading