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" 81635debe8SJoerg Sonnenberger #include "llvm/TableGen/Error.h" 825c87daf2SSebastian Pop using namespace llvm; 835c87daf2SSebastian Pop typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy; 845c87daf2SSebastian Pop 855c87daf2SSebastian Pop typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy; 865c87daf2SSebastian Pop 875c87daf2SSebastian Pop namespace { 885c87daf2SSebastian Pop 895c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 905c87daf2SSebastian Pop // This class is used to represent InstrMapping class defined in Target.td file. 915c87daf2SSebastian Pop class InstrMap { 925c87daf2SSebastian Pop private: 935c87daf2SSebastian Pop std::string Name; 945c87daf2SSebastian Pop std::string FilterClass; 955c87daf2SSebastian Pop ListInit *RowFields; 965c87daf2SSebastian Pop ListInit *ColFields; 975c87daf2SSebastian Pop ListInit *KeyCol; 985c87daf2SSebastian Pop std::vector<ListInit*> ValueCols; 995c87daf2SSebastian Pop 1005c87daf2SSebastian Pop public: 1015c87daf2SSebastian Pop InstrMap(Record* MapRec) { 1025c87daf2SSebastian Pop Name = MapRec->getName(); 1035c87daf2SSebastian Pop 1045c87daf2SSebastian Pop // FilterClass - It's used to reduce the search space only to the 1055c87daf2SSebastian Pop // instructions that define the kind of relationship modeled by 1065c87daf2SSebastian Pop // this InstrMapping object/record. 1075c87daf2SSebastian Pop const RecordVal *Filter = MapRec->getValue("FilterClass"); 1085c87daf2SSebastian Pop FilterClass = Filter->getValue()->getAsUnquotedString(); 1095c87daf2SSebastian Pop 1105c87daf2SSebastian Pop // List of fields/attributes that need to be same across all the 1115c87daf2SSebastian Pop // instructions in a row of the relation table. 1125c87daf2SSebastian Pop RowFields = MapRec->getValueAsListInit("RowFields"); 1135c87daf2SSebastian Pop 1145c87daf2SSebastian Pop // List of fields/attributes that are constant across all the instruction 1155c87daf2SSebastian Pop // in a column of the relation table. Ex: ColFields = 'predSense' 1165c87daf2SSebastian Pop ColFields = MapRec->getValueAsListInit("ColFields"); 1175c87daf2SSebastian Pop 1185c87daf2SSebastian Pop // Values for the fields/attributes listed in 'ColFields'. 119f907b891SAlp Toker // Ex: KeyCol = 'noPred' -- key instruction is non-predicated 1205c87daf2SSebastian Pop KeyCol = MapRec->getValueAsListInit("KeyCol"); 1215c87daf2SSebastian Pop 1225c87daf2SSebastian Pop // List of values for the fields/attributes listed in 'ColFields', one for 1235c87daf2SSebastian Pop // each column in the relation table. 1245c87daf2SSebastian Pop // 1255c87daf2SSebastian Pop // Ex: ValueCols = [['true'],['false']] -- it results two columns in the 1265c87daf2SSebastian Pop // table. First column requires all the instructions to have predSense 1275c87daf2SSebastian Pop // set to 'true' and second column requires it to be 'false'. 1285c87daf2SSebastian Pop ListInit *ColValList = MapRec->getValueAsListInit("ValueCols"); 1295c87daf2SSebastian Pop 1305c87daf2SSebastian Pop // Each instruction map must specify at least one column for it to be valid. 131ec9072d6SCraig Topper if (ColValList->empty()) 132635debe8SJoerg Sonnenberger PrintFatalError(MapRec->getLoc(), "InstrMapping record `" + 133635debe8SJoerg Sonnenberger MapRec->getName() + "' has empty " + "`ValueCols' field!"); 1345c87daf2SSebastian Pop 135ef0578a8SCraig Topper for (Init *I : ColValList->getValues()) { 136ef0578a8SCraig Topper ListInit *ColI = dyn_cast<ListInit>(I); 1375c87daf2SSebastian Pop 1385c87daf2SSebastian Pop // Make sure that all the sub-lists in 'ValueCols' have same number of 1395c87daf2SSebastian Pop // elements as the fields in 'ColFields'. 140664f6a04SCraig Topper if (ColI->size() != ColFields->size()) 141635debe8SJoerg Sonnenberger PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() + 142635debe8SJoerg Sonnenberger "', field `ValueCols' entries don't match with " + 143635debe8SJoerg Sonnenberger " the entries in 'ColFields'!"); 1445c87daf2SSebastian Pop ValueCols.push_back(ColI); 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): 203dcbd1608SDavid Blaikie Target(Target), InstrMapDesc(IMRec) { 2045c87daf2SSebastian Pop const std::string FilterClass = InstrMapDesc.getFilterClass(); 2055c87daf2SSebastian Pop InstrDefs = Records.getAllDerivedDefinitions(FilterClass); 206dcbd1608SDavid 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() { 242ef0578a8SCraig Topper for (Record *CurInstr : InstrDefs) { 2435c87daf2SSebastian Pop std::vector<Init*> KeyValue; 2445c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields(); 245ef0578a8SCraig Topper for (Init *RowField : RowFields->getValues()) { 246ef0578a8SCraig Topper Init *CurInstrVal = CurInstr->getValue(RowField)->getValue(); 2475c87daf2SSebastian Pop KeyValue.push_back(CurInstrVal); 2485c87daf2SSebastian Pop } 2495c87daf2SSebastian Pop 2505c87daf2SSebastian Pop // Collect key instructions into KeyInstrVec. Later, these instructions are 2515c87daf2SSebastian Pop // processed to assign column position to the instructions sharing 2525c87daf2SSebastian Pop // their KeyValue in RowInstrMap. 2535c87daf2SSebastian Pop if (isKeyColInstr(CurInstr)) 2545c87daf2SSebastian Pop KeyInstrVec.push_back(CurInstr); 2555c87daf2SSebastian Pop 2565c87daf2SSebastian Pop RowInstrMap[KeyValue].push_back(CurInstr); 2575c87daf2SSebastian Pop } 2585c87daf2SSebastian Pop } 2595c87daf2SSebastian Pop 2605c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2615c87daf2SSebastian Pop // Return true if an instruction is a KeyCol instruction. 2625c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2635c87daf2SSebastian Pop 2645c87daf2SSebastian Pop bool MapTableEmitter::isKeyColInstr(Record* CurInstr) { 2655c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 2665c87daf2SSebastian Pop ListInit *KeyCol = InstrMapDesc.getKeyCol(); 2675c87daf2SSebastian Pop 2685c87daf2SSebastian Pop // Check if the instruction is a KeyCol instruction. 2695c87daf2SSebastian Pop bool MatchFound = true; 270664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); 2715c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) { 2725c87daf2SSebastian Pop RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j)); 2735c87daf2SSebastian Pop std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString(); 2745c87daf2SSebastian Pop std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString(); 2755c87daf2SSebastian Pop MatchFound = (CurInstrVal == KeyColValue); 2765c87daf2SSebastian Pop } 2775c87daf2SSebastian Pop return MatchFound; 2785c87daf2SSebastian Pop } 2795c87daf2SSebastian Pop 2805c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2815c87daf2SSebastian Pop // Build a map to link key instructions with the column instructions arranged 2825c87daf2SSebastian Pop // according to their column positions. 2835c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2845c87daf2SSebastian Pop 2855c87daf2SSebastian Pop void MapTableEmitter::buildMapTable() { 2865c87daf2SSebastian Pop // Find column instructions for a given key based on the ColField 2875c87daf2SSebastian Pop // constraints. 2885c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 2895c87daf2SSebastian Pop unsigned NumOfCols = ValueCols.size(); 290ef0578a8SCraig Topper for (Record *CurKeyInstr : KeyInstrVec) { 2915c87daf2SSebastian Pop std::vector<Record*> ColInstrVec(NumOfCols); 2925c87daf2SSebastian Pop 2935c87daf2SSebastian Pop // Find the column instruction based on the constraints for the column. 2945c87daf2SSebastian Pop for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) { 2955c87daf2SSebastian Pop ListInit *CurValueCol = ValueCols[ColIdx]; 2965c87daf2SSebastian Pop Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol); 2975c87daf2SSebastian Pop ColInstrVec[ColIdx] = ColInstr; 2985c87daf2SSebastian Pop } 2995c87daf2SSebastian Pop MapTable[CurKeyInstr] = ColInstrVec; 3005c87daf2SSebastian Pop } 3015c87daf2SSebastian Pop } 3025c87daf2SSebastian Pop 3035c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 3045c87daf2SSebastian Pop // Find column instruction based on the constraints for that column. 3055c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 3065c87daf2SSebastian Pop 3075c87daf2SSebastian Pop Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr, 3085c87daf2SSebastian Pop ListInit *CurValueCol) { 3095c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields(); 3105c87daf2SSebastian Pop std::vector<Init*> KeyValue; 3115c87daf2SSebastian Pop 3125c87daf2SSebastian Pop // Construct KeyValue using KeyInstr's values for RowFields. 313ef0578a8SCraig Topper for (Init *RowField : RowFields->getValues()) { 314ef0578a8SCraig Topper Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue(); 3155c87daf2SSebastian Pop KeyValue.push_back(KeyInstrVal); 3165c87daf2SSebastian Pop } 3175c87daf2SSebastian Pop 3185c87daf2SSebastian Pop // Get all the instructions that share the same KeyValue as the KeyInstr 3195c87daf2SSebastian Pop // in RowInstrMap. We search through these instructions to find a match 3205c87daf2SSebastian Pop // for the current column, i.e., the instruction which has the same values 3215c87daf2SSebastian Pop // as CurValueCol for all the fields in ColFields. 3225c87daf2SSebastian Pop const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue]; 3235c87daf2SSebastian Pop 3245c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 32524064771SCraig Topper Record *MatchInstr = nullptr; 3265c87daf2SSebastian Pop 3275c87daf2SSebastian Pop for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) { 3285c87daf2SSebastian Pop bool MatchFound = true; 3295c87daf2SSebastian Pop Record *CurInstr = RelatedInstrVec[i]; 330664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); 3315c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) { 3325c87daf2SSebastian Pop Init *ColFieldJ = ColFields->getElement(j); 3335c87daf2SSebastian Pop Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue(); 3345c87daf2SSebastian Pop std::string CurInstrVal = CurInstrInit->getAsUnquotedString(); 3355c87daf2SSebastian Pop Init *ColFieldJVallue = CurValueCol->getElement(j); 3365c87daf2SSebastian Pop MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString()); 3375c87daf2SSebastian Pop } 3385c87daf2SSebastian Pop 3395c87daf2SSebastian Pop if (MatchFound) { 340411fbbf5SNicolai Haehnle if (MatchInstr) { 341411fbbf5SNicolai Haehnle // Already had a match 3425c87daf2SSebastian Pop // Error if multiple matches are found for a column. 343411fbbf5SNicolai Haehnle std::string KeyValueStr; 344411fbbf5SNicolai Haehnle for (Init *Value : KeyValue) { 345411fbbf5SNicolai Haehnle if (!KeyValueStr.empty()) 346411fbbf5SNicolai Haehnle KeyValueStr += ", "; 347411fbbf5SNicolai Haehnle KeyValueStr += Value->getAsString(); 348411fbbf5SNicolai Haehnle } 349411fbbf5SNicolai Haehnle 350635debe8SJoerg Sonnenberger PrintFatalError("Multiple matches found for `" + KeyInstr->getName() + 351411fbbf5SNicolai Haehnle "', for the relation `" + InstrMapDesc.getName() + "', row fields [" + 352411fbbf5SNicolai Haehnle KeyValueStr + "], column `" + CurValueCol->getAsString() + "'"); 353411fbbf5SNicolai Haehnle } 3545c87daf2SSebastian Pop MatchInstr = CurInstr; 3555c87daf2SSebastian Pop } 3565c87daf2SSebastian Pop } 3575c87daf2SSebastian Pop return MatchInstr; 3585c87daf2SSebastian Pop } 3595c87daf2SSebastian Pop 3605c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 3615c87daf2SSebastian Pop // Emit one table per relation. Only instructions with a valid relation of a 3625c87daf2SSebastian Pop // given type are included in the table sorted by their enum values (opcodes). 3635c87daf2SSebastian Pop // Binary search is used for locating instructions in the table. 3645c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 3655c87daf2SSebastian Pop 3665c87daf2SSebastian Pop unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) { 3675c87daf2SSebastian Pop 36828851b62SCraig Topper ArrayRef<const CodeGenInstruction*> NumberedInstructions = 3695c87daf2SSebastian Pop Target.getInstructionsByEnumValue(); 370*bb6cf737SKarl-Johan Karlsson std::string Namespace = Target.getInstNamespace(); 3715c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 3725c87daf2SSebastian Pop unsigned NumCol = ValueCols.size(); 3735c87daf2SSebastian Pop unsigned TotalNumInstr = NumberedInstructions.size(); 3745c87daf2SSebastian Pop unsigned TableSize = 0; 3755c87daf2SSebastian Pop 3765c87daf2SSebastian Pop OS << "static const uint16_t "<<InstrMapDesc.getName(); 3775c87daf2SSebastian Pop // Number of columns in the table are NumCol+1 because key instructions are 3785c87daf2SSebastian Pop // emitted as first column. 3795c87daf2SSebastian Pop OS << "Table[]["<< NumCol+1 << "] = {\n"; 3805c87daf2SSebastian Pop for (unsigned i = 0; i < TotalNumInstr; i++) { 3815c87daf2SSebastian Pop Record *CurInstr = NumberedInstructions[i]->TheDef; 3825c87daf2SSebastian Pop std::vector<Record*> ColInstrs = MapTable[CurInstr]; 3835c87daf2SSebastian Pop std::string OutStr(""); 3845c87daf2SSebastian Pop unsigned RelExists = 0; 3858c0809c7SAlexander Kornienko if (!ColInstrs.empty()) { 3865c87daf2SSebastian Pop for (unsigned j = 0; j < NumCol; j++) { 38724064771SCraig Topper if (ColInstrs[j] != nullptr) { 3885c87daf2SSebastian Pop RelExists = 1; 3895c87daf2SSebastian Pop OutStr += ", "; 390*bb6cf737SKarl-Johan Karlsson OutStr += Namespace; 3915c87daf2SSebastian Pop OutStr += "::"; 3925c87daf2SSebastian Pop OutStr += ColInstrs[j]->getName(); 3930a7a17dfSBenjamin Kramer } else { OutStr += ", (uint16_t)-1U";} 3945c87daf2SSebastian Pop } 3955c87daf2SSebastian Pop 3965c87daf2SSebastian Pop if (RelExists) { 397*bb6cf737SKarl-Johan Karlsson OS << " { " << Namespace << "::" << CurInstr->getName(); 3985c87daf2SSebastian Pop OS << OutStr <<" },\n"; 3995c87daf2SSebastian Pop TableSize++; 4005c87daf2SSebastian Pop } 4015c87daf2SSebastian Pop } 4025c87daf2SSebastian Pop } 4035c87daf2SSebastian Pop if (!TableSize) { 404*bb6cf737SKarl-Johan Karlsson OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, "; 405*bb6cf737SKarl-Johan Karlsson OS << Namespace << "::" << "INSTRUCTION_LIST_END }"; 4065c87daf2SSebastian Pop } 4075c87daf2SSebastian Pop OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n"; 4085c87daf2SSebastian Pop return TableSize; 4095c87daf2SSebastian Pop } 4105c87daf2SSebastian Pop 4115c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4125c87daf2SSebastian Pop // Emit binary search algorithm as part of the functions used to query 4135c87daf2SSebastian Pop // relation tables. 4145c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4155c87daf2SSebastian Pop 4165c87daf2SSebastian Pop void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) { 4175c87daf2SSebastian Pop OS << " unsigned mid;\n"; 4185c87daf2SSebastian Pop OS << " unsigned start = 0;\n"; 4195c87daf2SSebastian Pop OS << " unsigned end = " << TableSize << ";\n"; 4205c87daf2SSebastian Pop OS << " while (start < end) {\n"; 4215c87daf2SSebastian Pop OS << " mid = start + (end - start)/2;\n"; 4225c87daf2SSebastian Pop OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n"; 4235c87daf2SSebastian Pop OS << " break;\n"; 4245c87daf2SSebastian Pop OS << " }\n"; 4255c87daf2SSebastian Pop OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n"; 4265c87daf2SSebastian Pop OS << " end = mid;\n"; 4275c87daf2SSebastian Pop OS << " else\n"; 4285c87daf2SSebastian Pop OS << " start = mid + 1;\n"; 4295c87daf2SSebastian Pop OS << " }\n"; 4305c87daf2SSebastian Pop OS << " if (start == end)\n"; 4315c87daf2SSebastian Pop OS << " return -1; // Instruction doesn't exist in this table.\n\n"; 4325c87daf2SSebastian Pop } 4335c87daf2SSebastian Pop 4345c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4355c87daf2SSebastian Pop // Emit functions to query relation tables. 4365c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4375c87daf2SSebastian Pop 4385c87daf2SSebastian Pop void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, 4395c87daf2SSebastian Pop unsigned TableSize) { 4405c87daf2SSebastian Pop 4415c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 4425c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 4435c87daf2SSebastian Pop 4445c87daf2SSebastian Pop // Emit binary search algorithm to locate instructions in the 4455c87daf2SSebastian Pop // relation table. If found, return opcode value from the appropriate column 4465c87daf2SSebastian Pop // of the table. 4475c87daf2SSebastian Pop emitBinSearch(OS, TableSize); 4485c87daf2SSebastian Pop 4495c87daf2SSebastian Pop if (ValueCols.size() > 1) { 4505c87daf2SSebastian Pop for (unsigned i = 0, e = ValueCols.size(); i < e; i++) { 4515c87daf2SSebastian Pop ListInit *ColumnI = ValueCols[i]; 452664f6a04SCraig Topper for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) { 4535c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString(); 4545c87daf2SSebastian Pop OS << " if (in" << ColName; 4555c87daf2SSebastian Pop OS << " == "; 4565c87daf2SSebastian Pop OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString(); 457664f6a04SCraig Topper if (j < ColumnI->size() - 1) OS << " && "; 4585c87daf2SSebastian Pop else OS << ")\n"; 4595c87daf2SSebastian Pop } 4605c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName(); 4615c87daf2SSebastian Pop OS << "Table[mid]["<<i+1<<"];\n"; 4625c87daf2SSebastian Pop } 4635c87daf2SSebastian Pop OS << " return -1;"; 4645c87daf2SSebastian Pop } 4655c87daf2SSebastian Pop else 4665c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n"; 4675c87daf2SSebastian Pop 4685c87daf2SSebastian Pop OS <<"}\n\n"; 4695c87daf2SSebastian Pop } 4705c87daf2SSebastian Pop 4715c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4725c87daf2SSebastian Pop // Emit relation tables and the functions to query them. 4735c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4745c87daf2SSebastian Pop 4755c87daf2SSebastian Pop void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) { 4765c87daf2SSebastian Pop 4775c87daf2SSebastian Pop // Emit function name and the input parameters : mostly opcode value of the 4785c87daf2SSebastian Pop // current instruction. However, if a table has multiple columns (more than 2 4795c87daf2SSebastian Pop // since first column is used for the key instructions), then we also need 4805c87daf2SSebastian Pop // to pass another input to indicate the column to be selected. 4815c87daf2SSebastian Pop 4825c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 4835c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 484b12c0d9eSMatt Arsenault OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n"; 4855c87daf2SSebastian Pop OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode"; 4865c87daf2SSebastian Pop if (ValueCols.size() > 1) { 487ef0578a8SCraig Topper for (Init *CF : ColFields->getValues()) { 488ef0578a8SCraig Topper std::string ColName = CF->getAsUnquotedString(); 4895c87daf2SSebastian Pop OS << ", enum " << ColName << " in" << ColName << ") {\n"; 4905c87daf2SSebastian Pop } 4915c87daf2SSebastian Pop } else { OS << ") {\n"; } 4925c87daf2SSebastian Pop 4935c87daf2SSebastian Pop // Emit map table. 4945c87daf2SSebastian Pop unsigned TableSize = emitBinSearchTable(OS); 4955c87daf2SSebastian Pop 4965c87daf2SSebastian Pop // Emit rest of the function body. 4975c87daf2SSebastian Pop emitMapFuncBody(OS, TableSize); 4985c87daf2SSebastian Pop } 4995c87daf2SSebastian Pop 5005c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 5015c87daf2SSebastian Pop // Emit enums for the column fields across all the instruction maps. 5025c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 5035c87daf2SSebastian Pop 5045c87daf2SSebastian Pop static void emitEnums(raw_ostream &OS, RecordKeeper &Records) { 5055c87daf2SSebastian Pop 5065c87daf2SSebastian Pop std::vector<Record*> InstrMapVec; 5075c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping"); 5085c87daf2SSebastian Pop std::map<std::string, std::vector<Init*> > ColFieldValueMap; 5095c87daf2SSebastian Pop 5105c87daf2SSebastian Pop // Iterate over all InstrMapping records and create a map between column 5115c87daf2SSebastian Pop // fields and their possible values across all records. 5126e2edc4bSCraig Topper for (Record *CurMap : InstrMapVec) { 5135c87daf2SSebastian Pop ListInit *ColFields; 5145c87daf2SSebastian Pop ColFields = CurMap->getValueAsListInit("ColFields"); 5155c87daf2SSebastian Pop ListInit *List = CurMap->getValueAsListInit("ValueCols"); 5165c87daf2SSebastian Pop std::vector<ListInit*> ValueCols; 517664f6a04SCraig Topper unsigned ListSize = List->size(); 5185c87daf2SSebastian Pop 5195c87daf2SSebastian Pop for (unsigned j = 0; j < ListSize; j++) { 5205c87daf2SSebastian Pop ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j)); 5215c87daf2SSebastian Pop 522664f6a04SCraig Topper if (ListJ->size() != ColFields->size()) 523635debe8SJoerg Sonnenberger PrintFatalError("Record `" + CurMap->getName() + "', field " 524635debe8SJoerg Sonnenberger "`ValueCols' entries don't match with the entries in 'ColFields' !"); 5255c87daf2SSebastian Pop ValueCols.push_back(ListJ); 5265c87daf2SSebastian Pop } 5275c87daf2SSebastian Pop 528664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) { 5295c87daf2SSebastian Pop for (unsigned k = 0; k < ListSize; k++){ 5305c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString(); 5315c87daf2SSebastian Pop ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j)); 5325c87daf2SSebastian Pop } 5335c87daf2SSebastian Pop } 5345c87daf2SSebastian Pop } 5355c87daf2SSebastian Pop 5366e2edc4bSCraig Topper for (auto &Entry : ColFieldValueMap) { 5376e2edc4bSCraig Topper std::vector<Init*> FieldValues = Entry.second; 5385c87daf2SSebastian Pop 5395c87daf2SSebastian Pop // Delete duplicate entries from ColFieldValueMap 540ce3255e7SJyotsna Verma for (unsigned i = 0; i < FieldValues.size() - 1; i++) { 5415c87daf2SSebastian Pop Init *CurVal = FieldValues[i]; 542ce3255e7SJyotsna Verma for (unsigned j = i+1; j < FieldValues.size(); j++) { 5435c87daf2SSebastian Pop if (CurVal == FieldValues[j]) { 5445c87daf2SSebastian Pop FieldValues.erase(FieldValues.begin()+j); 54547de8391SVedant Kumar --j; 5465c87daf2SSebastian Pop } 5475c87daf2SSebastian Pop } 5485c87daf2SSebastian Pop } 5495c87daf2SSebastian Pop 5505c87daf2SSebastian Pop // Emit enumerated values for the column fields. 5516e2edc4bSCraig Topper OS << "enum " << Entry.first << " {\n"; 552ce3255e7SJyotsna Verma for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) { 5536e2edc4bSCraig Topper OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString(); 554ce3255e7SJyotsna Verma if (i != endFV - 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); 570*bb6cf737SKarl-Johan Karlsson std::string NameSpace = Target.getInstNamespace(); 5715c87daf2SSebastian Pop std::vector<Record*> InstrMapVec; 5725c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping"); 5735c87daf2SSebastian Pop 5748c0809c7SAlexander Kornienko if (InstrMapVec.empty()) 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"; 580*bb6cf737SKarl-Johan Karlsson OS << "namespace " << NameSpace << " {\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 // 5886e2edc4bSCraig Topper for (Record *CurMap : InstrMapVec) { 5896e2edc4bSCraig Topper MapTableEmitter IMap(Target, Records, CurMap); 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 } 603*bb6cf737SKarl-Johan Karlsson OS << "} // End " << NameSpace << " 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