15c87daf2SSebastian Pop //===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===//
25c87daf2SSebastian Pop //
35c87daf2SSebastian Pop //                     The LLVM Compiler Infrastructure
45c87daf2SSebastian Pop //
55c87daf2SSebastian Pop // This file is distributed under the University of Illinois Open Source
65c87daf2SSebastian Pop // License. See LICENSE.TXT for details.
75c87daf2SSebastian Pop //
85c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
95c87daf2SSebastian Pop // CodeGenMapTable provides functionality for the TabelGen to create
105c87daf2SSebastian Pop // relation mapping between instructions. Relation models are defined using
115c87daf2SSebastian Pop // InstrMapping as a base class. This file implements the functionality which
125c87daf2SSebastian Pop // parses these definitions and generates relation maps using the information
135c87daf2SSebastian Pop // specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc
145c87daf2SSebastian Pop // file along with the functions to query them.
155c87daf2SSebastian Pop //
165c87daf2SSebastian Pop // A relationship model to relate non-predicate instructions with their
175c87daf2SSebastian Pop // predicated true/false forms can be defined as follows:
185c87daf2SSebastian Pop //
195c87daf2SSebastian Pop // def getPredOpcode : InstrMapping {
205c87daf2SSebastian Pop //  let FilterClass = "PredRel";
215c87daf2SSebastian Pop //  let RowFields = ["BaseOpcode"];
225c87daf2SSebastian Pop //  let ColFields = ["PredSense"];
235c87daf2SSebastian Pop //  let KeyCol = ["none"];
245c87daf2SSebastian Pop //  let ValueCols = [["true"], ["false"]]; }
255c87daf2SSebastian Pop //
265c87daf2SSebastian Pop // CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc
275c87daf2SSebastian Pop // file that contains the instructions modeling this relationship. This table
285c87daf2SSebastian Pop // is defined in the function
295c87daf2SSebastian Pop // "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)"
305c87daf2SSebastian Pop // that can be used to retrieve the predicated form of the instruction by
315c87daf2SSebastian Pop // passing its opcode value and the predicate sense (true/false) of the desired
325c87daf2SSebastian Pop // instruction as arguments.
335c87daf2SSebastian Pop //
345c87daf2SSebastian Pop // Short description of the algorithm:
355c87daf2SSebastian Pop //
365c87daf2SSebastian Pop // 1) Iterate through all the records that derive from "InstrMapping" class.
375c87daf2SSebastian Pop // 2) For each record, filter out instructions based on the FilterClass value.
385c87daf2SSebastian Pop // 3) Iterate through this set of instructions and insert them into
395c87daf2SSebastian Pop // RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the
405c87daf2SSebastian Pop // vector of RowFields values and contains vectors of Records (instructions) as
415c87daf2SSebastian Pop // values. RowFields is a list of fields that are required to have the same
425c87daf2SSebastian Pop // values for all the instructions appearing in the same row of the relation
435c87daf2SSebastian Pop // table. All the instructions in a given row of the relation table have some
445c87daf2SSebastian Pop // sort of relationship with the key instruction defined by the corresponding
455c87daf2SSebastian Pop // relationship model.
465c87daf2SSebastian Pop //
475c87daf2SSebastian Pop // Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ]
485c87daf2SSebastian Pop // Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for
495c87daf2SSebastian Pop // RowFields. These groups of instructions are later matched against ValueCols
505c87daf2SSebastian Pop // to determine the column they belong to, if any.
515c87daf2SSebastian Pop //
525c87daf2SSebastian Pop // While building the RowInstrMap map, collect all the key instructions in
535c87daf2SSebastian Pop // KeyInstrVec. These are the instructions having the same values as KeyCol
545c87daf2SSebastian Pop // for all the fields listed in ColFields.
555c87daf2SSebastian Pop //
565c87daf2SSebastian Pop // For Example:
575c87daf2SSebastian Pop //
585c87daf2SSebastian Pop // Relate non-predicate instructions with their predicated true/false forms.
595c87daf2SSebastian Pop //
605c87daf2SSebastian Pop // def getPredOpcode : InstrMapping {
615c87daf2SSebastian Pop //  let FilterClass = "PredRel";
625c87daf2SSebastian Pop //  let RowFields = ["BaseOpcode"];
635c87daf2SSebastian Pop //  let ColFields = ["PredSense"];
645c87daf2SSebastian Pop //  let KeyCol = ["none"];
655c87daf2SSebastian Pop //  let ValueCols = [["true"], ["false"]]; }
665c87daf2SSebastian Pop //
675c87daf2SSebastian Pop // Here, only instructions that have "none" as PredSense will be selected as key
685c87daf2SSebastian Pop // instructions.
695c87daf2SSebastian Pop //
705c87daf2SSebastian Pop // 4) For each key instruction, get the group of instructions that share the
715c87daf2SSebastian Pop // same key-value as the key instruction from RowInstrMap. Iterate over the list
725c87daf2SSebastian Pop // of columns in ValueCols (it is defined as a list<list<string> >. Therefore,
735c87daf2SSebastian Pop // it can specify multi-column relationships). For each column, find the
745c87daf2SSebastian Pop // instruction from the group that matches all the values for the column.
755c87daf2SSebastian Pop // Multiple matches are not allowed.
765c87daf2SSebastian Pop //
775c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
785c87daf2SSebastian Pop 
795c87daf2SSebastian Pop #include "CodeGenTarget.h"
805c87daf2SSebastian Pop #include "llvm/Support/Format.h"
815c87daf2SSebastian Pop using namespace llvm;
825c87daf2SSebastian Pop typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy;
835c87daf2SSebastian Pop 
845c87daf2SSebastian Pop typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy;
855c87daf2SSebastian Pop 
865c87daf2SSebastian Pop namespace {
875c87daf2SSebastian Pop 
885c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
895c87daf2SSebastian Pop // This class is used to represent InstrMapping class defined in Target.td file.
905c87daf2SSebastian Pop class InstrMap {
915c87daf2SSebastian Pop private:
925c87daf2SSebastian Pop   std::string Name;
935c87daf2SSebastian Pop   std::string FilterClass;
945c87daf2SSebastian Pop   ListInit *RowFields;
955c87daf2SSebastian Pop   ListInit *ColFields;
965c87daf2SSebastian Pop   ListInit *KeyCol;
975c87daf2SSebastian Pop   std::vector<ListInit*> ValueCols;
985c87daf2SSebastian Pop 
995c87daf2SSebastian Pop public:
1005c87daf2SSebastian Pop   InstrMap(Record* MapRec) {
1015c87daf2SSebastian Pop     Name = MapRec->getName();
1025c87daf2SSebastian Pop 
1035c87daf2SSebastian Pop     // FilterClass - It's used to reduce the search space only to the
1045c87daf2SSebastian Pop     // instructions that define the kind of relationship modeled by
1055c87daf2SSebastian Pop     // this InstrMapping object/record.
1065c87daf2SSebastian Pop     const RecordVal *Filter = MapRec->getValue("FilterClass");
1075c87daf2SSebastian Pop     FilterClass = Filter->getValue()->getAsUnquotedString();
1085c87daf2SSebastian Pop 
1095c87daf2SSebastian Pop     // List of fields/attributes that need to be same across all the
1105c87daf2SSebastian Pop     // instructions in a row of the relation table.
1115c87daf2SSebastian Pop     RowFields = MapRec->getValueAsListInit("RowFields");
1125c87daf2SSebastian Pop 
1135c87daf2SSebastian Pop     // List of fields/attributes that are constant across all the instruction
1145c87daf2SSebastian Pop     // in a column of the relation table. Ex: ColFields = 'predSense'
1155c87daf2SSebastian Pop     ColFields = MapRec->getValueAsListInit("ColFields");
1165c87daf2SSebastian Pop 
1175c87daf2SSebastian Pop     // Values for the fields/attributes listed in 'ColFields'.
1185c87daf2SSebastian Pop     // Ex: KeyCol = 'noPred' -- key instruction is non predicated
1195c87daf2SSebastian Pop     KeyCol = MapRec->getValueAsListInit("KeyCol");
1205c87daf2SSebastian Pop 
1215c87daf2SSebastian Pop     // List of values for the fields/attributes listed in 'ColFields', one for
1225c87daf2SSebastian Pop     // each column in the relation table.
1235c87daf2SSebastian Pop     //
1245c87daf2SSebastian Pop     // Ex: ValueCols = [['true'],['false']] -- it results two columns in the
1255c87daf2SSebastian Pop     // table. First column requires all the instructions to have predSense
1265c87daf2SSebastian Pop     // set to 'true' and second column requires it to be 'false'.
1275c87daf2SSebastian Pop     ListInit *ColValList = MapRec->getValueAsListInit("ValueCols");
1285c87daf2SSebastian Pop 
1295c87daf2SSebastian Pop     // Each instruction map must specify at least one column for it to be valid.
1305c87daf2SSebastian Pop     if (ColValList->getSize() == 0)
1315c87daf2SSebastian Pop       throw "InstrMapping record `" + MapRec->getName() + "' has empty " +
1325c87daf2SSebastian Pop             "`ValueCols' field!";
1335c87daf2SSebastian Pop 
1345c87daf2SSebastian Pop     for (unsigned i = 0, e = ColValList->getSize(); i < e; i++) {
1355c87daf2SSebastian Pop       ListInit *ColI = dyn_cast<ListInit>(ColValList->getElement(i));
1365c87daf2SSebastian Pop 
1375c87daf2SSebastian Pop       // Make sure that all the sub-lists in 'ValueCols' have same number of
1385c87daf2SSebastian Pop       // elements as the fields in 'ColFields'.
1395c87daf2SSebastian Pop       if (ColI->getSize() == ColFields->getSize())
1405c87daf2SSebastian Pop         ValueCols.push_back(ColI);
1415c87daf2SSebastian Pop       else {
1425c87daf2SSebastian Pop         throw "Record `" + MapRec->getName() + "', field `" + "ValueCols" +
1435c87daf2SSebastian Pop             "' entries don't match with the entries in 'ColFields'!";
1445c87daf2SSebastian Pop       }
1455c87daf2SSebastian Pop     }
1465c87daf2SSebastian Pop   }
1475c87daf2SSebastian Pop 
1485c87daf2SSebastian Pop   std::string getName() const {
1495c87daf2SSebastian Pop     return Name;
1505c87daf2SSebastian Pop   }
1515c87daf2SSebastian Pop 
1525c87daf2SSebastian Pop   std::string getFilterClass() {
1535c87daf2SSebastian Pop     return FilterClass;
1545c87daf2SSebastian Pop   }
1555c87daf2SSebastian Pop 
1565c87daf2SSebastian Pop   ListInit *getRowFields() const {
1575c87daf2SSebastian Pop     return RowFields;
1585c87daf2SSebastian Pop   }
1595c87daf2SSebastian Pop 
1605c87daf2SSebastian Pop   ListInit *getColFields() const {
1615c87daf2SSebastian Pop     return ColFields;
1625c87daf2SSebastian Pop   }
1635c87daf2SSebastian Pop 
1645c87daf2SSebastian Pop   ListInit *getKeyCol() const {
1655c87daf2SSebastian Pop     return KeyCol;
1665c87daf2SSebastian Pop   }
1675c87daf2SSebastian Pop 
1685c87daf2SSebastian Pop   const std::vector<ListInit*> &getValueCols() const {
1695c87daf2SSebastian Pop     return ValueCols;
1705c87daf2SSebastian Pop   }
1715c87daf2SSebastian Pop };
1725c87daf2SSebastian Pop } // End anonymous namespace.
1735c87daf2SSebastian Pop 
1745c87daf2SSebastian Pop 
1755c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
1765c87daf2SSebastian Pop // class MapTableEmitter : It builds the instruction relation maps using
1775c87daf2SSebastian Pop // the information provided in InstrMapping records. It outputs these
1785c87daf2SSebastian Pop // relationship maps as tables into XXXGenInstrInfo.inc file along with the
1795c87daf2SSebastian Pop // functions to query them.
1805c87daf2SSebastian Pop 
1815c87daf2SSebastian Pop namespace {
1825c87daf2SSebastian Pop class MapTableEmitter {
1835c87daf2SSebastian Pop private:
1845c87daf2SSebastian Pop //  std::string TargetName;
1855c87daf2SSebastian Pop   const CodeGenTarget &Target;
1865c87daf2SSebastian Pop   // InstrMapDesc - InstrMapping record to be processed.
1875c87daf2SSebastian Pop   InstrMap InstrMapDesc;
1885c87daf2SSebastian Pop 
1895c87daf2SSebastian Pop   // InstrDefs - list of instructions filtered using FilterClass defined
1905c87daf2SSebastian Pop   // in InstrMapDesc.
1915c87daf2SSebastian Pop   std::vector<Record*> InstrDefs;
1925c87daf2SSebastian Pop 
1935c87daf2SSebastian Pop   // RowInstrMap - maps RowFields values to the instructions. It's keyed by the
1945c87daf2SSebastian Pop   // values of the row fields and contains vector of records as values.
1955c87daf2SSebastian Pop   RowInstrMapTy RowInstrMap;
1965c87daf2SSebastian Pop 
1975c87daf2SSebastian Pop   // KeyInstrVec - list of key instructions.
1985c87daf2SSebastian Pop   std::vector<Record*> KeyInstrVec;
1995c87daf2SSebastian Pop   DenseMap<Record*, std::vector<Record*> > MapTable;
2005c87daf2SSebastian Pop 
2015c87daf2SSebastian Pop public:
2025c87daf2SSebastian Pop   MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
203*dcbd1608SDavid Blaikie                   Target(Target), InstrMapDesc(IMRec) {
2045c87daf2SSebastian Pop     const std::string FilterClass = InstrMapDesc.getFilterClass();
2055c87daf2SSebastian Pop     InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
206*dcbd1608SDavid Blaikie   }
2075c87daf2SSebastian Pop 
2085c87daf2SSebastian Pop   void buildRowInstrMap();
2095c87daf2SSebastian Pop 
2105c87daf2SSebastian Pop   // Returns true if an instruction is a key instruction, i.e., its ColFields
2115c87daf2SSebastian Pop   // have same values as KeyCol.
2125c87daf2SSebastian Pop   bool isKeyColInstr(Record* CurInstr);
2135c87daf2SSebastian Pop 
2145c87daf2SSebastian Pop   // Find column instruction corresponding to a key instruction based on the
2155c87daf2SSebastian Pop   // constraints for that column.
2165c87daf2SSebastian Pop   Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
2175c87daf2SSebastian Pop 
2185c87daf2SSebastian Pop   // Find column instructions for each key instruction based
2195c87daf2SSebastian Pop   // on ValueCols and store them into MapTable.
2205c87daf2SSebastian Pop   void buildMapTable();
2215c87daf2SSebastian Pop 
2225c87daf2SSebastian Pop   void emitBinSearch(raw_ostream &OS, unsigned TableSize);
2235c87daf2SSebastian Pop   void emitTablesWithFunc(raw_ostream &OS);
2245c87daf2SSebastian Pop   unsigned emitBinSearchTable(raw_ostream &OS);
2255c87daf2SSebastian Pop 
2265c87daf2SSebastian Pop   // Lookup functions to query binary search tables.
2275c87daf2SSebastian Pop   void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
2285c87daf2SSebastian Pop 
2295c87daf2SSebastian Pop };
2305c87daf2SSebastian Pop } // End anonymous namespace.
2315c87daf2SSebastian Pop 
2325c87daf2SSebastian Pop 
2335c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2345c87daf2SSebastian Pop // Process all the instructions that model this relation (alreday present in
2355c87daf2SSebastian Pop // InstrDefs) and insert them into RowInstrMap which is keyed by the values of
2365c87daf2SSebastian Pop // the fields listed as RowFields. It stores vectors of records as values.
2375c87daf2SSebastian Pop // All the related instructions have the same values for the RowFields thus are
2385c87daf2SSebastian Pop // part of the same key-value pair.
2395c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2405c87daf2SSebastian Pop 
2415c87daf2SSebastian Pop void MapTableEmitter::buildRowInstrMap() {
2425c87daf2SSebastian Pop   for (unsigned i = 0, e = InstrDefs.size(); i < e; i++) {
2435c87daf2SSebastian Pop     std::vector<Record*> InstrList;
2445c87daf2SSebastian Pop     Record *CurInstr = InstrDefs[i];
2455c87daf2SSebastian Pop     std::vector<Init*> KeyValue;
2465c87daf2SSebastian Pop     ListInit *RowFields = InstrMapDesc.getRowFields();
2475c87daf2SSebastian Pop     for (unsigned j = 0, endRF = RowFields->getSize(); j < endRF; j++) {
2485c87daf2SSebastian Pop       Init *RowFieldsJ = RowFields->getElement(j);
2495c87daf2SSebastian Pop       Init *CurInstrVal = CurInstr->getValue(RowFieldsJ)->getValue();
2505c87daf2SSebastian Pop       KeyValue.push_back(CurInstrVal);
2515c87daf2SSebastian Pop     }
2525c87daf2SSebastian Pop 
2535c87daf2SSebastian Pop     // Collect key instructions into KeyInstrVec. Later, these instructions are
2545c87daf2SSebastian Pop     // processed to assign column position to the instructions sharing
2555c87daf2SSebastian Pop     // their KeyValue in RowInstrMap.
2565c87daf2SSebastian Pop     if (isKeyColInstr(CurInstr))
2575c87daf2SSebastian Pop       KeyInstrVec.push_back(CurInstr);
2585c87daf2SSebastian Pop 
2595c87daf2SSebastian Pop     RowInstrMap[KeyValue].push_back(CurInstr);
2605c87daf2SSebastian Pop   }
2615c87daf2SSebastian Pop }
2625c87daf2SSebastian Pop 
2635c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2645c87daf2SSebastian Pop // Return true if an instruction is a KeyCol instruction.
2655c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2665c87daf2SSebastian Pop 
2675c87daf2SSebastian Pop bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
2685c87daf2SSebastian Pop   ListInit *ColFields = InstrMapDesc.getColFields();
2695c87daf2SSebastian Pop   ListInit *KeyCol = InstrMapDesc.getKeyCol();
2705c87daf2SSebastian Pop 
2715c87daf2SSebastian Pop   // Check if the instruction is a KeyCol instruction.
2725c87daf2SSebastian Pop   bool MatchFound = true;
2735c87daf2SSebastian Pop   for (unsigned j = 0, endCF = ColFields->getSize();
2745c87daf2SSebastian Pop       (j < endCF) && MatchFound; j++) {
2755c87daf2SSebastian Pop     RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
2765c87daf2SSebastian Pop     std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
2775c87daf2SSebastian Pop     std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
2785c87daf2SSebastian Pop     MatchFound = (CurInstrVal == KeyColValue);
2795c87daf2SSebastian Pop   }
2805c87daf2SSebastian Pop   return MatchFound;
2815c87daf2SSebastian Pop }
2825c87daf2SSebastian Pop 
2835c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2845c87daf2SSebastian Pop // Build a map to link key instructions with the column instructions arranged
2855c87daf2SSebastian Pop // according to their column positions.
2865c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2875c87daf2SSebastian Pop 
2885c87daf2SSebastian Pop void MapTableEmitter::buildMapTable() {
2895c87daf2SSebastian Pop   // Find column instructions for a given key based on the ColField
2905c87daf2SSebastian Pop   // constraints.
2915c87daf2SSebastian Pop   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
2925c87daf2SSebastian Pop   unsigned NumOfCols = ValueCols.size();
2935c87daf2SSebastian Pop   for (unsigned j = 0, endKI = KeyInstrVec.size(); j < endKI; j++) {
2945c87daf2SSebastian Pop     Record *CurKeyInstr = KeyInstrVec[j];
2955c87daf2SSebastian Pop     std::vector<Record*> ColInstrVec(NumOfCols);
2965c87daf2SSebastian Pop 
2975c87daf2SSebastian Pop     // Find the column instruction based on the constraints for the column.
2985c87daf2SSebastian Pop     for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
2995c87daf2SSebastian Pop       ListInit *CurValueCol = ValueCols[ColIdx];
3005c87daf2SSebastian Pop       Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
3015c87daf2SSebastian Pop       ColInstrVec[ColIdx] = ColInstr;
3025c87daf2SSebastian Pop     }
3035c87daf2SSebastian Pop     MapTable[CurKeyInstr] = ColInstrVec;
3045c87daf2SSebastian Pop   }
3055c87daf2SSebastian Pop }
3065c87daf2SSebastian Pop 
3075c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3085c87daf2SSebastian Pop // Find column instruction based on the constraints for that column.
3095c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3105c87daf2SSebastian Pop 
3115c87daf2SSebastian Pop Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
3125c87daf2SSebastian Pop                                            ListInit *CurValueCol) {
3135c87daf2SSebastian Pop   ListInit *RowFields = InstrMapDesc.getRowFields();
3145c87daf2SSebastian Pop   std::vector<Init*> KeyValue;
3155c87daf2SSebastian Pop 
3165c87daf2SSebastian Pop   // Construct KeyValue using KeyInstr's values for RowFields.
3175c87daf2SSebastian Pop   for (unsigned j = 0, endRF = RowFields->getSize(); j < endRF; j++) {
3185c87daf2SSebastian Pop     Init *RowFieldsJ = RowFields->getElement(j);
3195c87daf2SSebastian Pop     Init *KeyInstrVal = KeyInstr->getValue(RowFieldsJ)->getValue();
3205c87daf2SSebastian Pop     KeyValue.push_back(KeyInstrVal);
3215c87daf2SSebastian Pop   }
3225c87daf2SSebastian Pop 
3235c87daf2SSebastian Pop   // Get all the instructions that share the same KeyValue as the KeyInstr
3245c87daf2SSebastian Pop   // in RowInstrMap. We search through these instructions to find a match
3255c87daf2SSebastian Pop   // for the current column, i.e., the instruction which has the same values
3265c87daf2SSebastian Pop   // as CurValueCol for all the fields in ColFields.
3275c87daf2SSebastian Pop   const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
3285c87daf2SSebastian Pop 
3295c87daf2SSebastian Pop   ListInit *ColFields = InstrMapDesc.getColFields();
3305c87daf2SSebastian Pop   Record *MatchInstr = NULL;
3315c87daf2SSebastian Pop 
3325c87daf2SSebastian Pop   for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) {
3335c87daf2SSebastian Pop     bool MatchFound = true;
3345c87daf2SSebastian Pop     Record *CurInstr = RelatedInstrVec[i];
3355c87daf2SSebastian Pop     for (unsigned j = 0, endCF = ColFields->getSize();
3365c87daf2SSebastian Pop         (j < endCF) && MatchFound; j++) {
3375c87daf2SSebastian Pop       Init *ColFieldJ = ColFields->getElement(j);
3385c87daf2SSebastian Pop       Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
3395c87daf2SSebastian Pop       std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
3405c87daf2SSebastian Pop       Init *ColFieldJVallue = CurValueCol->getElement(j);
3415c87daf2SSebastian Pop       MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
3425c87daf2SSebastian Pop     }
3435c87daf2SSebastian Pop 
3445c87daf2SSebastian Pop     if (MatchFound) {
3455c87daf2SSebastian Pop       if (MatchInstr) // Already had a match
3465c87daf2SSebastian Pop         // Error if multiple matches are found for a column.
3475c87daf2SSebastian Pop         throw "Multiple matches found for `" + KeyInstr->getName() +
3485c87daf2SSebastian Pop               "', for the relation `" + InstrMapDesc.getName();
3495c87daf2SSebastian Pop       else
3505c87daf2SSebastian Pop         MatchInstr = CurInstr;
3515c87daf2SSebastian Pop     }
3525c87daf2SSebastian Pop   }
3535c87daf2SSebastian Pop   return MatchInstr;
3545c87daf2SSebastian Pop }
3555c87daf2SSebastian Pop 
3565c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3575c87daf2SSebastian Pop // Emit one table per relation. Only instructions with a valid relation of a
3585c87daf2SSebastian Pop // given type are included in the table sorted by their enum values (opcodes).
3595c87daf2SSebastian Pop // Binary search is used for locating instructions in the table.
3605c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3615c87daf2SSebastian Pop 
3625c87daf2SSebastian Pop unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
3635c87daf2SSebastian Pop 
3645c87daf2SSebastian Pop   const std::vector<const CodeGenInstruction*> &NumberedInstructions =
3655c87daf2SSebastian Pop                                             Target.getInstructionsByEnumValue();
3665c87daf2SSebastian Pop   std::string TargetName = Target.getName();
3675c87daf2SSebastian Pop   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
3685c87daf2SSebastian Pop   unsigned NumCol = ValueCols.size();
3695c87daf2SSebastian Pop   unsigned TotalNumInstr = NumberedInstructions.size();
3705c87daf2SSebastian Pop   unsigned TableSize = 0;
3715c87daf2SSebastian Pop 
3725c87daf2SSebastian Pop   OS << "static const uint16_t "<<InstrMapDesc.getName();
3735c87daf2SSebastian Pop   // Number of columns in the table are NumCol+1 because key instructions are
3745c87daf2SSebastian Pop   // emitted as first column.
3755c87daf2SSebastian Pop   OS << "Table[]["<< NumCol+1 << "] = {\n";
3765c87daf2SSebastian Pop   for (unsigned i = 0; i < TotalNumInstr; i++) {
3775c87daf2SSebastian Pop     Record *CurInstr = NumberedInstructions[i]->TheDef;
3785c87daf2SSebastian Pop     std::vector<Record*> ColInstrs = MapTable[CurInstr];
3795c87daf2SSebastian Pop     std::string OutStr("");
3805c87daf2SSebastian Pop     unsigned RelExists = 0;
3815c87daf2SSebastian Pop     if (ColInstrs.size()) {
3825c87daf2SSebastian Pop       for (unsigned j = 0; j < NumCol; j++) {
3835c87daf2SSebastian Pop         if (ColInstrs[j] != NULL) {
3845c87daf2SSebastian Pop           RelExists = 1;
3855c87daf2SSebastian Pop           OutStr += ", ";
3865c87daf2SSebastian Pop           OutStr += TargetName;
3875c87daf2SSebastian Pop           OutStr += "::";
3885c87daf2SSebastian Pop           OutStr += ColInstrs[j]->getName();
3895c87daf2SSebastian Pop         } else { OutStr += ", -1";}
3905c87daf2SSebastian Pop       }
3915c87daf2SSebastian Pop 
3925c87daf2SSebastian Pop       if (RelExists) {
3935c87daf2SSebastian Pop         OS << "  { " << TargetName << "::" << CurInstr->getName();
3945c87daf2SSebastian Pop         OS << OutStr <<" },\n";
3955c87daf2SSebastian Pop         TableSize++;
3965c87daf2SSebastian Pop       }
3975c87daf2SSebastian Pop     }
3985c87daf2SSebastian Pop   }
3995c87daf2SSebastian Pop   if (!TableSize) {
4005c87daf2SSebastian Pop     OS << "  { " << TargetName << "::" << "INSTRUCTION_LIST_END, ";
4015c87daf2SSebastian Pop     OS << TargetName << "::" << "INSTRUCTION_LIST_END }";
4025c87daf2SSebastian Pop   }
4035c87daf2SSebastian Pop   OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
4045c87daf2SSebastian Pop   return TableSize;
4055c87daf2SSebastian Pop }
4065c87daf2SSebastian Pop 
4075c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4085c87daf2SSebastian Pop // Emit binary search algorithm as part of the functions used to query
4095c87daf2SSebastian Pop // relation tables.
4105c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4115c87daf2SSebastian Pop 
4125c87daf2SSebastian Pop void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
4135c87daf2SSebastian Pop   OS << "  unsigned mid;\n";
4145c87daf2SSebastian Pop   OS << "  unsigned start = 0;\n";
4155c87daf2SSebastian Pop   OS << "  unsigned end = " << TableSize << ";\n";
4165c87daf2SSebastian Pop   OS << "  while (start < end) {\n";
4175c87daf2SSebastian Pop   OS << "    mid = start + (end - start)/2;\n";
4185c87daf2SSebastian Pop   OS << "    if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
4195c87daf2SSebastian Pop   OS << "      break;\n";
4205c87daf2SSebastian Pop   OS << "    }\n";
4215c87daf2SSebastian Pop   OS << "    if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
4225c87daf2SSebastian Pop   OS << "      end = mid;\n";
4235c87daf2SSebastian Pop   OS << "    else\n";
4245c87daf2SSebastian Pop   OS << "      start = mid + 1;\n";
4255c87daf2SSebastian Pop   OS << "  }\n";
4265c87daf2SSebastian Pop   OS << "  if (start == end)\n";
4275c87daf2SSebastian Pop   OS << "    return -1; // Instruction doesn't exist in this table.\n\n";
4285c87daf2SSebastian Pop }
4295c87daf2SSebastian Pop 
4305c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4315c87daf2SSebastian Pop // Emit functions to query relation tables.
4325c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4335c87daf2SSebastian Pop 
4345c87daf2SSebastian Pop void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
4355c87daf2SSebastian Pop                                            unsigned TableSize) {
4365c87daf2SSebastian Pop 
4375c87daf2SSebastian Pop   ListInit *ColFields = InstrMapDesc.getColFields();
4385c87daf2SSebastian Pop   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
4395c87daf2SSebastian Pop 
4405c87daf2SSebastian Pop   // Emit binary search algorithm to locate instructions in the
4415c87daf2SSebastian Pop   // relation table. If found, return opcode value from the appropriate column
4425c87daf2SSebastian Pop   // of the table.
4435c87daf2SSebastian Pop   emitBinSearch(OS, TableSize);
4445c87daf2SSebastian Pop 
4455c87daf2SSebastian Pop   if (ValueCols.size() > 1) {
4465c87daf2SSebastian Pop     for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
4475c87daf2SSebastian Pop       ListInit *ColumnI = ValueCols[i];
4485c87daf2SSebastian Pop       for (unsigned j = 0, ColSize = ColumnI->getSize(); j < ColSize; j++) {
4495c87daf2SSebastian Pop         std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
4505c87daf2SSebastian Pop         OS << "  if (in" << ColName;
4515c87daf2SSebastian Pop         OS << " == ";
4525c87daf2SSebastian Pop         OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
4535c87daf2SSebastian Pop         if (j < ColumnI->getSize() - 1) OS << " && ";
4545c87daf2SSebastian Pop         else OS << ")\n";
4555c87daf2SSebastian Pop       }
4565c87daf2SSebastian Pop       OS << "    return " << InstrMapDesc.getName();
4575c87daf2SSebastian Pop       OS << "Table[mid]["<<i+1<<"];\n";
4585c87daf2SSebastian Pop     }
4595c87daf2SSebastian Pop     OS << "  return -1;";
4605c87daf2SSebastian Pop   }
4615c87daf2SSebastian Pop   else
4625c87daf2SSebastian Pop     OS << "  return " << InstrMapDesc.getName() << "Table[mid][1];\n";
4635c87daf2SSebastian Pop 
4645c87daf2SSebastian Pop   OS <<"}\n\n";
4655c87daf2SSebastian Pop }
4665c87daf2SSebastian Pop 
4675c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4685c87daf2SSebastian Pop // Emit relation tables and the functions to query them.
4695c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4705c87daf2SSebastian Pop 
4715c87daf2SSebastian Pop void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
4725c87daf2SSebastian Pop 
4735c87daf2SSebastian Pop   // Emit function name and the input parameters : mostly opcode value of the
4745c87daf2SSebastian Pop   // current instruction. However, if a table has multiple columns (more than 2
4755c87daf2SSebastian Pop   // since first column is used for the key instructions), then we also need
4765c87daf2SSebastian Pop   // to pass another input to indicate the column to be selected.
4775c87daf2SSebastian Pop 
4785c87daf2SSebastian Pop   ListInit *ColFields = InstrMapDesc.getColFields();
4795c87daf2SSebastian Pop   const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
4805c87daf2SSebastian Pop   OS << "// "<< InstrMapDesc.getName() << "\n";
4815c87daf2SSebastian Pop   OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
4825c87daf2SSebastian Pop   if (ValueCols.size() > 1) {
4835c87daf2SSebastian Pop     for (unsigned i = 0, e = ColFields->getSize(); i < e; i++) {
4845c87daf2SSebastian Pop       std::string ColName = ColFields->getElement(i)->getAsUnquotedString();
4855c87daf2SSebastian Pop       OS << ", enum " << ColName << " in" << ColName << ") {\n";
4865c87daf2SSebastian Pop     }
4875c87daf2SSebastian Pop   } else { OS << ") {\n"; }
4885c87daf2SSebastian Pop 
4895c87daf2SSebastian Pop   // Emit map table.
4905c87daf2SSebastian Pop   unsigned TableSize = emitBinSearchTable(OS);
4915c87daf2SSebastian Pop 
4925c87daf2SSebastian Pop   // Emit rest of the function body.
4935c87daf2SSebastian Pop   emitMapFuncBody(OS, TableSize);
4945c87daf2SSebastian Pop }
4955c87daf2SSebastian Pop 
4965c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4975c87daf2SSebastian Pop // Emit enums for the column fields across all the instruction maps.
4985c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4995c87daf2SSebastian Pop 
5005c87daf2SSebastian Pop static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
5015c87daf2SSebastian Pop 
5025c87daf2SSebastian Pop   std::vector<Record*> InstrMapVec;
5035c87daf2SSebastian Pop   InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
5045c87daf2SSebastian Pop   std::map<std::string, std::vector<Init*> > ColFieldValueMap;
5055c87daf2SSebastian Pop 
5065c87daf2SSebastian Pop   // Iterate over all InstrMapping records and create a map between column
5075c87daf2SSebastian Pop   // fields and their possible values across all records.
5085c87daf2SSebastian Pop   for (unsigned i = 0, e = InstrMapVec.size(); i < e; i++) {
5095c87daf2SSebastian Pop     Record *CurMap = InstrMapVec[i];
5105c87daf2SSebastian Pop     ListInit *ColFields;
5115c87daf2SSebastian Pop     ColFields = CurMap->getValueAsListInit("ColFields");
5125c87daf2SSebastian Pop     ListInit *List = CurMap->getValueAsListInit("ValueCols");
5135c87daf2SSebastian Pop     std::vector<ListInit*> ValueCols;
5145c87daf2SSebastian Pop     unsigned ListSize = List->getSize();
5155c87daf2SSebastian Pop 
5165c87daf2SSebastian Pop     for (unsigned j = 0; j < ListSize; j++) {
5175c87daf2SSebastian Pop       ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j));
5185c87daf2SSebastian Pop 
5195c87daf2SSebastian Pop       if (ListJ->getSize() != ColFields->getSize()) {
5205c87daf2SSebastian Pop         throw "Record `" + CurMap->getName() + "', field `" + "ValueCols" +
5215c87daf2SSebastian Pop             "' entries don't match with the entries in 'ColFields' !";
5225c87daf2SSebastian Pop       }
5235c87daf2SSebastian Pop       ValueCols.push_back(ListJ);
5245c87daf2SSebastian Pop     }
5255c87daf2SSebastian Pop 
5265c87daf2SSebastian Pop     for (unsigned j = 0, endCF = ColFields->getSize(); j < endCF; j++) {
5275c87daf2SSebastian Pop       for (unsigned k = 0; k < ListSize; k++){
5285c87daf2SSebastian Pop         std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
5295c87daf2SSebastian Pop         ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
5305c87daf2SSebastian Pop       }
5315c87daf2SSebastian Pop     }
5325c87daf2SSebastian Pop   }
5335c87daf2SSebastian Pop 
5345c87daf2SSebastian Pop   for (std::map<std::string, std::vector<Init*> >::iterator
5355c87daf2SSebastian Pop        II = ColFieldValueMap.begin(), IE = ColFieldValueMap.end();
5365c87daf2SSebastian Pop        II != IE; II++) {
5375c87daf2SSebastian Pop     std::vector<Init*> FieldValues = (*II).second;
5385c87daf2SSebastian Pop     unsigned FieldSize = FieldValues.size();
5395c87daf2SSebastian Pop 
5405c87daf2SSebastian Pop     // Delete duplicate entries from ColFieldValueMap
5415c87daf2SSebastian Pop     for (unsigned i = 0; i < FieldSize - 1; i++) {
5425c87daf2SSebastian Pop       Init *CurVal = FieldValues[i];
5435c87daf2SSebastian Pop       for (unsigned j = i+1; j < FieldSize; j++) {
5445c87daf2SSebastian Pop         if (CurVal == FieldValues[j]) {
5455c87daf2SSebastian Pop           FieldValues.erase(FieldValues.begin()+j);
5465c87daf2SSebastian Pop         }
5475c87daf2SSebastian Pop       }
5485c87daf2SSebastian Pop     }
5495c87daf2SSebastian Pop 
5505c87daf2SSebastian Pop     // Emit enumerated values for the column fields.
5515c87daf2SSebastian Pop     OS << "enum " << (*II).first << " {\n";
5525c87daf2SSebastian Pop     for (unsigned i = 0; i < FieldSize; i++) {
5535c87daf2SSebastian Pop       OS << "\t" << (*II).first << "_" << FieldValues[i]->getAsUnquotedString();
5545c87daf2SSebastian Pop       if (i != FieldValues.size() - 1)
5555c87daf2SSebastian Pop         OS << ",\n";
5565c87daf2SSebastian Pop       else
5575c87daf2SSebastian Pop         OS << "\n};\n\n";
5585c87daf2SSebastian Pop     }
5595c87daf2SSebastian Pop   }
5605c87daf2SSebastian Pop }
5615c87daf2SSebastian Pop 
5625c87daf2SSebastian Pop namespace llvm {
5635c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
5645c87daf2SSebastian Pop // Parse 'InstrMapping' records and use the information to form relationship
5655c87daf2SSebastian Pop // between instructions. These relations are emitted as a tables along with the
5665c87daf2SSebastian Pop // functions to query them.
5675c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
5685c87daf2SSebastian Pop void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
5695c87daf2SSebastian Pop   CodeGenTarget Target(Records);
5705c87daf2SSebastian Pop   std::string TargetName = Target.getName();
5715c87daf2SSebastian Pop   std::vector<Record*> InstrMapVec;
5725c87daf2SSebastian Pop   InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
5735c87daf2SSebastian Pop 
5745c87daf2SSebastian Pop   if (!InstrMapVec.size())
5755c87daf2SSebastian Pop     return;
5765c87daf2SSebastian Pop 
5775c87daf2SSebastian Pop   OS << "#ifdef GET_INSTRMAP_INFO\n";
5785c87daf2SSebastian Pop   OS << "#undef GET_INSTRMAP_INFO\n";
5795c87daf2SSebastian Pop   OS << "namespace llvm {\n\n";
5805c87daf2SSebastian Pop   OS << "namespace " << TargetName << " {\n\n";
5815c87daf2SSebastian Pop 
5825c87daf2SSebastian Pop   // Emit coulumn field names and their values as enums.
5835c87daf2SSebastian Pop   emitEnums(OS, Records);
5845c87daf2SSebastian Pop 
5855c87daf2SSebastian Pop   // Iterate over all instruction mapping records and construct relationship
5865c87daf2SSebastian Pop   // maps based on the information specified there.
5875c87daf2SSebastian Pop   //
5885c87daf2SSebastian Pop   for (unsigned i = 0, e = InstrMapVec.size(); i < e; i++) {
5895c87daf2SSebastian Pop     MapTableEmitter IMap(Target, Records, InstrMapVec[i]);
5905c87daf2SSebastian Pop 
5915c87daf2SSebastian Pop     // Build RowInstrMap to group instructions based on their values for
5925c87daf2SSebastian Pop     // RowFields. In the process, also collect key instructions into
5935c87daf2SSebastian Pop     // KeyInstrVec.
5945c87daf2SSebastian Pop     IMap.buildRowInstrMap();
5955c87daf2SSebastian Pop 
5965c87daf2SSebastian Pop     // Build MapTable to map key instructions with the corresponding column
5975c87daf2SSebastian Pop     // instructions.
5985c87daf2SSebastian Pop     IMap.buildMapTable();
5995c87daf2SSebastian Pop 
6005c87daf2SSebastian Pop     // Emit map tables and the functions to query them.
6015c87daf2SSebastian Pop     IMap.emitTablesWithFunc(OS);
6025c87daf2SSebastian Pop   }
6035c87daf2SSebastian Pop   OS << "} // End " << TargetName << " namespace\n";
6045c87daf2SSebastian Pop   OS << "} // End llvm namespace\n";
6055c87daf2SSebastian Pop   OS << "#endif // GET_INSTRMAP_INFO\n\n";
6065c87daf2SSebastian Pop }
6075c87daf2SSebastian Pop 
6085c87daf2SSebastian Pop } // End llvm namespace
609