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 TableGen 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 "Common/CodeGenInstruction.h" |
79 | #include "Common/CodeGenTarget.h" |
80 | #include "llvm/TableGen/Error.h" |
81 | #include "llvm/TableGen/Record.h" |
82 | using namespace llvm; |
83 | typedef std::map<std::string, std::vector<Record *>> InstrRelMapTy; |
84 | |
85 | typedef std::map<std::vector<Init *>, std::vector<Record *>> RowInstrMapTy; |
86 | |
87 | namespace { |
88 | |
89 | //===----------------------------------------------------------------------===// |
90 | // This class is used to represent InstrMapping class defined in Target.td file. |
91 | class InstrMap { |
92 | private: |
93 | std::string Name; |
94 | std::string FilterClass; |
95 | ListInit *RowFields; |
96 | ListInit *ColFields; |
97 | ListInit *KeyCol; |
98 | std::vector<ListInit *> ValueCols; |
99 | |
100 | public: |
101 | InstrMap(Record *MapRec) { |
102 | Name = std::string(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(Name: "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(FieldName: "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(FieldName: "ColFields" ); |
117 | |
118 | // Values for the fields/attributes listed in 'ColFields'. |
119 | // Ex: KeyCol = 'noPred' -- key instruction is non-predicated |
120 | KeyCol = MapRec->getValueAsListInit(FieldName: "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(FieldName: "ValueCols" ); |
129 | |
130 | // Each instruction map must specify at least one column for it to be valid. |
131 | if (ColValList->empty()) |
132 | PrintFatalError(ErrorLoc: MapRec->getLoc(), Msg: "InstrMapping record `" + |
133 | MapRec->getName() + "' has empty " + |
134 | "`ValueCols' field!" ); |
135 | |
136 | for (Init *I : ColValList->getValues()) { |
137 | auto *ColI = cast<ListInit>(Val: I); |
138 | |
139 | // Make sure that all the sub-lists in 'ValueCols' have same number of |
140 | // elements as the fields in 'ColFields'. |
141 | if (ColI->size() != ColFields->size()) |
142 | PrintFatalError(ErrorLoc: MapRec->getLoc(), |
143 | Msg: "Record `" + MapRec->getName() + |
144 | "', field `ValueCols' entries don't match with " + |
145 | " the entries in 'ColFields'!" ); |
146 | ValueCols.push_back(x: ColI); |
147 | } |
148 | } |
149 | |
150 | const std::string &getName() const { return Name; } |
151 | |
152 | const std::string &getFilterClass() const { return FilterClass; } |
153 | |
154 | ListInit *getRowFields() const { return RowFields; } |
155 | |
156 | ListInit *getColFields() const { return ColFields; } |
157 | |
158 | ListInit *getKeyCol() const { return KeyCol; } |
159 | |
160 | const std::vector<ListInit *> &getValueCols() const { return ValueCols; } |
161 | }; |
162 | } // end anonymous namespace |
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(ClassName: 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 | } // end anonymous namespace |
219 | |
220 | //===----------------------------------------------------------------------===// |
221 | // Process all the instructions that model this relation (alreday present in |
222 | // InstrDefs) and insert them into RowInstrMap which is keyed by the values of |
223 | // the fields listed as RowFields. It stores vectors of records as values. |
224 | // All the related instructions have the same values for the RowFields thus are |
225 | // part of the same key-value pair. |
226 | //===----------------------------------------------------------------------===// |
227 | |
228 | void MapTableEmitter::buildRowInstrMap() { |
229 | for (Record *CurInstr : InstrDefs) { |
230 | std::vector<Init *> KeyValue; |
231 | ListInit *RowFields = InstrMapDesc.getRowFields(); |
232 | for (Init *RowField : RowFields->getValues()) { |
233 | RecordVal *RecVal = CurInstr->getValue(Name: RowField); |
234 | if (RecVal == nullptr) |
235 | PrintFatalError(ErrorLoc: CurInstr->getLoc(), |
236 | Msg: "No value " + RowField->getAsString() + " found in \"" + |
237 | CurInstr->getName() + |
238 | "\" instruction description." ); |
239 | Init *CurInstrVal = RecVal->getValue(); |
240 | KeyValue.push_back(x: CurInstrVal); |
241 | } |
242 | |
243 | // Collect key instructions into KeyInstrVec. Later, these instructions are |
244 | // processed to assign column position to the instructions sharing |
245 | // their KeyValue in RowInstrMap. |
246 | if (isKeyColInstr(CurInstr)) |
247 | KeyInstrVec.push_back(x: CurInstr); |
248 | |
249 | RowInstrMap[KeyValue].push_back(x: CurInstr); |
250 | } |
251 | } |
252 | |
253 | //===----------------------------------------------------------------------===// |
254 | // Return true if an instruction is a KeyCol instruction. |
255 | //===----------------------------------------------------------------------===// |
256 | |
257 | bool MapTableEmitter::isKeyColInstr(Record *CurInstr) { |
258 | ListInit *ColFields = InstrMapDesc.getColFields(); |
259 | ListInit *KeyCol = InstrMapDesc.getKeyCol(); |
260 | |
261 | // Check if the instruction is a KeyCol instruction. |
262 | bool MatchFound = true; |
263 | for (unsigned j = 0, endCF = ColFields->size(); (j < endCF) && MatchFound; |
264 | j++) { |
265 | RecordVal *ColFieldName = CurInstr->getValue(Name: ColFields->getElement(i: j)); |
266 | std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString(); |
267 | std::string KeyColValue = KeyCol->getElement(i: j)->getAsUnquotedString(); |
268 | MatchFound = (CurInstrVal == KeyColValue); |
269 | } |
270 | return MatchFound; |
271 | } |
272 | |
273 | //===----------------------------------------------------------------------===// |
274 | // Build a map to link key instructions with the column instructions arranged |
275 | // according to their column positions. |
276 | //===----------------------------------------------------------------------===// |
277 | |
278 | void MapTableEmitter::buildMapTable() { |
279 | // Find column instructions for a given key based on the ColField |
280 | // constraints. |
281 | const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols(); |
282 | unsigned NumOfCols = ValueCols.size(); |
283 | for (Record *CurKeyInstr : KeyInstrVec) { |
284 | std::vector<Record *> ColInstrVec(NumOfCols); |
285 | |
286 | // Find the column instruction based on the constraints for the column. |
287 | for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) { |
288 | ListInit *CurValueCol = ValueCols[ColIdx]; |
289 | Record *ColInstr = getInstrForColumn(KeyInstr: CurKeyInstr, CurValueCol); |
290 | ColInstrVec[ColIdx] = ColInstr; |
291 | } |
292 | MapTable[CurKeyInstr] = ColInstrVec; |
293 | } |
294 | } |
295 | |
296 | //===----------------------------------------------------------------------===// |
297 | // Find column instruction based on the constraints for that column. |
298 | //===----------------------------------------------------------------------===// |
299 | |
300 | Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr, |
301 | ListInit *CurValueCol) { |
302 | ListInit *RowFields = InstrMapDesc.getRowFields(); |
303 | std::vector<Init *> KeyValue; |
304 | |
305 | // Construct KeyValue using KeyInstr's values for RowFields. |
306 | for (Init *RowField : RowFields->getValues()) { |
307 | Init *KeyInstrVal = KeyInstr->getValue(Name: RowField)->getValue(); |
308 | KeyValue.push_back(x: KeyInstrVal); |
309 | } |
310 | |
311 | // Get all the instructions that share the same KeyValue as the KeyInstr |
312 | // in RowInstrMap. We search through these instructions to find a match |
313 | // for the current column, i.e., the instruction which has the same values |
314 | // as CurValueCol for all the fields in ColFields. |
315 | const std::vector<Record *> &RelatedInstrVec = RowInstrMap[KeyValue]; |
316 | |
317 | ListInit *ColFields = InstrMapDesc.getColFields(); |
318 | Record *MatchInstr = nullptr; |
319 | |
320 | for (llvm::Record *CurInstr : RelatedInstrVec) { |
321 | bool MatchFound = true; |
322 | for (unsigned j = 0, endCF = ColFields->size(); (j < endCF) && MatchFound; |
323 | j++) { |
324 | Init *ColFieldJ = ColFields->getElement(i: j); |
325 | Init *CurInstrInit = CurInstr->getValue(Name: ColFieldJ)->getValue(); |
326 | std::string CurInstrVal = CurInstrInit->getAsUnquotedString(); |
327 | Init *ColFieldJVallue = CurValueCol->getElement(i: j); |
328 | MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString()); |
329 | } |
330 | |
331 | if (MatchFound) { |
332 | if (MatchInstr) { |
333 | // Already had a match |
334 | // Error if multiple matches are found for a column. |
335 | std::string KeyValueStr; |
336 | for (Init *Value : KeyValue) { |
337 | if (!KeyValueStr.empty()) |
338 | KeyValueStr += ", " ; |
339 | KeyValueStr += Value->getAsString(); |
340 | } |
341 | |
342 | PrintFatalError(Msg: "Multiple matches found for `" + KeyInstr->getName() + |
343 | "', for the relation `" + InstrMapDesc.getName() + |
344 | "', row fields [" + KeyValueStr + "], column `" + |
345 | CurValueCol->getAsString() + "'" ); |
346 | } |
347 | MatchInstr = CurInstr; |
348 | } |
349 | } |
350 | return MatchInstr; |
351 | } |
352 | |
353 | //===----------------------------------------------------------------------===// |
354 | // Emit one table per relation. Only instructions with a valid relation of a |
355 | // given type are included in the table sorted by their enum values (opcodes). |
356 | // Binary search is used for locating instructions in the table. |
357 | //===----------------------------------------------------------------------===// |
358 | |
359 | unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) { |
360 | |
361 | ArrayRef<const CodeGenInstruction *> NumberedInstructions = |
362 | Target.getInstructionsByEnumValue(); |
363 | StringRef Namespace = Target.getInstNamespace(); |
364 | const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols(); |
365 | unsigned NumCol = ValueCols.size(); |
366 | unsigned TotalNumInstr = NumberedInstructions.size(); |
367 | unsigned TableSize = 0; |
368 | |
369 | OS << "static const uint16_t " << InstrMapDesc.getName(); |
370 | // Number of columns in the table are NumCol+1 because key instructions are |
371 | // emitted as first column. |
372 | OS << "Table[][" << NumCol + 1 << "] = {\n" ; |
373 | for (unsigned i = 0; i < TotalNumInstr; i++) { |
374 | Record *CurInstr = NumberedInstructions[i]->TheDef; |
375 | std::vector<Record *> ColInstrs = MapTable[CurInstr]; |
376 | std::string OutStr; |
377 | unsigned RelExists = 0; |
378 | if (!ColInstrs.empty()) { |
379 | for (unsigned j = 0; j < NumCol; j++) { |
380 | if (ColInstrs[j] != nullptr) { |
381 | RelExists = 1; |
382 | OutStr += ", " ; |
383 | OutStr += Namespace; |
384 | OutStr += "::" ; |
385 | OutStr += ColInstrs[j]->getName(); |
386 | } else { |
387 | OutStr += ", (uint16_t)-1U" ; |
388 | } |
389 | } |
390 | |
391 | if (RelExists) { |
392 | OS << " { " << Namespace << "::" << CurInstr->getName(); |
393 | OS << OutStr << " },\n" ; |
394 | TableSize++; |
395 | } |
396 | } |
397 | } |
398 | if (!TableSize) { |
399 | OS << " { " << Namespace << "::" |
400 | << "INSTRUCTION_LIST_END, " ; |
401 | OS << Namespace << "::" |
402 | << "INSTRUCTION_LIST_END }" ; |
403 | } |
404 | OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n" ; |
405 | return TableSize; |
406 | } |
407 | |
408 | //===----------------------------------------------------------------------===// |
409 | // Emit binary search algorithm as part of the functions used to query |
410 | // relation tables. |
411 | //===----------------------------------------------------------------------===// |
412 | |
413 | void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) { |
414 | OS << " unsigned mid;\n" ; |
415 | OS << " unsigned start = 0;\n" ; |
416 | OS << " unsigned end = " << TableSize << ";\n" ; |
417 | OS << " while (start < end) {\n" ; |
418 | OS << " mid = start + (end - start) / 2;\n" ; |
419 | OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n" ; |
420 | OS << " break;\n" ; |
421 | OS << " }\n" ; |
422 | OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n" ; |
423 | OS << " end = mid;\n" ; |
424 | OS << " else\n" ; |
425 | OS << " start = mid + 1;\n" ; |
426 | OS << " }\n" ; |
427 | OS << " if (start == end)\n" ; |
428 | OS << " return -1; // Instruction doesn't exist in this table.\n\n" ; |
429 | } |
430 | |
431 | //===----------------------------------------------------------------------===// |
432 | // Emit functions to query relation tables. |
433 | //===----------------------------------------------------------------------===// |
434 | |
435 | void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, unsigned TableSize) { |
436 | |
437 | ListInit *ColFields = InstrMapDesc.getColFields(); |
438 | const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols(); |
439 | |
440 | // Emit binary search algorithm to locate instructions in the |
441 | // relation table. If found, return opcode value from the appropriate column |
442 | // of the table. |
443 | emitBinSearch(OS, TableSize); |
444 | |
445 | if (ValueCols.size() > 1) { |
446 | for (unsigned i = 0, e = ValueCols.size(); i < e; i++) { |
447 | ListInit *ColumnI = ValueCols[i]; |
448 | OS << " if (" ; |
449 | for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) { |
450 | std::string ColName = ColFields->getElement(i: j)->getAsUnquotedString(); |
451 | OS << "in" << ColName; |
452 | OS << " == " ; |
453 | OS << ColName << "_" << ColumnI->getElement(i: j)->getAsUnquotedString(); |
454 | if (j < ColumnI->size() - 1) |
455 | OS << " && " ; |
456 | } |
457 | OS << ")\n" ; |
458 | OS << " return " << InstrMapDesc.getName(); |
459 | OS << "Table[mid][" << i + 1 << "];\n" ; |
460 | } |
461 | OS << " return -1;" ; |
462 | } else |
463 | OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n" ; |
464 | |
465 | OS << "}\n\n" ; |
466 | } |
467 | |
468 | //===----------------------------------------------------------------------===// |
469 | // Emit relation tables and the functions to query them. |
470 | //===----------------------------------------------------------------------===// |
471 | |
472 | void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) { |
473 | |
474 | // Emit function name and the input parameters : mostly opcode value of the |
475 | // current instruction. However, if a table has multiple columns (more than 2 |
476 | // since first column is used for the key instructions), then we also need |
477 | // to pass another input to indicate the column to be selected. |
478 | |
479 | ListInit *ColFields = InstrMapDesc.getColFields(); |
480 | const std::vector<ListInit *> &ValueCols = InstrMapDesc.getValueCols(); |
481 | OS << "// " << InstrMapDesc.getName() << "\nLLVM_READONLY\n" ; |
482 | OS << "int " << InstrMapDesc.getName() << "(uint16_t Opcode" ; |
483 | if (ValueCols.size() > 1) { |
484 | for (Init *CF : ColFields->getValues()) { |
485 | std::string ColName = CF->getAsUnquotedString(); |
486 | OS << ", enum " << ColName << " in" << ColName; |
487 | } |
488 | } |
489 | OS << ") {\n" ; |
490 | |
491 | // Emit map table. |
492 | unsigned TableSize = emitBinSearchTable(OS); |
493 | |
494 | // Emit rest of the function body. |
495 | emitMapFuncBody(OS, TableSize); |
496 | } |
497 | |
498 | //===----------------------------------------------------------------------===// |
499 | // Emit enums for the column fields across all the instruction maps. |
500 | //===----------------------------------------------------------------------===// |
501 | |
502 | static void emitEnums(raw_ostream &OS, RecordKeeper &Records) { |
503 | |
504 | std::vector<Record *> InstrMapVec; |
505 | InstrMapVec = Records.getAllDerivedDefinitions(ClassName: "InstrMapping" ); |
506 | std::map<std::string, std::vector<Init *>> ColFieldValueMap; |
507 | |
508 | // Iterate over all InstrMapping records and create a map between column |
509 | // fields and their possible values across all records. |
510 | for (Record *CurMap : InstrMapVec) { |
511 | ListInit *ColFields; |
512 | ColFields = CurMap->getValueAsListInit(FieldName: "ColFields" ); |
513 | ListInit *List = CurMap->getValueAsListInit(FieldName: "ValueCols" ); |
514 | std::vector<ListInit *> ValueCols; |
515 | unsigned ListSize = List->size(); |
516 | |
517 | for (unsigned j = 0; j < ListSize; j++) { |
518 | auto *ListJ = cast<ListInit>(Val: List->getElement(i: j)); |
519 | |
520 | if (ListJ->size() != ColFields->size()) |
521 | PrintFatalError(Msg: "Record `" + CurMap->getName() + |
522 | "', field " |
523 | "`ValueCols' entries don't match with the entries in " |
524 | "'ColFields' !" ); |
525 | ValueCols.push_back(x: ListJ); |
526 | } |
527 | |
528 | for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) { |
529 | for (unsigned k = 0; k < ListSize; k++) { |
530 | std::string ColName = ColFields->getElement(i: j)->getAsUnquotedString(); |
531 | ColFieldValueMap[ColName].push_back(x: (ValueCols[k])->getElement(i: j)); |
532 | } |
533 | } |
534 | } |
535 | |
536 | for (auto &Entry : ColFieldValueMap) { |
537 | std::vector<Init *> FieldValues = Entry.second; |
538 | |
539 | // Delete duplicate entries from ColFieldValueMap |
540 | for (unsigned i = 0; i < FieldValues.size() - 1; i++) { |
541 | Init *CurVal = FieldValues[i]; |
542 | for (unsigned j = i + 1; j < FieldValues.size(); j++) { |
543 | if (CurVal == FieldValues[j]) { |
544 | FieldValues.erase(position: FieldValues.begin() + j); |
545 | --j; |
546 | } |
547 | } |
548 | } |
549 | |
550 | // Emit enumerated values for the column fields. |
551 | OS << "enum " << Entry.first << " {\n" ; |
552 | for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) { |
553 | OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString(); |
554 | if (i != endFV - 1) |
555 | OS << ",\n" ; |
556 | else |
557 | OS << "\n};\n\n" ; |
558 | } |
559 | } |
560 | } |
561 | |
562 | namespace 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 | //===----------------------------------------------------------------------===// |
568 | void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) { |
569 | CodeGenTarget Target(Records); |
570 | StringRef NameSpace = Target.getInstNamespace(); |
571 | std::vector<Record *> InstrMapVec; |
572 | InstrMapVec = Records.getAllDerivedDefinitions(ClassName: "InstrMapping" ); |
573 | |
574 | if (InstrMapVec.empty()) |
575 | return; |
576 | |
577 | OS << "#ifdef GET_INSTRMAP_INFO\n" ; |
578 | OS << "#undef GET_INSTRMAP_INFO\n" ; |
579 | OS << "namespace llvm {\n\n" ; |
580 | OS << "namespace " << NameSpace << " {\n\n" ; |
581 | |
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 | // |
588 | for (Record *CurMap : InstrMapVec) { |
589 | MapTableEmitter IMap(Target, Records, CurMap); |
590 | |
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 | } |
603 | OS << "} // end namespace " << NameSpace << "\n" ; |
604 | OS << "} // end namespace llvm\n" ; |
605 | OS << "#endif // GET_INSTRMAP_INFO\n\n" ; |
606 | } |
607 | |
608 | } // namespace llvm |
609 | |