@@ -250,29 +250,38 @@ void InstrInfoEmitter::emitOperandNameMappings(
250
250
// Map of operand names to their ID.
251
251
MapVector<StringRef, unsigned > OperandNameToID;
252
252
253
- // / The keys of this map is a map which have OpName ID values as their keys
254
- // / and instruction operand indices as their values. The values of this map
255
- // / are lists of instruction names. This map helps to unique entries among
253
+ // / A key in this map is a vector mapping OpName ID values to instruction
254
+ // / operand indices or -1 (but without any trailing -1 values which will be
255
+ // / added later). The corresponding value in this map is the index of that row
256
+ // / in the emitted OperandMap table. This map helps to unique entries among
256
257
// / instructions that have identical OpName -> Operand index mapping.
257
- std::map<std::map< unsigned , unsigned >, std::vector<StringRef> > OperandMap;
258
+ MapVector<SmallVector< int >, unsigned > OperandMap;
258
259
259
260
// Max operand index seen.
260
261
unsigned MaxOperandNo = 0 ;
261
262
262
263
// Fixed/Predefined instructions do not have UseNamedOperandTable enabled, so
263
- // we can just skip them.
264
+ // add a dummy map entry for them.
265
+ OperandMap.try_emplace ({}, 0 );
266
+ unsigned FirstTargetVal = TargetInstructions.front ()->EnumVal ;
267
+ SmallVector<unsigned > InstructionIndex (FirstTargetVal, 0 );
264
268
for (const CodeGenInstruction *Inst : TargetInstructions) {
265
- if (!Inst->TheDef ->getValueAsBit (" UseNamedOperandTable" ))
269
+ if (!Inst->TheDef ->getValueAsBit (" UseNamedOperandTable" )) {
270
+ InstructionIndex.push_back (0 );
266
271
continue ;
267
- std::map<unsigned , unsigned > OpList;
272
+ }
273
+ SmallVector<int > OpList;
268
274
for (const auto &Info : Inst->Operands ) {
269
275
unsigned ID =
270
276
OperandNameToID.try_emplace (Info.Name , OperandNameToID.size ())
271
277
.first ->second ;
278
+ OpList.resize (std::max ((unsigned )OpList.size (), ID + 1 ), -1 );
272
279
OpList[ID] = Info.MIOperandNo ;
273
280
MaxOperandNo = std::max (MaxOperandNo, Info.MIOperandNo );
274
281
}
275
- OperandMap[OpList].push_back (Inst->TheDef ->getName ());
282
+ auto [It, Inserted] =
283
+ OperandMap.try_emplace (std::move (OpList), OperandMap.size ());
284
+ InstructionIndex.push_back (It->second );
276
285
}
277
286
278
287
const size_t NumOperandNames = OperandNameToID.size ();
@@ -302,28 +311,22 @@ void InstrInfoEmitter::emitOperandNameMappings(
302
311
StringRef Type = MaxOperandNo <= INT8_MAX ? " int8_t" : " int16_t" ;
303
312
OS << " static constexpr " << Type << " OperandMap[][" << NumOperandNames
304
313
<< " ] = {\n " ;
305
- for (const auto &Entry : OperandMap) {
306
- const std::map<unsigned , unsigned > &OpList = Entry.first ;
307
-
314
+ for (const auto &[OpList, _] : OperandMap) {
308
315
// Emit a row of the OperandMap table.
309
316
OS << " {" ;
310
- for (unsigned ID = 0 ; ID < NumOperandNames; ++ID) {
311
- auto Iter = OpList.find (ID);
312
- OS << (Iter != OpList.end () ? (int )Iter->second : -1 ) << " , " ;
313
- }
317
+ for (unsigned ID = 0 ; ID < NumOperandNames; ++ID)
318
+ OS << (ID < OpList.size () ? OpList[ID] : -1 ) << " , " ;
314
319
OS << " },\n " ;
315
320
}
316
321
OS << " };\n " ;
317
322
318
- OS << " switch(Opcode) {\n " ;
319
- for (const auto &[TableIndex, Entry] : enumerate(OperandMap)) {
320
- for (StringRef Name : Entry.second )
321
- OS << " case " << Namespace << " ::" << Name << " :\n " ;
322
- OS << " return OperandMap[" << TableIndex
323
- << " ][static_cast<unsigned>(Name)];\n " ;
324
- }
325
- OS << " default: return -1;\n " ;
326
- OS << " }\n " ;
323
+ Type = OperandMap.size () <= UINT8_MAX + 1 ? " uint8_t" : " uint16_t" ;
324
+ OS << " static constexpr " << Type << " InstructionIndex[] = {" ;
325
+ for (auto [TableIndex, Entry] : enumerate(InstructionIndex))
326
+ OS << (TableIndex % 16 == 0 ? " \n " : " " ) << Entry << ' ,' ;
327
+ OS << " \n };\n " ;
328
+
329
+ OS << " return OperandMap[InstructionIndex[Opcode]][(unsigned)Name];\n " ;
327
330
} else {
328
331
// There are no operands, so no need to emit anything
329
332
OS << " return -1;\n " ;
0 commit comments