blob: 0db0f55f5ed6d9c63e1e076d896b5a7546ffbc27 [file] [log] [blame]
Eugene Zelenko51ecde12016-01-26 18:48:36 +00001//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
Anshuman Dasguptadc81e5d2011-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 Parzyszekbf390b02015-11-21 20:00:45 +000018#define DEBUG_TYPE "dfa-emitter"
19
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000020#include "CodeGenTarget.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000021#include "llvm/ADT/DenseSet.h"
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000022#include "llvm/ADT/SmallVector.h"
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +000023#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include "llvm/TableGen/Record.h"
25#include "llvm/TableGen/TableGenBackend.h"
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +000026#include "llvm/Support/Debug.h"
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000027#include "llvm/Support/raw_ostream.h"
28#include <cassert>
29#include <cstdint>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000030#include <map>
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000031#include <set>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000032#include <string>
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000033#include <vector>
Eugene Zelenko51ecde12016-01-26 18:48:36 +000034
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000035using namespace llvm;
36
Krzysztof Parzyszekd8d11cb2015-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 Zelenkoc02caf52016-11-30 17:48:10 +000065
Krzysztof Parzyszekd8d11cb2015-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 Zelenkoc02caf52016-11-30 17:48:10 +000075 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
76 "Exceeded maximum number of DFA terms");
Krzysztof Parzyszekd8d11cb2015-11-22 15:20:19 +000077 for (auto U : InsnClass)
78 InsnInput = addDFAFuncUnits(InsnInput, U);
79 return InsnInput;
80 }
Eugene Zelenkoc02caf52016-11-30 17:48:10 +000081
Eugene Zelenko51ecde12016-01-26 18:48:36 +000082} // end anonymous namespace
83
Krzysztof Parzyszekd8d11cb2015-11-22 15:20:19 +000084// --------------------------------------------------------------------
85
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +000086#ifndef NDEBUG
Krzysztof Parzyszekbf390b02015-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 Parzyszekb08711c2015-11-21 22:46:52 +0000100#endif
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000101
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000102//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000103// class DFAPacketizerEmitter: class that generates and prints out the DFA
104// for resource tracking.
105//
106namespace {
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000107
Jakob Stoklund Olesen6f36fa92012-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 Parzyszekbf390b02015-11-21 20:00:45 +0000115 std::vector<std::vector<unsigned>> allInsnClasses;
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000116 RecordKeeper &Records;
117
118public:
119 DFAPacketizerEmitter(RecordKeeper &R);
120
121 //
Krzysztof Parzyszekbf390b02015-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 Olesen6f36fa92012-06-11 15:37:55 +0000149 // used in each stage.
150 //
Krzysztof Parzyszekbf390b02015-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 Olesen6f36fa92012-06-11 15:37:55 +0000157
158 void run(raw_ostream &OS);
159};
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000160
161//
Anshuman Dasguptadc81e5d2011-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 Popf6f77e92011-12-06 17:34:11 +0000165// Specifically, currentState is a set of bit-masks.
Anshuman Dasguptadc81e5d2011-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 Popf6f77e92011-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 Dasguptadc81e5d2011-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 Popf6f77e92011-12-06 17:34:11 +0000177// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000178//
Anshuman Dasgupta3adf3b02012-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 Dasguptadc81e5d2011-12-01 21:10:21 +0000181//
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000182class State {
183 public:
184 static int currentStateNum;
David Blaikiec37ae282014-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 Parzyszekbf390b02015-11-21 20:00:45 +0000190 typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
David Blaikiec37ae282014-04-21 22:35:11 +0000191 mutable TransitionMap Transitions;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000192
193 State();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000194
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000195 bool operator<(const State &s) const {
196 return stateNum < s.stateNum;
197 }
198
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000199 //
Krzysztof Parzyszekbf390b02015-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 Dasguptae2529dc2012-06-27 19:38:29 +0000203 //
Krzysztof Parzyszekbf390b02015-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 Parzyszekc7fdae22015-11-21 17:23:52 +0000208 //
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000209 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
210 std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000211
Krzysztof Parzyszekc7fdae22015-11-21 17:23:52 +0000212 //
Krzysztof Parzyszeka00b4f62015-11-21 17:38:33 +0000213 // AddInsnClass - Return all combinations of resource reservation
Krzysztof Parzyszekc7fdae22015-11-21 17:23:52 +0000214 // which are possible from this state (PossibleStates).
215 //
Krzysztof Parzyszekbf390b02015-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 Zelenkoc02caf52016-11-30 17:48:10 +0000222
Krzysztof Parzyszekbf390b02015-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 Zelenkoc02caf52016-11-30 17:48:10 +0000234
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000235 //
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000236 // addTransition - Add a transition from this state given the input InsnClass
237 //
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000238 void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000239
Anshuman Dasgupta3adf3b02012-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 Parzyszekbf390b02015-11-21 20:00:45 +0000244 bool hasTransition(std::vector<unsigned> InsnClass) const;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000245};
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000246
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000247//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000248// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000249//
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000250class DFA {
251public:
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000252 DFA() = default;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000253
Sebastian Popf6f77e92011-12-06 17:34:11 +0000254 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikiec37ae282014-04-21 22:35:11 +0000255 typedef std::set<State> StateSet;
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000256 StateSet states;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000257
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000258 State *currentState = nullptr;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000259
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000260 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000261 // Modify the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000262 //
David Blaikiec37ae282014-04-21 22:35:11 +0000263 const State &newState();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000264
265 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000266 // writeTable: Print out a table representing the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000267 //
Krzysztof Parzyszekbf390b02015-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 Dasguptadc81e5d2011-12-01 21:10:21 +0000271};
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000272
Eugene Zelenko51ecde12016-01-26 18:48:36 +0000273} // end anonymous namespace
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000274
Krzysztof Parzyszekb61ef9d2015-11-21 22:19:50 +0000275#ifndef NDEBUG
Krzysztof Parzyszekbf390b02015-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) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000281 LLVM_DEBUG(dbgs() << "InsnClass: ");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000282 for (unsigned i = 0; i < InsnClass.size(); ++i) {
283 if (i > 0) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000284 LLVM_DEBUG(dbgs() << ", ");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000285 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000286 LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(InsnClass[i]));
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000287 }
288 DFAInput InsnInput = getDFAInsnInput(InsnClass);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000289 LLVM_DEBUG(dbgs() << " (input: 0x" << Twine::utohexstr(InsnInput) << ")");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000290}
291
292//
293// dbgsStateInfo - When debugging, print the set of state info.
294//
295void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000296 LLVM_DEBUG(dbgs() << "StateInfo: ");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000297 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) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000302 LLVM_DEBUG(dbgs() << ", ");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000303 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000304 LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(thisState));
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000305 }
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) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000313 LLVM_DEBUG(dbgs() << " ");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000314 }
315}
Eugene Zelenko51ecde12016-01-26 18:48:36 +0000316#endif // NDEBUG
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000317
318//
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000319// Constructors and destructors for State and DFA
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000320//
321State::State() :
322 stateNum(currentStateNum++), isInitial(false) {}
323
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000324//
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000325// addTransition - Add a transition from this state given the input InsnClass
326//
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000327void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
328 const {
Anshuman Dasgupta3adf3b02012-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 Parzyszekbf390b02015-11-21 20:00:45 +0000338bool State::hasTransition(std::vector<unsigned> InsnClass) const {
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000339 return Transitions.count(InsnClass) > 0;
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000340}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000341
342//
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000343// AddInsnClass - Return all combinations of resource reservation
344// which are possible from this state (PossibleStates).
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000345//
Krzysztof Parzyszekbf390b02015-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 Dasguptadc81e5d2011-12-01 21:10:21 +0000352 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000353 // Iterate over all resource states in currentState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000354 //
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000355 unsigned numstages = InsnClass.size();
356 assert((numstages > 0) && "InsnClass has no stages");
Anshuman Dasguptadc81e5d2011-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 Dasguptadc81e5d2011-12-01 21:10:21 +0000362 DenseSet<unsigned> VisitedResourceStates;
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000363
Nicola Zaghen0818e782018-05-14 12:53:11 +0000364 LLVM_DEBUG(dbgs() << " thisState: 0x" << Twine::utohexstr(thisState)
365 << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000366 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
367 numstages - 1, numstages,
368 thisState, thisState,
369 VisitedResourceStates, PossibleStates);
370 }
371}
372
373void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
374 std::map<unsigned, unsigned> &ComboBitToBitsMap,
375 unsigned chkstage, unsigned numstages,
376 unsigned prevState, unsigned origState,
377 DenseSet<unsigned> &VisitedResourceStates,
378 std::set<unsigned> &PossibleStates) const {
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000379 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
380 unsigned thisStage = InsnClass[chkstage];
381
Nicola Zaghen0818e782018-05-14 12:53:11 +0000382 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000383 dbgsIndent((1 + numstages - chkstage) << 1);
384 dbgs() << "AddInsnClassStages " << chkstage << " (0x"
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000385 << Twine::utohexstr(thisStage) << ") from ";
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000386 dbgsInsnClass(InsnClass);
387 dbgs() << "\n";
388 });
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000389
390 //
391 // Iterate over all possible resources used in thisStage.
392 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
393 //
394 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
395 unsigned resourceMask = (0x1 << j);
396 if (resourceMask & thisStage) {
397 unsigned combo = ComboBitToBitsMap[resourceMask];
398 if (combo && ((~prevState & combo) != combo)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000399 LLVM_DEBUG(dbgs() << "\tSkipped Add 0x" << Twine::utohexstr(prevState)
400 << " - combo op 0x" << Twine::utohexstr(resourceMask)
401 << " (0x" << Twine::utohexstr(combo)
402 << ") cannot be scheduled\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000403 continue;
404 }
405 //
406 // For each possible resource used in thisStage, generate the
407 // resource state if that resource was used.
408 //
409 unsigned ResultingResourceState = prevState | resourceMask | combo;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000410 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000411 dbgsIndent((2 + numstages - chkstage) << 1);
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000412 dbgs() << "0x" << Twine::utohexstr(prevState) << " | 0x"
413 << Twine::utohexstr(resourceMask);
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000414 if (combo)
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000415 dbgs() << " | 0x" << Twine::utohexstr(combo);
416 dbgs() << " = 0x" << Twine::utohexstr(ResultingResourceState) << " ";
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000417 });
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000418
419 //
420 // If this is the final stage for this class
421 //
422 if (chkstage == 0) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000423 //
424 // Check if the resulting resource state can be accommodated in this
Sebastian Popf6f77e92011-12-06 17:34:11 +0000425 // packet.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000426 // We compute resource OR prevState (originally started as origState).
427 // If the result of the OR is different than origState, it implies
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000428 // that there is at least one resource that can be used to schedule
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000429 // thisStage in the current packet.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000430 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Popf6f77e92011-12-06 17:34:11 +0000431 // processed ResultingResourceState before.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000432 //
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000433 if (ResultingResourceState != prevState) {
434 if (VisitedResourceStates.count(ResultingResourceState) == 0) {
435 VisitedResourceStates.insert(ResultingResourceState);
436 PossibleStates.insert(ResultingResourceState);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000437 LLVM_DEBUG(dbgs()
438 << "\tResultingResourceState: 0x"
439 << Twine::utohexstr(ResultingResourceState) << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000440 } else {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000441 LLVM_DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000442 }
443 } else {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000444 LLVM_DEBUG(dbgs()
445 << "\tSkipped Add - no final resources available\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000446 }
447 } else {
448 //
449 // If the current resource can be accommodated, check the next
450 // stage in InsnClass for available resources.
451 //
452 if (ResultingResourceState != prevState) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000453 LLVM_DEBUG(dbgs() << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000454 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
455 chkstage - 1, numstages,
456 ResultingResourceState, origState,
457 VisitedResourceStates, PossibleStates);
458 } else {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000459 LLVM_DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000460 }
461 }
462 }
463 }
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000464}
465
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000466//
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000467// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
468// may be a valid transition from this state i.e., can an instruction of type
469// InsnClass be added to the packet represented by this state.
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000470//
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000471// Note that this routine is performing conservative checks that can be
472// quickly executed acting as a filter before calling AddInsnClassStages.
473// Any cases allowed through here will be caught later in AddInsnClassStages
474// which performs the more expensive exact check.
475//
476bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
477 std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
Alexey Samsonov87dc7a42012-06-28 07:47:50 +0000478 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000479 SI != stateInfo.end(); ++SI) {
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000480 // Check to see if all required resources are available.
481 bool available = true;
482
483 // Inspect each stage independently.
484 // note: This is a conservative check as we aren't checking for
485 // possible resource competition between the stages themselves
486 // The full cross product is examined later in AddInsnClass.
487 for (unsigned i = 0; i < InsnClass.size(); ++i) {
488 unsigned resources = *SI;
489 if ((~resources & InsnClass[i]) == 0) {
490 available = false;
491 break;
492 }
493 // Make sure _all_ resources for a combo function are available.
494 // note: This is a quick conservative check as it won't catch an
495 // unscheduleable combo if this stage is an OR expression
496 // containing a combo.
497 // These cases are caught later in AddInsnClass.
498 unsigned combo = ComboBitToBitsMap[InsnClass[i]];
499 if (combo && ((~resources & combo) != combo)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000500 LLVM_DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x"
501 << Twine::utohexstr(resources) << " - combo op 0x"
502 << Twine::utohexstr(InsnClass[i]) << " (0x"
503 << Twine::utohexstr(combo)
504 << ") cannot be scheduled\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000505 available = false;
506 break;
507 }
508 }
509
510 if (available) {
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000511 return true;
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000512 }
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000513 }
514 return false;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000515}
516
David Blaikiec37ae282014-04-21 22:35:11 +0000517const State &DFA::newState() {
David Blaikie9f38d0d2014-04-21 22:46:09 +0000518 auto IterPair = states.insert(State());
David Blaikiec37ae282014-04-21 22:35:11 +0000519 assert(IterPair.second && "State already exists");
520 return *IterPair.first;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000521}
522
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000523int State::currentStateNum = 0;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000524
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000525DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000526 TargetName(CodeGenTarget(R).getName()), Records(R) {}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000527
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000528//
529// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000530// associated API to create a DFA packetizer.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000531//
532// Format:
533// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000534// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000535// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Popf6f77e92011-12-06 17:34:11 +0000536// the ith state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000537//
538//
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000539void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
540 int numInsnClasses,
541 int maxResources, int numCombos, int maxStages) {
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000542 unsigned numStates = states.size();
543
Nicola Zaghen0818e782018-05-14 12:53:11 +0000544 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
545 "----------------------\n");
546 LLVM_DEBUG(dbgs() << "writeTableAndAPI\n");
547 LLVM_DEBUG(dbgs() << "Total states: " << numStates << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000548
549 OS << "namespace llvm {\n";
550
551 OS << "\n// Input format:\n";
552 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
553 << "\t// maximum AND'ed resource terms\n";
554 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
555 << "\t// maximum resource bits in one term\n";
556
557 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
558 << "pairs of <Input, NextState> for all valid\n";
559 OS << "// transitions.\n";
560 OS << "// " << numStates << "\tstates\n";
561 OS << "// " << numInsnClasses << "\tinstruction classes\n";
562 OS << "// " << maxResources << "\tresources max\n";
563 OS << "// " << numCombos << "\tcombo resources\n";
564 OS << "// " << maxStages << "\tstages max\n";
565 OS << "const " << DFA_TBLTYPE << " "
566 << TargetName << "DFAStateInputTable[][2] = {\n";
567
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000568 // This table provides a map to the beginning of the transitions for State s
Sebastian Popf6f77e92011-12-06 17:34:11 +0000569 // in DFAStateInputTable.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000570 std::vector<int> StateEntry(numStates+1);
571 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000572
573 // Tracks the total valid transitions encountered so far. It is used
Sebastian Popf6f77e92011-12-06 17:34:11 +0000574 // to construct the StateEntry table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000575 int ValidTransitions = 0;
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000576 DFA::StateSet::iterator SI = states.begin();
577 for (unsigned i = 0; i < numStates; ++i, ++SI) {
David Blaikiec37ae282014-04-21 22:35:11 +0000578 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000579 StateEntry[i] = ValidTransitions;
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000580 for (State::TransitionMap::iterator
David Blaikiec37ae282014-04-21 22:35:11 +0000581 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000582 II != IE; ++II) {
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000583 OS << "{0x" << Twine::utohexstr(getDFAInsnInput(II->first)) << ", "
584 << II->second->stateNum << "},\t";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000585 }
David Blaikiec37ae282014-04-21 22:35:11 +0000586 ValidTransitions += SI->Transitions.size();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000587
Sebastian Popf6f77e92011-12-06 17:34:11 +0000588 // If there are no valid transitions from this stage, we need a sentinel
589 // transition.
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000590 if (ValidTransitions == StateEntry[i]) {
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000591 OS << SentinelEntry << ",\t";
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000592 ++ValidTransitions;
593 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000594
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000595 OS << " // state " << i << ": " << StateEntry[i];
596 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
597 OS << "-" << (ValidTransitions-1);
598 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000599 OS << "\n";
600 }
Anshuman Dasgupta079e0812012-12-10 22:45:57 +0000601
602 // Print out a sentinel entry at the end of the StateInputTable. This is
603 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000604 OS << SentinelEntry << "\t";
605 OS << " // state " << numStates << ": " << ValidTransitions;
606 OS << "\n";
607
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000608 OS << "};\n\n";
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000609 OS << "// " << TargetName << "DFAStateEntryTable[i] = "
610 << "Index of the first entry in DFAStateInputTable for\n";
611 OS << "// "
612 << "the ith state.\n";
613 OS << "// " << numStates << " states\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000614 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
615
616 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Popf6f77e92011-12-06 17:34:11 +0000617 // two numbers.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000618 unsigned lastState = 0;
619 for (unsigned i = 0; i < numStates; ++i) {
620 if (i && ((i % 10) == 0)) {
621 lastState = i-1;
622 OS << " // states " << (i-10) << ":" << lastState << "\n";
623 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000624 OS << StateEntry[i] << ", ";
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000625 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000626
Anshuman Dasgupta079e0812012-12-10 22:45:57 +0000627 // Print out the index to the sentinel entry in StateInputTable
628 OS << ValidTransitions << ", ";
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000629 OS << " // states " << (lastState+1) << ":" << numStates << "\n";
Anshuman Dasgupta079e0812012-12-10 22:45:57 +0000630
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000631 OS << "};\n";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000632 OS << "} // namespace\n";
633
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000634 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000635 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000636 //
637 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
638 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
639 OS << "namespace llvm {\n";
Sebastian Pop464f3a32011-12-06 17:34:16 +0000640 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000641 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
642 << " return new DFAPacketizer(IID, " << TargetName
643 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
644 OS << "} // End llvm namespace \n";
645}
646
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000647//
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000648// collectAllFuncUnits - Construct a map of function unit names to bits.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000649//
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000650int DFAPacketizerEmitter::collectAllFuncUnits(
651 std::vector<Record*> &ProcItinList,
652 std::map<std::string, unsigned> &FUNameToBitsMap,
653 int &maxFUs,
654 raw_ostream &OS) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000655 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
656 "----------------------\n");
657 LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
658 LLVM_DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000659
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000660 int totalFUs = 0;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000661 // Parse functional units for all the itineraries.
662 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
663 Record *Proc = ProcItinList[i];
664 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
665
Nicola Zaghen0818e782018-05-14 12:53:11 +0000666 LLVM_DEBUG(dbgs() << " FU:" << i << " (" << FUs.size() << " FUs) "
667 << Proc->getName());
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000668
Sebastian Popf6f77e92011-12-06 17:34:11 +0000669 // Convert macros to bits for each stage.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000670 unsigned numFUs = FUs.size();
671 for (unsigned j = 0; j < numFUs; ++j) {
672 assert ((j < DFA_MAX_RESOURCES) &&
673 "Exceeded maximum number of representable resources");
674 unsigned FuncResources = (unsigned) (1U << j);
675 FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000676 LLVM_DEBUG(dbgs() << " " << FUs[j]->getName() << ":0x"
677 << Twine::utohexstr(FuncResources));
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000678 }
679 if (((int) numFUs) > maxFUs) {
680 maxFUs = numFUs;
681 }
682 totalFUs += numFUs;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000683 LLVM_DEBUG(dbgs() << "\n");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000684 }
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000685 return totalFUs;
686}
687
688//
689// collectAllComboFuncs - Construct a map from a combo function unit bit to
690// the bits of all included functional units.
691//
692int DFAPacketizerEmitter::collectAllComboFuncs(
693 std::vector<Record*> &ComboFuncList,
694 std::map<std::string, unsigned> &FUNameToBitsMap,
695 std::map<unsigned, unsigned> &ComboBitToBitsMap,
696 raw_ostream &OS) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000697 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
698 "----------------------\n");
699 LLVM_DEBUG(dbgs() << "collectAllComboFuncs");
700 LLVM_DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000701
702 int numCombos = 0;
703 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
704 Record *Func = ComboFuncList[i];
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000705 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
706
Nicola Zaghen0818e782018-05-14 12:53:11 +0000707 LLVM_DEBUG(dbgs() << " CFD:" << i << " (" << FUs.size() << " combo FUs) "
708 << Func->getName() << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000709
710 // Convert macros to bits for each stage.
711 for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
712 assert ((j < DFA_MAX_RESOURCES) &&
713 "Exceeded maximum number of DFA resources");
714 Record *FuncData = FUs[j];
715 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
716 const std::vector<Record*> &FuncList =
717 FuncData->getValueAsListOfDefs("FuncList");
Benjamin Kramer13c42d22016-06-12 17:30:47 +0000718 const std::string &ComboFuncName = ComboFunc->getName();
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000719 unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
720 unsigned ComboResources = ComboBit;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000721 LLVM_DEBUG(dbgs() << " combo: " << ComboFuncName << ":0x"
722 << Twine::utohexstr(ComboResources) << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000723 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
724 std::string FuncName = FuncList[k]->getName();
725 unsigned FuncResources = FUNameToBitsMap[FuncName];
Nicola Zaghen0818e782018-05-14 12:53:11 +0000726 LLVM_DEBUG(dbgs() << " " << FuncName << ":0x"
727 << Twine::utohexstr(FuncResources) << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000728 ComboResources |= FuncResources;
729 }
730 ComboBitToBitsMap[ComboBit] = ComboResources;
731 numCombos++;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000732 LLVM_DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
733 << Twine::utohexstr(ComboBit) << " = 0x"
734 << Twine::utohexstr(ComboResources) << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000735 }
736 }
737 return numCombos;
738}
739
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000740//
741// collectOneInsnClass - Populate allInsnClasses with one instruction class
742//
743int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
744 std::vector<Record*> &ProcItinList,
745 std::map<std::string, unsigned> &FUNameToBitsMap,
746 Record *ItinData,
747 raw_ostream &OS) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000748 const std::vector<Record*> &StageList =
749 ItinData->getValueAsListOfDefs("Stages");
750
Sebastian Popf6f77e92011-12-06 17:34:11 +0000751 // The number of stages.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000752 unsigned NStages = StageList.size();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000753
Nicola Zaghen0818e782018-05-14 12:53:11 +0000754 LLVM_DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName()
755 << "\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000756
757 std::vector<unsigned> UnitBits;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000758
Sebastian Popf6f77e92011-12-06 17:34:11 +0000759 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000760 for (unsigned i = 0; i < NStages; ++i) {
761 const Record *Stage = StageList[i];
762
Sebastian Popf6f77e92011-12-06 17:34:11 +0000763 // Get unit list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000764 const std::vector<Record*> &UnitList =
765 Stage->getValueAsListOfDefs("Units");
766
Nicola Zaghen0818e782018-05-14 12:53:11 +0000767 LLVM_DEBUG(dbgs() << " stage:" << i << " [" << UnitList.size()
768 << " units]:");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000769 unsigned dbglen = 26; // cursor after stage dbgs
770
771 // Compute the bitwise or of each unit used in this stage.
772 unsigned UnitBitValue = 0;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000773 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000774 // Conduct bitwise or.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000775 std::string UnitName = UnitList[j]->getName();
Nicola Zaghen0818e782018-05-14 12:53:11 +0000776 LLVM_DEBUG(dbgs() << " " << j << ":" << UnitName);
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000777 dbglen += 3 + UnitName.length();
778 assert(FUNameToBitsMap.count(UnitName));
779 UnitBitValue |= FUNameToBitsMap[UnitName];
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000780 }
781
782 if (UnitBitValue != 0)
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000783 UnitBits.push_back(UnitBitValue);
784
785 while (dbglen <= 64) { // line up bits dbgs
786 dbglen += 8;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000787 LLVM_DEBUG(dbgs() << "\t");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000788 }
Nicola Zaghen0818e782018-05-14 12:53:11 +0000789 LLVM_DEBUG(dbgs() << " (bits: 0x" << Twine::utohexstr(UnitBitValue)
790 << ")\n");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000791 }
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000792
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000793 if (!UnitBits.empty())
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000794 allInsnClasses.push_back(UnitBits);
795
Nicola Zaghen0818e782018-05-14 12:53:11 +0000796 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000797 dbgs() << " ";
798 dbgsInsnClass(UnitBits);
799 dbgs() << "\n";
800 });
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000801
802 return NStages;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000803}
804
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000805//
806// collectAllInsnClasses - Populate allInsnClasses which is a set of units
807// used in each stage.
808//
809int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
810 std::vector<Record*> &ProcItinList,
811 std::map<std::string, unsigned> &FUNameToBitsMap,
812 std::vector<Record*> &ItinDataList,
813 int &maxStages,
814 raw_ostream &OS) {
815 // Collect all instruction classes.
816 unsigned M = ItinDataList.size();
817
818 int numInsnClasses = 0;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000819 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
820 "----------------------\n"
821 << "collectAllInsnClasses " << ProcName << " (" << M
822 << " classes)\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000823
824 // Collect stages for each instruction class for all itinerary data
825 for (unsigned j = 0; j < M; j++) {
826 Record *ItinData = ItinDataList[j];
827 int NStages = collectOneInsnClass(ProcName, ProcItinList,
828 FUNameToBitsMap, ItinData, OS);
829 if (NStages > maxStages) {
830 maxStages = NStages;
831 }
832 numInsnClasses++;
833 }
834 return numInsnClasses;
835}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000836
837//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000838// Run the worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000839//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000840void DFAPacketizerEmitter::run(raw_ostream &OS) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000841 // Collect processor iteraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000842 std::vector<Record*> ProcItinList =
843 Records.getAllDerivedDefinitions("ProcessorItineraries");
844
845 //
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000846 // Collect the Functional units.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000847 //
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000848 std::map<std::string, unsigned> FUNameToBitsMap;
849 int maxResources = 0;
850 collectAllFuncUnits(ProcItinList,
851 FUNameToBitsMap, maxResources, OS);
852
853 //
854 // Collect the Combo Functional units.
855 //
856 std::map<unsigned, unsigned> ComboBitToBitsMap;
857 std::vector<Record*> ComboFuncList =
858 Records.getAllDerivedDefinitions("ComboFuncUnits");
859 int numCombos = collectAllComboFuncs(ComboFuncList,
860 FUNameToBitsMap, ComboBitToBitsMap, OS);
861
862 //
863 // Collect the itineraries.
864 //
865 int maxStages = 0;
866 int numInsnClasses = 0;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000867 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
868 Record *Proc = ProcItinList[i];
869
Sebastian Popf6f77e92011-12-06 17:34:11 +0000870 // Get processor itinerary name.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000871 const std::string &ProcName = Proc->getName();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000872
Sebastian Popf6f77e92011-12-06 17:34:11 +0000873 // Skip default.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000874 if (ProcName == "NoItineraries")
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000875 continue;
876
Sebastian Popf6f77e92011-12-06 17:34:11 +0000877 // Sanity check for at least one instruction itinerary class.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000878 unsigned NItinClasses =
879 Records.getAllDerivedDefinitions("InstrItinClass").size();
880 if (NItinClasses == 0)
881 return;
882
Sebastian Popf6f77e92011-12-06 17:34:11 +0000883 // Get itinerary data list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000884 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
885
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000886 // Collect all instruction classes
887 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
888 FUNameToBitsMap, ItinDataList, maxStages, OS);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000889 }
890
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000891 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000892 // Run a worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000893 //
894 DFA D;
David Blaikiec37ae282014-04-21 22:35:11 +0000895 const State *Initial = &D.newState();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000896 Initial->isInitial = true;
897 Initial->stateInfo.insert(0x0);
David Blaikiec37ae282014-04-21 22:35:11 +0000898 SmallVector<const State*, 32> WorkList;
899 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000900
901 WorkList.push_back(Initial);
902
903 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000904 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000905 // C = {set of InsnClasses}
906 // Begin with initial node in worklist. Initial node does not have
907 // any consumed resources,
908 // ResourceState = 0x0
909 // Visited = {}
910 // While worklist != empty
911 // S = first element of worklist
912 // For every instruction class C
913 // if we can accommodate C in S:
914 // S' = state with resource states = {S Union C}
915 // Add a new transition: S x C -> S'
916 // If S' is not in Visited:
917 // Add S' to worklist
918 // Add S' to Visited
919 //
920 while (!WorkList.empty()) {
David Blaikiec37ae282014-04-21 22:35:11 +0000921 const State *current = WorkList.pop_back_val();
Nicola Zaghen0818e782018-05-14 12:53:11 +0000922 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000923 dbgs() << "---------------------\n";
924 dbgs() << "Processing state: " << current->stateNum << " - ";
925 dbgsStateInfo(current->stateInfo);
926 dbgs() << "\n";
927 });
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000928 for (unsigned i = 0; i < allInsnClasses.size(); i++) {
929 std::vector<unsigned> InsnClass = allInsnClasses[i];
Nicola Zaghen0818e782018-05-14 12:53:11 +0000930 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000931 dbgs() << i << " ";
932 dbgsInsnClass(InsnClass);
933 dbgs() << "\n";
934 });
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000935
936 std::set<unsigned> NewStateResources;
937 //
938 // If we haven't already created a transition for this input
Sebastian Popf6f77e92011-12-06 17:34:11 +0000939 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000940 //
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000941 if (!current->hasTransition(InsnClass) &&
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000942 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
Eugene Zelenko51ecde12016-01-26 18:48:36 +0000943 const State *NewState = nullptr;
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000944 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000945 if (NewStateResources.empty()) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000946 LLVM_DEBUG(dbgs() << " Skipped - no new states generated\n");
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000947 continue;
948 }
949
Nicola Zaghen0818e782018-05-14 12:53:11 +0000950 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000951 dbgs() << "\t";
952 dbgsStateInfo(NewStateResources);
953 dbgs() << "\n";
954 });
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000955
956 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000957 // If we have seen this state before, then do not create a new state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000958 //
David Blaikiec37ae282014-04-21 22:35:11 +0000959 auto VI = Visited.find(NewStateResources);
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000960 if (VI != Visited.end()) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000961 NewState = VI->second;
Nicola Zaghen0818e782018-05-14 12:53:11 +0000962 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000963 dbgs() << "\tFound existing state: " << NewState->stateNum
964 << " - ";
965 dbgsStateInfo(NewState->stateInfo);
966 dbgs() << "\n";
967 });
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000968 } else {
David Blaikiec37ae282014-04-21 22:35:11 +0000969 NewState = &D.newState();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000970 NewState->stateInfo = NewStateResources;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000971 Visited[NewStateResources] = NewState;
972 WorkList.push_back(NewState);
Nicola Zaghen0818e782018-05-14 12:53:11 +0000973 LLVM_DEBUG({
Krzysztof Parzyszekb08711c2015-11-21 22:46:52 +0000974 dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
975 dbgsStateInfo(NewState->stateInfo);
976 dbgs() << "\n";
977 });
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000978 }
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000979
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000980 current->addTransition(InsnClass, NewState);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000981 }
982 }
983 }
984
Sebastian Popf6f77e92011-12-06 17:34:11 +0000985 // Print out the table.
Krzysztof Parzyszekbf390b02015-11-21 20:00:45 +0000986 D.writeTableAndAPI(OS, TargetName,
987 numInsnClasses, maxResources, numCombos, maxStages);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000988}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000989
990namespace llvm {
991
992void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
993 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
994 DFAPacketizerEmitter(RK).run(OS);
995}
996
Eugene Zelenkoc02caf52016-11-30 17:48:10 +0000997} // end namespace llvm