Skip to content

Commit d919d4d

Browse files
committed
[TableGen][NFC] Factor early-out range check.
Combine the EarlyOut and IsContiguous range check. Also avoid "comparison is always false" warnings in emitted code when the lower-bound check is against 0.
1 parent 416f1c4 commit d919d4d

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
@@ -392,37 +392,32 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
392392
}
393393
}
394394

395-
if (IsContiguous) {
395+
if (Index.EarlyOut || IsContiguous) {
396396
const GenericField &Field = Index.Fields[0];
397397
std::string FirstRepr = primaryRepresentation(
398398
Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name));
399399
std::string LastRepr = primaryRepresentation(
400400
Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name));
401-
OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
402-
OS << " (" << Field.Name << " > " << LastRepr << "))\n";
403-
OS << " return nullptr;\n";
404-
OS << " auto Table = ArrayRef(" << IndexName << ");\n";
405-
OS << " size_t Idx = " << Index.Fields[0].Name << " - " << FirstRepr
406-
<< ";\n";
407-
OS << " return ";
408-
if (IsPrimary)
409-
OS << "&Table[Idx]";
401+
if (getNumericKey(Index, IndexRows[0]) == 0)
402+
OS << " if (";
410403
else
411-
OS << "&" << Table.Name << "[Table[Idx]._index]";
412-
OS << ";\n";
413-
OS << "}\n";
414-
return;
415-
}
416-
417-
if (Index.EarlyOut) {
418-
const GenericField &Field = Index.Fields[0];
419-
std::string FirstRepr = primaryRepresentation(
420-
Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name));
421-
std::string LastRepr = primaryRepresentation(
422-
Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name));
423-
OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
404+
OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
424405
OS << " (" << Field.Name << " > " << LastRepr << "))\n";
425406
OS << " return nullptr;\n\n";
407+
408+
if (IsContiguous) {
409+
OS << " auto Table = ArrayRef(" << IndexName << ");\n";
410+
OS << " size_t Idx = " << Index.Fields[0].Name << " - " << FirstRepr
411+
<< ";\n";
412+
OS << " return ";
413+
if (IsPrimary)
414+
OS << "&Table[Idx]";
415+
else
416+
OS << "&" << Table.Name << "[Table[Idx]._index]";
417+
OS << ";\n";
418+
OS << "}\n";
419+
return;
420+
}
426421
}
427422

428423
OS << " struct KeyType {\n";

0 commit comments

Comments
 (0)