77
77
78
78
#include " Common/CodeGenInstruction.h"
79
79
#include " Common/CodeGenTarget.h"
80
+ #include " TableGenBackends.h"
81
+ #include " llvm/ADT/StringExtras.h"
80
82
#include " llvm/TableGen/Error.h"
81
83
#include " llvm/TableGen/Record.h"
82
- using namespace llvm ;
83
- typedef std::map<std::string, std::vector<Record *>> InstrRelMapTy;
84
84
85
- typedef std::map<std::vector<Init *>, std::vector<Record *>> RowInstrMapTy;
85
+ using namespace llvm ;
86
+ typedef std::map<std::string, std::vector<const Record *>> InstrRelMapTy;
87
+ typedef std::map<std::vector<const Init *>, std::vector<const Record *>>
88
+ RowInstrMapTy;
86
89
87
90
namespace {
88
91
@@ -92,13 +95,13 @@ class InstrMap {
92
95
private:
93
96
std::string Name;
94
97
std::string FilterClass;
95
- ListInit *RowFields;
96
- ListInit *ColFields;
97
- ListInit *KeyCol;
98
- std::vector<ListInit *> ValueCols;
98
+ const ListInit *RowFields;
99
+ const ListInit *ColFields;
100
+ const ListInit *KeyCol;
101
+ std::vector<const ListInit *> ValueCols;
99
102
100
103
public:
101
- InstrMap (Record *MapRec) {
104
+ InstrMap (const Record *MapRec) {
102
105
Name = std::string (MapRec->getName ());
103
106
104
107
// FilterClass - It's used to reduce the search space only to the
@@ -133,8 +136,8 @@ class InstrMap {
133
136
MapRec->getName () + " ' has empty " +
134
137
" `ValueCols' field!" );
135
138
136
- for (Init *I : ColValList->getValues ()) {
137
- auto *ColI = cast<ListInit>(I);
139
+ for (const Init *I : ColValList->getValues ()) {
140
+ const auto *ColI = cast<ListInit>(I);
138
141
139
142
// Make sure that all the sub-lists in 'ValueCols' have same number of
140
143
// elements as the fields in 'ColFields'.
@@ -148,26 +151,19 @@ class InstrMap {
148
151
}
149
152
150
153
const std::string &getName () const { return Name; }
151
-
152
154
const std::string &getFilterClass () const { return FilterClass; }
153
-
154
- ListInit *getRowFields () const { return RowFields; }
155
-
156
- ListInit *getColFields () const { return ColFields; }
157
-
158
- ListInit *getKeyCol () const { return KeyCol; }
159
-
160
- const std::vector<ListInit *> &getValueCols () const { return ValueCols; }
155
+ const ListInit *getRowFields () const { return RowFields; }
156
+ const ListInit *getColFields () const { return ColFields; }
157
+ const ListInit *getKeyCol () const { return KeyCol; }
158
+ ArrayRef<const ListInit *> getValueCols () const { return ValueCols; }
161
159
};
162
- } // end anonymous namespace
163
160
164
161
// ===----------------------------------------------------------------------===//
165
162
// class MapTableEmitter : It builds the instruction relation maps using
166
163
// the information provided in InstrMapping records. It outputs these
167
164
// relationship maps as tables into XXXGenInstrInfo.inc file along with the
168
165
// functions to query them.
169
166
170
- namespace {
171
167
class MapTableEmitter {
172
168
private:
173
169
// std::string TargetName;
@@ -177,18 +173,19 @@ class MapTableEmitter {
177
173
178
174
// InstrDefs - list of instructions filtered using FilterClass defined
179
175
// in InstrMapDesc.
180
- std::vector< Record *> InstrDefs;
176
+ ArrayRef< const Record *> InstrDefs;
181
177
182
178
// RowInstrMap - maps RowFields values to the instructions. It's keyed by the
183
179
// values of the row fields and contains vector of records as values.
184
180
RowInstrMapTy RowInstrMap;
185
181
186
182
// KeyInstrVec - list of key instructions.
187
- std::vector<Record *> KeyInstrVec;
188
- DenseMap<const Record *, std::vector<Record *>> MapTable;
183
+ std::vector<const Record *> KeyInstrVec;
184
+ DenseMap<const Record *, std::vector<const Record *>> MapTable;
189
185
190
186
public:
191
- MapTableEmitter (CodeGenTarget &Target, RecordKeeper &Records, Record *IMRec)
187
+ MapTableEmitter (const CodeGenTarget &Target, const RecordKeeper &Records,
188
+ const Record *IMRec)
192
189
: Target(Target), InstrMapDesc(IMRec) {
193
190
const std::string &FilterClass = InstrMapDesc.getFilterClass ();
194
191
InstrDefs = Records.getAllDerivedDefinitions (FilterClass);
@@ -198,11 +195,12 @@ class MapTableEmitter {
198
195
199
196
// Returns true if an instruction is a key instruction, i.e., its ColFields
200
197
// have same values as KeyCol.
201
- bool isKeyColInstr (Record *CurInstr);
198
+ bool isKeyColInstr (const Record *CurInstr);
202
199
203
200
// Find column instruction corresponding to a key instruction based on the
204
201
// constraints for that column.
205
- Record *getInstrForColumn (Record *KeyInstr, ListInit *CurValueCol);
202
+ const Record *getInstrForColumn (const Record *KeyInstr,
203
+ const ListInit *CurValueCol);
206
204
207
205
// Find column instructions for each key instruction based
208
206
// on ValueCols and store them into MapTable.
@@ -226,17 +224,17 @@ class MapTableEmitter {
226
224
// ===----------------------------------------------------------------------===//
227
225
228
226
void MapTableEmitter::buildRowInstrMap () {
229
- for (Record *CurInstr : InstrDefs) {
230
- std::vector<Init *> KeyValue;
231
- ListInit *RowFields = InstrMapDesc.getRowFields ();
232
- for (Init *RowField : RowFields->getValues ()) {
233
- RecordVal *RecVal = CurInstr->getValue (RowField);
227
+ for (const Record *CurInstr : InstrDefs) {
228
+ std::vector<const Init *> KeyValue;
229
+ const ListInit *RowFields = InstrMapDesc.getRowFields ();
230
+ for (const Init *RowField : RowFields->getValues ()) {
231
+ const RecordVal *RecVal = CurInstr->getValue (RowField);
234
232
if (RecVal == nullptr )
235
233
PrintFatalError (CurInstr->getLoc (),
236
234
" No value " + RowField->getAsString () + " found in \" " +
237
235
CurInstr->getName () +
238
236
" \" instruction description." );
239
- Init *CurInstrVal = RecVal->getValue ();
237
+ const Init *CurInstrVal = RecVal->getValue ();
240
238
KeyValue.push_back (CurInstrVal);
241
239
}
242
240
@@ -254,18 +252,19 @@ void MapTableEmitter::buildRowInstrMap() {
254
252
// Return true if an instruction is a KeyCol instruction.
255
253
// ===----------------------------------------------------------------------===//
256
254
257
- bool MapTableEmitter::isKeyColInstr (Record *CurInstr) {
258
- ListInit *ColFields = InstrMapDesc.getColFields ();
259
- ListInit *KeyCol = InstrMapDesc.getKeyCol ();
255
+ bool MapTableEmitter::isKeyColInstr (const Record *CurInstr) {
256
+ const ListInit *ColFields = InstrMapDesc.getColFields ();
257
+ const ListInit *KeyCol = InstrMapDesc.getKeyCol ();
260
258
261
259
// Check if the instruction is a KeyCol instruction.
262
260
bool MatchFound = true ;
263
261
for (unsigned j = 0 , endCF = ColFields->size (); (j < endCF) && MatchFound;
264
262
j++) {
265
- RecordVal *ColFieldName = CurInstr->getValue (ColFields->getElement (j));
263
+ const RecordVal *ColFieldName =
264
+ CurInstr->getValue (ColFields->getElement (j));
266
265
std::string CurInstrVal = ColFieldName->getValue ()->getAsUnquotedString ();
267
266
std::string KeyColValue = KeyCol->getElement (j)->getAsUnquotedString ();
268
- MatchFound = ( CurInstrVal == KeyColValue) ;
267
+ MatchFound = CurInstrVal == KeyColValue;
269
268
}
270
269
return MatchFound;
271
270
}
@@ -278,15 +277,15 @@ bool MapTableEmitter::isKeyColInstr(Record *CurInstr) {
278
277
void MapTableEmitter::buildMapTable () {
279
278
// Find column instructions for a given key based on the ColField
280
279
// constraints.
281
- const std::vector< ListInit *> & ValueCols = InstrMapDesc.getValueCols ();
280
+ ArrayRef< const ListInit *> ValueCols = InstrMapDesc.getValueCols ();
282
281
unsigned NumOfCols = ValueCols.size ();
283
- for (Record *CurKeyInstr : KeyInstrVec) {
284
- std::vector<Record *> ColInstrVec (NumOfCols);
282
+ for (const Record *CurKeyInstr : KeyInstrVec) {
283
+ std::vector<const Record *> ColInstrVec (NumOfCols);
285
284
286
285
// Find the column instruction based on the constraints for the column.
287
286
for (unsigned ColIdx = 0 ; ColIdx < NumOfCols; ColIdx++) {
288
- ListInit *CurValueCol = ValueCols[ColIdx];
289
- Record *ColInstr = getInstrForColumn (CurKeyInstr, CurValueCol);
287
+ const ListInit *CurValueCol = ValueCols[ColIdx];
288
+ const Record *ColInstr = getInstrForColumn (CurKeyInstr, CurValueCol);
290
289
ColInstrVec[ColIdx] = ColInstr;
291
290
}
292
291
MapTable[CurKeyInstr] = ColInstrVec;
@@ -297,43 +296,43 @@ void MapTableEmitter::buildMapTable() {
297
296
// Find column instruction based on the constraints for that column.
298
297
// ===----------------------------------------------------------------------===//
299
298
300
- Record *MapTableEmitter::getInstrForColumn (Record *KeyInstr,
301
- ListInit *CurValueCol) {
302
- ListInit *RowFields = InstrMapDesc.getRowFields ();
303
- std::vector<Init *> KeyValue;
299
+ const Record *MapTableEmitter::getInstrForColumn (const Record *KeyInstr,
300
+ const ListInit *CurValueCol) {
301
+ const ListInit *RowFields = InstrMapDesc.getRowFields ();
302
+ std::vector<const Init *> KeyValue;
304
303
305
304
// Construct KeyValue using KeyInstr's values for RowFields.
306
- for (Init *RowField : RowFields->getValues ()) {
307
- Init *KeyInstrVal = KeyInstr->getValue (RowField)->getValue ();
305
+ for (const Init *RowField : RowFields->getValues ()) {
306
+ const Init *KeyInstrVal = KeyInstr->getValue (RowField)->getValue ();
308
307
KeyValue.push_back (KeyInstrVal);
309
308
}
310
309
311
310
// Get all the instructions that share the same KeyValue as the KeyInstr
312
311
// in RowInstrMap. We search through these instructions to find a match
313
312
// for the current column, i.e., the instruction which has the same values
314
313
// as CurValueCol for all the fields in ColFields.
315
- const std::vector< Record *> & RelatedInstrVec = RowInstrMap[KeyValue];
314
+ ArrayRef< const Record *> RelatedInstrVec = RowInstrMap[KeyValue];
316
315
317
- ListInit *ColFields = InstrMapDesc.getColFields ();
318
- Record *MatchInstr = nullptr ;
316
+ const ListInit *ColFields = InstrMapDesc.getColFields ();
317
+ const Record *MatchInstr = nullptr ;
319
318
320
- for (llvm:: Record *CurInstr : RelatedInstrVec) {
319
+ for (const Record *CurInstr : RelatedInstrVec) {
321
320
bool MatchFound = true ;
322
321
for (unsigned j = 0 , endCF = ColFields->size (); (j < endCF) && MatchFound;
323
322
j++) {
324
- Init *ColFieldJ = ColFields->getElement (j);
325
- Init *CurInstrInit = CurInstr->getValue (ColFieldJ)->getValue ();
323
+ const Init *ColFieldJ = ColFields->getElement (j);
324
+ const Init *CurInstrInit = CurInstr->getValue (ColFieldJ)->getValue ();
326
325
std::string CurInstrVal = CurInstrInit->getAsUnquotedString ();
327
- Init *ColFieldJVallue = CurValueCol->getElement (j);
328
- MatchFound = ( CurInstrVal == ColFieldJVallue->getAsUnquotedString () );
326
+ const Init *ColFieldJVallue = CurValueCol->getElement (j);
327
+ MatchFound = CurInstrVal == ColFieldJVallue->getAsUnquotedString ();
329
328
}
330
329
331
330
if (MatchFound) {
332
331
if (MatchInstr) {
333
332
// Already had a match
334
333
// Error if multiple matches are found for a column.
335
334
std::string KeyValueStr;
336
- for (Init *Value : KeyValue) {
335
+ for (const Init *Value : KeyValue) {
337
336
if (!KeyValueStr.empty ())
338
337
KeyValueStr += " , " ;
339
338
KeyValueStr += Value->getAsString ();
@@ -357,11 +356,10 @@ Record *MapTableEmitter::getInstrForColumn(Record *KeyInstr,
357
356
// ===----------------------------------------------------------------------===//
358
357
359
358
unsigned MapTableEmitter::emitBinSearchTable (raw_ostream &OS) {
360
-
361
359
ArrayRef<const CodeGenInstruction *> NumberedInstructions =
362
360
Target.getInstructionsByEnumValue ();
363
361
StringRef Namespace = Target.getInstNamespace ();
364
- const std::vector< ListInit *> & ValueCols = InstrMapDesc.getValueCols ();
362
+ ArrayRef< const ListInit *> ValueCols = InstrMapDesc.getValueCols ();
365
363
unsigned NumCol = ValueCols.size ();
366
364
unsigned TotalNumInstr = NumberedInstructions.size ();
367
365
unsigned TableSize = 0 ;
@@ -372,7 +370,7 @@ unsigned MapTableEmitter::emitBinSearchTable(raw_ostream &OS) {
372
370
OS << " Table[][" << NumCol + 1 << " ] = {\n " ;
373
371
for (unsigned i = 0 ; i < TotalNumInstr; i++) {
374
372
const Record *CurInstr = NumberedInstructions[i]->TheDef ;
375
- std::vector< Record *> ColInstrs = MapTable[CurInstr];
373
+ ArrayRef< const Record *> ColInstrs = MapTable[CurInstr];
376
374
std::string OutStr;
377
375
unsigned RelExists = 0 ;
378
376
if (!ColInstrs.empty ()) {
@@ -434,8 +432,8 @@ void MapTableEmitter::emitBinSearch(raw_ostream &OS, unsigned TableSize) {
434
432
435
433
void MapTableEmitter::emitMapFuncBody (raw_ostream &OS, unsigned TableSize) {
436
434
437
- ListInit *ColFields = InstrMapDesc.getColFields ();
438
- const std::vector< ListInit *> & ValueCols = InstrMapDesc.getValueCols ();
435
+ const ListInit *ColFields = InstrMapDesc.getColFields ();
436
+ ArrayRef< const ListInit *> ValueCols = InstrMapDesc.getValueCols ();
439
437
440
438
// Emit binary search algorithm to locate instructions in the
441
439
// relation table. If found, return opcode value from the appropriate column
@@ -444,7 +442,7 @@ void MapTableEmitter::emitMapFuncBody(raw_ostream &OS, unsigned TableSize) {
444
442
445
443
if (ValueCols.size () > 1 ) {
446
444
for (unsigned i = 0 , e = ValueCols.size (); i < e; i++) {
447
- ListInit *ColumnI = ValueCols[i];
445
+ const ListInit *ColumnI = ValueCols[i];
448
446
OS << " if (" ;
449
447
for (unsigned j = 0 , ColSize = ColumnI->size (); j < ColSize; ++j) {
450
448
std::string ColName = ColFields->getElement (j)->getAsUnquotedString ();
@@ -476,8 +474,8 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
476
474
// since first column is used for the key instructions), then we also need
477
475
// to pass another input to indicate the column to be selected.
478
476
479
- ListInit *ColFields = InstrMapDesc.getColFields ();
480
- const std::vector< ListInit *> & ValueCols = InstrMapDesc.getValueCols ();
477
+ const ListInit *ColFields = InstrMapDesc.getColFields ();
478
+ ArrayRef< const ListInit *> ValueCols = InstrMapDesc.getValueCols ();
481
479
OS << " // " << InstrMapDesc.getName () << " \n LLVM_READONLY\n " ;
482
480
OS << " int " << InstrMapDesc.getName () << " (uint16_t Opcode" ;
483
481
if (ValueCols.size () > 1 ) {
@@ -499,23 +497,20 @@ void MapTableEmitter::emitTablesWithFunc(raw_ostream &OS) {
499
497
// Emit enums for the column fields across all the instruction maps.
500
498
// ===----------------------------------------------------------------------===//
501
499
502
- static void emitEnums (raw_ostream &OS, RecordKeeper &Records) {
503
-
504
- std::vector<Record *> InstrMapVec;
505
- InstrMapVec = Records.getAllDerivedDefinitions (" InstrMapping" );
506
- std::map<std::string, std::vector<Init *>> ColFieldValueMap;
500
+ static void emitEnums (raw_ostream &OS, const RecordKeeper &Records) {
501
+ std::map<std::string, std::vector<const Init *>> ColFieldValueMap;
507
502
508
503
// Iterate over all InstrMapping records and create a map between column
509
504
// fields and their possible values across all records.
510
- for (Record *CurMap : InstrMapVec) {
511
- ListInit *ColFields;
512
- ColFields = CurMap->getValueAsListInit (" ColFields" );
513
- ListInit *List = CurMap->getValueAsListInit (" ValueCols" );
514
- std::vector<ListInit *> ValueCols;
505
+ for (const Record *CurMap :
506
+ Records. getAllDerivedDefinitions ( " InstrMapping " )) {
507
+ const ListInit * ColFields = CurMap->getValueAsListInit (" ColFields" );
508
+ const ListInit *List = CurMap->getValueAsListInit (" ValueCols" );
509
+ std::vector<const ListInit *> ValueCols;
515
510
unsigned ListSize = List->size ();
516
511
517
512
for (unsigned j = 0 ; j < ListSize; j++) {
518
- auto *ListJ = cast<ListInit>(List->getElement (j));
513
+ const auto *ListJ = cast<ListInit>(List->getElement (j));
519
514
520
515
if (ListJ->size () != ColFields->size ())
521
516
PrintFatalError (" Record `" + CurMap->getName () +
@@ -533,12 +528,10 @@ static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
533
528
}
534
529
}
535
530
536
- for (auto &Entry : ColFieldValueMap) {
537
- std::vector<Init *> FieldValues = Entry.second ;
538
-
531
+ for (auto &[EnumName, FieldValues] : ColFieldValueMap) {
539
532
// Delete duplicate entries from ColFieldValueMap
540
533
for (unsigned i = 0 ; i < FieldValues.size () - 1 ; i++) {
541
- Init *CurVal = FieldValues[i];
534
+ const Init *CurVal = FieldValues[i];
542
535
for (unsigned j = i + 1 ; j < FieldValues.size (); j++) {
543
536
if (CurVal == FieldValues[j]) {
544
537
FieldValues.erase (FieldValues.begin () + j);
@@ -548,28 +541,24 @@ static void emitEnums(raw_ostream &OS, RecordKeeper &Records) {
548
541
}
549
542
550
543
// Emit enumerated values for the column fields.
551
- OS << " enum " << Entry.first << " {\n " ;
552
- for (unsigned i = 0 , endFV = FieldValues.size (); i < endFV; i++) {
553
- OS << " \t " << Entry.first << " _" << FieldValues[i]->getAsUnquotedString ();
554
- if (i != endFV - 1 )
555
- OS << " ,\n " ;
556
- else
557
- OS << " \n };\n\n " ;
558
- }
544
+ OS << " enum " << EnumName << " {\n " ;
545
+ ListSeparator LS (" ,\n " );
546
+ for (const Init *Field : FieldValues)
547
+ OS << LS << " \t " << EnumName << " _" << Field->getAsUnquotedString ();
548
+ OS << " \n };\n\n " ;
559
549
}
560
550
}
561
551
562
- namespace llvm {
563
552
// ===----------------------------------------------------------------------===//
564
553
// Parse 'InstrMapping' records and use the information to form relationship
565
554
// between instructions. These relations are emitted as a tables along with the
566
555
// functions to query them.
567
556
// ===----------------------------------------------------------------------===//
568
- void EmitMapTable (RecordKeeper &Records, raw_ostream &OS) {
557
+ void llvm:: EmitMapTable (const RecordKeeper &Records, raw_ostream &OS) {
569
558
CodeGenTarget Target (Records);
570
559
StringRef NameSpace = Target.getInstNamespace ();
571
- std::vector< Record *> InstrMapVec;
572
- InstrMapVec = Records.getAllDerivedDefinitions (" InstrMapping" );
560
+ ArrayRef< const Record *> InstrMapVec =
561
+ Records.getAllDerivedDefinitions (" InstrMapping" );
573
562
574
563
if (InstrMapVec.empty ())
575
564
return ;
@@ -585,7 +574,7 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
585
574
// Iterate over all instruction mapping records and construct relationship
586
575
// maps based on the information specified there.
587
576
//
588
- for (Record *CurMap : InstrMapVec) {
577
+ for (const Record *CurMap : InstrMapVec) {
589
578
MapTableEmitter IMap (Target, Records, CurMap);
590
579
591
580
// Build RowInstrMap to group instructions based on their values for
@@ -604,5 +593,3 @@ void EmitMapTable(RecordKeeper &Records, raw_ostream &OS) {
604
593
OS << " } // end namespace llvm\n " ;
605
594
OS << " #endif // GET_INSTRMAP_INFO\n\n " ;
606
595
}
607
-
608
- } // namespace llvm
0 commit comments