blob: 26ab76390e01b267b859702dd5a43664861edf9c [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 //
Sebastian Pop464f3a32011-12-06 17:34:16 +000097 bool canAddInsnClass(unsigned InsnClass, std::set<unsigned> &PossibleStates);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000098};
Sebastian Popf6f77e92011-12-06 17:34:11 +000099} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000100
101
102namespace {
103struct Transition {
104 public:
105 static int currentTransitionNum;
106 int transitionNum;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000107 State *from;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000108 unsigned input;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000109 State *to;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000110
Sebastian Pop464f3a32011-12-06 17:34:16 +0000111 Transition(State *from_, unsigned input_, State *to_);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000112};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000113} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000114
115
116//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000117// Comparators to keep set of states sorted.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000118//
119namespace {
120struct ltState {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000121 bool operator()(const State *s1, const State *s2) const;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000122};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000123} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000124
125
126//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000127// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000128//
129namespace {
130class DFA {
131public:
132 DFA();
133
Sebastian Popf6f77e92011-12-06 17:34:11 +0000134 // Set of states. Need to keep this sorted to emit the transition table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000135 std::set<State*, ltState> states;
136
Sebastian Popf6f77e92011-12-06 17:34:11 +0000137 // Map from a state to the list of transitions with that state as source.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000138 std::map<State*, SmallVector<Transition*, 16>, ltState> stateTransitions;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000139 State *currentState;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000140
Sebastian Popf6f77e92011-12-06 17:34:11 +0000141 // Highest valued Input seen.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000142 unsigned LargestInput;
143
144 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000145 // Modify the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000146 //
147 void initialize();
Sebastian Pop464f3a32011-12-06 17:34:16 +0000148 void addState(State *);
149 void addTransition(Transition *);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000150
151 //
152 // getTransition - Return the state when a transition is made from
Sebastian Popf6f77e92011-12-06 17:34:11 +0000153 // State From with Input I. If a transition is not found, return NULL.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000154 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000155 State *getTransition(State *, unsigned);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000156
157 //
158 // isValidTransition: Predicate that checks if there is a valid transition
Sebastian Popf6f77e92011-12-06 17:34:11 +0000159 // from state From on input InsnClass.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000160 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000161 bool isValidTransition(State *From, unsigned InsnClass);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000162
163 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000164 // writeTable: Print out a table representing the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000165 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000166 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000167};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000168} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000169
170
171//
172// Constructors for State, Transition, and DFA
173//
174State::State() :
175 stateNum(currentStateNum++), isInitial(false) {}
176
177
Sebastian Pop464f3a32011-12-06 17:34:16 +0000178State::State(const State &S) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000179 stateNum(currentStateNum++), isInitial(S.isInitial),
180 stateInfo(S.stateInfo) {}
181
182
Sebastian Pop464f3a32011-12-06 17:34:16 +0000183Transition::Transition(State *from_, unsigned input_, State *to_) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000184 transitionNum(currentTransitionNum++), from(from_), input(input_),
185 to(to_) {}
186
187
188DFA::DFA() :
189 LargestInput(0) {}
190
191
Sebastian Pop464f3a32011-12-06 17:34:16 +0000192bool ltState::operator()(const State *s1, const State *s2) const {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000193 return (s1->stateNum < s2->stateNum);
194}
195
196
197//
198// canAddInsnClass - Returns true if an instruction of type InsnClass is a
199// valid transition from this state i.e., can an instruction of type InsnClass
Sebastian Popf6f77e92011-12-06 17:34:11 +0000200// be added to the packet represented by this state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000201//
202// PossibleStates is the set of valid resource states that ensue from valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000203// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000204//
205bool State::canAddInsnClass(unsigned InsnClass,
Sebastian Pop464f3a32011-12-06 17:34:16 +0000206 std::set<unsigned> &PossibleStates) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000207 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000208 // Iterate over all resource states in currentState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000209 //
210 bool AddedState = false;
211
212 for (std::set<unsigned>::iterator SI = stateInfo.begin();
213 SI != stateInfo.end(); ++SI) {
214 unsigned thisState = *SI;
215
216 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000217 // Iterate over all possible resources used in InsnClass.
218 // For ex: for InsnClass = 0x11, all resources = {0x01, 0x10}.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000219 //
220
221 DenseSet<unsigned> VisitedResourceStates;
222 for (unsigned int j = 0; j < sizeof(InsnClass) * 8; ++j) {
223 if ((0x1 << j) & InsnClass) {
224 //
225 // For each possible resource used in InsnClass, generate the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000226 // resource state if that resource was used.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000227 //
228 unsigned ResultingResourceState = thisState | (0x1 << j);
229 //
230 // Check if the resulting resource state can be accommodated in this
Sebastian Popf6f77e92011-12-06 17:34:11 +0000231 // packet.
232 // We compute ResultingResourceState OR thisState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000233 // If the result of the OR is different than thisState, it implies
234 // that there is at least one resource that can be used to schedule
Sebastian Popf6f77e92011-12-06 17:34:11 +0000235 // InsnClass in the current packet.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000236 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Popf6f77e92011-12-06 17:34:11 +0000237 // processed ResultingResourceState before.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000238 //
239 if ((ResultingResourceState != thisState) &&
240 (VisitedResourceStates.count(ResultingResourceState) == 0)) {
241 VisitedResourceStates.insert(ResultingResourceState);
242 PossibleStates.insert(ResultingResourceState);
243 AddedState = true;
244 }
245 }
246 }
247 }
248
249 return AddedState;
250}
251
252
253void DFA::initialize() {
254 currentState->isInitial = true;
255}
256
257
Sebastian Pop464f3a32011-12-06 17:34:16 +0000258void DFA::addState(State *S) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000259 assert(!states.count(S) && "State already exists");
260 states.insert(S);
261}
262
263
Sebastian Pop464f3a32011-12-06 17:34:16 +0000264void DFA::addTransition(Transition *T) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000265 // Update LargestInput.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000266 if (T->input > LargestInput)
267 LargestInput = T->input;
268
Sebastian Popf6f77e92011-12-06 17:34:11 +0000269 // Add the new transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000270 stateTransitions[T->from].push_back(T);
271}
272
273
274//
275// getTransition - Return the state when a transition is made from
Sebastian Popf6f77e92011-12-06 17:34:11 +0000276// State From with Input I. If a transition is not found, return NULL.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000277//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000278State *DFA::getTransition(State *From, unsigned I) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000279 // Do we have a transition from state From?
280 if (!stateTransitions.count(From))
281 return NULL;
282
283 // Do we have a transition from state From with Input I?
284 for (SmallVector<Transition*, 16>::iterator VI =
285 stateTransitions[From].begin();
286 VI != stateTransitions[From].end(); ++VI)
287 if ((*VI)->input == I)
288 return (*VI)->to;
289
290 return NULL;
291}
292
293
Sebastian Pop464f3a32011-12-06 17:34:16 +0000294bool DFA::isValidTransition(State *From, unsigned InsnClass) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000295 return (getTransition(From, InsnClass) != NULL);
296}
297
298
299int State::currentStateNum = 0;
300int Transition::currentTransitionNum = 0;
301
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000302DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000303 TargetName(CodeGenTarget(R).getName()),
304 allInsnClasses(), Records(R) {}
305
306
307//
308// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000309// associated API to create a DFA packetizer.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000310//
311// Format:
312// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000313// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000314// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Popf6f77e92011-12-06 17:34:11 +0000315// the ith state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000316//
317//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000318void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000319 std::set<State*, ltState>::iterator SI = states.begin();
320 // This table provides a map to the beginning of the transitions for State s
Sebastian Popf6f77e92011-12-06 17:34:11 +0000321 // in DFAStateInputTable.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000322 std::vector<int> StateEntry(states.size());
323
324 OS << "namespace llvm {\n\n";
325 OS << "const int " << TargetName << "DFAStateInputTable[][2] = {\n";
326
327 // Tracks the total valid transitions encountered so far. It is used
Sebastian Popf6f77e92011-12-06 17:34:11 +0000328 // to construct the StateEntry table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000329 int ValidTransitions = 0;
330 for (unsigned i = 0; i < states.size(); ++i, ++SI) {
331 StateEntry[i] = ValidTransitions;
332 for (unsigned j = 0; j <= LargestInput; ++j) {
333 assert (((*SI)->stateNum == (int) i) && "Mismatch in state numbers");
334 if (!isValidTransition(*SI, j))
335 continue;
336
337 OS << "{" << j << ", "
338 << getTransition(*SI, j)->stateNum
339 << "}, ";
340 ++ValidTransitions;
341 }
342
Sebastian Popf6f77e92011-12-06 17:34:11 +0000343 // If there are no valid transitions from this stage, we need a sentinel
344 // transition.
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000345 if (ValidTransitions == StateEntry[i]) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000346 OS << "{-1, -1},";
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000347 ++ValidTransitions;
348 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000349
350 OS << "\n";
351 }
352 OS << "};\n\n";
353 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
354
355 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Popf6f77e92011-12-06 17:34:11 +0000356 // two numbers.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000357 for (unsigned i = 0; i < states.size(); ++i)
358 OS << StateEntry[i] << ", ";
359
360 OS << "\n};\n";
361 OS << "} // namespace\n";
362
363
364 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000365 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000366 //
367 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
368 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
369 OS << "namespace llvm {\n";
Sebastian Pop464f3a32011-12-06 17:34:16 +0000370 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000371 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
372 << " return new DFAPacketizer(IID, " << TargetName
373 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
374 OS << "} // End llvm namespace \n";
375}
376
377
378//
379// collectAllInsnClasses - Populate allInsnClasses which is a set of units
380// used in each stage.
381//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000382void DFAPacketizerEmitter::collectAllInsnClasses(const std::string &Name,
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000383 Record *ItinData,
384 unsigned &NStages,
385 raw_ostream &OS) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000386 // Collect processor itineraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000387 std::vector<Record*> ProcItinList =
Sebastian Popf6f77e92011-12-06 17:34:11 +0000388 Records.getAllDerivedDefinitions("ProcessorItineraries");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000389
Sebastian Popf6f77e92011-12-06 17:34:11 +0000390 // If just no itinerary then don't bother.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000391 if (ProcItinList.size() < 2)
392 return;
393 std::map<std::string, unsigned> NameToBitsMap;
394
395 // Parse functional units for all the itineraries.
396 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
397 Record *Proc = ProcItinList[i];
398 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
399
Sebastian Popf6f77e92011-12-06 17:34:11 +0000400 // Convert macros to bits for each stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000401 for (unsigned i = 0, N = FUs.size(); i < N; ++i)
402 NameToBitsMap[FUs[i]->getName()] = (unsigned) (1U << i);
403 }
404
405 const std::vector<Record*> &StageList =
406 ItinData->getValueAsListOfDefs("Stages");
407
Sebastian Popf6f77e92011-12-06 17:34:11 +0000408 // The number of stages.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000409 NStages = StageList.size();
410
Sebastian Popf6f77e92011-12-06 17:34:11 +0000411 // For each unit.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000412 unsigned UnitBitValue = 0;
413
Sebastian Popf6f77e92011-12-06 17:34:11 +0000414 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000415 for (unsigned i = 0; i < NStages; ++i) {
416 const Record *Stage = StageList[i];
417
Sebastian Popf6f77e92011-12-06 17:34:11 +0000418 // Get unit list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000419 const std::vector<Record*> &UnitList =
420 Stage->getValueAsListOfDefs("Units");
421
422 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000423 // Conduct bitwise or.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000424 std::string UnitName = UnitList[j]->getName();
425 assert(NameToBitsMap.count(UnitName));
426 UnitBitValue |= NameToBitsMap[UnitName];
427 }
428
429 if (UnitBitValue != 0)
430 allInsnClasses.insert(UnitBitValue);
431 }
432}
433
434
435//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000436// Run the worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000437//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000438void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000439
Sebastian Popf6f77e92011-12-06 17:34:11 +0000440 // Collect processor iteraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000441 std::vector<Record*> ProcItinList =
442 Records.getAllDerivedDefinitions("ProcessorItineraries");
443
444 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000445 // Collect the instruction classes.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000446 //
447 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
448 Record *Proc = ProcItinList[i];
449
Sebastian Popf6f77e92011-12-06 17:34:11 +0000450 // Get processor itinerary name.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000451 const std::string &Name = Proc->getName();
452
Sebastian Popf6f77e92011-12-06 17:34:11 +0000453 // Skip default.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000454 if (Name == "NoItineraries")
455 continue;
456
Sebastian Popf6f77e92011-12-06 17:34:11 +0000457 // Sanity check for at least one instruction itinerary class.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000458 unsigned NItinClasses =
459 Records.getAllDerivedDefinitions("InstrItinClass").size();
460 if (NItinClasses == 0)
461 return;
462
Sebastian Popf6f77e92011-12-06 17:34:11 +0000463 // Get itinerary data list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000464 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
465
Sebastian Popf6f77e92011-12-06 17:34:11 +0000466 // Collect instruction classes for all itinerary data.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000467 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
468 Record *ItinData = ItinDataList[j];
469 unsigned NStages;
470 collectAllInsnClasses(Name, ItinData, NStages, OS);
471 }
472 }
473
474
475 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000476 // Run a worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000477 //
478 DFA D;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000479 State *Initial = new State;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000480 Initial->isInitial = true;
481 Initial->stateInfo.insert(0x0);
482 D.addState(Initial);
483 SmallVector<State*, 32> WorkList;
484 std::map<std::set<unsigned>, State*> Visited;
485
486 WorkList.push_back(Initial);
487
488 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000489 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000490 // C = {set of InsnClasses}
491 // Begin with initial node in worklist. Initial node does not have
492 // any consumed resources,
493 // ResourceState = 0x0
494 // Visited = {}
495 // While worklist != empty
496 // S = first element of worklist
497 // For every instruction class C
498 // if we can accommodate C in S:
499 // S' = state with resource states = {S Union C}
500 // Add a new transition: S x C -> S'
501 // If S' is not in Visited:
502 // Add S' to worklist
503 // Add S' to Visited
504 //
505 while (!WorkList.empty()) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000506 State *current = WorkList.pop_back_val();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000507 for (DenseSet<unsigned>::iterator CI = allInsnClasses.begin(),
508 CE = allInsnClasses.end(); CI != CE; ++CI) {
509 unsigned InsnClass = *CI;
510
511 std::set<unsigned> NewStateResources;
512 //
513 // If we haven't already created a transition for this input
Sebastian Popf6f77e92011-12-06 17:34:11 +0000514 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000515 //
516 if (!D.getTransition(current, InsnClass) &&
517 current->canAddInsnClass(InsnClass, NewStateResources)) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000518 State *NewState = NULL;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000519
520 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000521 // If we have seen this state before, then do not create a new state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000522 //
523 //
524 std::map<std::set<unsigned>, State*>::iterator VI;
525 if ((VI = Visited.find(NewStateResources)) != Visited.end())
526 NewState = VI->second;
527 else {
528 NewState = new State;
529 NewState->stateInfo = NewStateResources;
530 D.addState(NewState);
531 Visited[NewStateResources] = NewState;
532 WorkList.push_back(NewState);
533 }
534
Sebastian Pop464f3a32011-12-06 17:34:16 +0000535 Transition *NewTransition = new Transition(current, InsnClass,
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000536 NewState);
537 D.addTransition(NewTransition);
538 }
539 }
540 }
541
Sebastian Popf6f77e92011-12-06 17:34:11 +0000542 // Print out the table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000543 D.writeTableAndAPI(OS, TargetName);
544}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000545
546namespace llvm {
547
548void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
549 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
550 DFAPacketizerEmitter(RK).run(OS);
551}
552
553} // End llvm namespace