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