xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/CodeGenMapTable.cpp (revision 4e71258227d6a0d7ae5adadb0f3672bfce2bc906)
1  //===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===//
2  //
3  // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4  // See https://llvm.org/LICENSE.txt for license information.
5  // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6  //
7  //===----------------------------------------------------------------------===//
8  // CodeGenMapTable provides functionality for the TabelGen to create
9  // relation mapping between instructions. Relation models are defined using
10  // InstrMapping as a base class. This file implements the functionality which
11  // parses these definitions and generates relation maps using the information
12  // specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc
13  // file along with the functions to query them.
14  //
15  // A relationship model to relate non-predicate instructions with their
16  // predicated true/false forms can be defined as follows:
17  //
18  // def getPredOpcode : InstrMapping {
19  //  let FilterClass = "PredRel";
20  //  let RowFields = ["BaseOpcode"];
21  //  let ColFields = ["PredSense"];
22  //  let KeyCol = ["none"];
23  //  let ValueCols = [["true"], ["false"]]; }
24  //
25  // CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc
26  // file that contains the instructions modeling this relationship. This table
27  // is defined in the function
28  // "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)"
29  // that can be used to retrieve the predicated form of the instruction by
30  // passing its opcode value and the predicate sense (true/false) of the desired
31  // instruction as arguments.
32  //
33  // Short description of the algorithm:
34  //
35  // 1) Iterate through all the records that derive from "InstrMapping" class.
36  // 2) For each record, filter out instructions based on the FilterClass value.
37  // 3) Iterate through this set of instructions and insert them into
38  // RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the
39  // vector of RowFields values and contains vectors of Records (instructions) as
40  // values. RowFields is a list of fields that are required to have the same
41  // values for all the instructions appearing in the same row of the relation
42  // table. All the instructions in a given row of the relation table have some
43  // sort of relationship with the key instruction defined by the corresponding
44  // relationship model.
45  //
46  // Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ]
47  // Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for
48  // RowFields. These groups of instructions are later matched against ValueCols
49  // to determine the column they belong to, if any.
50  //
51  // While building the RowInstrMap map, collect all the key instructions in
52  // KeyInstrVec. These are the instructions having the same values as KeyCol
53  // for all the fields listed in ColFields.
54  //
55  // For Example:
56  //
57  // Relate non-predicate instructions with their predicated true/false forms.
58  //
59  // def getPredOpcode : InstrMapping {
60  //  let FilterClass = "PredRel";
61  //  let RowFields = ["BaseOpcode"];
62  //  let ColFields = ["PredSense"];
63  //  let KeyCol = ["none"];
64  //  let ValueCols = [["true"], ["false"]]; }
65  //
66  // Here, only instructions that have "none" as PredSense will be selected as key
67  // instructions.
68  //
69  // 4) For each key instruction, get the group of instructions that share the
70  // same key-value as the key instruction from RowInstrMap. Iterate over the list
71  // of columns in ValueCols (it is defined as a list<list<string> >. Therefore,
72  // it can specify multi-column relationships). For each column, find the
73  // instruction from the group that matches all the values for the column.
74  // Multiple matches are not allowed.
75  //
76  //===----------------------------------------------------------------------===//
77  
78  #include "CodeGenTarget.h"
79  #include "llvm/Support/Format.h"
80  #include "llvm/TableGen/Error.h"
81  using namespace llvm;
82  typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy;
83  
84  typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy;
85  
86  namespace {
87  
88  //===----------------------------------------------------------------------===//
89  // This class is used to represent InstrMapping class defined in Target.td file.
90  class InstrMap {
91  private:
92    std::string Name;
93    std::string FilterClass;
94    ListInit *RowFields;
95    ListInit *ColFields;
96    ListInit *KeyCol;
97    std::vector<ListInit*> ValueCols;
98  
99  public:
100    InstrMap(Record* MapRec) {
101      Name = std::string(MapRec->getName());
102  
103      // FilterClass - It's used to reduce the search space only to the
104      // instructions that define the kind of relationship modeled by
105      // this InstrMapping object/record.
106      const RecordVal *Filter = MapRec->getValue("FilterClass");
107      FilterClass = Filter->getValue()->getAsUnquotedString();
108  
109      // List of fields/attributes that need to be same across all the
110      // instructions in a row of the relation table.
111      RowFields = MapRec->getValueAsListInit("RowFields");
112  
113      // List of fields/attributes that are constant across all the instruction
114      // in a column of the relation table. Ex: ColFields = 'predSense'
115      ColFields = MapRec->getValueAsListInit("ColFields");
116  
117      // Values for the fields/attributes listed in 'ColFields'.
118      // Ex: KeyCol = 'noPred' -- key instruction is non-predicated
119      KeyCol = MapRec->getValueAsListInit("KeyCol");
120  
121      // List of values for the fields/attributes listed in 'ColFields', one for
122      // each column in the relation table.
123      //
124      // Ex: ValueCols = [['true'],['false']] -- it results two columns in the
125      // table. First column requires all the instructions to have predSense
126      // set to 'true' and second column requires it to be 'false'.
127      ListInit *ColValList = MapRec->getValueAsListInit("ValueCols");
128  
129      // Each instruction map must specify at least one column for it to be valid.
130      if (ColValList->empty())
131        PrintFatalError(MapRec->getLoc(), "InstrMapping record `" +
132          MapRec->getName() + "' has empty " + "`ValueCols' field!");
133  
134      for (Init *I : ColValList->getValues()) {
135        auto *ColI = cast<ListInit>(I);
136  
137        // Make sure that all the sub-lists in 'ValueCols' have same number of
138        // elements as the fields in 'ColFields'.
139        if (ColI->size() != ColFields->size())
140          PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() +
141            "', field `ValueCols' entries don't match with " +
142            " the entries in 'ColFields'!");
143        ValueCols.push_back(ColI);
144      }
145    }
146  
147    const std::string &getName() const { return Name; }
148  
149    const std::string &getFilterClass() const { return FilterClass; }
150  
151    ListInit *getRowFields() const { return RowFields; }
152  
153    ListInit *getColFields() const { return ColFields; }
154  
155    ListInit *getKeyCol() const { return KeyCol; }
156  
157    const std::vector<ListInit*> &getValueCols() const {
158      return ValueCols;
159    }
160  };
161  } // end anonymous namespace
162  
163  
164  //===----------------------------------------------------------------------===//
165  // class MapTableEmitter : It builds the instruction relation maps using
166  // the information provided in InstrMapping records. It outputs these
167  // relationship maps as tables into XXXGenInstrInfo.inc file along with the
168  // functions to query them.
169  
170  namespace {
171  class MapTableEmitter {
172  private:
173  //  std::string TargetName;
174    const CodeGenTarget &Target;
175    // InstrMapDesc - InstrMapping record to be processed.
176    InstrMap InstrMapDesc;
177  
178    // InstrDefs - list of instructions filtered using FilterClass defined
179    // in InstrMapDesc.
180    std::vector<Record*> InstrDefs;
181  
182    // RowInstrMap - maps RowFields values to the instructions. It's keyed by the
183    // values of the row fields and contains vector of records as values.
184    RowInstrMapTy RowInstrMap;
185  
186    // KeyInstrVec - list of key instructions.
187    std::vector<Record*> KeyInstrVec;
188    DenseMap<Record*, std::vector<Record*> > MapTable;
189  
190  public:
191    MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
192                    Target(Target), InstrMapDesc(IMRec) {
193      const std::string &FilterClass = InstrMapDesc.getFilterClass();
194      InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
195    }
196  
197    void buildRowInstrMap();
198  
199    // Returns true if an instruction is a key instruction, i.e., its ColFields
200    // have same values as KeyCol.
201    bool isKeyColInstr(Record* CurInstr);
202  
203    // Find column instruction corresponding to a key instruction based on the
204    // constraints for that column.
205    Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
206  
207    // Find column instructions for each key instruction based
208    // on ValueCols and store them into MapTable.
209    void buildMapTable();
210  
211    void emitBinSearch(raw_ostream &OS, unsigned TableSize);
212    void emitTablesWithFunc(raw_ostream &OS);
213    unsigned emitBinSearchTable(raw_ostream &OS);
214  
215    // Lookup functions to query binary search tables.
216    void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
217  
218  };
219  } // end anonymous namespace
220  
221  
222  //===----------------------------------------------------------------------===//
223  // Process all the instructions that model this relation (alreday present in
224  // InstrDefs) and insert them into RowInstrMap which is keyed by the values of
225  // the fields listed as RowFields. It stores vectors of records as values.
226  // All the related instructions have the same values for the RowFields thus are
227  // part of the same key-value pair.
228  //===----------------------------------------------------------------------===//
229  
230  void MapTableEmitter::buildRowInstrMap() {
231    for (Record *CurInstr : InstrDefs) {
232      std::vector<Init*> KeyValue;
233      ListInit *RowFields = InstrMapDesc.getRowFields();
234      for (Init *RowField : RowFields->getValues()) {
235        RecordVal *RecVal = CurInstr->getValue(RowField);
236        if (RecVal == nullptr)
237          PrintFatalError(CurInstr->getLoc(), "No value " +
238                          RowField->getAsString() + " found in \"" +
239                          CurInstr->getName() + "\" instruction description.");
240        Init *CurInstrVal = RecVal->getValue();
241        KeyValue.push_back(CurInstrVal);
242      }
243  
244      // Collect key instructions into KeyInstrVec. Later, these instructions are
245      // processed to assign column position to the instructions sharing
246      // their KeyValue in RowInstrMap.
247      if (isKeyColInstr(CurInstr))
248        KeyInstrVec.push_back(CurInstr);
249  
250      RowInstrMap[KeyValue].push_back(CurInstr);
251    }
252  }
253  
254  //===----------------------------------------------------------------------===//
255  // Return true if an instruction is a KeyCol instruction.
256  //===----------------------------------------------------------------------===//
257  
258  bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
259    ListInit *ColFields = InstrMapDesc.getColFields();
260    ListInit *KeyCol = InstrMapDesc.getKeyCol();
261  
262    // Check if the instruction is a KeyCol instruction.
263    bool MatchFound = true;
264    for (unsigned j = 0, endCF = ColFields->size();
265        (j < endCF) && MatchFound; j++) {
266      RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
267      std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
268      std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
269      MatchFound = (CurInstrVal == KeyColValue);
270    }
271    return MatchFound;
272  }
273  
274  //===----------------------------------------------------------------------===//
275  // Build a map to link key instructions with the column instructions arranged
276  // according to their column positions.
277  //===----------------------------------------------------------------------===//
278  
279  void MapTableEmitter::buildMapTable() {
280    // Find column instructions for a given key based on the ColField
281    // constraints.
282    const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
283    unsigned NumOfCols = ValueCols.size();
284    for (Record *CurKeyInstr : KeyInstrVec) {
285      std::vector<Record*> ColInstrVec(NumOfCols);
286  
287      // Find the column instruction based on the constraints for the column.
288      for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
289        ListInit *CurValueCol = ValueCols[ColIdx];
290        Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
291        ColInstrVec[ColIdx] = ColInstr;
292      }
293      MapTable[CurKeyInstr] = ColInstrVec;
294    }
295  }
296  
297  //===----------------------------------------------------------------------===//
298  // Find column instruction based on the constraints for that column.
299  //===----------------------------------------------------------------------===//
300  
301  Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
302                                             ListInit *CurValueCol) {
303    ListInit *RowFields = InstrMapDesc.getRowFields();
304    std::vector<Init*> KeyValue;
305  
306    // Construct KeyValue using KeyInstr's values for RowFields.
307    for (Init *RowField : RowFields->getValues()) {
308      Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
309      KeyValue.push_back(KeyInstrVal);
310    }
311  
312    // Get all the instructions that share the same KeyValue as the KeyInstr
313    // in RowInstrMap. We search through these instructions to find a match
314    // for the current column, i.e., the instruction which has the same values
315    // as CurValueCol for all the fields in ColFields.
316    const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
317  
318    ListInit *ColFields = InstrMapDesc.getColFields();
319    Record *MatchInstr = nullptr;
320  
321    for (llvm::Record *CurInstr : RelatedInstrVec) {
322      bool MatchFound = true;
323      for (unsigned j = 0, endCF = ColFields->size();
324           (j < endCF) && MatchFound; j++) {
325        Init *ColFieldJ = ColFields->getElement(j);
326        Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
327        std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
328        Init *ColFieldJVallue = CurValueCol->getElement(j);
329        MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
330      }
331  
332      if (MatchFound) {
333        if (MatchInstr) {
334          // Already had a match
335          // Error if multiple matches are found for a column.
336          std::string KeyValueStr;
337          for (Init *Value : KeyValue) {
338            if (!KeyValueStr.empty())
339              KeyValueStr += ", ";
340            KeyValueStr += Value->getAsString();
341          }
342  
343          PrintFatalError("Multiple matches found for `" + KeyInstr->getName() +
344                          "', for the relation `" + InstrMapDesc.getName() +
345                          "', row fields [" + KeyValueStr + "], column `" +
346                          CurValueCol->getAsString() + "'");
347        }
348        MatchInstr = CurInstr;
349      }
350    }
351    return MatchInstr;
352  }
353  
354  //===----------------------------------------------------------------------===//
355  // Emit one table per relation. Only instructions with a valid relation of a
356  // given type are included in the table sorted by their enum values (opcodes).
357  // Binary search is used for locating instructions in the table.
358  //===----------------------------------------------------------------------===//
359  
360  unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
361  
362    ArrayRef<const CodeGenInstruction*> NumberedInstructions =
363                                              Target.getInstructionsByEnumValue();
364    StringRef Namespace = Target.getInstNamespace();
365    const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
366    unsigned NumCol = ValueCols.size();
367    unsigned TotalNumInstr = NumberedInstructions.size();
368    unsigned TableSize = 0;
369  
370    OS << "static const uint16_t "<<InstrMapDesc.getName();
371    // Number of columns in the table are NumCol+1 because key instructions are
372    // emitted as first column.
373    OS << "Table[]["<< NumCol+1 << "] = {\n";
374    for (unsigned i = 0; i < TotalNumInstr; i++) {
375      Record *CurInstr = NumberedInstructions[i]->TheDef;
376      std::vector<Record*> ColInstrs = MapTable[CurInstr];
377      std::string OutStr;
378      unsigned RelExists = 0;
379      if (!ColInstrs.empty()) {
380        for (unsigned j = 0; j < NumCol; j++) {
381          if (ColInstrs[j] != nullptr) {
382            RelExists = 1;
383            OutStr += ", ";
384            OutStr += Namespace;
385            OutStr += "::";
386            OutStr += ColInstrs[j]->getName();
387          } else { OutStr += ", (uint16_t)-1U";}
388        }
389  
390        if (RelExists) {
391          OS << "  { " << Namespace << "::" << CurInstr->getName();
392          OS << OutStr <<" },\n";
393          TableSize++;
394        }
395      }
396    }
397    if (!TableSize) {
398      OS << "  { " << Namespace << "::" << "INSTRUCTION_LIST_END, ";
399      OS << Namespace << "::" << "INSTRUCTION_LIST_END }";
400    }
401    OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
402    return TableSize;
403  }
404  
405  //===----------------------------------------------------------------------===//
406  // Emit binary search algorithm as part of the functions used to query
407  // relation tables.
408  //===----------------------------------------------------------------------===//
409  
410  void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
411    OS << "  unsigned mid;\n";
412    OS << "  unsigned start = 0;\n";
413    OS << "  unsigned end = " << TableSize << ";\n";
414    OS << "  while (start < end) {\n";
415    OS << "    mid = start + (end - start) / 2;\n";
416    OS << "    if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
417    OS << "      break;\n";
418    OS << "    }\n";
419    OS << "    if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
420    OS << "      end = mid;\n";
421    OS << "    else\n";
422    OS << "      start = mid + 1;\n";
423    OS << "  }\n";
424    OS << "  if (start == end)\n";
425    OS << "    return -1; // Instruction doesn't exist in this table.\n\n";
426  }
427  
428  //===----------------------------------------------------------------------===//
429  // Emit functions to query relation tables.
430  //===----------------------------------------------------------------------===//
431  
432  void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
433                                             unsigned TableSize) {
434  
435    ListInit *ColFields = InstrMapDesc.getColFields();
436    const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
437  
438    // Emit binary search algorithm to locate instructions in the
439    // relation table. If found, return opcode value from the appropriate column
440    // of the table.
441    emitBinSearch(OS, TableSize);
442  
443    if (ValueCols.size() > 1) {
444      for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
445        ListInit *ColumnI = ValueCols[i];
446        for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
447          std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
448          OS << "  if (in" << ColName;
449          OS << " == ";
450          OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
451          if (j < ColumnI->size() - 1) OS << " && ";
452          else OS << ")\n";
453        }
454        OS << "    return " << InstrMapDesc.getName();
455        OS << "Table[mid]["<<i+1<<"];\n";
456      }
457      OS << "  return -1;";
458    }
459    else
460      OS << "  return " << InstrMapDesc.getName() << "Table[mid][1];\n";
461  
462    OS <<"}\n\n";
463  }
464  
465  //===----------------------------------------------------------------------===//
466  // Emit relation tables and the functions to query them.
467  //===----------------------------------------------------------------------===//
468  
469  void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
470  
471    // Emit function name and the input parameters : mostly opcode value of the
472    // current instruction. However, if a table has multiple columns (more than 2
473    // since first column is used for the key instructions), then we also need
474    // to pass another input to indicate the column to be selected.
475  
476    ListInit *ColFields = InstrMapDesc.getColFields();
477    const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
478    OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
479    OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
480    if (ValueCols.size() > 1) {
481      for (Init *CF : ColFields->getValues()) {
482        std::string ColName = CF->getAsUnquotedString();
483        OS << ", enum " << ColName << " in" << ColName << ") {\n";
484      }
485    } else { OS << ") {\n"; }
486  
487    // Emit map table.
488    unsigned TableSize = emitBinSearchTable(OS);
489  
490    // Emit rest of the function body.
491    emitMapFuncBody(OS, TableSize);
492  }
493  
494  //===----------------------------------------------------------------------===//
495  // Emit enums for the column fields across all the instruction maps.
496  //===----------------------------------------------------------------------===//
497  
498  static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
499  
500    std::vector<Record*> InstrMapVec;
501    InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
502    std::map<std::string, std::vector<Init*> > ColFieldValueMap;
503  
504    // Iterate over all InstrMapping records and create a map between column
505    // fields and their possible values across all records.
506    for (Record *CurMap : InstrMapVec) {
507      ListInit *ColFields;
508      ColFields = CurMap->getValueAsListInit("ColFields");
509      ListInit *List = CurMap->getValueAsListInit("ValueCols");
510      std::vector<ListInit*> ValueCols;
511      unsigned ListSize = List->size();
512  
513      for (unsigned j = 0; j < ListSize; j++) {
514        auto *ListJ = cast<ListInit>(List->getElement(j));
515  
516        if (ListJ->size() != ColFields->size())
517          PrintFatalError("Record `" + CurMap->getName() + "', field "
518            "`ValueCols' entries don't match with the entries in 'ColFields' !");
519        ValueCols.push_back(ListJ);
520      }
521  
522      for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) {
523        for (unsigned k = 0; k < ListSize; k++){
524          std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
525          ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
526        }
527      }
528    }
529  
530    for (auto &Entry : ColFieldValueMap) {
531      std::vector<Init*> FieldValues = Entry.second;
532  
533      // Delete duplicate entries from ColFieldValueMap
534      for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
535        Init *CurVal = FieldValues[i];
536        for (unsigned j = i+1; j < FieldValues.size(); j++) {
537          if (CurVal == FieldValues[j]) {
538            FieldValues.erase(FieldValues.begin()+j);
539            --j;
540          }
541        }
542      }
543  
544      // Emit enumerated values for the column fields.
545      OS << "enum " << Entry.first << " {\n";
546      for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
547        OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
548        if (i != endFV - 1)
549          OS << ",\n";
550        else
551          OS << "\n};\n\n";
552      }
553    }
554  }
555  
556  namespace llvm {
557  //===----------------------------------------------------------------------===//
558  // Parse 'InstrMapping' records and use the information to form relationship
559  // between instructions. These relations are emitted as a tables along with the
560  // functions to query them.
561  //===----------------------------------------------------------------------===//
562  void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
563    CodeGenTarget Target(Records);
564    StringRef NameSpace = Target.getInstNamespace();
565    std::vector<Record*> InstrMapVec;
566    InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
567  
568    if (InstrMapVec.empty())
569      return;
570  
571    OS << "#ifdef GET_INSTRMAP_INFO\n";
572    OS << "#undef GET_INSTRMAP_INFO\n";
573    OS << "namespace llvm {\n\n";
574    OS << "namespace " << NameSpace << " {\n\n";
575  
576    // Emit coulumn field names and their values as enums.
577    emitEnums(OS, Records);
578  
579    // Iterate over all instruction mapping records and construct relationship
580    // maps based on the information specified there.
581    //
582    for (Record *CurMap : InstrMapVec) {
583      MapTableEmitter IMap(Target, Records, CurMap);
584  
585      // Build RowInstrMap to group instructions based on their values for
586      // RowFields. In the process, also collect key instructions into
587      // KeyInstrVec.
588      IMap.buildRowInstrMap();
589  
590      // Build MapTable to map key instructions with the corresponding column
591      // instructions.
592      IMap.buildMapTable();
593  
594      // Emit map tables and the functions to query them.
595      IMap.emitTablesWithFunc(OS);
596    }
597    OS << "} // end namespace " << NameSpace << "\n";
598    OS << "} // end namespace llvm\n";
599    OS << "#endif // GET_INSTRMAP_INFO\n\n";
600  }
601  
602  } // End llvm namespace
603