@@ -236,9 +236,9 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
236
236
bool SearchableTableEmitter::compareBy (const Record *LHS, const Record *RHS,
237
237
const SearchIndex &Index) {
238
238
// 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.
242
242
auto CmpLTValue = [](const auto &LHS,
243
243
const auto &RHS) -> std::optional<bool > {
244
244
if (LHS < RHS)
@@ -248,11 +248,21 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
248
248
return std::nullopt;
249
249
};
250
250
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
+
251
261
// 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 ](
256
266
const Init *LHSI, const Init *RHSI,
257
267
const GenericField &Field) -> std::optional<bool > {
258
268
if (isa<BitsRecTy>(Field.RecType ) || isa<IntRecTy>(Field.RecType )) {
@@ -264,8 +274,10 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
264
274
if (Field.IsIntrinsic ) {
265
275
const CodeGenIntrinsic &LHSi = getIntrinsic (LHSI);
266
276
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 );
269
281
}
270
282
271
283
if (Field.IsInstruction ) {
@@ -276,8 +288,9 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
276
288
// Order pseudo instructions before non-pseudo ones.
277
289
bool LHSNotPseudo = !LHSr->getValueAsBit (" isPseudo" );
278
290
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 ());
281
294
}
282
295
283
296
if (Field.Enum ) {
@@ -290,12 +303,11 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
290
303
291
304
std::string LHSs = primaryRepresentation (Index.Loc , Field, LHSI);
292
305
std::string RHSs = primaryRepresentation (Index.Loc , Field, RHSI);
293
-
294
306
if (isa<StringRecTy>(Field.RecType )) {
295
307
LHSs = StringRef (LHSs).upper ();
296
308
RHSs = StringRef (RHSs).upper ();
297
309
}
298
- return CmpLTValue (LHSs, RHSs);
310
+ return CmpLTString (LHSs, RHSs);
299
311
};
300
312
301
313
for (const GenericField &Field : Index.Fields ) {
0 commit comments