15c87daf2SSebastian Pop //===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===// 25c87daf2SSebastian Pop // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65c87daf2SSebastian Pop // 75c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 85c87daf2SSebastian Pop // CodeGenMapTable provides functionality for the TabelGen to create 95c87daf2SSebastian Pop // relation mapping between instructions. Relation models are defined using 105c87daf2SSebastian Pop // InstrMapping as a base class. This file implements the functionality which 115c87daf2SSebastian Pop // parses these definitions and generates relation maps using the information 125c87daf2SSebastian Pop // specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc 135c87daf2SSebastian Pop // file along with the functions to query them. 145c87daf2SSebastian Pop // 155c87daf2SSebastian Pop // A relationship model to relate non-predicate instructions with their 165c87daf2SSebastian Pop // predicated true/false forms can be defined as follows: 175c87daf2SSebastian Pop // 185c87daf2SSebastian Pop // def getPredOpcode : InstrMapping { 195c87daf2SSebastian Pop // let FilterClass = "PredRel"; 205c87daf2SSebastian Pop // let RowFields = ["BaseOpcode"]; 215c87daf2SSebastian Pop // let ColFields = ["PredSense"]; 225c87daf2SSebastian Pop // let KeyCol = ["none"]; 235c87daf2SSebastian Pop // let ValueCols = [["true"], ["false"]]; } 245c87daf2SSebastian Pop // 255c87daf2SSebastian Pop // CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc 265c87daf2SSebastian Pop // file that contains the instructions modeling this relationship. This table 275c87daf2SSebastian Pop // is defined in the function 285c87daf2SSebastian Pop // "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)" 295c87daf2SSebastian Pop // that can be used to retrieve the predicated form of the instruction by 305c87daf2SSebastian Pop // passing its opcode value and the predicate sense (true/false) of the desired 315c87daf2SSebastian Pop // instruction as arguments. 325c87daf2SSebastian Pop // 335c87daf2SSebastian Pop // Short description of the algorithm: 345c87daf2SSebastian Pop // 355c87daf2SSebastian Pop // 1) Iterate through all the records that derive from "InstrMapping" class. 365c87daf2SSebastian Pop // 2) For each record, filter out instructions based on the FilterClass value. 375c87daf2SSebastian Pop // 3) Iterate through this set of instructions and insert them into 385c87daf2SSebastian Pop // RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the 395c87daf2SSebastian Pop // vector of RowFields values and contains vectors of Records (instructions) as 405c87daf2SSebastian Pop // values. RowFields is a list of fields that are required to have the same 415c87daf2SSebastian Pop // values for all the instructions appearing in the same row of the relation 425c87daf2SSebastian Pop // table. All the instructions in a given row of the relation table have some 435c87daf2SSebastian Pop // sort of relationship with the key instruction defined by the corresponding 445c87daf2SSebastian Pop // relationship model. 455c87daf2SSebastian Pop // 465c87daf2SSebastian Pop // Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ] 475c87daf2SSebastian Pop // Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for 485c87daf2SSebastian Pop // RowFields. These groups of instructions are later matched against ValueCols 495c87daf2SSebastian Pop // to determine the column they belong to, if any. 505c87daf2SSebastian Pop // 515c87daf2SSebastian Pop // While building the RowInstrMap map, collect all the key instructions in 525c87daf2SSebastian Pop // KeyInstrVec. These are the instructions having the same values as KeyCol 535c87daf2SSebastian Pop // for all the fields listed in ColFields. 545c87daf2SSebastian Pop // 555c87daf2SSebastian Pop // For Example: 565c87daf2SSebastian Pop // 575c87daf2SSebastian Pop // Relate non-predicate instructions with their predicated true/false forms. 585c87daf2SSebastian Pop // 595c87daf2SSebastian Pop // def getPredOpcode : InstrMapping { 605c87daf2SSebastian Pop // let FilterClass = "PredRel"; 615c87daf2SSebastian Pop // let RowFields = ["BaseOpcode"]; 625c87daf2SSebastian Pop // let ColFields = ["PredSense"]; 635c87daf2SSebastian Pop // let KeyCol = ["none"]; 645c87daf2SSebastian Pop // let ValueCols = [["true"], ["false"]]; } 655c87daf2SSebastian Pop // 665c87daf2SSebastian Pop // Here, only instructions that have "none" as PredSense will be selected as key 675c87daf2SSebastian Pop // instructions. 685c87daf2SSebastian Pop // 695c87daf2SSebastian Pop // 4) For each key instruction, get the group of instructions that share the 705c87daf2SSebastian Pop // same key-value as the key instruction from RowInstrMap. Iterate over the list 715c87daf2SSebastian Pop // of columns in ValueCols (it is defined as a list<list<string> >. Therefore, 725c87daf2SSebastian Pop // it can specify multi-column relationships). For each column, find the 735c87daf2SSebastian Pop // instruction from the group that matches all the values for the column. 745c87daf2SSebastian Pop // Multiple matches are not allowed. 755c87daf2SSebastian Pop // 765c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 775c87daf2SSebastian Pop 785c87daf2SSebastian Pop #include "CodeGenTarget.h" 795c87daf2SSebastian Pop #include "llvm/Support/Format.h" 80635debe8SJoerg Sonnenberger #include "llvm/TableGen/Error.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'. 118f907b891SAlp Toker // 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. 130ec9072d6SCraig Topper if (ColValList->empty()) 131635debe8SJoerg Sonnenberger PrintFatalError(MapRec->getLoc(), "InstrMapping record `" + 132635debe8SJoerg Sonnenberger MapRec->getName() + "' has empty " + "`ValueCols' field!"); 1335c87daf2SSebastian Pop 134ef0578a8SCraig Topper for (Init *I : ColValList->getValues()) { 135ef0578a8SCraig Topper ListInit *ColI = dyn_cast<ListInit>(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'. 139664f6a04SCraig Topper if (ColI->size() != ColFields->size()) 140635debe8SJoerg Sonnenberger PrintFatalError(MapRec->getLoc(), "Record `" + MapRec->getName() + 141635debe8SJoerg Sonnenberger "', field `ValueCols' entries don't match with " + 142635debe8SJoerg Sonnenberger " the entries in 'ColFields'!"); 1435c87daf2SSebastian Pop ValueCols.push_back(ColI); 1445c87daf2SSebastian Pop } 1455c87daf2SSebastian Pop } 1465c87daf2SSebastian Pop 1475c87daf2SSebastian Pop std::string getName() const { 1485c87daf2SSebastian Pop return Name; 1495c87daf2SSebastian Pop } 1505c87daf2SSebastian Pop 1515c87daf2SSebastian Pop std::string getFilterClass() { 1525c87daf2SSebastian Pop return FilterClass; 1535c87daf2SSebastian Pop } 1545c87daf2SSebastian Pop 1555c87daf2SSebastian Pop ListInit *getRowFields() const { 1565c87daf2SSebastian Pop return RowFields; 1575c87daf2SSebastian Pop } 1585c87daf2SSebastian Pop 1595c87daf2SSebastian Pop ListInit *getColFields() const { 1605c87daf2SSebastian Pop return ColFields; 1615c87daf2SSebastian Pop } 1625c87daf2SSebastian Pop 1635c87daf2SSebastian Pop ListInit *getKeyCol() const { 1645c87daf2SSebastian Pop return KeyCol; 1655c87daf2SSebastian Pop } 1665c87daf2SSebastian Pop 1675c87daf2SSebastian Pop const std::vector<ListInit*> &getValueCols() const { 1685c87daf2SSebastian Pop return ValueCols; 1695c87daf2SSebastian Pop } 1705c87daf2SSebastian Pop }; 1715c87daf2SSebastian Pop } // End anonymous namespace. 1725c87daf2SSebastian Pop 1735c87daf2SSebastian Pop 1745c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 1755c87daf2SSebastian Pop // class MapTableEmitter : It builds the instruction relation maps using 1765c87daf2SSebastian Pop // the information provided in InstrMapping records. It outputs these 1775c87daf2SSebastian Pop // relationship maps as tables into XXXGenInstrInfo.inc file along with the 1785c87daf2SSebastian Pop // functions to query them. 1795c87daf2SSebastian Pop 1805c87daf2SSebastian Pop namespace { 1815c87daf2SSebastian Pop class MapTableEmitter { 1825c87daf2SSebastian Pop private: 1835c87daf2SSebastian Pop // std::string TargetName; 1845c87daf2SSebastian Pop const CodeGenTarget &Target; 1855c87daf2SSebastian Pop // InstrMapDesc - InstrMapping record to be processed. 1865c87daf2SSebastian Pop InstrMap InstrMapDesc; 1875c87daf2SSebastian Pop 1885c87daf2SSebastian Pop // InstrDefs - list of instructions filtered using FilterClass defined 1895c87daf2SSebastian Pop // in InstrMapDesc. 1905c87daf2SSebastian Pop std::vector<Record*> InstrDefs; 1915c87daf2SSebastian Pop 1925c87daf2SSebastian Pop // RowInstrMap - maps RowFields values to the instructions. It's keyed by the 1935c87daf2SSebastian Pop // values of the row fields and contains vector of records as values. 1945c87daf2SSebastian Pop RowInstrMapTy RowInstrMap; 1955c87daf2SSebastian Pop 1965c87daf2SSebastian Pop // KeyInstrVec - list of key instructions. 1975c87daf2SSebastian Pop std::vector<Record*> KeyInstrVec; 1985c87daf2SSebastian Pop DenseMap<Record*, std::vector<Record*> > MapTable; 1995c87daf2SSebastian Pop 2005c87daf2SSebastian Pop public: 2015c87daf2SSebastian Pop MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec): 202dcbd1608SDavid Blaikie Target(Target), InstrMapDesc(IMRec) { 2035c87daf2SSebastian Pop const std::string FilterClass = InstrMapDesc.getFilterClass(); 2045c87daf2SSebastian Pop InstrDefs = Records.getAllDerivedDefinitions(FilterClass); 205dcbd1608SDavid Blaikie } 2065c87daf2SSebastian Pop 2075c87daf2SSebastian Pop void buildRowInstrMap(); 2085c87daf2SSebastian Pop 2095c87daf2SSebastian Pop // Returns true if an instruction is a key instruction, i.e., its ColFields 2105c87daf2SSebastian Pop // have same values as KeyCol. 2115c87daf2SSebastian Pop bool isKeyColInstr(Record* CurInstr); 2125c87daf2SSebastian Pop 2135c87daf2SSebastian Pop // Find column instruction corresponding to a key instruction based on the 2145c87daf2SSebastian Pop // constraints for that column. 2155c87daf2SSebastian Pop Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol); 2165c87daf2SSebastian Pop 2175c87daf2SSebastian Pop // Find column instructions for each key instruction based 2185c87daf2SSebastian Pop // on ValueCols and store them into MapTable. 2195c87daf2SSebastian Pop void buildMapTable(); 2205c87daf2SSebastian Pop 2215c87daf2SSebastian Pop void emitBinSearch(raw_ostream &OS, unsigned TableSize); 2225c87daf2SSebastian Pop void emitTablesWithFunc(raw_ostream &OS); 2235c87daf2SSebastian Pop unsigned emitBinSearchTable(raw_ostream &OS); 2245c87daf2SSebastian Pop 2255c87daf2SSebastian Pop // Lookup functions to query binary search tables. 2265c87daf2SSebastian Pop void emitMapFuncBody(raw_ostream &OS, unsigned TableSize); 2275c87daf2SSebastian Pop 2285c87daf2SSebastian Pop }; 2295c87daf2SSebastian Pop } // End anonymous namespace. 2305c87daf2SSebastian Pop 2315c87daf2SSebastian Pop 2325c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2335c87daf2SSebastian Pop // Process all the instructions that model this relation (alreday present in 2345c87daf2SSebastian Pop // InstrDefs) and insert them into RowInstrMap which is keyed by the values of 2355c87daf2SSebastian Pop // the fields listed as RowFields. It stores vectors of records as values. 2365c87daf2SSebastian Pop // All the related instructions have the same values for the RowFields thus are 2375c87daf2SSebastian Pop // part of the same key-value pair. 2385c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2395c87daf2SSebastian Pop 2405c87daf2SSebastian Pop void MapTableEmitter::buildRowInstrMap() { 241ef0578a8SCraig Topper for (Record *CurInstr : InstrDefs) { 2425c87daf2SSebastian Pop std::vector<Init*> KeyValue; 2435c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields(); 244ef0578a8SCraig Topper for (Init *RowField : RowFields->getValues()) { 245f02ad15fSAleksandar Beserminji RecordVal *RecVal = CurInstr->getValue(RowField); 246f02ad15fSAleksandar Beserminji if (RecVal == nullptr) 247f02ad15fSAleksandar Beserminji PrintFatalError(CurInstr->getLoc(), "No value " + 248f02ad15fSAleksandar Beserminji RowField->getAsString() + " found in \"" + 249f02ad15fSAleksandar Beserminji CurInstr->getName() + "\" instruction description."); 250f02ad15fSAleksandar Beserminji Init *CurInstrVal = RecVal->getValue(); 2515c87daf2SSebastian Pop KeyValue.push_back(CurInstrVal); 2525c87daf2SSebastian Pop } 2535c87daf2SSebastian Pop 2545c87daf2SSebastian Pop // Collect key instructions into KeyInstrVec. Later, these instructions are 2555c87daf2SSebastian Pop // processed to assign column position to the instructions sharing 2565c87daf2SSebastian Pop // their KeyValue in RowInstrMap. 2575c87daf2SSebastian Pop if (isKeyColInstr(CurInstr)) 2585c87daf2SSebastian Pop KeyInstrVec.push_back(CurInstr); 2595c87daf2SSebastian Pop 2605c87daf2SSebastian Pop RowInstrMap[KeyValue].push_back(CurInstr); 2615c87daf2SSebastian Pop } 2625c87daf2SSebastian Pop } 2635c87daf2SSebastian Pop 2645c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2655c87daf2SSebastian Pop // Return true if an instruction is a KeyCol instruction. 2665c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2675c87daf2SSebastian Pop 2685c87daf2SSebastian Pop bool MapTableEmitter::isKeyColInstr(Record* CurInstr) { 2695c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 2705c87daf2SSebastian Pop ListInit *KeyCol = InstrMapDesc.getKeyCol(); 2715c87daf2SSebastian Pop 2725c87daf2SSebastian Pop // Check if the instruction is a KeyCol instruction. 2735c87daf2SSebastian Pop bool MatchFound = true; 274664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); 2755c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) { 2765c87daf2SSebastian Pop RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j)); 2775c87daf2SSebastian Pop std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString(); 2785c87daf2SSebastian Pop std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString(); 2795c87daf2SSebastian Pop MatchFound = (CurInstrVal == KeyColValue); 2805c87daf2SSebastian Pop } 2815c87daf2SSebastian Pop return MatchFound; 2825c87daf2SSebastian Pop } 2835c87daf2SSebastian Pop 2845c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2855c87daf2SSebastian Pop // Build a map to link key instructions with the column instructions arranged 2865c87daf2SSebastian Pop // according to their column positions. 2875c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 2885c87daf2SSebastian Pop 2895c87daf2SSebastian Pop void MapTableEmitter::buildMapTable() { 2905c87daf2SSebastian Pop // Find column instructions for a given key based on the ColField 2915c87daf2SSebastian Pop // constraints. 2925c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 2935c87daf2SSebastian Pop unsigned NumOfCols = ValueCols.size(); 294ef0578a8SCraig Topper for (Record *CurKeyInstr : KeyInstrVec) { 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. 317ef0578a8SCraig Topper for (Init *RowField : RowFields->getValues()) { 318ef0578a8SCraig Topper Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue(); 3195c87daf2SSebastian Pop KeyValue.push_back(KeyInstrVal); 3205c87daf2SSebastian Pop } 3215c87daf2SSebastian Pop 3225c87daf2SSebastian Pop // Get all the instructions that share the same KeyValue as the KeyInstr 3235c87daf2SSebastian Pop // in RowInstrMap. We search through these instructions to find a match 3245c87daf2SSebastian Pop // for the current column, i.e., the instruction which has the same values 3255c87daf2SSebastian Pop // as CurValueCol for all the fields in ColFields. 3265c87daf2SSebastian Pop const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue]; 3275c87daf2SSebastian Pop 3285c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 32924064771SCraig Topper Record *MatchInstr = nullptr; 3305c87daf2SSebastian Pop 3315c87daf2SSebastian Pop for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) { 3325c87daf2SSebastian Pop bool MatchFound = true; 3335c87daf2SSebastian Pop Record *CurInstr = RelatedInstrVec[i]; 334664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); 3355c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) { 3365c87daf2SSebastian Pop Init *ColFieldJ = ColFields->getElement(j); 3375c87daf2SSebastian Pop Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue(); 3385c87daf2SSebastian Pop std::string CurInstrVal = CurInstrInit->getAsUnquotedString(); 3395c87daf2SSebastian Pop Init *ColFieldJVallue = CurValueCol->getElement(j); 3405c87daf2SSebastian Pop MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString()); 3415c87daf2SSebastian Pop } 3425c87daf2SSebastian Pop 3435c87daf2SSebastian Pop if (MatchFound) { 344411fbbf5SNicolai Haehnle if (MatchInstr) { 345411fbbf5SNicolai Haehnle // Already had a match 3465c87daf2SSebastian Pop // Error if multiple matches are found for a column. 347411fbbf5SNicolai Haehnle std::string KeyValueStr; 348411fbbf5SNicolai Haehnle for (Init *Value : KeyValue) { 349411fbbf5SNicolai Haehnle if (!KeyValueStr.empty()) 350411fbbf5SNicolai Haehnle KeyValueStr += ", "; 351411fbbf5SNicolai Haehnle KeyValueStr += Value->getAsString(); 352411fbbf5SNicolai Haehnle } 353411fbbf5SNicolai Haehnle 354635debe8SJoerg Sonnenberger PrintFatalError("Multiple matches found for `" + KeyInstr->getName() + 355411fbbf5SNicolai Haehnle "', for the relation `" + InstrMapDesc.getName() + "', row fields [" + 356411fbbf5SNicolai Haehnle KeyValueStr + "], column `" + CurValueCol->getAsString() + "'"); 357411fbbf5SNicolai Haehnle } 3585c87daf2SSebastian Pop MatchInstr = CurInstr; 3595c87daf2SSebastian Pop } 3605c87daf2SSebastian Pop } 3615c87daf2SSebastian Pop return MatchInstr; 3625c87daf2SSebastian Pop } 3635c87daf2SSebastian Pop 3645c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 3655c87daf2SSebastian Pop // Emit one table per relation. Only instructions with a valid relation of a 3665c87daf2SSebastian Pop // given type are included in the table sorted by their enum values (opcodes). 3675c87daf2SSebastian Pop // Binary search is used for locating instructions in the table. 3685c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 3695c87daf2SSebastian Pop 3705c87daf2SSebastian Pop unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) { 3715c87daf2SSebastian Pop 37228851b62SCraig Topper ArrayRef<const CodeGenInstruction*> NumberedInstructions = 3735c87daf2SSebastian Pop Target.getInstructionsByEnumValue(); 37486a9aee8SCraig Topper StringRef Namespace = Target.getInstNamespace(); 3755c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 3765c87daf2SSebastian Pop unsigned NumCol = ValueCols.size(); 3775c87daf2SSebastian Pop unsigned TotalNumInstr = NumberedInstructions.size(); 3785c87daf2SSebastian Pop unsigned TableSize = 0; 3795c87daf2SSebastian Pop 3805c87daf2SSebastian Pop OS << "static const uint16_t "<<InstrMapDesc.getName(); 3815c87daf2SSebastian Pop // Number of columns in the table are NumCol+1 because key instructions are 3825c87daf2SSebastian Pop // emitted as first column. 3835c87daf2SSebastian Pop OS << "Table[]["<< NumCol+1 << "] = {\n"; 3845c87daf2SSebastian Pop for (unsigned i = 0; i < TotalNumInstr; i++) { 3855c87daf2SSebastian Pop Record *CurInstr = NumberedInstructions[i]->TheDef; 3865c87daf2SSebastian Pop std::vector<Record*> ColInstrs = MapTable[CurInstr]; 3875c87daf2SSebastian Pop std::string OutStr(""); 3885c87daf2SSebastian Pop unsigned RelExists = 0; 3898c0809c7SAlexander Kornienko if (!ColInstrs.empty()) { 3905c87daf2SSebastian Pop for (unsigned j = 0; j < NumCol; j++) { 39124064771SCraig Topper if (ColInstrs[j] != nullptr) { 3925c87daf2SSebastian Pop RelExists = 1; 3935c87daf2SSebastian Pop OutStr += ", "; 394bb6cf737SKarl-Johan Karlsson OutStr += Namespace; 3955c87daf2SSebastian Pop OutStr += "::"; 3965c87daf2SSebastian Pop OutStr += ColInstrs[j]->getName(); 3970a7a17dfSBenjamin Kramer } else { OutStr += ", (uint16_t)-1U";} 3985c87daf2SSebastian Pop } 3995c87daf2SSebastian Pop 4005c87daf2SSebastian Pop if (RelExists) { 401bb6cf737SKarl-Johan Karlsson OS << " { " << Namespace << "::" << CurInstr->getName(); 4025c87daf2SSebastian Pop OS << OutStr <<" },\n"; 4035c87daf2SSebastian Pop TableSize++; 4045c87daf2SSebastian Pop } 4055c87daf2SSebastian Pop } 4065c87daf2SSebastian Pop } 4075c87daf2SSebastian Pop if (!TableSize) { 408bb6cf737SKarl-Johan Karlsson OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, "; 409bb6cf737SKarl-Johan Karlsson OS << Namespace << "::" << "INSTRUCTION_LIST_END }"; 4105c87daf2SSebastian Pop } 4115c87daf2SSebastian Pop OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n"; 4125c87daf2SSebastian Pop return TableSize; 4135c87daf2SSebastian Pop } 4145c87daf2SSebastian Pop 4155c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4165c87daf2SSebastian Pop // Emit binary search algorithm as part of the functions used to query 4175c87daf2SSebastian Pop // relation tables. 4185c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4195c87daf2SSebastian Pop 4205c87daf2SSebastian Pop void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) { 4215c87daf2SSebastian Pop OS << " unsigned mid;\n"; 4225c87daf2SSebastian Pop OS << " unsigned start = 0;\n"; 4235c87daf2SSebastian Pop OS << " unsigned end = " << TableSize << ";\n"; 4245c87daf2SSebastian Pop OS << " while (start < end) {\n"; 4255c87daf2SSebastian Pop OS << " mid = start + (end - start)/2;\n"; 4265c87daf2SSebastian Pop OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n"; 4275c87daf2SSebastian Pop OS << " break;\n"; 4285c87daf2SSebastian Pop OS << " }\n"; 4295c87daf2SSebastian Pop OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n"; 4305c87daf2SSebastian Pop OS << " end = mid;\n"; 4315c87daf2SSebastian Pop OS << " else\n"; 4325c87daf2SSebastian Pop OS << " start = mid + 1;\n"; 4335c87daf2SSebastian Pop OS << " }\n"; 4345c87daf2SSebastian Pop OS << " if (start == end)\n"; 4355c87daf2SSebastian Pop OS << " return -1; // Instruction doesn't exist in this table.\n\n"; 4365c87daf2SSebastian Pop } 4375c87daf2SSebastian Pop 4385c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4395c87daf2SSebastian Pop // Emit functions to query relation tables. 4405c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4415c87daf2SSebastian Pop 4425c87daf2SSebastian Pop void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, 4435c87daf2SSebastian Pop unsigned TableSize) { 4445c87daf2SSebastian Pop 4455c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 4465c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 4475c87daf2SSebastian Pop 4485c87daf2SSebastian Pop // Emit binary search algorithm to locate instructions in the 4495c87daf2SSebastian Pop // relation table. If found, return opcode value from the appropriate column 4505c87daf2SSebastian Pop // of the table. 4515c87daf2SSebastian Pop emitBinSearch(OS, TableSize); 4525c87daf2SSebastian Pop 4535c87daf2SSebastian Pop if (ValueCols.size() > 1) { 4545c87daf2SSebastian Pop for (unsigned i = 0, e = ValueCols.size(); i < e; i++) { 4555c87daf2SSebastian Pop ListInit *ColumnI = ValueCols[i]; 456664f6a04SCraig Topper for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) { 4575c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString(); 4585c87daf2SSebastian Pop OS << " if (in" << ColName; 4595c87daf2SSebastian Pop OS << " == "; 4605c87daf2SSebastian Pop OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString(); 461664f6a04SCraig Topper if (j < ColumnI->size() - 1) OS << " && "; 4625c87daf2SSebastian Pop else OS << ")\n"; 4635c87daf2SSebastian Pop } 4645c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName(); 4655c87daf2SSebastian Pop OS << "Table[mid]["<<i+1<<"];\n"; 4665c87daf2SSebastian Pop } 4675c87daf2SSebastian Pop OS << " return -1;"; 4685c87daf2SSebastian Pop } 4695c87daf2SSebastian Pop else 4705c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n"; 4715c87daf2SSebastian Pop 4725c87daf2SSebastian Pop OS <<"}\n\n"; 4735c87daf2SSebastian Pop } 4745c87daf2SSebastian Pop 4755c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4765c87daf2SSebastian Pop // Emit relation tables and the functions to query them. 4775c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 4785c87daf2SSebastian Pop 4795c87daf2SSebastian Pop void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) { 4805c87daf2SSebastian Pop 4815c87daf2SSebastian Pop // Emit function name and the input parameters : mostly opcode value of the 4825c87daf2SSebastian Pop // current instruction. However, if a table has multiple columns (more than 2 4835c87daf2SSebastian Pop // since first column is used for the key instructions), then we also need 4845c87daf2SSebastian Pop // to pass another input to indicate the column to be selected. 4855c87daf2SSebastian Pop 4865c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 4875c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 488b12c0d9eSMatt Arsenault OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n"; 4895c87daf2SSebastian Pop OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode"; 4905c87daf2SSebastian Pop if (ValueCols.size() > 1) { 491ef0578a8SCraig Topper for (Init *CF : ColFields->getValues()) { 492ef0578a8SCraig Topper std::string ColName = CF->getAsUnquotedString(); 4935c87daf2SSebastian Pop OS << ", enum " << ColName << " in" << ColName << ") {\n"; 4945c87daf2SSebastian Pop } 4955c87daf2SSebastian Pop } else { OS << ") {\n"; } 4965c87daf2SSebastian Pop 4975c87daf2SSebastian Pop // Emit map table. 4985c87daf2SSebastian Pop unsigned TableSize = emitBinSearchTable(OS); 4995c87daf2SSebastian Pop 5005c87daf2SSebastian Pop // Emit rest of the function body. 5015c87daf2SSebastian Pop emitMapFuncBody(OS, TableSize); 5025c87daf2SSebastian Pop } 5035c87daf2SSebastian Pop 5045c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 5055c87daf2SSebastian Pop // Emit enums for the column fields across all the instruction maps. 5065c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 5075c87daf2SSebastian Pop 5085c87daf2SSebastian Pop static void emitEnums(raw_ostream &OS, RecordKeeper &Records) { 5095c87daf2SSebastian Pop 5105c87daf2SSebastian Pop std::vector<Record*> InstrMapVec; 5115c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping"); 5125c87daf2SSebastian Pop std::map<std::string, std::vector<Init*> > ColFieldValueMap; 5135c87daf2SSebastian Pop 5145c87daf2SSebastian Pop // Iterate over all InstrMapping records and create a map between column 5155c87daf2SSebastian Pop // fields and their possible values across all records. 5166e2edc4bSCraig Topper for (Record *CurMap : InstrMapVec) { 5175c87daf2SSebastian Pop ListInit *ColFields; 5185c87daf2SSebastian Pop ColFields = CurMap->getValueAsListInit("ColFields"); 5195c87daf2SSebastian Pop ListInit *List = CurMap->getValueAsListInit("ValueCols"); 5205c87daf2SSebastian Pop std::vector<ListInit*> ValueCols; 521664f6a04SCraig Topper unsigned ListSize = List->size(); 5225c87daf2SSebastian Pop 5235c87daf2SSebastian Pop for (unsigned j = 0; j < ListSize; j++) { 5245c87daf2SSebastian Pop ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j)); 5255c87daf2SSebastian Pop 526664f6a04SCraig Topper if (ListJ->size() != ColFields->size()) 527635debe8SJoerg Sonnenberger PrintFatalError("Record `" + CurMap->getName() + "', field " 528635debe8SJoerg Sonnenberger "`ValueCols' entries don't match with the entries in 'ColFields' !"); 5295c87daf2SSebastian Pop ValueCols.push_back(ListJ); 5305c87daf2SSebastian Pop } 5315c87daf2SSebastian Pop 532664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) { 5335c87daf2SSebastian Pop for (unsigned k = 0; k < ListSize; k++){ 5345c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString(); 5355c87daf2SSebastian Pop ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j)); 5365c87daf2SSebastian Pop } 5375c87daf2SSebastian Pop } 5385c87daf2SSebastian Pop } 5395c87daf2SSebastian Pop 5406e2edc4bSCraig Topper for (auto &Entry : ColFieldValueMap) { 5416e2edc4bSCraig Topper std::vector<Init*> FieldValues = Entry.second; 5425c87daf2SSebastian Pop 5435c87daf2SSebastian Pop // Delete duplicate entries from ColFieldValueMap 544ce3255e7SJyotsna Verma for (unsigned i = 0; i < FieldValues.size() - 1; i++) { 5455c87daf2SSebastian Pop Init *CurVal = FieldValues[i]; 546ce3255e7SJyotsna Verma for (unsigned j = i+1; j < FieldValues.size(); j++) { 5475c87daf2SSebastian Pop if (CurVal == FieldValues[j]) { 5485c87daf2SSebastian Pop FieldValues.erase(FieldValues.begin()+j); 54947de8391SVedant Kumar --j; 5505c87daf2SSebastian Pop } 5515c87daf2SSebastian Pop } 5525c87daf2SSebastian Pop } 5535c87daf2SSebastian Pop 5545c87daf2SSebastian Pop // Emit enumerated values for the column fields. 5556e2edc4bSCraig Topper OS << "enum " << Entry.first << " {\n"; 556ce3255e7SJyotsna Verma for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) { 5576e2edc4bSCraig Topper OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString(); 558ce3255e7SJyotsna Verma if (i != endFV - 1) 5595c87daf2SSebastian Pop OS << ",\n"; 5605c87daf2SSebastian Pop else 5615c87daf2SSebastian Pop OS << "\n};\n\n"; 5625c87daf2SSebastian Pop } 5635c87daf2SSebastian Pop } 5645c87daf2SSebastian Pop } 5655c87daf2SSebastian Pop 5665c87daf2SSebastian Pop namespace llvm { 5675c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 5685c87daf2SSebastian Pop // Parse 'InstrMapping' records and use the information to form relationship 5695c87daf2SSebastian Pop // between instructions. These relations are emitted as a tables along with the 5705c87daf2SSebastian Pop // functions to query them. 5715c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 5725c87daf2SSebastian Pop void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) { 5735c87daf2SSebastian Pop CodeGenTarget Target(Records); 57486a9aee8SCraig Topper StringRef NameSpace = Target.getInstNamespace(); 5755c87daf2SSebastian Pop std::vector<Record*> InstrMapVec; 5765c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping"); 5775c87daf2SSebastian Pop 5788c0809c7SAlexander Kornienko if (InstrMapVec.empty()) 5795c87daf2SSebastian Pop return; 5805c87daf2SSebastian Pop 5815c87daf2SSebastian Pop OS << "#ifdef GET_INSTRMAP_INFO\n"; 5825c87daf2SSebastian Pop OS << "#undef GET_INSTRMAP_INFO\n"; 5835c87daf2SSebastian Pop OS << "namespace llvm {\n\n"; 584bb6cf737SKarl-Johan Karlsson OS << "namespace " << NameSpace << " {\n\n"; 5855c87daf2SSebastian Pop 5865c87daf2SSebastian Pop // Emit coulumn field names and their values as enums. 5875c87daf2SSebastian Pop emitEnums(OS, Records); 5885c87daf2SSebastian Pop 5895c87daf2SSebastian Pop // Iterate over all instruction mapping records and construct relationship 5905c87daf2SSebastian Pop // maps based on the information specified there. 5915c87daf2SSebastian Pop // 5926e2edc4bSCraig Topper for (Record *CurMap : InstrMapVec) { 5936e2edc4bSCraig Topper MapTableEmitter IMap(Target, Records, CurMap); 5945c87daf2SSebastian Pop 5955c87daf2SSebastian Pop // Build RowInstrMap to group instructions based on their values for 5965c87daf2SSebastian Pop // RowFields. In the process, also collect key instructions into 5975c87daf2SSebastian Pop // KeyInstrVec. 5985c87daf2SSebastian Pop IMap.buildRowInstrMap(); 5995c87daf2SSebastian Pop 6005c87daf2SSebastian Pop // Build MapTable to map key instructions with the corresponding column 6015c87daf2SSebastian Pop // instructions. 6025c87daf2SSebastian Pop IMap.buildMapTable(); 6035c87daf2SSebastian Pop 6045c87daf2SSebastian Pop // Emit map tables and the functions to query them. 6055c87daf2SSebastian Pop IMap.emitTablesWithFunc(OS); 6065c87daf2SSebastian Pop } 607bb6cf737SKarl-Johan Karlsson OS << "} // End " << NameSpace << " namespace\n"; 6085c87daf2SSebastian Pop OS << "} // End llvm namespace\n"; 6095c87daf2SSebastian Pop OS << "#endif // GET_INSTRMAP_INFO\n\n"; 6105c87daf2SSebastian Pop } 6115c87daf2SSebastian Pop 6125c87daf2SSebastian Pop } // End llvm namespace 613