Skip to content

Commit 8887292

Browse files
committed
Use -1/0/1 instead of optional<bool> for comparison result
1 parent f18da32 commit 8887292

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
@@ -236,35 +236,31 @@ int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
236236
bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
237237
const SearchIndex &Index) {
238238
// 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> {
239+
// * -1 if LHS < RHS.
240+
// * 1 if LHS > RHS.
241+
// * 0 if LHS == RHS.
242+
auto CmpLTValue = [](const auto &LHS, const auto &RHS) -> int {
244243
if (LHS < RHS)
245-
return true;
244+
return -1;
246245
if (LHS > RHS)
247-
return false;
248-
return std::nullopt;
246+
return 1;
247+
return 0;
249248
};
250249

251250
// Specialized form of `CmpLTValue` for string-like types that uses compare()
252251
// to do the comparison of the 2 strings once (instead if 2 comparisons if we
253252
// 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;
253+
auto CmpLTString = [](StringRef LHS, StringRef RHS) -> int {
254+
return LHS.compare(RHS);
259255
};
260256

261257
// Compare two fields and returns:
262258
// - true if LHS < RHS.
263259
// - false if LHS > RHS.
264260
// - std::nullopt if LHS == RHS.
265-
auto CmpLTField = [this, &Index, &CmpLTValue, &CmpLTString](
266-
const Init *LHSI, const Init *RHSI,
267-
const GenericField &Field) -> std::optional<bool> {
261+
auto CmpLTField = [this, &Index, &CmpLTValue,
262+
&CmpLTString](const Init *LHSI, const Init *RHSI,
263+
const GenericField &Field) -> int {
268264
if (isa<BitsRecTy>(Field.RecType) || isa<IntRecTy>(Field.RecType)) {
269265
int64_t LHSi = getAsInt(LHSI);
270266
int64_t RHSi = getAsInt(RHSI);
@@ -274,9 +270,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
274270
if (Field.IsIntrinsic) {
275271
const CodeGenIntrinsic &LHSi = getIntrinsic(LHSI);
276272
const CodeGenIntrinsic &RHSi = getIntrinsic(RHSI);
277-
if (std::optional<bool> Cmp =
278-
CmpLTString(LHSi.TargetPrefix, RHSi.TargetPrefix))
279-
return *Cmp;
273+
if (int Cmp = CmpLTString(LHSi.TargetPrefix, RHSi.TargetPrefix))
274+
return Cmp;
280275
return CmpLTString(LHSi.Name, RHSi.Name);
281276
}
282277

@@ -288,8 +283,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
288283
// Order pseudo instructions before non-pseudo ones.
289284
bool LHSNotPseudo = !LHSr->getValueAsBit("isPseudo");
290285
bool RHSNotPseudo = !RHSr->getValueAsBit("isPseudo");
291-
if (std::optional<bool> Cmp = CmpLTValue(LHSNotPseudo, RHSNotPseudo))
292-
return *Cmp;
286+
if (int Cmp = CmpLTValue(LHSNotPseudo, RHSNotPseudo))
287+
return Cmp;
293288
return CmpLTString(LHSr->getName(), RHSr->getName());
294289
}
295290

@@ -313,8 +308,8 @@ bool SearchableTableEmitter::compareBy(const Record *LHS, const Record *RHS,
313308
for (const GenericField &Field : Index.Fields) {
314309
const Init *LHSI = LHS->getValueInit(Field.Name);
315310
const Init *RHSI = RHS->getValueInit(Field.Name);
316-
if (std::optional<bool> Cmp = CmpLTField(LHSI, RHSI, Field))
317-
return *Cmp;
311+
if (int Cmp = CmpLTField(LHSI, RHSI, Field))
312+
return Cmp < 0;
318313
}
319314
return false;
320315
}

0 commit comments

Comments
 (0)