Skip to content

Commit f18da32

Browse files
committed
Use compare() to order strings
1 parent 27ed81d commit f18da32

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
@@ -236,9 +236,9 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
236236
bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
237237
const SearchIndex &Index) {
238238
// Compare two values and return:
239-
// true if LHS < RHS
240-
// false if LHS > RHS
241-
// std::nullopt if LHS == RHS
239+
// - true if LHS < RHS.
240+
// - false if LHS > RHS.
241+
// - std::nullopt if LHS == RHS.
242242
auto CmpLTValue = [](const auto &LHS,
243243
const auto &RHS) -> std::optional<bool> {
244244
if (LHS < RHS)
@@ -248,11 +248,21 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
248248
return std::nullopt;
249249
};
250250

251+
// Specialized form of `CmpLTValue` for string-like types that uses compare()
252+
// to do the comparison of the 2 strings once (instead if 2 comparisons if we
253+
// use `CmpLTValue`).
254+
auto CmpLTString = [](StringRef LHS, StringRef RHS) -> std::optional<bool> {
255+
int Cmp = LHS.compare(RHS);
256+
if (Cmp == 0)
257+
return std::nullopt;
258+
return Cmp < 0;
259+
};
260+
251261
// Compare two fields and returns:
252-
// true if LHS < RHS
253-
// false if LHS > RHS
254-
// std::nullopt if LHS == RHS
255-
auto CmpLTField = [this, &Index, CmpLTValue](
262+
// - true if LHS < RHS.
263+
// - false if LHS > RHS.
264+
// - std::nullopt if LHS == RHS.
265+
auto CmpLTField = [this, &Index, &CmpLTValue, &CmpLTString](
256266
const Init *LHSI, const Init *RHSI,
257267
const GenericField &Field) -> std::optional<bool> {
258268
if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
@@ -264,8 +274,10 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
264274
if (Field.IsIntrinsic) {
265275
const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
266276
const CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
267-
return CmpLTValue(std::tie(LHSi.TargetPrefix, LHSi.Name),
268-
std::tie(RHSi.TargetPrefix, RHSi.Name));
277+
if (std::optional<bool> Cmp =
278+
CmpLTString(LHSi.TargetPrefix, RHSi.TargetPrefix))
279+
return *Cmp;
280+
return CmpLTString(LHSi.Name, RHSi.Name);
269281
}
270282

271283
if (Field.IsInstruction) {
@@ -276,8 +288,9 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
276288
// Order pseudo instructions before non-pseudo ones.
277289
bool LHSNotPseudo = !LHSr->getValueAsBit("isPseudo");
278290
bool RHSNotPseudo = !RHSr->getValueAsBit("isPseudo");
279-
return CmpLTValue(std::tuple(LHSNotPseudo, LHSr->getName()),
280-
std::tuple(RHSNotPseudo, RHSr->getName()));
291+
if (std::optional<bool> Cmp = CmpLTValue(LHSNotPseudo, RHSNotPseudo))
292+
return *Cmp;
293+
return CmpLTString(LHSr->getName(), RHSr->getName());
281294
}
282295

283296
if (Field.Enum) {
@@ -290,12 +303,11 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
290303

291304
std::string LHSs = primaryRepresentation(Index.Loc, Field, LHSI);
292305
std::string RHSs = primaryRepresentation(Index.Loc, Field, RHSI);
293-
294306
if (isa<StringRecTy>(Field.RecType)) {
295307
LHSs = StringRef(LHSs).upper();
296308
RHSs = StringRef(RHSs).upper();
297309
}
298-
return CmpLTValue(LHSs, RHSs);
310+
return CmpLTString(LHSs, RHSs);
299311
};
300312

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

0 commit comments

Comments
 (0)