1 //===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class parses the Schedule.td file and produces an API that can be used
10 // to reason about whether an instruction can be added to a packet on a VLIW
11 // architecture. The class internally generates a deterministic finite
12 // automaton (DFA) that models all possible mappings of machine instructions
13 // to functional units as instructions are added to a packet.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #define DEBUG_TYPE "dfa-emitter"
18 
19 #include "CodeGenTarget.h"
20 #include "llvm/ADT/DenseSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/TableGen/Record.h"
24 #include "llvm/TableGen/TableGenBackend.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cassert>
28 #include <cstdint>
29 #include <map>
30 #include <set>
31 #include <string>
32 #include <vector>
33 
34 using namespace llvm;
35 
36 // --------------------------------------------------------------------
37 // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
38 
39 // DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
40 // This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
41 //
42 // e.g. terms x resource bit combinations that fit in uint32_t:
43 //      4 terms x 8  bits = 32 bits
44 //      3 terms x 10 bits = 30 bits
45 //      2 terms x 16 bits = 32 bits
46 //
47 // e.g. terms x resource bit combinations that fit in uint64_t:
48 //      8 terms x 8  bits = 64 bits
49 //      7 terms x 9  bits = 63 bits
50 //      6 terms x 10 bits = 60 bits
51 //      5 terms x 12 bits = 60 bits
52 //      4 terms x 16 bits = 64 bits <--- current
53 //      3 terms x 21 bits = 63 bits
54 //      2 terms x 32 bits = 64 bits
55 //
56 #define DFA_MAX_RESTERMS        4   // The max # of AND'ed resource terms.
57 #define DFA_MAX_RESOURCES       16  // The max # of resource bits in one term.
58 
59 typedef uint64_t                DFAInput;
60 typedef int64_t                 DFAStateInput;
61 #define DFA_TBLTYPE             "int64_t" // For generating DFAStateInputTable.
62 
63 namespace {
64 
65   DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
66     return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
67   }
68 
69   /// Return the DFAInput for an instruction class input vector.
70   /// This function is used in both DFAPacketizer.cpp and in
71   /// DFAPacketizerEmitter.cpp.
72   DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
73     DFAInput InsnInput = 0;
74     assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
75            "Exceeded maximum number of DFA terms");
76     for (auto U : InsnClass)
77       InsnInput = addDFAFuncUnits(InsnInput, U);
78     return InsnInput;
79   }
80 
81 } // end anonymous namespace
82 
83 // --------------------------------------------------------------------
84 
85 #ifndef NDEBUG
86 // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
87 //
88 // dbgsInsnClass - When debugging, print instruction class stages.
89 //
90 void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
91 //
92 // dbgsStateInfo - When debugging, print the set of state info.
93 //
94 void dbgsStateInfo(const std::set<unsigned> &stateInfo);
95 //
96 // dbgsIndent - When debugging, indent by the specified amount.
97 //
98 void dbgsIndent(unsigned indent);
99 #endif
100 
101 //
102 // class DFAPacketizerEmitter: class that generates and prints out the DFA
103 // for resource tracking.
104 //
105 namespace {
106 
107 class DFAPacketizerEmitter {
108 private:
109   std::string TargetName;
110   //
111   // allInsnClasses is the set of all possible resources consumed by an
112   // InstrStage.
113   //
114   std::vector<std::vector<unsigned>> allInsnClasses;
115   RecordKeeper &Records;
116 
117 public:
118   DFAPacketizerEmitter(RecordKeeper &R);
119 
120   //
121   // collectAllFuncUnits - Construct a map of function unit names to bits.
122   //
123   int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
124                            std::map<std::string, unsigned> &FUNameToBitsMap,
125                            int &maxResources,
126                            raw_ostream &OS);
127 
128   //
129   // collectAllComboFuncs - Construct a map from a combo function unit bit to
130   //                        the bits of all included functional units.
131   //
132   int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
133                            std::map<std::string, unsigned> &FUNameToBitsMap,
134                            std::map<unsigned, unsigned> &ComboBitToBitsMap,
135                            raw_ostream &OS);
136 
137   //
138   // collectOneInsnClass - Populate allInsnClasses with one instruction class.
139   //
140   int collectOneInsnClass(const std::string &ProcName,
141                            std::vector<Record*> &ProcItinList,
142                            std::map<std::string, unsigned> &FUNameToBitsMap,
143                            Record *ItinData,
144                            raw_ostream &OS);
145 
146   //
147   // collectAllInsnClasses - Populate allInsnClasses which is a set of units
148   // used in each stage.
149   //
150   int collectAllInsnClasses(const std::string &ProcName,
151                            std::vector<Record*> &ProcItinList,
152                            std::map<std::string, unsigned> &FUNameToBitsMap,
153                            std::vector<Record*> &ItinDataList,
154                            int &maxStages,
155                            raw_ostream &OS);
156 
157   void run(raw_ostream &OS);
158 };
159 
160 //
161 // State represents the usage of machine resources if the packet contains
162 // a set of instruction classes.
163 //
164 // Specifically, currentState is a set of bit-masks.
165 // The nth bit in a bit-mask indicates whether the nth resource is being used
166 // by this state. The set of bit-masks in a state represent the different
167 // possible outcomes of transitioning to this state.
168 // For example: consider a two resource architecture: resource L and resource M
169 // with three instruction classes: L, M, and L_or_M.
170 // From the initial state (currentState = 0x00), if we add instruction class
171 // L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
172 // represents the possible resource states that can result from adding a L_or_M
173 // instruction
174 //
175 // Another way of thinking about this transition is we are mapping a NDFA with
176 // two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
177 //
178 // A State instance also contains a collection of transitions from that state:
179 // a map from inputs to new states.
180 //
181 class State {
182  public:
183   static int currentStateNum;
184   // stateNum is the only member used for equality/ordering, all other members
185   // can be mutated even in const State objects.
186   const int stateNum;
187   mutable bool isInitial;
188   mutable std::set<unsigned> stateInfo;
189   typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
190   mutable TransitionMap Transitions;
191 
192   State();
193 
194   bool operator<(const State &s) const {
195     return stateNum < s.stateNum;
196   }
197 
198   //
199   // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
200   // may be a valid transition from this state i.e., can an instruction of type
201   // InsnClass be added to the packet represented by this state.
202   //
203   // Note that for multiple stages, this quick check does not take into account
204   // any possible resource competition between the stages themselves.  That is
205   // enforced in AddInsnClassStages which checks the cross product of all
206   // stages for resource availability (which is a more involved check).
207   //
208   bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
209                         std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
210 
211   //
212   // AddInsnClass - Return all combinations of resource reservation
213   // which are possible from this state (PossibleStates).
214   //
215   // PossibleStates is the set of valid resource states that ensue from valid
216   // transitions.
217   //
218   void AddInsnClass(std::vector<unsigned> &InsnClass,
219                         std::map<unsigned, unsigned> &ComboBitToBitsMap,
220                         std::set<unsigned> &PossibleStates) const;
221 
222   //
223   // AddInsnClassStages - Return all combinations of resource reservation
224   // resulting from the cross product of all stages for this InsnClass
225   // which are possible from this state (PossibleStates).
226   //
227   void AddInsnClassStages(std::vector<unsigned> &InsnClass,
228                         std::map<unsigned, unsigned> &ComboBitToBitsMap,
229                         unsigned chkstage, unsigned numstages,
230                         unsigned prevState, unsigned origState,
231                         DenseSet<unsigned> &VisitedResourceStates,
232                         std::set<unsigned> &PossibleStates) const;
233 
234   //
235   // addTransition - Add a transition from this state given the input InsnClass
236   //
237   void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
238 
239   //
240   // hasTransition - Returns true if there is a transition from this state
241   // given the input InsnClass
242   //
243   bool hasTransition(std::vector<unsigned> InsnClass) const;
244 };
245 
246 //
247 // class DFA: deterministic finite automaton for processor resource tracking.
248 //
249 class DFA {
250 public:
251   DFA() = default;
252 
253   // Set of states. Need to keep this sorted to emit the transition table.
254   typedef std::set<State> StateSet;
255   StateSet states;
256 
257   State *currentState = nullptr;
258 
259   //
260   // Modify the DFA.
261   //
262   const State &newState();
263 
264   //
265   // writeTable: Print out a table representing the DFA.
266   //
267   void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
268                  int numInsnClasses = 0,
269                  int maxResources = 0, int numCombos = 0, int maxStages = 0);
270 };
271 
272 } // end anonymous namespace
273 
274 #ifndef NDEBUG
275 // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
276 //
277 // dbgsInsnClass - When debugging, print instruction class stages.
278 //
279 void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
280   LLVM_DEBUG(dbgs() << "InsnClass: ");
281   for (unsigned i = 0; i < InsnClass.size(); ++i) {
282     if (i > 0) {
283       LLVM_DEBUG(dbgs() << ", ");
284     }
285     LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(InsnClass[i]));
286   }
287   DFAInput InsnInput = getDFAInsnInput(InsnClass);
288   LLVM_DEBUG(dbgs() << " (input: 0x" << Twine::utohexstr(InsnInput) << ")");
289 }
290 
291 //
292 // dbgsStateInfo - When debugging, print the set of state info.
293 //
294 void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
295   LLVM_DEBUG(dbgs() << "StateInfo: ");
296   unsigned i = 0;
297   for (std::set<unsigned>::iterator SI = stateInfo.begin();
298        SI != stateInfo.end(); ++SI, ++i) {
299     unsigned thisState = *SI;
300     if (i > 0) {
301       LLVM_DEBUG(dbgs() << ", ");
302     }
303     LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(thisState));
304   }
305 }
306 
307 //
308 // dbgsIndent - When debugging, indent by the specified amount.
309 //
310 void dbgsIndent(unsigned indent) {
311   for (unsigned i = 0; i < indent; ++i) {
312     LLVM_DEBUG(dbgs() << " ");
313   }
314 }
315 #endif // NDEBUG
316 
317 //
318 // Constructors and destructors for State and DFA
319 //
320 State::State() :
321   stateNum(currentStateNum++), isInitial(false) {}
322 
323 //
324 // addTransition - Add a transition from this state given the input InsnClass
325 //
326 void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
327       const {
328   assert(!Transitions.count(InsnClass) &&
329       "Cannot have multiple transitions for the same input");
330   Transitions[InsnClass] = To;
331 }
332 
333 //
334 // hasTransition - Returns true if there is a transition from this state
335 // given the input InsnClass
336 //
337 bool State::hasTransition(std::vector<unsigned> InsnClass) const {
338   return Transitions.count(InsnClass) > 0;
339 }
340 
341 //
342 // AddInsnClass - Return all combinations of resource reservation
343 // which are possible from this state (PossibleStates).
344 //
345 // PossibleStates is the set of valid resource states that ensue from valid
346 // transitions.
347 //
348 void State::AddInsnClass(std::vector<unsigned> &InsnClass,
349                         std::map<unsigned, unsigned> &ComboBitToBitsMap,
350                         std::set<unsigned> &PossibleStates) const {
351   //
352   // Iterate over all resource states in currentState.
353   //
354   unsigned numstages = InsnClass.size();
355   assert((numstages > 0) && "InsnClass has no stages");
356 
357   for (std::set<unsigned>::iterator SI = stateInfo.begin();
358        SI != stateInfo.end(); ++SI) {
359     unsigned thisState = *SI;
360 
361     DenseSet<unsigned> VisitedResourceStates;
362 
363     LLVM_DEBUG(dbgs() << "  thisState: 0x" << Twine::utohexstr(thisState)
364                       << "\n");
365     AddInsnClassStages(InsnClass, ComboBitToBitsMap,
366                                 numstages - 1, numstages,
367                                 thisState, thisState,
368                                 VisitedResourceStates, PossibleStates);
369   }
370 }
371 
372 void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
373                         std::map<unsigned, unsigned> &ComboBitToBitsMap,
374                         unsigned chkstage, unsigned numstages,
375                         unsigned prevState, unsigned origState,
376                         DenseSet<unsigned> &VisitedResourceStates,
377                         std::set<unsigned> &PossibleStates) const {
378   assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
379   unsigned thisStage = InsnClass[chkstage];
380 
381   LLVM_DEBUG({
382     dbgsIndent((1 + numstages - chkstage) << 1);
383     dbgs() << "AddInsnClassStages " << chkstage << " (0x"
384            << Twine::utohexstr(thisStage) << ") from ";
385     dbgsInsnClass(InsnClass);
386     dbgs() << "\n";
387   });
388 
389   //
390   // Iterate over all possible resources used in thisStage.
391   // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
392   //
393   for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
394     unsigned resourceMask = (0x1 << j);
395     if (resourceMask & thisStage) {
396       unsigned combo = ComboBitToBitsMap[resourceMask];
397       if (combo && ((~prevState & combo) != combo)) {
398         LLVM_DEBUG(dbgs() << "\tSkipped Add 0x" << Twine::utohexstr(prevState)
399                           << " - combo op 0x" << Twine::utohexstr(resourceMask)
400                           << " (0x" << Twine::utohexstr(combo)
401                           << ") cannot be scheduled\n");
402         continue;
403       }
404       //
405       // For each possible resource used in thisStage, generate the
406       // resource state if that resource was used.
407       //
408       unsigned ResultingResourceState = prevState | resourceMask | combo;
409       LLVM_DEBUG({
410         dbgsIndent((2 + numstages - chkstage) << 1);
411         dbgs() << "0x" << Twine::utohexstr(prevState) << " | 0x"
412                << Twine::utohexstr(resourceMask);
413         if (combo)
414           dbgs() << " | 0x" << Twine::utohexstr(combo);
415         dbgs() << " = 0x" << Twine::utohexstr(ResultingResourceState) << " ";
416       });
417 
418       //
419       // If this is the final stage for this class
420       //
421       if (chkstage == 0) {
422         //
423         // Check if the resulting resource state can be accommodated in this
424         // packet.
425         // We compute resource OR prevState (originally started as origState).
426         // If the result of the OR is different than origState, it implies
427         // that there is at least one resource that can be used to schedule
428         // thisStage in the current packet.
429         // Insert ResultingResourceState into PossibleStates only if we haven't
430         // processed ResultingResourceState before.
431         //
432         if (ResultingResourceState != prevState) {
433           if (VisitedResourceStates.count(ResultingResourceState) == 0) {
434             VisitedResourceStates.insert(ResultingResourceState);
435             PossibleStates.insert(ResultingResourceState);
436             LLVM_DEBUG(dbgs()
437                        << "\tResultingResourceState: 0x"
438                        << Twine::utohexstr(ResultingResourceState) << "\n");
439           } else {
440             LLVM_DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
441           }
442         } else {
443           LLVM_DEBUG(dbgs()
444                      << "\tSkipped Add - no final resources available\n");
445         }
446       } else {
447         //
448         // If the current resource can be accommodated, check the next
449         // stage in InsnClass for available resources.
450         //
451         if (ResultingResourceState != prevState) {
452           LLVM_DEBUG(dbgs() << "\n");
453           AddInsnClassStages(InsnClass, ComboBitToBitsMap,
454                                 chkstage - 1, numstages,
455                                 ResultingResourceState, origState,
456                                 VisitedResourceStates, PossibleStates);
457         } else {
458           LLVM_DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
459         }
460       }
461     }
462   }
463 }
464 
465 //
466 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
467 // may be a valid transition from this state i.e., can an instruction of type
468 // InsnClass be added to the packet represented by this state.
469 //
470 // Note that this routine is performing conservative checks that can be
471 // quickly executed acting as a filter before calling AddInsnClassStages.
472 // Any cases allowed through here will be caught later in AddInsnClassStages
473 // which performs the more expensive exact check.
474 //
475 bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
476                     std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
477   for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
478        SI != stateInfo.end(); ++SI) {
479     // Check to see if all required resources are available.
480     bool available = true;
481 
482     // Inspect each stage independently.
483     // note: This is a conservative check as we aren't checking for
484     //       possible resource competition between the stages themselves
485     //       The full cross product is examined later in AddInsnClass.
486     for (unsigned i = 0; i < InsnClass.size(); ++i) {
487       unsigned resources = *SI;
488       if ((~resources & InsnClass[i]) == 0) {
489         available = false;
490         break;
491       }
492       // Make sure _all_ resources for a combo function are available.
493       // note: This is a quick conservative check as it won't catch an
494       //       unscheduleable combo if this stage is an OR expression
495       //       containing a combo.
496       //       These cases are caught later in AddInsnClass.
497       unsigned combo = ComboBitToBitsMap[InsnClass[i]];
498       if (combo && ((~resources & combo) != combo)) {
499         LLVM_DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x"
500                           << Twine::utohexstr(resources) << " - combo op 0x"
501                           << Twine::utohexstr(InsnClass[i]) << " (0x"
502                           << Twine::utohexstr(combo)
503                           << ") cannot be scheduled\n");
504         available = false;
505         break;
506       }
507     }
508 
509     if (available) {
510       return true;
511     }
512   }
513   return false;
514 }
515 
516 const State &DFA::newState() {
517   auto IterPair = states.insert(State());
518   assert(IterPair.second && "State already exists");
519   return *IterPair.first;
520 }
521 
522 int State::currentStateNum = 0;
523 
524 DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
525   TargetName(CodeGenTarget(R).getName()), Records(R) {}
526 
527 //
528 // writeTableAndAPI - Print out a table representing the DFA and the
529 // associated API to create a DFA packetizer.
530 //
531 // Format:
532 // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
533 //                           transitions.
534 // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
535 //                         the ith state.
536 //
537 //
538 void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
539                            int numInsnClasses,
540                            int maxResources, int numCombos, int maxStages) {
541   unsigned numStates = states.size();
542 
543   LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
544                        "----------------------\n");
545   LLVM_DEBUG(dbgs() << "writeTableAndAPI\n");
546   LLVM_DEBUG(dbgs() << "Total states: " << numStates << "\n");
547 
548   OS << "namespace llvm {\n";
549 
550   OS << "\n// Input format:\n";
551   OS << "#define DFA_MAX_RESTERMS        " << DFA_MAX_RESTERMS
552      << "\t// maximum AND'ed resource terms\n";
553   OS << "#define DFA_MAX_RESOURCES       " << DFA_MAX_RESOURCES
554      << "\t// maximum resource bits in one term\n";
555 
556   OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
557      << "pairs of <Input, NextState> for all valid\n";
558   OS << "//                           transitions.\n";
559   OS << "// " << numStates << "\tstates\n";
560   OS << "// " << numInsnClasses << "\tinstruction classes\n";
561   OS << "// " << maxResources << "\tresources max\n";
562   OS << "// " << numCombos << "\tcombo resources\n";
563   OS << "// " << maxStages << "\tstages max\n";
564   OS << "const " << DFA_TBLTYPE << " "
565      << TargetName << "DFAStateInputTable[][2] = {\n";
566 
567   // This table provides a map to the beginning of the transitions for State s
568   // in DFAStateInputTable.
569   std::vector<int> StateEntry(numStates+1);
570   static const std::string SentinelEntry = "{-1, -1}";
571 
572   // Tracks the total valid transitions encountered so far. It is used
573   // to construct the StateEntry table.
574   int ValidTransitions = 0;
575   DFA::StateSet::iterator SI = states.begin();
576   for (unsigned i = 0; i < numStates; ++i, ++SI) {
577     assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
578     StateEntry[i] = ValidTransitions;
579     for (State::TransitionMap::iterator
580         II = SI->Transitions.begin(), IE = SI->Transitions.end();
581         II != IE; ++II) {
582       OS << "{0x" << Twine::utohexstr(getDFAInsnInput(II->first)) << ", "
583          << II->second->stateNum << "},\t";
584     }
585     ValidTransitions += SI->Transitions.size();
586 
587     // If there are no valid transitions from this stage, we need a sentinel
588     // transition.
589     if (ValidTransitions == StateEntry[i]) {
590       OS << SentinelEntry << ",\t";
591       ++ValidTransitions;
592     }
593 
594     OS << " // state " << i << ": " << StateEntry[i];
595     if (StateEntry[i] != (ValidTransitions-1)) {   // More than one transition.
596        OS << "-" << (ValidTransitions-1);
597     }
598     OS << "\n";
599   }
600 
601   // Print out a sentinel entry at the end of the StateInputTable. This is
602   // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
603   OS << SentinelEntry << "\t";
604   OS << " // state " << numStates << ": " << ValidTransitions;
605   OS << "\n";
606 
607   OS << "};\n\n";
608   OS << "// " << TargetName << "DFAStateEntryTable[i] = "
609      << "Index of the first entry in DFAStateInputTable for\n";
610   OS << "//                         "
611      << "the ith state.\n";
612   OS << "// " << numStates << " states\n";
613   OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
614 
615   // Multiply i by 2 since each entry in DFAStateInputTable is a set of
616   // two numbers.
617   unsigned lastState = 0;
618   for (unsigned i = 0; i < numStates; ++i) {
619     if (i && ((i % 10) == 0)) {
620         lastState = i-1;
621         OS << "   // states " << (i-10) << ":" << lastState << "\n";
622     }
623     OS << StateEntry[i] << ", ";
624   }
625 
626   // Print out the index to the sentinel entry in StateInputTable
627   OS << ValidTransitions << ", ";
628   OS << "   // states " << (lastState+1) << ":" << numStates << "\n";
629 
630   OS << "};\n";
631   OS << "} // namespace\n";
632 
633   //
634   // Emit DFA Packetizer tables if the target is a VLIW machine.
635   //
636   std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
637   OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
638   OS << "namespace llvm {\n";
639   OS << "DFAPacketizer *" << SubTargetClassName << "::"
640      << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
641      << "   return new DFAPacketizer(IID, " << TargetName
642      << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
643   OS << "} // End llvm namespace \n";
644 }
645 
646 //
647 // collectAllFuncUnits - Construct a map of function unit names to bits.
648 //
649 int DFAPacketizerEmitter::collectAllFuncUnits(
650                             std::vector<Record*> &ProcItinList,
651                             std::map<std::string, unsigned> &FUNameToBitsMap,
652                             int &maxFUs,
653                             raw_ostream &OS) {
654   LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
655                        "----------------------\n");
656   LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
657   LLVM_DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
658 
659   int totalFUs = 0;
660   // Parse functional units for all the itineraries.
661   for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
662     Record *Proc = ProcItinList[i];
663     std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
664 
665     LLVM_DEBUG(dbgs() << "    FU:" << i << " (" << FUs.size() << " FUs) "
666                       << Proc->getName());
667 
668     // Convert macros to bits for each stage.
669     unsigned numFUs = FUs.size();
670     for (unsigned j = 0; j < numFUs; ++j) {
671       assert ((j < DFA_MAX_RESOURCES) &&
672                       "Exceeded maximum number of representable resources");
673       unsigned FuncResources = (unsigned) (1U << j);
674       FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
675       LLVM_DEBUG(dbgs() << " " << FUs[j]->getName() << ":0x"
676                         << Twine::utohexstr(FuncResources));
677     }
678     if (((int) numFUs) > maxFUs) {
679       maxFUs = numFUs;
680     }
681     totalFUs += numFUs;
682     LLVM_DEBUG(dbgs() << "\n");
683   }
684   return totalFUs;
685 }
686 
687 //
688 // collectAllComboFuncs - Construct a map from a combo function unit bit to
689 //                        the bits of all included functional units.
690 //
691 int DFAPacketizerEmitter::collectAllComboFuncs(
692                             std::vector<Record*> &ComboFuncList,
693                             std::map<std::string, unsigned> &FUNameToBitsMap,
694                             std::map<unsigned, unsigned> &ComboBitToBitsMap,
695                             raw_ostream &OS) {
696   LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
697                        "----------------------\n");
698   LLVM_DEBUG(dbgs() << "collectAllComboFuncs");
699   LLVM_DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
700 
701   int numCombos = 0;
702   for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
703     Record *Func = ComboFuncList[i];
704     std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
705 
706     LLVM_DEBUG(dbgs() << "    CFD:" << i << " (" << FUs.size() << " combo FUs) "
707                       << Func->getName() << "\n");
708 
709     // Convert macros to bits for each stage.
710     for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
711       assert ((j < DFA_MAX_RESOURCES) &&
712                       "Exceeded maximum number of DFA resources");
713       Record *FuncData = FUs[j];
714       Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
715       const std::vector<Record*> &FuncList =
716                                    FuncData->getValueAsListOfDefs("FuncList");
717       const std::string &ComboFuncName = ComboFunc->getName();
718       unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
719       unsigned ComboResources = ComboBit;
720       LLVM_DEBUG(dbgs() << "      combo: " << ComboFuncName << ":0x"
721                         << Twine::utohexstr(ComboResources) << "\n");
722       for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
723         std::string FuncName = FuncList[k]->getName();
724         unsigned FuncResources = FUNameToBitsMap[FuncName];
725         LLVM_DEBUG(dbgs() << "        " << FuncName << ":0x"
726                           << Twine::utohexstr(FuncResources) << "\n");
727         ComboResources |= FuncResources;
728       }
729       ComboBitToBitsMap[ComboBit] = ComboResources;
730       numCombos++;
731       LLVM_DEBUG(dbgs() << "          => combo bits: " << ComboFuncName << ":0x"
732                         << Twine::utohexstr(ComboBit) << " = 0x"
733                         << Twine::utohexstr(ComboResources) << "\n");
734     }
735   }
736   return numCombos;
737 }
738 
739 //
740 // collectOneInsnClass - Populate allInsnClasses with one instruction class
741 //
742 int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
743                         std::vector<Record*> &ProcItinList,
744                         std::map<std::string, unsigned> &FUNameToBitsMap,
745                         Record *ItinData,
746                         raw_ostream &OS) {
747   const std::vector<Record*> &StageList =
748     ItinData->getValueAsListOfDefs("Stages");
749 
750   // The number of stages.
751   unsigned NStages = StageList.size();
752 
753   LLVM_DEBUG(dbgs() << "    " << ItinData->getValueAsDef("TheClass")->getName()
754                     << "\n");
755 
756   std::vector<unsigned> UnitBits;
757 
758   // Compute the bitwise or of each unit used in this stage.
759   for (unsigned i = 0; i < NStages; ++i) {
760     const Record *Stage = StageList[i];
761 
762     // Get unit list.
763     const std::vector<Record*> &UnitList =
764       Stage->getValueAsListOfDefs("Units");
765 
766     LLVM_DEBUG(dbgs() << "        stage:" << i << " [" << UnitList.size()
767                       << " units]:");
768     unsigned dbglen = 26;  // cursor after stage dbgs
769 
770     // Compute the bitwise or of each unit used in this stage.
771     unsigned UnitBitValue = 0;
772     for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
773       // Conduct bitwise or.
774       std::string UnitName = UnitList[j]->getName();
775       LLVM_DEBUG(dbgs() << " " << j << ":" << UnitName);
776       dbglen += 3 + UnitName.length();
777       assert(FUNameToBitsMap.count(UnitName));
778       UnitBitValue |= FUNameToBitsMap[UnitName];
779     }
780 
781     if (UnitBitValue != 0)
782       UnitBits.push_back(UnitBitValue);
783 
784     while (dbglen <= 64) {   // line up bits dbgs
785         dbglen += 8;
786         LLVM_DEBUG(dbgs() << "\t");
787     }
788     LLVM_DEBUG(dbgs() << " (bits: 0x" << Twine::utohexstr(UnitBitValue)
789                       << ")\n");
790   }
791 
792   if (!UnitBits.empty())
793     allInsnClasses.push_back(UnitBits);
794 
795   LLVM_DEBUG({
796     dbgs() << "        ";
797     dbgsInsnClass(UnitBits);
798     dbgs() << "\n";
799   });
800 
801   return NStages;
802 }
803 
804 //
805 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
806 // used in each stage.
807 //
808 int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
809                             std::vector<Record*> &ProcItinList,
810                             std::map<std::string, unsigned> &FUNameToBitsMap,
811                             std::vector<Record*> &ItinDataList,
812                             int &maxStages,
813                             raw_ostream &OS) {
814   // Collect all instruction classes.
815   unsigned M = ItinDataList.size();
816 
817   int numInsnClasses = 0;
818   LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
819                        "----------------------\n"
820                     << "collectAllInsnClasses " << ProcName << " (" << M
821                     << " classes)\n");
822 
823   // Collect stages for each instruction class for all itinerary data
824   for (unsigned j = 0; j < M; j++) {
825     Record *ItinData = ItinDataList[j];
826     int NStages = collectOneInsnClass(ProcName, ProcItinList,
827                                       FUNameToBitsMap, ItinData, OS);
828     if (NStages > maxStages) {
829       maxStages = NStages;
830     }
831     numInsnClasses++;
832   }
833   return numInsnClasses;
834 }
835 
836 //
837 // Run the worklist algorithm to generate the DFA.
838 //
839 void DFAPacketizerEmitter::run(raw_ostream &OS) {
840   // Collect processor iteraries.
841   std::vector<Record*> ProcItinList =
842     Records.getAllDerivedDefinitions("ProcessorItineraries");
843 
844   //
845   // Collect the Functional units.
846   //
847   std::map<std::string, unsigned> FUNameToBitsMap;
848   int maxResources = 0;
849   collectAllFuncUnits(ProcItinList,
850                               FUNameToBitsMap, maxResources, OS);
851 
852   //
853   // Collect the Combo Functional units.
854   //
855   std::map<unsigned, unsigned> ComboBitToBitsMap;
856   std::vector<Record*> ComboFuncList =
857     Records.getAllDerivedDefinitions("ComboFuncUnits");
858   int numCombos = collectAllComboFuncs(ComboFuncList,
859                               FUNameToBitsMap, ComboBitToBitsMap, OS);
860 
861   //
862   // Collect the itineraries.
863   //
864   int maxStages = 0;
865   int numInsnClasses = 0;
866   for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
867     Record *Proc = ProcItinList[i];
868 
869     // Get processor itinerary name.
870     const std::string &ProcName = Proc->getName();
871 
872     // Skip default.
873     if (ProcName == "NoItineraries")
874       continue;
875 
876     // Sanity check for at least one instruction itinerary class.
877     unsigned NItinClasses =
878       Records.getAllDerivedDefinitions("InstrItinClass").size();
879     if (NItinClasses == 0)
880       return;
881 
882     // Get itinerary data list.
883     std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
884 
885     // Collect all instruction classes
886     numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
887                           FUNameToBitsMap, ItinDataList, maxStages, OS);
888   }
889 
890   //
891   // Run a worklist algorithm to generate the DFA.
892   //
893   DFA D;
894   const State *Initial = &D.newState();
895   Initial->isInitial = true;
896   Initial->stateInfo.insert(0x0);
897   SmallVector<const State*, 32> WorkList;
898   std::map<std::set<unsigned>, const State*> Visited;
899 
900   WorkList.push_back(Initial);
901 
902   //
903   // Worklist algorithm to create a DFA for processor resource tracking.
904   // C = {set of InsnClasses}
905   // Begin with initial node in worklist. Initial node does not have
906   // any consumed resources,
907   //     ResourceState = 0x0
908   // Visited = {}
909   // While worklist != empty
910   //    S = first element of worklist
911   //    For every instruction class C
912   //      if we can accommodate C in S:
913   //          S' = state with resource states = {S Union C}
914   //          Add a new transition: S x C -> S'
915   //          If S' is not in Visited:
916   //             Add S' to worklist
917   //             Add S' to Visited
918   //
919   while (!WorkList.empty()) {
920     const State *current = WorkList.pop_back_val();
921     LLVM_DEBUG({
922       dbgs() << "---------------------\n";
923       dbgs() << "Processing state: " << current->stateNum << " - ";
924       dbgsStateInfo(current->stateInfo);
925       dbgs() << "\n";
926     });
927     for (unsigned i = 0; i < allInsnClasses.size(); i++) {
928       std::vector<unsigned> InsnClass = allInsnClasses[i];
929       LLVM_DEBUG({
930         dbgs() << i << " ";
931         dbgsInsnClass(InsnClass);
932         dbgs() << "\n";
933       });
934 
935       std::set<unsigned> NewStateResources;
936       //
937       // If we haven't already created a transition for this input
938       // and the state can accommodate this InsnClass, create a transition.
939       //
940       if (!current->hasTransition(InsnClass) &&
941           current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
942         const State *NewState = nullptr;
943         current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
944         if (NewStateResources.empty()) {
945           LLVM_DEBUG(dbgs() << "  Skipped - no new states generated\n");
946           continue;
947         }
948 
949         LLVM_DEBUG({
950           dbgs() << "\t";
951           dbgsStateInfo(NewStateResources);
952           dbgs() << "\n";
953         });
954 
955         //
956         // If we have seen this state before, then do not create a new state.
957         //
958         auto VI = Visited.find(NewStateResources);
959         if (VI != Visited.end()) {
960           NewState = VI->second;
961           LLVM_DEBUG({
962             dbgs() << "\tFound existing state: " << NewState->stateNum
963                    << " - ";
964             dbgsStateInfo(NewState->stateInfo);
965             dbgs() << "\n";
966           });
967         } else {
968           NewState = &D.newState();
969           NewState->stateInfo = NewStateResources;
970           Visited[NewStateResources] = NewState;
971           WorkList.push_back(NewState);
972           LLVM_DEBUG({
973             dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
974             dbgsStateInfo(NewState->stateInfo);
975             dbgs() << "\n";
976           });
977         }
978 
979         current->addTransition(InsnClass, NewState);
980       }
981     }
982   }
983 
984   // Print out the table.
985   D.writeTableAndAPI(OS, TargetName,
986                numInsnClasses, maxResources, numCombos, maxStages);
987 }
988 
989 namespace llvm {
990 
991 void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
992   emitSourceFileHeader("Target DFA Packetizer Tables", OS);
993   DFAPacketizerEmitter(RK).run(OS);
994 }
995 
996 } // end namespace llvm
997