1*5c87daf2SSebastian Pop //===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===// 2*5c87daf2SSebastian Pop // 3*5c87daf2SSebastian Pop // The LLVM Compiler Infrastructure 4*5c87daf2SSebastian Pop // 5*5c87daf2SSebastian Pop // This file is distributed under the University of Illinois Open Source 6*5c87daf2SSebastian Pop // License. See LICENSE.TXT for details. 7*5c87daf2SSebastian Pop // 8*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 9*5c87daf2SSebastian Pop // CodeGenMapTable provides functionality for the TabelGen to create 10*5c87daf2SSebastian Pop // relation mapping between instructions. Relation models are defined using 11*5c87daf2SSebastian Pop // InstrMapping as a base class. This file implements the functionality which 12*5c87daf2SSebastian Pop // parses these definitions and generates relation maps using the information 13*5c87daf2SSebastian Pop // specified there. These maps are emitted as tables in the XXXGenInstrInfo.inc 14*5c87daf2SSebastian Pop // file along with the functions to query them. 15*5c87daf2SSebastian Pop // 16*5c87daf2SSebastian Pop // A relationship model to relate non-predicate instructions with their 17*5c87daf2SSebastian Pop // predicated true/false forms can be defined as follows: 18*5c87daf2SSebastian Pop // 19*5c87daf2SSebastian Pop // def getPredOpcode : InstrMapping { 20*5c87daf2SSebastian Pop // let FilterClass = "PredRel"; 21*5c87daf2SSebastian Pop // let RowFields = ["BaseOpcode"]; 22*5c87daf2SSebastian Pop // let ColFields = ["PredSense"]; 23*5c87daf2SSebastian Pop // let KeyCol = ["none"]; 24*5c87daf2SSebastian Pop // let ValueCols = [["true"], ["false"]]; } 25*5c87daf2SSebastian Pop // 26*5c87daf2SSebastian Pop // CodeGenMapTable parses this map and generates a table in XXXGenInstrInfo.inc 27*5c87daf2SSebastian Pop // file that contains the instructions modeling this relationship. This table 28*5c87daf2SSebastian Pop // is defined in the function 29*5c87daf2SSebastian Pop // "int getPredOpcode(uint16_t Opcode, enum PredSense inPredSense)" 30*5c87daf2SSebastian Pop // that can be used to retrieve the predicated form of the instruction by 31*5c87daf2SSebastian Pop // passing its opcode value and the predicate sense (true/false) of the desired 32*5c87daf2SSebastian Pop // instruction as arguments. 33*5c87daf2SSebastian Pop // 34*5c87daf2SSebastian Pop // Short description of the algorithm: 35*5c87daf2SSebastian Pop // 36*5c87daf2SSebastian Pop // 1) Iterate through all the records that derive from "InstrMapping" class. 37*5c87daf2SSebastian Pop // 2) For each record, filter out instructions based on the FilterClass value. 38*5c87daf2SSebastian Pop // 3) Iterate through this set of instructions and insert them into 39*5c87daf2SSebastian Pop // RowInstrMap map based on their RowFields values. RowInstrMap is keyed by the 40*5c87daf2SSebastian Pop // vector of RowFields values and contains vectors of Records (instructions) as 41*5c87daf2SSebastian Pop // values. RowFields is a list of fields that are required to have the same 42*5c87daf2SSebastian Pop // values for all the instructions appearing in the same row of the relation 43*5c87daf2SSebastian Pop // table. All the instructions in a given row of the relation table have some 44*5c87daf2SSebastian Pop // sort of relationship with the key instruction defined by the corresponding 45*5c87daf2SSebastian Pop // relationship model. 46*5c87daf2SSebastian Pop // 47*5c87daf2SSebastian Pop // Ex: RowInstrMap(RowVal1, RowVal2, ...) -> [Instr1, Instr2, Instr3, ... ] 48*5c87daf2SSebastian Pop // Here Instr1, Instr2, Instr3 have same values (RowVal1, RowVal2) for 49*5c87daf2SSebastian Pop // RowFields. These groups of instructions are later matched against ValueCols 50*5c87daf2SSebastian Pop // to determine the column they belong to, if any. 51*5c87daf2SSebastian Pop // 52*5c87daf2SSebastian Pop // While building the RowInstrMap map, collect all the key instructions in 53*5c87daf2SSebastian Pop // KeyInstrVec. These are the instructions having the same values as KeyCol 54*5c87daf2SSebastian Pop // for all the fields listed in ColFields. 55*5c87daf2SSebastian Pop // 56*5c87daf2SSebastian Pop // For Example: 57*5c87daf2SSebastian Pop // 58*5c87daf2SSebastian Pop // Relate non-predicate instructions with their predicated true/false forms. 59*5c87daf2SSebastian Pop // 60*5c87daf2SSebastian Pop // def getPredOpcode : InstrMapping { 61*5c87daf2SSebastian Pop // let FilterClass = "PredRel"; 62*5c87daf2SSebastian Pop // let RowFields = ["BaseOpcode"]; 63*5c87daf2SSebastian Pop // let ColFields = ["PredSense"]; 64*5c87daf2SSebastian Pop // let KeyCol = ["none"]; 65*5c87daf2SSebastian Pop // let ValueCols = [["true"], ["false"]]; } 66*5c87daf2SSebastian Pop // 67*5c87daf2SSebastian Pop // Here, only instructions that have "none" as PredSense will be selected as key 68*5c87daf2SSebastian Pop // instructions. 69*5c87daf2SSebastian Pop // 70*5c87daf2SSebastian Pop // 4) For each key instruction, get the group of instructions that share the 71*5c87daf2SSebastian Pop // same key-value as the key instruction from RowInstrMap. Iterate over the list 72*5c87daf2SSebastian Pop // of columns in ValueCols (it is defined as a list<list<string> >. Therefore, 73*5c87daf2SSebastian Pop // it can specify multi-column relationships). For each column, find the 74*5c87daf2SSebastian Pop // instruction from the group that matches all the values for the column. 75*5c87daf2SSebastian Pop // Multiple matches are not allowed. 76*5c87daf2SSebastian Pop // 77*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 78*5c87daf2SSebastian Pop 79*5c87daf2SSebastian Pop #include "CodeGenTarget.h" 80*5c87daf2SSebastian Pop #include "llvm/Support/Format.h" 81*5c87daf2SSebastian Pop using namespace llvm; 82*5c87daf2SSebastian Pop typedef std::map<std::string, std::vector<Record*> > InstrRelMapTy; 83*5c87daf2SSebastian Pop 84*5c87daf2SSebastian Pop typedef std::map<std::vector<Init*>, std::vector<Record*> > RowInstrMapTy; 85*5c87daf2SSebastian Pop 86*5c87daf2SSebastian Pop namespace { 87*5c87daf2SSebastian Pop 88*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 89*5c87daf2SSebastian Pop // This class is used to represent InstrMapping class defined in Target.td file. 90*5c87daf2SSebastian Pop class InstrMap { 91*5c87daf2SSebastian Pop private: 92*5c87daf2SSebastian Pop std::string Name; 93*5c87daf2SSebastian Pop std::string FilterClass; 94*5c87daf2SSebastian Pop ListInit *RowFields; 95*5c87daf2SSebastian Pop ListInit *ColFields; 96*5c87daf2SSebastian Pop ListInit *KeyCol; 97*5c87daf2SSebastian Pop std::vector<ListInit*> ValueCols; 98*5c87daf2SSebastian Pop 99*5c87daf2SSebastian Pop public: 100*5c87daf2SSebastian Pop InstrMap(Record* MapRec) { 101*5c87daf2SSebastian Pop Name = MapRec->getName(); 102*5c87daf2SSebastian Pop 103*5c87daf2SSebastian Pop // FilterClass - It's used to reduce the search space only to the 104*5c87daf2SSebastian Pop // instructions that define the kind of relationship modeled by 105*5c87daf2SSebastian Pop // this InstrMapping object/record. 106*5c87daf2SSebastian Pop const RecordVal *Filter = MapRec->getValue("FilterClass"); 107*5c87daf2SSebastian Pop FilterClass = Filter->getValue()->getAsUnquotedString(); 108*5c87daf2SSebastian Pop 109*5c87daf2SSebastian Pop // List of fields/attributes that need to be same across all the 110*5c87daf2SSebastian Pop // instructions in a row of the relation table. 111*5c87daf2SSebastian Pop RowFields = MapRec->getValueAsListInit("RowFields"); 112*5c87daf2SSebastian Pop 113*5c87daf2SSebastian Pop // List of fields/attributes that are constant across all the instruction 114*5c87daf2SSebastian Pop // in a column of the relation table. Ex: ColFields = 'predSense' 115*5c87daf2SSebastian Pop ColFields = MapRec->getValueAsListInit("ColFields"); 116*5c87daf2SSebastian Pop 117*5c87daf2SSebastian Pop // Values for the fields/attributes listed in 'ColFields'. 118*5c87daf2SSebastian Pop // Ex: KeyCol = 'noPred' -- key instruction is non predicated 119*5c87daf2SSebastian Pop KeyCol = MapRec->getValueAsListInit("KeyCol"); 120*5c87daf2SSebastian Pop 121*5c87daf2SSebastian Pop // List of values for the fields/attributes listed in 'ColFields', one for 122*5c87daf2SSebastian Pop // each column in the relation table. 123*5c87daf2SSebastian Pop // 124*5c87daf2SSebastian Pop // Ex: ValueCols = [['true'],['false']] -- it results two columns in the 125*5c87daf2SSebastian Pop // table. First column requires all the instructions to have predSense 126*5c87daf2SSebastian Pop // set to 'true' and second column requires it to be 'false'. 127*5c87daf2SSebastian Pop ListInit *ColValList = MapRec->getValueAsListInit("ValueCols"); 128*5c87daf2SSebastian Pop 129*5c87daf2SSebastian Pop // Each instruction map must specify at least one column for it to be valid. 130*5c87daf2SSebastian Pop if (ColValList->getSize() == 0) 131*5c87daf2SSebastian Pop throw "InstrMapping record `" + MapRec->getName() + "' has empty " + 132*5c87daf2SSebastian Pop "`ValueCols' field!"; 133*5c87daf2SSebastian Pop 134*5c87daf2SSebastian Pop for (unsigned i = 0, e = ColValList->getSize(); i < e; i++) { 135*5c87daf2SSebastian Pop ListInit *ColI = dyn_cast<ListInit>(ColValList->getElement(i)); 136*5c87daf2SSebastian Pop 137*5c87daf2SSebastian Pop // Make sure that all the sub-lists in 'ValueCols' have same number of 138*5c87daf2SSebastian Pop // elements as the fields in 'ColFields'. 139*5c87daf2SSebastian Pop if (ColI->getSize() == ColFields->getSize()) 140*5c87daf2SSebastian Pop ValueCols.push_back(ColI); 141*5c87daf2SSebastian Pop else { 142*5c87daf2SSebastian Pop throw "Record `" + MapRec->getName() + "', field `" + "ValueCols" + 143*5c87daf2SSebastian Pop "' entries don't match with the entries in 'ColFields'!"; 144*5c87daf2SSebastian Pop } 145*5c87daf2SSebastian Pop } 146*5c87daf2SSebastian Pop } 147*5c87daf2SSebastian Pop 148*5c87daf2SSebastian Pop std::string getName() const { 149*5c87daf2SSebastian Pop return Name; 150*5c87daf2SSebastian Pop } 151*5c87daf2SSebastian Pop 152*5c87daf2SSebastian Pop std::string getFilterClass() { 153*5c87daf2SSebastian Pop return FilterClass; 154*5c87daf2SSebastian Pop } 155*5c87daf2SSebastian Pop 156*5c87daf2SSebastian Pop ListInit *getRowFields() const { 157*5c87daf2SSebastian Pop return RowFields; 158*5c87daf2SSebastian Pop } 159*5c87daf2SSebastian Pop 160*5c87daf2SSebastian Pop ListInit *getColFields() const { 161*5c87daf2SSebastian Pop return ColFields; 162*5c87daf2SSebastian Pop } 163*5c87daf2SSebastian Pop 164*5c87daf2SSebastian Pop ListInit *getKeyCol() const { 165*5c87daf2SSebastian Pop return KeyCol; 166*5c87daf2SSebastian Pop } 167*5c87daf2SSebastian Pop 168*5c87daf2SSebastian Pop const std::vector<ListInit*> &getValueCols() const { 169*5c87daf2SSebastian Pop return ValueCols; 170*5c87daf2SSebastian Pop } 171*5c87daf2SSebastian Pop }; 172*5c87daf2SSebastian Pop } // End anonymous namespace. 173*5c87daf2SSebastian Pop 174*5c87daf2SSebastian Pop 175*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 176*5c87daf2SSebastian Pop // class MapTableEmitter : It builds the instruction relation maps using 177*5c87daf2SSebastian Pop // the information provided in InstrMapping records. It outputs these 178*5c87daf2SSebastian Pop // relationship maps as tables into XXXGenInstrInfo.inc file along with the 179*5c87daf2SSebastian Pop // functions to query them. 180*5c87daf2SSebastian Pop 181*5c87daf2SSebastian Pop namespace { 182*5c87daf2SSebastian Pop class MapTableEmitter { 183*5c87daf2SSebastian Pop private: 184*5c87daf2SSebastian Pop // std::string TargetName; 185*5c87daf2SSebastian Pop const CodeGenTarget &Target; 186*5c87daf2SSebastian Pop RecordKeeper &Records; 187*5c87daf2SSebastian Pop // InstrMapDesc - InstrMapping record to be processed. 188*5c87daf2SSebastian Pop InstrMap InstrMapDesc; 189*5c87daf2SSebastian Pop 190*5c87daf2SSebastian Pop // InstrDefs - list of instructions filtered using FilterClass defined 191*5c87daf2SSebastian Pop // in InstrMapDesc. 192*5c87daf2SSebastian Pop std::vector<Record*> InstrDefs; 193*5c87daf2SSebastian Pop 194*5c87daf2SSebastian Pop // RowInstrMap - maps RowFields values to the instructions. It's keyed by the 195*5c87daf2SSebastian Pop // values of the row fields and contains vector of records as values. 196*5c87daf2SSebastian Pop RowInstrMapTy RowInstrMap; 197*5c87daf2SSebastian Pop 198*5c87daf2SSebastian Pop // KeyInstrVec - list of key instructions. 199*5c87daf2SSebastian Pop std::vector<Record*> KeyInstrVec; 200*5c87daf2SSebastian Pop DenseMap<Record*, std::vector<Record*> > MapTable; 201*5c87daf2SSebastian Pop 202*5c87daf2SSebastian Pop public: 203*5c87daf2SSebastian Pop MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec): 204*5c87daf2SSebastian Pop Target(Target), Records(Records), InstrMapDesc(IMRec) { 205*5c87daf2SSebastian Pop const std::string FilterClass = InstrMapDesc.getFilterClass(); 206*5c87daf2SSebastian Pop InstrDefs = Records.getAllDerivedDefinitions(FilterClass); 207*5c87daf2SSebastian Pop }; 208*5c87daf2SSebastian Pop 209*5c87daf2SSebastian Pop void buildRowInstrMap(); 210*5c87daf2SSebastian Pop 211*5c87daf2SSebastian Pop // Returns true if an instruction is a key instruction, i.e., its ColFields 212*5c87daf2SSebastian Pop // have same values as KeyCol. 213*5c87daf2SSebastian Pop bool isKeyColInstr(Record* CurInstr); 214*5c87daf2SSebastian Pop 215*5c87daf2SSebastian Pop // Find column instruction corresponding to a key instruction based on the 216*5c87daf2SSebastian Pop // constraints for that column. 217*5c87daf2SSebastian Pop Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol); 218*5c87daf2SSebastian Pop 219*5c87daf2SSebastian Pop // Find column instructions for each key instruction based 220*5c87daf2SSebastian Pop // on ValueCols and store them into MapTable. 221*5c87daf2SSebastian Pop void buildMapTable(); 222*5c87daf2SSebastian Pop 223*5c87daf2SSebastian Pop void emitBinSearch(raw_ostream &OS, unsigned TableSize); 224*5c87daf2SSebastian Pop void emitTablesWithFunc(raw_ostream &OS); 225*5c87daf2SSebastian Pop unsigned emitBinSearchTable(raw_ostream &OS); 226*5c87daf2SSebastian Pop 227*5c87daf2SSebastian Pop // Lookup functions to query binary search tables. 228*5c87daf2SSebastian Pop void emitMapFuncBody(raw_ostream &OS, unsigned TableSize); 229*5c87daf2SSebastian Pop 230*5c87daf2SSebastian Pop }; 231*5c87daf2SSebastian Pop } // End anonymous namespace. 232*5c87daf2SSebastian Pop 233*5c87daf2SSebastian Pop 234*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 235*5c87daf2SSebastian Pop // Process all the instructions that model this relation (alreday present in 236*5c87daf2SSebastian Pop // InstrDefs) and insert them into RowInstrMap which is keyed by the values of 237*5c87daf2SSebastian Pop // the fields listed as RowFields. It stores vectors of records as values. 238*5c87daf2SSebastian Pop // All the related instructions have the same values for the RowFields thus are 239*5c87daf2SSebastian Pop // part of the same key-value pair. 240*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 241*5c87daf2SSebastian Pop 242*5c87daf2SSebastian Pop void MapTableEmitter::buildRowInstrMap() { 243*5c87daf2SSebastian Pop for (unsigned i = 0, e = InstrDefs.size(); i < e; i++) { 244*5c87daf2SSebastian Pop std::vector<Record*> InstrList; 245*5c87daf2SSebastian Pop Record *CurInstr = InstrDefs[i]; 246*5c87daf2SSebastian Pop std::vector<Init*> KeyValue; 247*5c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields(); 248*5c87daf2SSebastian Pop for (unsigned j = 0, endRF = RowFields->getSize(); j < endRF; j++) { 249*5c87daf2SSebastian Pop Init *RowFieldsJ = RowFields->getElement(j); 250*5c87daf2SSebastian Pop Init *CurInstrVal = CurInstr->getValue(RowFieldsJ)->getValue(); 251*5c87daf2SSebastian Pop KeyValue.push_back(CurInstrVal); 252*5c87daf2SSebastian Pop } 253*5c87daf2SSebastian Pop 254*5c87daf2SSebastian Pop // Collect key instructions into KeyInstrVec. Later, these instructions are 255*5c87daf2SSebastian Pop // processed to assign column position to the instructions sharing 256*5c87daf2SSebastian Pop // their KeyValue in RowInstrMap. 257*5c87daf2SSebastian Pop if (isKeyColInstr(CurInstr)) 258*5c87daf2SSebastian Pop KeyInstrVec.push_back(CurInstr); 259*5c87daf2SSebastian Pop 260*5c87daf2SSebastian Pop RowInstrMap[KeyValue].push_back(CurInstr); 261*5c87daf2SSebastian Pop } 262*5c87daf2SSebastian Pop } 263*5c87daf2SSebastian Pop 264*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 265*5c87daf2SSebastian Pop // Return true if an instruction is a KeyCol instruction. 266*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 267*5c87daf2SSebastian Pop 268*5c87daf2SSebastian Pop bool MapTableEmitter::isKeyColInstr(Record* CurInstr) { 269*5c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 270*5c87daf2SSebastian Pop ListInit *KeyCol = InstrMapDesc.getKeyCol(); 271*5c87daf2SSebastian Pop 272*5c87daf2SSebastian Pop // Check if the instruction is a KeyCol instruction. 273*5c87daf2SSebastian Pop bool MatchFound = true; 274*5c87daf2SSebastian Pop for (unsigned j = 0, endCF = ColFields->getSize(); 275*5c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) { 276*5c87daf2SSebastian Pop RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j)); 277*5c87daf2SSebastian Pop std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString(); 278*5c87daf2SSebastian Pop std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString(); 279*5c87daf2SSebastian Pop MatchFound = (CurInstrVal == KeyColValue); 280*5c87daf2SSebastian Pop } 281*5c87daf2SSebastian Pop return MatchFound; 282*5c87daf2SSebastian Pop } 283*5c87daf2SSebastian Pop 284*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 285*5c87daf2SSebastian Pop // Build a map to link key instructions with the column instructions arranged 286*5c87daf2SSebastian Pop // according to their column positions. 287*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 288*5c87daf2SSebastian Pop 289*5c87daf2SSebastian Pop void MapTableEmitter::buildMapTable() { 290*5c87daf2SSebastian Pop // Find column instructions for a given key based on the ColField 291*5c87daf2SSebastian Pop // constraints. 292*5c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 293*5c87daf2SSebastian Pop unsigned NumOfCols = ValueCols.size(); 294*5c87daf2SSebastian Pop for (unsigned j = 0, endKI = KeyInstrVec.size(); j < endKI; j++) { 295*5c87daf2SSebastian Pop Record *CurKeyInstr = KeyInstrVec[j]; 296*5c87daf2SSebastian Pop std::vector<Record*> ColInstrVec(NumOfCols); 297*5c87daf2SSebastian Pop 298*5c87daf2SSebastian Pop // Find the column instruction based on the constraints for the column. 299*5c87daf2SSebastian Pop for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) { 300*5c87daf2SSebastian Pop ListInit *CurValueCol = ValueCols[ColIdx]; 301*5c87daf2SSebastian Pop Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol); 302*5c87daf2SSebastian Pop ColInstrVec[ColIdx] = ColInstr; 303*5c87daf2SSebastian Pop } 304*5c87daf2SSebastian Pop MapTable[CurKeyInstr] = ColInstrVec; 305*5c87daf2SSebastian Pop } 306*5c87daf2SSebastian Pop } 307*5c87daf2SSebastian Pop 308*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 309*5c87daf2SSebastian Pop // Find column instruction based on the constraints for that column. 310*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 311*5c87daf2SSebastian Pop 312*5c87daf2SSebastian Pop Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr, 313*5c87daf2SSebastian Pop ListInit *CurValueCol) { 314*5c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields(); 315*5c87daf2SSebastian Pop std::vector<Init*> KeyValue; 316*5c87daf2SSebastian Pop 317*5c87daf2SSebastian Pop // Construct KeyValue using KeyInstr's values for RowFields. 318*5c87daf2SSebastian Pop for (unsigned j = 0, endRF = RowFields->getSize(); j < endRF; j++) { 319*5c87daf2SSebastian Pop Init *RowFieldsJ = RowFields->getElement(j); 320*5c87daf2SSebastian Pop Init *KeyInstrVal = KeyInstr->getValue(RowFieldsJ)->getValue(); 321*5c87daf2SSebastian Pop KeyValue.push_back(KeyInstrVal); 322*5c87daf2SSebastian Pop } 323*5c87daf2SSebastian Pop 324*5c87daf2SSebastian Pop // Get all the instructions that share the same KeyValue as the KeyInstr 325*5c87daf2SSebastian Pop // in RowInstrMap. We search through these instructions to find a match 326*5c87daf2SSebastian Pop // for the current column, i.e., the instruction which has the same values 327*5c87daf2SSebastian Pop // as CurValueCol for all the fields in ColFields. 328*5c87daf2SSebastian Pop const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue]; 329*5c87daf2SSebastian Pop 330*5c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 331*5c87daf2SSebastian Pop Record *MatchInstr = NULL; 332*5c87daf2SSebastian Pop 333*5c87daf2SSebastian Pop for (unsigned i = 0, e = RelatedInstrVec.size(); i < e; i++) { 334*5c87daf2SSebastian Pop bool MatchFound = true; 335*5c87daf2SSebastian Pop Record *CurInstr = RelatedInstrVec[i]; 336*5c87daf2SSebastian Pop for (unsigned j = 0, endCF = ColFields->getSize(); 337*5c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) { 338*5c87daf2SSebastian Pop Init *ColFieldJ = ColFields->getElement(j); 339*5c87daf2SSebastian Pop Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue(); 340*5c87daf2SSebastian Pop std::string CurInstrVal = CurInstrInit->getAsUnquotedString(); 341*5c87daf2SSebastian Pop Init *ColFieldJVallue = CurValueCol->getElement(j); 342*5c87daf2SSebastian Pop MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString()); 343*5c87daf2SSebastian Pop } 344*5c87daf2SSebastian Pop 345*5c87daf2SSebastian Pop if (MatchFound) { 346*5c87daf2SSebastian Pop if (MatchInstr) // Already had a match 347*5c87daf2SSebastian Pop // Error if multiple matches are found for a column. 348*5c87daf2SSebastian Pop throw "Multiple matches found for `" + KeyInstr->getName() + 349*5c87daf2SSebastian Pop "', for the relation `" + InstrMapDesc.getName(); 350*5c87daf2SSebastian Pop else 351*5c87daf2SSebastian Pop MatchInstr = CurInstr; 352*5c87daf2SSebastian Pop } 353*5c87daf2SSebastian Pop } 354*5c87daf2SSebastian Pop return MatchInstr; 355*5c87daf2SSebastian Pop } 356*5c87daf2SSebastian Pop 357*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 358*5c87daf2SSebastian Pop // Emit one table per relation. Only instructions with a valid relation of a 359*5c87daf2SSebastian Pop // given type are included in the table sorted by their enum values (opcodes). 360*5c87daf2SSebastian Pop // Binary search is used for locating instructions in the table. 361*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 362*5c87daf2SSebastian Pop 363*5c87daf2SSebastian Pop unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) { 364*5c87daf2SSebastian Pop 365*5c87daf2SSebastian Pop const std::vector<const CodeGenInstruction*> &NumberedInstructions = 366*5c87daf2SSebastian Pop Target.getInstructionsByEnumValue(); 367*5c87daf2SSebastian Pop std::string TargetName = Target.getName(); 368*5c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 369*5c87daf2SSebastian Pop unsigned NumCol = ValueCols.size(); 370*5c87daf2SSebastian Pop unsigned TotalNumInstr = NumberedInstructions.size(); 371*5c87daf2SSebastian Pop unsigned TableSize = 0; 372*5c87daf2SSebastian Pop 373*5c87daf2SSebastian Pop OS << "static const uint16_t "<<InstrMapDesc.getName(); 374*5c87daf2SSebastian Pop // Number of columns in the table are NumCol+1 because key instructions are 375*5c87daf2SSebastian Pop // emitted as first column. 376*5c87daf2SSebastian Pop OS << "Table[]["<< NumCol+1 << "] = {\n"; 377*5c87daf2SSebastian Pop for (unsigned i = 0; i < TotalNumInstr; i++) { 378*5c87daf2SSebastian Pop Record *CurInstr = NumberedInstructions[i]->TheDef; 379*5c87daf2SSebastian Pop std::vector<Record*> ColInstrs = MapTable[CurInstr]; 380*5c87daf2SSebastian Pop std::string OutStr(""); 381*5c87daf2SSebastian Pop unsigned RelExists = 0; 382*5c87daf2SSebastian Pop if (ColInstrs.size()) { 383*5c87daf2SSebastian Pop for (unsigned j = 0; j < NumCol; j++) { 384*5c87daf2SSebastian Pop if (ColInstrs[j] != NULL) { 385*5c87daf2SSebastian Pop RelExists = 1; 386*5c87daf2SSebastian Pop OutStr += ", "; 387*5c87daf2SSebastian Pop OutStr += TargetName; 388*5c87daf2SSebastian Pop OutStr += "::"; 389*5c87daf2SSebastian Pop OutStr += ColInstrs[j]->getName(); 390*5c87daf2SSebastian Pop } else { OutStr += ", -1";} 391*5c87daf2SSebastian Pop } 392*5c87daf2SSebastian Pop 393*5c87daf2SSebastian Pop if (RelExists) { 394*5c87daf2SSebastian Pop OS << " { " << TargetName << "::" << CurInstr->getName(); 395*5c87daf2SSebastian Pop OS << OutStr <<" },\n"; 396*5c87daf2SSebastian Pop TableSize++; 397*5c87daf2SSebastian Pop } 398*5c87daf2SSebastian Pop } 399*5c87daf2SSebastian Pop } 400*5c87daf2SSebastian Pop if (!TableSize) { 401*5c87daf2SSebastian Pop OS << " { " << TargetName << "::" << "INSTRUCTION_LIST_END, "; 402*5c87daf2SSebastian Pop OS << TargetName << "::" << "INSTRUCTION_LIST_END }"; 403*5c87daf2SSebastian Pop } 404*5c87daf2SSebastian Pop OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n"; 405*5c87daf2SSebastian Pop return TableSize; 406*5c87daf2SSebastian Pop } 407*5c87daf2SSebastian Pop 408*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 409*5c87daf2SSebastian Pop // Emit binary search algorithm as part of the functions used to query 410*5c87daf2SSebastian Pop // relation tables. 411*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 412*5c87daf2SSebastian Pop 413*5c87daf2SSebastian Pop void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) { 414*5c87daf2SSebastian Pop OS << " unsigned mid;\n"; 415*5c87daf2SSebastian Pop OS << " unsigned start = 0;\n"; 416*5c87daf2SSebastian Pop OS << " unsigned end = " << TableSize << ";\n"; 417*5c87daf2SSebastian Pop OS << " while (start < end) {\n"; 418*5c87daf2SSebastian Pop OS << " mid = start + (end - start)/2;\n"; 419*5c87daf2SSebastian Pop OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n"; 420*5c87daf2SSebastian Pop OS << " break;\n"; 421*5c87daf2SSebastian Pop OS << " }\n"; 422*5c87daf2SSebastian Pop OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n"; 423*5c87daf2SSebastian Pop OS << " end = mid;\n"; 424*5c87daf2SSebastian Pop OS << " else\n"; 425*5c87daf2SSebastian Pop OS << " start = mid + 1;\n"; 426*5c87daf2SSebastian Pop OS << " }\n"; 427*5c87daf2SSebastian Pop OS << " if (start == end)\n"; 428*5c87daf2SSebastian Pop OS << " return -1; // Instruction doesn't exist in this table.\n\n"; 429*5c87daf2SSebastian Pop } 430*5c87daf2SSebastian Pop 431*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 432*5c87daf2SSebastian Pop // Emit functions to query relation tables. 433*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 434*5c87daf2SSebastian Pop 435*5c87daf2SSebastian Pop void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, 436*5c87daf2SSebastian Pop unsigned TableSize) { 437*5c87daf2SSebastian Pop 438*5c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 439*5c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 440*5c87daf2SSebastian Pop 441*5c87daf2SSebastian Pop // Emit binary search algorithm to locate instructions in the 442*5c87daf2SSebastian Pop // relation table. If found, return opcode value from the appropriate column 443*5c87daf2SSebastian Pop // of the table. 444*5c87daf2SSebastian Pop emitBinSearch(OS, TableSize); 445*5c87daf2SSebastian Pop 446*5c87daf2SSebastian Pop if (ValueCols.size() > 1) { 447*5c87daf2SSebastian Pop for (unsigned i = 0, e = ValueCols.size(); i < e; i++) { 448*5c87daf2SSebastian Pop ListInit *ColumnI = ValueCols[i]; 449*5c87daf2SSebastian Pop for (unsigned j = 0, ColSize = ColumnI->getSize(); j < ColSize; j++) { 450*5c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString(); 451*5c87daf2SSebastian Pop OS << " if (in" << ColName; 452*5c87daf2SSebastian Pop OS << " == "; 453*5c87daf2SSebastian Pop OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString(); 454*5c87daf2SSebastian Pop if (j < ColumnI->getSize() - 1) OS << " && "; 455*5c87daf2SSebastian Pop else OS << ")\n"; 456*5c87daf2SSebastian Pop } 457*5c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName(); 458*5c87daf2SSebastian Pop OS << "Table[mid]["<<i+1<<"];\n"; 459*5c87daf2SSebastian Pop } 460*5c87daf2SSebastian Pop OS << " return -1;"; 461*5c87daf2SSebastian Pop } 462*5c87daf2SSebastian Pop else 463*5c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n"; 464*5c87daf2SSebastian Pop 465*5c87daf2SSebastian Pop OS <<"}\n\n"; 466*5c87daf2SSebastian Pop } 467*5c87daf2SSebastian Pop 468*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 469*5c87daf2SSebastian Pop // Emit relation tables and the functions to query them. 470*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 471*5c87daf2SSebastian Pop 472*5c87daf2SSebastian Pop void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) { 473*5c87daf2SSebastian Pop 474*5c87daf2SSebastian Pop // Emit function name and the input parameters : mostly opcode value of the 475*5c87daf2SSebastian Pop // current instruction. However, if a table has multiple columns (more than 2 476*5c87daf2SSebastian Pop // since first column is used for the key instructions), then we also need 477*5c87daf2SSebastian Pop // to pass another input to indicate the column to be selected. 478*5c87daf2SSebastian Pop 479*5c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields(); 480*5c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols(); 481*5c87daf2SSebastian Pop OS << "// "<< InstrMapDesc.getName() << "\n"; 482*5c87daf2SSebastian Pop OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode"; 483*5c87daf2SSebastian Pop if (ValueCols.size() > 1) { 484*5c87daf2SSebastian Pop for (unsigned i = 0, e = ColFields->getSize(); i < e; i++) { 485*5c87daf2SSebastian Pop std::string ColName = ColFields->getElement(i)->getAsUnquotedString(); 486*5c87daf2SSebastian Pop OS << ", enum " << ColName << " in" << ColName << ") {\n"; 487*5c87daf2SSebastian Pop } 488*5c87daf2SSebastian Pop } else { OS << ") {\n"; } 489*5c87daf2SSebastian Pop 490*5c87daf2SSebastian Pop // Emit map table. 491*5c87daf2SSebastian Pop unsigned TableSize = emitBinSearchTable(OS); 492*5c87daf2SSebastian Pop 493*5c87daf2SSebastian Pop // Emit rest of the function body. 494*5c87daf2SSebastian Pop emitMapFuncBody(OS, TableSize); 495*5c87daf2SSebastian Pop } 496*5c87daf2SSebastian Pop 497*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 498*5c87daf2SSebastian Pop // Emit enums for the column fields across all the instruction maps. 499*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 500*5c87daf2SSebastian Pop 501*5c87daf2SSebastian Pop static void emitEnums(raw_ostream &OS, RecordKeeper &Records) { 502*5c87daf2SSebastian Pop 503*5c87daf2SSebastian Pop std::vector<Record*> InstrMapVec; 504*5c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping"); 505*5c87daf2SSebastian Pop std::map<std::string, std::vector<Init*> > ColFieldValueMap; 506*5c87daf2SSebastian Pop 507*5c87daf2SSebastian Pop // Iterate over all InstrMapping records and create a map between column 508*5c87daf2SSebastian Pop // fields and their possible values across all records. 509*5c87daf2SSebastian Pop for (unsigned i = 0, e = InstrMapVec.size(); i < e; i++) { 510*5c87daf2SSebastian Pop Record *CurMap = InstrMapVec[i]; 511*5c87daf2SSebastian Pop ListInit *ColFields; 512*5c87daf2SSebastian Pop ColFields = CurMap->getValueAsListInit("ColFields"); 513*5c87daf2SSebastian Pop ListInit *List = CurMap->getValueAsListInit("ValueCols"); 514*5c87daf2SSebastian Pop std::vector<ListInit*> ValueCols; 515*5c87daf2SSebastian Pop unsigned ListSize = List->getSize(); 516*5c87daf2SSebastian Pop 517*5c87daf2SSebastian Pop for (unsigned j = 0; j < ListSize; j++) { 518*5c87daf2SSebastian Pop ListInit *ListJ = dyn_cast<ListInit>(List->getElement(j)); 519*5c87daf2SSebastian Pop 520*5c87daf2SSebastian Pop if (ListJ->getSize() != ColFields->getSize()) { 521*5c87daf2SSebastian Pop throw "Record `" + CurMap->getName() + "', field `" + "ValueCols" + 522*5c87daf2SSebastian Pop "' entries don't match with the entries in 'ColFields' !"; 523*5c87daf2SSebastian Pop } 524*5c87daf2SSebastian Pop ValueCols.push_back(ListJ); 525*5c87daf2SSebastian Pop } 526*5c87daf2SSebastian Pop 527*5c87daf2SSebastian Pop for (unsigned j = 0, endCF = ColFields->getSize(); j < endCF; j++) { 528*5c87daf2SSebastian Pop for (unsigned k = 0; k < ListSize; k++){ 529*5c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString(); 530*5c87daf2SSebastian Pop ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j)); 531*5c87daf2SSebastian Pop } 532*5c87daf2SSebastian Pop } 533*5c87daf2SSebastian Pop } 534*5c87daf2SSebastian Pop 535*5c87daf2SSebastian Pop for (std::map<std::string, std::vector<Init*> >::iterator 536*5c87daf2SSebastian Pop II = ColFieldValueMap.begin(), IE = ColFieldValueMap.end(); 537*5c87daf2SSebastian Pop II != IE; II++) { 538*5c87daf2SSebastian Pop std::vector<Init*> FieldValues = (*II).second; 539*5c87daf2SSebastian Pop unsigned FieldSize = FieldValues.size(); 540*5c87daf2SSebastian Pop 541*5c87daf2SSebastian Pop // Delete duplicate entries from ColFieldValueMap 542*5c87daf2SSebastian Pop for (unsigned i = 0; i < FieldSize - 1; i++) { 543*5c87daf2SSebastian Pop Init *CurVal = FieldValues[i]; 544*5c87daf2SSebastian Pop for (unsigned j = i+1; j < FieldSize; j++) { 545*5c87daf2SSebastian Pop if (CurVal == FieldValues[j]) { 546*5c87daf2SSebastian Pop FieldValues.erase(FieldValues.begin()+j); 547*5c87daf2SSebastian Pop } 548*5c87daf2SSebastian Pop } 549*5c87daf2SSebastian Pop } 550*5c87daf2SSebastian Pop 551*5c87daf2SSebastian Pop // Emit enumerated values for the column fields. 552*5c87daf2SSebastian Pop OS << "enum " << (*II).first << " {\n"; 553*5c87daf2SSebastian Pop for (unsigned i = 0; i < FieldSize; i++) { 554*5c87daf2SSebastian Pop OS << "\t" << (*II).first << "_" << FieldValues[i]->getAsUnquotedString(); 555*5c87daf2SSebastian Pop if (i != FieldValues.size() - 1) 556*5c87daf2SSebastian Pop OS << ",\n"; 557*5c87daf2SSebastian Pop else 558*5c87daf2SSebastian Pop OS << "\n};\n\n"; 559*5c87daf2SSebastian Pop } 560*5c87daf2SSebastian Pop } 561*5c87daf2SSebastian Pop } 562*5c87daf2SSebastian Pop 563*5c87daf2SSebastian Pop namespace llvm { 564*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 565*5c87daf2SSebastian Pop // Parse 'InstrMapping' records and use the information to form relationship 566*5c87daf2SSebastian Pop // between instructions. These relations are emitted as a tables along with the 567*5c87daf2SSebastian Pop // functions to query them. 568*5c87daf2SSebastian Pop //===----------------------------------------------------------------------===// 569*5c87daf2SSebastian Pop void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) { 570*5c87daf2SSebastian Pop CodeGenTarget Target(Records); 571*5c87daf2SSebastian Pop std::string TargetName = Target.getName(); 572*5c87daf2SSebastian Pop std::vector<Record*> InstrMapVec; 573*5c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping"); 574*5c87daf2SSebastian Pop 575*5c87daf2SSebastian Pop if (!InstrMapVec.size()) 576*5c87daf2SSebastian Pop return; 577*5c87daf2SSebastian Pop 578*5c87daf2SSebastian Pop OS << "#ifdef GET_INSTRMAP_INFO\n"; 579*5c87daf2SSebastian Pop OS << "#undef GET_INSTRMAP_INFO\n"; 580*5c87daf2SSebastian Pop OS << "namespace llvm {\n\n"; 581*5c87daf2SSebastian Pop OS << "namespace " << TargetName << " {\n\n"; 582*5c87daf2SSebastian Pop 583*5c87daf2SSebastian Pop // Emit coulumn field names and their values as enums. 584*5c87daf2SSebastian Pop emitEnums(OS, Records); 585*5c87daf2SSebastian Pop 586*5c87daf2SSebastian Pop // Iterate over all instruction mapping records and construct relationship 587*5c87daf2SSebastian Pop // maps based on the information specified there. 588*5c87daf2SSebastian Pop // 589*5c87daf2SSebastian Pop for (unsigned i = 0, e = InstrMapVec.size(); i < e; i++) { 590*5c87daf2SSebastian Pop MapTableEmitter IMap(Target, Records, InstrMapVec[i]); 591*5c87daf2SSebastian Pop 592*5c87daf2SSebastian Pop // Build RowInstrMap to group instructions based on their values for 593*5c87daf2SSebastian Pop // RowFields. In the process, also collect key instructions into 594*5c87daf2SSebastian Pop // KeyInstrVec. 595*5c87daf2SSebastian Pop IMap.buildRowInstrMap(); 596*5c87daf2SSebastian Pop 597*5c87daf2SSebastian Pop // Build MapTable to map key instructions with the corresponding column 598*5c87daf2SSebastian Pop // instructions. 599*5c87daf2SSebastian Pop IMap.buildMapTable(); 600*5c87daf2SSebastian Pop 601*5c87daf2SSebastian Pop // Emit map tables and the functions to query them. 602*5c87daf2SSebastian Pop IMap.emitTablesWithFunc(OS); 603*5c87daf2SSebastian Pop } 604*5c87daf2SSebastian Pop OS << "} // End " << TargetName << " namespace\n"; 605*5c87daf2SSebastian Pop OS << "} // End llvm namespace\n"; 606*5c87daf2SSebastian Pop OS << "#endif // GET_INSTRMAP_INFO\n\n"; 607*5c87daf2SSebastian Pop } 608*5c87daf2SSebastian Pop 609*5c87daf2SSebastian Pop } // End llvm namespace 610