Skip to content

Commit b9f9093

Browse files
committed
Use compare() to order strings
1 parent 0a4f6ae commit b9f9093

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
247247
bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
248248
const SearchIndex &Index) {
249249
// Compare two values and return:
250-
// true if LHS < RHS
251-
// false if LHS > RHS
252-
// std::nullopt if LHS == RHS
250+
// - true if LHS < RHS.
251+
// - false if LHS > RHS.
252+
// - std::nullopt if LHS == RHS.
253253
auto CmpLTValue = [](const auto &LHS,
254254
const auto &RHS) -> std::optional<bool> {
255255
if (LHS < RHS)
@@ -259,11 +259,21 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
259259
return std::nullopt;
260260
};
261261

262+
// Specialized form of `CmpLTValue` for string-like types that uses compare()
263+
// to do the comparison of the 2 strings once (instead if 2 comparisons if we
264+
// use `CmpLTValue`).
265+
auto CmpLTString = [](StringRef LHS, StringRef RHS) -> std::optional<bool> {
266+
int Cmp = LHS.compare(RHS);
267+
if (Cmp == 0)
268+
return std::nullopt;
269+
return Cmp < 0;
270+
};
271+
262272
// Compare two fields and returns:
263-
// true if LHS < RHS
264-
// false if LHS > RHS
265-
// std::nullopt if LHS == RHS
266-
auto CmpLTField = [this, &Index, CmpLTValue](
273+
// - true if LHS < RHS.
274+
// - false if LHS > RHS.
275+
// - std::nullopt if LHS == RHS.
276+
auto CmpLTField = [this, &Index, &CmpLTValue, &CmpLTString](
267277
const Init *LHSI, const Init *RHSI,
268278
const GenericField &Field) -> std::optional<bool> {
269279
if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
@@ -275,8 +285,10 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
275285
if (Field.IsIntrinsic) {
276286
const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
277287
const CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
278-
return CmpLTValue(std::tie(LHSi.TargetPrefix, LHSi.Name),
279-
std::tie(RHSi.TargetPrefix, RHSi.Name));
288+
if (std::optional<bool> Cmp =
289+
CmpLTString(LHSi.TargetPrefix, RHSi.TargetPrefix))
290+
return *Cmp;
291+
return CmpLTString(LHSi.Name, RHSi.Name);
280292
}
281293

282294
if (Field.IsInstruction) {
@@ -287,8 +299,9 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
287299
// Order pseudo instructions before non-pseudo ones.
288300
bool LHSNotPseudo = !LHSr->getValueAsBit("isPseudo");
289301
bool RHSNotPseudo = !RHSr->getValueAsBit("isPseudo");
290-
return CmpLTValue(std::tuple(LHSNotPseudo, LHSr->getName()),
291-
std::tuple(RHSNotPseudo, RHSr->getName()));
302+
if (std::optional<bool> Cmp = CmpLTValue(LHSNotPseudo, RHSNotPseudo))
303+
return *Cmp;
304+
return CmpLTString(LHSr->getName(), RHSr->getName());
292305
}
293306

294307
if (Field.Enum) {
@@ -301,12 +314,11 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
301314

302315
std::string LHSs = primaryRepresentation(Index.Loc, Field, LHSI);
303316
std::string RHSs = primaryRepresentation(Index.Loc, Field, RHSI);
304-
305317
if (isa<StringRecTy>(Field.RecType)) {
306318
LHSs = StringRef(LHSs).upper();
307319
RHSs = StringRef(RHSs).upper();
308320
}
309-
return CmpLTValue(LHSs, RHSs);
321+
return CmpLTString(LHSs, RHSs);
310322
};
311323

312324
for (const GenericField &Field : Index.Fields) {

0 commit comments

Comments
 (0)