blob: f879a5bae21515dc291bd2715efda85c9570fcf4 [file] [log] [blame]
Eugene Zelenko6ac3f732016-01-26 18:48:36 +00001//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class parses the Schedule.td file and produces an API that can be used
11// to reason about whether an instruction can be added to a packet on a VLIW
12// architecture. The class internally generates a deterministic finite
13// automaton (DFA) that models all possible mappings of machine instructions
14// to functional units as instructions are added to a packet.
15//
16//===----------------------------------------------------------------------===//
17
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000018#define DEBUG_TYPE "dfa-emitter"
19
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000020#include "CodeGenTarget.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/ADT/DenseSet.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000022#include "llvm/ADT/SmallVector.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"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000027#include "llvm/Support/raw_ostream.h"
28#include <cassert>
29#include <cstdint>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000030#include <map>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000031#include <set>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000032#include <string>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000033#include <vector>
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000034
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000035using namespace llvm;
36
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000037// --------------------------------------------------------------------
38// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
39
40// DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
41// This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
42//
43// e.g. terms x resource bit combinations that fit in uint32_t:
44// 4 terms x 8 bits = 32 bits
45// 3 terms x 10 bits = 30 bits
46// 2 terms x 16 bits = 32 bits
47//
48// e.g. terms x resource bit combinations that fit in uint64_t:
49// 8 terms x 8 bits = 64 bits
50// 7 terms x 9 bits = 63 bits
51// 6 terms x 10 bits = 60 bits
52// 5 terms x 12 bits = 60 bits
53// 4 terms x 16 bits = 64 bits <--- current
54// 3 terms x 21 bits = 63 bits
55// 2 terms x 32 bits = 64 bits
56//
57#define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
58#define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
59
60typedef uint64_t DFAInput;
61typedef int64_t DFAStateInput;
62#define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
63
64namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000065
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000066 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
67 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
68 }
69
70 /// Return the DFAInput for an instruction class input vector.
71 /// This function is used in both DFAPacketizer.cpp and in
72 /// DFAPacketizerEmitter.cpp.
73 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
74 DFAInput InsnInput = 0;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000075 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
76 "Exceeded maximum number of DFA terms");
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000077 for (auto U : InsnClass)
78 InsnInput = addDFAFuncUnits(InsnInput, U);
79 return InsnInput;
80 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000081
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000082} // end anonymous namespace
83
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000084// --------------------------------------------------------------------
85
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000086#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000087// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
88//
89// dbgsInsnClass - When debugging, print instruction class stages.
90//
91void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
92//
93// dbgsStateInfo - When debugging, print the set of state info.
94//
95void dbgsStateInfo(const std::set<unsigned> &stateInfo);
96//
97// dbgsIndent - When debugging, indent by the specified amount.
98//
99void dbgsIndent(unsigned indent);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000100#endif
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000101
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000102//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000103// class DFAPacketizerEmitter: class that generates and prints out the DFA
104// for resource tracking.
105//
106namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000107
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000108class DFAPacketizerEmitter {
109private:
110 std::string TargetName;
111 //
112 // allInsnClasses is the set of all possible resources consumed by an
113 // InstrStage.
114 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000115 std::vector<std::vector<unsigned>> allInsnClasses;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000116 RecordKeeper &Records;
117
118public:
119 DFAPacketizerEmitter(RecordKeeper &R);
120
121 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000122 // collectAllFuncUnits - Construct a map of function unit names to bits.
123 //
124 int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
125 std::map<std::string, unsigned> &FUNameToBitsMap,
126 int &maxResources,
127 raw_ostream &OS);
128
129 //
130 // collectAllComboFuncs - Construct a map from a combo function unit bit to
131 // the bits of all included functional units.
132 //
133 int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
134 std::map<std::string, unsigned> &FUNameToBitsMap,
135 std::map<unsigned, unsigned> &ComboBitToBitsMap,
136 raw_ostream &OS);
137
138 //
139 // collectOneInsnClass - Populate allInsnClasses with one instruction class.
140 //
141 int collectOneInsnClass(const std::string &ProcName,
142 std::vector<Record*> &ProcItinList,
143 std::map<std::string, unsigned> &FUNameToBitsMap,
144 Record *ItinData,
145 raw_ostream &OS);
146
147 //
148 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000149 // used in each stage.
150 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000151 int collectAllInsnClasses(const std::string &ProcName,
152 std::vector<Record*> &ProcItinList,
153 std::map<std::string, unsigned> &FUNameToBitsMap,
154 std::vector<Record*> &ItinDataList,
155 int &maxStages,
156 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000157
158 void run(raw_ostream &OS);
159};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000160
161//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000162// State represents the usage of machine resources if the packet contains
163// a set of instruction classes.
164//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000165// Specifically, currentState is a set of bit-masks.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000166// The nth bit in a bit-mask indicates whether the nth resource is being used
167// by this state. The set of bit-masks in a state represent the different
168// possible outcomes of transitioning to this state.
Sebastian Pop9aa61372011-12-06 17:34:11 +0000169// For example: consider a two resource architecture: resource L and resource M
170// with three instruction classes: L, M, and L_or_M.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000171// From the initial state (currentState = 0x00), if we add instruction class
172// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
173// represents the possible resource states that can result from adding a L_or_M
174// instruction
175//
176// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Pop9aa61372011-12-06 17:34:11 +0000177// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000178//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000179// A State instance also contains a collection of transitions from that state:
180// a map from inputs to new states.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000181//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000182class State {
183 public:
184 static int currentStateNum;
David Blaikied43046b2014-04-21 22:35:11 +0000185 // stateNum is the only member used for equality/ordering, all other members
186 // can be mutated even in const State objects.
187 const int stateNum;
188 mutable bool isInitial;
189 mutable std::set<unsigned> stateInfo;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000190 typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
David Blaikied43046b2014-04-21 22:35:11 +0000191 mutable TransitionMap Transitions;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000192
193 State();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000194
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000195 bool operator<(const State &s) const {
196 return stateNum < s.stateNum;
197 }
198
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000199 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000200 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
201 // may be a valid transition from this state i.e., can an instruction of type
202 // InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000203 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000204 // Note that for multiple stages, this quick check does not take into account
205 // any possible resource competition between the stages themselves. That is
206 // enforced in AddInsnClassStages which checks the cross product of all
207 // stages for resource availability (which is a more involved check).
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000208 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000209 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
210 std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000211
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000212 //
Krzysztof Parzyszek4ca21fc2015-11-21 17:38:33 +0000213 // AddInsnClass - Return all combinations of resource reservation
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000214 // which are possible from this state (PossibleStates).
215 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000216 // PossibleStates is the set of valid resource states that ensue from valid
217 // transitions.
218 //
219 void AddInsnClass(std::vector<unsigned> &InsnClass,
220 std::map<unsigned, unsigned> &ComboBitToBitsMap,
221 std::set<unsigned> &PossibleStates) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000222
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000223 //
224 // AddInsnClassStages - Return all combinations of resource reservation
225 // resulting from the cross product of all stages for this InsnClass
226 // which are possible from this state (PossibleStates).
227 //
228 void AddInsnClassStages(std::vector<unsigned> &InsnClass,
229 std::map<unsigned, unsigned> &ComboBitToBitsMap,
230 unsigned chkstage, unsigned numstages,
231 unsigned prevState, unsigned origState,
232 DenseSet<unsigned> &VisitedResourceStates,
233 std::set<unsigned> &PossibleStates) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000234
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000235 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000236 // addTransition - Add a transition from this state given the input InsnClass
237 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000238 void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000239
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000240 //
241 // hasTransition - Returns true if there is a transition from this state
242 // given the input InsnClass
243 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000244 bool hasTransition(std::vector<unsigned> InsnClass) const;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000245};
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000246
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000247//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000248// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000249//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000250class DFA {
251public:
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000252 DFA() = default;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000253
Sebastian Pop9aa61372011-12-06 17:34:11 +0000254 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikied43046b2014-04-21 22:35:11 +0000255 typedef std::set<State> StateSet;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000256 StateSet states;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000257
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000258 State *currentState = nullptr;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000259
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000260 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000261 // Modify the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000262 //
David Blaikied43046b2014-04-21 22:35:11 +0000263 const State &newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000264
265 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000266 // writeTable: Print out a table representing the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000267 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000268 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
269 int numInsnClasses = 0,
270 int maxResources = 0, int numCombos = 0, int maxStages = 0);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000271};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000272
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000273} // end anonymous namespace
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000274
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000275#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000276// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
277//
278// dbgsInsnClass - When debugging, print instruction class stages.
279//
280void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
281 DEBUG(dbgs() << "InsnClass: ");
282 for (unsigned i = 0; i < InsnClass.size(); ++i) {
283 if (i > 0) {
284 DEBUG(dbgs() << ", ");
285 }
286 DEBUG(dbgs() << "0x" << utohexstr(InsnClass[i]));
287 }
288 DFAInput InsnInput = getDFAInsnInput(InsnClass);
289 DEBUG(dbgs() << " (input: 0x" << utohexstr(InsnInput) << ")");
290}
291
292//
293// dbgsStateInfo - When debugging, print the set of state info.
294//
295void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
296 DEBUG(dbgs() << "StateInfo: ");
297 unsigned i = 0;
298 for (std::set<unsigned>::iterator SI = stateInfo.begin();
299 SI != stateInfo.end(); ++SI, ++i) {
300 unsigned thisState = *SI;
301 if (i > 0) {
302 DEBUG(dbgs() << ", ");
303 }
304 DEBUG(dbgs() << "0x" << utohexstr(thisState));
305 }
306}
307
308//
309// dbgsIndent - When debugging, indent by the specified amount.
310//
311void dbgsIndent(unsigned indent) {
312 for (unsigned i = 0; i < indent; ++i) {
313 DEBUG(dbgs() << " ");
314 }
315}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000316#endif // NDEBUG
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000317
318//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000319// Constructors and destructors for State and DFA
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000320//
321State::State() :
322 stateNum(currentStateNum++), isInitial(false) {}
323
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000324//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000325// addTransition - Add a transition from this state given the input InsnClass
326//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000327void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
328 const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000329 assert(!Transitions.count(InsnClass) &&
330 "Cannot have multiple transitions for the same input");
331 Transitions[InsnClass] = To;
332}
333
334//
335// hasTransition - Returns true if there is a transition from this state
336// given the input InsnClass
337//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000338bool State::hasTransition(std::vector<unsigned> InsnClass) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000339 return Transitions.count(InsnClass) > 0;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000340}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000341
342//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000343// AddInsnClass - Return all combinations of resource reservation
344// which are possible from this state (PossibleStates).
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000345//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000346// PossibleStates is the set of valid resource states that ensue from valid
347// transitions.
348//
349void State::AddInsnClass(std::vector<unsigned> &InsnClass,
350 std::map<unsigned, unsigned> &ComboBitToBitsMap,
351 std::set<unsigned> &PossibleStates) const {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000352 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000353 // Iterate over all resource states in currentState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000354 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000355 unsigned numstages = InsnClass.size();
356 assert((numstages > 0) && "InsnClass has no stages");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000357
358 for (std::set<unsigned>::iterator SI = stateInfo.begin();
359 SI != stateInfo.end(); ++SI) {
360 unsigned thisState = *SI;
361
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000362 DenseSet<unsigned> VisitedResourceStates;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000363
364 DEBUG(dbgs() << " thisState: 0x" << utohexstr(thisState) << "\n");
365 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
366 numstages - 1, numstages,
367 thisState, thisState,
368 VisitedResourceStates, PossibleStates);
369 }
370}
371
372void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
373 std::map<unsigned, unsigned> &ComboBitToBitsMap,
374 unsigned chkstage, unsigned numstages,
375 unsigned prevState, unsigned origState,
376 DenseSet<unsigned> &VisitedResourceStates,
377 std::set<unsigned> &PossibleStates) const {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000378 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
379 unsigned thisStage = InsnClass[chkstage];
380
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000381 DEBUG({
382 dbgsIndent((1 + numstages - chkstage) << 1);
383 dbgs() << "AddInsnClassStages " << chkstage << " (0x"
384 << utohexstr(thisStage) << ") from ";
385 dbgsInsnClass(InsnClass);
386 dbgs() << "\n";
387 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000388
389 //
390 // Iterate over all possible resources used in thisStage.
391 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
392 //
393 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
394 unsigned resourceMask = (0x1 << j);
395 if (resourceMask & thisStage) {
396 unsigned combo = ComboBitToBitsMap[resourceMask];
397 if (combo && ((~prevState & combo) != combo)) {
398 DEBUG(dbgs() << "\tSkipped Add 0x" << utohexstr(prevState)
399 << " - combo op 0x" << utohexstr(resourceMask)
400 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
401 continue;
402 }
403 //
404 // For each possible resource used in thisStage, generate the
405 // resource state if that resource was used.
406 //
407 unsigned ResultingResourceState = prevState | resourceMask | combo;
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000408 DEBUG({
409 dbgsIndent((2 + numstages - chkstage) << 1);
410 dbgs() << "0x" << utohexstr(prevState)
411 << " | 0x" << utohexstr(resourceMask);
412 if (combo)
413 dbgs() << " | 0x" << utohexstr(combo);
414 dbgs() << " = 0x" << utohexstr(ResultingResourceState) << " ";
415 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000416
417 //
418 // If this is the final stage for this class
419 //
420 if (chkstage == 0) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000421 //
422 // Check if the resulting resource state can be accommodated in this
Sebastian Pop9aa61372011-12-06 17:34:11 +0000423 // packet.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000424 // We compute resource OR prevState (originally started as origState).
425 // If the result of the OR is different than origState, it implies
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000426 // that there is at least one resource that can be used to schedule
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000427 // thisStage in the current packet.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000428 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Pop9aa61372011-12-06 17:34:11 +0000429 // processed ResultingResourceState before.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000430 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000431 if (ResultingResourceState != prevState) {
432 if (VisitedResourceStates.count(ResultingResourceState) == 0) {
433 VisitedResourceStates.insert(ResultingResourceState);
434 PossibleStates.insert(ResultingResourceState);
435 DEBUG(dbgs() << "\tResultingResourceState: 0x"
436 << utohexstr(ResultingResourceState) << "\n");
437 } else {
438 DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
439 }
440 } else {
441 DEBUG(dbgs() << "\tSkipped Add - no final resources available\n");
442 }
443 } else {
444 //
445 // If the current resource can be accommodated, check the next
446 // stage in InsnClass for available resources.
447 //
448 if (ResultingResourceState != prevState) {
449 DEBUG(dbgs() << "\n");
450 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
451 chkstage - 1, numstages,
452 ResultingResourceState, origState,
453 VisitedResourceStates, PossibleStates);
454 } else {
455 DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000456 }
457 }
458 }
459 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000460}
461
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000462//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000463// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
464// may be a valid transition from this state i.e., can an instruction of type
465// InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000466//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000467// Note that this routine is performing conservative checks that can be
468// quickly executed acting as a filter before calling AddInsnClassStages.
469// Any cases allowed through here will be caught later in AddInsnClassStages
470// which performs the more expensive exact check.
471//
472bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
473 std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
Alexey Samsonov420a4ed2012-06-28 07:47:50 +0000474 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000475 SI != stateInfo.end(); ++SI) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000476 // Check to see if all required resources are available.
477 bool available = true;
478
479 // Inspect each stage independently.
480 // note: This is a conservative check as we aren't checking for
481 // possible resource competition between the stages themselves
482 // The full cross product is examined later in AddInsnClass.
483 for (unsigned i = 0; i < InsnClass.size(); ++i) {
484 unsigned resources = *SI;
485 if ((~resources & InsnClass[i]) == 0) {
486 available = false;
487 break;
488 }
489 // Make sure _all_ resources for a combo function are available.
490 // note: This is a quick conservative check as it won't catch an
491 // unscheduleable combo if this stage is an OR expression
492 // containing a combo.
493 // These cases are caught later in AddInsnClass.
494 unsigned combo = ComboBitToBitsMap[InsnClass[i]];
495 if (combo && ((~resources & combo) != combo)) {
496 DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x" << utohexstr(resources)
497 << " - combo op 0x" << utohexstr(InsnClass[i])
498 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n");
499 available = false;
500 break;
501 }
502 }
503
504 if (available) {
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000505 return true;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000506 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000507 }
508 return false;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000509}
510
David Blaikied43046b2014-04-21 22:35:11 +0000511const State &DFA::newState() {
David Blaikie4bcfcc12014-04-21 22:46:09 +0000512 auto IterPair = states.insert(State());
David Blaikied43046b2014-04-21 22:35:11 +0000513 assert(IterPair.second && "State already exists");
514 return *IterPair.first;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000515}
516
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000517int State::currentStateNum = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000518
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000519DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000520 TargetName(CodeGenTarget(R).getName()), Records(R) {}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000521
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000522//
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) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000536 unsigned numStates = states.size();
537
538 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
539 DEBUG(dbgs() << "writeTableAndAPI\n");
540 DEBUG(dbgs() << "Total states: " << numStates << "\n");
541
542 OS << "namespace llvm {\n";
543
544 OS << "\n// Input format:\n";
545 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
546 << "\t// maximum AND'ed resource terms\n";
547 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
548 << "\t// maximum resource bits in one term\n";
549
550 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
551 << "pairs of <Input, NextState> for all valid\n";
552 OS << "// transitions.\n";
553 OS << "// " << numStates << "\tstates\n";
554 OS << "// " << numInsnClasses << "\tinstruction classes\n";
555 OS << "// " << maxResources << "\tresources max\n";
556 OS << "// " << numCombos << "\tcombo resources\n";
557 OS << "// " << maxStages << "\tstages max\n";
558 OS << "const " << DFA_TBLTYPE << " "
559 << TargetName << "DFAStateInputTable[][2] = {\n";
560
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000561 // This table provides a map to the beginning of the transitions for State s
Sebastian Pop9aa61372011-12-06 17:34:11 +0000562 // in DFAStateInputTable.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000563 std::vector<int> StateEntry(numStates+1);
564 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000565
566 // Tracks the total valid transitions encountered so far. It is used
Sebastian Pop9aa61372011-12-06 17:34:11 +0000567 // to construct the StateEntry table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000568 int ValidTransitions = 0;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000569 DFA::StateSet::iterator SI = states.begin();
570 for (unsigned i = 0; i < numStates; ++i, ++SI) {
David Blaikied43046b2014-04-21 22:35:11 +0000571 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000572 StateEntry[i] = ValidTransitions;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000573 for (State::TransitionMap::iterator
David Blaikied43046b2014-04-21 22:35:11 +0000574 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000575 II != IE; ++II) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000576 OS << "{0x" << utohexstr(getDFAInsnInput(II->first)) << ", "
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000577 << II->second->stateNum
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000578 << "},\t";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000579 }
David Blaikied43046b2014-04-21 22:35:11 +0000580 ValidTransitions += SI->Transitions.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000581
Sebastian Pop9aa61372011-12-06 17:34:11 +0000582 // If there are no valid transitions from this stage, we need a sentinel
583 // transition.
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000584 if (ValidTransitions == StateEntry[i]) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000585 OS << SentinelEntry << ",\t";
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000586 ++ValidTransitions;
587 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000588
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000589 OS << " // state " << i << ": " << StateEntry[i];
590 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
591 OS << "-" << (ValidTransitions-1);
592 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000593 OS << "\n";
594 }
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000595
596 // Print out a sentinel entry at the end of the StateInputTable. This is
597 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000598 OS << SentinelEntry << "\t";
599 OS << " // state " << numStates << ": " << ValidTransitions;
600 OS << "\n";
601
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000602 OS << "};\n\n";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000603 OS << "// " << TargetName << "DFAStateEntryTable[i] = "
604 << "Index of the first entry in DFAStateInputTable for\n";
605 OS << "// "
606 << "the ith state.\n";
607 OS << "// " << numStates << " states\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000608 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
609
610 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Pop9aa61372011-12-06 17:34:11 +0000611 // two numbers.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000612 unsigned lastState = 0;
613 for (unsigned i = 0; i < numStates; ++i) {
614 if (i && ((i % 10) == 0)) {
615 lastState = i-1;
616 OS << " // states " << (i-10) << ":" << lastState << "\n";
617 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000618 OS << StateEntry[i] << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000619 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000620
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000621 // Print out the index to the sentinel entry in StateInputTable
622 OS << ValidTransitions << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000623 OS << " // states " << (lastState+1) << ":" << numStates << "\n";
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000624
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000625 OS << "};\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000626 OS << "} // namespace\n";
627
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000628 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000629 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000630 //
631 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
632 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
633 OS << "namespace llvm {\n";
Sebastian Popac35a4d2011-12-06 17:34:16 +0000634 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000635 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
636 << " return new DFAPacketizer(IID, " << TargetName
637 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
638 OS << "} // End llvm namespace \n";
639}
640
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000641//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000642// collectAllFuncUnits - Construct a map of function unit names to bits.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000643//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000644int DFAPacketizerEmitter::collectAllFuncUnits(
645 std::vector<Record*> &ProcItinList,
646 std::map<std::string, unsigned> &FUNameToBitsMap,
647 int &maxFUs,
648 raw_ostream &OS) {
649 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
650 DEBUG(dbgs() << "collectAllFuncUnits");
651 DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000652
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000653 int totalFUs = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000654 // Parse functional units for all the itineraries.
655 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
656 Record *Proc = ProcItinList[i];
657 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
658
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000659 DEBUG(dbgs() << " FU:" << i
660 << " (" << FUs.size() << " FUs) "
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000661 << Proc->getName());
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000662
663
Sebastian Pop9aa61372011-12-06 17:34:11 +0000664 // Convert macros to bits for each stage.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000665 unsigned numFUs = FUs.size();
666 for (unsigned j = 0; j < numFUs; ++j) {
667 assert ((j < DFA_MAX_RESOURCES) &&
668 "Exceeded maximum number of representable resources");
669 unsigned FuncResources = (unsigned) (1U << j);
670 FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
671 DEBUG(dbgs() << " " << FUs[j]->getName()
672 << ":0x" << utohexstr(FuncResources));
673 }
674 if (((int) numFUs) > maxFUs) {
675 maxFUs = numFUs;
676 }
677 totalFUs += numFUs;
678 DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000679 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000680 return totalFUs;
681}
682
683//
684// collectAllComboFuncs - Construct a map from a combo function unit bit to
685// the bits of all included functional units.
686//
687int DFAPacketizerEmitter::collectAllComboFuncs(
688 std::vector<Record*> &ComboFuncList,
689 std::map<std::string, unsigned> &FUNameToBitsMap,
690 std::map<unsigned, unsigned> &ComboBitToBitsMap,
691 raw_ostream &OS) {
692 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n");
693 DEBUG(dbgs() << "collectAllComboFuncs");
694 DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
695
696 int numCombos = 0;
697 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
698 Record *Func = ComboFuncList[i];
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000699 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
700
701 DEBUG(dbgs() << " CFD:" << i
702 << " (" << FUs.size() << " combo FUs) "
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000703 << Func->getName() << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000704
705 // Convert macros to bits for each stage.
706 for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
707 assert ((j < DFA_MAX_RESOURCES) &&
708 "Exceeded maximum number of DFA resources");
709 Record *FuncData = FUs[j];
710 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
711 const std::vector<Record*> &FuncList =
712 FuncData->getValueAsListOfDefs("FuncList");
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +0000713 const std::string &ComboFuncName = ComboFunc->getName();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000714 unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
715 unsigned ComboResources = ComboBit;
716 DEBUG(dbgs() << " combo: " << ComboFuncName
717 << ":0x" << utohexstr(ComboResources) << "\n");
718 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
719 std::string FuncName = FuncList[k]->getName();
720 unsigned FuncResources = FUNameToBitsMap[FuncName];
721 DEBUG(dbgs() << " " << FuncName
722 << ":0x" << utohexstr(FuncResources) << "\n");
723 ComboResources |= FuncResources;
724 }
725 ComboBitToBitsMap[ComboBit] = ComboResources;
726 numCombos++;
727 DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
728 << utohexstr(ComboBit) << " = 0x"
729 << utohexstr(ComboResources) << "\n");
730 }
731 }
732 return numCombos;
733}
734
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000735//
736// collectOneInsnClass - Populate allInsnClasses with one instruction class
737//
738int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
739 std::vector<Record*> &ProcItinList,
740 std::map<std::string, unsigned> &FUNameToBitsMap,
741 Record *ItinData,
742 raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000743 const std::vector<Record*> &StageList =
744 ItinData->getValueAsListOfDefs("Stages");
745
Sebastian Pop9aa61372011-12-06 17:34:11 +0000746 // The number of stages.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000747 unsigned NStages = StageList.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000748
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000749 DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000750 << "\n");
751
752 std::vector<unsigned> UnitBits;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000753
Sebastian Pop9aa61372011-12-06 17:34:11 +0000754 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000755 for (unsigned i = 0; i < NStages; ++i) {
756 const Record *Stage = StageList[i];
757
Sebastian Pop9aa61372011-12-06 17:34:11 +0000758 // Get unit list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000759 const std::vector<Record*> &UnitList =
760 Stage->getValueAsListOfDefs("Units");
761
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000762 DEBUG(dbgs() << " stage:" << i
763 << " [" << UnitList.size() << " units]:");
764 unsigned dbglen = 26; // cursor after stage dbgs
765
766 // Compute the bitwise or of each unit used in this stage.
767 unsigned UnitBitValue = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000768 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000769 // Conduct bitwise or.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000770 std::string UnitName = UnitList[j]->getName();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000771 DEBUG(dbgs() << " " << j << ":" << UnitName);
772 dbglen += 3 + UnitName.length();
773 assert(FUNameToBitsMap.count(UnitName));
774 UnitBitValue |= FUNameToBitsMap[UnitName];
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000775 }
776
777 if (UnitBitValue != 0)
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000778 UnitBits.push_back(UnitBitValue);
779
780 while (dbglen <= 64) { // line up bits dbgs
781 dbglen += 8;
782 DEBUG(dbgs() << "\t");
783 }
784 DEBUG(dbgs() << " (bits: 0x" << utohexstr(UnitBitValue) << ")\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000785 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000786
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000787 if (!UnitBits.empty())
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000788 allInsnClasses.push_back(UnitBits);
789
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000790 DEBUG({
791 dbgs() << " ";
792 dbgsInsnClass(UnitBits);
793 dbgs() << "\n";
794 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000795
796 return NStages;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000797}
798
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000799//
800// collectAllInsnClasses - Populate allInsnClasses which is a set of units
801// used in each stage.
802//
803int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
804 std::vector<Record*> &ProcItinList,
805 std::map<std::string, unsigned> &FUNameToBitsMap,
806 std::vector<Record*> &ItinDataList,
807 int &maxStages,
808 raw_ostream &OS) {
809 // Collect all instruction classes.
810 unsigned M = ItinDataList.size();
811
812 int numInsnClasses = 0;
813 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"
814 << "collectAllInsnClasses "
815 << ProcName
816 << " (" << M << " classes)\n");
817
818 // Collect stages for each instruction class for all itinerary data
819 for (unsigned j = 0; j < M; j++) {
820 Record *ItinData = ItinDataList[j];
821 int NStages = collectOneInsnClass(ProcName, ProcItinList,
822 FUNameToBitsMap, ItinData, OS);
823 if (NStages > maxStages) {
824 maxStages = NStages;
825 }
826 numInsnClasses++;
827 }
828 return numInsnClasses;
829}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000830
831//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000832// Run the worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000833//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000834void DFAPacketizerEmitter::run(raw_ostream &OS) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000835 // Collect processor iteraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000836 std::vector<Record*> ProcItinList =
837 Records.getAllDerivedDefinitions("ProcessorItineraries");
838
839 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000840 // Collect the Functional units.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000841 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000842 std::map<std::string, unsigned> FUNameToBitsMap;
843 int maxResources = 0;
844 collectAllFuncUnits(ProcItinList,
845 FUNameToBitsMap, maxResources, OS);
846
847 //
848 // Collect the Combo Functional units.
849 //
850 std::map<unsigned, unsigned> ComboBitToBitsMap;
851 std::vector<Record*> ComboFuncList =
852 Records.getAllDerivedDefinitions("ComboFuncUnits");
853 int numCombos = collectAllComboFuncs(ComboFuncList,
854 FUNameToBitsMap, ComboBitToBitsMap, OS);
855
856 //
857 // Collect the itineraries.
858 //
859 int maxStages = 0;
860 int numInsnClasses = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000861 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
862 Record *Proc = ProcItinList[i];
863
Sebastian Pop9aa61372011-12-06 17:34:11 +0000864 // Get processor itinerary name.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000865 const std::string &ProcName = Proc->getName();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000866
Sebastian Pop9aa61372011-12-06 17:34:11 +0000867 // Skip default.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000868 if (ProcName == "NoItineraries")
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000869 continue;
870
Sebastian Pop9aa61372011-12-06 17:34:11 +0000871 // Sanity check for at least one instruction itinerary class.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000872 unsigned NItinClasses =
873 Records.getAllDerivedDefinitions("InstrItinClass").size();
874 if (NItinClasses == 0)
875 return;
876
Sebastian Pop9aa61372011-12-06 17:34:11 +0000877 // Get itinerary data list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000878 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
879
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000880 // Collect all instruction classes
881 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
882 FUNameToBitsMap, ItinDataList, maxStages, OS);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000883 }
884
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000885 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000886 // Run a worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000887 //
888 DFA D;
David Blaikied43046b2014-04-21 22:35:11 +0000889 const State *Initial = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000890 Initial->isInitial = true;
891 Initial->stateInfo.insert(0x0);
David Blaikied43046b2014-04-21 22:35:11 +0000892 SmallVector<const State*, 32> WorkList;
893 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000894
895 WorkList.push_back(Initial);
896
897 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000898 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000899 // C = {set of InsnClasses}
900 // Begin with initial node in worklist. Initial node does not have
901 // any consumed resources,
902 // ResourceState = 0x0
903 // Visited = {}
904 // While worklist != empty
905 // S = first element of worklist
906 // For every instruction class C
907 // if we can accommodate C in S:
908 // S' = state with resource states = {S Union C}
909 // Add a new transition: S x C -> S'
910 // If S' is not in Visited:
911 // Add S' to worklist
912 // Add S' to Visited
913 //
914 while (!WorkList.empty()) {
David Blaikied43046b2014-04-21 22:35:11 +0000915 const State *current = WorkList.pop_back_val();
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000916 DEBUG({
917 dbgs() << "---------------------\n";
918 dbgs() << "Processing state: " << current->stateNum << " - ";
919 dbgsStateInfo(current->stateInfo);
920 dbgs() << "\n";
921 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000922 for (unsigned i = 0; i < allInsnClasses.size(); i++) {
923 std::vector<unsigned> InsnClass = allInsnClasses[i];
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000924 DEBUG({
925 dbgs() << i << " ";
926 dbgsInsnClass(InsnClass);
927 dbgs() << "\n";
928 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000929
930 std::set<unsigned> NewStateResources;
931 //
932 // If we haven't already created a transition for this input
Sebastian Pop9aa61372011-12-06 17:34:11 +0000933 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000934 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000935 if (!current->hasTransition(InsnClass) &&
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000936 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000937 const State *NewState = nullptr;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000938 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000939 if (NewStateResources.empty()) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000940 DEBUG(dbgs() << " Skipped - no new states generated\n");
941 continue;
942 }
943
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000944 DEBUG({
945 dbgs() << "\t";
946 dbgsStateInfo(NewStateResources);
947 dbgs() << "\n";
948 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000949
950 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000951 // If we have seen this state before, then do not create a new state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000952 //
David Blaikied43046b2014-04-21 22:35:11 +0000953 auto VI = Visited.find(NewStateResources);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000954 if (VI != Visited.end()) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000955 NewState = VI->second;
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000956 DEBUG({
957 dbgs() << "\tFound existing state: " << NewState->stateNum
958 << " - ";
959 dbgsStateInfo(NewState->stateInfo);
960 dbgs() << "\n";
961 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000962 } else {
David Blaikied43046b2014-04-21 22:35:11 +0000963 NewState = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000964 NewState->stateInfo = NewStateResources;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000965 Visited[NewStateResources] = NewState;
966 WorkList.push_back(NewState);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000967 DEBUG({
968 dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
969 dbgsStateInfo(NewState->stateInfo);
970 dbgs() << "\n";
971 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000972 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000973
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000974 current->addTransition(InsnClass, NewState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000975 }
976 }
977 }
978
Sebastian Pop9aa61372011-12-06 17:34:11 +0000979 // Print out the table.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000980 D.writeTableAndAPI(OS, TargetName,
981 numInsnClasses, maxResources, numCombos, maxStages);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000982}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000983
984namespace llvm {
985
986void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
987 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
988 DFAPacketizerEmitter(RK).run(OS);
989}
990
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000991} // end namespace llvm