|
17 | 17 | #include "Common/CodeGenTarget.h" |
18 | 18 | #include "llvm/ADT/ArrayRef.h" |
19 | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | +#include "llvm/ADT/MapVector.h" |
20 | 21 | #include "llvm/ADT/STLExtras.h" |
21 | 22 | #include "llvm/ADT/StringExtras.h" |
22 | 23 | #include "llvm/TableGen/Error.h" |
@@ -47,23 +48,19 @@ struct GenericEnum { |
47 | 48 | struct Entry { |
48 | 49 | StringRef Name; |
49 | 50 | int64_t Value; |
50 | | - const Record *Def; |
51 | | - Entry(StringRef N, int64_t V, const Record *D) |
52 | | - : Name(N), Value(V), Def(D) {} |
| 51 | + Entry(StringRef N, int64_t V) : Name(N), Value(V) {} |
53 | 52 | }; |
54 | 53 |
|
55 | 54 | std::string Name; |
56 | 55 | const Record *Class = nullptr; |
57 | 56 | std::string PreprocessorGuard; |
58 | | - std::vector<Entry> Entries; |
59 | | - // Map from a Record to an index into the `Entries` vector. |
60 | | - DenseMap<const Record *, uint32_t> EntryMap; |
| 57 | + MapVector<const Record *, Entry> Entries; |
61 | 58 |
|
62 | 59 | const Entry *getEntry(const Record *Def) const { |
63 | | - auto II = EntryMap.find(Def); |
64 | | - if (II == EntryMap.end()) |
| 60 | + auto II = Entries.find(Def); |
| 61 | + if (II == Entries.end()) |
65 | 62 | return nullptr; |
66 | | - return &Entries[II->second]; |
| 63 | + return &II->second; |
67 | 64 | } |
68 | 65 | }; |
69 | 66 |
|
@@ -323,8 +320,8 @@ void SearchableTableEmitter::emitGenericEnum(const GenericEnum &Enum, |
323 | 320 | emitIfdef((Twine("GET_") + Enum.PreprocessorGuard + "_DECL").str(), OS); |
324 | 321 |
|
325 | 322 | OS << "enum " << Enum.Name << " {\n"; |
326 | | - for (const auto &[Name, Value, _] : Enum.Entries) |
327 | | - OS << " " << Name << " = " << Value << ",\n"; |
| 323 | + for (const auto &[_, Entry] : Enum.Entries.getArrayRef()) |
| 324 | + OS << " " << Entry.Name << " = " << Entry.Value << ",\n"; |
328 | 325 | OS << "};\n"; |
329 | 326 |
|
330 | 327 | OS << "#endif\n\n"; |
@@ -645,24 +642,25 @@ void SearchableTableEmitter::collectEnumEntries( |
645 | 642 | StringRef Name = NameField.empty() ? EntryRec->getName() |
646 | 643 | : EntryRec->getValueAsString(NameField); |
647 | 644 | int64_t Value = ValueField.empty() ? 0 : getInt(EntryRec, ValueField); |
648 | | - Enum.Entries.emplace_back(Name, Value, EntryRec); |
| 645 | + Enum.Entries.try_emplace(EntryRec, Name, Value); |
649 | 646 | } |
650 | 647 |
|
651 | 648 | // If no values are provided for enums, assign values in the order of sorted |
652 | 649 | // enum names. |
653 | 650 | if (ValueField.empty()) { |
654 | | - llvm::stable_sort(Enum.Entries, [](const GenericEnum::Entry &LHS, |
655 | | - const GenericEnum::Entry &RHS) { |
656 | | - return LHS.Name < RHS.Name; |
657 | | - }); |
658 | | - |
659 | | - for (auto [Idx, Entry] : enumerate(Enum.Entries)) |
660 | | - Entry.Value = Idx; |
| 651 | + // Copy the map entries for sorting and clear the map. |
| 652 | + auto SavedEntries = Enum.Entries.takeVector(); |
| 653 | + llvm::stable_sort( |
| 654 | + SavedEntries, |
| 655 | + [](const std::pair<const Record *, GenericEnum::Entry> &LHS, |
| 656 | + const std::pair<const Record *, GenericEnum::Entry> &RHS) { |
| 657 | + return LHS.second.Name < RHS.second.Name; |
| 658 | + }); |
| 659 | + |
| 660 | + // Repopulate entries using the new sorted order. |
| 661 | + for (auto [Idx, Entry] : enumerate(SavedEntries)) |
| 662 | + Enum.Entries.try_emplace(Entry.first, Entry.second.Name, Idx); |
661 | 663 | } |
662 | | - |
663 | | - // Populate the entry map after the `Entries` vector is finalized. |
664 | | - for (auto [Idx, Entry] : enumerate(Enum.Entries)) |
665 | | - Enum.EntryMap.try_emplace(Entry.Def, Idx); |
666 | 664 | } |
667 | 665 |
|
668 | 666 | void SearchableTableEmitter::collectTableEntries( |
|
0 commit comments