blob: 1a5af84c6e0aec70220313769788f19099788608 [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
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000018#include "CodeGenTarget.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000019#include "llvm/ADT/DenseSet.h"
20#include "llvm/TableGen/Record.h"
21#include "llvm/TableGen/TableGenBackend.h"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000022#include <list>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000023#include <map>
24#include <string>
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000025using namespace llvm;
26
27//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000028// class DFAPacketizerEmitter: class that generates and prints out the DFA
29// for resource tracking.
30//
31namespace {
32class DFAPacketizerEmitter {
33private:
34 std::string TargetName;
35 //
36 // allInsnClasses is the set of all possible resources consumed by an
37 // InstrStage.
38 //
39 DenseSet<unsigned> allInsnClasses;
40 RecordKeeper &Records;
41
42public:
43 DFAPacketizerEmitter(RecordKeeper &R);
44
45 //
46 // collectAllInsnClasses: Populate allInsnClasses which is a set of units
47 // used in each stage.
48 //
49 void collectAllInsnClasses(const std::string &Name,
50 Record *ItinData,
51 unsigned &NStages,
52 raw_ostream &OS);
53
54 void run(raw_ostream &OS);
55};
56} // End anonymous namespace.
57
58//
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000059//
60// State represents the usage of machine resources if the packet contains
61// a set of instruction classes.
62//
Sebastian Popf6f77e92011-12-06 17:34:11 +000063// Specifically, currentState is a set of bit-masks.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000064// The nth bit in a bit-mask indicates whether the nth resource is being used
65// by this state. The set of bit-masks in a state represent the different
66// possible outcomes of transitioning to this state.
Sebastian Popf6f77e92011-12-06 17:34:11 +000067// For example: consider a two resource architecture: resource L and resource M
68// with three instruction classes: L, M, and L_or_M.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000069// From the initial state (currentState = 0x00), if we add instruction class
70// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
71// represents the possible resource states that can result from adding a L_or_M
72// instruction
73//
74// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Popf6f77e92011-12-06 17:34:11 +000075// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000076//
77//
78namespace {
79class State {
80 public:
81 static int currentStateNum;
82 int stateNum;
83 bool isInitial;
84 std::set<unsigned> stateInfo;
85
86 State();
Sebastian Pop464f3a32011-12-06 17:34:16 +000087 State(const State &S);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000088
89 //
90 // canAddInsnClass - Returns true if an instruction of type InsnClass is a
Sebastian Popf6f77e92011-12-06 17:34:11 +000091 // valid transition from this state, i.e., can an instruction of type InsnClass
92 // be added to the packet represented by this state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000093 //
94 // PossibleStates is the set of valid resource states that ensue from valid
Sebastian Popf6f77e92011-12-06 17:34:11 +000095 // transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000096 //
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +000097 bool canAddInsnClass(unsigned InsnClass) const;
98 //
99 // AddInsnClass - Return all combinations of resource reservation
100 // which are possible from this state (PossibleStates).
101 //
102 void AddInsnClass(unsigned InsnClass, std::set<unsigned> &PossibleStates);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000103};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000104} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000105
106
107namespace {
108struct Transition {
109 public:
110 static int currentTransitionNum;
111 int transitionNum;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000112 State *from;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000113 unsigned input;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000114 State *to;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000115
Sebastian Pop464f3a32011-12-06 17:34:16 +0000116 Transition(State *from_, unsigned input_, State *to_);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000117};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000118} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000119
120
121//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000122// Comparators to keep set of states sorted.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000123//
124namespace {
125struct ltState {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000126 bool operator()(const State *s1, const State *s2) const;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000127};
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000128
129struct ltTransition {
130 bool operator()(const Transition *s1, const Transition *s2) const;
131};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000132} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000133
134
135//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000136// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000137//
138namespace {
139class DFA {
140public:
141 DFA();
142
Sebastian Popf6f77e92011-12-06 17:34:11 +0000143 // Set of states. Need to keep this sorted to emit the transition table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000144 std::set<State*, ltState> states;
145
Sebastian Popf6f77e92011-12-06 17:34:11 +0000146 // Map from a state to the list of transitions with that state as source.
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000147 std::map<State*, std::set<Transition*, ltTransition>, ltState>
148 stateTransitions;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000149 State *currentState;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000150
Sebastian Popf6f77e92011-12-06 17:34:11 +0000151 // Highest valued Input seen.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000152 unsigned LargestInput;
153
154 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000155 // Modify the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000156 //
157 void initialize();
Sebastian Pop464f3a32011-12-06 17:34:16 +0000158 void addState(State *);
159 void addTransition(Transition *);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000160
161 //
162 // getTransition - Return the state when a transition is made from
Sebastian Popf6f77e92011-12-06 17:34:11 +0000163 // State From with Input I. If a transition is not found, return NULL.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000164 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000165 State *getTransition(State *, unsigned);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000166
167 //
168 // isValidTransition: Predicate that checks if there is a valid transition
Sebastian Popf6f77e92011-12-06 17:34:11 +0000169 // from state From on input InsnClass.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000170 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000171 bool isValidTransition(State *From, unsigned InsnClass);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000172
173 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000174 // writeTable: Print out a table representing the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000175 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000176 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000177};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000178} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000179
180
181//
182// Constructors for State, Transition, and DFA
183//
184State::State() :
185 stateNum(currentStateNum++), isInitial(false) {}
186
187
Sebastian Pop464f3a32011-12-06 17:34:16 +0000188State::State(const State &S) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000189 stateNum(currentStateNum++), isInitial(S.isInitial),
190 stateInfo(S.stateInfo) {}
191
192
Sebastian Pop464f3a32011-12-06 17:34:16 +0000193Transition::Transition(State *from_, unsigned input_, State *to_) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000194 transitionNum(currentTransitionNum++), from(from_), input(input_),
195 to(to_) {}
196
197
198DFA::DFA() :
199 LargestInput(0) {}
200
201
Sebastian Pop464f3a32011-12-06 17:34:16 +0000202bool ltState::operator()(const State *s1, const State *s2) const {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000203 return (s1->stateNum < s2->stateNum);
204}
205
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000206bool ltTransition::operator()(const Transition *s1, const Transition *s2) const {
207 return (s1->input < s2->input);
208}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000209
210//
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000211// AddInsnClass - Return all combinations of resource reservation
212// which are possible from this state (PossibleStates).
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000213//
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000214void State::AddInsnClass(unsigned InsnClass,
Sebastian Pop464f3a32011-12-06 17:34:16 +0000215 std::set<unsigned> &PossibleStates) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000216 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000217 // Iterate over all resource states in currentState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000218 //
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000219
220 for (std::set<unsigned>::iterator SI = stateInfo.begin();
221 SI != stateInfo.end(); ++SI) {
222 unsigned thisState = *SI;
223
224 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000225 // Iterate over all possible resources used in InsnClass.
226 // For ex: for InsnClass = 0x11, all resources = {0x01, 0x10}.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000227 //
228
229 DenseSet<unsigned> VisitedResourceStates;
230 for (unsigned int j = 0; j < sizeof(InsnClass) * 8; ++j) {
231 if ((0x1 << j) & InsnClass) {
232 //
233 // For each possible resource used in InsnClass, generate the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000234 // resource state if that resource was used.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000235 //
236 unsigned ResultingResourceState = thisState | (0x1 << j);
237 //
238 // Check if the resulting resource state can be accommodated in this
Sebastian Popf6f77e92011-12-06 17:34:11 +0000239 // packet.
240 // We compute ResultingResourceState OR thisState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000241 // If the result of the OR is different than thisState, it implies
242 // that there is at least one resource that can be used to schedule
Sebastian Popf6f77e92011-12-06 17:34:11 +0000243 // InsnClass in the current packet.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000244 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Popf6f77e92011-12-06 17:34:11 +0000245 // processed ResultingResourceState before.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000246 //
247 if ((ResultingResourceState != thisState) &&
248 (VisitedResourceStates.count(ResultingResourceState) == 0)) {
249 VisitedResourceStates.insert(ResultingResourceState);
250 PossibleStates.insert(ResultingResourceState);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000251 }
252 }
253 }
254 }
255
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000256}
257
258
259//
260// canAddInsnClass - Quickly verifies if an instruction of type InsnClass is a
261// valid transition from this state i.e., can an instruction of type InsnClass
262// be added to the packet represented by this state.
263//
264bool State::canAddInsnClass(unsigned InsnClass) const {
265 for (std::set<unsigned>::iterator SI = stateInfo.begin();
266 SI != stateInfo.end(); ++SI) {
267 if (~*SI & InsnClass)
268 return true;
269 }
270 return false;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000271}
272
273
274void DFA::initialize() {
275 currentState->isInitial = true;
276}
277
278
Sebastian Pop464f3a32011-12-06 17:34:16 +0000279void DFA::addState(State *S) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000280 assert(!states.count(S) && "State already exists");
281 states.insert(S);
282}
283
284
Sebastian Pop464f3a32011-12-06 17:34:16 +0000285void DFA::addTransition(Transition *T) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000286 // Update LargestInput.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000287 if (T->input > LargestInput)
288 LargestInput = T->input;
289
Sebastian Popf6f77e92011-12-06 17:34:11 +0000290 // Add the new transition.
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000291 bool Added = stateTransitions[T->from].insert(T).second;
292 assert(Added && "Cannot have multiple states for the same input");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000293}
294
295
296//
297// getTransition - Return the state when a transition is made from
Sebastian Popf6f77e92011-12-06 17:34:11 +0000298// State From with Input I. If a transition is not found, return NULL.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000299//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000300State *DFA::getTransition(State *From, unsigned I) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000301 // Do we have a transition from state From?
302 if (!stateTransitions.count(From))
303 return NULL;
304
305 // Do we have a transition from state From with Input I?
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000306 Transition TVal(NULL, I, NULL);
307 // Do not count this temporal instance
308 Transition::currentTransitionNum--;
309 std::set<Transition*, ltTransition>::iterator T =
310 stateTransitions[From].find(&TVal);
311 if (T != stateTransitions[From].end())
312 return (*T)->to;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000313
314 return NULL;
315}
316
317
Sebastian Pop464f3a32011-12-06 17:34:16 +0000318bool DFA::isValidTransition(State *From, unsigned InsnClass) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000319 return (getTransition(From, InsnClass) != NULL);
320}
321
322
323int State::currentStateNum = 0;
324int Transition::currentTransitionNum = 0;
325
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000326DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000327 TargetName(CodeGenTarget(R).getName()),
328 allInsnClasses(), Records(R) {}
329
330
331//
332// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000333// associated API to create a DFA packetizer.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000334//
335// Format:
336// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000337// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000338// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Popf6f77e92011-12-06 17:34:11 +0000339// the ith state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000340//
341//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000342void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000343 std::set<State*, ltState>::iterator SI = states.begin();
344 // This table provides a map to the beginning of the transitions for State s
Sebastian Popf6f77e92011-12-06 17:34:11 +0000345 // in DFAStateInputTable.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000346 std::vector<int> StateEntry(states.size());
347
348 OS << "namespace llvm {\n\n";
349 OS << "const int " << TargetName << "DFAStateInputTable[][2] = {\n";
350
351 // Tracks the total valid transitions encountered so far. It is used
Sebastian Popf6f77e92011-12-06 17:34:11 +0000352 // to construct the StateEntry table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000353 int ValidTransitions = 0;
354 for (unsigned i = 0; i < states.size(); ++i, ++SI) {
355 StateEntry[i] = ValidTransitions;
356 for (unsigned j = 0; j <= LargestInput; ++j) {
357 assert (((*SI)->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000358 State *To = getTransition(*SI, j);
359 if (To == NULL)
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000360 continue;
361
362 OS << "{" << j << ", "
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000363 << To->stateNum
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000364 << "}, ";
365 ++ValidTransitions;
366 }
367
Sebastian Popf6f77e92011-12-06 17:34:11 +0000368 // If there are no valid transitions from this stage, we need a sentinel
369 // transition.
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000370 if (ValidTransitions == StateEntry[i]) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000371 OS << "{-1, -1},";
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000372 ++ValidTransitions;
373 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000374
375 OS << "\n";
376 }
377 OS << "};\n\n";
378 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
379
380 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Popf6f77e92011-12-06 17:34:11 +0000381 // two numbers.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000382 for (unsigned i = 0; i < states.size(); ++i)
383 OS << StateEntry[i] << ", ";
384
385 OS << "\n};\n";
386 OS << "} // namespace\n";
387
388
389 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000390 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000391 //
392 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
393 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
394 OS << "namespace llvm {\n";
Sebastian Pop464f3a32011-12-06 17:34:16 +0000395 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000396 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
397 << " return new DFAPacketizer(IID, " << TargetName
398 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
399 OS << "} // End llvm namespace \n";
400}
401
402
403//
404// collectAllInsnClasses - Populate allInsnClasses which is a set of units
405// used in each stage.
406//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000407void DFAPacketizerEmitter::collectAllInsnClasses(const std::string &Name,
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000408 Record *ItinData,
409 unsigned &NStages,
410 raw_ostream &OS) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000411 // Collect processor itineraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000412 std::vector<Record*> ProcItinList =
Sebastian Popf6f77e92011-12-06 17:34:11 +0000413 Records.getAllDerivedDefinitions("ProcessorItineraries");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000414
Sebastian Popf6f77e92011-12-06 17:34:11 +0000415 // If just no itinerary then don't bother.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000416 if (ProcItinList.size() < 2)
417 return;
418 std::map<std::string, unsigned> NameToBitsMap;
419
420 // Parse functional units for all the itineraries.
421 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
422 Record *Proc = ProcItinList[i];
423 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
424
Sebastian Popf6f77e92011-12-06 17:34:11 +0000425 // Convert macros to bits for each stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000426 for (unsigned i = 0, N = FUs.size(); i < N; ++i)
427 NameToBitsMap[FUs[i]->getName()] = (unsigned) (1U << i);
428 }
429
430 const std::vector<Record*> &StageList =
431 ItinData->getValueAsListOfDefs("Stages");
432
Sebastian Popf6f77e92011-12-06 17:34:11 +0000433 // The number of stages.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000434 NStages = StageList.size();
435
Sebastian Popf6f77e92011-12-06 17:34:11 +0000436 // For each unit.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000437 unsigned UnitBitValue = 0;
438
Sebastian Popf6f77e92011-12-06 17:34:11 +0000439 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000440 for (unsigned i = 0; i < NStages; ++i) {
441 const Record *Stage = StageList[i];
442
Sebastian Popf6f77e92011-12-06 17:34:11 +0000443 // Get unit list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000444 const std::vector<Record*> &UnitList =
445 Stage->getValueAsListOfDefs("Units");
446
447 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000448 // Conduct bitwise or.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000449 std::string UnitName = UnitList[j]->getName();
450 assert(NameToBitsMap.count(UnitName));
451 UnitBitValue |= NameToBitsMap[UnitName];
452 }
453
454 if (UnitBitValue != 0)
455 allInsnClasses.insert(UnitBitValue);
456 }
457}
458
459
460//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000461// Run the worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000462//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000463void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000464
Sebastian Popf6f77e92011-12-06 17:34:11 +0000465 // Collect processor iteraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000466 std::vector<Record*> ProcItinList =
467 Records.getAllDerivedDefinitions("ProcessorItineraries");
468
469 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000470 // Collect the instruction classes.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000471 //
472 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
473 Record *Proc = ProcItinList[i];
474
Sebastian Popf6f77e92011-12-06 17:34:11 +0000475 // Get processor itinerary name.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000476 const std::string &Name = Proc->getName();
477
Sebastian Popf6f77e92011-12-06 17:34:11 +0000478 // Skip default.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000479 if (Name == "NoItineraries")
480 continue;
481
Sebastian Popf6f77e92011-12-06 17:34:11 +0000482 // Sanity check for at least one instruction itinerary class.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000483 unsigned NItinClasses =
484 Records.getAllDerivedDefinitions("InstrItinClass").size();
485 if (NItinClasses == 0)
486 return;
487
Sebastian Popf6f77e92011-12-06 17:34:11 +0000488 // Get itinerary data list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000489 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
490
Sebastian Popf6f77e92011-12-06 17:34:11 +0000491 // Collect instruction classes for all itinerary data.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000492 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
493 Record *ItinData = ItinDataList[j];
494 unsigned NStages;
495 collectAllInsnClasses(Name, ItinData, NStages, OS);
496 }
497 }
498
499
500 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000501 // Run a worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000502 //
503 DFA D;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000504 State *Initial = new State;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000505 Initial->isInitial = true;
506 Initial->stateInfo.insert(0x0);
507 D.addState(Initial);
508 SmallVector<State*, 32> WorkList;
509 std::map<std::set<unsigned>, State*> Visited;
510
511 WorkList.push_back(Initial);
512
513 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000514 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000515 // C = {set of InsnClasses}
516 // Begin with initial node in worklist. Initial node does not have
517 // any consumed resources,
518 // ResourceState = 0x0
519 // Visited = {}
520 // While worklist != empty
521 // S = first element of worklist
522 // For every instruction class C
523 // if we can accommodate C in S:
524 // S' = state with resource states = {S Union C}
525 // Add a new transition: S x C -> S'
526 // If S' is not in Visited:
527 // Add S' to worklist
528 // Add S' to Visited
529 //
530 while (!WorkList.empty()) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000531 State *current = WorkList.pop_back_val();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000532 for (DenseSet<unsigned>::iterator CI = allInsnClasses.begin(),
533 CE = allInsnClasses.end(); CI != CE; ++CI) {
534 unsigned InsnClass = *CI;
535
536 std::set<unsigned> NewStateResources;
537 //
538 // If we haven't already created a transition for this input
Sebastian Popf6f77e92011-12-06 17:34:11 +0000539 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000540 //
541 if (!D.getTransition(current, InsnClass) &&
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000542 current->canAddInsnClass(InsnClass)) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000543 State *NewState = NULL;
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000544 current->AddInsnClass(InsnClass, NewStateResources);
545 assert(NewStateResources.size() && "New states must be generated");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000546
547 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000548 // If we have seen this state before, then do not create a new state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000549 //
550 //
551 std::map<std::set<unsigned>, State*>::iterator VI;
552 if ((VI = Visited.find(NewStateResources)) != Visited.end())
553 NewState = VI->second;
554 else {
555 NewState = new State;
556 NewState->stateInfo = NewStateResources;
557 D.addState(NewState);
558 Visited[NewStateResources] = NewState;
559 WorkList.push_back(NewState);
560 }
561
Sebastian Pop464f3a32011-12-06 17:34:16 +0000562 Transition *NewTransition = new Transition(current, InsnClass,
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000563 NewState);
564 D.addTransition(NewTransition);
565 }
566 }
567 }
568
Sebastian Popf6f77e92011-12-06 17:34:11 +0000569 // Print out the table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000570 D.writeTableAndAPI(OS, TargetName);
571}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000572
573namespace llvm {
574
575void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
576 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
577 DFAPacketizerEmitter(RK).run(OS);
578}
579
580} // End llvm namespace