CodeGenMapTable.cpp 23 KB

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