1 //===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
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
7 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "dfa-emitter"
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"
36 // --------------------------------------------------------------------
37 // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
39 // DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
40 // This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
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
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
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.
59 typedef uint64_t DFAInput
;
60 typedef int64_t DFAStateInput
;
61 #define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
65 DFAInput
addDFAFuncUnits(DFAInput Inp
, unsigned FuncUnits
) {
66 return (Inp
<< DFA_MAX_RESOURCES
) | FuncUnits
;
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
);
81 } // end anonymous namespace
83 // --------------------------------------------------------------------
86 // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
88 // dbgsInsnClass - When debugging, print instruction class stages.
90 void dbgsInsnClass(const std::vector
<unsigned> &InsnClass
);
92 // dbgsStateInfo - When debugging, print the set of state info.
94 void dbgsStateInfo(const std::set
<unsigned> &stateInfo
);
96 // dbgsIndent - When debugging, indent by the specified amount.
98 void dbgsIndent(unsigned indent
);
102 // class DFAPacketizerEmitter: class that generates and prints out the DFA
103 // for resource tracking.
107 class DFAPacketizerEmitter
{
109 std::string TargetName
;
111 // allInsnClasses is the set of all possible resources consumed by an
114 std::vector
<std::vector
<unsigned>> allInsnClasses
;
115 RecordKeeper
&Records
;
118 DFAPacketizerEmitter(RecordKeeper
&R
);
121 // collectAllFuncUnits - Construct a map of function unit names to bits.
123 int collectAllFuncUnits(std::vector
<Record
*> &ProcItinList
,
124 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
129 // collectAllComboFuncs - Construct a map from a combo function unit bit to
130 // the bits of all included functional units.
132 int collectAllComboFuncs(std::vector
<Record
*> &ComboFuncList
,
133 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
134 std::map
<unsigned, unsigned> &ComboBitToBitsMap
,
138 // collectOneInsnClass - Populate allInsnClasses with one instruction class.
140 int collectOneInsnClass(const std::string
&ProcName
,
141 std::vector
<Record
*> &ProcItinList
,
142 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
147 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
148 // used in each stage.
150 int collectAllInsnClasses(const std::string
&ProcName
,
151 std::vector
<Record
*> &ProcItinList
,
152 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
153 std::vector
<Record
*> &ItinDataList
,
157 void run(raw_ostream
&OS
);
161 // State represents the usage of machine resources if the packet contains
162 // a set of instruction classes.
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
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].
178 // A State instance also contains a collection of transitions from that state:
179 // a map from inputs to new states.
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.
187 mutable bool isInitial
;
188 mutable std::set
<unsigned> stateInfo
;
189 typedef std::map
<std::vector
<unsigned>, const State
*> TransitionMap
;
190 mutable TransitionMap Transitions
;
194 bool operator<(const State
&s
) const {
195 return stateNum
< s
.stateNum
;
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.
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).
208 bool canMaybeAddInsnClass(std::vector
<unsigned> &InsnClass
,
209 std::map
<unsigned, unsigned> &ComboBitToBitsMap
) const;
212 // AddInsnClass - Return all combinations of resource reservation
213 // which are possible from this state (PossibleStates).
215 // PossibleStates is the set of valid resource states that ensue from valid
218 void AddInsnClass(std::vector
<unsigned> &InsnClass
,
219 std::map
<unsigned, unsigned> &ComboBitToBitsMap
,
220 std::set
<unsigned> &PossibleStates
) const;
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).
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;
235 // addTransition - Add a transition from this state given the input InsnClass
237 void addTransition(std::vector
<unsigned> InsnClass
, const State
*To
) const;
240 // hasTransition - Returns true if there is a transition from this state
241 // given the input InsnClass
243 bool hasTransition(std::vector
<unsigned> InsnClass
) const;
247 // class DFA: deterministic finite automaton for processor resource tracking.
253 // Set of states. Need to keep this sorted to emit the transition table.
254 typedef std::set
<State
> StateSet
;
257 State
*currentState
= nullptr;
262 const State
&newState();
265 // writeTable: Print out a table representing the DFA.
267 void writeTableAndAPI(raw_ostream
&OS
, const std::string
&ClassName
,
268 int numInsnClasses
= 0,
269 int maxResources
= 0, int numCombos
= 0, int maxStages
= 0);
272 } // end anonymous namespace
275 // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
277 // dbgsInsnClass - When debugging, print instruction class stages.
279 void dbgsInsnClass(const std::vector
<unsigned> &InsnClass
) {
280 LLVM_DEBUG(dbgs() << "InsnClass: ");
281 for (unsigned i
= 0; i
< InsnClass
.size(); ++i
) {
283 LLVM_DEBUG(dbgs() << ", ");
285 LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(InsnClass
[i
]));
287 DFAInput InsnInput
= getDFAInsnInput(InsnClass
);
288 LLVM_DEBUG(dbgs() << " (input: 0x" << Twine::utohexstr(InsnInput
) << ")");
292 // dbgsStateInfo - When debugging, print the set of state info.
294 void dbgsStateInfo(const std::set
<unsigned> &stateInfo
) {
295 LLVM_DEBUG(dbgs() << "StateInfo: ");
297 for (std::set
<unsigned>::iterator SI
= stateInfo
.begin();
298 SI
!= stateInfo
.end(); ++SI
, ++i
) {
299 unsigned thisState
= *SI
;
301 LLVM_DEBUG(dbgs() << ", ");
303 LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(thisState
));
308 // dbgsIndent - When debugging, indent by the specified amount.
310 void dbgsIndent(unsigned indent
) {
311 for (unsigned i
= 0; i
< indent
; ++i
) {
312 LLVM_DEBUG(dbgs() << " ");
318 // Constructors and destructors for State and DFA
321 stateNum(currentStateNum
++), isInitial(false) {}
324 // addTransition - Add a transition from this state given the input InsnClass
326 void State::addTransition(std::vector
<unsigned> InsnClass
, const State
*To
)
328 assert(!Transitions
.count(InsnClass
) &&
329 "Cannot have multiple transitions for the same input");
330 Transitions
[InsnClass
] = To
;
334 // hasTransition - Returns true if there is a transition from this state
335 // given the input InsnClass
337 bool State::hasTransition(std::vector
<unsigned> InsnClass
) const {
338 return Transitions
.count(InsnClass
) > 0;
342 // AddInsnClass - Return all combinations of resource reservation
343 // which are possible from this state (PossibleStates).
345 // PossibleStates is the set of valid resource states that ensue from valid
348 void State::AddInsnClass(std::vector
<unsigned> &InsnClass
,
349 std::map
<unsigned, unsigned> &ComboBitToBitsMap
,
350 std::set
<unsigned> &PossibleStates
) const {
352 // Iterate over all resource states in currentState.
354 unsigned numstages
= InsnClass
.size();
355 assert((numstages
> 0) && "InsnClass has no stages");
357 for (std::set
<unsigned>::iterator SI
= stateInfo
.begin();
358 SI
!= stateInfo
.end(); ++SI
) {
359 unsigned thisState
= *SI
;
361 DenseSet
<unsigned> VisitedResourceStates
;
363 LLVM_DEBUG(dbgs() << " thisState: 0x" << Twine::utohexstr(thisState
)
365 AddInsnClassStages(InsnClass
, ComboBitToBitsMap
,
366 numstages
- 1, numstages
,
367 thisState
, thisState
,
368 VisitedResourceStates
, PossibleStates
);
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
];
382 dbgsIndent((1 + numstages
- chkstage
) << 1);
383 dbgs() << "AddInsnClassStages " << chkstage
<< " (0x"
384 << Twine::utohexstr(thisStage
) << ") from ";
385 dbgsInsnClass(InsnClass
);
390 // Iterate over all possible resources used in thisStage.
391 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
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");
405 // For each possible resource used in thisStage, generate the
406 // resource state if that resource was used.
408 unsigned ResultingResourceState
= prevState
| resourceMask
| combo
;
410 dbgsIndent((2 + numstages
- chkstage
) << 1);
411 dbgs() << "0x" << Twine::utohexstr(prevState
) << " | 0x"
412 << Twine::utohexstr(resourceMask
);
414 dbgs() << " | 0x" << Twine::utohexstr(combo
);
415 dbgs() << " = 0x" << Twine::utohexstr(ResultingResourceState
) << " ";
419 // If this is the final stage for this class
423 // Check if the resulting resource state can be accommodated in this
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.
432 if (ResultingResourceState
!= prevState
) {
433 if (VisitedResourceStates
.count(ResultingResourceState
) == 0) {
434 VisitedResourceStates
.insert(ResultingResourceState
);
435 PossibleStates
.insert(ResultingResourceState
);
437 << "\tResultingResourceState: 0x"
438 << Twine::utohexstr(ResultingResourceState
) << "\n");
440 LLVM_DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
444 << "\tSkipped Add - no final resources available\n");
448 // If the current resource can be accommodated, check the next
449 // stage in InsnClass for available resources.
451 if (ResultingResourceState
!= prevState
) {
452 LLVM_DEBUG(dbgs() << "\n");
453 AddInsnClassStages(InsnClass
, ComboBitToBitsMap
,
454 chkstage
- 1, numstages
,
455 ResultingResourceState
, origState
,
456 VisitedResourceStates
, PossibleStates
);
458 LLVM_DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
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.
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.
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;
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) {
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");
516 const State
&DFA::newState() {
517 auto IterPair
= states
.insert(State());
518 assert(IterPair
.second
&& "State already exists");
519 return *IterPair
.first
;
522 int State::currentStateNum
= 0;
524 DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper
&R
):
525 TargetName(CodeGenTarget(R
).getName()), Records(R
) {}
528 // writeTableAndAPI - Print out a table representing the DFA and the
529 // associated API to create a DFA packetizer.
532 // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
534 // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
538 void DFA::writeTableAndAPI(raw_ostream
&OS
, const std::string
&TargetName
,
540 int maxResources
, int numCombos
, int maxStages
) {
541 unsigned numStates
= states
.size();
543 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
544 "----------------------\n");
545 LLVM_DEBUG(dbgs() << "writeTableAndAPI\n");
546 LLVM_DEBUG(dbgs() << "Total states: " << numStates
<< "\n");
548 OS
<< "namespace llvm {\n";
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";
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";
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}";
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();
582 OS
<< "{0x" << Twine::utohexstr(getDFAInsnInput(II
->first
)) << ", "
583 << II
->second
->stateNum
<< "},\t";
585 ValidTransitions
+= SI
->Transitions
.size();
587 // If there are no valid transitions from this stage, we need a sentinel
589 if (ValidTransitions
== StateEntry
[i
]) {
590 OS
<< SentinelEntry
<< ",\t";
594 OS
<< " // state " << i
<< ": " << StateEntry
[i
];
595 if (StateEntry
[i
] != (ValidTransitions
-1)) { // More than one transition.
596 OS
<< "-" << (ValidTransitions
-1);
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
;
608 OS
<< "// " << TargetName
<< "DFAStateEntryTable[i] = "
609 << "Index of the first entry in DFAStateInputTable for\n";
611 << "the ith state.\n";
612 OS
<< "// " << numStates
<< " states\n";
613 OS
<< "const unsigned int " << TargetName
<< "DFAStateEntryTable[] = {\n";
615 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
617 unsigned lastState
= 0;
618 for (unsigned i
= 0; i
< numStates
; ++i
) {
619 if (i
&& ((i
% 10) == 0)) {
621 OS
<< " // states " << (i
-10) << ":" << lastState
<< "\n";
623 OS
<< StateEntry
[i
] << ", ";
626 // Print out the index to the sentinel entry in StateInputTable
627 OS
<< ValidTransitions
<< ", ";
628 OS
<< " // states " << (lastState
+1) << ":" << numStates
<< "\n";
631 OS
<< "} // namespace\n";
634 // Emit DFA Packetizer tables if the target is a VLIW machine.
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";
647 // collectAllFuncUnits - Construct a map of function unit names to bits.
649 int DFAPacketizerEmitter::collectAllFuncUnits(
650 std::vector
<Record
*> &ProcItinList
,
651 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
654 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
655 "----------------------\n");
656 LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
657 LLVM_DEBUG(dbgs() << " (" << ProcItinList
.size() << " itineraries)\n");
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");
665 LLVM_DEBUG(dbgs() << " FU:" << i
<< " (" << FUs
.size() << " FUs) "
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
));
678 if (((int) numFUs
) > maxFUs
) {
682 LLVM_DEBUG(dbgs() << "\n");
688 // collectAllComboFuncs - Construct a map from a combo function unit bit to
689 // the bits of all included functional units.
691 int DFAPacketizerEmitter::collectAllComboFuncs(
692 std::vector
<Record
*> &ComboFuncList
,
693 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
694 std::map
<unsigned, unsigned> &ComboBitToBitsMap
,
696 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
697 "----------------------\n");
698 LLVM_DEBUG(dbgs() << "collectAllComboFuncs");
699 LLVM_DEBUG(dbgs() << " (" << ComboFuncList
.size() << " sets)\n");
702 for (unsigned i
= 0, N
= ComboFuncList
.size(); i
< N
; ++i
) {
703 Record
*Func
= ComboFuncList
[i
];
704 std::vector
<Record
*> FUs
= Func
->getValueAsListOfDefs("CFD");
706 LLVM_DEBUG(dbgs() << " CFD:" << i
<< " (" << FUs
.size() << " combo FUs) "
707 << Func
->getName() << "\n");
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
;
729 ComboBitToBitsMap
[ComboBit
] = ComboResources
;
731 LLVM_DEBUG(dbgs() << " => combo bits: " << ComboFuncName
<< ":0x"
732 << Twine::utohexstr(ComboBit
) << " = 0x"
733 << Twine::utohexstr(ComboResources
) << "\n");
740 // collectOneInsnClass - Populate allInsnClasses with one instruction class
742 int DFAPacketizerEmitter::collectOneInsnClass(const std::string
&ProcName
,
743 std::vector
<Record
*> &ProcItinList
,
744 std::map
<std::string
, unsigned> &FUNameToBitsMap
,
747 const std::vector
<Record
*> &StageList
=
748 ItinData
->getValueAsListOfDefs("Stages");
750 // The number of stages.
751 unsigned NStages
= StageList
.size();
753 LLVM_DEBUG(dbgs() << " " << ItinData
->getValueAsDef("TheClass")->getName()
756 std::vector
<unsigned> UnitBits
;
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
];
763 const std::vector
<Record
*> &UnitList
=
764 Stage
->getValueAsListOfDefs("Units");
766 LLVM_DEBUG(dbgs() << " stage:" << i
<< " [" << UnitList
.size()
768 unsigned dbglen
= 26; // cursor after stage dbgs
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
];
781 if (UnitBitValue
!= 0)
782 UnitBits
.push_back(UnitBitValue
);
784 while (dbglen
<= 64) { // line up bits dbgs
786 LLVM_DEBUG(dbgs() << "\t");
788 LLVM_DEBUG(dbgs() << " (bits: 0x" << Twine::utohexstr(UnitBitValue
)
792 if (!UnitBits
.empty())
793 allInsnClasses
.push_back(UnitBits
);
797 dbgsInsnClass(UnitBits
);
805 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
806 // used in each stage.
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
,
814 // Collect all instruction classes.
815 unsigned M
= ItinDataList
.size();
817 int numInsnClasses
= 0;
818 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
819 "----------------------\n"
820 << "collectAllInsnClasses " << ProcName
<< " (" << M
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
) {
833 return numInsnClasses
;
837 // Run the worklist algorithm to generate the DFA.
839 void DFAPacketizerEmitter::run(raw_ostream
&OS
) {
840 // Collect processor iteraries.
841 std::vector
<Record
*> ProcItinList
=
842 Records
.getAllDerivedDefinitions("ProcessorItineraries");
845 // Collect the Functional units.
847 std::map
<std::string
, unsigned> FUNameToBitsMap
;
848 int maxResources
= 0;
849 collectAllFuncUnits(ProcItinList
,
850 FUNameToBitsMap
, maxResources
, OS
);
853 // Collect the Combo Functional units.
855 std::map
<unsigned, unsigned> ComboBitToBitsMap
;
856 std::vector
<Record
*> ComboFuncList
=
857 Records
.getAllDerivedDefinitions("ComboFuncUnits");
858 int numCombos
= collectAllComboFuncs(ComboFuncList
,
859 FUNameToBitsMap
, ComboBitToBitsMap
, OS
);
862 // Collect the itineraries.
865 int numInsnClasses
= 0;
866 for (unsigned i
= 0, N
= ProcItinList
.size(); i
< N
; i
++) {
867 Record
*Proc
= ProcItinList
[i
];
869 // Get processor itinerary name.
870 const std::string
&ProcName
= Proc
->getName();
873 if (ProcName
== "NoItineraries")
876 // Sanity check for at least one instruction itinerary class.
877 unsigned NItinClasses
=
878 Records
.getAllDerivedDefinitions("InstrItinClass").size();
879 if (NItinClasses
== 0)
882 // Get itinerary data list.
883 std::vector
<Record
*> ItinDataList
= Proc
->getValueAsListOfDefs("IID");
885 // Collect all instruction classes
886 numInsnClasses
+= collectAllInsnClasses(ProcName
, ProcItinList
,
887 FUNameToBitsMap
, ItinDataList
, maxStages
, OS
);
891 // Run a worklist algorithm to generate the DFA.
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
;
900 WorkList
.push_back(Initial
);
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
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
919 while (!WorkList
.empty()) {
920 const State
*current
= WorkList
.pop_back_val();
922 dbgs() << "---------------------\n";
923 dbgs() << "Processing state: " << current
->stateNum
<< " - ";
924 dbgsStateInfo(current
->stateInfo
);
927 for (unsigned i
= 0; i
< allInsnClasses
.size(); i
++) {
928 std::vector
<unsigned> InsnClass
= allInsnClasses
[i
];
931 dbgsInsnClass(InsnClass
);
935 std::set
<unsigned> NewStateResources
;
937 // If we haven't already created a transition for this input
938 // and the state can accommodate this InsnClass, create a transition.
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");
951 dbgsStateInfo(NewStateResources
);
956 // If we have seen this state before, then do not create a new state.
958 auto VI
= Visited
.find(NewStateResources
);
959 if (VI
!= Visited
.end()) {
960 NewState
= VI
->second
;
962 dbgs() << "\tFound existing state: " << NewState
->stateNum
964 dbgsStateInfo(NewState
->stateInfo
);
968 NewState
= &D
.newState();
969 NewState
->stateInfo
= NewStateResources
;
970 Visited
[NewStateResources
] = NewState
;
971 WorkList
.push_back(NewState
);
973 dbgs() << "\tAccepted new state: " << NewState
->stateNum
<< " - ";
974 dbgsStateInfo(NewState
->stateInfo
);
979 current
->addTransition(InsnClass
, NewState
);
984 // Print out the table.
985 D
.writeTableAndAPI(OS
, TargetName
,
986 numInsnClasses
, maxResources
, numCombos
, maxStages
);
991 void EmitDFAPacketizer(RecordKeeper
&RK
, raw_ostream
&OS
) {
992 emitSourceFileHeader("Target DFA Packetizer Tables", OS
);
993 DFAPacketizerEmitter(RK
).run(OS
);
996 } // end namespace llvm