CodeGenMapTable.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. #include "CodeGenTarget.h"
  78. #include "llvm/Support/Format.h"
  79. #include "llvm/TableGen/Error.h"
  80. using namespace llvm;
  81. typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy;
  82. typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy;
  83. namespace {
  84. //===----------------------------------------------------------------------===//
  85. // This class is used to represent InstrMapping class defined in Target.td file.
  86. class InstrMap {
  87. private:
  88. std::string Name;
  89. std::string FilterClass;
  90. ListInit *RowFields;
  91. ListInit *ColFields;
  92. ListInit *KeyCol;
  93. std::vector<ListInit*> ValueCols;
  94. public:
  95. InstrMap(Record* MapRec) {
  96. Name = std::string(MapRec->getName());
  97. // FilterClass - It's used to reduce the search space only to the
  98. // instructions that define the kind of relationship modeled by
  99. // this InstrMapping object/record.
  100. const RecordVal *Filter = MapRec->getValue("FilterClass");
  101. FilterClass = Filter->getValue()->getAsUnquotedString();
  102. // List of fields/attributes that need to be same across all the
  103. // instructions in a row of the relation table.
  104. RowFields = MapRec->getValueAsListInit("RowFields");
  105. // List of fields/attributes that are constant across all the instruction
  106. // in a column of the relation table. Ex: ColFields = 'predSense'
  107. ColFields = MapRec->getValueAsListInit("ColFields");
  108. // Values for the fields/attributes listed in 'ColFields'.
  109. // Ex: KeyCol = 'noPred' -- key instruction is non-predicated
  110. KeyCol = MapRec->getValueAsListInit("KeyCol");
  111. // List of values for the fields/attributes listed in 'ColFields', one for
  112. // each column in the relation table.
  113. //
  114. // Ex: ValueCols = [['true'],['false']] -- it results two columns in the
  115. // table. First column requires all the instructions to have predSense
  116. // set to 'true' and second column requires it to be 'false'.
  117. ListInit *ColValList = MapRec->getValueAsListInit("ValueCols");
  118. // Each instruction map must specify at least one column for it to be valid.
  119. if (ColValList->empty())
  120. PrintFatalError(MapRec->getLoc(), "InstrMapping record `" +
  121. MapRec->getName() + "' has empty " + "`ValueCols' field!");
  122. for (Init *I : ColValList->getValues()) {
  123. auto *ColI = cast<ListInit>(I);
  124. // Make sure that all the sub-lists in 'ValueCols' have same number of
  125. // elements as the fields in 'ColFields'.
  126. if (ColI->size() != ColFields->size())
  127. PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() +
  128. "', field `ValueCols' entries don't match with " +
  129. " the entries in 'ColFields'!");
  130. ValueCols.push_back(ColI);
  131. }
  132. }
  133. const std::string &getName() const { return Name; }
  134. const std::string &getFilterClass() const { return FilterClass; }
  135. ListInit *getRowFields() const { return RowFields; }
  136. ListInit *getColFields() const { return ColFields; }
  137. ListInit *getKeyCol() const { return KeyCol; }
  138. const std::vector<ListInit*> &getValueCols() const {
  139. return ValueCols;
  140. }
  141. };
  142. } // end anonymous namespace
  143. //===----------------------------------------------------------------------===//
  144. // class MapTableEmitter : It builds the instruction relation maps using
  145. // the information provided in InstrMapping records. It outputs these
  146. // relationship maps as tables into XXXGenInstrInfo.inc file along with the
  147. // functions to query them.
  148. namespace {
  149. class MapTableEmitter {
  150. private:
  151. // std::string TargetName;
  152. const CodeGenTarget &Target;
  153. // InstrMapDesc - InstrMapping record to be processed.
  154. InstrMap InstrMapDesc;
  155. // InstrDefs - list of instructions filtered using FilterClass defined
  156. // in InstrMapDesc.
  157. std::vector<Record*> InstrDefs;
  158. // RowInstrMap - maps RowFields values to the instructions. It's keyed by the
  159. // values of the row fields and contains vector of records as values.
  160. RowInstrMapTy RowInstrMap;
  161. // KeyInstrVec - list of key instructions.
  162. std::vector<Record*> KeyInstrVec;
  163. DenseMap<Record*, std::vector<Record*> > MapTable;
  164. public:
  165. MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
  166. Target(Target), InstrMapDesc(IMRec) {
  167. const std::string &FilterClass = InstrMapDesc.getFilterClass();
  168. InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
  169. }
  170. void buildRowInstrMap();
  171. // Returns true if an instruction is a key instruction, i.e., its ColFields
  172. // have same values as KeyCol.
  173. bool isKeyColInstr(Record* CurInstr);
  174. // Find column instruction corresponding to a key instruction based on the
  175. // constraints for that column.
  176. Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
  177. // Find column instructions for each key instruction based
  178. // on ValueCols and store them into MapTable.
  179. void buildMapTable();
  180. void emitBinSearch(raw_ostream &OS, unsigned TableSize);
  181. void emitTablesWithFunc(raw_ostream &OS);
  182. unsigned emitBinSearchTable(raw_ostream &OS);
  183. // Lookup functions to query binary search tables.
  184. void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
  185. };
  186. } // end anonymous namespace
  187. //===----------------------------------------------------------------------===//
  188. // Process all the instructions that model this relation (alreday present in
  189. // InstrDefs) and insert them into RowInstrMap which is keyed by the values of
  190. // the fields listed as RowFields. It stores vectors of records as values.
  191. // All the related instructions have the same values for the RowFields thus are
  192. // part of the same key-value pair.
  193. //===----------------------------------------------------------------------===//
  194. void MapTableEmitter::buildRowInstrMap() {
  195. for (Record *CurInstr : InstrDefs) {
  196. std::vector<Init*> KeyValue;
  197. ListInit *RowFields = InstrMapDesc.getRowFields();
  198. for (Init *RowField : RowFields->getValues()) {
  199. RecordVal *RecVal = CurInstr->getValue(RowField);
  200. if (RecVal == nullptr)
  201. PrintFatalError(CurInstr->getLoc(), "No value " +
  202. RowField->getAsString() + " found in \"" +
  203. CurInstr->getName() + "\" instruction description.");
  204. Init *CurInstrVal = RecVal->getValue();
  205. KeyValue.push_back(CurInstrVal);
  206. }
  207. // Collect key instructions into KeyInstrVec. Later, these instructions are
  208. // processed to assign column position to the instructions sharing
  209. // their KeyValue in RowInstrMap.
  210. if (isKeyColInstr(CurInstr))
  211. KeyInstrVec.push_back(CurInstr);
  212. RowInstrMap[KeyValue].push_back(CurInstr);
  213. }
  214. }
  215. //===----------------------------------------------------------------------===//
  216. // Return true if an instruction is a KeyCol instruction.
  217. //===----------------------------------------------------------------------===//
  218. bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
  219. ListInit *ColFields = InstrMapDesc.getColFields();
  220. ListInit *KeyCol = InstrMapDesc.getKeyCol();
  221. // Check if the instruction is a KeyCol instruction.
  222. bool MatchFound = true;
  223. for (unsigned j = 0, endCF = ColFields->size();
  224. (j < endCF) && MatchFound; j++) {
  225. RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
  226. std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
  227. std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
  228. MatchFound = (CurInstrVal == KeyColValue);
  229. }
  230. return MatchFound;
  231. }
  232. //===----------------------------------------------------------------------===//
  233. // Build a map to link key instructions with the column instructions arranged
  234. // according to their column positions.
  235. //===----------------------------------------------------------------------===//
  236. void MapTableEmitter::buildMapTable() {
  237. // Find column instructions for a given key based on the ColField
  238. // constraints.
  239. const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
  240. unsigned NumOfCols = ValueCols.size();
  241. for (Record *CurKeyInstr : KeyInstrVec) {
  242. std::vector<Record*> ColInstrVec(NumOfCols);
  243. // Find the column instruction based on the constraints for the column.
  244. for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
  245. ListInit *CurValueCol = ValueCols[ColIdx];
  246. Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
  247. ColInstrVec[ColIdx] = ColInstr;
  248. }
  249. MapTable[CurKeyInstr] = ColInstrVec;
  250. }
  251. }
  252. //===----------------------------------------------------------------------===//
  253. // Find column instruction based on the constraints for that column.
  254. //===----------------------------------------------------------------------===//
  255. Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
  256. ListInit *CurValueCol) {
  257. ListInit *RowFields = InstrMapDesc.getRowFields();
  258. std::vector<Init*> KeyValue;
  259. // Construct KeyValue using KeyInstr's values for RowFields.
  260. for (Init *RowField : RowFields->getValues()) {
  261. Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
  262. KeyValue.push_back(KeyInstrVal);
  263. }
  264. // Get all the instructions that share the same KeyValue as the KeyInstr
  265. // in RowInstrMap. We search through these instructions to find a match
  266. // for the current column, i.e., the instruction which has the same values
  267. // as CurValueCol for all the fields in ColFields.
  268. const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
  269. ListInit *ColFields = InstrMapDesc.getColFields();
  270. Record *MatchInstr = nullptr;
  271. for (llvm::Record *CurInstr : RelatedInstrVec) {
  272. bool MatchFound = true;
  273. for (unsigned j = 0, endCF = ColFields->size();
  274. (j < endCF) && MatchFound; j++) {
  275. Init *ColFieldJ = ColFields->getElement(j);
  276. Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
  277. std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
  278. Init *ColFieldJVallue = CurValueCol->getElement(j);
  279. MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
  280. }
  281. if (MatchFound) {
  282. if (MatchInstr) {
  283. // Already had a match
  284. // Error if multiple matches are found for a column.
  285. std::string KeyValueStr;
  286. for (Init *Value : KeyValue) {
  287. if (!KeyValueStr.empty())
  288. KeyValueStr += ", ";
  289. KeyValueStr += Value->getAsString();
  290. }
  291. PrintFatalError("Multiple matches found for `" + KeyInstr->getName() +
  292. "', for the relation `" + InstrMapDesc.getName() +
  293. "', row fields [" + KeyValueStr + "], column `" +
  294. CurValueCol->getAsString() + "'");
  295. }
  296. MatchInstr = CurInstr;
  297. }
  298. }
  299. return MatchInstr;
  300. }
  301. //===----------------------------------------------------------------------===//
  302. // Emit one table per relation. Only instructions with a valid relation of a
  303. // given type are included in the table sorted by their enum values (opcodes).
  304. // Binary search is used for locating instructions in the table.
  305. //===----------------------------------------------------------------------===//
  306. unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
  307. ArrayRef<const CodeGenInstruction*> NumberedInstructions =
  308. Target.getInstructionsByEnumValue();
  309. StringRef Namespace = Target.getInstNamespace();
  310. const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
  311. unsigned NumCol = ValueCols.size();
  312. unsigned TotalNumInstr = NumberedInstructions.size();
  313. unsigned TableSize = 0;
  314. OS << "static const uint16_t "<<InstrMapDesc.getName();
  315. // Number of columns in the table are NumCol+1 because key instructions are
  316. // emitted as first column.
  317. OS << "Table[]["<< NumCol+1 << "] = {\n";
  318. for (unsigned i = 0; i < TotalNumInstr; i++) {
  319. Record *CurInstr = NumberedInstructions[i]->TheDef;
  320. std::vector<Record*> ColInstrs = MapTable[CurInstr];
  321. std::string OutStr;
  322. unsigned RelExists = 0;
  323. if (!ColInstrs.empty()) {
  324. for (unsigned j = 0; j < NumCol; j++) {
  325. if (ColInstrs[j] != nullptr) {
  326. RelExists = 1;
  327. OutStr += ", ";
  328. OutStr += Namespace;
  329. OutStr += "::";
  330. OutStr += ColInstrs[j]->getName();
  331. } else { OutStr += ", (uint16_t)-1U";}
  332. }
  333. if (RelExists) {
  334. OS << " { " << Namespace << "::" << CurInstr->getName();
  335. OS << OutStr <<" },\n";
  336. TableSize++;
  337. }
  338. }
  339. }
  340. if (!TableSize) {
  341. OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, ";
  342. OS << Namespace << "::" << "INSTRUCTION_LIST_END }";
  343. }
  344. OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
  345. return TableSize;
  346. }
  347. //===----------------------------------------------------------------------===//
  348. // Emit binary search algorithm as part of the functions used to query
  349. // relation tables.
  350. //===----------------------------------------------------------------------===//
  351. void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
  352. OS << " unsigned mid;\n";
  353. OS << " unsigned start = 0;\n";
  354. OS << " unsigned end = " << TableSize << ";\n";
  355. OS << " while (start < end) {\n";
  356. OS << " mid = start + (end - start) / 2;\n";
  357. OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
  358. OS << " break;\n";
  359. OS << " }\n";
  360. OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
  361. OS << " end = mid;\n";
  362. OS << " else\n";
  363. OS << " start = mid + 1;\n";
  364. OS << " }\n";
  365. OS << " if (start == end)\n";
  366. OS << " return -1; // Instruction doesn't exist in this table.\n\n";
  367. }
  368. //===----------------------------------------------------------------------===//
  369. // Emit functions to query relation tables.
  370. //===----------------------------------------------------------------------===//
  371. void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
  372. unsigned TableSize) {
  373. ListInit *ColFields = InstrMapDesc.getColFields();
  374. const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
  375. // Emit binary search algorithm to locate instructions in the
  376. // relation table. If found, return opcode value from the appropriate column
  377. // of the table.
  378. emitBinSearch(OS, TableSize);
  379. if (ValueCols.size() > 1) {
  380. for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
  381. ListInit *ColumnI = ValueCols[i];
  382. OS << " if (";
  383. for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
  384. std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
  385. OS << "in" << ColName;
  386. OS << " == ";
  387. OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
  388. if (j < ColumnI->size() - 1)
  389. OS << " && ";
  390. }
  391. OS << ")\n";
  392. OS << " return " << InstrMapDesc.getName();
  393. OS << "Table[mid]["<<i+1<<"];\n";
  394. }
  395. OS << " return -1;";
  396. }
  397. else
  398. OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n";
  399. OS <<"}\n\n";
  400. }
  401. //===----------------------------------------------------------------------===//
  402. // Emit relation tables and the functions to query them.
  403. //===----------------------------------------------------------------------===//
  404. void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
  405. // Emit function name and the input parameters : mostly opcode value of the
  406. // current instruction. However, if a table has multiple columns (more than 2
  407. // since first column is used for the key instructions), then we also need
  408. // to pass another input to indicate the column to be selected.
  409. ListInit *ColFields = InstrMapDesc.getColFields();
  410. const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
  411. OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
  412. OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
  413. if (ValueCols.size() > 1) {
  414. for (Init *CF : ColFields->getValues()) {
  415. std::string ColName = CF->getAsUnquotedString();
  416. OS << ", enum " << ColName << " in" << ColName;
  417. }
  418. }
  419. OS << ") {\n";
  420. // Emit map table.
  421. unsigned TableSize = emitBinSearchTable(OS);
  422. // Emit rest of the function body.
  423. emitMapFuncBody(OS, TableSize);
  424. }
  425. //===----------------------------------------------------------------------===//
  426. // Emit enums for the column fields across all the instruction maps.
  427. //===----------------------------------------------------------------------===//
  428. static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
  429. std::vector<Record*> InstrMapVec;
  430. InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
  431. std::map<std::string, std::vector<Init*> > ColFieldValueMap;
  432. // Iterate over all InstrMapping records and create a map between column
  433. // fields and their possible values across all records.
  434. for (Record *CurMap : InstrMapVec) {
  435. ListInit *ColFields;
  436. ColFields = CurMap->getValueAsListInit("ColFields");
  437. ListInit *List = CurMap->getValueAsListInit("ValueCols");
  438. std::vector<ListInit*> ValueCols;
  439. unsigned ListSize = List->size();
  440. for (unsigned j = 0; j < ListSize; j++) {
  441. auto *ListJ = cast<ListInit>(List->getElement(j));
  442. if (ListJ->size() != ColFields->size())
  443. PrintFatalError("Record `" + CurMap->getName() + "', field "
  444. "`ValueCols' entries don't match with the entries in 'ColFields' !");
  445. ValueCols.push_back(ListJ);
  446. }
  447. for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) {
  448. for (unsigned k = 0; k < ListSize; k++){
  449. std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
  450. ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
  451. }
  452. }
  453. }
  454. for (auto &Entry : ColFieldValueMap) {
  455. std::vector<Init*> FieldValues = Entry.second;
  456. // Delete duplicate entries from ColFieldValueMap
  457. for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
  458. Init *CurVal = FieldValues[i];
  459. for (unsigned j = i+1; j < FieldValues.size(); j++) {
  460. if (CurVal == FieldValues[j]) {
  461. FieldValues.erase(FieldValues.begin()+j);
  462. --j;
  463. }
  464. }
  465. }
  466. // Emit enumerated values for the column fields.
  467. OS << "enum " << Entry.first << " {\n";
  468. for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
  469. OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
  470. if (i != endFV - 1)
  471. OS << ",\n";
  472. else
  473. OS << "\n};\n\n";
  474. }
  475. }
  476. }
  477. namespace llvm {
  478. //===----------------------------------------------------------------------===//
  479. // Parse 'InstrMapping' records and use the information to form relationship
  480. // between instructions. These relations are emitted as a tables along with the
  481. // functions to query them.
  482. //===----------------------------------------------------------------------===//
  483. void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
  484. CodeGenTarget Target(Records);
  485. StringRef NameSpace = Target.getInstNamespace();
  486. std::vector<Record*> InstrMapVec;
  487. InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
  488. if (InstrMapVec.empty())
  489. return;
  490. OS << "#ifdef GET_INSTRMAP_INFO\n";
  491. OS << "#undef GET_INSTRMAP_INFO\n";
  492. OS << "namespace llvm {\n\n";
  493. OS << "namespace " << NameSpace << " {\n\n";
  494. // Emit coulumn field names and their values as enums.
  495. emitEnums(OS, Records);
  496. // Iterate over all instruction mapping records and construct relationship
  497. // maps based on the information specified there.
  498. //
  499. for (Record *CurMap : InstrMapVec) {
  500. MapTableEmitter IMap(Target, Records, CurMap);
  501. // Build RowInstrMap to group instructions based on their values for
  502. // RowFields. In the process, also collect key instructions into
  503. // KeyInstrVec.
  504. IMap.buildRowInstrMap();
  505. // Build MapTable to map key instructions with the corresponding column
  506. // instructions.
  507. IMap.buildMapTable();
  508. // Emit map tables and the functions to query them.
  509. IMap.emitTablesWithFunc(OS);
  510. }
  511. OS << "} // end namespace " << NameSpace << "\n";
  512. OS << "} // end namespace llvm\n";
  513. OS << "#endif // GET_INSTRMAP_INFO\n\n";
  514. }
  515. } // End llvm namespace