blob: 77afff7ab5c230cbe80e893ae092711cbd5865ae [file] [log] [blame]
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine-----===//
2//
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>
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000031using namespace llvm;
32
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000033// --------------------------------------------------------------------
34// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
35
36// DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
37// This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
38//
39// e.g. terms x resource bit combinations that fit in uint32_t:
40// 4 terms x 8 bits = 32 bits
41// 3 terms x 10 bits = 30 bits
42// 2 terms x 16 bits = 32 bits
43//
44// e.g. terms x resource bit combinations that fit in uint64_t:
45// 8 terms x 8 bits = 64 bits
46// 7 terms x 9 bits = 63 bits
47// 6 terms x 10 bits = 60 bits
48// 5 terms x 12 bits = 60 bits
49// 4 terms x 16 bits = 64 bits <--- current
50// 3 terms x 21 bits = 63 bits
51// 2 terms x 32 bits = 64 bits
52//
53#define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
54#define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
55
56typedef uint64_t DFAInput;
57typedef int64_t DFAStateInput;
58#define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
59
60namespace {
61 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
62 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
63 }
64
65 /// Return the DFAInput for an instruction class input vector.
66 /// This function is used in both DFAPacketizer.cpp and in
67 /// DFAPacketizerEmitter.cpp.
68 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
69 DFAInput InsnInput = 0;
70 assert ((InsnClass.size() <= DFA_MAX_RESTERMS) &&
71 "Exceeded maximum number of DFA terms");
72 for (auto U : InsnClass)
73 InsnInput = addDFAFuncUnits(InsnInput, U);
74 return InsnInput;
75 }
76}
77// --------------------------------------------------------------------
78
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000079#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000080// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
81//
82// dbgsInsnClass - When debugging, print instruction class stages.
83//
84void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
85//
86// dbgsStateInfo - When debugging, print the set of state info.
87//
88void dbgsStateInfo(const std::set<unsigned> &stateInfo);
89//
90// dbgsIndent - When debugging, indent by the specified amount.
91//
92void dbgsIndent(unsigned indent);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000093#endif
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000094
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000095//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000096// class DFAPacketizerEmitter: class that generates and prints out the DFA
97// for resource tracking.
98//
99namespace {
100class DFAPacketizerEmitter {
101private:
102 std::string TargetName;
103 //
104 // allInsnClasses is the set of all possible resources consumed by an
105 // InstrStage.
106 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000107 std::vector<std::vector<unsigned>> allInsnClasses;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000108 RecordKeeper &Records;
109
110public:
111 DFAPacketizerEmitter(RecordKeeper &R);
112
113 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000114 // collectAllFuncUnits - Construct a map of function unit names to bits.
115 //
116 int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
117 std::map<std::string, unsigned> &FUNameToBitsMap,
118 int &maxResources,
119 raw_ostream &OS);
120
121 //
122 // collectAllComboFuncs - Construct a map from a combo function unit bit to
123 // the bits of all included functional units.
124 //
125 int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
126 std::map<std::string, unsigned> &FUNameToBitsMap,
127 std::map<unsigned, unsigned> &ComboBitToBitsMap,
128 raw_ostream &OS);
129
130 //
131 // collectOneInsnClass - Populate allInsnClasses with one instruction class.
132 //
133 int collectOneInsnClass(const std::string &ProcName,
134 std::vector<Record*> &ProcItinList,
135 std::map<std::string, unsigned> &FUNameToBitsMap,
136 Record *ItinData,
137 raw_ostream &OS);
138
139 //
140 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000141 // used in each stage.
142 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000143 int collectAllInsnClasses(const std::string &ProcName,
144 std::vector<Record*> &ProcItinList,
145 std::map<std::string, unsigned> &FUNameToBitsMap,
146 std::vector<Record*> &ItinDataList,
147 int &maxStages,
148 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000149
150 void run(raw_ostream &OS);
151};
152} // End anonymous namespace.
153
154//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000155//
156// State represents the usage of machine resources if the packet contains
157// a set of instruction classes.
158//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000159// Specifically, currentState is a set of bit-masks.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000160// The nth bit in a bit-mask indicates whether the nth resource is being used
161// by this state. The set of bit-masks in a state represent the different
162// possible outcomes of transitioning to this state.
Sebastian Pop9aa61372011-12-06 17:34:11 +0000163// For example: consider a two resource architecture: resource L and resource M
164// with three instruction classes: L, M, and L_or_M.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000165// From the initial state (currentState = 0x00), if we add instruction class
166// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
167// represents the possible resource states that can result from adding a L_or_M
168// instruction
169//
170// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Pop9aa61372011-12-06 17:34:11 +0000171// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000172//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000173// A State instance also contains a collection of transitions from that state:
174// a map from inputs to new states.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000175//
176namespace {
177class State {
178 public:
179 static int currentStateNum;
David Blaikied43046b2014-04-21 22:35:11 +0000180 // stateNum is the only member used for equality/ordering, all other members
181 // can be mutated even in const State objects.
182 const int stateNum;
183 mutable bool isInitial;
184 mutable std::set<unsigned> stateInfo;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000185 typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
David Blaikied43046b2014-04-21 22:35:11 +0000186 mutable TransitionMap Transitions;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000187
188 State();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000189
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000190 bool operator<(const State &s) const {
191 return stateNum < s.stateNum;
192 }
193
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000194 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000195 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
196 // may be a valid transition from this state i.e., can an instruction of type
197 // InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000198 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000199 // Note that for multiple stages, this quick check does not take into account
200 // any possible resource competition between the stages themselves. That is
201 // enforced in AddInsnClassStages which checks the cross product of all
202 // stages for resource availability (which is a more involved check).
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000203 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000204 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
205 std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000206 //
Krzysztof Parzyszek4ca21fc2015-11-21 17:38:33 +0000207 // AddInsnClass - Return all combinations of resource reservation
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000208 // which are possible from this state (PossibleStates).
209 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000210 // PossibleStates is the set of valid resource states that ensue from valid
211 // transitions.
212 //
213 void AddInsnClass(std::vector<unsigned> &InsnClass,
214 std::map<unsigned, unsigned> &ComboBitToBitsMap,
215 std::set<unsigned> &PossibleStates) const;
216 //
217 // AddInsnClassStages - Return all combinations of resource reservation
218 // resulting from the cross product of all stages for this InsnClass
219 // which are possible from this state (PossibleStates).
220 //
221 void AddInsnClassStages(std::vector<unsigned> &InsnClass,
222 std::map<unsigned, unsigned> &ComboBitToBitsMap,
223 unsigned chkstage, unsigned numstages,
224 unsigned prevState, unsigned origState,
225 DenseSet<unsigned> &VisitedResourceStates,
226 std::set<unsigned> &PossibleStates) const;
227 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000228 // addTransition - Add a transition from this state given the input InsnClass
229 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000230 void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000231 //
232 // hasTransition - Returns true if there is a transition from this state
233 // given the input InsnClass
234 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000235 bool hasTransition(std::vector<unsigned> InsnClass) const;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000236};
Sebastian Pop9aa61372011-12-06 17:34:11 +0000237} // End anonymous namespace.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000238
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000239//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000240// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000241//
242namespace {
243class DFA {
244public:
245 DFA();
246
Sebastian Pop9aa61372011-12-06 17:34:11 +0000247 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikied43046b2014-04-21 22:35:11 +0000248 typedef std::set<State> StateSet;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000249 StateSet states;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000250
Sebastian Popac35a4d2011-12-06 17:34:16 +0000251 State *currentState;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000252
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000253 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000254 // Modify the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000255 //
David Blaikied43046b2014-04-21 22:35:11 +0000256 const State &newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000257
258 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000259 // writeTable: Print out a table representing the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000260 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000261 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
262 int numInsnClasses = 0,
263 int maxResources = 0, int numCombos = 0, int maxStages = 0);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000264};
Sebastian Pop9aa61372011-12-06 17:34:11 +0000265} // End anonymous namespace.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000266
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000267#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000268// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
269//
270// dbgsInsnClass - When debugging, print instruction class stages.
271//
272void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
273 DEBUG(dbgs() << "InsnClass: ");
274 for (unsigned i = 0; i < InsnClass.size(); ++i) {
275 if (i > 0) {
276 DEBUG(dbgs() << ", ");
277 }
278 DEBUG(dbgs() << "0x" << utohexstr(InsnClass[i]));
279 }
280 DFAInput InsnInput = getDFAInsnInput(InsnClass);
281 DEBUG(dbgs() << " (input: 0x" << utohexstr(InsnInput) << ")");
282}
283
284//
285// dbgsStateInfo - When debugging, print the set of state info.
286//
287void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
288 DEBUG(dbgs() << "StateInfo: ");
289 unsigned i = 0;
290 for (std::set<unsigned>::iterator SI = stateInfo.begin();
291 SI != stateInfo.end(); ++SI, ++i) {
292 unsigned thisState = *SI;
293 if (i > 0) {
294 DEBUG(dbgs() << ", ");
295 }
296 DEBUG(dbgs() << "0x" << utohexstr(thisState));
297 }
298}
299
300//
301// dbgsIndent - When debugging, indent by the specified amount.
302//
303void dbgsIndent(unsigned indent) {
304 for (unsigned i = 0; i < indent; ++i) {
305 DEBUG(dbgs() << " ");
306 }
307}
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000308#endif
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000309
310//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000311// Constructors and destructors for State and DFA
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000312//
313State::State() :
314 stateNum(currentStateNum++), isInitial(false) {}
315
Craig Topper24064772014-04-15 07:20:03 +0000316DFA::DFA(): currentState(nullptr) {}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000317
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000318//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000319// addTransition - Add a transition from this state given the input InsnClass
320//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000321void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
322 const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000323 assert(!Transitions.count(InsnClass) &&
324 "Cannot have multiple transitions for the same input");
325 Transitions[InsnClass] = To;
326}
327
328//
329// hasTransition - Returns true if there is a transition from this state
330// given the input InsnClass
331//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000332bool State::hasTransition(std::vector<unsigned> InsnClass) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000333 return Transitions.count(InsnClass) > 0;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000334}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000335
336//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000337// AddInsnClass - Return all combinations of resource reservation
338// which are possible from this state (PossibleStates).
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000339//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000340// PossibleStates is the set of valid resource states that ensue from valid
341// transitions.
342//
343void State::AddInsnClass(std::vector<unsigned> &InsnClass,
344 std::map<unsigned, unsigned> &ComboBitToBitsMap,
345 std::set<unsigned> &PossibleStates) const {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000346 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000347 // Iterate over all resource states in currentState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000348 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000349 unsigned numstages = InsnClass.size();
350 assert((numstages > 0) && "InsnClass has no stages");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000351
352 for (std::set<unsigned>::iterator SI = stateInfo.begin();
353 SI != stateInfo.end(); ++SI) {
354 unsigned thisState = *SI;
355
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000356 DenseSet<unsigned> VisitedResourceStates;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000357
358 DEBUG(dbgs() << " thisState: 0x" << utohexstr(thisState) << "\n");
359 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
360 numstages - 1, numstages,
361 thisState, thisState,
362 VisitedResourceStates, PossibleStates);
363 }
364}
365
366void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
367 std::map<unsigned, unsigned> &ComboBitToBitsMap,
368 unsigned chkstage, unsigned numstages,
369 unsigned prevState, unsigned origState,
370 DenseSet<unsigned> &VisitedResourceStates,
371 std::set<unsigned> &PossibleStates) const {
372
373 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
374 unsigned thisStage = InsnClass[chkstage];
375
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000376 DEBUG({
377 dbgsIndent((1 + numstages - chkstage) << 1);
378 dbgs() << "AddInsnClassStages " << chkstage << " (0x"
379 << utohexstr(thisStage) << ") from ";
380 dbgsInsnClass(InsnClass);
381 dbgs() << "\n";
382 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000383
384 //
385 // Iterate over all possible resources used in thisStage.
386 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
387 //
388 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
389 unsigned resourceMask = (0x1 << j);
390 if (resourceMask & thisStage) {
391 unsigned combo = ComboBitToBitsMap[resourceMask];
392 if (combo && ((~prevState & combo) != combo)) {
393 DEBUG(dbgs() << "\tSkipped Add 0x" << utohexstr(prevState)
394 << " - combo op 0x" << utohexstr(resourceMask)
395 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
396 continue;
397 }
398 //
399 // For each possible resource used in thisStage, generate the
400 // resource state if that resource was used.
401 //
402 unsigned ResultingResourceState = prevState | resourceMask | combo;
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000403 DEBUG({
404 dbgsIndent((2 + numstages - chkstage) << 1);
405 dbgs() << "0x" << utohexstr(prevState)
406 << " | 0x" << utohexstr(resourceMask);
407 if (combo)
408 dbgs() << " | 0x" << utohexstr(combo);
409 dbgs() << " = 0x" << utohexstr(ResultingResourceState) << " ";
410 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000411
412 //
413 // If this is the final stage for this class
414 //
415 if (chkstage == 0) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000416 //
417 // Check if the resulting resource state can be accommodated in this
Sebastian Pop9aa61372011-12-06 17:34:11 +0000418 // packet.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000419 // We compute resource OR prevState (originally started as origState).
420 // If the result of the OR is different than origState, it implies
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000421 // that there is at least one resource that can be used to schedule
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000422 // thisStage in the current packet.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000423 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Pop9aa61372011-12-06 17:34:11 +0000424 // processed ResultingResourceState before.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000425 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000426 if (ResultingResourceState != prevState) {
427 if (VisitedResourceStates.count(ResultingResourceState) == 0) {
428 VisitedResourceStates.insert(ResultingResourceState);
429 PossibleStates.insert(ResultingResourceState);
430 DEBUG(dbgs() << "\tResultingResourceState: 0x"
431 << utohexstr(ResultingResourceState) << "\n");
432 } else {
433 DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
434 }
435 } else {
436 DEBUG(dbgs() << "\tSkipped Add - no final resources available\n");
437 }
438 } else {
439 //
440 // If the current resource can be accommodated, check the next
441 // stage in InsnClass for available resources.
442 //
443 if (ResultingResourceState != prevState) {
444 DEBUG(dbgs() << "\n");
445 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
446 chkstage - 1, numstages,
447 ResultingResourceState, origState,
448 VisitedResourceStates, PossibleStates);
449 } else {
450 DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000451 }
452 }
453 }
454 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000455}
456
457
458//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000459// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
460// may be a valid transition from this state i.e., can an instruction of type
461// InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000462//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000463// Note that this routine is performing conservative checks that can be
464// quickly executed acting as a filter before calling AddInsnClassStages.
465// Any cases allowed through here will be caught later in AddInsnClassStages
466// which performs the more expensive exact check.
467//
468bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
469 std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
Alexey Samsonov420a4ed2012-06-28 07:47:50 +0000470 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000471 SI != stateInfo.end(); ++SI) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000472
473 // Check to see if all required resources are available.
474 bool available = true;
475
476 // Inspect each stage independently.
477 // note: This is a conservative check as we aren't checking for
478 // possible resource competition between the stages themselves
479 // The full cross product is examined later in AddInsnClass.
480 for (unsigned i = 0; i < InsnClass.size(); ++i) {
481 unsigned resources = *SI;
482 if ((~resources & InsnClass[i]) == 0) {
483 available = false;
484 break;
485 }
486 // Make sure _all_ resources for a combo function are available.
487 // note: This is a quick conservative check as it won't catch an
488 // unscheduleable combo if this stage is an OR expression
489 // containing a combo.
490 // These cases are caught later in AddInsnClass.
491 unsigned combo = ComboBitToBitsMap[InsnClass[i]];
492 if (combo && ((~resources & combo) != combo)) {
493 DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x" << utohexstr(resources)
494 << " - combo op 0x" << utohexstr(InsnClass[i])
495 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
496 available = false;
497 break;
498 }
499 }
500
501 if (available) {
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000502 return true;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000503 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000504 }
505 return false;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000506}
507
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
521
522//
523// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Pop9aa61372011-12-06 17:34:11 +0000524// associated API to create a DFA packetizer.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000525//
526// Format:
527// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Pop9aa61372011-12-06 17:34:11 +0000528// transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000529// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Pop9aa61372011-12-06 17:34:11 +0000530// the ith state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000531//
532//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000533void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
534 int numInsnClasses,
535 int maxResources, int numCombos, int maxStages) {
536
537 unsigned numStates = states.size();
538
539 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
540 DEBUG(dbgs() << "writeTableAndAPI\n");
541 DEBUG(dbgs() << "Total states: " << numStates << "\n");
542
543 OS << "namespace llvm {\n";
544
545 OS << "\n// Input format:\n";
546 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
547 << "\t// maximum AND'ed resource terms\n";
548 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
549 << "\t// maximum resource bits in one term\n";
550
551 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
552 << "pairs of <Input, NextState> for all valid\n";
553 OS << "// transitions.\n";
554 OS << "// " << numStates << "\tstates\n";
555 OS << "// " << numInsnClasses << "\tinstruction classes\n";
556 OS << "// " << maxResources << "\tresources max\n";
557 OS << "// " << numCombos << "\tcombo resources\n";
558 OS << "// " << maxStages << "\tstages max\n";
559 OS << "const " << DFA_TBLTYPE << " "
560 << TargetName << "DFAStateInputTable[][2] = {\n";
561
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000562 // This table provides a map to the beginning of the transitions for State s
Sebastian Pop9aa61372011-12-06 17:34:11 +0000563 // in DFAStateInputTable.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000564 std::vector<int> StateEntry(numStates+1);
565 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000566
567 // Tracks the total valid transitions encountered so far. It is used
Sebastian Pop9aa61372011-12-06 17:34:11 +0000568 // to construct the StateEntry table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000569 int ValidTransitions = 0;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000570 DFA::StateSet::iterator SI = states.begin();
571 for (unsigned i = 0; i < numStates; ++i, ++SI) {
David Blaikied43046b2014-04-21 22:35:11 +0000572 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000573 StateEntry[i] = ValidTransitions;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000574 for (State::TransitionMap::iterator
David Blaikied43046b2014-04-21 22:35:11 +0000575 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000576 II != IE; ++II) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000577 OS << "{0x" << utohexstr(getDFAInsnInput(II->first)) << ", "
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000578 << II->second->stateNum
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000579 << "},\t";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000580 }
David Blaikied43046b2014-04-21 22:35:11 +0000581 ValidTransitions += SI->Transitions.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000582
Sebastian Pop9aa61372011-12-06 17:34:11 +0000583 // If there are no valid transitions from this stage, we need a sentinel
584 // transition.
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000585 if (ValidTransitions == StateEntry[i]) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000586 OS << SentinelEntry << ",\t";
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000587 ++ValidTransitions;
588 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000589
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000590 OS << " // state " << i << ": " << StateEntry[i];
591 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
592 OS << "-" << (ValidTransitions-1);
593 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000594 OS << "\n";
595 }
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000596
597 // Print out a sentinel entry at the end of the StateInputTable. This is
598 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000599 OS << SentinelEntry << "\t";
600 OS << " // state " << numStates << ": " << ValidTransitions;
601 OS << "\n";
602
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000603 OS << "};\n\n";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000604 OS << "// " << TargetName << "DFAStateEntryTable[i] = "
605 << "Index of the first entry in DFAStateInputTable for\n";
606 OS << "// "
607 << "the ith state.\n";
608 OS << "// " << numStates << " states\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000609 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
610
611 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Pop9aa61372011-12-06 17:34:11 +0000612 // two numbers.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000613 unsigned lastState = 0;
614 for (unsigned i = 0; i < numStates; ++i) {
615 if (i && ((i % 10) == 0)) {
616 lastState = i-1;
617 OS << " // states " << (i-10) << ":" << lastState << "\n";
618 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000619 OS << StateEntry[i] << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000620 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000621
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000622 // Print out the index to the sentinel entry in StateInputTable
623 OS << ValidTransitions << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000624 OS << " // states " << (lastState+1) << ":" << numStates << "\n";
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000625
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000626 OS << "};\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000627 OS << "} // namespace\n";
628
629
630 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000631 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000632 //
633 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
634 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
635 OS << "namespace llvm {\n";
Sebastian Popac35a4d2011-12-06 17:34:16 +0000636 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000637 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
638 << " return new DFAPacketizer(IID, " << TargetName
639 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
640 OS << "} // End llvm namespace \n";
641}
642
643
644//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000645// collectAllFuncUnits - Construct a map of function unit names to bits.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000646//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000647int DFAPacketizerEmitter::collectAllFuncUnits(
648 std::vector<Record*> &ProcItinList,
649 std::map<std::string, unsigned> &FUNameToBitsMap,
650 int &maxFUs,
651 raw_ostream &OS) {
652 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
653 DEBUG(dbgs() << "collectAllFuncUnits");
654 DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000655
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000656 int totalFUs = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000657 // Parse functional units for all the itineraries.
658 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
659 Record *Proc = ProcItinList[i];
660 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
661
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000662 DEBUG(dbgs() << " FU:" << i
663 << " (" << FUs.size() << " FUs) "
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000664 << Proc->getName());
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000665
666
Sebastian Pop9aa61372011-12-06 17:34:11 +0000667 // Convert macros to bits for each stage.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000668 unsigned numFUs = FUs.size();
669 for (unsigned j = 0; j < numFUs; ++j) {
670 assert ((j < DFA_MAX_RESOURCES) &&
671 "Exceeded maximum number of representable resources");
672 unsigned FuncResources = (unsigned) (1U << j);
673 FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
674 DEBUG(dbgs() << " " << FUs[j]->getName()
675 << ":0x" << utohexstr(FuncResources));
676 }
677 if (((int) numFUs) > maxFUs) {
678 maxFUs = numFUs;
679 }
680 totalFUs += numFUs;
681 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000682 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000683 return totalFUs;
684}
685
686//
687// collectAllComboFuncs - Construct a map from a combo function unit bit to
688// the bits of all included functional units.
689//
690int DFAPacketizerEmitter::collectAllComboFuncs(
691 std::vector<Record*> &ComboFuncList,
692 std::map<std::string, unsigned> &FUNameToBitsMap,
693 std::map<unsigned, unsigned> &ComboBitToBitsMap,
694 raw_ostream &OS) {
695 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
696 DEBUG(dbgs() << "collectAllComboFuncs");
697 DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
698
699 int numCombos = 0;
700 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
701 Record *Func = ComboFuncList[i];
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000702 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
703
704 DEBUG(dbgs() << " CFD:" << i
705 << " (" << FUs.size() << " combo FUs) "
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000706 << Func->getName() << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000707
708 // Convert macros to bits for each stage.
709 for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
710 assert ((j < DFA_MAX_RESOURCES) &&
711 "Exceeded maximum number of DFA resources");
712 Record *FuncData = FUs[j];
713 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
714 const std::vector<Record*> &FuncList =
715 FuncData->getValueAsListOfDefs("FuncList");
716 std::string ComboFuncName = ComboFunc->getName();
717 unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
718 unsigned ComboResources = ComboBit;
719 DEBUG(dbgs() << " combo: " << ComboFuncName
720 << ":0x" << utohexstr(ComboResources) << "\n");
721 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
722 std::string FuncName = FuncList[k]->getName();
723 unsigned FuncResources = FUNameToBitsMap[FuncName];
724 DEBUG(dbgs() << " " << FuncName
725 << ":0x" << utohexstr(FuncResources) << "\n");
726 ComboResources |= FuncResources;
727 }
728 ComboBitToBitsMap[ComboBit] = ComboResources;
729 numCombos++;
730 DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
731 << utohexstr(ComboBit) << " = 0x"
732 << utohexstr(ComboResources) << "\n");
733 }
734 }
735 return numCombos;
736}
737
738
739//
740// collectOneInsnClass - Populate allInsnClasses with one instruction class
741//
742int 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) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000747 const std::vector<Record*> &StageList =
748 ItinData->getValueAsListOfDefs("Stages");
749
Sebastian Pop9aa61372011-12-06 17:34:11 +0000750 // The number of stages.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000751 unsigned NStages = StageList.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000752
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000753 DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000754 << "\n");
755
756 std::vector<unsigned> UnitBits;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000757
Sebastian Pop9aa61372011-12-06 17:34:11 +0000758 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000759 for (unsigned i = 0; i < NStages; ++i) {
760 const Record *Stage = StageList[i];
761
Sebastian Pop9aa61372011-12-06 17:34:11 +0000762 // Get unit list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000763 const std::vector<Record*> &UnitList =
764 Stage->getValueAsListOfDefs("Units");
765
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000766 DEBUG(dbgs() << " stage:" << i
767 << " [" << UnitList.size() << " 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;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000772 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000773 // Conduct bitwise or.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000774 std::string UnitName = UnitList[j]->getName();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000775 DEBUG(dbgs() << " " << j << ":" << UnitName);
776 dbglen += 3 + UnitName.length();
777 assert(FUNameToBitsMap.count(UnitName));
778 UnitBitValue |= FUNameToBitsMap[UnitName];
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000779 }
780
781 if (UnitBitValue != 0)
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000782 UnitBits.push_back(UnitBitValue);
783
784 while (dbglen <= 64) { // line up bits dbgs
785 dbglen += 8;
786 DEBUG(dbgs() << "\t");
787 }
788 DEBUG(dbgs() << " (bits: 0x" << utohexstr(UnitBitValue) << ")\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000789 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000790
791 if (UnitBits.size() > 0)
792 allInsnClasses.push_back(UnitBits);
793
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000794 DEBUG({
795 dbgs() << " ";
796 dbgsInsnClass(UnitBits);
797 dbgs() << "\n";
798 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000799
800 return NStages;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000801}
802
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000803//
804// collectAllInsnClasses - Populate allInsnClasses which is a set of units
805// used in each stage.
806//
807int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
808 std::vector<Record*> &ProcItinList,
809 std::map<std::string, unsigned> &FUNameToBitsMap,
810 std::vector<Record*> &ItinDataList,
811 int &maxStages,
812 raw_ostream &OS) {
813 // Collect all instruction classes.
814 unsigned M = ItinDataList.size();
815
816 int numInsnClasses = 0;
817 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"
818 << "collectAllInsnClasses "
819 << ProcName
820 << " (" << M << " classes)\n");
821
822 // Collect stages for each instruction class for all itinerary data
823 for (unsigned j = 0; j < M; j++) {
824 Record *ItinData = ItinDataList[j];
825 int NStages = collectOneInsnClass(ProcName, ProcItinList,
826 FUNameToBitsMap, ItinData, OS);
827 if (NStages > maxStages) {
828 maxStages = NStages;
829 }
830 numInsnClasses++;
831 }
832 return numInsnClasses;
833}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000834
835//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000836// Run the worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000837//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000838void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000839
Sebastian Pop9aa61372011-12-06 17:34:11 +0000840 // Collect processor iteraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000841 std::vector<Record*> ProcItinList =
842 Records.getAllDerivedDefinitions("ProcessorItineraries");
843
844 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000845 // Collect the Functional units.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000846 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000847 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;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000866 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
867 Record *Proc = ProcItinList[i];
868
Sebastian Pop9aa61372011-12-06 17:34:11 +0000869 // Get processor itinerary name.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000870 const std::string &ProcName = Proc->getName();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000871
Sebastian Pop9aa61372011-12-06 17:34:11 +0000872 // Skip default.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000873 if (ProcName == "NoItineraries")
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000874 continue;
875
Sebastian Pop9aa61372011-12-06 17:34:11 +0000876 // Sanity check for at least one instruction itinerary class.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000877 unsigned NItinClasses =
878 Records.getAllDerivedDefinitions("InstrItinClass").size();
879 if (NItinClasses == 0)
880 return;
881
Sebastian Pop9aa61372011-12-06 17:34:11 +0000882 // Get itinerary data list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000883 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
884
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000885 // Collect all instruction classes
886 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
887 FUNameToBitsMap, ItinDataList, maxStages, OS);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000888 }
889
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000890 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000891 // Run a worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000892 //
893 DFA D;
David Blaikied43046b2014-04-21 22:35:11 +0000894 const State *Initial = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000895 Initial->isInitial = true;
896 Initial->stateInfo.insert(0x0);
David Blaikied43046b2014-04-21 22:35:11 +0000897 SmallVector<const State*, 32> WorkList;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000898// std::queue<State*> WorkList;
David Blaikied43046b2014-04-21 22:35:11 +0000899 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000900
901 WorkList.push_back(Initial);
902
903 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000904 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000905 // C = {set of InsnClasses}
906 // Begin with initial node in worklist. Initial node does not have
907 // any consumed resources,
908 // ResourceState = 0x0
909 // Visited = {}
910 // While worklist != empty
911 // S = first element of worklist
912 // For every instruction class C
913 // if we can accommodate C in S:
914 // S' = state with resource states = {S Union C}
915 // Add a new transition: S x C -> S'
916 // If S' is not in Visited:
917 // Add S' to worklist
918 // Add S' to Visited
919 //
920 while (!WorkList.empty()) {
David Blaikied43046b2014-04-21 22:35:11 +0000921 const State *current = WorkList.pop_back_val();
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000922 DEBUG({
923 dbgs() << "---------------------\n";
924 dbgs() << "Processing state: " << current->stateNum << " - ";
925 dbgsStateInfo(current->stateInfo);
926 dbgs() << "\n";
927 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000928 for (unsigned i = 0; i < allInsnClasses.size(); i++) {
929 std::vector<unsigned> InsnClass = allInsnClasses[i];
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000930 DEBUG({
931 dbgs() << i << " ";
932 dbgsInsnClass(InsnClass);
933 dbgs() << "\n";
934 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000935
936 std::set<unsigned> NewStateResources;
937 //
938 // If we haven't already created a transition for this input
Sebastian Pop9aa61372011-12-06 17:34:11 +0000939 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000940 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000941 if (!current->hasTransition(InsnClass) &&
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000942 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
943 const State *NewState = NULL;
944 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
945 if (NewStateResources.size() == 0) {
946 DEBUG(dbgs() << " Skipped - no new states generated\n");
947 continue;
948 }
949
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000950 DEBUG({
951 dbgs() << "\t";
952 dbgsStateInfo(NewStateResources);
953 dbgs() << "\n";
954 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000955
956 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000957 // If we have seen this state before, then do not create a new state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000958 //
David Blaikied43046b2014-04-21 22:35:11 +0000959 auto VI = Visited.find(NewStateResources);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000960 if (VI != Visited.end()) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000961 NewState = VI->second;
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000962 DEBUG({
963 dbgs() << "\tFound existing state: " << NewState->stateNum
964 << " - ";
965 dbgsStateInfo(NewState->stateInfo);
966 dbgs() << "\n";
967 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000968 } else {
David Blaikied43046b2014-04-21 22:35:11 +0000969 NewState = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000970 NewState->stateInfo = NewStateResources;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000971 Visited[NewStateResources] = NewState;
972 WorkList.push_back(NewState);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000973 DEBUG({
974 dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
975 dbgsStateInfo(NewState->stateInfo);
976 dbgs() << "\n";
977 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000978 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000979
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000980 current->addTransition(InsnClass, NewState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000981 }
982 }
983 }
984
Sebastian Pop9aa61372011-12-06 17:34:11 +0000985 // Print out the table.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000986 D.writeTableAndAPI(OS, TargetName,
987 numInsnClasses, maxResources, numCombos, maxStages);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000988}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000989
990namespace llvm {
991
992void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
993 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
994 DFAPacketizerEmitter(RK).run(OS);
995}
996
997} // End llvm namespace