Skip to content

Commit 27ed81d

Browse files
committed
Review feedback
1 parent 3f12ecd commit 27ed81d

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 63 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,16 @@ class SearchableTableEmitter {
120120
return SI->getValue().str();
121121
else
122122
return SI->getAsString();
123-
} else if (const auto *BI = dyn_cast<BitsInit>(I)) {
123+
}
124+
if (const auto *BI = dyn_cast<BitsInit>(I))
124125
return "0x" + utohexstr(getAsInt(BI));
125-
} else if (const auto *BI = dyn_cast<BitInit>(I)) {
126+
if (const auto *BI = dyn_cast<BitInit>(I))
126127
return BI->getValue() ? "true" : "false";
127-
} else if (Field.IsIntrinsic) {
128+
if (Field.IsIntrinsic)
128129
return "Intrinsic::" + getIntrinsic(I).EnumName.str();
129-
} else if (Field.IsInstruction) {
130+
if (Field.IsInstruction)
130131
return I->getAsString();
131-
} else if (Field.Enum) {
132+
if (Field.Enum) {
132133
auto *Entry = Field.Enum->EntryMap[cast<DefInit>(I)->getDef()];
133134
if (!Entry)
134135
PrintFatalError(Loc,
@@ -162,7 +163,8 @@ class SearchableTableEmitter {
162163
if (Ctx == TypeInTempStruct)
163164
return "std::string";
164165
return "StringRef";
165-
} else if (const auto *BI = dyn_cast<BitsRecTy>(Field.RecType)) {
166+
}
167+
if (const auto *BI = dyn_cast<BitsRecTy>(Field.RecType)) {
166168
unsigned NumBits = BI->getNumBits();
167169
if (NumBits <= 8)
168170
return "uint8_t";
@@ -176,11 +178,11 @@ class SearchableTableEmitter {
176178
"' lookup method '" + Index.Name +
177179
"', key field '" + Field.Name +
178180
"' of type bits is too large");
179-
} else if (isa<BitRecTy>(Field.RecType)) {
181+
}
182+
if (isa<BitRecTy>(Field.RecType))
180183
return "bool";
181-
} else if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction) {
184+
if (Field.Enum || Field.IsIntrinsic || Field.IsInstruction)
182185
return "unsigned";
183-
}
184186
PrintFatalError(Index.Loc,
185187
Twine("In table '") + Table.Name + "' lookup method '" +
186188
Index.Name + "', key field '" + Field.Name +
@@ -233,67 +235,74 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
233235
/// key of \p Index.
234236
bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
235237
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+
};
239250

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> {
240258
if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
241259
int64_t LHSi = getAsInt(LHSI);
242260
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) {
248265
const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
249266
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) {
257272
// This does not correctly compare the predefined instructions!
258273
const Record *LHSr = cast<DefInit>(LHSI)->getDef();
259274
const Record *RHSr = cast<DefInit>(RHSI)->getDef();
260275

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+
}
267282

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();
276286
int64_t LHSv = Field.Enum->EntryMap[LHSr]->second;
277287
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+
}
285290

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);
290293

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();
296297
}
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;
297306
}
298307
return false;
299308
}

0 commit comments

Comments
 (0)