13861d79fSDimitry Andric //===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===//
23861d79fSDimitry Andric //
33861d79fSDimitry Andric //                     The LLVM Compiler Infrastructure
43861d79fSDimitry Andric //
53861d79fSDimitry Andric // This file is distributed under the University of Illinois Open Source
63861d79fSDimitry Andric // License. See LICENSE.TXT for details.
73861d79fSDimitry Andric //
83861d79fSDimitry Andric //===----------------------------------------------------------------------===//
93861d79fSDimitry Andric // CodeGenMapTable provides functionality for the TabelGen to create
103861d79fSDimitry Andric // relation mapping between instructions. Relation models are defined using
113861d79fSDimitry Andric // InstrMapping as a base class. This file implements the functionality which
123861d79fSDimitry Andric // parses these definitions and generates relation maps using the information
133861d79fSDimitry Andric // specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc
143861d79fSDimitry Andric // file along with the functions to query them.
153861d79fSDimitry Andric //
163861d79fSDimitry Andric // A relationship model to relate non-predicate instructions with their
173861d79fSDimitry Andric // predicated true/false forms can be defined as follows:
183861d79fSDimitry Andric //
193861d79fSDimitry Andric // def getPredOpcode : InstrMapping {
203861d79fSDimitry Andric //  let FilterClass = "PredRel";
213861d79fSDimitry Andric //  let RowFields = ["BaseOpcode"];
223861d79fSDimitry Andric //  let ColFields = ["PredSense"];
233861d79fSDimitry Andric //  let KeyCol = ["none"];
243861d79fSDimitry Andric //  let ValueCols = [["true"], ["false"]]; }
253861d79fSDimitry Andric //
263861d79fSDimitry Andric // CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc
273861d79fSDimitry Andric // file that contains the instructions modeling this relationship. This table
283861d79fSDimitry Andric // is defined in the function
293861d79fSDimitry Andric // "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)"
303861d79fSDimitry Andric // that can be used to retrieve the predicated form of the instruction by
313861d79fSDimitry Andric // passing its opcode value and the predicate sense (true/false) of the desired
323861d79fSDimitry Andric // instruction as arguments.
333861d79fSDimitry Andric //
343861d79fSDimitry Andric // Short description of the algorithm:
353861d79fSDimitry Andric //
363861d79fSDimitry Andric // 1) Iterate through all the records that derive from "InstrMapping" class.
373861d79fSDimitry Andric // 2) For each record, filter out instructions based on the FilterClass value.
383861d79fSDimitry Andric // 3) Iterate through this set of instructions and insert them into
393861d79fSDimitry Andric // RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the
403861d79fSDimitry Andric // vector of RowFields values and contains vectors of Records (instructions) as
413861d79fSDimitry Andric // values. RowFields is a list of fields that are required to have the same
423861d79fSDimitry Andric // values for all the instructions appearing in the same row of the relation
433861d79fSDimitry Andric // table. All the instructions in a given row of the relation table have some
443861d79fSDimitry Andric // sort of relationship with the key instruction defined by the corresponding
453861d79fSDimitry Andric // relationship model.
463861d79fSDimitry Andric //
473861d79fSDimitry Andric // Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ]
483861d79fSDimitry Andric // Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for
493861d79fSDimitry Andric // RowFields. These groups of instructions are later matched against ValueCols
503861d79fSDimitry Andric // to determine the column they belong to, if any.
513861d79fSDimitry Andric //
523861d79fSDimitry Andric // While building the RowInstrMap map, collect all the key instructions in
533861d79fSDimitry Andric // KeyInstrVec. These are the instructions having the same values as KeyCol
543861d79fSDimitry Andric // for all the fields listed in ColFields.
553861d79fSDimitry Andric //
563861d79fSDimitry Andric // For Example:
573861d79fSDimitry Andric //
583861d79fSDimitry Andric // Relate non-predicate instructions with their predicated true/false forms.
593861d79fSDimitry Andric //
603861d79fSDimitry Andric // def getPredOpcode : InstrMapping {
613861d79fSDimitry Andric //  let FilterClass = "PredRel";
623861d79fSDimitry Andric //  let RowFields = ["BaseOpcode"];
633861d79fSDimitry Andric //  let ColFields = ["PredSense"];
643861d79fSDimitry Andric //  let KeyCol = ["none"];
653861d79fSDimitry Andric //  let ValueCols = [["true"], ["false"]]; }
663861d79fSDimitry Andric //
673861d79fSDimitry Andric // Here, only instructions that have "none" as PredSense will be selected as key
683861d79fSDimitry Andric // instructions.
693861d79fSDimitry Andric //
703861d79fSDimitry Andric // 4) For each key instruction, get the group of instructions that share the
713861d79fSDimitry Andric // same key-value as the key instruction from RowInstrMap. Iterate over the list
723861d79fSDimitry Andric // of columns in ValueCols (it is defined as a list<list<string> >. Therefore,
733861d79fSDimitry Andric // it can specify multi-column relationships). For each column, find the
743861d79fSDimitry Andric // instruction from the group that matches all the values for the column.
753861d79fSDimitry Andric // Multiple matches are not allowed.
763861d79fSDimitry Andric //
773861d79fSDimitry Andric //===----------------------------------------------------------------------===//
783861d79fSDimitry Andric 
793861d79fSDimitry Andric #include "CodeGenTarget.h"
803861d79fSDimitry Andric #include "llvm/Support/Format.h"
813861d79fSDimitry Andric #include "llvm/TableGen/Error.h"
823861d79fSDimitry Andric using namespace llvm;
833861d79fSDimitry Andric typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy;
843861d79fSDimitry Andric 
853861d79fSDimitry Andric typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy;
863861d79fSDimitry Andric 
873861d79fSDimitry Andric namespace {
883861d79fSDimitry Andric 
893861d79fSDimitry Andric //===----------------------------------------------------------------------===//
903861d79fSDimitry Andric // This class is used to represent InstrMapping class defined in Target.td file.
913861d79fSDimitry Andric class InstrMap {
923861d79fSDimitry Andric private:
933861d79fSDimitry Andric   std::string Name;
943861d79fSDimitry Andric   std::string FilterClass;
953861d79fSDimitry Andric   ListInit *RowFields;
963861d79fSDimitry Andric   ListInit *ColFields;
973861d79fSDimitry Andric   ListInit *KeyCol;
983861d79fSDimitry Andric   std::vector<ListInit*> ValueCols;
993861d79fSDimitry Andric 
1003861d79fSDimitry Andric public:
1013861d79fSDimitry Andric   InstrMap(Record* MapRec) {
1023861d79fSDimitry Andric     Name = MapRec->getName();
1033861d79fSDimitry Andric 
1043861d79fSDimitry Andric     // FilterClass - It's used to reduce the search space only to the
1053861d79fSDimitry Andric     // instructions that define the kind of relationship modeled by
1063861d79fSDimitry Andric     // this InstrMapping object/record.
1073861d79fSDimitry Andric     const RecordVal *Filter = MapRec->getValue("FilterClass");
1083861d79fSDimitry Andric     FilterClass = Filter->getValue()->getAsUnquotedString();
1093861d79fSDimitry Andric 
1103861d79fSDimitry Andric     // List of fields/attributes that need to be same across all the
1113861d79fSDimitry Andric     // instructions in a row of the relation table.
1123861d79fSDimitry Andric     RowFields = MapRec->getValueAsListInit("RowFields");
1133861d79fSDimitry Andric 
1143861d79fSDimitry Andric     // List of fields/attributes that are constant across all the instruction
1153861d79fSDimitry Andric     // in a column of the relation table. Ex: ColFields = 'predSense'
1163861d79fSDimitry Andric     ColFields = MapRec->getValueAsListInit("ColFields");
1173861d79fSDimitry Andric 
1183861d79fSDimitry Andric     // Values for the fields/attributes listed in 'ColFields'.
11991bc56edSDimitry Andric     // Ex: KeyCol = 'noPred' -- key instruction is non-predicated
1203861d79fSDimitry Andric     KeyCol = MapRec->getValueAsListInit("KeyCol");
1213861d79fSDimitry Andric 
1223861d79fSDimitry Andric     // List of values for the fields/attributes listed in 'ColFields', one for
1233861d79fSDimitry Andric     // each column in the relation table.
1243861d79fSDimitry Andric     //
1253861d79fSDimitry Andric     // Ex: ValueCols = [['true'],['false']] -- it results two columns in the
1263861d79fSDimitry Andric     // table. First column requires all the instructions to have predSense
1273861d79fSDimitry Andric     // set to 'true' and second column requires it to be 'false'.
1283861d79fSDimitry Andric     ListInit *ColValList = MapRec->getValueAsListInit("ValueCols");
1293861d79fSDimitry Andric 
1303861d79fSDimitry Andric     // Each instruction map must specify at least one column for it to be valid.
131ff0cc061SDimitry Andric     if (ColValList->empty())
1323861d79fSDimitry Andric       PrintFatalError(MapRec->getLoc(), "InstrMapping record `" +
1333861d79fSDimitry Andric         MapRec->getName() + "' has empty " + "`ValueCols' field!");
1343861d79fSDimitry Andric 
13597bc6c73SDimitry Andric     for (Init *I : ColValList->getValues()) {
13697bc6c73SDimitry Andric       ListInit *ColI = dyn_cast<ListInit>(I);
1373861d79fSDimitry Andric 
1383861d79fSDimitry Andric       // Make sure that all the sub-lists in 'ValueCols' have same number of
1393861d79fSDimitry Andric       // elements as the fields in 'ColFields'.
14097bc6c73SDimitry Andric       if (ColI->size() != ColFields->size())
1413861d79fSDimitry Andric         PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() +
1423861d79fSDimitry Andric           "', field `ValueCols' entries don't match with " +
1433861d79fSDimitry Andric           " the entries in 'ColFields'!");
1443861d79fSDimitry Andric       ValueCols.push_back(ColI);
1453861d79fSDimitry Andric     }
1463861d79fSDimitry Andric   }
1473861d79fSDimitry Andric 
1483861d79fSDimitry Andric   std::string getName() const {
1493861d79fSDimitry Andric     return Name;
1503861d79fSDimitry Andric   }
1513861d79fSDimitry Andric 
1523861d79fSDimitry Andric   std::string getFilterClass() {
1533861d79fSDimitry Andric     return FilterClass;
1543861d79fSDimitry Andric   }
1553861d79fSDimitry Andric 
1563861d79fSDimitry Andric   ListInit *getRowFields() const {
1573861d79fSDimitry Andric     return RowFields;
1583861d79fSDimitry Andric   }
1593861d79fSDimitry Andric 
1603861d79fSDimitry Andric   ListInit *getColFields() const {
1613861d79fSDimitry Andric     return ColFields;
1623861d79fSDimitry Andric   }
1633861d79fSDimitry Andric 
1643861d79fSDimitry Andric   ListInit *getKeyCol() const {
1653861d79fSDimitry Andric     return KeyCol;
1663861d79fSDimitry Andric   }
1673861d79fSDimitry Andric 
1683861d79fSDimitry Andric   const std::vector<ListInit*> &getValueCols() const {
1693861d79fSDimitry Andric     return ValueCols;
1703861d79fSDimitry Andric   }
1713861d79fSDimitry Andric };
1723861d79fSDimitry Andric } // End anonymous namespace.
1733861d79fSDimitry Andric 
1743861d79fSDimitry Andric 
1753861d79fSDimitry Andric //===----------------------------------------------------------------------===//
1763861d79fSDimitry Andric // class MapTableEmitter : It builds the instruction relation maps using
1773861d79fSDimitry Andric // the information provided in InstrMapping records. It outputs these
1783861d79fSDimitry Andric // relationship maps as tables into XXXGenInstrInfo.inc file along with the
1793861d79fSDimitry Andric // functions to query them.
1803861d79fSDimitry Andric 
1813861d79fSDimitry Andric namespace {
1823861d79fSDimitry Andric class MapTableEmitter {
1833861d79fSDimitry Andric private:
1843861d79fSDimitry Andric //  std::string TargetName;
1853861d79fSDimitry Andric   const CodeGenTarget &Target;
1863861d79fSDimitry Andric   // InstrMapDesc - InstrMapping record to be processed.
1873861d79fSDimitry Andric   InstrMap InstrMapDesc;
1883861d79fSDimitry Andric 
1893861d79fSDimitry Andric   // InstrDefs - list of instructions filtered using FilterClass defined
1903861d79fSDimitry Andric   // in InstrMapDesc.
1913861d79fSDimitry Andric   std::vector<Record*> InstrDefs;
1923861d79fSDimitry Andric 
1933861d79fSDimitry Andric   // RowInstrMap - maps RowFields values to the instructions. It's keyed by the
1943861d79fSDimitry Andric   // values of the row fields and contains vector of records as values.
1953861d79fSDimitry Andric   RowInstrMapTy RowInstrMap;
1963861d79fSDimitry Andric 
1973861d79fSDimitry Andric   // KeyInstrVec - list of key instructions.
1983861d79fSDimitry Andric   std::vector<Record*> KeyInstrVec;
1993861d79fSDimitry Andric   DenseMap<Record*, std::vector<Record*> > MapTable;
2003861d79fSDimitry Andric 
2013861d79fSDimitry Andric public:
2023861d79fSDimitry Andric   MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
2033861d79fSDimitry Andric                   Target(Target), InstrMapDesc(IMRec) {
2043861d79fSDimitry Andric     const std::string FilterClass = InstrMapDesc.getFilterClass();
2053861d79fSDimitry Andric     InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
2063861d79fSDimitry Andric   }
2073861d79fSDimitry Andric 
2083861d79fSDimitry Andric   void buildRowInstrMap();
2093861d79fSDimitry Andric 
2103861d79fSDimitry Andric   // Returns true if an instruction is a key instruction, i.e., its ColFields
2113861d79fSDimitry Andric   // have same values as KeyCol.
2123861d79fSDimitry Andric   bool isKeyColInstr(Record* CurInstr);
2133861d79fSDimitry Andric 
2143861d79fSDimitry Andric   // Find column instruction corresponding to a key instruction based on the
2153861d79fSDimitry Andric   // constraints for that column.
2163861d79fSDimitry Andric   Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
2173861d79fSDimitry Andric 
2183861d79fSDimitry Andric   // Find column instructions for each key instruction based
2193861d79fSDimitry Andric   // on ValueCols and store them into MapTable.
2203861d79fSDimitry Andric   void buildMapTable();
2213861d79fSDimitry Andric 
2223861d79fSDimitry Andric   void emitBinSearch(raw_ostream &OS, unsigned TableSize);
2233861d79fSDimitry Andric   void emitTablesWithFunc(raw_ostream &OS);
2243861d79fSDimitry Andric   unsigned emitBinSearchTable(raw_ostream &OS);
2253861d79fSDimitry Andric 
2263861d79fSDimitry Andric   // Lookup functions to query binary search tables.
2273861d79fSDimitry Andric   void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
2283861d79fSDimitry Andric 
2293861d79fSDimitry Andric };
2303861d79fSDimitry Andric } // End anonymous namespace.
2313861d79fSDimitry Andric 
2323861d79fSDimitry Andric 
2333861d79fSDimitry Andric //===----------------------------------------------------------------------===//
2343861d79fSDimitry Andric // Process all the instructions that model this relation (alreday present in
2353861d79fSDimitry Andric // InstrDefs) and insert them into RowInstrMap which is keyed by the values of
2363861d79fSDimitry Andric // the fields listed as RowFields. It stores vectors of records as values.
2373861d79fSDimitry Andric // All the related instructions have the same values for the RowFields thus are
2383861d79fSDimitry Andric // part of the same key-value pair.
2393861d79fSDimitry Andric //===----------------------------------------------------------------------===//
2403861d79fSDimitry Andric 
2413861d79fSDimitry Andric void MapTableEmitter::buildRowInstrMap() {
24297bc6c73SDimitry Andric   for (Record *CurInstr : InstrDefs) {
2433861d79fSDimitry Andric     std::vector<Init*> KeyValue;
2443861d79fSDimitry Andric     ListInit *RowFields = InstrMapDesc.getRowFields();
24597bc6c73SDimitry Andric     for (Init *RowField : RowFields->getValues()) {
24697bc6c73SDimitry Andric       Init *CurInstrVal = CurInstr->getValue(RowField)->getValue();
2473861d79fSDimitry Andric       KeyValue.push_back(CurInstrVal);
2483861d79fSDimitry Andric     }
2493861d79fSDimitry Andric 
2503861d79fSDimitry Andric     // Collect key instructions into KeyInstrVec. Later, these instructions are
2513861d79fSDimitry Andric     // processed to assign column position to the instructions sharing
2523861d79fSDimitry Andric     // their KeyValue in RowInstrMap.
2533861d79fSDimitry Andric     if (isKeyColInstr(CurInstr))
2543861d79fSDimitry Andric       KeyInstrVec.push_back(CurInstr);
2553861d79fSDimitry Andric 
2563861d79fSDimitry Andric     RowInstrMap[KeyValue].push_back(CurInstr);
2573861d79fSDimitry Andric   }
2583861d79fSDimitry Andric }
2593861d79fSDimitry Andric 
2603861d79fSDimitry Andric //===----------------------------------------------------------------------===//
2613861d79fSDimitry Andric // Return true if an instruction is a KeyCol instruction.
2623861d79fSDimitry Andric //===----------------------------------------------------------------------===//
2633861d79fSDimitry Andric 
2643861d79fSDimitry Andric bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
2653861d79fSDimitry Andric   ListInit *ColFields = InstrMapDesc.getColFields();
2663861d79fSDimitry Andric   ListInit *KeyCol = InstrMapDesc.getKeyCol();
2673861d79fSDimitry Andric 
2683861d79fSDimitry Andric   // Check if the instruction is a KeyCol instruction.
2693861d79fSDimitry Andric   bool MatchFound = true;
27097bc6c73SDimitry Andric   for (unsigned j = 0, endCF = ColFields->size();
2713861d79fSDimitry Andric       (j < endCF) && MatchFound; j++) {
2723861d79fSDimitry Andric     RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
2733861d79fSDimitry Andric     std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
2743861d79fSDimitry Andric     std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
2753861d79fSDimitry Andric     MatchFound = (CurInstrVal == KeyColValue);
2763861d79fSDimitry Andric   }
2773861d79fSDimitry Andric   return MatchFound;
2783861d79fSDimitry Andric }
2793861d79fSDimitry Andric 
2803861d79fSDimitry Andric //===----------------------------------------------------------------------===//
2813861d79fSDimitry Andric // Build a map to link key instructions with the column instructions arranged
2823861d79fSDimitry Andric // according to their column positions.
2833861d79fSDimitry Andric //===----------------------------------------------------------------------===//
2843861d79fSDimitry Andric 
2853861d79fSDimitry Andric void MapTableEmitter::buildMapTable() {
2863861d79fSDimitry Andric   // Find column instructions for a given key based on the ColField
2873861d79fSDimitry Andric   // constraints.
2883861d79fSDimitry Andric   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
2893861d79fSDimitry Andric   unsigned NumOfCols = ValueCols.size();
29097bc6c73SDimitry Andric   for (Record *CurKeyInstr : KeyInstrVec) {
2913861d79fSDimitry Andric     std::vector<Record*> ColInstrVec(NumOfCols);
2923861d79fSDimitry Andric 
2933861d79fSDimitry Andric     // Find the column instruction based on the constraints for the column.
2943861d79fSDimitry Andric     for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
2953861d79fSDimitry Andric       ListInit *CurValueCol = ValueCols[ColIdx];
2963861d79fSDimitry Andric       Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
2973861d79fSDimitry Andric       ColInstrVec[ColIdx] = ColInstr;
2983861d79fSDimitry Andric     }
2993861d79fSDimitry Andric     MapTable[CurKeyInstr] = ColInstrVec;
3003861d79fSDimitry Andric   }
3013861d79fSDimitry Andric }
3023861d79fSDimitry Andric 
3033861d79fSDimitry Andric //===----------------------------------------------------------------------===//
3043861d79fSDimitry Andric // Find column instruction based on the constraints for that column.
3053861d79fSDimitry Andric //===----------------------------------------------------------------------===//
3063861d79fSDimitry Andric 
3073861d79fSDimitry Andric Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
3083861d79fSDimitry Andric                                            ListInit *CurValueCol) {
3093861d79fSDimitry Andric   ListInit *RowFields = InstrMapDesc.getRowFields();
3103861d79fSDimitry Andric   std::vector<Init*> KeyValue;
3113861d79fSDimitry Andric 
3123861d79fSDimitry Andric   // Construct KeyValue using KeyInstr's values for RowFields.
31397bc6c73SDimitry Andric   for (Init *RowField : RowFields->getValues()) {
31497bc6c73SDimitry Andric     Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
3153861d79fSDimitry Andric     KeyValue.push_back(KeyInstrVal);
3163861d79fSDimitry Andric   }
3173861d79fSDimitry Andric 
3183861d79fSDimitry Andric   // Get all the instructions that share the same KeyValue as the KeyInstr
3193861d79fSDimitry Andric   // in RowInstrMap. We search through these instructions to find a match
3203861d79fSDimitry Andric   // for the current column, i.e., the instruction which has the same values
3213861d79fSDimitry Andric   // as CurValueCol for all the fields in ColFields.
3223861d79fSDimitry Andric   const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
3233861d79fSDimitry Andric 
3243861d79fSDimitry Andric   ListInit *ColFields = InstrMapDesc.getColFields();
32591bc56edSDimitry Andric   Record *MatchInstr = nullptr;
3263861d79fSDimitry Andric 
3273861d79fSDimitry Andric   for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) {
3283861d79fSDimitry Andric     bool MatchFound = true;
3293861d79fSDimitry Andric     Record *CurInstr = RelatedInstrVec[i];
33097bc6c73SDimitry Andric     for (unsigned j = 0, endCF = ColFields->size();
3313861d79fSDimitry Andric         (j < endCF) && MatchFound; j++) {
3323861d79fSDimitry Andric       Init *ColFieldJ = ColFields->getElement(j);
3333861d79fSDimitry Andric       Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
3343861d79fSDimitry Andric       std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
3353861d79fSDimitry Andric       Init *ColFieldJVallue = CurValueCol->getElement(j);
3363861d79fSDimitry Andric       MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
3373861d79fSDimitry Andric     }
3383861d79fSDimitry Andric 
3393861d79fSDimitry Andric     if (MatchFound) {
3403ca95b02SDimitry Andric       if (MatchInstr) {
3413ca95b02SDimitry Andric         // Already had a match
3423861d79fSDimitry Andric         // Error if multiple matches are found for a column.
3433ca95b02SDimitry Andric         std::string KeyValueStr;
3443ca95b02SDimitry Andric         for (Init *Value : KeyValue) {
3453ca95b02SDimitry Andric           if (!KeyValueStr.empty())
3463ca95b02SDimitry Andric             KeyValueStr += ", ";
3473ca95b02SDimitry Andric           KeyValueStr += Value->getAsString();
3483ca95b02SDimitry Andric         }
3493ca95b02SDimitry Andric 
3503861d79fSDimitry Andric         PrintFatalError("Multiple matches found for `" + KeyInstr->getName() +
3513ca95b02SDimitry Andric               "', for the relation `" + InstrMapDesc.getName() + "', row fields [" +
3523ca95b02SDimitry Andric               KeyValueStr + "], column `" + CurValueCol->getAsString() + "'");
3533ca95b02SDimitry Andric       }
3543861d79fSDimitry Andric       MatchInstr = CurInstr;
3553861d79fSDimitry Andric     }
3563861d79fSDimitry Andric   }
3573861d79fSDimitry Andric   return MatchInstr;
3583861d79fSDimitry Andric }
3593861d79fSDimitry Andric 
3603861d79fSDimitry Andric //===----------------------------------------------------------------------===//
3613861d79fSDimitry Andric // Emit one table per relation. Only instructions with a valid relation of a
3623861d79fSDimitry Andric // given type are included in the table sorted by their enum values (opcodes).
3633861d79fSDimitry Andric // Binary search is used for locating instructions in the table.
3643861d79fSDimitry Andric //===----------------------------------------------------------------------===//
3653861d79fSDimitry Andric 
3663861d79fSDimitry Andric unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
3673861d79fSDimitry Andric 
3683ca95b02SDimitry Andric   ArrayRef<const CodeGenInstruction*> NumberedInstructions =
3693861d79fSDimitry Andric                                             Target.getInstructionsByEnumValue();
3703861d79fSDimitry Andric   std::string TargetName = Target.getName();
3713861d79fSDimitry Andric   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
3723861d79fSDimitry Andric   unsigned NumCol = ValueCols.size();
3733861d79fSDimitry Andric   unsigned TotalNumInstr = NumberedInstructions.size();
3743861d79fSDimitry Andric   unsigned TableSize = 0;
3753861d79fSDimitry Andric 
3763861d79fSDimitry Andric   OS << "static const uint16_t "<<InstrMapDesc.getName();
3773861d79fSDimitry Andric   // Number of columns in the table are NumCol+1 because key instructions are
3783861d79fSDimitry Andric   // emitted as first column.
3793861d79fSDimitry Andric   OS << "Table[]["<< NumCol+1 << "] = {\n";
3803861d79fSDimitry Andric   for (unsigned i = 0; i < TotalNumInstr; i++) {
3813861d79fSDimitry Andric     Record *CurInstr = NumberedInstructions[i]->TheDef;
3823861d79fSDimitry Andric     std::vector<Record*> ColInstrs = MapTable[CurInstr];
3833861d79fSDimitry Andric     std::string OutStr("");
3843861d79fSDimitry Andric     unsigned RelExists = 0;
385ff0cc061SDimitry Andric     if (!ColInstrs.empty()) {
3863861d79fSDimitry Andric       for (unsigned j = 0; j < NumCol; j++) {
38791bc56edSDimitry Andric         if (ColInstrs[j] != nullptr) {
3883861d79fSDimitry Andric           RelExists = 1;
3893861d79fSDimitry Andric           OutStr += ", ";
3903861d79fSDimitry Andric           OutStr += TargetName;
3913861d79fSDimitry Andric           OutStr += "::";
3923861d79fSDimitry Andric           OutStr += ColInstrs[j]->getName();
39391bc56edSDimitry Andric         } else { OutStr += ", (uint16_t)-1U";}
3943861d79fSDimitry Andric       }
3953861d79fSDimitry Andric 
3963861d79fSDimitry Andric       if (RelExists) {
3973861d79fSDimitry Andric         OS << "  { " << TargetName << "::" << CurInstr->getName();
3983861d79fSDimitry Andric         OS << OutStr <<" },\n";
3993861d79fSDimitry Andric         TableSize++;
4003861d79fSDimitry Andric       }
4013861d79fSDimitry Andric     }
4023861d79fSDimitry Andric   }
4033861d79fSDimitry Andric   if (!TableSize) {
4043861d79fSDimitry Andric     OS << "  { " << TargetName << "::" << "INSTRUCTION_LIST_END, ";
4053861d79fSDimitry Andric     OS << TargetName << "::" << "INSTRUCTION_LIST_END }";
4063861d79fSDimitry Andric   }
4073861d79fSDimitry Andric   OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
4083861d79fSDimitry Andric   return TableSize;
4093861d79fSDimitry Andric }
4103861d79fSDimitry Andric 
4113861d79fSDimitry Andric //===----------------------------------------------------------------------===//
4123861d79fSDimitry Andric // Emit binary search algorithm as part of the functions used to query
4133861d79fSDimitry Andric // relation tables.
4143861d79fSDimitry Andric //===----------------------------------------------------------------------===//
4153861d79fSDimitry Andric 
4163861d79fSDimitry Andric void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
4173861d79fSDimitry Andric   OS << "  unsigned mid;\n";
4183861d79fSDimitry Andric   OS << "  unsigned start = 0;\n";
4193861d79fSDimitry Andric   OS << "  unsigned end = " << TableSize << ";\n";
4203861d79fSDimitry Andric   OS << "  while (start < end) {\n";
4213861d79fSDimitry Andric   OS << "    mid = start + (end - start)/2;\n";
4223861d79fSDimitry Andric   OS << "    if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
4233861d79fSDimitry Andric   OS << "      break;\n";
4243861d79fSDimitry Andric   OS << "    }\n";
4253861d79fSDimitry Andric   OS << "    if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
4263861d79fSDimitry Andric   OS << "      end = mid;\n";
4273861d79fSDimitry Andric   OS << "    else\n";
4283861d79fSDimitry Andric   OS << "      start = mid + 1;\n";
4293861d79fSDimitry Andric   OS << "  }\n";
4303861d79fSDimitry Andric   OS << "  if (start == end)\n";
4313861d79fSDimitry Andric   OS << "    return -1; // Instruction doesn't exist in this table.\n\n";
4323861d79fSDimitry Andric }
4333861d79fSDimitry Andric 
4343861d79fSDimitry Andric //===----------------------------------------------------------------------===//
4353861d79fSDimitry Andric // Emit functions to query relation tables.
4363861d79fSDimitry Andric //===----------------------------------------------------------------------===//
4373861d79fSDimitry Andric 
4383861d79fSDimitry Andric void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
4393861d79fSDimitry Andric                                            unsigned TableSize) {
4403861d79fSDimitry Andric 
4413861d79fSDimitry Andric   ListInit *ColFields = InstrMapDesc.getColFields();
4423861d79fSDimitry Andric   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
4433861d79fSDimitry Andric 
4443861d79fSDimitry Andric   // Emit binary search algorithm to locate instructions in the
4453861d79fSDimitry Andric   // relation table. If found, return opcode value from the appropriate column
4463861d79fSDimitry Andric   // of the table.
4473861d79fSDimitry Andric   emitBinSearch(OS, TableSize);
4483861d79fSDimitry Andric 
4493861d79fSDimitry Andric   if (ValueCols.size() > 1) {
4503861d79fSDimitry Andric     for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
4513861d79fSDimitry Andric       ListInit *ColumnI = ValueCols[i];
45297bc6c73SDimitry Andric       for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
4533861d79fSDimitry Andric         std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
4543861d79fSDimitry Andric         OS << "  if (in" << ColName;
4553861d79fSDimitry Andric         OS << " == ";
4563861d79fSDimitry Andric         OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
45797bc6c73SDimitry Andric         if (j < ColumnI->size() - 1) OS << " && ";
4583861d79fSDimitry Andric         else OS << ")\n";
4593861d79fSDimitry Andric       }
4603861d79fSDimitry Andric       OS << "    return " << InstrMapDesc.getName();
4613861d79fSDimitry Andric       OS << "Table[mid]["<<i+1<<"];\n";
4623861d79fSDimitry Andric     }
4633861d79fSDimitry Andric     OS << "  return -1;";
4643861d79fSDimitry Andric   }
4653861d79fSDimitry Andric   else
4663861d79fSDimitry Andric     OS << "  return " << InstrMapDesc.getName() << "Table[mid][1];\n";
4673861d79fSDimitry Andric 
4683861d79fSDimitry Andric   OS <<"}\n\n";
4693861d79fSDimitry Andric }
4703861d79fSDimitry Andric 
4713861d79fSDimitry Andric //===----------------------------------------------------------------------===//
4723861d79fSDimitry Andric // Emit relation tables and the functions to query them.
4733861d79fSDimitry Andric //===----------------------------------------------------------------------===//
4743861d79fSDimitry Andric 
4753861d79fSDimitry Andric void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
4763861d79fSDimitry Andric 
4773861d79fSDimitry Andric   // Emit function name and the input parameters : mostly opcode value of the
4783861d79fSDimitry Andric   // current instruction. However, if a table has multiple columns (more than 2
4793861d79fSDimitry Andric   // since first column is used for the key instructions), then we also need
4803861d79fSDimitry Andric   // to pass another input to indicate the column to be selected.
4813861d79fSDimitry Andric 
4823861d79fSDimitry Andric   ListInit *ColFields = InstrMapDesc.getColFields();
4833861d79fSDimitry Andric   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
4847d523365SDimitry Andric   OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
4853861d79fSDimitry Andric   OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
4863861d79fSDimitry Andric   if (ValueCols.size() > 1) {
48797bc6c73SDimitry Andric     for (Init *CF : ColFields->getValues()) {
48897bc6c73SDimitry Andric       std::string ColName = CF->getAsUnquotedString();
4893861d79fSDimitry Andric       OS << ", enum " << ColName << " in" << ColName << ") {\n";
4903861d79fSDimitry Andric     }
4913861d79fSDimitry Andric   } else { OS << ") {\n"; }
4923861d79fSDimitry Andric 
4933861d79fSDimitry Andric   // Emit map table.
4943861d79fSDimitry Andric   unsigned TableSize = emitBinSearchTable(OS);
4953861d79fSDimitry Andric 
4963861d79fSDimitry Andric   // Emit rest of the function body.
4973861d79fSDimitry Andric   emitMapFuncBody(OS, TableSize);
4983861d79fSDimitry Andric }
4993861d79fSDimitry Andric 
5003861d79fSDimitry Andric //===----------------------------------------------------------------------===//
5013861d79fSDimitry Andric // Emit enums for the column fields across all the instruction maps.
5023861d79fSDimitry Andric //===----------------------------------------------------------------------===//
5033861d79fSDimitry Andric 
5043861d79fSDimitry Andric static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
5053861d79fSDimitry Andric 
5063861d79fSDimitry Andric   std::vector<Record*> InstrMapVec;
5073861d79fSDimitry Andric   InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
5083861d79fSDimitry Andric   std::map<std::string, std::vector<Init*> > ColFieldValueMap;
5093861d79fSDimitry Andric 
5103861d79fSDimitry Andric   // Iterate over all InstrMapping records and create a map between column
5113861d79fSDimitry Andric   // fields and their possible values across all records.
5123ca95b02SDimitry Andric   for (Record *CurMap : InstrMapVec) {
5133861d79fSDimitry Andric     ListInit *ColFields;
5143861d79fSDimitry Andric     ColFields = CurMap->getValueAsListInit("ColFields");
5153861d79fSDimitry Andric     ListInit *List = CurMap->getValueAsListInit("ValueCols");
5163861d79fSDimitry Andric     std::vector<ListInit*> ValueCols;
51797bc6c73SDimitry Andric     unsigned ListSize = List->size();
5183861d79fSDimitry Andric 
5193861d79fSDimitry Andric     for (unsigned j = 0; j < ListSize; j++) {
5203861d79fSDimitry Andric       ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j));
5213861d79fSDimitry Andric 
52297bc6c73SDimitry Andric       if (ListJ->size() != ColFields->size())
5233861d79fSDimitry Andric         PrintFatalError("Record `" + CurMap->getName() + "', field "
5243861d79fSDimitry Andric           "`ValueCols' entries don't match with the entries in 'ColFields' !");
5253861d79fSDimitry Andric       ValueCols.push_back(ListJ);
5263861d79fSDimitry Andric     }
5273861d79fSDimitry Andric 
52897bc6c73SDimitry Andric     for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) {
5293861d79fSDimitry Andric       for (unsigned k = 0; k < ListSize; k++){
5303861d79fSDimitry Andric         std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
5313861d79fSDimitry Andric         ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
5323861d79fSDimitry Andric       }
5333861d79fSDimitry Andric     }
5343861d79fSDimitry Andric   }
5353861d79fSDimitry Andric 
5363ca95b02SDimitry Andric   for (auto &Entry : ColFieldValueMap) {
5373ca95b02SDimitry Andric     std::vector<Init*> FieldValues = Entry.second;
5383861d79fSDimitry Andric 
5393861d79fSDimitry Andric     // Delete duplicate entries from ColFieldValueMap
540139f7f9bSDimitry Andric     for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
5413861d79fSDimitry Andric       Init *CurVal = FieldValues[i];
542139f7f9bSDimitry Andric       for (unsigned j = i+1; j < FieldValues.size(); j++) {
5433861d79fSDimitry Andric         if (CurVal == FieldValues[j]) {
5443861d79fSDimitry Andric           FieldValues.erase(FieldValues.begin()+j);
5453861d79fSDimitry Andric         }
5463861d79fSDimitry Andric       }
5473861d79fSDimitry Andric     }
5483861d79fSDimitry Andric 
5493861d79fSDimitry Andric     // Emit enumerated values for the column fields.
5503ca95b02SDimitry Andric     OS << "enum " << Entry.first << " {\n";
551139f7f9bSDimitry Andric     for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
5523ca95b02SDimitry Andric       OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
553139f7f9bSDimitry Andric       if (i != endFV - 1)
5543861d79fSDimitry Andric         OS << ",\n";
5553861d79fSDimitry Andric       else
5563861d79fSDimitry Andric         OS << "\n};\n\n";
5573861d79fSDimitry Andric     }
5583861d79fSDimitry Andric   }
5593861d79fSDimitry Andric }
5603861d79fSDimitry Andric 
5613861d79fSDimitry Andric namespace llvm {
5623861d79fSDimitry Andric //===----------------------------------------------------------------------===//
5633861d79fSDimitry Andric // Parse 'InstrMapping' records and use the information to form relationship
5643861d79fSDimitry Andric // between instructions. These relations are emitted as a tables along with the
5653861d79fSDimitry Andric // functions to query them.
5663861d79fSDimitry Andric //===----------------------------------------------------------------------===//
5673861d79fSDimitry Andric void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
5683861d79fSDimitry Andric   CodeGenTarget Target(Records);
5693861d79fSDimitry Andric   std::string TargetName = Target.getName();
5703861d79fSDimitry Andric   std::vector<Record*> InstrMapVec;
5713861d79fSDimitry Andric   InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
5723861d79fSDimitry Andric 
573ff0cc061SDimitry Andric   if (InstrMapVec.empty())
5743861d79fSDimitry Andric     return;
5753861d79fSDimitry Andric 
5763861d79fSDimitry Andric   OS << "#ifdef GET_INSTRMAP_INFO\n";
5773861d79fSDimitry Andric   OS << "#undef GET_INSTRMAP_INFO\n";
5783861d79fSDimitry Andric   OS << "namespace llvm {\n\n";
5793861d79fSDimitry Andric   OS << "namespace " << TargetName << " {\n\n";
5803861d79fSDimitry Andric 
5813861d79fSDimitry Andric   // Emit coulumn field names and their values as enums.
5823861d79fSDimitry Andric   emitEnums(OS, Records);
5833861d79fSDimitry Andric 
5843861d79fSDimitry Andric   // Iterate over all instruction mapping records and construct relationship
5853861d79fSDimitry Andric   // maps based on the information specified there.
5863861d79fSDimitry Andric   //
5873ca95b02SDimitry Andric   for (Record *CurMap : InstrMapVec) {
5883ca95b02SDimitry Andric     MapTableEmitter IMap(Target, Records, CurMap);
5893861d79fSDimitry Andric 
5903861d79fSDimitry Andric     // Build RowInstrMap to group instructions based on their values for
5913861d79fSDimitry Andric     // RowFields. In the process, also collect key instructions into
5923861d79fSDimitry Andric     // KeyInstrVec.
5933861d79fSDimitry Andric     IMap.buildRowInstrMap();
5943861d79fSDimitry Andric 
5953861d79fSDimitry Andric     // Build MapTable to map key instructions with the corresponding column
5963861d79fSDimitry Andric     // instructions.
5973861d79fSDimitry Andric     IMap.buildMapTable();
5983861d79fSDimitry Andric 
5993861d79fSDimitry Andric     // Emit map tables and the functions to query them.
6003861d79fSDimitry Andric     IMap.emitTablesWithFunc(OS);
6013861d79fSDimitry Andric   }
6023861d79fSDimitry Andric   OS << "} // End " << TargetName << " namespace\n";
6033861d79fSDimitry Andric   OS << "} // End llvm namespace\n";
6043861d79fSDimitry Andric   OS << "#endif // GET_INSTRMAP_INFO\n\n";
6053861d79fSDimitry Andric }
6063861d79fSDimitry Andric 
6073861d79fSDimitry Andric } // End llvm namespace
608