15c87daf2SSebastian Pop //===- CodeGenMapTable.cpp - Instruction Mapping Table Generator ----------===//
25c87daf2SSebastian Pop //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65c87daf2SSebastian Pop //
75c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
80d02aa6eSShivam Gupta // CodeGenMapTable provides functionality for the TableGen 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
78*fbbc41f8Sserge-sans-paille #include "CodeGenInstruction.h"
795c87daf2SSebastian Pop #include "CodeGenTarget.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:
InstrMap(Record * MapRec)1005c87daf2SSebastian Pop InstrMap(Record* MapRec) {
101adcd0268SBenjamin Kramer Name = std::string(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()) {
135c191c243SSimon Pilgrim auto *ColI = 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
getName() const147bd343d26SSimon Pilgrim const std::string &getName() const { return Name; }
1485c87daf2SSebastian Pop
getFilterClass() const149bd343d26SSimon Pilgrim const std::string &getFilterClass() const { return FilterClass; }
1505c87daf2SSebastian Pop
getRowFields() const151b9890ae1SSimon Pilgrim ListInit *getRowFields() const { return RowFields; }
1525c87daf2SSebastian Pop
getColFields() const153b9890ae1SSimon Pilgrim ListInit *getColFields() const { return ColFields; }
1545c87daf2SSebastian Pop
getKeyCol() const155b9890ae1SSimon Pilgrim ListInit *getKeyCol() const { return KeyCol; }
1565c87daf2SSebastian Pop
getValueCols() const1575c87daf2SSebastian Pop const std::vector<ListInit*> &getValueCols() const {
1585c87daf2SSebastian Pop return ValueCols;
1595c87daf2SSebastian Pop }
1605c87daf2SSebastian Pop };
1616bd3a9eaSBjorn Pettersson } // end anonymous namespace
1625c87daf2SSebastian Pop
1635c87daf2SSebastian Pop
1645c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
1655c87daf2SSebastian Pop // class MapTableEmitter : It builds the instruction relation maps using
1665c87daf2SSebastian Pop // the information provided in InstrMapping records. It outputs these
1675c87daf2SSebastian Pop // relationship maps as tables into XXXGenInstrInfo.inc file along with the
1685c87daf2SSebastian Pop // functions to query them.
1695c87daf2SSebastian Pop
1705c87daf2SSebastian Pop namespace {
1715c87daf2SSebastian Pop class MapTableEmitter {
1725c87daf2SSebastian Pop private:
1735c87daf2SSebastian Pop // std::string TargetName;
1745c87daf2SSebastian Pop const CodeGenTarget &Target;
1755c87daf2SSebastian Pop // InstrMapDesc - InstrMapping record to be processed.
1765c87daf2SSebastian Pop InstrMap InstrMapDesc;
1775c87daf2SSebastian Pop
1785c87daf2SSebastian Pop // InstrDefs - list of instructions filtered using FilterClass defined
1795c87daf2SSebastian Pop // in InstrMapDesc.
1805c87daf2SSebastian Pop std::vector<Record*> InstrDefs;
1815c87daf2SSebastian Pop
1825c87daf2SSebastian Pop // RowInstrMap - maps RowFields values to the instructions. It's keyed by the
1835c87daf2SSebastian Pop // values of the row fields and contains vector of records as values.
1845c87daf2SSebastian Pop RowInstrMapTy RowInstrMap;
1855c87daf2SSebastian Pop
1865c87daf2SSebastian Pop // KeyInstrVec - list of key instructions.
1875c87daf2SSebastian Pop std::vector<Record*> KeyInstrVec;
1885c87daf2SSebastian Pop DenseMap<Record*, std::vector<Record*> > MapTable;
1895c87daf2SSebastian Pop
1905c87daf2SSebastian Pop public:
MapTableEmitter(CodeGenTarget & Target,RecordKeeper & Records,Record * IMRec)1915c87daf2SSebastian Pop MapTableEmitter(CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec):
192dcbd1608SDavid Blaikie Target(Target), InstrMapDesc(IMRec) {
193bd343d26SSimon Pilgrim const std::string &FilterClass = InstrMapDesc.getFilterClass();
1945c87daf2SSebastian Pop InstrDefs = Records.getAllDerivedDefinitions(FilterClass);
195dcbd1608SDavid Blaikie }
1965c87daf2SSebastian Pop
1975c87daf2SSebastian Pop void buildRowInstrMap();
1985c87daf2SSebastian Pop
1995c87daf2SSebastian Pop // Returns true if an instruction is a key instruction, i.e., its ColFields
2005c87daf2SSebastian Pop // have same values as KeyCol.
2015c87daf2SSebastian Pop bool isKeyColInstr(Record* CurInstr);
2025c87daf2SSebastian Pop
2035c87daf2SSebastian Pop // Find column instruction corresponding to a key instruction based on the
2045c87daf2SSebastian Pop // constraints for that column.
2055c87daf2SSebastian Pop Record *getInstrForColumn(Record *KeyInstr, ListInit *CurValueCol);
2065c87daf2SSebastian Pop
2075c87daf2SSebastian Pop // Find column instructions for each key instruction based
2085c87daf2SSebastian Pop // on ValueCols and store them into MapTable.
2095c87daf2SSebastian Pop void buildMapTable();
2105c87daf2SSebastian Pop
2115c87daf2SSebastian Pop void emitBinSearch(raw_ostream &OS, unsigned TableSize);
2125c87daf2SSebastian Pop void emitTablesWithFunc(raw_ostream &OS);
2135c87daf2SSebastian Pop unsigned emitBinSearchTable(raw_ostream &OS);
2145c87daf2SSebastian Pop
2155c87daf2SSebastian Pop // Lookup functions to query binary search tables.
2165c87daf2SSebastian Pop void emitMapFuncBody(raw_ostream &OS, unsigned TableSize);
2175c87daf2SSebastian Pop
2185c87daf2SSebastian Pop };
2196bd3a9eaSBjorn Pettersson } // end anonymous namespace
2205c87daf2SSebastian Pop
2215c87daf2SSebastian Pop
2225c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2235c87daf2SSebastian Pop // Process all the instructions that model this relation (alreday present in
2245c87daf2SSebastian Pop // InstrDefs) and insert them into RowInstrMap which is keyed by the values of
2255c87daf2SSebastian Pop // the fields listed as RowFields. It stores vectors of records as values.
2265c87daf2SSebastian Pop // All the related instructions have the same values for the RowFields thus are
2275c87daf2SSebastian Pop // part of the same key-value pair.
2285c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2295c87daf2SSebastian Pop
buildRowInstrMap()2305c87daf2SSebastian Pop void MapTableEmitter::buildRowInstrMap() {
231ef0578a8SCraig Topper for (Record *CurInstr : InstrDefs) {
2325c87daf2SSebastian Pop std::vector<Init*> KeyValue;
2335c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields();
234ef0578a8SCraig Topper for (Init *RowField : RowFields->getValues()) {
235f02ad15fSAleksandar Beserminji RecordVal *RecVal = CurInstr->getValue(RowField);
236f02ad15fSAleksandar Beserminji if (RecVal == nullptr)
237f02ad15fSAleksandar Beserminji PrintFatalError(CurInstr->getLoc(), "No value " +
238f02ad15fSAleksandar Beserminji RowField->getAsString() + " found in \"" +
239f02ad15fSAleksandar Beserminji CurInstr->getName() + "\" instruction description.");
240f02ad15fSAleksandar Beserminji Init *CurInstrVal = RecVal->getValue();
2415c87daf2SSebastian Pop KeyValue.push_back(CurInstrVal);
2425c87daf2SSebastian Pop }
2435c87daf2SSebastian Pop
2445c87daf2SSebastian Pop // Collect key instructions into KeyInstrVec. Later, these instructions are
2455c87daf2SSebastian Pop // processed to assign column position to the instructions sharing
2465c87daf2SSebastian Pop // their KeyValue in RowInstrMap.
2475c87daf2SSebastian Pop if (isKeyColInstr(CurInstr))
2485c87daf2SSebastian Pop KeyInstrVec.push_back(CurInstr);
2495c87daf2SSebastian Pop
2505c87daf2SSebastian Pop RowInstrMap[KeyValue].push_back(CurInstr);
2515c87daf2SSebastian Pop }
2525c87daf2SSebastian Pop }
2535c87daf2SSebastian Pop
2545c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2555c87daf2SSebastian Pop // Return true if an instruction is a KeyCol instruction.
2565c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2575c87daf2SSebastian Pop
isKeyColInstr(Record * CurInstr)2585c87daf2SSebastian Pop bool MapTableEmitter::isKeyColInstr(Record* CurInstr) {
2595c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields();
2605c87daf2SSebastian Pop ListInit *KeyCol = InstrMapDesc.getKeyCol();
2615c87daf2SSebastian Pop
2625c87daf2SSebastian Pop // Check if the instruction is a KeyCol instruction.
2635c87daf2SSebastian Pop bool MatchFound = true;
264664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size();
2655c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) {
2665c87daf2SSebastian Pop RecordVal *ColFieldName = CurInstr->getValue(ColFields->getElement(j));
2675c87daf2SSebastian Pop std::string CurInstrVal = ColFieldName->getValue()->getAsUnquotedString();
2685c87daf2SSebastian Pop std::string KeyColValue = KeyCol->getElement(j)->getAsUnquotedString();
2695c87daf2SSebastian Pop MatchFound = (CurInstrVal == KeyColValue);
2705c87daf2SSebastian Pop }
2715c87daf2SSebastian Pop return MatchFound;
2725c87daf2SSebastian Pop }
2735c87daf2SSebastian Pop
2745c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2755c87daf2SSebastian Pop // Build a map to link key instructions with the column instructions arranged
2765c87daf2SSebastian Pop // according to their column positions.
2775c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2785c87daf2SSebastian Pop
buildMapTable()2795c87daf2SSebastian Pop void MapTableEmitter::buildMapTable() {
2805c87daf2SSebastian Pop // Find column instructions for a given key based on the ColField
2815c87daf2SSebastian Pop // constraints.
2825c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
2835c87daf2SSebastian Pop unsigned NumOfCols = ValueCols.size();
284ef0578a8SCraig Topper for (Record *CurKeyInstr : KeyInstrVec) {
2855c87daf2SSebastian Pop std::vector<Record*> ColInstrVec(NumOfCols);
2865c87daf2SSebastian Pop
2875c87daf2SSebastian Pop // Find the column instruction based on the constraints for the column.
2885c87daf2SSebastian Pop for (unsigned ColIdx = 0; ColIdx < NumOfCols; ColIdx++) {
2895c87daf2SSebastian Pop ListInit *CurValueCol = ValueCols[ColIdx];
2905c87daf2SSebastian Pop Record *ColInstr = getInstrForColumn(CurKeyInstr, CurValueCol);
2915c87daf2SSebastian Pop ColInstrVec[ColIdx] = ColInstr;
2925c87daf2SSebastian Pop }
2935c87daf2SSebastian Pop MapTable[CurKeyInstr] = ColInstrVec;
2945c87daf2SSebastian Pop }
2955c87daf2SSebastian Pop }
2965c87daf2SSebastian Pop
2975c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
2985c87daf2SSebastian Pop // Find column instruction based on the constraints for that column.
2995c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3005c87daf2SSebastian Pop
getInstrForColumn(Record * KeyInstr,ListInit * CurValueCol)3015c87daf2SSebastian Pop Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
3025c87daf2SSebastian Pop ListInit *CurValueCol) {
3035c87daf2SSebastian Pop ListInit *RowFields = InstrMapDesc.getRowFields();
3045c87daf2SSebastian Pop std::vector<Init*> KeyValue;
3055c87daf2SSebastian Pop
3065c87daf2SSebastian Pop // Construct KeyValue using KeyInstr's values for RowFields.
307ef0578a8SCraig Topper for (Init *RowField : RowFields->getValues()) {
308ef0578a8SCraig Topper Init *KeyInstrVal = KeyInstr->getValue(RowField)->getValue();
3095c87daf2SSebastian Pop KeyValue.push_back(KeyInstrVal);
3105c87daf2SSebastian Pop }
3115c87daf2SSebastian Pop
3125c87daf2SSebastian Pop // Get all the instructions that share the same KeyValue as the KeyInstr
3135c87daf2SSebastian Pop // in RowInstrMap. We search through these instructions to find a match
3145c87daf2SSebastian Pop // for the current column, i.e., the instruction which has the same values
3155c87daf2SSebastian Pop // as CurValueCol for all the fields in ColFields.
3165c87daf2SSebastian Pop const std::vector<Record*> &RelatedInstrVec = RowInstrMap[KeyValue];
3175c87daf2SSebastian Pop
3185c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields();
31924064771SCraig Topper Record *MatchInstr = nullptr;
3205c87daf2SSebastian Pop
3210168da30SSimon Pilgrim for (llvm::Record *CurInstr : RelatedInstrVec) {
3225c87daf2SSebastian Pop bool MatchFound = true;
323664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size();
3245c87daf2SSebastian Pop (j < endCF) && MatchFound; j++) {
3255c87daf2SSebastian Pop Init *ColFieldJ = ColFields->getElement(j);
3265c87daf2SSebastian Pop Init *CurInstrInit = CurInstr->getValue(ColFieldJ)->getValue();
3275c87daf2SSebastian Pop std::string CurInstrVal = CurInstrInit->getAsUnquotedString();
3285c87daf2SSebastian Pop Init *ColFieldJVallue = CurValueCol->getElement(j);
3295c87daf2SSebastian Pop MatchFound = (CurInstrVal == ColFieldJVallue->getAsUnquotedString());
3305c87daf2SSebastian Pop }
3315c87daf2SSebastian Pop
3325c87daf2SSebastian Pop if (MatchFound) {
333411fbbf5SNicolai Haehnle if (MatchInstr) {
334411fbbf5SNicolai Haehnle // Already had a match
3355c87daf2SSebastian Pop // Error if multiple matches are found for a column.
336411fbbf5SNicolai Haehnle std::string KeyValueStr;
337411fbbf5SNicolai Haehnle for (Init *Value : KeyValue) {
338411fbbf5SNicolai Haehnle if (!KeyValueStr.empty())
339411fbbf5SNicolai Haehnle KeyValueStr += ", ";
340411fbbf5SNicolai Haehnle KeyValueStr += Value->getAsString();
341411fbbf5SNicolai Haehnle }
342411fbbf5SNicolai Haehnle
343635debe8SJoerg Sonnenberger PrintFatalError("Multiple matches found for `" + KeyInstr->getName() +
3440168da30SSimon Pilgrim "', for the relation `" + InstrMapDesc.getName() +
3450168da30SSimon Pilgrim "', row fields [" + KeyValueStr + "], column `" +
3460168da30SSimon Pilgrim CurValueCol->getAsString() + "'");
347411fbbf5SNicolai Haehnle }
3485c87daf2SSebastian Pop MatchInstr = CurInstr;
3495c87daf2SSebastian Pop }
3505c87daf2SSebastian Pop }
3515c87daf2SSebastian Pop return MatchInstr;
3525c87daf2SSebastian Pop }
3535c87daf2SSebastian Pop
3545c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3555c87daf2SSebastian Pop // Emit one table per relation. Only instructions with a valid relation of a
3565c87daf2SSebastian Pop // given type are included in the table sorted by their enum values (opcodes).
3575c87daf2SSebastian Pop // Binary search is used for locating instructions in the table.
3585c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
3595c87daf2SSebastian Pop
emitBinSearchTable(raw_ostream & OS)3605c87daf2SSebastian Pop unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
3615c87daf2SSebastian Pop
36228851b62SCraig Topper ArrayRef<const CodeGenInstruction*> NumberedInstructions =
3635c87daf2SSebastian Pop Target.getInstructionsByEnumValue();
36486a9aee8SCraig Topper StringRef Namespace = Target.getInstNamespace();
3655c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
3665c87daf2SSebastian Pop unsigned NumCol = ValueCols.size();
3675c87daf2SSebastian Pop unsigned TotalNumInstr = NumberedInstructions.size();
3685c87daf2SSebastian Pop unsigned TableSize = 0;
3695c87daf2SSebastian Pop
3705c87daf2SSebastian Pop OS << "static const uint16_t "<<InstrMapDesc.getName();
3715c87daf2SSebastian Pop // Number of columns in the table are NumCol+1 because key instructions are
3725c87daf2SSebastian Pop // emitted as first column.
3735c87daf2SSebastian Pop OS << "Table[]["<< NumCol+1 << "] = {\n";
3745c87daf2SSebastian Pop for (unsigned i = 0; i < TotalNumInstr; i++) {
3755c87daf2SSebastian Pop Record *CurInstr = NumberedInstructions[i]->TheDef;
3765c87daf2SSebastian Pop std::vector<Record*> ColInstrs = MapTable[CurInstr];
37712fc9ca3SKazu Hirata std::string OutStr;
3785c87daf2SSebastian Pop unsigned RelExists = 0;
3798c0809c7SAlexander Kornienko if (!ColInstrs.empty()) {
3805c87daf2SSebastian Pop for (unsigned j = 0; j < NumCol; j++) {
38124064771SCraig Topper if (ColInstrs[j] != nullptr) {
3825c87daf2SSebastian Pop RelExists = 1;
3835c87daf2SSebastian Pop OutStr += ", ";
384bb6cf737SKarl-Johan Karlsson OutStr += Namespace;
3855c87daf2SSebastian Pop OutStr += "::";
3865c87daf2SSebastian Pop OutStr += ColInstrs[j]->getName();
3870a7a17dfSBenjamin Kramer } else { OutStr += ", (uint16_t)-1U";}
3885c87daf2SSebastian Pop }
3895c87daf2SSebastian Pop
3905c87daf2SSebastian Pop if (RelExists) {
391bb6cf737SKarl-Johan Karlsson OS << " { " << Namespace << "::" << CurInstr->getName();
3925c87daf2SSebastian Pop OS << OutStr <<" },\n";
3935c87daf2SSebastian Pop TableSize++;
3945c87daf2SSebastian Pop }
3955c87daf2SSebastian Pop }
3965c87daf2SSebastian Pop }
3975c87daf2SSebastian Pop if (!TableSize) {
398bb6cf737SKarl-Johan Karlsson OS << " { " << Namespace << "::" << "INSTRUCTION_LIST_END, ";
399bb6cf737SKarl-Johan Karlsson OS << Namespace << "::" << "INSTRUCTION_LIST_END }";
4005c87daf2SSebastian Pop }
4015c87daf2SSebastian Pop OS << "}; // End of " << InstrMapDesc.getName() << "Table\n\n";
4025c87daf2SSebastian Pop return TableSize;
4035c87daf2SSebastian Pop }
4045c87daf2SSebastian Pop
4055c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4065c87daf2SSebastian Pop // Emit binary search algorithm as part of the functions used to query
4075c87daf2SSebastian Pop // relation tables.
4085c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4095c87daf2SSebastian Pop
emitBinSearch(raw_ostream & OS,unsigned TableSize)4105c87daf2SSebastian Pop void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
4115c87daf2SSebastian Pop OS << " unsigned mid;\n";
4125c87daf2SSebastian Pop OS << " unsigned start = 0;\n";
4135c87daf2SSebastian Pop OS << " unsigned end = " << TableSize << ";\n";
4145c87daf2SSebastian Pop OS << " while (start < end) {\n";
4155c87daf2SSebastian Pop OS << " mid = start + (end - start) / 2;\n";
4165c87daf2SSebastian Pop OS << " if (Opcode == " << InstrMapDesc.getName() << "Table[mid][0]) {\n";
4175c87daf2SSebastian Pop OS << " break;\n";
4185c87daf2SSebastian Pop OS << " }\n";
4195c87daf2SSebastian Pop OS << " if (Opcode < " << InstrMapDesc.getName() << "Table[mid][0])\n";
4205c87daf2SSebastian Pop OS << " end = mid;\n";
4215c87daf2SSebastian Pop OS << " else\n";
4225c87daf2SSebastian Pop OS << " start = mid + 1;\n";
4235c87daf2SSebastian Pop OS << " }\n";
4245c87daf2SSebastian Pop OS << " if (start == end)\n";
4255c87daf2SSebastian Pop OS << " return -1; // Instruction doesn't exist in this table.\n\n";
4265c87daf2SSebastian Pop }
4275c87daf2SSebastian Pop
4285c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4295c87daf2SSebastian Pop // Emit functions to query relation tables.
4305c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4315c87daf2SSebastian Pop
emitMapFuncBody(raw_ostream & OS,unsigned TableSize)4325c87daf2SSebastian Pop void MapTableEmitter::emitMapFuncBody(raw_ostream &OS,
4335c87daf2SSebastian Pop unsigned TableSize) {
4345c87daf2SSebastian Pop
4355c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields();
4365c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
4375c87daf2SSebastian Pop
4385c87daf2SSebastian Pop // Emit binary search algorithm to locate instructions in the
4395c87daf2SSebastian Pop // relation table. If found, return opcode value from the appropriate column
4405c87daf2SSebastian Pop // of the table.
4415c87daf2SSebastian Pop emitBinSearch(OS, TableSize);
4425c87daf2SSebastian Pop
4435c87daf2SSebastian Pop if (ValueCols.size() > 1) {
4445c87daf2SSebastian Pop for (unsigned i = 0, e = ValueCols.size(); i < e; i++) {
4455c87daf2SSebastian Pop ListInit *ColumnI = ValueCols[i];
446de44af4cSJyun-Yan You OS << " if (";
447664f6a04SCraig Topper for (unsigned j = 0, ColSize = ColumnI->size(); j < ColSize; ++j) {
4485c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
449de44af4cSJyun-Yan You OS << "in" << ColName;
4505c87daf2SSebastian Pop OS << " == ";
4515c87daf2SSebastian Pop OS << ColName << "_" << ColumnI->getElement(j)->getAsUnquotedString();
452de44af4cSJyun-Yan You if (j < ColumnI->size() - 1)
453de44af4cSJyun-Yan You OS << " && ";
4545c87daf2SSebastian Pop }
455de44af4cSJyun-Yan You OS << ")\n";
4565c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName();
4575c87daf2SSebastian Pop OS << "Table[mid]["<<i+1<<"];\n";
4585c87daf2SSebastian Pop }
4595c87daf2SSebastian Pop OS << " return -1;";
4605c87daf2SSebastian Pop }
4615c87daf2SSebastian Pop else
4625c87daf2SSebastian Pop OS << " return " << InstrMapDesc.getName() << "Table[mid][1];\n";
4635c87daf2SSebastian Pop
4645c87daf2SSebastian Pop OS <<"}\n\n";
4655c87daf2SSebastian Pop }
4665c87daf2SSebastian Pop
4675c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4685c87daf2SSebastian Pop // Emit relation tables and the functions to query them.
4695c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4705c87daf2SSebastian Pop
emitTablesWithFunc(raw_ostream & OS)4715c87daf2SSebastian Pop void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
4725c87daf2SSebastian Pop
4735c87daf2SSebastian Pop // Emit function name and the input parameters : mostly opcode value of the
4745c87daf2SSebastian Pop // current instruction. However, if a table has multiple columns (more than 2
4755c87daf2SSebastian Pop // since first column is used for the key instructions), then we also need
4765c87daf2SSebastian Pop // to pass another input to indicate the column to be selected.
4775c87daf2SSebastian Pop
4785c87daf2SSebastian Pop ListInit *ColFields = InstrMapDesc.getColFields();
4795c87daf2SSebastian Pop const std::vector<ListInit*> &ValueCols = InstrMapDesc.getValueCols();
480b12c0d9eSMatt Arsenault OS << "// "<< InstrMapDesc.getName() << "\nLLVM_READONLY\n";
4815c87daf2SSebastian Pop OS << "int "<< InstrMapDesc.getName() << "(uint16_t Opcode";
4825c87daf2SSebastian Pop if (ValueCols.size() > 1) {
483ef0578a8SCraig Topper for (Init *CF : ColFields->getValues()) {
484ef0578a8SCraig Topper std::string ColName = CF->getAsUnquotedString();
485de44af4cSJyun-Yan You OS << ", enum " << ColName << " in" << ColName;
4865c87daf2SSebastian Pop }
487de44af4cSJyun-Yan You }
488de44af4cSJyun-Yan You OS << ") {\n";
4895c87daf2SSebastian Pop
4905c87daf2SSebastian Pop // Emit map table.
4915c87daf2SSebastian Pop unsigned TableSize = emitBinSearchTable(OS);
4925c87daf2SSebastian Pop
4935c87daf2SSebastian Pop // Emit rest of the function body.
4945c87daf2SSebastian Pop emitMapFuncBody(OS, TableSize);
4955c87daf2SSebastian Pop }
4965c87daf2SSebastian Pop
4975c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
4985c87daf2SSebastian Pop // Emit enums for the column fields across all the instruction maps.
4995c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
5005c87daf2SSebastian Pop
emitEnums(raw_ostream & OS,RecordKeeper & Records)5015c87daf2SSebastian Pop static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
5025c87daf2SSebastian Pop
5035c87daf2SSebastian Pop std::vector<Record*> InstrMapVec;
5045c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
5055c87daf2SSebastian Pop std::map<std::string, std::vector<Init*> > ColFieldValueMap;
5065c87daf2SSebastian Pop
5075c87daf2SSebastian Pop // Iterate over all InstrMapping records and create a map between column
5085c87daf2SSebastian Pop // fields and their possible values across all records.
5096e2edc4bSCraig Topper for (Record *CurMap : InstrMapVec) {
5105c87daf2SSebastian Pop ListInit *ColFields;
5115c87daf2SSebastian Pop ColFields = CurMap->getValueAsListInit("ColFields");
5125c87daf2SSebastian Pop ListInit *List = CurMap->getValueAsListInit("ValueCols");
5135c87daf2SSebastian Pop std::vector<ListInit*> ValueCols;
514664f6a04SCraig Topper unsigned ListSize = List->size();
5155c87daf2SSebastian Pop
5165c87daf2SSebastian Pop for (unsigned j = 0; j < ListSize; j++) {
517c191c243SSimon Pilgrim auto *ListJ = cast<ListInit>(List->getElement(j));
5185c87daf2SSebastian Pop
519664f6a04SCraig Topper if (ListJ->size() != ColFields->size())
520635debe8SJoerg Sonnenberger PrintFatalError("Record `" + CurMap->getName() + "', field "
521635debe8SJoerg Sonnenberger "`ValueCols' entries don't match with the entries in 'ColFields' !");
5225c87daf2SSebastian Pop ValueCols.push_back(ListJ);
5235c87daf2SSebastian Pop }
5245c87daf2SSebastian Pop
525664f6a04SCraig Topper for (unsigned j = 0, endCF = ColFields->size(); j < endCF; j++) {
5265c87daf2SSebastian Pop for (unsigned k = 0; k < ListSize; k++){
5275c87daf2SSebastian Pop std::string ColName = ColFields->getElement(j)->getAsUnquotedString();
5285c87daf2SSebastian Pop ColFieldValueMap[ColName].push_back((ValueCols[k])->getElement(j));
5295c87daf2SSebastian Pop }
5305c87daf2SSebastian Pop }
5315c87daf2SSebastian Pop }
5325c87daf2SSebastian Pop
5336e2edc4bSCraig Topper for (auto &Entry : ColFieldValueMap) {
5346e2edc4bSCraig Topper std::vector<Init*> FieldValues = Entry.second;
5355c87daf2SSebastian Pop
5365c87daf2SSebastian Pop // Delete duplicate entries from ColFieldValueMap
537ce3255e7SJyotsna Verma for (unsigned i = 0; i < FieldValues.size() - 1; i++) {
5385c87daf2SSebastian Pop Init *CurVal = FieldValues[i];
539ce3255e7SJyotsna Verma for (unsigned j = i+1; j < FieldValues.size(); j++) {
5405c87daf2SSebastian Pop if (CurVal == FieldValues[j]) {
5415c87daf2SSebastian Pop FieldValues.erase(FieldValues.begin()+j);
54247de8391SVedant Kumar --j;
5435c87daf2SSebastian Pop }
5445c87daf2SSebastian Pop }
5455c87daf2SSebastian Pop }
5465c87daf2SSebastian Pop
5475c87daf2SSebastian Pop // Emit enumerated values for the column fields.
5486e2edc4bSCraig Topper OS << "enum " << Entry.first << " {\n";
549ce3255e7SJyotsna Verma for (unsigned i = 0, endFV = FieldValues.size(); i < endFV; i++) {
5506e2edc4bSCraig Topper OS << "\t" << Entry.first << "_" << FieldValues[i]->getAsUnquotedString();
551ce3255e7SJyotsna Verma if (i != endFV - 1)
5525c87daf2SSebastian Pop OS << ",\n";
5535c87daf2SSebastian Pop else
5545c87daf2SSebastian Pop OS << "\n};\n\n";
5555c87daf2SSebastian Pop }
5565c87daf2SSebastian Pop }
5575c87daf2SSebastian Pop }
5585c87daf2SSebastian Pop
5595c87daf2SSebastian Pop namespace llvm {
5605c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
5615c87daf2SSebastian Pop // Parse 'InstrMapping' records and use the information to form relationship
5625c87daf2SSebastian Pop // between instructions. These relations are emitted as a tables along with the
5635c87daf2SSebastian Pop // functions to query them.
5645c87daf2SSebastian Pop //===----------------------------------------------------------------------===//
EmitMapTable(RecordKeeper & Records,raw_ostream & OS)5655c87daf2SSebastian Pop void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
5665c87daf2SSebastian Pop CodeGenTarget Target(Records);
56786a9aee8SCraig Topper StringRef NameSpace = Target.getInstNamespace();
5685c87daf2SSebastian Pop std::vector<Record*> InstrMapVec;
5695c87daf2SSebastian Pop InstrMapVec = Records.getAllDerivedDefinitions("InstrMapping");
5705c87daf2SSebastian Pop
5718c0809c7SAlexander Kornienko if (InstrMapVec.empty())
5725c87daf2SSebastian Pop return;
5735c87daf2SSebastian Pop
5745c87daf2SSebastian Pop OS << "#ifdef GET_INSTRMAP_INFO\n";
5755c87daf2SSebastian Pop OS << "#undef GET_INSTRMAP_INFO\n";
5765c87daf2SSebastian Pop OS << "namespace llvm {\n\n";
577bb6cf737SKarl-Johan Karlsson OS << "namespace " << NameSpace << " {\n\n";
5785c87daf2SSebastian Pop
5795c87daf2SSebastian Pop // Emit coulumn field names and their values as enums.
5805c87daf2SSebastian Pop emitEnums(OS, Records);
5815c87daf2SSebastian Pop
5825c87daf2SSebastian Pop // Iterate over all instruction mapping records and construct relationship
5835c87daf2SSebastian Pop // maps based on the information specified there.
5845c87daf2SSebastian Pop //
5856e2edc4bSCraig Topper for (Record *CurMap : InstrMapVec) {
5866e2edc4bSCraig Topper MapTableEmitter IMap(Target, Records, CurMap);
5875c87daf2SSebastian Pop
5885c87daf2SSebastian Pop // Build RowInstrMap to group instructions based on their values for
5895c87daf2SSebastian Pop // RowFields. In the process, also collect key instructions into
5905c87daf2SSebastian Pop // KeyInstrVec.
5915c87daf2SSebastian Pop IMap.buildRowInstrMap();
5925c87daf2SSebastian Pop
5935c87daf2SSebastian Pop // Build MapTable to map key instructions with the corresponding column
5945c87daf2SSebastian Pop // instructions.
5955c87daf2SSebastian Pop IMap.buildMapTable();
5965c87daf2SSebastian Pop
5975c87daf2SSebastian Pop // Emit map tables and the functions to query them.
5985c87daf2SSebastian Pop IMap.emitTablesWithFunc(OS);
5995c87daf2SSebastian Pop }
6006bd3a9eaSBjorn Pettersson OS << "} // end namespace " << NameSpace << "\n";
6016bd3a9eaSBjorn Pettersson OS << "} // end namespace llvm\n";
6025c87daf2SSebastian Pop OS << "#endif // GET_INSTRMAP_INFO\n\n";
6035c87daf2SSebastian Pop }
6045c87daf2SSebastian Pop
6055c87daf2SSebastian Pop } // End llvm namespace
606