@@ -247,9 +247,9 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
247
247
bool SearchableTableEmitter::compareBy (const Record *LHS, const Record *RHS,
248
248
const SearchIndex &Index) {
249
249
// 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.
253
253
auto CmpLTValue = [](const auto &LHS,
254
254
const auto &RHS) -> std::optional<bool > {
255
255
if (LHS < RHS)
@@ -259,11 +259,21 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
259
259
return std::nullopt;
260
260
};
261
261
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
+
262
272
// 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 ](
267
277
const Init *LHSI, const Init *RHSI,
268
278
const GenericField &Field) -> std::optional<bool > {
269
279
if (isa<BitsRecTy>(Field.RecType ) || isa<IntRecTy>(Field.RecType )) {
@@ -275,8 +285,10 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
275
285
if (Field.IsIntrinsic ) {
276
286
const CodeGenIntrinsic &LHSi = getIntrinsic (LHSI);
277
287
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 );
280
292
}
281
293
282
294
if (Field.IsInstruction ) {
@@ -287,8 +299,9 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
287
299
// Order pseudo instructions before non-pseudo ones.
288
300
bool LHSNotPseudo = !LHSr->getValueAsBit (" isPseudo" );
289
301
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 ());
292
305
}
293
306
294
307
if (Field.Enum ) {
@@ -301,12 +314,11 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
301
314
302
315
std::string LHSs = primaryRepresentation (Index.Loc , Field, LHSI);
303
316
std::string RHSs = primaryRepresentation (Index.Loc , Field, RHSI);
304
-
305
317
if (isa<StringRecTy>(Field.RecType )) {
306
318
LHSs = StringRef (LHSs).upper ();
307
319
RHSs = StringRef (RHSs).upper ();
308
320
}
309
- return CmpLTValue (LHSs, RHSs);
321
+ return CmpLTString (LHSs, RHSs);
310
322
};
311
323
312
324
for (const GenericField &Field : Index.Fields ) {
0 commit comments