blob: fc70e97d7e21d8715ad01741e9b6c52f4f0c926b [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"
24#include "llvm/CodeGen/DFAPacketizerDefs.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000025#include "llvm/TableGen/Record.h"
26#include "llvm/TableGen/TableGenBackend.h"
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000027#include "llvm/Support/Debug.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000028#include <list>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000029#include <map>
30#include <string>
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000031#include <queue>
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000032using namespace llvm;
33
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000034// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
35//
36// dbgsInsnClass - When debugging, print instruction class stages.
37//
38void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
39//
40// dbgsStateInfo - When debugging, print the set of state info.
41//
42void dbgsStateInfo(const std::set<unsigned> &stateInfo);
43//
44// dbgsIndent - When debugging, indent by the specified amount.
45//
46void dbgsIndent(unsigned indent);
47
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000048//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000049// class DFAPacketizerEmitter: class that generates and prints out the DFA
50// for resource tracking.
51//
52namespace {
53class DFAPacketizerEmitter {
54private:
55 std::string TargetName;
56 //
57 // allInsnClasses is the set of all possible resources consumed by an
58 // InstrStage.
59 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000060 std::vector<std::vector<unsigned>> allInsnClasses;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000061 RecordKeeper &Records;
62
63public:
64 DFAPacketizerEmitter(RecordKeeper &R);
65
66 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000067 // collectAllFuncUnits - Construct a map of function unit names to bits.
68 //
69 int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
70 std::map<std::string, unsigned> &FUNameToBitsMap,
71 int &maxResources,
72 raw_ostream &OS);
73
74 //
75 // collectAllComboFuncs - Construct a map from a combo function unit bit to
76 // the bits of all included functional units.
77 //
78 int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
79 std::map<std::string, unsigned> &FUNameToBitsMap,
80 std::map<unsigned, unsigned> &ComboBitToBitsMap,
81 raw_ostream &OS);
82
83 //
84 // collectOneInsnClass - Populate allInsnClasses with one instruction class.
85 //
86 int collectOneInsnClass(const std::string &ProcName,
87 std::vector<Record*> &ProcItinList,
88 std::map<std::string, unsigned> &FUNameToBitsMap,
89 Record *ItinData,
90 raw_ostream &OS);
91
92 //
93 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000094 // used in each stage.
95 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000096 int collectAllInsnClasses(const std::string &ProcName,
97 std::vector<Record*> &ProcItinList,
98 std::map<std::string, unsigned> &FUNameToBitsMap,
99 std::vector<Record*> &ItinDataList,
100 int &maxStages,
101 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000102
103 void run(raw_ostream &OS);
104};
105} // End anonymous namespace.
106
107//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000108//
109// State represents the usage of machine resources if the packet contains
110// a set of instruction classes.
111//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000112// Specifically, currentState is a set of bit-masks.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000113// The nth bit in a bit-mask indicates whether the nth resource is being used
114// by this state. The set of bit-masks in a state represent the different
115// possible outcomes of transitioning to this state.
Sebastian Pop9aa61372011-12-06 17:34:11 +0000116// For example: consider a two resource architecture: resource L and resource M
117// with three instruction classes: L, M, and L_or_M.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000118// From the initial state (currentState = 0x00), if we add instruction class
119// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
120// represents the possible resource states that can result from adding a L_or_M
121// instruction
122//
123// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Pop9aa61372011-12-06 17:34:11 +0000124// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000125//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000126// A State instance also contains a collection of transitions from that state:
127// a map from inputs to new states.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000128//
129namespace {
130class State {
131 public:
132 static int currentStateNum;
David Blaikied43046b2014-04-21 22:35:11 +0000133 // stateNum is the only member used for equality/ordering, all other members
134 // can be mutated even in const State objects.
135 const int stateNum;
136 mutable bool isInitial;
137 mutable std::set<unsigned> stateInfo;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000138 typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
David Blaikied43046b2014-04-21 22:35:11 +0000139 mutable TransitionMap Transitions;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000140
141 State();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000142
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000143 bool operator<(const State &s) const {
144 return stateNum < s.stateNum;
145 }
146
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000147 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000148 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
149 // may be a valid transition from this state i.e., can an instruction of type
150 // InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000151 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000152 // Note that for multiple stages, this quick check does not take into account
153 // any possible resource competition between the stages themselves. That is
154 // enforced in AddInsnClassStages which checks the cross product of all
155 // stages for resource availability (which is a more involved check).
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000156 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000157 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
158 std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000159 //
Krzysztof Parzyszek4ca21fc2015-11-21 17:38:33 +0000160 // AddInsnClass - Return all combinations of resource reservation
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000161 // which are possible from this state (PossibleStates).
162 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000163 // PossibleStates is the set of valid resource states that ensue from valid
164 // transitions.
165 //
166 void AddInsnClass(std::vector<unsigned> &InsnClass,
167 std::map<unsigned, unsigned> &ComboBitToBitsMap,
168 std::set<unsigned> &PossibleStates) const;
169 //
170 // AddInsnClassStages - Return all combinations of resource reservation
171 // resulting from the cross product of all stages for this InsnClass
172 // which are possible from this state (PossibleStates).
173 //
174 void AddInsnClassStages(std::vector<unsigned> &InsnClass,
175 std::map<unsigned, unsigned> &ComboBitToBitsMap,
176 unsigned chkstage, unsigned numstages,
177 unsigned prevState, unsigned origState,
178 DenseSet<unsigned> &VisitedResourceStates,
179 std::set<unsigned> &PossibleStates) const;
180 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000181 // addTransition - Add a transition from this state given the input InsnClass
182 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000183 void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000184 //
185 // hasTransition - Returns true if there is a transition from this state
186 // given the input InsnClass
187 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000188 bool hasTransition(std::vector<unsigned> InsnClass) const;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000189};
Sebastian Pop9aa61372011-12-06 17:34:11 +0000190} // End anonymous namespace.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000191
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000192//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000193// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000194//
195namespace {
196class DFA {
197public:
198 DFA();
199
Sebastian Pop9aa61372011-12-06 17:34:11 +0000200 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikied43046b2014-04-21 22:35:11 +0000201 typedef std::set<State> StateSet;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000202 StateSet states;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000203
Sebastian Popac35a4d2011-12-06 17:34:16 +0000204 State *currentState;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000205
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000206 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000207 // Modify the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000208 //
David Blaikied43046b2014-04-21 22:35:11 +0000209 const State &newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000210
211 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000212 // writeTable: Print out a table representing the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000213 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000214 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
215 int numInsnClasses = 0,
216 int maxResources = 0, int numCombos = 0, int maxStages = 0);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000217};
Sebastian Pop9aa61372011-12-06 17:34:11 +0000218} // End anonymous namespace.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000219
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000220// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
221//
222// dbgsInsnClass - When debugging, print instruction class stages.
223//
224void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
225 DEBUG(dbgs() << "InsnClass: ");
226 for (unsigned i = 0; i < InsnClass.size(); ++i) {
227 if (i > 0) {
228 DEBUG(dbgs() << ", ");
229 }
230 DEBUG(dbgs() << "0x" << utohexstr(InsnClass[i]));
231 }
232 DFAInput InsnInput = getDFAInsnInput(InsnClass);
233 DEBUG(dbgs() << " (input: 0x" << utohexstr(InsnInput) << ")");
234}
235
236//
237// dbgsStateInfo - When debugging, print the set of state info.
238//
239void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
240 DEBUG(dbgs() << "StateInfo: ");
241 unsigned i = 0;
242 for (std::set<unsigned>::iterator SI = stateInfo.begin();
243 SI != stateInfo.end(); ++SI, ++i) {
244 unsigned thisState = *SI;
245 if (i > 0) {
246 DEBUG(dbgs() << ", ");
247 }
248 DEBUG(dbgs() << "0x" << utohexstr(thisState));
249 }
250}
251
252//
253// dbgsIndent - When debugging, indent by the specified amount.
254//
255void dbgsIndent(unsigned indent) {
256 for (unsigned i = 0; i < indent; ++i) {
257 DEBUG(dbgs() << " ");
258 }
259}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000260
261//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000262// Constructors and destructors for State and DFA
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000263//
264State::State() :
265 stateNum(currentStateNum++), isInitial(false) {}
266
Craig Topper24064772014-04-15 07:20:03 +0000267DFA::DFA(): currentState(nullptr) {}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000268
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000269//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000270// addTransition - Add a transition from this state given the input InsnClass
271//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000272void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
273 const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000274 assert(!Transitions.count(InsnClass) &&
275 "Cannot have multiple transitions for the same input");
276 Transitions[InsnClass] = To;
277}
278
279//
280// hasTransition - Returns true if there is a transition from this state
281// given the input InsnClass
282//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000283bool State::hasTransition(std::vector<unsigned> InsnClass) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000284 return Transitions.count(InsnClass) > 0;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000285}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000286
287//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000288// AddInsnClass - Return all combinations of resource reservation
289// which are possible from this state (PossibleStates).
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000290//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000291// PossibleStates is the set of valid resource states that ensue from valid
292// transitions.
293//
294void State::AddInsnClass(std::vector<unsigned> &InsnClass,
295 std::map<unsigned, unsigned> &ComboBitToBitsMap,
296 std::set<unsigned> &PossibleStates) const {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000297 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000298 // Iterate over all resource states in currentState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000299 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000300 unsigned numstages = InsnClass.size();
301 assert((numstages > 0) && "InsnClass has no stages");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000302
303 for (std::set<unsigned>::iterator SI = stateInfo.begin();
304 SI != stateInfo.end(); ++SI) {
305 unsigned thisState = *SI;
306
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000307 DenseSet<unsigned> VisitedResourceStates;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000308
309 DEBUG(dbgs() << " thisState: 0x" << utohexstr(thisState) << "\n");
310 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
311 numstages - 1, numstages,
312 thisState, thisState,
313 VisitedResourceStates, PossibleStates);
314 }
315}
316
317void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
318 std::map<unsigned, unsigned> &ComboBitToBitsMap,
319 unsigned chkstage, unsigned numstages,
320 unsigned prevState, unsigned origState,
321 DenseSet<unsigned> &VisitedResourceStates,
322 std::set<unsigned> &PossibleStates) const {
323
324 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
325 unsigned thisStage = InsnClass[chkstage];
326
327 dbgsIndent((1 + numstages - chkstage) << 1);
328 DEBUG(dbgs() << "AddInsnClassStages " << chkstage
329 << " (0x" << utohexstr(thisStage) << ") from ");
330 dbgsInsnClass(InsnClass);
331 DEBUG(dbgs() << "\n");
332
333 //
334 // Iterate over all possible resources used in thisStage.
335 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
336 //
337 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
338 unsigned resourceMask = (0x1 << j);
339 if (resourceMask & thisStage) {
340 unsigned combo = ComboBitToBitsMap[resourceMask];
341 if (combo && ((~prevState & combo) != combo)) {
342 DEBUG(dbgs() << "\tSkipped Add 0x" << utohexstr(prevState)
343 << " - combo op 0x" << utohexstr(resourceMask)
344 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
345 continue;
346 }
347 //
348 // For each possible resource used in thisStage, generate the
349 // resource state if that resource was used.
350 //
351 unsigned ResultingResourceState = prevState | resourceMask | combo;
352 dbgsIndent((2 + numstages - chkstage) << 1);
353 DEBUG(dbgs() << "0x" << utohexstr(prevState)
354 << " | 0x" << utohexstr(resourceMask));
355 if (combo) {
356 DEBUG(dbgs() << " | 0x" << utohexstr(combo));
357 }
358 DEBUG(dbgs() << " = 0x" << utohexstr(ResultingResourceState) << " ");
359
360 //
361 // If this is the final stage for this class
362 //
363 if (chkstage == 0) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000364 //
365 // Check if the resulting resource state can be accommodated in this
Sebastian Pop9aa61372011-12-06 17:34:11 +0000366 // packet.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000367 // We compute resource OR prevState (originally started as origState).
368 // If the result of the OR is different than origState, it implies
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000369 // that there is at least one resource that can be used to schedule
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000370 // thisStage in the current packet.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000371 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Pop9aa61372011-12-06 17:34:11 +0000372 // processed ResultingResourceState before.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000373 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000374 if (ResultingResourceState != prevState) {
375 if (VisitedResourceStates.count(ResultingResourceState) == 0) {
376 VisitedResourceStates.insert(ResultingResourceState);
377 PossibleStates.insert(ResultingResourceState);
378 DEBUG(dbgs() << "\tResultingResourceState: 0x"
379 << utohexstr(ResultingResourceState) << "\n");
380 } else {
381 DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
382 }
383 } else {
384 DEBUG(dbgs() << "\tSkipped Add - no final resources available\n");
385 }
386 } else {
387 //
388 // If the current resource can be accommodated, check the next
389 // stage in InsnClass for available resources.
390 //
391 if (ResultingResourceState != prevState) {
392 DEBUG(dbgs() << "\n");
393 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
394 chkstage - 1, numstages,
395 ResultingResourceState, origState,
396 VisitedResourceStates, PossibleStates);
397 } else {
398 DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000399 }
400 }
401 }
402 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000403}
404
405
406//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000407// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
408// may be a valid transition from this state i.e., can an instruction of type
409// InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000410//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000411// Note that this routine is performing conservative checks that can be
412// quickly executed acting as a filter before calling AddInsnClassStages.
413// Any cases allowed through here will be caught later in AddInsnClassStages
414// which performs the more expensive exact check.
415//
416bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
417 std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
Alexey Samsonov420a4ed2012-06-28 07:47:50 +0000418 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000419 SI != stateInfo.end(); ++SI) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000420
421 // Check to see if all required resources are available.
422 bool available = true;
423
424 // Inspect each stage independently.
425 // note: This is a conservative check as we aren't checking for
426 // possible resource competition between the stages themselves
427 // The full cross product is examined later in AddInsnClass.
428 for (unsigned i = 0; i < InsnClass.size(); ++i) {
429 unsigned resources = *SI;
430 if ((~resources & InsnClass[i]) == 0) {
431 available = false;
432 break;
433 }
434 // Make sure _all_ resources for a combo function are available.
435 // note: This is a quick conservative check as it won't catch an
436 // unscheduleable combo if this stage is an OR expression
437 // containing a combo.
438 // These cases are caught later in AddInsnClass.
439 unsigned combo = ComboBitToBitsMap[InsnClass[i]];
440 if (combo && ((~resources & combo) != combo)) {
441 DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x" << utohexstr(resources)
442 << " - combo op 0x" << utohexstr(InsnClass[i])
443 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
444 available = false;
445 break;
446 }
447 }
448
449 if (available) {
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000450 return true;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000451 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000452 }
453 return false;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000454}
455
456
David Blaikied43046b2014-04-21 22:35:11 +0000457const State &DFA::newState() {
David Blaikie4bcfcc12014-04-21 22:46:09 +0000458 auto IterPair = states.insert(State());
David Blaikied43046b2014-04-21 22:35:11 +0000459 assert(IterPair.second && "State already exists");
460 return *IterPair.first;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000461}
462
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000463int State::currentStateNum = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000464
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000465DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000466 TargetName(CodeGenTarget(R).getName()),
467 allInsnClasses(), Records(R) {}
468
469
470//
471// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Pop9aa61372011-12-06 17:34:11 +0000472// associated API to create a DFA packetizer.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000473//
474// Format:
475// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Pop9aa61372011-12-06 17:34:11 +0000476// transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000477// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Pop9aa61372011-12-06 17:34:11 +0000478// the ith state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000479//
480//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000481void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
482 int numInsnClasses,
483 int maxResources, int numCombos, int maxStages) {
484
485 unsigned numStates = states.size();
486
487 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
488 DEBUG(dbgs() << "writeTableAndAPI\n");
489 DEBUG(dbgs() << "Total states: " << numStates << "\n");
490
491 OS << "namespace llvm {\n";
492
493 OS << "\n// Input format:\n";
494 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
495 << "\t// maximum AND'ed resource terms\n";
496 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
497 << "\t// maximum resource bits in one term\n";
498
499 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
500 << "pairs of <Input, NextState> for all valid\n";
501 OS << "// transitions.\n";
502 OS << "// " << numStates << "\tstates\n";
503 OS << "// " << numInsnClasses << "\tinstruction classes\n";
504 OS << "// " << maxResources << "\tresources max\n";
505 OS << "// " << numCombos << "\tcombo resources\n";
506 OS << "// " << maxStages << "\tstages max\n";
507 OS << "const " << DFA_TBLTYPE << " "
508 << TargetName << "DFAStateInputTable[][2] = {\n";
509
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000510 // This table provides a map to the beginning of the transitions for State s
Sebastian Pop9aa61372011-12-06 17:34:11 +0000511 // in DFAStateInputTable.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000512 std::vector<int> StateEntry(numStates+1);
513 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000514
515 // Tracks the total valid transitions encountered so far. It is used
Sebastian Pop9aa61372011-12-06 17:34:11 +0000516 // to construct the StateEntry table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000517 int ValidTransitions = 0;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000518 DFA::StateSet::iterator SI = states.begin();
519 for (unsigned i = 0; i < numStates; ++i, ++SI) {
David Blaikied43046b2014-04-21 22:35:11 +0000520 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000521 StateEntry[i] = ValidTransitions;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000522 for (State::TransitionMap::iterator
David Blaikied43046b2014-04-21 22:35:11 +0000523 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000524 II != IE; ++II) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000525 OS << "{0x" << utohexstr(getDFAInsnInput(II->first)) << ", "
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000526 << II->second->stateNum
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000527 << "},\t";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000528 }
David Blaikied43046b2014-04-21 22:35:11 +0000529 ValidTransitions += SI->Transitions.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000530
Sebastian Pop9aa61372011-12-06 17:34:11 +0000531 // If there are no valid transitions from this stage, we need a sentinel
532 // transition.
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000533 if (ValidTransitions == StateEntry[i]) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000534 OS << SentinelEntry << ",\t";
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000535 ++ValidTransitions;
536 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000537
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000538 OS << " // state " << i << ": " << StateEntry[i];
539 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
540 OS << "-" << (ValidTransitions-1);
541 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000542 OS << "\n";
543 }
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000544
545 // Print out a sentinel entry at the end of the StateInputTable. This is
546 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000547 OS << SentinelEntry << "\t";
548 OS << " // state " << numStates << ": " << ValidTransitions;
549 OS << "\n";
550
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000551 OS << "};\n\n";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000552 OS << "// " << TargetName << "DFAStateEntryTable[i] = "
553 << "Index of the first entry in DFAStateInputTable for\n";
554 OS << "// "
555 << "the ith state.\n";
556 OS << "// " << numStates << " states\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000557 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
558
559 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Pop9aa61372011-12-06 17:34:11 +0000560 // two numbers.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000561 unsigned lastState = 0;
562 for (unsigned i = 0; i < numStates; ++i) {
563 if (i && ((i % 10) == 0)) {
564 lastState = i-1;
565 OS << " // states " << (i-10) << ":" << lastState << "\n";
566 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000567 OS << StateEntry[i] << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000568 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000569
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000570 // Print out the index to the sentinel entry in StateInputTable
571 OS << ValidTransitions << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000572 OS << " // states " << (lastState+1) << ":" << numStates << "\n";
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000573
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000574 OS << "};\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000575 OS << "} // namespace\n";
576
577
578 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000579 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000580 //
581 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
582 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
583 OS << "namespace llvm {\n";
Sebastian Popac35a4d2011-12-06 17:34:16 +0000584 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000585 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
586 << " return new DFAPacketizer(IID, " << TargetName
587 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
588 OS << "} // End llvm namespace \n";
589}
590
591
592//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000593// collectAllFuncUnits - Construct a map of function unit names to bits.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000594//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000595int DFAPacketizerEmitter::collectAllFuncUnits(
596 std::vector<Record*> &ProcItinList,
597 std::map<std::string, unsigned> &FUNameToBitsMap,
598 int &maxFUs,
599 raw_ostream &OS) {
600 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
601 DEBUG(dbgs() << "collectAllFuncUnits");
602 DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000603
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000604 int totalFUs = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000605 // Parse functional units for all the itineraries.
606 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
607 Record *Proc = ProcItinList[i];
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000608 const std::string &ProcName = Proc->getName();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000609 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
610
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000611 DEBUG(dbgs() << " FU:" << i
612 << " (" << FUs.size() << " FUs) "
613 << ProcName);
614
615
Sebastian Pop9aa61372011-12-06 17:34:11 +0000616 // Convert macros to bits for each stage.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000617 unsigned numFUs = FUs.size();
618 for (unsigned j = 0; j < numFUs; ++j) {
619 assert ((j < DFA_MAX_RESOURCES) &&
620 "Exceeded maximum number of representable resources");
621 unsigned FuncResources = (unsigned) (1U << j);
622 FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
623 DEBUG(dbgs() << " " << FUs[j]->getName()
624 << ":0x" << utohexstr(FuncResources));
625 }
626 if (((int) numFUs) > maxFUs) {
627 maxFUs = numFUs;
628 }
629 totalFUs += numFUs;
630 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000631 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000632 return totalFUs;
633}
634
635//
636// collectAllComboFuncs - Construct a map from a combo function unit bit to
637// the bits of all included functional units.
638//
639int DFAPacketizerEmitter::collectAllComboFuncs(
640 std::vector<Record*> &ComboFuncList,
641 std::map<std::string, unsigned> &FUNameToBitsMap,
642 std::map<unsigned, unsigned> &ComboBitToBitsMap,
643 raw_ostream &OS) {
644 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
645 DEBUG(dbgs() << "collectAllComboFuncs");
646 DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
647
648 int numCombos = 0;
649 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
650 Record *Func = ComboFuncList[i];
651 const std::string &ProcName = Func->getName();
652 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
653
654 DEBUG(dbgs() << " CFD:" << i
655 << " (" << FUs.size() << " combo FUs) "
656 << ProcName << "\n");
657
658 // Convert macros to bits for each stage.
659 for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
660 assert ((j < DFA_MAX_RESOURCES) &&
661 "Exceeded maximum number of DFA resources");
662 Record *FuncData = FUs[j];
663 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
664 const std::vector<Record*> &FuncList =
665 FuncData->getValueAsListOfDefs("FuncList");
666 std::string ComboFuncName = ComboFunc->getName();
667 unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
668 unsigned ComboResources = ComboBit;
669 DEBUG(dbgs() << " combo: " << ComboFuncName
670 << ":0x" << utohexstr(ComboResources) << "\n");
671 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
672 std::string FuncName = FuncList[k]->getName();
673 unsigned FuncResources = FUNameToBitsMap[FuncName];
674 DEBUG(dbgs() << " " << FuncName
675 << ":0x" << utohexstr(FuncResources) << "\n");
676 ComboResources |= FuncResources;
677 }
678 ComboBitToBitsMap[ComboBit] = ComboResources;
679 numCombos++;
680 DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
681 << utohexstr(ComboBit) << " = 0x"
682 << utohexstr(ComboResources) << "\n");
683 }
684 }
685 return numCombos;
686}
687
688
689//
690// collectOneInsnClass - Populate allInsnClasses with one instruction class
691//
692int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
693 std::vector<Record*> &ProcItinList,
694 std::map<std::string, unsigned> &FUNameToBitsMap,
695 Record *ItinData,
696 raw_ostream &OS) {
697 // Collect instruction classes.
698 Record *ItinDef = ItinData->getValueAsDef("TheClass");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000699
700 const std::vector<Record*> &StageList =
701 ItinData->getValueAsListOfDefs("Stages");
702
Sebastian Pop9aa61372011-12-06 17:34:11 +0000703 // The number of stages.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000704 unsigned NStages = StageList.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000705
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000706 DEBUG(dbgs() << " " << ItinDef->getName()
707 << "\n");
708
709 std::vector<unsigned> UnitBits;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000710
Sebastian Pop9aa61372011-12-06 17:34:11 +0000711 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000712 for (unsigned i = 0; i < NStages; ++i) {
713 const Record *Stage = StageList[i];
714
Sebastian Pop9aa61372011-12-06 17:34:11 +0000715 // Get unit list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000716 const std::vector<Record*> &UnitList =
717 Stage->getValueAsListOfDefs("Units");
718
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000719 DEBUG(dbgs() << " stage:" << i
720 << " [" << UnitList.size() << " units]:");
721 unsigned dbglen = 26; // cursor after stage dbgs
722
723 // Compute the bitwise or of each unit used in this stage.
724 unsigned UnitBitValue = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000725 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000726 // Conduct bitwise or.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000727 std::string UnitName = UnitList[j]->getName();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000728 DEBUG(dbgs() << " " << j << ":" << UnitName);
729 dbglen += 3 + UnitName.length();
730 assert(FUNameToBitsMap.count(UnitName));
731 UnitBitValue |= FUNameToBitsMap[UnitName];
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000732 }
733
734 if (UnitBitValue != 0)
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000735 UnitBits.push_back(UnitBitValue);
736
737 while (dbglen <= 64) { // line up bits dbgs
738 dbglen += 8;
739 DEBUG(dbgs() << "\t");
740 }
741 DEBUG(dbgs() << " (bits: 0x" << utohexstr(UnitBitValue) << ")\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000742 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000743
744 if (UnitBits.size() > 0)
745 allInsnClasses.push_back(UnitBits);
746
747 DEBUG(dbgs() << " ");
748 dbgsInsnClass(UnitBits);
749 DEBUG(dbgs() << "\n");
750
751 return NStages;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000752}
753
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000754//
755// collectAllInsnClasses - Populate allInsnClasses which is a set of units
756// used in each stage.
757//
758int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
759 std::vector<Record*> &ProcItinList,
760 std::map<std::string, unsigned> &FUNameToBitsMap,
761 std::vector<Record*> &ItinDataList,
762 int &maxStages,
763 raw_ostream &OS) {
764 // Collect all instruction classes.
765 unsigned M = ItinDataList.size();
766
767 int numInsnClasses = 0;
768 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"
769 << "collectAllInsnClasses "
770 << ProcName
771 << " (" << M << " classes)\n");
772
773 // Collect stages for each instruction class for all itinerary data
774 for (unsigned j = 0; j < M; j++) {
775 Record *ItinData = ItinDataList[j];
776 int NStages = collectOneInsnClass(ProcName, ProcItinList,
777 FUNameToBitsMap, ItinData, OS);
778 if (NStages > maxStages) {
779 maxStages = NStages;
780 }
781 numInsnClasses++;
782 }
783 return numInsnClasses;
784}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000785
786//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000787// Run the worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000788//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000789void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000790
Sebastian Pop9aa61372011-12-06 17:34:11 +0000791 // Collect processor iteraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000792 std::vector<Record*> ProcItinList =
793 Records.getAllDerivedDefinitions("ProcessorItineraries");
794
795 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000796 // Collect the Functional units.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000797 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000798 std::map<std::string, unsigned> FUNameToBitsMap;
799 int maxResources = 0;
800 collectAllFuncUnits(ProcItinList,
801 FUNameToBitsMap, maxResources, OS);
802
803 //
804 // Collect the Combo Functional units.
805 //
806 std::map<unsigned, unsigned> ComboBitToBitsMap;
807 std::vector<Record*> ComboFuncList =
808 Records.getAllDerivedDefinitions("ComboFuncUnits");
809 int numCombos = collectAllComboFuncs(ComboFuncList,
810 FUNameToBitsMap, ComboBitToBitsMap, OS);
811
812 //
813 // Collect the itineraries.
814 //
815 int maxStages = 0;
816 int numInsnClasses = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000817 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
818 Record *Proc = ProcItinList[i];
819
Sebastian Pop9aa61372011-12-06 17:34:11 +0000820 // Get processor itinerary name.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000821 const std::string &ProcName = Proc->getName();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000822
Sebastian Pop9aa61372011-12-06 17:34:11 +0000823 // Skip default.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000824 if (ProcName == "NoItineraries")
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000825 continue;
826
Sebastian Pop9aa61372011-12-06 17:34:11 +0000827 // Sanity check for at least one instruction itinerary class.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000828 unsigned NItinClasses =
829 Records.getAllDerivedDefinitions("InstrItinClass").size();
830 if (NItinClasses == 0)
831 return;
832
Sebastian Pop9aa61372011-12-06 17:34:11 +0000833 // Get itinerary data list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000834 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
835
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000836 // Collect all instruction classes
837 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
838 FUNameToBitsMap, ItinDataList, maxStages, OS);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000839 }
840
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000841 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000842 // Run a worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000843 //
844 DFA D;
David Blaikied43046b2014-04-21 22:35:11 +0000845 const State *Initial = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000846 Initial->isInitial = true;
847 Initial->stateInfo.insert(0x0);
David Blaikied43046b2014-04-21 22:35:11 +0000848 SmallVector<const State*, 32> WorkList;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000849// std::queue<State*> WorkList;
David Blaikied43046b2014-04-21 22:35:11 +0000850 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000851
852 WorkList.push_back(Initial);
853
854 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000855 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000856 // C = {set of InsnClasses}
857 // Begin with initial node in worklist. Initial node does not have
858 // any consumed resources,
859 // ResourceState = 0x0
860 // Visited = {}
861 // While worklist != empty
862 // S = first element of worklist
863 // For every instruction class C
864 // if we can accommodate C in S:
865 // S' = state with resource states = {S Union C}
866 // Add a new transition: S x C -> S'
867 // If S' is not in Visited:
868 // Add S' to worklist
869 // Add S' to Visited
870 //
871 while (!WorkList.empty()) {
David Blaikied43046b2014-04-21 22:35:11 +0000872 const State *current = WorkList.pop_back_val();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000873 DEBUG(dbgs() << "---------------------\n");
874 DEBUG(dbgs() << "Processing state: " << current->stateNum << " - ");
875 dbgsStateInfo(current->stateInfo);
876 DEBUG(dbgs() << "\n");
877 for (unsigned i = 0; i < allInsnClasses.size(); i++) {
878 std::vector<unsigned> InsnClass = allInsnClasses[i];
879 DEBUG(dbgs() << i << " ");
880 dbgsInsnClass(InsnClass);
881 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000882
883 std::set<unsigned> NewStateResources;
884 //
885 // If we haven't already created a transition for this input
Sebastian Pop9aa61372011-12-06 17:34:11 +0000886 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000887 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000888 if (!current->hasTransition(InsnClass) &&
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000889 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
890 const State *NewState = NULL;
891 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
892 if (NewStateResources.size() == 0) {
893 DEBUG(dbgs() << " Skipped - no new states generated\n");
894 continue;
895 }
896
897 DEBUG(dbgs() << "\t");
898 dbgsStateInfo(NewStateResources);
899 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000900
901 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000902 // If we have seen this state before, then do not create a new state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000903 //
David Blaikied43046b2014-04-21 22:35:11 +0000904 auto VI = Visited.find(NewStateResources);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000905 if (VI != Visited.end()) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000906 NewState = VI->second;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000907 DEBUG(dbgs() << "\tFound existing state: "
908 << NewState->stateNum << " - ");
909 dbgsStateInfo(NewState->stateInfo);
910 DEBUG(dbgs() << "\n");
911 } else {
David Blaikied43046b2014-04-21 22:35:11 +0000912 NewState = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000913 NewState->stateInfo = NewStateResources;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000914 Visited[NewStateResources] = NewState;
915 WorkList.push_back(NewState);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000916 DEBUG(dbgs() << "\tAccepted new state: "
917 << NewState->stateNum << " - ");
918 dbgsStateInfo(NewState->stateInfo);
919 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000920 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000921
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000922 current->addTransition(InsnClass, NewState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000923 }
924 }
925 }
926
Sebastian Pop9aa61372011-12-06 17:34:11 +0000927 // Print out the table.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000928 D.writeTableAndAPI(OS, TargetName,
929 numInsnClasses, maxResources, numCombos, maxStages);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000930}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000931
932namespace llvm {
933
934void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
935 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
936 DFAPacketizerEmitter(RK).run(OS);
937}
938
939} // End llvm namespace