Skip to content

Commit 100c9c0

Browse files
authored
[DataLayout] Add helper predicates to sort specifications (NFC) (#104417)
1 parent 79658d6 commit 100c9c0

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

llvm/lib/IR/DataLayout.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ bool DataLayout::PointerSpec::operator==(const PointerSpec &Other) const {
153153
IndexBitWidth == Other.IndexBitWidth;
154154
}
155155

156+
namespace {
157+
/// Predicate to sort primitive specs by bit width.
158+
struct LessPrimitiveBitWidth {
159+
bool operator()(const DataLayout::PrimitiveSpec &LHS,
160+
unsigned RHSBitWidth) const {
161+
return LHS.BitWidth < RHSBitWidth;
162+
}
163+
};
164+
165+
/// Predicate to sort pointer specs by address space number.
166+
struct LessPointerAddrSpace {
167+
bool operator()(const DataLayout::PointerSpec &LHS,
168+
unsigned RHSAddrSpace) const {
169+
return LHS.AddrSpace < RHSAddrSpace;
170+
}
171+
};
172+
} // namespace
173+
156174
const char *DataLayout::getManglingComponent(const Triple &T) {
157175
if (T.isOSBinFormatGOFF())
158176
return "-m:l";
@@ -581,15 +599,6 @@ Error DataLayout::parseSpecifier(StringRef Desc) {
581599
return Error::success();
582600
}
583601

584-
static SmallVectorImpl<DataLayout::PrimitiveSpec>::const_iterator
585-
findPrimitiveSpecLowerBound(
586-
const SmallVectorImpl<DataLayout::PrimitiveSpec> &Specs,
587-
uint32_t BitWidth) {
588-
return partition_point(Specs, [BitWidth](const DataLayout::PrimitiveSpec &E) {
589-
return E.BitWidth < BitWidth;
590-
});
591-
}
592-
593602
Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
594603
Align ABIAlign, Align PrefAlign) {
595604
// AlignmentsTy::ABIAlign and AlignmentsTy::PrefAlign were once stored as
@@ -620,9 +629,7 @@ Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
620629
break;
621630
}
622631

623-
auto I = partition_point(*Specs, [BitWidth](const PrimitiveSpec &E) {
624-
return E.BitWidth < BitWidth;
625-
});
632+
auto I = lower_bound(*Specs, BitWidth, LessPrimitiveBitWidth());
626633
if (I != Specs->end() && I->BitWidth == BitWidth) {
627634
// Update the abi, preferred alignments.
628635
I->ABIAlign = ABIAlign;
@@ -637,10 +644,7 @@ Error DataLayout::setPrimitiveSpec(TypeSpecifier Specifier, uint32_t BitWidth,
637644
const DataLayout::PointerSpec &
638645
DataLayout::getPointerSpec(uint32_t AddrSpace) const {
639646
if (AddrSpace != 0) {
640-
auto I = lower_bound(PointerSpecs, AddrSpace,
641-
[](const PointerSpec &Spec, uint32_t AddrSpace) {
642-
return Spec.AddrSpace < AddrSpace;
643-
});
647+
auto I = lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
644648
if (I != PointerSpecs.end() && I->AddrSpace == AddrSpace)
645649
return *I;
646650
}
@@ -658,10 +662,7 @@ Error DataLayout::setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth,
658662
if (IndexBitWidth > BitWidth)
659663
return reportError("Index width cannot be larger than pointer width");
660664

661-
auto I = lower_bound(PointerSpecs, AddrSpace,
662-
[](const PointerSpec &A, uint32_t AddrSpace) {
663-
return A.AddrSpace < AddrSpace;
664-
});
665+
auto I = lower_bound(PointerSpecs, AddrSpace, LessPointerAddrSpace());
665666
if (I == PointerSpecs.end() || I->AddrSpace != AddrSpace) {
666667
PointerSpecs.insert(I, PointerSpec{AddrSpace, BitWidth, ABIAlign, PrefAlign,
667668
IndexBitWidth});
@@ -676,7 +677,7 @@ Error DataLayout::setPointerSpec(uint32_t AddrSpace, uint32_t BitWidth,
676677

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

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

0 commit comments

Comments
 (0)