blob: 5060b6e9ce7c0b059fac364b563fa3b1a849ef68 [file] [log] [blame]
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00001//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine-----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class parses the Schedule.td file and produces an API that can be used
11// to reason about whether an instruction can be added to a packet on a VLIW
12// architecture. The class internally generates a deterministic finite
13// automaton (DFA) that models all possible mappings of machine instructions
14// to functional units as instructions are added to a packet.
15//
16//===----------------------------------------------------------------------===//
17
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000018#include "CodeGenTarget.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000019#include "llvm/ADT/DenseSet.h"
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +000020#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000021#include "llvm/TableGen/Record.h"
22#include "llvm/TableGen/TableGenBackend.h"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000023#include <list>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000024#include <map>
25#include <string>
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000026using namespace llvm;
27
28//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000029// class DFAPacketizerEmitter: class that generates and prints out the DFA
30// for resource tracking.
31//
32namespace {
33class DFAPacketizerEmitter {
34private:
35 std::string TargetName;
36 //
37 // allInsnClasses is the set of all possible resources consumed by an
38 // InstrStage.
39 //
40 DenseSet<unsigned> allInsnClasses;
41 RecordKeeper &Records;
42
43public:
44 DFAPacketizerEmitter(RecordKeeper &R);
45
46 //
47 // collectAllInsnClasses: Populate allInsnClasses which is a set of units
48 // used in each stage.
49 //
50 void collectAllInsnClasses(const std::string &Name,
51 Record *ItinData,
52 unsigned &NStages,
53 raw_ostream &OS);
54
55 void run(raw_ostream &OS);
56};
57} // End anonymous namespace.
58
59//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000060//
61// State represents the usage of machine resources if the packet contains
62// a set of instruction classes.
63//
Sebastian Pop9aa61372011-12-06 17:34:11 +000064// Specifically, currentState is a set of bit-masks.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000065// The nth bit in a bit-mask indicates whether the nth resource is being used
66// by this state. The set of bit-masks in a state represent the different
67// possible outcomes of transitioning to this state.
Sebastian Pop9aa61372011-12-06 17:34:11 +000068// For example: consider a two resource architecture: resource L and resource M
69// with three instruction classes: L, M, and L_or_M.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000070// From the initial state (currentState = 0x00), if we add instruction class
71// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
72// represents the possible resource states that can result from adding a L_or_M
73// instruction
74//
75// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Pop9aa61372011-12-06 17:34:11 +000076// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000077//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +000078// A State instance also contains a collection of transitions from that state:
79// a map from inputs to new states.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000080//
81namespace {
82class State {
83 public:
84 static int currentStateNum;
David Blaikied43046b2014-04-21 22:35:11 +000085 // stateNum is the only member used for equality/ordering, all other members
86 // can be mutated even in const State objects.
87 const int stateNum;
88 mutable bool isInitial;
89 mutable std::set<unsigned> stateInfo;
90 typedef std::map<unsigned, const State *> TransitionMap;
91 mutable TransitionMap Transitions;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000092
93 State();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000094
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +000095 bool operator<(const State &s) const {
96 return stateNum < s.stateNum;
97 }
98
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000099 //
100 // canAddInsnClass - Returns true if an instruction of type InsnClass is a
Sebastian Pop9aa61372011-12-06 17:34:11 +0000101 // valid transition from this state, i.e., can an instruction of type InsnClass
102 // be added to the packet represented by this state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000103 //
104 // PossibleStates is the set of valid resource states that ensue from valid
Sebastian Pop9aa61372011-12-06 17:34:11 +0000105 // transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000106 //
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000107 bool canAddInsnClass(unsigned InsnClass) const;
108 //
109 // AddInsnClass - Return all combinations of resource reservation
110 // which are possible from this state (PossibleStates).
111 //
David Blaikied43046b2014-04-21 22:35:11 +0000112 void AddInsnClass(unsigned InsnClass, std::set<unsigned> &PossibleStates) const;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000113 //
114 // addTransition - Add a transition from this state given the input InsnClass
115 //
David Blaikied43046b2014-04-21 22:35:11 +0000116 void addTransition(unsigned InsnClass, const State *To) const;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000117 //
118 // hasTransition - Returns true if there is a transition from this state
119 // given the input InsnClass
120 //
David Blaikied43046b2014-04-21 22:35:11 +0000121 bool hasTransition(unsigned InsnClass) const;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000122};
Sebastian Pop9aa61372011-12-06 17:34:11 +0000123} // End anonymous namespace.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000124
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000125//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000126// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000127//
128namespace {
129class DFA {
130public:
131 DFA();
132
Sebastian Pop9aa61372011-12-06 17:34:11 +0000133 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikied43046b2014-04-21 22:35:11 +0000134 typedef std::set<State> StateSet;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000135 StateSet states;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000136
Sebastian Popac35a4d2011-12-06 17:34:16 +0000137 State *currentState;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000138
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000139 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000140 // Modify the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000141 //
David Blaikied43046b2014-04-21 22:35:11 +0000142 const State &newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000143
144 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000145 // writeTable: Print out a table representing the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000146 //
Sebastian Popac35a4d2011-12-06 17:34:16 +0000147 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000148};
Sebastian Pop9aa61372011-12-06 17:34:11 +0000149} // End anonymous namespace.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000150
151
152//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000153// Constructors and destructors for State and DFA
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000154//
155State::State() :
156 stateNum(currentStateNum++), isInitial(false) {}
157
Craig Topper24064772014-04-15 07:20:03 +0000158DFA::DFA(): currentState(nullptr) {}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000159
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000160//
161// addTransition - Add a transition from this state given the input InsnClass
162//
David Blaikied43046b2014-04-21 22:35:11 +0000163void State::addTransition(unsigned InsnClass, const State *To) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000164 assert(!Transitions.count(InsnClass) &&
165 "Cannot have multiple transitions for the same input");
166 Transitions[InsnClass] = To;
167}
168
169//
170// hasTransition - Returns true if there is a transition from this state
171// given the input InsnClass
172//
David Blaikied43046b2014-04-21 22:35:11 +0000173bool State::hasTransition(unsigned InsnClass) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000174 return Transitions.count(InsnClass) > 0;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000175}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000176
177//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000178// AddInsnClass - Return all combinations of resource reservation
179// which are possible from this state (PossibleStates).
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000180//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000181void State::AddInsnClass(unsigned InsnClass,
David Blaikied43046b2014-04-21 22:35:11 +0000182 std::set<unsigned> &PossibleStates) const {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000183 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000184 // Iterate over all resource states in currentState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000185 //
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000186
187 for (std::set<unsigned>::iterator SI = stateInfo.begin();
188 SI != stateInfo.end(); ++SI) {
189 unsigned thisState = *SI;
190
191 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000192 // Iterate over all possible resources used in InsnClass.
193 // For ex: for InsnClass = 0x11, all resources = {0x01, 0x10}.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000194 //
195
196 DenseSet<unsigned> VisitedResourceStates;
197 for (unsigned int j = 0; j < sizeof(InsnClass) * 8; ++j) {
198 if ((0x1 << j) & InsnClass) {
199 //
200 // For each possible resource used in InsnClass, generate the
Sebastian Pop9aa61372011-12-06 17:34:11 +0000201 // resource state if that resource was used.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000202 //
203 unsigned ResultingResourceState = thisState | (0x1 << j);
204 //
205 // Check if the resulting resource state can be accommodated in this
Sebastian Pop9aa61372011-12-06 17:34:11 +0000206 // packet.
207 // We compute ResultingResourceState OR thisState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000208 // If the result of the OR is different than thisState, it implies
209 // that there is at least one resource that can be used to schedule
Sebastian Pop9aa61372011-12-06 17:34:11 +0000210 // InsnClass in the current packet.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000211 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Pop9aa61372011-12-06 17:34:11 +0000212 // processed ResultingResourceState before.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000213 //
214 if ((ResultingResourceState != thisState) &&
215 (VisitedResourceStates.count(ResultingResourceState) == 0)) {
216 VisitedResourceStates.insert(ResultingResourceState);
217 PossibleStates.insert(ResultingResourceState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000218 }
219 }
220 }
221 }
222
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000223}
224
225
226//
227// canAddInsnClass - Quickly verifies if an instruction of type InsnClass is a
228// valid transition from this state i.e., can an instruction of type InsnClass
229// be added to the packet represented by this state.
230//
231bool State::canAddInsnClass(unsigned InsnClass) const {
Alexey Samsonov420a4ed2012-06-28 07:47:50 +0000232 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000233 SI != stateInfo.end(); ++SI) {
234 if (~*SI & InsnClass)
235 return true;
236 }
237 return false;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000238}
239
240
David Blaikied43046b2014-04-21 22:35:11 +0000241const State &DFA::newState() {
David Blaikie4bcfcc12014-04-21 22:46:09 +0000242 auto IterPair = states.insert(State());
David Blaikied43046b2014-04-21 22:35:11 +0000243 assert(IterPair.second && "State already exists");
244 return *IterPair.first;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000245}
246
247
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000248int State::currentStateNum = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000249
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000250DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000251 TargetName(CodeGenTarget(R).getName()),
252 allInsnClasses(), Records(R) {}
253
254
255//
256// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Pop9aa61372011-12-06 17:34:11 +0000257// associated API to create a DFA packetizer.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000258//
259// Format:
260// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Pop9aa61372011-12-06 17:34:11 +0000261// transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000262// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Pop9aa61372011-12-06 17:34:11 +0000263// the ith state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000264//
265//
Sebastian Popac35a4d2011-12-06 17:34:16 +0000266void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) {
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000267 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000268 DFA::StateSet::iterator SI = states.begin();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000269 // This table provides a map to the beginning of the transitions for State s
Sebastian Pop9aa61372011-12-06 17:34:11 +0000270 // in DFAStateInputTable.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000271 std::vector<int> StateEntry(states.size());
272
273 OS << "namespace llvm {\n\n";
274 OS << "const int " << TargetName << "DFAStateInputTable[][2] = {\n";
275
276 // Tracks the total valid transitions encountered so far. It is used
Sebastian Pop9aa61372011-12-06 17:34:11 +0000277 // to construct the StateEntry table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000278 int ValidTransitions = 0;
279 for (unsigned i = 0; i < states.size(); ++i, ++SI) {
David Blaikied43046b2014-04-21 22:35:11 +0000280 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000281 StateEntry[i] = ValidTransitions;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000282 for (State::TransitionMap::iterator
David Blaikied43046b2014-04-21 22:35:11 +0000283 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000284 II != IE; ++II) {
285 OS << "{" << II->first << ", "
286 << II->second->stateNum
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000287 << "}, ";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000288 }
David Blaikied43046b2014-04-21 22:35:11 +0000289 ValidTransitions += SI->Transitions.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000290
Sebastian Pop9aa61372011-12-06 17:34:11 +0000291 // If there are no valid transitions from this stage, we need a sentinel
292 // transition.
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000293 if (ValidTransitions == StateEntry[i]) {
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000294 OS << SentinelEntry << ",";
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000295 ++ValidTransitions;
296 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000297
298 OS << "\n";
299 }
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000300
301 // Print out a sentinel entry at the end of the StateInputTable. This is
302 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
303 OS << SentinelEntry << "\n";
304
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000305 OS << "};\n\n";
306 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
307
308 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Pop9aa61372011-12-06 17:34:11 +0000309 // two numbers.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000310 for (unsigned i = 0; i < states.size(); ++i)
311 OS << StateEntry[i] << ", ";
312
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000313 // Print out the index to the sentinel entry in StateInputTable
314 OS << ValidTransitions << ", ";
315
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000316 OS << "\n};\n";
317 OS << "} // namespace\n";
318
319
320 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000321 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000322 //
323 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
324 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
325 OS << "namespace llvm {\n";
Sebastian Popac35a4d2011-12-06 17:34:16 +0000326 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000327 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
328 << " return new DFAPacketizer(IID, " << TargetName
329 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
330 OS << "} // End llvm namespace \n";
331}
332
333
334//
335// collectAllInsnClasses - Populate allInsnClasses which is a set of units
336// used in each stage.
337//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000338void DFAPacketizerEmitter::collectAllInsnClasses(const std::string &Name,
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000339 Record *ItinData,
340 unsigned &NStages,
341 raw_ostream &OS) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000342 // Collect processor itineraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000343 std::vector<Record*> ProcItinList =
Sebastian Pop9aa61372011-12-06 17:34:11 +0000344 Records.getAllDerivedDefinitions("ProcessorItineraries");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000345
Sebastian Pop9aa61372011-12-06 17:34:11 +0000346 // If just no itinerary then don't bother.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000347 if (ProcItinList.size() < 2)
348 return;
349 std::map<std::string, unsigned> NameToBitsMap;
350
351 // Parse functional units for all the itineraries.
352 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
353 Record *Proc = ProcItinList[i];
354 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
355
Sebastian Pop9aa61372011-12-06 17:34:11 +0000356 // Convert macros to bits for each stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000357 for (unsigned i = 0, N = FUs.size(); i < N; ++i)
358 NameToBitsMap[FUs[i]->getName()] = (unsigned) (1U << i);
359 }
360
361 const std::vector<Record*> &StageList =
362 ItinData->getValueAsListOfDefs("Stages");
363
Sebastian Pop9aa61372011-12-06 17:34:11 +0000364 // The number of stages.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000365 NStages = StageList.size();
366
Sebastian Pop9aa61372011-12-06 17:34:11 +0000367 // For each unit.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000368 unsigned UnitBitValue = 0;
369
Sebastian Pop9aa61372011-12-06 17:34:11 +0000370 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000371 for (unsigned i = 0; i < NStages; ++i) {
372 const Record *Stage = StageList[i];
373
Sebastian Pop9aa61372011-12-06 17:34:11 +0000374 // Get unit list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000375 const std::vector<Record*> &UnitList =
376 Stage->getValueAsListOfDefs("Units");
377
378 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000379 // Conduct bitwise or.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000380 std::string UnitName = UnitList[j]->getName();
381 assert(NameToBitsMap.count(UnitName));
382 UnitBitValue |= NameToBitsMap[UnitName];
383 }
384
385 if (UnitBitValue != 0)
386 allInsnClasses.insert(UnitBitValue);
387 }
388}
389
390
391//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000392// Run the worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000393//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000394void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000395
Sebastian Pop9aa61372011-12-06 17:34:11 +0000396 // Collect processor iteraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000397 std::vector<Record*> ProcItinList =
398 Records.getAllDerivedDefinitions("ProcessorItineraries");
399
400 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000401 // Collect the instruction classes.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000402 //
403 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
404 Record *Proc = ProcItinList[i];
405
Sebastian Pop9aa61372011-12-06 17:34:11 +0000406 // Get processor itinerary name.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000407 const std::string &Name = Proc->getName();
408
Sebastian Pop9aa61372011-12-06 17:34:11 +0000409 // Skip default.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000410 if (Name == "NoItineraries")
411 continue;
412
Sebastian Pop9aa61372011-12-06 17:34:11 +0000413 // Sanity check for at least one instruction itinerary class.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000414 unsigned NItinClasses =
415 Records.getAllDerivedDefinitions("InstrItinClass").size();
416 if (NItinClasses == 0)
417 return;
418
Sebastian Pop9aa61372011-12-06 17:34:11 +0000419 // Get itinerary data list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000420 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
421
Sebastian Pop9aa61372011-12-06 17:34:11 +0000422 // Collect instruction classes for all itinerary data.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000423 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
424 Record *ItinData = ItinDataList[j];
425 unsigned NStages;
426 collectAllInsnClasses(Name, ItinData, NStages, OS);
427 }
428 }
429
430
431 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000432 // Run a worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000433 //
434 DFA D;
David Blaikied43046b2014-04-21 22:35:11 +0000435 const State *Initial = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000436 Initial->isInitial = true;
437 Initial->stateInfo.insert(0x0);
David Blaikied43046b2014-04-21 22:35:11 +0000438 SmallVector<const State*, 32> WorkList;
439 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000440
441 WorkList.push_back(Initial);
442
443 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000444 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000445 // C = {set of InsnClasses}
446 // Begin with initial node in worklist. Initial node does not have
447 // any consumed resources,
448 // ResourceState = 0x0
449 // Visited = {}
450 // While worklist != empty
451 // S = first element of worklist
452 // For every instruction class C
453 // if we can accommodate C in S:
454 // S' = state with resource states = {S Union C}
455 // Add a new transition: S x C -> S'
456 // If S' is not in Visited:
457 // Add S' to worklist
458 // Add S' to Visited
459 //
460 while (!WorkList.empty()) {
David Blaikied43046b2014-04-21 22:35:11 +0000461 const State *current = WorkList.pop_back_val();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000462 for (DenseSet<unsigned>::iterator CI = allInsnClasses.begin(),
463 CE = allInsnClasses.end(); CI != CE; ++CI) {
464 unsigned InsnClass = *CI;
465
466 std::set<unsigned> NewStateResources;
467 //
468 // If we haven't already created a transition for this input
Sebastian Pop9aa61372011-12-06 17:34:11 +0000469 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000470 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000471 if (!current->hasTransition(InsnClass) &&
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000472 current->canAddInsnClass(InsnClass)) {
David Blaikied43046b2014-04-21 22:35:11 +0000473 const State *NewState;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000474 current->AddInsnClass(InsnClass, NewStateResources);
Alexander Kornienko8c0809c2015-01-15 11:41:30 +0000475 assert(!NewStateResources.empty() && "New states must be generated");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000476
477 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000478 // If we have seen this state before, then do not create a new state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000479 //
480 //
David Blaikied43046b2014-04-21 22:35:11 +0000481 auto VI = Visited.find(NewStateResources);
482 if (VI != Visited.end())
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000483 NewState = VI->second;
484 else {
David Blaikied43046b2014-04-21 22:35:11 +0000485 NewState = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000486 NewState->stateInfo = NewStateResources;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000487 Visited[NewStateResources] = NewState;
488 WorkList.push_back(NewState);
489 }
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000490
491 current->addTransition(InsnClass, NewState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000492 }
493 }
494 }
495
Sebastian Pop9aa61372011-12-06 17:34:11 +0000496 // Print out the table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000497 D.writeTableAndAPI(OS, TargetName);
498}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000499
500namespace llvm {
501
502void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
503 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
504 DFAPacketizerEmitter(RK).run(OS);
505}
506
507} // End llvm namespace