blob: 5721121dd73f093cab5f8fbf3cdc4722964b7ced [file] [log] [blame]
Anshuman Dasguptadc81e5d2011-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
18#include "llvm/MC/MCInstrDesc.h"
19#include "llvm/MC/MCInstrItineraries.h"
20#include "llvm/TableGen/Record.h"
21#include "CodeGenTarget.h"
22#include "DFAPacketizerEmitter.h"
23#include <list>
24
25using namespace llvm;
26
27//
28//
29// State represents the usage of machine resources if the packet contains
30// a set of instruction classes.
31//
Sebastian Popf6f77e92011-12-06 17:34:11 +000032// Specifically, currentState is a set of bit-masks.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000033// The nth bit in a bit-mask indicates whether the nth resource is being used
34// by this state. The set of bit-masks in a state represent the different
35// possible outcomes of transitioning to this state.
Sebastian Popf6f77e92011-12-06 17:34:11 +000036// For example: consider a two resource architecture: resource L and resource M
37// with three instruction classes: L, M, and L_or_M.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000038// From the initial state (currentState = 0x00), if we add instruction class
39// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
40// represents the possible resource states that can result from adding a L_or_M
41// instruction
42//
43// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Popf6f77e92011-12-06 17:34:11 +000044// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000045//
46//
47namespace {
48class State {
49 public:
50 static int currentStateNum;
51 int stateNum;
52 bool isInitial;
53 std::set<unsigned> stateInfo;
54
55 State();
Sebastian Pop464f3a32011-12-06 17:34:16 +000056 State(const State &S);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000057
58 //
59 // canAddInsnClass - Returns true if an instruction of type InsnClass is a
Sebastian Popf6f77e92011-12-06 17:34:11 +000060 // valid transition from this state, i.e., can an instruction of type InsnClass
61 // be added to the packet represented by this state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000062 //
63 // PossibleStates is the set of valid resource states that ensue from valid
Sebastian Popf6f77e92011-12-06 17:34:11 +000064 // transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000065 //
Sebastian Pop464f3a32011-12-06 17:34:16 +000066 bool canAddInsnClass(unsigned InsnClass, std::set<unsigned> &PossibleStates);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000067};
Sebastian Popf6f77e92011-12-06 17:34:11 +000068} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000069
70
71namespace {
72struct Transition {
73 public:
74 static int currentTransitionNum;
75 int transitionNum;
Sebastian Pop464f3a32011-12-06 17:34:16 +000076 State *from;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000077 unsigned input;
Sebastian Pop464f3a32011-12-06 17:34:16 +000078 State *to;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000079
Sebastian Pop464f3a32011-12-06 17:34:16 +000080 Transition(State *from_, unsigned input_, State *to_);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000081};
Sebastian Popf6f77e92011-12-06 17:34:11 +000082} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000083
84
85//
Sebastian Popf6f77e92011-12-06 17:34:11 +000086// Comparators to keep set of states sorted.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000087//
88namespace {
89struct ltState {
Sebastian Pop464f3a32011-12-06 17:34:16 +000090 bool operator()(const State *s1, const State *s2) const;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000091};
Sebastian Popf6f77e92011-12-06 17:34:11 +000092} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000093
94
95//
Sebastian Popf6f77e92011-12-06 17:34:11 +000096// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000097//
98namespace {
99class DFA {
100public:
101 DFA();
102
Sebastian Popf6f77e92011-12-06 17:34:11 +0000103 // Set of states. Need to keep this sorted to emit the transition table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000104 std::set<State*, ltState> states;
105
Sebastian Popf6f77e92011-12-06 17:34:11 +0000106 // Map from a state to the list of transitions with that state as source.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000107 std::map<State*, SmallVector<Transition*, 16>, ltState> stateTransitions;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000108 State *currentState;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000109
Sebastian Popf6f77e92011-12-06 17:34:11 +0000110 // Highest valued Input seen.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000111 unsigned LargestInput;
112
113 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000114 // Modify the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000115 //
116 void initialize();
Sebastian Pop464f3a32011-12-06 17:34:16 +0000117 void addState(State *);
118 void addTransition(Transition *);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000119
120 //
121 // getTransition - Return the state when a transition is made from
Sebastian Popf6f77e92011-12-06 17:34:11 +0000122 // State From with Input I. If a transition is not found, return NULL.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000123 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000124 State *getTransition(State *, unsigned);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000125
126 //
127 // isValidTransition: Predicate that checks if there is a valid transition
Sebastian Popf6f77e92011-12-06 17:34:11 +0000128 // from state From on input InsnClass.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000129 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000130 bool isValidTransition(State *From, unsigned InsnClass);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000131
132 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000133 // writeTable: Print out a table representing the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000134 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000135 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000136};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000137} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000138
139
140//
141// Constructors for State, Transition, and DFA
142//
143State::State() :
144 stateNum(currentStateNum++), isInitial(false) {}
145
146
Sebastian Pop464f3a32011-12-06 17:34:16 +0000147State::State(const State &S) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000148 stateNum(currentStateNum++), isInitial(S.isInitial),
149 stateInfo(S.stateInfo) {}
150
151
Sebastian Pop464f3a32011-12-06 17:34:16 +0000152Transition::Transition(State *from_, unsigned input_, State *to_) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000153 transitionNum(currentTransitionNum++), from(from_), input(input_),
154 to(to_) {}
155
156
157DFA::DFA() :
158 LargestInput(0) {}
159
160
Sebastian Pop464f3a32011-12-06 17:34:16 +0000161bool ltState::operator()(const State *s1, const State *s2) const {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000162 return (s1->stateNum < s2->stateNum);
163}
164
165
166//
167// canAddInsnClass - Returns true if an instruction of type InsnClass is a
168// valid transition from this state i.e., can an instruction of type InsnClass
Sebastian Popf6f77e92011-12-06 17:34:11 +0000169// be added to the packet represented by this state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000170//
171// PossibleStates is the set of valid resource states that ensue from valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000172// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000173//
174bool State::canAddInsnClass(unsigned InsnClass,
Sebastian Pop464f3a32011-12-06 17:34:16 +0000175 std::set<unsigned> &PossibleStates) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000176 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000177 // Iterate over all resource states in currentState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000178 //
179 bool AddedState = false;
180
181 for (std::set<unsigned>::iterator SI = stateInfo.begin();
182 SI != stateInfo.end(); ++SI) {
183 unsigned thisState = *SI;
184
185 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000186 // Iterate over all possible resources used in InsnClass.
187 // For ex: for InsnClass = 0x11, all resources = {0x01, 0x10}.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000188 //
189
190 DenseSet<unsigned> VisitedResourceStates;
191 for (unsigned int j = 0; j < sizeof(InsnClass) * 8; ++j) {
192 if ((0x1 << j) & InsnClass) {
193 //
194 // For each possible resource used in InsnClass, generate the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000195 // resource state if that resource was used.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000196 //
197 unsigned ResultingResourceState = thisState | (0x1 << j);
198 //
199 // Check if the resulting resource state can be accommodated in this
Sebastian Popf6f77e92011-12-06 17:34:11 +0000200 // packet.
201 // We compute ResultingResourceState OR thisState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000202 // If the result of the OR is different than thisState, it implies
203 // that there is at least one resource that can be used to schedule
Sebastian Popf6f77e92011-12-06 17:34:11 +0000204 // InsnClass in the current packet.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000205 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Popf6f77e92011-12-06 17:34:11 +0000206 // processed ResultingResourceState before.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000207 //
208 if ((ResultingResourceState != thisState) &&
209 (VisitedResourceStates.count(ResultingResourceState) == 0)) {
210 VisitedResourceStates.insert(ResultingResourceState);
211 PossibleStates.insert(ResultingResourceState);
212 AddedState = true;
213 }
214 }
215 }
216 }
217
218 return AddedState;
219}
220
221
222void DFA::initialize() {
223 currentState->isInitial = true;
224}
225
226
Sebastian Pop464f3a32011-12-06 17:34:16 +0000227void DFA::addState(State *S) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000228 assert(!states.count(S) && "State already exists");
229 states.insert(S);
230}
231
232
Sebastian Pop464f3a32011-12-06 17:34:16 +0000233void DFA::addTransition(Transition *T) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000234 // Update LargestInput.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000235 if (T->input > LargestInput)
236 LargestInput = T->input;
237
Sebastian Popf6f77e92011-12-06 17:34:11 +0000238 // Add the new transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000239 stateTransitions[T->from].push_back(T);
240}
241
242
243//
244// getTransition - Return the state when a transition is made from
Sebastian Popf6f77e92011-12-06 17:34:11 +0000245// State From with Input I. If a transition is not found, return NULL.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000246//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000247State *DFA::getTransition(State *From, unsigned I) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000248 // Do we have a transition from state From?
249 if (!stateTransitions.count(From))
250 return NULL;
251
252 // Do we have a transition from state From with Input I?
253 for (SmallVector<Transition*, 16>::iterator VI =
254 stateTransitions[From].begin();
255 VI != stateTransitions[From].end(); ++VI)
256 if ((*VI)->input == I)
257 return (*VI)->to;
258
259 return NULL;
260}
261
262
Sebastian Pop464f3a32011-12-06 17:34:16 +0000263bool DFA::isValidTransition(State *From, unsigned InsnClass) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000264 return (getTransition(From, InsnClass) != NULL);
265}
266
267
268int State::currentStateNum = 0;
269int Transition::currentTransitionNum = 0;
270
Sebastian Pop464f3a32011-12-06 17:34:16 +0000271DFAGen::DFAGen(RecordKeeper &R):
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000272 TargetName(CodeGenTarget(R).getName()),
273 allInsnClasses(), Records(R) {}
274
275
276//
277// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000278// associated API to create a DFA packetizer.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000279//
280// Format:
281// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000282// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000283// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Popf6f77e92011-12-06 17:34:11 +0000284// the ith state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000285//
286//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000287void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000288 std::set<State*, ltState>::iterator SI = states.begin();
289 // This table provides a map to the beginning of the transitions for State s
Sebastian Popf6f77e92011-12-06 17:34:11 +0000290 // in DFAStateInputTable.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000291 std::vector<int> StateEntry(states.size());
292
293 OS << "namespace llvm {\n\n";
294 OS << "const int " << TargetName << "DFAStateInputTable[][2] = {\n";
295
296 // Tracks the total valid transitions encountered so far. It is used
Sebastian Popf6f77e92011-12-06 17:34:11 +0000297 // to construct the StateEntry table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000298 int ValidTransitions = 0;
299 for (unsigned i = 0; i < states.size(); ++i, ++SI) {
300 StateEntry[i] = ValidTransitions;
301 for (unsigned j = 0; j <= LargestInput; ++j) {
302 assert (((*SI)->stateNum == (int) i) && "Mismatch in state numbers");
303 if (!isValidTransition(*SI, j))
304 continue;
305
306 OS << "{" << j << ", "
307 << getTransition(*SI, j)->stateNum
308 << "}, ";
309 ++ValidTransitions;
310 }
311
Sebastian Popf6f77e92011-12-06 17:34:11 +0000312 // If there are no valid transitions from this stage, we need a sentinel
313 // transition.
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000314 if (ValidTransitions == StateEntry[i]) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000315 OS << "{-1, -1},";
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000316 ++ValidTransitions;
317 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000318
319 OS << "\n";
320 }
321 OS << "};\n\n";
322 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
323
324 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Popf6f77e92011-12-06 17:34:11 +0000325 // two numbers.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000326 for (unsigned i = 0; i < states.size(); ++i)
327 OS << StateEntry[i] << ", ";
328
329 OS << "\n};\n";
330 OS << "} // namespace\n";
331
332
333 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000334 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000335 //
336 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
337 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
338 OS << "namespace llvm {\n";
Sebastian Pop464f3a32011-12-06 17:34:16 +0000339 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000340 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
341 << " return new DFAPacketizer(IID, " << TargetName
342 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
343 OS << "} // End llvm namespace \n";
344}
345
346
347//
348// collectAllInsnClasses - Populate allInsnClasses which is a set of units
349// used in each stage.
350//
351void DFAGen::collectAllInsnClasses(const std::string &Name,
352 Record *ItinData,
353 unsigned &NStages,
354 raw_ostream &OS) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000355 // Collect processor itineraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000356 std::vector<Record*> ProcItinList =
Sebastian Popf6f77e92011-12-06 17:34:11 +0000357 Records.getAllDerivedDefinitions("ProcessorItineraries");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000358
Sebastian Popf6f77e92011-12-06 17:34:11 +0000359 // If just no itinerary then don't bother.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000360 if (ProcItinList.size() < 2)
361 return;
362 std::map<std::string, unsigned> NameToBitsMap;
363
364 // Parse functional units for all the itineraries.
365 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
366 Record *Proc = ProcItinList[i];
367 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
368
Sebastian Popf6f77e92011-12-06 17:34:11 +0000369 // Convert macros to bits for each stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000370 for (unsigned i = 0, N = FUs.size(); i < N; ++i)
371 NameToBitsMap[FUs[i]->getName()] = (unsigned) (1U << i);
372 }
373
374 const std::vector<Record*> &StageList =
375 ItinData->getValueAsListOfDefs("Stages");
376
Sebastian Popf6f77e92011-12-06 17:34:11 +0000377 // The number of stages.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000378 NStages = StageList.size();
379
Sebastian Popf6f77e92011-12-06 17:34:11 +0000380 // For each unit.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000381 unsigned UnitBitValue = 0;
382
Sebastian Popf6f77e92011-12-06 17:34:11 +0000383 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000384 for (unsigned i = 0; i < NStages; ++i) {
385 const Record *Stage = StageList[i];
386
Sebastian Popf6f77e92011-12-06 17:34:11 +0000387 // Get unit list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000388 const std::vector<Record*> &UnitList =
389 Stage->getValueAsListOfDefs("Units");
390
391 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000392 // Conduct bitwise or.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000393 std::string UnitName = UnitList[j]->getName();
394 assert(NameToBitsMap.count(UnitName));
395 UnitBitValue |= NameToBitsMap[UnitName];
396 }
397
398 if (UnitBitValue != 0)
399 allInsnClasses.insert(UnitBitValue);
400 }
401}
402
403
404//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000405// Run the worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000406//
407void DFAGen::run(raw_ostream &OS) {
408 EmitSourceFileHeader("Target DFA Packetizer Tables", OS);
409
Sebastian Popf6f77e92011-12-06 17:34:11 +0000410 // Collect processor iteraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000411 std::vector<Record*> ProcItinList =
412 Records.getAllDerivedDefinitions("ProcessorItineraries");
413
414 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000415 // Collect the instruction classes.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000416 //
417 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
418 Record *Proc = ProcItinList[i];
419
Sebastian Popf6f77e92011-12-06 17:34:11 +0000420 // Get processor itinerary name.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000421 const std::string &Name = Proc->getName();
422
Sebastian Popf6f77e92011-12-06 17:34:11 +0000423 // Skip default.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000424 if (Name == "NoItineraries")
425 continue;
426
Sebastian Popf6f77e92011-12-06 17:34:11 +0000427 // Sanity check for at least one instruction itinerary class.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000428 unsigned NItinClasses =
429 Records.getAllDerivedDefinitions("InstrItinClass").size();
430 if (NItinClasses == 0)
431 return;
432
Sebastian Popf6f77e92011-12-06 17:34:11 +0000433 // Get itinerary data list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000434 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
435
Sebastian Popf6f77e92011-12-06 17:34:11 +0000436 // Collect instruction classes for all itinerary data.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000437 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
438 Record *ItinData = ItinDataList[j];
439 unsigned NStages;
440 collectAllInsnClasses(Name, ItinData, NStages, OS);
441 }
442 }
443
444
445 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000446 // Run a worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000447 //
448 DFA D;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000449 State *Initial = new State;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000450 Initial->isInitial = true;
451 Initial->stateInfo.insert(0x0);
452 D.addState(Initial);
453 SmallVector<State*, 32> WorkList;
454 std::map<std::set<unsigned>, State*> Visited;
455
456 WorkList.push_back(Initial);
457
458 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000459 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000460 // C = {set of InsnClasses}
461 // Begin with initial node in worklist. Initial node does not have
462 // any consumed resources,
463 // ResourceState = 0x0
464 // Visited = {}
465 // While worklist != empty
466 // S = first element of worklist
467 // For every instruction class C
468 // if we can accommodate C in S:
469 // S' = state with resource states = {S Union C}
470 // Add a new transition: S x C -> S'
471 // If S' is not in Visited:
472 // Add S' to worklist
473 // Add S' to Visited
474 //
475 while (!WorkList.empty()) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000476 State *current = WorkList.pop_back_val();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000477 for (DenseSet<unsigned>::iterator CI = allInsnClasses.begin(),
478 CE = allInsnClasses.end(); CI != CE; ++CI) {
479 unsigned InsnClass = *CI;
480
481 std::set<unsigned> NewStateResources;
482 //
483 // If we haven't already created a transition for this input
Sebastian Popf6f77e92011-12-06 17:34:11 +0000484 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000485 //
486 if (!D.getTransition(current, InsnClass) &&
487 current->canAddInsnClass(InsnClass, NewStateResources)) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000488 State *NewState = NULL;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000489
490 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000491 // If we have seen this state before, then do not create a new state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000492 //
493 //
494 std::map<std::set<unsigned>, State*>::iterator VI;
495 if ((VI = Visited.find(NewStateResources)) != Visited.end())
496 NewState = VI->second;
497 else {
498 NewState = new State;
499 NewState->stateInfo = NewStateResources;
500 D.addState(NewState);
501 Visited[NewStateResources] = NewState;
502 WorkList.push_back(NewState);
503 }
504
Sebastian Pop464f3a32011-12-06 17:34:16 +0000505 Transition *NewTransition = new Transition(current, InsnClass,
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000506 NewState);
507 D.addTransition(NewTransition);
508 }
509 }
510 }
511
Sebastian Popf6f77e92011-12-06 17:34:11 +0000512 // Print out the table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000513 D.writeTableAndAPI(OS, TargetName);
514}