blob: 43348b622a74548034a6fa949246053e2089d7de [file] [log] [blame]
Sebastian Pop5c87daf2012-10-25 15:54:06 +00001//===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// CodeGenMapTable provides functionality for the TabelGen to create
10// relation mapping between instructions. Relation models are defined using
11// InstrMapping as a base class. This file implements the functionality which
12// parses these definitions and generates relation maps using the information
13// specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc
14// file along with the functions to query them.
15//
16// A relationship model to relate non-predicate instructions with their
17// predicated true/false forms can be defined as follows:
18//
19// def getPredOpcode : InstrMapping {
20// let FilterClass = "PredRel";
21// let RowFields = ["BaseOpcode"];
22// let ColFields = ["PredSense"];
23// let KeyCol = ["none"];
24// let ValueCols = [["true"], ["false"]]; }
25//
26// CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc
27// file that contains the instructions modeling this relationship. This table
28// is defined in the function
29// "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)"
30// that can be used to retrieve the predicated form of the instruction by
31// passing its opcode value and the predicate sense (true/false) of the desired
32// instruction as arguments.
33//
34// Short description of the algorithm:
35//
36// 1) Iterate through all the records that derive from "InstrMapping" class.
37// 2) For each record, filter out instructions based on the FilterClass value.
38// 3) Iterate through this set of instructions and insert them into
39// RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the
40// vector of RowFields values and contains vectors of Records (instructions) as
41// values. RowFields is a list of fields that are required to have the same
42// values for all the instructions appearing in the same row of the relation
43// table. All the instructions in a given row of the relation table have some
44// sort of relationship with the key instruction defined by the corresponding
45// relationship model.
46//
47// Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ]
48// Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for
49// RowFields. These groups of instructions are later matched against ValueCols
50// to determine the column they belong to, if any.
51//
52// While building the RowInstrMap map, collect all the key instructions in
53// KeyInstrVec. These are the instructions having the same values as KeyCol
54// for all the fields listed in ColFields.
55//
56// For Example:
57//
58// Relate non-predicate instructions with their predicated true/false forms.
59//
60// def getPredOpcode : InstrMapping {
61// let FilterClass = "PredRel";
62// let RowFields = ["BaseOpcode"];
63// let ColFields = ["PredSense"];
64// let KeyCol = ["none"];
65// let ValueCols = [["true"], ["false"]]; }
66//
67// Here, only instructions that have "none" as PredSense will be selected as key
68// instructions.
69//
70// 4) For each key instruction, get the group of instructions that share the
71// same key-value as the key instruction from RowInstrMap. Iterate over the list
72// of columns in ValueCols (it is defined as a list<list<string> >. Therefore,
73// it can specify multi-column relationships). For each column, find the
74// instruction from the group that matches all the values for the column.
75// Multiple matches are not allowed.
76//
77//===----------------------------------------------------------------------===//
78
79#include "CodeGenTarget.h"
80#include "llvm/Support/Format.h"
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000081#include "llvm/TableGen/Error.h"
Sebastian Pop5c87daf2012-10-25 15:54:06 +000082using namespace llvm;
83typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy;
84
85typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy;
86
87namespace {
88
89//===----------------------------------------------------------------------===//
90// This class is used to represent InstrMapping class defined in Target.td file.
91class InstrMap {
92private:
93 std::string Name;
94 std::string FilterClass;
95 ListInit *RowFields;
96 ListInit *ColFields;
97 ListInit *KeyCol;
98 std::vector<ListInit*> ValueCols;
99
100public:
101 InstrMap(Record* MapRec) {
102 Name = MapRec->getName();
103
104 // FilterClass - It's used to reduce the search space only to the
105 // instructions that define the kind of relationship modeled by
106 // this InstrMapping object/record.
107 const RecordVal *Filter = MapRec->getValue("FilterClass");
108 FilterClass = Filter->getValue()->getAsUnquotedString();
109
110 // List of fields/attributes that need to be same across all the
111 // instructions in a row of the relation table.
112 RowFields = MapRec->getValueAsListInit("RowFields");
113
114 // List of fields/attributes that are constant across all the instruction
115 // in a column of the relation table. Ex: ColFields = 'predSense'
116 ColFields = MapRec->getValueAsListInit("ColFields");
117
118 // Values for the fields/attributes listed in 'ColFields'.
Alp Tokerf907b892013-12-05 05:44:44 +0000119 // Ex: KeyCol = 'noPred' -- key instruction is non-predicated
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000120 KeyCol = MapRec->getValueAsListInit("KeyCol");
121
122 // List of values for the fields/attributes listed in 'ColFields', one for
123 // each column in the relation table.
124 //
125 // Ex: ValueCols = [['true'],['false']] -- it results two columns in the
126 // table. First column requires all the instructions to have predSense
127 // set to 'true' and second column requires it to be 'false'.
128 ListInit *ColValList = MapRec->getValueAsListInit("ValueCols");
129
130 // Each instruction map must specify at least one column for it to be valid.
Craig Topperec9072d2015-05-14 05:53:53 +0000131 if (ColValList->empty())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000132 PrintFatalError(MapRec->getLoc(), "InstrMapping record `" +
133 MapRec->getName() + "' has empty " + "`ValueCols' field!");
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000134
Craig Topperef0578a2015-06-02 04:15:51 +0000135 for (Init *I : ColValList->getValues()) {
136 ListInit *ColI = dyn_cast<ListInit>(I);
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000137
138 // Make sure that all the sub-lists in 'ValueCols' have same number of
139 // elements as the fields in 'ColFields'.
Craig Topper664f6a02015-06-02 04:15:57 +0000140 if (ColI->size() != ColFields->size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000141 PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() +
142 "', field `ValueCols' entries don't match with " +
143 " the entries in 'ColFields'!");
144 ValueCols.push_back(ColI);
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000145 }
146 }
147
148 std::string getName() const {
149 return Name;
150 }
151
152 std::string getFilterClass() {
153 return FilterClass;
154 }
155
156 ListInit *getRowFields() const {
157 return RowFields;
158 }
159
160 ListInit *getColFields() const {
161 return ColFields;
162 }
163
164 ListInit *getKeyCol() const {
165 return KeyCol;
166 }
167
168 const std::vector<ListInit*> &getValueCols() const {
169 return ValueCols;
170 }
171};
172} // End anonymous namespace.
173
174
175//===----------------------------------------------------------------------===//
176// class MapTableEmitter : It builds the instruction relation maps using
177// the information provided in InstrMapping records. It outputs these
178// relationship maps as tables into XXXGenInstrInfo.inc file along with the
179// functions to query them.
180
181namespace {
182class MapTableEmitter {
183private:
184// std::string TargetName;
185 const CodeGenTarget &Target;
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000186 // InstrMapDesc - InstrMapping record to be processed.
187 InstrMap InstrMapDesc;
188
189 // InstrDefs - list of instructions filtered using FilterClass defined
190 // in InstrMapDesc.
191 std::vector<Record*> InstrDefs;
192
193 // RowInstrMap - maps RowFields values to the instructions. It's keyed by the
194 // values of the row fields and contains vector of records as values.
195 RowInstrMapTy RowInstrMap;
196
197 // KeyInstrVec - list of key instructions.
198 std::vector<Record*> KeyInstrVec;
199 DenseMap<Record*, std::vector<Record*> > MapTable;
200
201public:
202 MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
David Blaikiedcbd1602012-10-25 17:04:55 +0000203 Target(Target), InstrMapDesc(IMRec) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000204 const std::string FilterClass = InstrMapDesc.getFilterClass();
205 InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
David Blaikiedcbd1602012-10-25 17:04:55 +0000206 }
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000207
208 void buildRowInstrMap();
209
210 // Returns true if an instruction is a key instruction, i.e., its ColFields
211 // have same values as KeyCol.
212 bool isKeyColInstr(Record* CurInstr);
213
214 // Find column instruction corresponding to a key instruction based on the
215 // constraints for that column.
216 Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
217
218 // Find column instructions for each key instruction based
219 // on ValueCols and store them into MapTable.
220 void buildMapTable();
221
222 void emitBinSearch(raw_ostream &OS, unsigned TableSize);
223 void emitTablesWithFunc(raw_ostream &OS);
224 unsigned emitBinSearchTable(raw_ostream &OS);
225
226 // Lookup functions to query binary search tables.
227 void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
228
229};
230} // End anonymous namespace.
231
232
233//===----------------------------------------------------------------------===//
234// Process all the instructions that model this relation (alreday present in
235// InstrDefs) and insert them into RowInstrMap which is keyed by the values of
236// the fields listed as RowFields. It stores vectors of records as values.
237// All the related instructions have the same values for the RowFields thus are
238// part of the same key-value pair.
239//===----------------------------------------------------------------------===//
240
241void MapTableEmitter::buildRowInstrMap() {
Craig Topperef0578a2015-06-02 04:15:51 +0000242 for (Record *CurInstr : InstrDefs) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000243 std::vector<Init*> KeyValue;
244 ListInit *RowFields = InstrMapDesc.getRowFields();
Craig Topperef0578a2015-06-02 04:15:51 +0000245 for (Init *RowField : RowFields->getValues()) {
246 Init *CurInstrVal = CurInstr->getValue(RowField)->getValue();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000247 KeyValue.push_back(CurInstrVal);
248 }
249
250 // Collect key instructions into KeyInstrVec. Later, these instructions are
251 // processed to assign column position to the instructions sharing
252 // their KeyValue in RowInstrMap.
253 if (isKeyColInstr(CurInstr))
254 KeyInstrVec.push_back(CurInstr);
255
256 RowInstrMap[KeyValue].push_back(CurInstr);
257 }
258}
259
260//===----------------------------------------------------------------------===//
261// Return true if an instruction is a KeyCol instruction.
262//===----------------------------------------------------------------------===//
263
264bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
265 ListInit *ColFields = InstrMapDesc.getColFields();
266 ListInit *KeyCol = InstrMapDesc.getKeyCol();
267
268 // Check if the instruction is a KeyCol instruction.
269 bool MatchFound = true;
Craig Topper664f6a02015-06-02 04:15:57 +0000270 for (unsigned j = 0, endCF = ColFields->size();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000271 (j < endCF) && MatchFound; j++) {
272 RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
273 std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
274 std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
275 MatchFound = (CurInstrVal == KeyColValue);
276 }
277 return MatchFound;
278}
279
280//===----------------------------------------------------------------------===//
281// Build a map to link key instructions with the column instructions arranged
282// according to their column positions.
283//===----------------------------------------------------------------------===//
284
285void MapTableEmitter::buildMapTable() {
286 // Find column instructions for a given key based on the ColField
287 // constraints.
288 const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
289 unsigned NumOfCols = ValueCols.size();
Craig Topperef0578a2015-06-02 04:15:51 +0000290 for (Record *CurKeyInstr : KeyInstrVec) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000291 std::vector<Record*> ColInstrVec(NumOfCols);
292
293 // Find the column instruction based on the constraints for the column.
294 for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
295 ListInit *CurValueCol = ValueCols[ColIdx];
296 Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
297 ColInstrVec[ColIdx] = ColInstr;
298 }
299 MapTable[CurKeyInstr] = ColInstrVec;
300 }
301}
302
303//===----------------------------------------------------------------------===//
304// Find column instruction based on the constraints for that column.
305//===----------------------------------------------------------------------===//
306
307Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
308 ListInit *CurValueCol) {
309 ListInit *RowFields = InstrMapDesc.getRowFields();
310 std::vector<Init*> KeyValue;
311
312 // Construct KeyValue using KeyInstr's values for RowFields.
Craig Topperef0578a2015-06-02 04:15:51 +0000313 for (Init *RowField : RowFields->getValues()) {
314 Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000315 KeyValue.push_back(KeyInstrVal);
316 }
317
318 // Get all the instructions that share the same KeyValue as the KeyInstr
319 // in RowInstrMap. We search through these instructions to find a match
320 // for the current column, i.e., the instruction which has the same values
321 // as CurValueCol for all the fields in ColFields.
322 const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
323
324 ListInit *ColFields = InstrMapDesc.getColFields();
Craig Topper24064772014-04-15 07:20:03 +0000325 Record *MatchInstr = nullptr;
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000326
327 for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) {
328 bool MatchFound = true;
329 Record *CurInstr = RelatedInstrVec[i];
Craig Topper664f6a02015-06-02 04:15:57 +0000330 for (unsigned j = 0, endCF = ColFields->size();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000331 (j < endCF) && MatchFound; j++) {
332 Init *ColFieldJ = ColFields->getElement(j);
333 Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
334 std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
335 Init *ColFieldJVallue = CurValueCol->getElement(j);
336 MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
337 }
338
339 if (MatchFound) {
Nicolai Haehnle411fbbf2016-03-10 18:51:58 +0000340 if (MatchInstr) {
341 // Already had a match
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000342 // Error if multiple matches are found for a column.
Nicolai Haehnle411fbbf2016-03-10 18:51:58 +0000343 std::string KeyValueStr;
344 for (Init *Value : KeyValue) {
345 if (!KeyValueStr.empty())
346 KeyValueStr += ", ";
347 KeyValueStr += Value->getAsString();
348 }
349
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000350 PrintFatalError("Multiple matches found for `" + KeyInstr->getName() +
Nicolai Haehnle411fbbf2016-03-10 18:51:58 +0000351 "', for the relation `" + InstrMapDesc.getName() + "', row fields [" +
352 KeyValueStr + "], column `" + CurValueCol->getAsString() + "'");
353 }
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000354 MatchInstr = CurInstr;
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000355 }
356 }
357 return MatchInstr;
358}
359
360//===----------------------------------------------------------------------===//
361// Emit one table per relation. Only instructions with a valid relation of a
362// given type are included in the table sorted by their enum values (opcodes).
363// Binary search is used for locating instructions in the table.
364//===----------------------------------------------------------------------===//
365
366unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
367
Craig Topper28851b62016-02-01 01:33:42 +0000368 ArrayRef<const CodeGenInstruction*> NumberedInstructions =
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000369 Target.getInstructionsByEnumValue();
Craig Topper86a9aee2017-07-07 06:22:35 +0000370 StringRef Namespace = Target.getInstNamespace();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000371 const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
372 unsigned NumCol = ValueCols.size();
373 unsigned TotalNumInstr = NumberedInstructions.size();
374 unsigned TableSize = 0;
375
376 OS << "static const uint16_t "<<InstrMapDesc.getName();
377 // Number of columns in the table are NumCol+1 because key instructions are
378 // emitted as first column.
379 OS << "Table[]["<< NumCol+1 << "] = {\n";
380 for (unsigned i = 0; i < TotalNumInstr; i++) {
381 Record *CurInstr = NumberedInstructions[i]->TheDef;
382 std::vector<Record*> ColInstrs = MapTable[CurInstr];
383 std::string OutStr("");
384 unsigned RelExists = 0;
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000385 if (!ColInstrs.empty()) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000386 for (unsigned j = 0; j < NumCol; j++) {
Craig Topper24064772014-04-15 07:20:03 +0000387 if (ColInstrs[j] != nullptr) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000388 RelExists = 1;
389 OutStr += ", ";
Karl-Johan Karlssonbb6cf732017-03-27 07:13:44 +0000390 OutStr += Namespace;
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000391 OutStr += "::";
392 OutStr += ColInstrs[j]->getName();
Benjamin Kramer0a7a17d2014-03-03 13:59:41 +0000393 } else { OutStr += ", (uint16_t)-1U";}
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000394 }
395
396 if (RelExists) {
Karl-Johan Karlssonbb6cf732017-03-27 07:13:44 +0000397 OS << " { " << Namespace << "::" << CurInstr->getName();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000398 OS << OutStr <<" },\n";
399 TableSize++;
400 }
401 }
402 }
403 if (!TableSize) {
Karl-Johan Karlssonbb6cf732017-03-27 07:13:44 +0000404 OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, ";
405 OS << Namespace << "::" << "INSTRUCTION_LIST_END }";
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000406 }
407 OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
408 return TableSize;
409}
410
411//===----------------------------------------------------------------------===//
412// Emit binary search algorithm as part of the functions used to query
413// relation tables.
414//===----------------------------------------------------------------------===//
415
416void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
417 OS << " unsigned mid;\n";
418 OS << " unsigned start = 0;\n";
419 OS << " unsigned end = " << TableSize << ";\n";
420 OS << " while (start < end) {\n";
421 OS << " mid = start + (end - start)/2;\n";
422 OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
423 OS << " break;\n";
424 OS << " }\n";
425 OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
426 OS << " end = mid;\n";
427 OS << " else\n";
428 OS << " start = mid + 1;\n";
429 OS << " }\n";
430 OS << " if (start == end)\n";
431 OS << " return -1; // Instruction doesn't exist in this table.\n\n";
432}
433
434//===----------------------------------------------------------------------===//
435// Emit functions to query relation tables.
436//===----------------------------------------------------------------------===//
437
438void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
439 unsigned TableSize) {
440
441 ListInit *ColFields = InstrMapDesc.getColFields();
442 const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
443
444 // Emit binary search algorithm to locate instructions in the
445 // relation table. If found, return opcode value from the appropriate column
446 // of the table.
447 emitBinSearch(OS, TableSize);
448
449 if (ValueCols.size() > 1) {
450 for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
451 ListInit *ColumnI = ValueCols[i];
Craig Topper664f6a02015-06-02 04:15:57 +0000452 for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000453 std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
454 OS << " if (in" << ColName;
455 OS << " == ";
456 OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
Craig Topper664f6a02015-06-02 04:15:57 +0000457 if (j < ColumnI->size() - 1) OS << " && ";
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000458 else OS << ")\n";
459 }
460 OS << " return " << InstrMapDesc.getName();
461 OS << "Table[mid]["<<i+1<<"];\n";
462 }
463 OS << " return -1;";
464 }
465 else
466 OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n";
467
468 OS <<"}\n\n";
469}
470
471//===----------------------------------------------------------------------===//
472// Emit relation tables and the functions to query them.
473//===----------------------------------------------------------------------===//
474
475void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
476
477 // Emit function name and the input parameters : mostly opcode value of the
478 // current instruction. However, if a table has multiple columns (more than 2
479 // since first column is used for the key instructions), then we also need
480 // to pass another input to indicate the column to be selected.
481
482 ListInit *ColFields = InstrMapDesc.getColFields();
483 const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
Matt Arsenaultb12c0d92015-09-24 07:51:20 +0000484 OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000485 OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
486 if (ValueCols.size() > 1) {
Craig Topperef0578a2015-06-02 04:15:51 +0000487 for (Init *CF : ColFields->getValues()) {
488 std::string ColName = CF->getAsUnquotedString();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000489 OS << ", enum " << ColName << " in" << ColName << ") {\n";
490 }
491 } else { OS << ") {\n"; }
492
493 // Emit map table.
494 unsigned TableSize = emitBinSearchTable(OS);
495
496 // Emit rest of the function body.
497 emitMapFuncBody(OS, TableSize);
498}
499
500//===----------------------------------------------------------------------===//
501// Emit enums for the column fields across all the instruction maps.
502//===----------------------------------------------------------------------===//
503
504static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
505
506 std::vector<Record*> InstrMapVec;
507 InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
508 std::map<std::string, std::vector<Init*> > ColFieldValueMap;
509
510 // Iterate over all InstrMapping records and create a map between column
511 // fields and their possible values across all records.
Craig Topper6e2edc42016-02-11 07:39:29 +0000512 for (Record *CurMap : InstrMapVec) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000513 ListInit *ColFields;
514 ColFields = CurMap->getValueAsListInit("ColFields");
515 ListInit *List = CurMap->getValueAsListInit("ValueCols");
516 std::vector<ListInit*> ValueCols;
Craig Topper664f6a02015-06-02 04:15:57 +0000517 unsigned ListSize = List->size();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000518
519 for (unsigned j = 0; j < ListSize; j++) {
520 ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j));
521
Craig Topper664f6a02015-06-02 04:15:57 +0000522 if (ListJ->size() != ColFields->size())
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000523 PrintFatalError("Record `" + CurMap->getName() + "', field "
524 "`ValueCols' entries don't match with the entries in 'ColFields' !");
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000525 ValueCols.push_back(ListJ);
526 }
527
Craig Topper664f6a02015-06-02 04:15:57 +0000528 for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000529 for (unsigned k = 0; k < ListSize; k++){
530 std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
531 ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
532 }
533 }
534 }
535
Craig Topper6e2edc42016-02-11 07:39:29 +0000536 for (auto &Entry : ColFieldValueMap) {
537 std::vector<Init*> FieldValues = Entry.second;
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000538
539 // Delete duplicate entries from ColFieldValueMap
Jyotsna Vermace3255e2013-02-14 17:58:13 +0000540 for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000541 Init *CurVal = FieldValues[i];
Jyotsna Vermace3255e2013-02-14 17:58:13 +0000542 for (unsigned j = i+1; j < FieldValues.size(); j++) {
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000543 if (CurVal == FieldValues[j]) {
544 FieldValues.erase(FieldValues.begin()+j);
Vedant Kumar47de8392016-12-01 19:38:50 +0000545 --j;
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000546 }
547 }
548 }
549
550 // Emit enumerated values for the column fields.
Craig Topper6e2edc42016-02-11 07:39:29 +0000551 OS << "enum " << Entry.first << " {\n";
Jyotsna Vermace3255e2013-02-14 17:58:13 +0000552 for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
Craig Topper6e2edc42016-02-11 07:39:29 +0000553 OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
Jyotsna Vermace3255e2013-02-14 17:58:13 +0000554 if (i != endFV - 1)
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000555 OS << ",\n";
556 else
557 OS << "\n};\n\n";
558 }
559 }
560}
561
562namespace llvm {
563//===----------------------------------------------------------------------===//
564// Parse 'InstrMapping' records and use the information to form relationship
565// between instructions. These relations are emitted as a tables along with the
566// functions to query them.
567//===----------------------------------------------------------------------===//
568void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
569 CodeGenTarget Target(Records);
Craig Topper86a9aee2017-07-07 06:22:35 +0000570 StringRef NameSpace = Target.getInstNamespace();
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000571 std::vector<Record*> InstrMapVec;
572 InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
573
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000574 if (InstrMapVec.empty())
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000575 return;
576
577 OS << "#ifdef GET_INSTRMAP_INFO\n";
578 OS << "#undef GET_INSTRMAP_INFO\n";
579 OS << "namespace llvm {\n\n";
Karl-Johan Karlssonbb6cf732017-03-27 07:13:44 +0000580 OS << "namespace " << NameSpace << " {\n\n";
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000581
582 // Emit coulumn field names and their values as enums.
583 emitEnums(OS, Records);
584
585 // Iterate over all instruction mapping records and construct relationship
586 // maps based on the information specified there.
587 //
Craig Topper6e2edc42016-02-11 07:39:29 +0000588 for (Record *CurMap : InstrMapVec) {
589 MapTableEmitter IMap(Target, Records, CurMap);
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000590
591 // Build RowInstrMap to group instructions based on their values for
592 // RowFields. In the process, also collect key instructions into
593 // KeyInstrVec.
594 IMap.buildRowInstrMap();
595
596 // Build MapTable to map key instructions with the corresponding column
597 // instructions.
598 IMap.buildMapTable();
599
600 // Emit map tables and the functions to query them.
601 IMap.emitTablesWithFunc(OS);
602 }
Karl-Johan Karlssonbb6cf732017-03-27 07:13:44 +0000603 OS << "} // End " << NameSpace << " namespace\n";
Sebastian Pop5c87daf2012-10-25 15:54:06 +0000604 OS << "} // End llvm namespace\n";
605 OS << "#endif // GET_INSTRMAP_INFO\n\n";
606}
607
608} // End llvm namespace