Skip to content

Commit c28604c

Browse files
committed
Use clamp, update test accordingly.
1 parent 5a5e072 commit c28604c

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

llvm/test/TableGen/generic-tables-instruction.td

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ def Arch : Target { let InstructionSet = ArchInstrInfo; }
1818
// A contiguous primary (Instruction) key should get a direct lookup instead of
1919
// binary search.
2020
// CHECK: const MyInstr *getCustomEncodingHelper(unsigned Opcode) {
21-
// CHECK: if ((Opcode < B) ||
22-
// CHECK: (Opcode > D))
23-
// CHECK: return nullptr;
21+
// CHECK: if ((unsigned)Opcode != std::clamp((unsigned)Opcode, (unsigned)B, (unsigned)D))
22+
// CHECK: return nullptr;
2423
// CHECK: auto Table = ArrayRef(InstrTable);
2524
// CHECK: size_t Idx = Opcode - B;
2625
// CHECK: return &Table[Idx];
2726

28-
2927
class MyInstr<int op> : Instruction {
3028
let OutOperandList = (outs);
3129
let InOperandList = (ins);
@@ -56,8 +54,7 @@ def InstrTable : GenericTable {
5654
//
5755
// Verify contiguous check for SearchIndex.
5856
// const MyInfoEntry *getTable2ByValue(uint8_t Value) {
59-
// CHECK: if ((Value < 0xB) ||
60-
// CHECK: (Value > 0xD))
57+
// CHECK: if ((uint8_t)Value != std::clamp((uint8_t)Value, (uint8_t)0xB, (uint8_t)0xD))
6158
// CHECK: return nullptr;
6259
// CHECK: auto Table = ArrayRef(Index);
6360
// CHECK: size_t Idx = Value - 0xB;

llvm/test/TableGen/generic-tables.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def lookupBTableByNameAndFlag : SearchIndex {
113113
// CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind);
114114
// CHECK-LABEL: GET_CTable_IMPL
115115
// CHECK: const CEntry *lookupCEntryByEncoding(uint16_t Encoding) {
116-
// CHECK: if ((Encoding < 0xA) ||
117-
// CHECK: (Encoding > 0xF))
116+
// CHECK: if ((uint16_t)Encoding != std::clamp((uint16_t)Encoding, (uint16_t)0xA, (uint16_t)0xF))
118117
// CHECK: return nullptr;
119118

120119
// CHECK: const CEntry *lookupCEntry(StringRef Name, unsigned Kind) {

llvm/utils/TableGen/SearchableTableEmitter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ class SearchableTableEmitter {
211211
// known, return that numeric value.
212212
int64_t SearchableTableEmitter::getNumericKey(const SearchIndex &Index,
213213
const Record *Rec) {
214+
assert(Index.Fields.size() == 1);
214215
const GenericField &Field = Index.Fields[0];
215216

216217
// To be consistent with compareBy and primaryRepresentation elsewhere,
@@ -399,11 +400,11 @@ void SearchableTableEmitter::emitLookupFunction(const GenericTable &Table,
399400
Index.Loc, Field, IndexRows[0]->getValueInit(Field.Name));
400401
std::string LastRepr = primaryRepresentation(
401402
Index.Loc, Field, IndexRows.back()->getValueInit(Field.Name));
402-
if (getNumericKey(Index, IndexRows[0]) == 0)
403-
OS << " if (";
404-
else
405-
OS << " if ((" << Field.Name << " < " << FirstRepr << ") ||\n";
406-
OS << " (" << Field.Name << " > " << LastRepr << "))\n";
403+
std::string TS =
404+
'(' + searchableFieldType(Table, Index, Field, TypeInStaticStruct) +
405+
')';
406+
OS << " if (" << TS << Field.Name << " != std::clamp(" << TS << Field.Name
407+
<< ", " << TS << FirstRepr << ", " << TS << LastRepr << "))\n";
407408
OS << " return nullptr;\n\n";
408409

409410
if (IsContiguous && !Index.EarlyOut) {

0 commit comments

Comments
 (0)