Skip to content

Commit 2390e7f

Browse files
committed
Use -1/0/1 instead of optional<bool> for comparison result
1 parent b9f9093 commit 2390e7f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -247,35 +247,31 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
247247
bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
248248
const SearchIndex &Index) {
249249
// Compare two values and return:
250-
// - true if LHS < RHS.
251-
// - false if LHS > RHS.
252-
// - std::nullopt if LHS == RHS.
253-
auto CmpLTValue = [](const auto &LHS,
254-
const auto &RHS) -> std::optional<bool> {
250+
// * -1 if LHS < RHS.
251+
// * 1 if LHS > RHS.
252+
// * 0 if LHS == RHS.
253+
auto CmpLTValue = [](const auto &LHS, const auto &RHS) -> int {
255254
if (LHS < RHS)
256-
return true;
255+
return -1;
257256
if (LHS > RHS)
258-
return false;
259-
return std::nullopt;
257+
return 1;
258+
return 0;
260259
};
261260

262261
// Specialized form of `CmpLTValue` for string-like types that uses compare()
263262
// to do the comparison of the 2 strings once (instead if 2 comparisons if we
264263
// 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;
264+
auto CmpLTString = [](StringRef LHS, StringRef RHS) -> int {
265+
return LHS.compare(RHS);
270266
};
271267

272268
// Compare two fields and returns:
273269
// - true if LHS < RHS.
274270
// - false if LHS > RHS.
275271
// - std::nullopt if LHS == RHS.
276-
auto CmpLTField = [this, &Index, &CmpLTValue, &CmpLTString](
277-
const Init *LHSI, const Init *RHSI,
278-
const GenericField &Field) -> std::optional<bool> {
272+
auto CmpLTField = [this, &Index, &CmpLTValue,
273+
&CmpLTString](const Init *LHSI, const Init *RHSI,
274+
const GenericField &Field) -> int {
279275
if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
280276
int64_t LHSi = getAsInt(LHSI);
281277
int64_t RHSi = getAsInt(RHSI);
@@ -285,9 +281,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
285281
if (Field.IsIntrinsic) {
286282
const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
287283
const CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
288-
if (std::optional<bool> Cmp =
289-
CmpLTString(LHSi.TargetPrefix, RHSi.TargetPrefix))
290-
return *Cmp;
284+
if (int Cmp = CmpLTString(LHSi.TargetPrefix, RHSi.TargetPrefix))
285+
return Cmp;
291286
return CmpLTString(LHSi.Name, RHSi.Name);
292287
}
293288

@@ -299,8 +294,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
299294
// Order pseudo instructions before non-pseudo ones.
300295
bool LHSNotPseudo = !LHSr->getValueAsBit("isPseudo");
301296
bool RHSNotPseudo = !RHSr->getValueAsBit("isPseudo");
302-
if (std::optional<bool> Cmp = CmpLTValue(LHSNotPseudo, RHSNotPseudo))
303-
return *Cmp;
297+
if (int Cmp = CmpLTValue(LHSNotPseudo, RHSNotPseudo))
298+
return Cmp;
304299
return CmpLTString(LHSr->getName(), RHSr->getName());
305300
}
306301

@@ -324,8 +319,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
324319
for (const GenericField &Field : Index.Fields) {
325320
const Init *LHSI = LHS->getValueInit(Field.Name);
326321
const Init *RHSI = RHS->getValueInit(Field.Name);
327-
if (std::optional<bool> Cmp = CmpLTField(LHSI, RHSI, Field))
328-
return *Cmp;
322+
if (int Cmp = CmpLTField(LHSI, RHSI, Field))
323+
return Cmp < 0;
329324
}
330325
return false;
331326
}

0 commit comments

Comments
 (0)