@@ -250,29 +250,38 @@ void InstrInfoEmitter::emitOperandNameMappings(
250250 // Map of operand names to their ID.
251251 MapVector<StringRef, unsigned > OperandNameToID;
252252
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
256257 // / 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;
258259
259260 // Max operand index seen.
260261 unsigned MaxOperandNo = 0 ;
261262
262263 // 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 );
264268 for (const CodeGenInstruction *Inst : TargetInstructions) {
265- if (!Inst->TheDef ->getValueAsBit (" UseNamedOperandTable" ))
269+ if (!Inst->TheDef ->getValueAsBit (" UseNamedOperandTable" )) {
270+ InstructionIndex.push_back (0 );
266271 continue ;
267- std::map<unsigned , unsigned > OpList;
272+ }
273+ SmallVector<int > OpList;
268274 for (const auto &Info : Inst->Operands ) {
269275 unsigned ID =
270276 OperandNameToID.try_emplace (Info.Name , OperandNameToID.size ())
271277 .first ->second ;
278+ OpList.resize (std::max ((unsigned )OpList.size (), ID + 1 ), -1 );
272279 OpList[ID] = Info.MIOperandNo ;
273280 MaxOperandNo = std::max (MaxOperandNo, Info.MIOperandNo );
274281 }
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 );
276285 }
277286
278287 const size_t NumOperandNames = OperandNameToID.size ();
@@ -302,28 +311,22 @@ void InstrInfoEmitter::emitOperandNameMappings(
302311 StringRef Type = MaxOperandNo <= INT8_MAX ? " int8_t" : " int16_t" ;
303312 OS << " static constexpr " << Type << " OperandMap[][" << NumOperandNames
304313 << " ] = {\n " ;
305- for (const auto &Entry : OperandMap) {
306- const std::map<unsigned , unsigned > &OpList = Entry.first ;
307-
314+ for (const auto &[OpList, _] : OperandMap) {
308315 // Emit a row of the OperandMap table.
309316 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 ) << " , " ;
314319 OS << " },\n " ;
315320 }
316321 OS << " };\n " ;
317322
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 " ;
327330 } else {
328331 // There are no operands, so no need to emit anything
329332 OS << " return -1;\n " ;
0 commit comments