@@ -120,15 +120,16 @@ class SearchableTableEmitter {
120
120
return SI->getValue ().str ();
121
121
else
122
122
return SI->getAsString ();
123
- } else if (const auto *BI = dyn_cast<BitsInit>(I)) {
123
+ }
124
+ if (const auto *BI = dyn_cast<BitsInit>(I))
124
125
return " 0x" + utohexstr (getAsInt (BI));
125
- } else if (const auto *BI = dyn_cast<BitInit>(I)) {
126
+ if (const auto *BI = dyn_cast<BitInit>(I))
126
127
return BI->getValue () ? " true" : " false" ;
127
- } else if (Field.IsIntrinsic ) {
128
+ if (Field.IsIntrinsic )
128
129
return " Intrinsic::" + getIntrinsic (I).EnumName .str ();
129
- } else if (Field.IsInstruction ) {
130
+ if (Field.IsInstruction )
130
131
return I->getAsString ();
131
- } else if (Field.Enum ) {
132
+ if (Field.Enum ) {
132
133
auto *Entry = Field.Enum ->EntryMap [cast<DefInit>(I)->getDef ()];
133
134
if (!Entry)
134
135
PrintFatalError (Loc,
@@ -162,7 +163,8 @@ class SearchableTableEmitter {
162
163
if (Ctx == TypeInTempStruct)
163
164
return " std::string" ;
164
165
return " StringRef" ;
165
- } else if (const auto *BI = dyn_cast<BitsRecTy>(Field.RecType )) {
166
+ }
167
+ if (const auto *BI = dyn_cast<BitsRecTy>(Field.RecType )) {
166
168
unsigned NumBits = BI->getNumBits ();
167
169
if (NumBits <= 8 )
168
170
return " uint8_t" ;
@@ -176,11 +178,11 @@ class SearchableTableEmitter {
176
178
" ' lookup method '" + Index.Name +
177
179
" ', key field '" + Field.Name +
178
180
" ' of type bits is too large" );
179
- } else if (isa<BitRecTy>(Field.RecType )) {
181
+ }
182
+ if (isa<BitRecTy>(Field.RecType ))
180
183
return " bool" ;
181
- } else if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction ) {
184
+ if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction )
182
185
return " unsigned" ;
183
- }
184
186
PrintFatalError (Index.Loc ,
185
187
Twine (" In table '" ) + Table.Name + " ' lookup method '" +
186
188
Index.Name + " ', key field '" + Field.Name +
@@ -233,67 +235,74 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
233
235
// / key of \p Index.
234
236
bool SearchableTableEmitter::compareBy (const Record *LHS, const Record *RHS,
235
237
const SearchIndex &Index) {
236
- for (const auto &Field : Index.Fields ) {
237
- const Init *LHSI = LHS->getValueInit (Field.Name );
238
- const Init *RHSI = RHS->getValueInit (Field.Name );
238
+ // Compare two values and return:
239
+ // true if LHS < RHS
240
+ // false if LHS > RHS
241
+ // std::nullopt if LHS == RHS
242
+ auto CmpLTValue = [](const auto &LHS,
243
+ const auto &RHS) -> std::optional<bool > {
244
+ if (LHS < RHS)
245
+ return true ;
246
+ if (LHS > RHS)
247
+ return false ;
248
+ return std::nullopt;
249
+ };
239
250
251
+ // 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](
256
+ const Init *LHSI, const Init *RHSI,
257
+ const GenericField &Field) -> std::optional<bool > {
240
258
if (isa<BitsRecTy>(Field.RecType ) || isa<IntRecTy>(Field.RecType )) {
241
259
int64_t LHSi = getAsInt (LHSI);
242
260
int64_t RHSi = getAsInt (RHSI);
243
- if (LHSi < RHSi)
244
- return true ;
245
- if (LHSi > RHSi)
246
- return false ;
247
- } else if (Field.IsIntrinsic ) {
261
+ return CmpLTValue (LHSi, RHSi);
262
+ }
263
+
264
+ if (Field.IsIntrinsic ) {
248
265
const CodeGenIntrinsic &LHSi = getIntrinsic (LHSI);
249
266
const CodeGenIntrinsic &RHSi = getIntrinsic (RHSI);
250
- if (std::tie (LHSi.TargetPrefix , LHSi.Name ) <
251
- std::tie (RHSi.TargetPrefix , RHSi.Name ))
252
- return true ;
253
- if (std::tie (LHSi.TargetPrefix , LHSi.Name ) >
254
- std::tie (RHSi.TargetPrefix , RHSi.Name ))
255
- return false ;
256
- } else if (Field.IsInstruction ) {
267
+ return CmpLTValue (std::tie (LHSi.TargetPrefix , LHSi.Name ),
268
+ std::tie (RHSi.TargetPrefix , RHSi.Name ));
269
+ }
270
+
271
+ if (Field.IsInstruction ) {
257
272
// This does not correctly compare the predefined instructions!
258
273
const Record *LHSr = cast<DefInit>(LHSI)->getDef ();
259
274
const Record *RHSr = cast<DefInit>(RHSI)->getDef ();
260
275
261
- bool LHSpseudo = LHSr-> getValueAsBit ( " isPseudo " );
262
- bool RHSpseudo = RHSr ->getValueAsBit (" isPseudo" );
263
- if (LHSpseudo && !RHSpseudo)
264
- return true ;
265
- if (!LHSpseudo && RHSpseudo)
266
- return false ;
276
+ // Order pseudo instructions before non-pseudo ones.
277
+ bool LHSNotPseudo = !LHSr ->getValueAsBit (" isPseudo" );
278
+ bool RHSNotPseudo = !RHSr-> getValueAsBit ( " isPseudo " );
279
+ return CmpLTValue ( std::tuple (LHSNotPseudo, LHSr-> getName ()),
280
+ std::tuple (RHSNotPseudo, RHSr-> getName ()));
281
+ }
267
282
268
- int comp = LHSr->getName ().compare (RHSr->getName ());
269
- if (comp < 0 )
270
- return true ;
271
- if (comp > 0 )
272
- return false ;
273
- } else if (Field.Enum ) {
274
- auto LHSr = cast<DefInit>(LHSI)->getDef ();
275
- auto RHSr = cast<DefInit>(RHSI)->getDef ();
283
+ if (Field.Enum ) {
284
+ const Record *LHSr = cast<DefInit>(LHSI)->getDef ();
285
+ const Record *RHSr = cast<DefInit>(RHSI)->getDef ();
276
286
int64_t LHSv = Field.Enum ->EntryMap [LHSr]->second ;
277
287
int64_t RHSv = Field.Enum ->EntryMap [RHSr]->second ;
278
- if (LHSv < RHSv)
279
- return true ;
280
- if (LHSv > RHSv)
281
- return false ;
282
- } else {
283
- std::string LHSs = primaryRepresentation (Index.Loc , Field, LHSI);
284
- std::string RHSs = primaryRepresentation (Index.Loc , Field, RHSI);
288
+ return CmpLTValue (LHSv, RHSv);
289
+ }
285
290
286
- if (isa<StringRecTy>(Field.RecType )) {
287
- LHSs = StringRef (LHSs).upper ();
288
- RHSs = StringRef (RHSs).upper ();
289
- }
291
+ std::string LHSs = primaryRepresentation (Index.Loc , Field, LHSI);
292
+ std::string RHSs = primaryRepresentation (Index.Loc , Field, RHSI);
290
293
291
- int comp = LHSs.compare (RHSs);
292
- if (comp < 0 )
293
- return true ;
294
- if (comp > 0 )
295
- return false ;
294
+ if (isa<StringRecTy>(Field.RecType )) {
295
+ LHSs = StringRef (LHSs).upper ();
296
+ RHSs = StringRef (RHSs).upper ();
296
297
}
298
+ return CmpLTValue (LHSs, RHSs);
299
+ };
300
+
301
+ for (const GenericField &Field : Index.Fields ) {
302
+ const Init *LHSI = LHS->getValueInit (Field.Name );
303
+ const Init *RHSI = RHS->getValueInit (Field.Name );
304
+ if (std::optional<bool > Cmp = CmpLTField (LHSI, RHSI, Field))
305
+ return *Cmp;
297
306
}
298
307
return false ;
299
308
}
0 commit comments