blob: 17e9e40acd488640de9109c0a4226a22676fae78 [file] [log] [blame]
Eugene Zelenko6ac3f732016-01-26 18:48:36 +00001//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class parses the Schedule.td file and produces an API that can be used
11// to reason about whether an instruction can be added to a packet on a VLIW
12// architecture. The class internally generates a deterministic finite
13// automaton (DFA) that models all possible mappings of machine instructions
14// to functional units as instructions are added to a packet.
15//
16//===----------------------------------------------------------------------===//
17
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000018#define DEBUG_TYPE "dfa-emitter"
19
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000020#include "CodeGenTarget.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/ADT/DenseSet.h"
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +000022#include "llvm/ADT/STLExtras.h"
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000023#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000024#include "llvm/TableGen/Record.h"
25#include "llvm/TableGen/TableGenBackend.h"
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000026#include "llvm/Support/Debug.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000027#include <list>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000028#include <map>
29#include <string>
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000030#include <queue>
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000031
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000032using namespace llvm;
33
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000034// --------------------------------------------------------------------
35// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
36
37// DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
38// This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
39//
40// e.g. terms x resource bit combinations that fit in uint32_t:
41// 4 terms x 8 bits = 32 bits
42// 3 terms x 10 bits = 30 bits
43// 2 terms x 16 bits = 32 bits
44//
45// e.g. terms x resource bit combinations that fit in uint64_t:
46// 8 terms x 8 bits = 64 bits
47// 7 terms x 9 bits = 63 bits
48// 6 terms x 10 bits = 60 bits
49// 5 terms x 12 bits = 60 bits
50// 4 terms x 16 bits = 64 bits <--- current
51// 3 terms x 21 bits = 63 bits
52// 2 terms x 32 bits = 64 bits
53//
54#define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
55#define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
56
57typedef uint64_t DFAInput;
58typedef int64_t DFAStateInput;
59#define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
60
61namespace {
62 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
63 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
64 }
65
66 /// Return the DFAInput for an instruction class input vector.
67 /// This function is used in both DFAPacketizer.cpp and in
68 /// DFAPacketizerEmitter.cpp.
69 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
70 DFAInput InsnInput = 0;
71 assert ((InsnClass.size() <= DFA_MAX_RESTERMS) &&
72 "Exceeded maximum number of DFA terms");
73 for (auto U : InsnClass)
74 InsnInput = addDFAFuncUnits(InsnInput, U);
75 return InsnInput;
76 }
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000077} // end anonymous namespace
78
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000079// --------------------------------------------------------------------
80
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000081#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000082// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
83//
84// dbgsInsnClass - When debugging, print instruction class stages.
85//
86void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
87//
88// dbgsStateInfo - When debugging, print the set of state info.
89//
90void dbgsStateInfo(const std::set<unsigned> &stateInfo);
91//
92// dbgsIndent - When debugging, indent by the specified amount.
93//
94void dbgsIndent(unsigned indent);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000095#endif
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000096
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000097//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000098// class DFAPacketizerEmitter: class that generates and prints out the DFA
99// for resource tracking.
100//
101namespace {
102class DFAPacketizerEmitter {
103private:
104 std::string TargetName;
105 //
106 // allInsnClasses is the set of all possible resources consumed by an
107 // InstrStage.
108 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000109 std::vector<std::vector<unsigned>> allInsnClasses;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000110 RecordKeeper &Records;
111
112public:
113 DFAPacketizerEmitter(RecordKeeper &R);
114
115 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000116 // collectAllFuncUnits - Construct a map of function unit names to bits.
117 //
118 int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
119 std::map<std::string, unsigned> &FUNameToBitsMap,
120 int &maxResources,
121 raw_ostream &OS);
122
123 //
124 // collectAllComboFuncs - Construct a map from a combo function unit bit to
125 // the bits of all included functional units.
126 //
127 int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
128 std::map<std::string, unsigned> &FUNameToBitsMap,
129 std::map<unsigned, unsigned> &ComboBitToBitsMap,
130 raw_ostream &OS);
131
132 //
133 // collectOneInsnClass - Populate allInsnClasses with one instruction class.
134 //
135 int collectOneInsnClass(const std::string &ProcName,
136 std::vector<Record*> &ProcItinList,
137 std::map<std::string, unsigned> &FUNameToBitsMap,
138 Record *ItinData,
139 raw_ostream &OS);
140
141 //
142 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000143 // used in each stage.
144 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000145 int collectAllInsnClasses(const std::string &ProcName,
146 std::vector<Record*> &ProcItinList,
147 std::map<std::string, unsigned> &FUNameToBitsMap,
148 std::vector<Record*> &ItinDataList,
149 int &maxStages,
150 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000151
152 void run(raw_ostream &OS);
153};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000154} // end anonymous namespace
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000155
156//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000157//
158// State represents the usage of machine resources if the packet contains
159// a set of instruction classes.
160//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000161// Specifically, currentState is a set of bit-masks.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000162// The nth bit in a bit-mask indicates whether the nth resource is being used
163// by this state. The set of bit-masks in a state represent the different
164// possible outcomes of transitioning to this state.
Sebastian Pop9aa61372011-12-06 17:34:11 +0000165// For example: consider a two resource architecture: resource L and resource M
166// with three instruction classes: L, M, and L_or_M.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000167// From the initial state (currentState = 0x00), if we add instruction class
168// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
169// represents the possible resource states that can result from adding a L_or_M
170// instruction
171//
172// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Pop9aa61372011-12-06 17:34:11 +0000173// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000174//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000175// A State instance also contains a collection of transitions from that state:
176// a map from inputs to new states.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000177//
178namespace {
179class State {
180 public:
181 static int currentStateNum;
David Blaikied43046b2014-04-21 22:35:11 +0000182 // stateNum is the only member used for equality/ordering, all other members
183 // can be mutated even in const State objects.
184 const int stateNum;
185 mutable bool isInitial;
186 mutable std::set<unsigned> stateInfo;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000187 typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
David Blaikied43046b2014-04-21 22:35:11 +0000188 mutable TransitionMap Transitions;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000189
190 State();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000191
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000192 bool operator<(const State &s) const {
193 return stateNum < s.stateNum;
194 }
195
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000196 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000197 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
198 // may be a valid transition from this state i.e., can an instruction of type
199 // InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000200 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000201 // Note that for multiple stages, this quick check does not take into account
202 // any possible resource competition between the stages themselves. That is
203 // enforced in AddInsnClassStages which checks the cross product of all
204 // stages for resource availability (which is a more involved check).
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000205 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000206 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
207 std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000208 //
Krzysztof Parzyszek4ca21fc2015-11-21 17:38:33 +0000209 // AddInsnClass - Return all combinations of resource reservation
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000210 // which are possible from this state (PossibleStates).
211 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000212 // PossibleStates is the set of valid resource states that ensue from valid
213 // transitions.
214 //
215 void AddInsnClass(std::vector<unsigned> &InsnClass,
216 std::map<unsigned, unsigned> &ComboBitToBitsMap,
217 std::set<unsigned> &PossibleStates) const;
218 //
219 // AddInsnClassStages - Return all combinations of resource reservation
220 // resulting from the cross product of all stages for this InsnClass
221 // which are possible from this state (PossibleStates).
222 //
223 void AddInsnClassStages(std::vector<unsigned> &InsnClass,
224 std::map<unsigned, unsigned> &ComboBitToBitsMap,
225 unsigned chkstage, unsigned numstages,
226 unsigned prevState, unsigned origState,
227 DenseSet<unsigned> &VisitedResourceStates,
228 std::set<unsigned> &PossibleStates) const;
229 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000230 // addTransition - Add a transition from this state given the input InsnClass
231 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000232 void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000233 //
234 // hasTransition - Returns true if there is a transition from this state
235 // given the input InsnClass
236 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000237 bool hasTransition(std::vector<unsigned> InsnClass) const;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000238};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000239} // end anonymous namespace
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000240
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000241//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000242// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000243//
244namespace {
245class DFA {
246public:
247 DFA();
248
Sebastian Pop9aa61372011-12-06 17:34:11 +0000249 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikied43046b2014-04-21 22:35:11 +0000250 typedef std::set<State> StateSet;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000251 StateSet states;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000252
Sebastian Popac35a4d2011-12-06 17:34:16 +0000253 State *currentState;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000254
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000255 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000256 // Modify the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000257 //
David Blaikied43046b2014-04-21 22:35:11 +0000258 const State &newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000259
260 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000261 // writeTable: Print out a table representing the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000262 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000263 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
264 int numInsnClasses = 0,
265 int maxResources = 0, int numCombos = 0, int maxStages = 0);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000266};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000267} // end anonymous namespace
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000268
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000269#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000270// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
271//
272// dbgsInsnClass - When debugging, print instruction class stages.
273//
274void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
275 DEBUG(dbgs() << "InsnClass: ");
276 for (unsigned i = 0; i < InsnClass.size(); ++i) {
277 if (i > 0) {
278 DEBUG(dbgs() << ", ");
279 }
280 DEBUG(dbgs() << "0x" << utohexstr(InsnClass[i]));
281 }
282 DFAInput InsnInput = getDFAInsnInput(InsnClass);
283 DEBUG(dbgs() << " (input: 0x" << utohexstr(InsnInput) << ")");
284}
285
286//
287// dbgsStateInfo - When debugging, print the set of state info.
288//
289void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
290 DEBUG(dbgs() << "StateInfo: ");
291 unsigned i = 0;
292 for (std::set<unsigned>::iterator SI = stateInfo.begin();
293 SI != stateInfo.end(); ++SI, ++i) {
294 unsigned thisState = *SI;
295 if (i > 0) {
296 DEBUG(dbgs() << ", ");
297 }
298 DEBUG(dbgs() << "0x" << utohexstr(thisState));
299 }
300}
301
302//
303// dbgsIndent - When debugging, indent by the specified amount.
304//
305void dbgsIndent(unsigned indent) {
306 for (unsigned i = 0; i < indent; ++i) {
307 DEBUG(dbgs() << " ");
308 }
309}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000310#endif // NDEBUG
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000311
312//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000313// Constructors and destructors for State and DFA
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000314//
315State::State() :
316 stateNum(currentStateNum++), isInitial(false) {}
317
Craig Topper24064772014-04-15 07:20:03 +0000318DFA::DFA(): currentState(nullptr) {}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000319
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000320//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000321// addTransition - Add a transition from this state given the input InsnClass
322//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000323void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
324 const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000325 assert(!Transitions.count(InsnClass) &&
326 "Cannot have multiple transitions for the same input");
327 Transitions[InsnClass] = To;
328}
329
330//
331// hasTransition - Returns true if there is a transition from this state
332// given the input InsnClass
333//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000334bool State::hasTransition(std::vector<unsigned> InsnClass) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000335 return Transitions.count(InsnClass) > 0;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000336}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000337
338//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000339// AddInsnClass - Return all combinations of resource reservation
340// which are possible from this state (PossibleStates).
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000341//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000342// PossibleStates is the set of valid resource states that ensue from valid
343// transitions.
344//
345void State::AddInsnClass(std::vector<unsigned> &InsnClass,
346 std::map<unsigned, unsigned> &ComboBitToBitsMap,
347 std::set<unsigned> &PossibleStates) const {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000348 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000349 // Iterate over all resource states in currentState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000350 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000351 unsigned numstages = InsnClass.size();
352 assert((numstages > 0) && "InsnClass has no stages");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000353
354 for (std::set<unsigned>::iterator SI = stateInfo.begin();
355 SI != stateInfo.end(); ++SI) {
356 unsigned thisState = *SI;
357
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000358 DenseSet<unsigned> VisitedResourceStates;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000359
360 DEBUG(dbgs() << " thisState: 0x" << utohexstr(thisState) << "\n");
361 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
362 numstages - 1, numstages,
363 thisState, thisState,
364 VisitedResourceStates, PossibleStates);
365 }
366}
367
368void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
369 std::map<unsigned, unsigned> &ComboBitToBitsMap,
370 unsigned chkstage, unsigned numstages,
371 unsigned prevState, unsigned origState,
372 DenseSet<unsigned> &VisitedResourceStates,
373 std::set<unsigned> &PossibleStates) const {
374
375 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
376 unsigned thisStage = InsnClass[chkstage];
377
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000378 DEBUG({
379 dbgsIndent((1 + numstages - chkstage) << 1);
380 dbgs() << "AddInsnClassStages " << chkstage << " (0x"
381 << utohexstr(thisStage) << ") from ";
382 dbgsInsnClass(InsnClass);
383 dbgs() << "\n";
384 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000385
386 //
387 // Iterate over all possible resources used in thisStage.
388 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
389 //
390 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
391 unsigned resourceMask = (0x1 << j);
392 if (resourceMask & thisStage) {
393 unsigned combo = ComboBitToBitsMap[resourceMask];
394 if (combo && ((~prevState & combo) != combo)) {
395 DEBUG(dbgs() << "\tSkipped Add 0x" << utohexstr(prevState)
396 << " - combo op 0x" << utohexstr(resourceMask)
397 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
398 continue;
399 }
400 //
401 // For each possible resource used in thisStage, generate the
402 // resource state if that resource was used.
403 //
404 unsigned ResultingResourceState = prevState | resourceMask | combo;
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000405 DEBUG({
406 dbgsIndent((2 + numstages - chkstage) << 1);
407 dbgs() << "0x" << utohexstr(prevState)
408 << " | 0x" << utohexstr(resourceMask);
409 if (combo)
410 dbgs() << " | 0x" << utohexstr(combo);
411 dbgs() << " = 0x" << utohexstr(ResultingResourceState) << " ";
412 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000413
414 //
415 // If this is the final stage for this class
416 //
417 if (chkstage == 0) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000418 //
419 // Check if the resulting resource state can be accommodated in this
Sebastian Pop9aa61372011-12-06 17:34:11 +0000420 // packet.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000421 // We compute resource OR prevState (originally started as origState).
422 // If the result of the OR is different than origState, it implies
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000423 // that there is at least one resource that can be used to schedule
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000424 // thisStage in the current packet.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000425 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Pop9aa61372011-12-06 17:34:11 +0000426 // processed ResultingResourceState before.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000427 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000428 if (ResultingResourceState != prevState) {
429 if (VisitedResourceStates.count(ResultingResourceState) == 0) {
430 VisitedResourceStates.insert(ResultingResourceState);
431 PossibleStates.insert(ResultingResourceState);
432 DEBUG(dbgs() << "\tResultingResourceState: 0x"
433 << utohexstr(ResultingResourceState) << "\n");
434 } else {
435 DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
436 }
437 } else {
438 DEBUG(dbgs() << "\tSkipped Add - no final resources available\n");
439 }
440 } else {
441 //
442 // If the current resource can be accommodated, check the next
443 // stage in InsnClass for available resources.
444 //
445 if (ResultingResourceState != prevState) {
446 DEBUG(dbgs() << "\n");
447 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
448 chkstage - 1, numstages,
449 ResultingResourceState, origState,
450 VisitedResourceStates, PossibleStates);
451 } else {
452 DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000453 }
454 }
455 }
456 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000457}
458
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000459//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000460// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
461// may be a valid transition from this state i.e., can an instruction of type
462// InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000463//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000464// Note that this routine is performing conservative checks that can be
465// quickly executed acting as a filter before calling AddInsnClassStages.
466// Any cases allowed through here will be caught later in AddInsnClassStages
467// which performs the more expensive exact check.
468//
469bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
470 std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
Alexey Samsonov420a4ed2012-06-28 07:47:50 +0000471 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000472 SI != stateInfo.end(); ++SI) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000473
474 // Check to see if all required resources are available.
475 bool available = true;
476
477 // Inspect each stage independently.
478 // note: This is a conservative check as we aren't checking for
479 // possible resource competition between the stages themselves
480 // The full cross product is examined later in AddInsnClass.
481 for (unsigned i = 0; i < InsnClass.size(); ++i) {
482 unsigned resources = *SI;
483 if ((~resources & InsnClass[i]) == 0) {
484 available = false;
485 break;
486 }
487 // Make sure _all_ resources for a combo function are available.
488 // note: This is a quick conservative check as it won't catch an
489 // unscheduleable combo if this stage is an OR expression
490 // containing a combo.
491 // These cases are caught later in AddInsnClass.
492 unsigned combo = ComboBitToBitsMap[InsnClass[i]];
493 if (combo && ((~resources & combo) != combo)) {
494 DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x" << utohexstr(resources)
495 << " - combo op 0x" << utohexstr(InsnClass[i])
496 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
497 available = false;
498 break;
499 }
500 }
501
502 if (available) {
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000503 return true;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000504 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000505 }
506 return false;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000507}
508
David Blaikied43046b2014-04-21 22:35:11 +0000509const State &DFA::newState() {
David Blaikie4bcfcc12014-04-21 22:46:09 +0000510 auto IterPair = states.insert(State());
David Blaikied43046b2014-04-21 22:35:11 +0000511 assert(IterPair.second && "State already exists");
512 return *IterPair.first;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000513}
514
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000515int State::currentStateNum = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000516
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000517DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000518 TargetName(CodeGenTarget(R).getName()),
519 allInsnClasses(), Records(R) {}
520
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000521//
522// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Pop9aa61372011-12-06 17:34:11 +0000523// associated API to create a DFA packetizer.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000524//
525// Format:
526// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Pop9aa61372011-12-06 17:34:11 +0000527// transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000528// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Pop9aa61372011-12-06 17:34:11 +0000529// the ith state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000530//
531//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000532void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
533 int numInsnClasses,
534 int maxResources, int numCombos, int maxStages) {
535
536 unsigned numStates = states.size();
537
538 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
539 DEBUG(dbgs() << "writeTableAndAPI\n");
540 DEBUG(dbgs() << "Total states: " << numStates << "\n");
541
542 OS << "namespace llvm {\n";
543
544 OS << "\n// Input format:\n";
545 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
546 << "\t// maximum AND'ed resource terms\n";
547 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
548 << "\t// maximum resource bits in one term\n";
549
550 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
551 << "pairs of <Input, NextState> for all valid\n";
552 OS << "// transitions.\n";
553 OS << "// " << numStates << "\tstates\n";
554 OS << "// " << numInsnClasses << "\tinstruction classes\n";
555 OS << "// " << maxResources << "\tresources max\n";
556 OS << "// " << numCombos << "\tcombo resources\n";
557 OS << "// " << maxStages << "\tstages max\n";
558 OS << "const " << DFA_TBLTYPE << " "
559 << TargetName << "DFAStateInputTable[][2] = {\n";
560
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000561 // This table provides a map to the beginning of the transitions for State s
Sebastian Pop9aa61372011-12-06 17:34:11 +0000562 // in DFAStateInputTable.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000563 std::vector<int> StateEntry(numStates+1);
564 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000565
566 // Tracks the total valid transitions encountered so far. It is used
Sebastian Pop9aa61372011-12-06 17:34:11 +0000567 // to construct the StateEntry table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000568 int ValidTransitions = 0;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000569 DFA::StateSet::iterator SI = states.begin();
570 for (unsigned i = 0; i < numStates; ++i, ++SI) {
David Blaikied43046b2014-04-21 22:35:11 +0000571 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000572 StateEntry[i] = ValidTransitions;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000573 for (State::TransitionMap::iterator
David Blaikied43046b2014-04-21 22:35:11 +0000574 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000575 II != IE; ++II) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000576 OS << "{0x" << utohexstr(getDFAInsnInput(II->first)) << ", "
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000577 << II->second->stateNum
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000578 << "},\t";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000579 }
David Blaikied43046b2014-04-21 22:35:11 +0000580 ValidTransitions += SI->Transitions.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000581
Sebastian Pop9aa61372011-12-06 17:34:11 +0000582 // If there are no valid transitions from this stage, we need a sentinel
583 // transition.
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000584 if (ValidTransitions == StateEntry[i]) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000585 OS << SentinelEntry << ",\t";
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000586 ++ValidTransitions;
587 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000588
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000589 OS << " // state " << i << ": " << StateEntry[i];
590 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
591 OS << "-" << (ValidTransitions-1);
592 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000593 OS << "\n";
594 }
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000595
596 // Print out a sentinel entry at the end of the StateInputTable. This is
597 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000598 OS << SentinelEntry << "\t";
599 OS << " // state " << numStates << ": " << ValidTransitions;
600 OS << "\n";
601
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000602 OS << "};\n\n";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000603 OS << "// " << TargetName << "DFAStateEntryTable[i] = "
604 << "Index of the first entry in DFAStateInputTable for\n";
605 OS << "// "
606 << "the ith state.\n";
607 OS << "// " << numStates << " states\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000608 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
609
610 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Pop9aa61372011-12-06 17:34:11 +0000611 // two numbers.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000612 unsigned lastState = 0;
613 for (unsigned i = 0; i < numStates; ++i) {
614 if (i && ((i % 10) == 0)) {
615 lastState = i-1;
616 OS << " // states " << (i-10) << ":" << lastState << "\n";
617 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000618 OS << StateEntry[i] << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000619 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000620
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000621 // Print out the index to the sentinel entry in StateInputTable
622 OS << ValidTransitions << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000623 OS << " // states " << (lastState+1) << ":" << numStates << "\n";
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000624
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000625 OS << "};\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000626 OS << "} // namespace\n";
627
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000628 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000629 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000630 //
631 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
632 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
633 OS << "namespace llvm {\n";
Sebastian Popac35a4d2011-12-06 17:34:16 +0000634 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000635 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
636 << " return new DFAPacketizer(IID, " << TargetName
637 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
638 OS << "} // End llvm namespace \n";
639}
640
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000641//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000642// collectAllFuncUnits - Construct a map of function unit names to bits.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000643//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000644int DFAPacketizerEmitter::collectAllFuncUnits(
645 std::vector<Record*> &ProcItinList,
646 std::map<std::string, unsigned> &FUNameToBitsMap,
647 int &maxFUs,
648 raw_ostream &OS) {
649 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
650 DEBUG(dbgs() << "collectAllFuncUnits");
651 DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000652
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000653 int totalFUs = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000654 // Parse functional units for all the itineraries.
655 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
656 Record *Proc = ProcItinList[i];
657 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
658
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000659 DEBUG(dbgs() << " FU:" << i
660 << " (" << FUs.size() << " FUs) "
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000661 << Proc->getName());
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000662
663
Sebastian Pop9aa61372011-12-06 17:34:11 +0000664 // Convert macros to bits for each stage.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000665 unsigned numFUs = FUs.size();
666 for (unsigned j = 0; j < numFUs; ++j) {
667 assert ((j < DFA_MAX_RESOURCES) &&
668 "Exceeded maximum number of representable resources");
669 unsigned FuncResources = (unsigned) (1U << j);
670 FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
671 DEBUG(dbgs() << " " << FUs[j]->getName()
672 << ":0x" << utohexstr(FuncResources));
673 }
674 if (((int) numFUs) > maxFUs) {
675 maxFUs = numFUs;
676 }
677 totalFUs += numFUs;
678 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000679 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000680 return totalFUs;
681}
682
683//
684// collectAllComboFuncs - Construct a map from a combo function unit bit to
685// the bits of all included functional units.
686//
687int DFAPacketizerEmitter::collectAllComboFuncs(
688 std::vector<Record*> &ComboFuncList,
689 std::map<std::string, unsigned> &FUNameToBitsMap,
690 std::map<unsigned, unsigned> &ComboBitToBitsMap,
691 raw_ostream &OS) {
692 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
693 DEBUG(dbgs() << "collectAllComboFuncs");
694 DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
695
696 int numCombos = 0;
697 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
698 Record *Func = ComboFuncList[i];
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000699 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
700
701 DEBUG(dbgs() << " CFD:" << i
702 << " (" << FUs.size() << " combo FUs) "
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000703 << Func->getName() << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000704
705 // Convert macros to bits for each stage.
706 for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
707 assert ((j < DFA_MAX_RESOURCES) &&
708 "Exceeded maximum number of DFA resources");
709 Record *FuncData = FUs[j];
710 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
711 const std::vector<Record*> &FuncList =
712 FuncData->getValueAsListOfDefs("FuncList");
713 std::string ComboFuncName = ComboFunc->getName();
714 unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
715 unsigned ComboResources = ComboBit;
716 DEBUG(dbgs() << " combo: " << ComboFuncName
717 << ":0x" << utohexstr(ComboResources) << "\n");
718 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
719 std::string FuncName = FuncList[k]->getName();
720 unsigned FuncResources = FUNameToBitsMap[FuncName];
721 DEBUG(dbgs() << " " << FuncName
722 << ":0x" << utohexstr(FuncResources) << "\n");
723 ComboResources |= FuncResources;
724 }
725 ComboBitToBitsMap[ComboBit] = ComboResources;
726 numCombos++;
727 DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
728 << utohexstr(ComboBit) << " = 0x"
729 << utohexstr(ComboResources) << "\n");
730 }
731 }
732 return numCombos;
733}
734
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000735//
736// collectOneInsnClass - Populate allInsnClasses with one instruction class
737//
738int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
739 std::vector<Record*> &ProcItinList,
740 std::map<std::string, unsigned> &FUNameToBitsMap,
741 Record *ItinData,
742 raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000743 const std::vector<Record*> &StageList =
744 ItinData->getValueAsListOfDefs("Stages");
745
Sebastian Pop9aa61372011-12-06 17:34:11 +0000746 // The number of stages.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000747 unsigned NStages = StageList.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000748
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000749 DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000750 << "\n");
751
752 std::vector<unsigned> UnitBits;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000753
Sebastian Pop9aa61372011-12-06 17:34:11 +0000754 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000755 for (unsigned i = 0; i < NStages; ++i) {
756 const Record *Stage = StageList[i];
757
Sebastian Pop9aa61372011-12-06 17:34:11 +0000758 // Get unit list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000759 const std::vector<Record*> &UnitList =
760 Stage->getValueAsListOfDefs("Units");
761
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000762 DEBUG(dbgs() << " stage:" << i
763 << " [" << UnitList.size() << " units]:");
764 unsigned dbglen = 26; // cursor after stage dbgs
765
766 // Compute the bitwise or of each unit used in this stage.
767 unsigned UnitBitValue = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000768 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000769 // Conduct bitwise or.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000770 std::string UnitName = UnitList[j]->getName();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000771 DEBUG(dbgs() << " " << j << ":" << UnitName);
772 dbglen += 3 + UnitName.length();
773 assert(FUNameToBitsMap.count(UnitName));
774 UnitBitValue |= FUNameToBitsMap[UnitName];
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000775 }
776
777 if (UnitBitValue != 0)
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000778 UnitBits.push_back(UnitBitValue);
779
780 while (dbglen <= 64) { // line up bits dbgs
781 dbglen += 8;
782 DEBUG(dbgs() << "\t");
783 }
784 DEBUG(dbgs() << " (bits: 0x" << utohexstr(UnitBitValue) << ")\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000785 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000786
787 if (UnitBits.size() > 0)
788 allInsnClasses.push_back(UnitBits);
789
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000790 DEBUG({
791 dbgs() << " ";
792 dbgsInsnClass(UnitBits);
793 dbgs() << "\n";
794 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000795
796 return NStages;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000797}
798
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000799//
800// collectAllInsnClasses - Populate allInsnClasses which is a set of units
801// used in each stage.
802//
803int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
804 std::vector<Record*> &ProcItinList,
805 std::map<std::string, unsigned> &FUNameToBitsMap,
806 std::vector<Record*> &ItinDataList,
807 int &maxStages,
808 raw_ostream &OS) {
809 // Collect all instruction classes.
810 unsigned M = ItinDataList.size();
811
812 int numInsnClasses = 0;
813 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"
814 << "collectAllInsnClasses "
815 << ProcName
816 << " (" << M << " classes)\n");
817
818 // Collect stages for each instruction class for all itinerary data
819 for (unsigned j = 0; j < M; j++) {
820 Record *ItinData = ItinDataList[j];
821 int NStages = collectOneInsnClass(ProcName, ProcItinList,
822 FUNameToBitsMap, ItinData, OS);
823 if (NStages > maxStages) {
824 maxStages = NStages;
825 }
826 numInsnClasses++;
827 }
828 return numInsnClasses;
829}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000830
831//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000832// Run the worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000833//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000834void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000835
Sebastian Pop9aa61372011-12-06 17:34:11 +0000836 // Collect processor iteraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000837 std::vector<Record*> ProcItinList =
838 Records.getAllDerivedDefinitions("ProcessorItineraries");
839
840 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000841 // Collect the Functional units.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000842 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000843 std::map<std::string, unsigned> FUNameToBitsMap;
844 int maxResources = 0;
845 collectAllFuncUnits(ProcItinList,
846 FUNameToBitsMap, maxResources, OS);
847
848 //
849 // Collect the Combo Functional units.
850 //
851 std::map<unsigned, unsigned> ComboBitToBitsMap;
852 std::vector<Record*> ComboFuncList =
853 Records.getAllDerivedDefinitions("ComboFuncUnits");
854 int numCombos = collectAllComboFuncs(ComboFuncList,
855 FUNameToBitsMap, ComboBitToBitsMap, OS);
856
857 //
858 // Collect the itineraries.
859 //
860 int maxStages = 0;
861 int numInsnClasses = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000862 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
863 Record *Proc = ProcItinList[i];
864
Sebastian Pop9aa61372011-12-06 17:34:11 +0000865 // Get processor itinerary name.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000866 const std::string &ProcName = Proc->getName();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000867
Sebastian Pop9aa61372011-12-06 17:34:11 +0000868 // Skip default.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000869 if (ProcName == "NoItineraries")
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000870 continue;
871
Sebastian Pop9aa61372011-12-06 17:34:11 +0000872 // Sanity check for at least one instruction itinerary class.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000873 unsigned NItinClasses =
874 Records.getAllDerivedDefinitions("InstrItinClass").size();
875 if (NItinClasses == 0)
876 return;
877
Sebastian Pop9aa61372011-12-06 17:34:11 +0000878 // Get itinerary data list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000879 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
880
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000881 // Collect all instruction classes
882 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
883 FUNameToBitsMap, ItinDataList, maxStages, OS);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000884 }
885
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000886 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000887 // Run a worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000888 //
889 DFA D;
David Blaikied43046b2014-04-21 22:35:11 +0000890 const State *Initial = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000891 Initial->isInitial = true;
892 Initial->stateInfo.insert(0x0);
David Blaikied43046b2014-04-21 22:35:11 +0000893 SmallVector<const State*, 32> WorkList;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000894// std::queue<State*> WorkList;
David Blaikied43046b2014-04-21 22:35:11 +0000895 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000896
897 WorkList.push_back(Initial);
898
899 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000900 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000901 // C = {set of InsnClasses}
902 // Begin with initial node in worklist. Initial node does not have
903 // any consumed resources,
904 // ResourceState = 0x0
905 // Visited = {}
906 // While worklist != empty
907 // S = first element of worklist
908 // For every instruction class C
909 // if we can accommodate C in S:
910 // S' = state with resource states = {S Union C}
911 // Add a new transition: S x C -> S'
912 // If S' is not in Visited:
913 // Add S' to worklist
914 // Add S' to Visited
915 //
916 while (!WorkList.empty()) {
David Blaikied43046b2014-04-21 22:35:11 +0000917 const State *current = WorkList.pop_back_val();
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000918 DEBUG({
919 dbgs() << "---------------------\n";
920 dbgs() << "Processing state: " << current->stateNum << " - ";
921 dbgsStateInfo(current->stateInfo);
922 dbgs() << "\n";
923 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000924 for (unsigned i = 0; i < allInsnClasses.size(); i++) {
925 std::vector<unsigned> InsnClass = allInsnClasses[i];
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000926 DEBUG({
927 dbgs() << i << " ";
928 dbgsInsnClass(InsnClass);
929 dbgs() << "\n";
930 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000931
932 std::set<unsigned> NewStateResources;
933 //
934 // If we haven't already created a transition for this input
Sebastian Pop9aa61372011-12-06 17:34:11 +0000935 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000936 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000937 if (!current->hasTransition(InsnClass) &&
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000938 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000939 const State *NewState = nullptr;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000940 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
941 if (NewStateResources.size() == 0) {
942 DEBUG(dbgs() << " Skipped - no new states generated\n");
943 continue;
944 }
945
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000946 DEBUG({
947 dbgs() << "\t";
948 dbgsStateInfo(NewStateResources);
949 dbgs() << "\n";
950 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000951
952 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000953 // If we have seen this state before, then do not create a new state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000954 //
David Blaikied43046b2014-04-21 22:35:11 +0000955 auto VI = Visited.find(NewStateResources);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000956 if (VI != Visited.end()) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000957 NewState = VI->second;
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000958 DEBUG({
959 dbgs() << "\tFound existing state: " << NewState->stateNum
960 << " - ";
961 dbgsStateInfo(NewState->stateInfo);
962 dbgs() << "\n";
963 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000964 } else {
David Blaikied43046b2014-04-21 22:35:11 +0000965 NewState = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000966 NewState->stateInfo = NewStateResources;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000967 Visited[NewStateResources] = NewState;
968 WorkList.push_back(NewState);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000969 DEBUG({
970 dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
971 dbgsStateInfo(NewState->stateInfo);
972 dbgs() << "\n";
973 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000974 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000975
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000976 current->addTransition(InsnClass, NewState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000977 }
978 }
979 }
980
Sebastian Pop9aa61372011-12-06 17:34:11 +0000981 // Print out the table.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000982 D.writeTableAndAPI(OS, TargetName,
983 numInsnClasses, maxResources, numCombos, maxStages);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000984}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000985
986namespace llvm {
987
988void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
989 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
990 DFAPacketizerEmitter(RK).run(OS);
991}
992
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000993} // end namespaec llvm