xref: /src/contrib/llvm-project/llvm/utils/TableGen/DFAPacketizerEmitter.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
101095a5dSDimitry Andric //===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
263faed5bSDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
663faed5bSDimitry Andric //
763faed5bSDimitry Andric //===----------------------------------------------------------------------===//
863faed5bSDimitry Andric //
963faed5bSDimitry Andric // This class parses the Schedule.td file and produces an API that can be used
1063faed5bSDimitry Andric // to reason about whether an instruction can be added to a packet on a VLIW
1163faed5bSDimitry Andric // architecture. The class internally generates a deterministic finite
1263faed5bSDimitry Andric // automaton (DFA) that models all possible mappings of machine instructions
1363faed5bSDimitry Andric // to functional units as instructions are added to a packet.
1463faed5bSDimitry Andric //
1563faed5bSDimitry Andric //===----------------------------------------------------------------------===//
1663faed5bSDimitry Andric 
17ac9a064cSDimitry Andric #include "Common/CodeGenSchedule.h"
18ac9a064cSDimitry Andric #include "Common/CodeGenTarget.h"
191d5ae102SDimitry Andric #include "DFAEmitter.h"
20b915e9e0SDimitry Andric #include "llvm/ADT/SmallVector.h"
21dd58ef01SDimitry Andric #include "llvm/Support/Debug.h"
22b915e9e0SDimitry Andric #include "llvm/Support/raw_ostream.h"
23706b4fc4SDimitry Andric #include "llvm/TableGen/Record.h"
24706b4fc4SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
25b915e9e0SDimitry Andric #include <cassert>
26b915e9e0SDimitry Andric #include <cstdint>
277fa27ce4SDimitry Andric #include <deque>
2858b69754SDimitry Andric #include <map>
29b915e9e0SDimitry Andric #include <set>
3058b69754SDimitry Andric #include <string>
311d5ae102SDimitry Andric #include <unordered_map>
32b915e9e0SDimitry Andric #include <vector>
3301095a5dSDimitry Andric 
34344a3780SDimitry Andric #define DEBUG_TYPE "dfa-emitter"
35344a3780SDimitry Andric 
3663faed5bSDimitry Andric using namespace llvm;
3763faed5bSDimitry Andric 
38706b4fc4SDimitry Andric // We use a uint64_t to represent a resource bitmask.
39706b4fc4SDimitry Andric #define DFA_MAX_RESOURCES 64
40dd58ef01SDimitry Andric 
41dd58ef01SDimitry Andric namespace {
42706b4fc4SDimitry Andric using ResourceVector = SmallVector<uint64_t, 4>;
43b915e9e0SDimitry Andric 
44706b4fc4SDimitry Andric struct ScheduleClass {
45706b4fc4SDimitry Andric   /// The parent itinerary index (processor model ID).
46706b4fc4SDimitry Andric   unsigned ItineraryID;
47dd58ef01SDimitry Andric 
48706b4fc4SDimitry Andric   /// Index within this itinerary of the schedule class.
49706b4fc4SDimitry Andric   unsigned Idx;
50b915e9e0SDimitry Andric 
51706b4fc4SDimitry Andric   /// The index within the uniqued set of required resources of Resources.
52706b4fc4SDimitry Andric   unsigned ResourcesIdx;
5301095a5dSDimitry Andric 
54706b4fc4SDimitry Andric   /// Conjunctive list of resource requirements:
55706b4fc4SDimitry Andric   ///   {a|b, b|c} => (a OR b) AND (b or c).
56706b4fc4SDimitry Andric   /// Resources are unique across all itineraries.
57706b4fc4SDimitry Andric   ResourceVector Resources;
58706b4fc4SDimitry Andric };
59dd58ef01SDimitry Andric 
60706b4fc4SDimitry Andric // Generates and prints out the DFA for resource tracking.
6158b69754SDimitry Andric class DFAPacketizerEmitter {
6258b69754SDimitry Andric private:
6358b69754SDimitry Andric   std::string TargetName;
6458b69754SDimitry Andric   RecordKeeper &Records;
6558b69754SDimitry Andric 
66706b4fc4SDimitry Andric   UniqueVector<ResourceVector> UniqueResources;
67706b4fc4SDimitry Andric   std::vector<ScheduleClass> ScheduleClasses;
68706b4fc4SDimitry Andric   std::map<std::string, uint64_t> FUNameToBitsMap;
69706b4fc4SDimitry Andric   std::map<unsigned, uint64_t> ComboBitToBitsMap;
70706b4fc4SDimitry Andric 
7158b69754SDimitry Andric public:
7258b69754SDimitry Andric   DFAPacketizerEmitter(RecordKeeper &R);
7358b69754SDimitry Andric 
74706b4fc4SDimitry Andric   // Construct a map of function unit names to bits.
75ac9a064cSDimitry Andric   int collectAllFuncUnits(ArrayRef<const CodeGenProcModel *> ProcModels);
76dd58ef01SDimitry Andric 
77706b4fc4SDimitry Andric   // Construct a map from a combo function unit bit to the bits of all included
78706b4fc4SDimitry Andric   // functional units.
79706b4fc4SDimitry Andric   int collectAllComboFuncs(ArrayRef<Record *> ComboFuncList);
80dd58ef01SDimitry Andric 
81706b4fc4SDimitry Andric   ResourceVector getResourcesForItinerary(Record *Itinerary);
82706b4fc4SDimitry Andric   void createScheduleClasses(unsigned ItineraryIdx, const RecVec &Itineraries);
8358b69754SDimitry Andric 
841d5ae102SDimitry Andric   // Emit code for a subset of itineraries.
851d5ae102SDimitry Andric   void emitForItineraries(raw_ostream &OS,
86706b4fc4SDimitry Andric                           std::vector<const CodeGenProcModel *> &ProcItinList,
871d5ae102SDimitry Andric                           std::string DFAName);
881d5ae102SDimitry Andric 
8958b69754SDimitry Andric   void run(raw_ostream &OS);
9058b69754SDimitry Andric };
9101095a5dSDimitry Andric } // end anonymous namespace
9263faed5bSDimitry Andric 
DFAPacketizerEmitter(RecordKeeper & R)93706b4fc4SDimitry Andric DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R)
94cfca06d7SDimitry Andric     : TargetName(std::string(CodeGenTarget(R).getName())), Records(R) {}
95dd58ef01SDimitry Andric 
collectAllFuncUnits(ArrayRef<const CodeGenProcModel * > ProcModels)96dd58ef01SDimitry Andric int DFAPacketizerEmitter::collectAllFuncUnits(
97706b4fc4SDimitry Andric     ArrayRef<const CodeGenProcModel *> ProcModels) {
98eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
99eb11fae6SDimitry Andric                        "----------------------\n");
100eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
101706b4fc4SDimitry Andric   LLVM_DEBUG(dbgs() << " (" << ProcModels.size() << " itineraries)\n");
102706b4fc4SDimitry Andric 
103706b4fc4SDimitry Andric   std::set<Record *> ProcItinList;
104706b4fc4SDimitry Andric   for (const CodeGenProcModel *Model : ProcModels)
105706b4fc4SDimitry Andric     ProcItinList.insert(Model->ItinsDef);
10663faed5bSDimitry Andric 
107dd58ef01SDimitry Andric   int totalFUs = 0;
10863faed5bSDimitry Andric   // Parse functional units for all the itineraries.
109706b4fc4SDimitry Andric   for (Record *Proc : ProcItinList) {
11063faed5bSDimitry Andric     std::vector<Record *> FUs = Proc->getValueAsListOfDefs("FU");
11163faed5bSDimitry Andric 
112706b4fc4SDimitry Andric     LLVM_DEBUG(dbgs() << "    FU:"
113706b4fc4SDimitry Andric                       << " (" << FUs.size() << " FUs) " << Proc->getName());
114dd58ef01SDimitry Andric 
11563faed5bSDimitry Andric     // Convert macros to bits for each stage.
116dd58ef01SDimitry Andric     unsigned numFUs = FUs.size();
117dd58ef01SDimitry Andric     for (unsigned j = 0; j < numFUs; ++j) {
118dd58ef01SDimitry Andric       assert((j < DFA_MAX_RESOURCES) &&
119dd58ef01SDimitry Andric              "Exceeded maximum number of representable resources");
120706b4fc4SDimitry Andric       uint64_t FuncResources = 1ULL << j;
121cfca06d7SDimitry Andric       FUNameToBitsMap[std::string(FUs[j]->getName())] = FuncResources;
122eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << " " << FUs[j]->getName() << ":0x"
123b2b7c066SDimitry Andric                         << Twine::utohexstr(FuncResources));
124dd58ef01SDimitry Andric     }
125dd58ef01SDimitry Andric     totalFUs += numFUs;
126eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "\n");
127dd58ef01SDimitry Andric   }
128dd58ef01SDimitry Andric   return totalFUs;
12963faed5bSDimitry Andric }
13063faed5bSDimitry Andric 
collectAllComboFuncs(ArrayRef<Record * > ComboFuncList)131ac9a064cSDimitry Andric int DFAPacketizerEmitter::collectAllComboFuncs(
132ac9a064cSDimitry Andric     ArrayRef<Record *> ComboFuncList) {
133eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
134eb11fae6SDimitry Andric                        "----------------------\n");
135eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "collectAllComboFuncs");
136eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
137dd58ef01SDimitry Andric 
138dd58ef01SDimitry Andric   int numCombos = 0;
139dd58ef01SDimitry Andric   for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
140dd58ef01SDimitry Andric     Record *Func = ComboFuncList[i];
141dd58ef01SDimitry Andric     std::vector<Record *> FUs = Func->getValueAsListOfDefs("CFD");
142dd58ef01SDimitry Andric 
143eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "    CFD:" << i << " (" << FUs.size() << " combo FUs) "
144dd58ef01SDimitry Andric                       << Func->getName() << "\n");
145dd58ef01SDimitry Andric 
146dd58ef01SDimitry Andric     // Convert macros to bits for each stage.
147dd58ef01SDimitry Andric     for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
148dd58ef01SDimitry Andric       assert((j < DFA_MAX_RESOURCES) &&
149dd58ef01SDimitry Andric              "Exceeded maximum number of DFA resources");
150dd58ef01SDimitry Andric       Record *FuncData = FUs[j];
151dd58ef01SDimitry Andric       Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
152dd58ef01SDimitry Andric       const std::vector<Record *> &FuncList =
153dd58ef01SDimitry Andric           FuncData->getValueAsListOfDefs("FuncList");
154cfca06d7SDimitry Andric       const std::string &ComboFuncName = std::string(ComboFunc->getName());
155706b4fc4SDimitry Andric       uint64_t ComboBit = FUNameToBitsMap[ComboFuncName];
156706b4fc4SDimitry Andric       uint64_t ComboResources = ComboBit;
157eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "      combo: " << ComboFuncName << ":0x"
158b2b7c066SDimitry Andric                         << Twine::utohexstr(ComboResources) << "\n");
159344a3780SDimitry Andric       for (auto *K : FuncList) {
160344a3780SDimitry Andric         std::string FuncName = std::string(K->getName());
161706b4fc4SDimitry Andric         uint64_t FuncResources = FUNameToBitsMap[FuncName];
162eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << "        " << FuncName << ":0x"
163b2b7c066SDimitry Andric                           << Twine::utohexstr(FuncResources) << "\n");
164dd58ef01SDimitry Andric         ComboResources |= FuncResources;
165dd58ef01SDimitry Andric       }
166dd58ef01SDimitry Andric       ComboBitToBitsMap[ComboBit] = ComboResources;
167dd58ef01SDimitry Andric       numCombos++;
168eb11fae6SDimitry Andric       LLVM_DEBUG(dbgs() << "          => combo bits: " << ComboFuncName << ":0x"
169b2b7c066SDimitry Andric                         << Twine::utohexstr(ComboBit) << " = 0x"
170b2b7c066SDimitry Andric                         << Twine::utohexstr(ComboResources) << "\n");
171dd58ef01SDimitry Andric     }
172dd58ef01SDimitry Andric   }
173dd58ef01SDimitry Andric   return numCombos;
174dd58ef01SDimitry Andric }
175dd58ef01SDimitry Andric 
176706b4fc4SDimitry Andric ResourceVector
getResourcesForItinerary(Record * Itinerary)177706b4fc4SDimitry Andric DFAPacketizerEmitter::getResourcesForItinerary(Record *Itinerary) {
178706b4fc4SDimitry Andric   ResourceVector Resources;
179706b4fc4SDimitry Andric   assert(Itinerary);
180706b4fc4SDimitry Andric   for (Record *StageDef : Itinerary->getValueAsListOfDefs("Stages")) {
181706b4fc4SDimitry Andric     uint64_t StageResources = 0;
182706b4fc4SDimitry Andric     for (Record *Unit : StageDef->getValueAsListOfDefs("Units")) {
183cfca06d7SDimitry Andric       StageResources |= FUNameToBitsMap[std::string(Unit->getName())];
184706b4fc4SDimitry Andric     }
185706b4fc4SDimitry Andric     if (StageResources != 0)
186706b4fc4SDimitry Andric       Resources.push_back(StageResources);
187706b4fc4SDimitry Andric   }
188706b4fc4SDimitry Andric   return Resources;
18963faed5bSDimitry Andric }
19063faed5bSDimitry Andric 
createScheduleClasses(unsigned ItineraryIdx,const RecVec & Itineraries)191706b4fc4SDimitry Andric void DFAPacketizerEmitter::createScheduleClasses(unsigned ItineraryIdx,
192706b4fc4SDimitry Andric                                                  const RecVec &Itineraries) {
193706b4fc4SDimitry Andric   unsigned Idx = 0;
194706b4fc4SDimitry Andric   for (Record *Itinerary : Itineraries) {
195706b4fc4SDimitry Andric     if (!Itinerary) {
196706b4fc4SDimitry Andric       ScheduleClasses.push_back({ItineraryIdx, Idx++, 0, ResourceVector{}});
197706b4fc4SDimitry Andric       continue;
19863faed5bSDimitry Andric     }
199706b4fc4SDimitry Andric     ResourceVector Resources = getResourcesForItinerary(Itinerary);
200706b4fc4SDimitry Andric     ScheduleClasses.push_back(
201706b4fc4SDimitry Andric         {ItineraryIdx, Idx++, UniqueResources.insert(Resources), Resources});
20263faed5bSDimitry Andric   }
203dd58ef01SDimitry Andric }
20463faed5bSDimitry Andric 
20563faed5bSDimitry Andric //
20663faed5bSDimitry Andric // Run the worklist algorithm to generate the DFA.
20763faed5bSDimitry Andric //
run(raw_ostream & OS)20858b69754SDimitry Andric void DFAPacketizerEmitter::run(raw_ostream &OS) {
2097fa27ce4SDimitry Andric   emitSourceFileHeader("Target DFA Packetizer Tables", OS);
2101d5ae102SDimitry Andric   OS << "\n"
2111d5ae102SDimitry Andric      << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
2121d5ae102SDimitry Andric   OS << "namespace llvm {\n";
2131d5ae102SDimitry Andric 
214706b4fc4SDimitry Andric   CodeGenTarget CGT(Records);
215706b4fc4SDimitry Andric   CodeGenSchedModels CGS(Records, CGT);
2161d5ae102SDimitry Andric 
217706b4fc4SDimitry Andric   std::unordered_map<std::string, std::vector<const CodeGenProcModel *>>
218706b4fc4SDimitry Andric       ItinsByNamespace;
219706b4fc4SDimitry Andric   for (const CodeGenProcModel &ProcModel : CGS.procModels()) {
220706b4fc4SDimitry Andric     if (ProcModel.hasItineraries()) {
221706b4fc4SDimitry Andric       auto NS = ProcModel.ItinsDef->getValueAsString("PacketizerNamespace");
222cfca06d7SDimitry Andric       ItinsByNamespace[std::string(NS)].push_back(&ProcModel);
223706b4fc4SDimitry Andric     }
224706b4fc4SDimitry Andric   }
2251d5ae102SDimitry Andric 
2261d5ae102SDimitry Andric   for (auto &KV : ItinsByNamespace)
2271d5ae102SDimitry Andric     emitForItineraries(OS, KV.second, KV.first);
2281d5ae102SDimitry Andric   OS << "} // end namespace llvm\n";
2291d5ae102SDimitry Andric }
2301d5ae102SDimitry Andric 
emitForItineraries(raw_ostream & OS,std::vector<const CodeGenProcModel * > & ProcModels,std::string DFAName)2311d5ae102SDimitry Andric void DFAPacketizerEmitter::emitForItineraries(
232706b4fc4SDimitry Andric     raw_ostream &OS, std::vector<const CodeGenProcModel *> &ProcModels,
2331d5ae102SDimitry Andric     std::string DFAName) {
234706b4fc4SDimitry Andric   OS << "} // end namespace llvm\n\n";
235706b4fc4SDimitry Andric   OS << "namespace {\n";
236706b4fc4SDimitry Andric   collectAllFuncUnits(ProcModels);
237706b4fc4SDimitry Andric   collectAllComboFuncs(Records.getAllDerivedDefinitions("ComboFuncUnits"));
238dd58ef01SDimitry Andric 
239dd58ef01SDimitry Andric   // Collect the itineraries.
240706b4fc4SDimitry Andric   DenseMap<const CodeGenProcModel *, unsigned> ProcModelStartIdx;
241706b4fc4SDimitry Andric   for (const CodeGenProcModel *Model : ProcModels) {
242706b4fc4SDimitry Andric     assert(Model->hasItineraries());
243706b4fc4SDimitry Andric     ProcModelStartIdx[Model] = ScheduleClasses.size();
244706b4fc4SDimitry Andric     createScheduleClasses(Model->Index, Model->ItinDefList);
24563faed5bSDimitry Andric   }
24663faed5bSDimitry Andric 
247706b4fc4SDimitry Andric   // Output the mapping from ScheduleClass to ResourcesIdx.
248706b4fc4SDimitry Andric   unsigned Idx = 0;
249cfca06d7SDimitry Andric   OS << "constexpr unsigned " << TargetName << DFAName
250cfca06d7SDimitry Andric      << "ResourceIndices[] = {";
251706b4fc4SDimitry Andric   for (const ScheduleClass &SC : ScheduleClasses) {
252706b4fc4SDimitry Andric     if (Idx++ % 32 == 0)
253706b4fc4SDimitry Andric       OS << "\n  ";
254706b4fc4SDimitry Andric     OS << SC.ResourcesIdx << ", ";
255706b4fc4SDimitry Andric   }
256706b4fc4SDimitry Andric   OS << "\n};\n\n";
257706b4fc4SDimitry Andric 
258706b4fc4SDimitry Andric   // And the mapping from Itinerary index into the previous table.
259cfca06d7SDimitry Andric   OS << "constexpr unsigned " << TargetName << DFAName
260706b4fc4SDimitry Andric      << "ProcResourceIndexStart[] = {\n";
261706b4fc4SDimitry Andric   OS << "  0, // NoSchedModel\n";
262706b4fc4SDimitry Andric   for (const CodeGenProcModel *Model : ProcModels) {
263706b4fc4SDimitry Andric     OS << "  " << ProcModelStartIdx[Model] << ", // " << Model->ModelName
264706b4fc4SDimitry Andric        << "\n";
265706b4fc4SDimitry Andric   }
266b60736ecSDimitry Andric   OS << "  " << ScheduleClasses.size() << "\n};\n\n";
267706b4fc4SDimitry Andric 
2681d5ae102SDimitry Andric   // The type of a state in the nondeterministic automaton we're defining.
269706b4fc4SDimitry Andric   using NfaStateTy = uint64_t;
27063faed5bSDimitry Andric 
2711d5ae102SDimitry Andric   // Given a resource state, return all resource states by applying
2721d5ae102SDimitry Andric   // InsnClass.
273706b4fc4SDimitry Andric   auto applyInsnClass = [&](const ResourceVector &InsnClass,
274706b4fc4SDimitry Andric                             NfaStateTy State) -> std::deque<NfaStateTy> {
275706b4fc4SDimitry Andric     std::deque<NfaStateTy> V(1, State);
2761d5ae102SDimitry Andric     // Apply every stage in the class individually.
277706b4fc4SDimitry Andric     for (NfaStateTy Stage : InsnClass) {
2781d5ae102SDimitry Andric       // Apply this stage to every existing member of V in turn.
2791d5ae102SDimitry Andric       size_t Sz = V.size();
2801d5ae102SDimitry Andric       for (unsigned I = 0; I < Sz; ++I) {
281706b4fc4SDimitry Andric         NfaStateTy S = V.front();
2821d5ae102SDimitry Andric         V.pop_front();
28363faed5bSDimitry Andric 
2841d5ae102SDimitry Andric         // For this stage, state combination, try all possible resources.
2851d5ae102SDimitry Andric         for (unsigned J = 0; J < DFA_MAX_RESOURCES; ++J) {
286706b4fc4SDimitry Andric           NfaStateTy ResourceMask = 1ULL << J;
2871d5ae102SDimitry Andric           if ((ResourceMask & Stage) == 0)
2881d5ae102SDimitry Andric             // This resource isn't required by this stage.
289dd58ef01SDimitry Andric             continue;
290706b4fc4SDimitry Andric           NfaStateTy Combo = ComboBitToBitsMap[ResourceMask];
2911d5ae102SDimitry Andric           if (Combo && ((~S & Combo) != Combo))
2921d5ae102SDimitry Andric             // This combo units bits are not available.
2931d5ae102SDimitry Andric             continue;
294706b4fc4SDimitry Andric           NfaStateTy ResultingResourceState = S | ResourceMask | Combo;
2951d5ae102SDimitry Andric           if (ResultingResourceState == S)
2961d5ae102SDimitry Andric             continue;
2971d5ae102SDimitry Andric           V.push_back(ResultingResourceState);
298dd58ef01SDimitry Andric         }
29963faed5bSDimitry Andric       }
3001d5ae102SDimitry Andric     }
3011d5ae102SDimitry Andric     return V;
3021d5ae102SDimitry Andric   };
30363faed5bSDimitry Andric 
3041d5ae102SDimitry Andric   // Given a resource state, return a quick (conservative) guess as to whether
3051d5ae102SDimitry Andric   // InsnClass can be applied. This is a filter for the more heavyweight
3061d5ae102SDimitry Andric   // applyInsnClass.
307706b4fc4SDimitry Andric   auto canApplyInsnClass = [](const ResourceVector &InsnClass,
3081d5ae102SDimitry Andric                               NfaStateTy State) -> bool {
309706b4fc4SDimitry Andric     for (NfaStateTy Resources : InsnClass) {
3101d5ae102SDimitry Andric       if ((State | Resources) == State)
3111d5ae102SDimitry Andric         return false;
3121d5ae102SDimitry Andric     }
3131d5ae102SDimitry Andric     return true;
3141d5ae102SDimitry Andric   };
3151d5ae102SDimitry Andric 
3161d5ae102SDimitry Andric   DfaEmitter Emitter;
3171d5ae102SDimitry Andric   std::deque<NfaStateTy> Worklist(1, 0);
3181d5ae102SDimitry Andric   std::set<NfaStateTy> SeenStates;
3191d5ae102SDimitry Andric   SeenStates.insert(Worklist.front());
3201d5ae102SDimitry Andric   while (!Worklist.empty()) {
3211d5ae102SDimitry Andric     NfaStateTy State = Worklist.front();
3221d5ae102SDimitry Andric     Worklist.pop_front();
323706b4fc4SDimitry Andric     for (const ResourceVector &Resources : UniqueResources) {
324706b4fc4SDimitry Andric       if (!canApplyInsnClass(Resources, State))
3251d5ae102SDimitry Andric         continue;
326706b4fc4SDimitry Andric       unsigned ResourcesID = UniqueResources.idFor(Resources);
327706b4fc4SDimitry Andric       for (uint64_t NewState : applyInsnClass(Resources, State)) {
3281d5ae102SDimitry Andric         if (SeenStates.emplace(NewState).second)
3291d5ae102SDimitry Andric           Worklist.emplace_back(NewState);
330706b4fc4SDimitry Andric         Emitter.addTransition(State, NewState, ResourcesID);
33163faed5bSDimitry Andric       }
33263faed5bSDimitry Andric     }
33363faed5bSDimitry Andric   }
33463faed5bSDimitry Andric 
3351d5ae102SDimitry Andric   std::string TargetAndDFAName = TargetName + DFAName;
3361d5ae102SDimitry Andric   Emitter.emit(TargetAndDFAName, OS);
3371d5ae102SDimitry Andric   OS << "} // end anonymous namespace\n\n";
3381d5ae102SDimitry Andric 
3391d5ae102SDimitry Andric   std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
3401d5ae102SDimitry Andric   OS << "namespace llvm {\n";
3411d5ae102SDimitry Andric   OS << "DFAPacketizer *" << SubTargetClassName << "::"
3421d5ae102SDimitry Andric      << "create" << DFAName
3431d5ae102SDimitry Andric      << "DFAPacketizer(const InstrItineraryData *IID) const {\n"
3441d5ae102SDimitry Andric      << "  static Automaton<uint64_t> A(ArrayRef<" << TargetAndDFAName
3451d5ae102SDimitry Andric      << "Transition>(" << TargetAndDFAName << "Transitions), "
3461d5ae102SDimitry Andric      << TargetAndDFAName << "TransitionInfo);\n"
347706b4fc4SDimitry Andric      << "  unsigned ProcResIdxStart = " << TargetAndDFAName
348706b4fc4SDimitry Andric      << "ProcResourceIndexStart[IID->SchedModel.ProcID];\n"
349706b4fc4SDimitry Andric      << "  unsigned ProcResIdxNum = " << TargetAndDFAName
350706b4fc4SDimitry Andric      << "ProcResourceIndexStart[IID->SchedModel.ProcID + 1] - "
351706b4fc4SDimitry Andric         "ProcResIdxStart;\n"
352706b4fc4SDimitry Andric      << "  return new DFAPacketizer(IID, A, {&" << TargetAndDFAName
353706b4fc4SDimitry Andric      << "ResourceIndices[ProcResIdxStart], ProcResIdxNum});\n"
3541d5ae102SDimitry Andric      << "\n}\n\n";
35563faed5bSDimitry Andric }
35658b69754SDimitry Andric 
3577fa27ce4SDimitry Andric static TableGen::Emitter::OptClass<DFAPacketizerEmitter>
3587fa27ce4SDimitry Andric     X("gen-dfa-packetizer", "Generate DFA Packetizer for VLIW targets");
359