xref: /llvm-project/llvm/test/TableGen/generic-tables-instruction.td (revision 271b3383d7d86d5945b13117558cfe600aac3528)
1// RUN: llvm-tblgen -gen-searchable-tables -I %p/../../include %s | FileCheck %s
2// XFAIL: vg_leak
3
4include "llvm/TableGen/SearchableTable.td"
5include "llvm/Target/Target.td"
6
7def ArchInstrInfo : InstrInfo { }
8def Arch : Target { let InstructionSet = ArchInstrInfo; }
9
10// CHECK-LABEL: GET_InstrTable_IMPL
11// CHECK: constexpr MyInstr InstrTable[] = {
12// CHECK:   { B, 0xA },
13// CHECK:   { C, 0x0 },
14// CHECK:   { A, 0x5 },
15// CHECK:   { D, 0x8 },
16// CHECK: };
17
18// A contiguous primary (Instruction) key should get a direct lookup instead of
19// binary search.
20// CHECK: const MyInstr *getCustomEncodingHelper(unsigned Opcode) {
21// CHECK: if ((unsigned)Opcode != std::clamp((unsigned)Opcode, (unsigned)B, (unsigned)D))
22// CHECK:   return nullptr;
23// CHECK:   auto Table = ArrayRef(InstrTable);
24// CHECK:   size_t Idx = Opcode - B;
25// CHECK:   return &Table[Idx];
26
27class MyInstr<int op> : Instruction {
28  let OutOperandList = (outs);
29  let InOperandList = (ins);
30  Instruction Opcode = !cast<Instruction>(NAME);
31  bits<16> CustomEncoding = op;
32}
33
34def A : MyInstr<5>;
35def D : MyInstr<8>;
36let isPseudo = 1 in {
37  def C : MyInstr<0>;
38  def B : MyInstr<10>;
39}
40
41def InstrTable : GenericTable {
42  let FilterClass = "MyInstr";
43  let Fields = ["Opcode", "CustomEncoding"];
44
45  let PrimaryKey = ["Opcode"];
46  let PrimaryKeyName = "getCustomEncodingHelper";
47}
48
49
50// Non-contiguous instructions should get a binary search instead of direct
51// lookup.
52// CHECK: const MyInfoEntry *getTable2ByOpcode(unsigned Opcode) {
53// CHECK:   auto Idx = std::lower_bound(Table.begin(), Table.end(), Key,
54//
55// Verify contiguous check for SearchIndex.
56// const MyInfoEntry *getTable2ByValue(uint8_t Value) {
57// CHECK:   if ((uint8_t)Value != std::clamp((uint8_t)Value, (uint8_t)0xB, (uint8_t)0xD))
58// CHECK:    return nullptr;
59// CHECK:  auto Table = ArrayRef(Index);
60// CHECK:  size_t Idx = Value - 0xB;
61// CHECK:  return &InstrTable2[Table[Idx]._index];
62
63
64class MyInfoEntry<int V, string S> {
65  Instruction Opcode = !cast<Instruction>(NAME);
66  bits<4> Value = V;
67  string Name = S;
68}
69
70let OutOperandList = (outs), InOperandList = (ins) in {
71def W : Instruction, MyInfoEntry<12, "IW">;
72def X : Instruction;
73def Y : Instruction, MyInfoEntry<13, "IY">;
74def Z : Instruction, MyInfoEntry<11, "IZ">;
75}
76
77def InstrTable2 : GenericTable {
78  let FilterClass = "MyInfoEntry";
79  let Fields = ["Opcode", "Value", "Name"];
80
81  let PrimaryKey = ["Opcode"];
82  let PrimaryKeyName = "getTable2ByOpcode";
83}
84
85def getTable2ByValue : SearchIndex {
86  let Table = InstrTable2;
87  let Key = ["Value"];
88}
89