blob: 0ad25a5428dbbc59f892a0a417c828a9add15a9f [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"
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +000020#include "llvm/ADT/STLExtras.h"
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000021#include "llvm/TableGen/Record.h"
22#include "llvm/TableGen/TableGenBackend.h"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000023#include <list>
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +000024#include <map>
25#include <string>
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000026using namespace llvm;
27
28//
Jakob Stoklund Olesen6f36fa92012-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 Dasguptadc81e5d2011-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 Popf6f77e92011-12-06 17:34:11 +000064// Specifically, currentState is a set of bit-masks.
Anshuman Dasguptadc81e5d2011-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 Popf6f77e92011-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 Dasguptadc81e5d2011-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 Popf6f77e92011-12-06 17:34:11 +000076// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000077//
Anshuman Dasgupta3adf3b02012-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 Dasguptadc81e5d2011-12-01 21:10:21 +000080//
81namespace {
82class State {
83 public:
84 static int currentStateNum;
85 int stateNum;
86 bool isInitial;
87 std::set<unsigned> stateInfo;
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +000088 typedef std::map<unsigned, State *> TransitionMap;
89 TransitionMap Transitions;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000090
91 State();
Sebastian Pop464f3a32011-12-06 17:34:16 +000092 State(const State &S);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000093
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +000094 bool operator<(const State &s) const {
95 return stateNum < s.stateNum;
96 }
97
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +000098 //
99 // canAddInsnClass - Returns true if an instruction of type InsnClass is a
Sebastian Popf6f77e92011-12-06 17:34:11 +0000100 // valid transition from this state, i.e., can an instruction of type InsnClass
101 // be added to the packet represented by this state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000102 //
103 // PossibleStates is the set of valid resource states that ensue from valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000104 // transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000105 //
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000106 bool canAddInsnClass(unsigned InsnClass) const;
107 //
108 // AddInsnClass - Return all combinations of resource reservation
109 // which are possible from this state (PossibleStates).
110 //
111 void AddInsnClass(unsigned InsnClass, std::set<unsigned> &PossibleStates);
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000112 //
113 // addTransition - Add a transition from this state given the input InsnClass
114 //
115 void addTransition(unsigned InsnClass, State *To);
116 //
117 // hasTransition - Returns true if there is a transition from this state
118 // given the input InsnClass
119 //
120 bool hasTransition(unsigned InsnClass);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000121};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000122} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000123
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000124//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000125// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000126//
127namespace {
128class DFA {
129public:
130 DFA();
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000131 ~DFA();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000132
Sebastian Popf6f77e92011-12-06 17:34:11 +0000133 // Set of states. Need to keep this sorted to emit the transition table.
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000134 typedef std::set<State *, less_ptr<State> > StateSet;
135 StateSet states;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000136
Sebastian Pop464f3a32011-12-06 17:34:16 +0000137 State *currentState;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000138
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000139 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000140 // Modify the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000141 //
142 void initialize();
Sebastian Pop464f3a32011-12-06 17:34:16 +0000143 void addState(State *);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000144
145 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000146 // writeTable: Print out a table representing the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000147 //
Sebastian Pop464f3a32011-12-06 17:34:16 +0000148 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000149};
Sebastian Popf6f77e92011-12-06 17:34:11 +0000150} // End anonymous namespace.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000151
152
153//
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000154// Constructors and destructors for State and DFA
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000155//
156State::State() :
157 stateNum(currentStateNum++), isInitial(false) {}
158
159
Sebastian Pop464f3a32011-12-06 17:34:16 +0000160State::State(const State &S) :
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000161 stateNum(currentStateNum++), isInitial(S.isInitial),
162 stateInfo(S.stateInfo) {}
163
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000164DFA::DFA(): currentState(NULL) {}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000165
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000166DFA::~DFA() {
167 DeleteContainerPointers(states);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000168}
169
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000170//
171// addTransition - Add a transition from this state given the input InsnClass
172//
173void State::addTransition(unsigned InsnClass, State *To) {
174 assert(!Transitions.count(InsnClass) &&
175 "Cannot have multiple transitions for the same input");
176 Transitions[InsnClass] = To;
177}
178
179//
180// hasTransition - Returns true if there is a transition from this state
181// given the input InsnClass
182//
183bool State::hasTransition(unsigned InsnClass) {
184 return Transitions.count(InsnClass) > 0;
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000185}
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000186
187//
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000188// AddInsnClass - Return all combinations of resource reservation
189// which are possible from this state (PossibleStates).
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000190//
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000191void State::AddInsnClass(unsigned InsnClass,
Sebastian Pop464f3a32011-12-06 17:34:16 +0000192 std::set<unsigned> &PossibleStates) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000193 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000194 // Iterate over all resource states in currentState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000195 //
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000196
197 for (std::set<unsigned>::iterator SI = stateInfo.begin();
198 SI != stateInfo.end(); ++SI) {
199 unsigned thisState = *SI;
200
201 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000202 // Iterate over all possible resources used in InsnClass.
203 // For ex: for InsnClass = 0x11, all resources = {0x01, 0x10}.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000204 //
205
206 DenseSet<unsigned> VisitedResourceStates;
207 for (unsigned int j = 0; j < sizeof(InsnClass) * 8; ++j) {
208 if ((0x1 << j) & InsnClass) {
209 //
210 // For each possible resource used in InsnClass, generate the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000211 // resource state if that resource was used.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000212 //
213 unsigned ResultingResourceState = thisState | (0x1 << j);
214 //
215 // Check if the resulting resource state can be accommodated in this
Sebastian Popf6f77e92011-12-06 17:34:11 +0000216 // packet.
217 // We compute ResultingResourceState OR thisState.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000218 // If the result of the OR is different than thisState, it implies
219 // that there is at least one resource that can be used to schedule
Sebastian Popf6f77e92011-12-06 17:34:11 +0000220 // InsnClass in the current packet.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000221 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Popf6f77e92011-12-06 17:34:11 +0000222 // processed ResultingResourceState before.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000223 //
224 if ((ResultingResourceState != thisState) &&
225 (VisitedResourceStates.count(ResultingResourceState) == 0)) {
226 VisitedResourceStates.insert(ResultingResourceState);
227 PossibleStates.insert(ResultingResourceState);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000228 }
229 }
230 }
231 }
232
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000233}
234
235
236//
237// canAddInsnClass - Quickly verifies if an instruction of type InsnClass is a
238// valid transition from this state i.e., can an instruction of type InsnClass
239// be added to the packet represented by this state.
240//
241bool State::canAddInsnClass(unsigned InsnClass) const {
Alexey Samsonov87dc7a42012-06-28 07:47:50 +0000242 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000243 SI != stateInfo.end(); ++SI) {
244 if (~*SI & InsnClass)
245 return true;
246 }
247 return false;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000248}
249
250
251void DFA::initialize() {
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000252 assert(currentState && "Missing current state");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000253 currentState->isInitial = true;
254}
255
256
Sebastian Pop464f3a32011-12-06 17:34:16 +0000257void DFA::addState(State *S) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000258 assert(!states.count(S) && "State already exists");
259 states.insert(S);
260}
261
262
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000263int State::currentStateNum = 0;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000264
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000265DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000266 TargetName(CodeGenTarget(R).getName()),
267 allInsnClasses(), Records(R) {}
268
269
270//
271// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Popf6f77e92011-12-06 17:34:11 +0000272// associated API to create a DFA packetizer.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000273//
274// Format:
275// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Popf6f77e92011-12-06 17:34:11 +0000276// transitions.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000277// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Popf6f77e92011-12-06 17:34:11 +0000278// the ith state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000279//
280//
Sebastian Pop464f3a32011-12-06 17:34:16 +0000281void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) {
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000282 DFA::StateSet::iterator SI = states.begin();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000283 // This table provides a map to the beginning of the transitions for State s
Sebastian Popf6f77e92011-12-06 17:34:11 +0000284 // in DFAStateInputTable.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000285 std::vector<int> StateEntry(states.size());
286
287 OS << "namespace llvm {\n\n";
288 OS << "const int " << TargetName << "DFAStateInputTable[][2] = {\n";
289
290 // Tracks the total valid transitions encountered so far. It is used
Sebastian Popf6f77e92011-12-06 17:34:11 +0000291 // to construct the StateEntry table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000292 int ValidTransitions = 0;
293 for (unsigned i = 0; i < states.size(); ++i, ++SI) {
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000294 assert (((*SI)->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000295 StateEntry[i] = ValidTransitions;
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000296 for (State::TransitionMap::iterator
297 II = (*SI)->Transitions.begin(), IE = (*SI)->Transitions.end();
298 II != IE; ++II) {
299 OS << "{" << II->first << ", "
300 << II->second->stateNum
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000301 << "}, ";
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000302 }
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000303 ValidTransitions += (*SI)->Transitions.size();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000304
Sebastian Popf6f77e92011-12-06 17:34:11 +0000305 // If there are no valid transitions from this stage, we need a sentinel
306 // transition.
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000307 if (ValidTransitions == StateEntry[i]) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000308 OS << "{-1, -1},";
Brendon Cahoonffbd0712012-02-03 21:08:25 +0000309 ++ValidTransitions;
310 }
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000311
312 OS << "\n";
313 }
314 OS << "};\n\n";
315 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
316
317 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Popf6f77e92011-12-06 17:34:11 +0000318 // two numbers.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000319 for (unsigned i = 0; i < states.size(); ++i)
320 OS << StateEntry[i] << ", ";
321
322 OS << "\n};\n";
323 OS << "} // namespace\n";
324
325
326 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000327 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000328 //
329 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
330 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
331 OS << "namespace llvm {\n";
Sebastian Pop464f3a32011-12-06 17:34:16 +0000332 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000333 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
334 << " return new DFAPacketizer(IID, " << TargetName
335 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
336 OS << "} // End llvm namespace \n";
337}
338
339
340//
341// collectAllInsnClasses - Populate allInsnClasses which is a set of units
342// used in each stage.
343//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000344void DFAPacketizerEmitter::collectAllInsnClasses(const std::string &Name,
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000345 Record *ItinData,
346 unsigned &NStages,
347 raw_ostream &OS) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000348 // Collect processor itineraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000349 std::vector<Record*> ProcItinList =
Sebastian Popf6f77e92011-12-06 17:34:11 +0000350 Records.getAllDerivedDefinitions("ProcessorItineraries");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000351
Sebastian Popf6f77e92011-12-06 17:34:11 +0000352 // If just no itinerary then don't bother.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000353 if (ProcItinList.size() < 2)
354 return;
355 std::map<std::string, unsigned> NameToBitsMap;
356
357 // Parse functional units for all the itineraries.
358 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
359 Record *Proc = ProcItinList[i];
360 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
361
Sebastian Popf6f77e92011-12-06 17:34:11 +0000362 // Convert macros to bits for each stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000363 for (unsigned i = 0, N = FUs.size(); i < N; ++i)
364 NameToBitsMap[FUs[i]->getName()] = (unsigned) (1U << i);
365 }
366
367 const std::vector<Record*> &StageList =
368 ItinData->getValueAsListOfDefs("Stages");
369
Sebastian Popf6f77e92011-12-06 17:34:11 +0000370 // The number of stages.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000371 NStages = StageList.size();
372
Sebastian Popf6f77e92011-12-06 17:34:11 +0000373 // For each unit.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000374 unsigned UnitBitValue = 0;
375
Sebastian Popf6f77e92011-12-06 17:34:11 +0000376 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000377 for (unsigned i = 0; i < NStages; ++i) {
378 const Record *Stage = StageList[i];
379
Sebastian Popf6f77e92011-12-06 17:34:11 +0000380 // Get unit list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000381 const std::vector<Record*> &UnitList =
382 Stage->getValueAsListOfDefs("Units");
383
384 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Popf6f77e92011-12-06 17:34:11 +0000385 // Conduct bitwise or.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000386 std::string UnitName = UnitList[j]->getName();
387 assert(NameToBitsMap.count(UnitName));
388 UnitBitValue |= NameToBitsMap[UnitName];
389 }
390
391 if (UnitBitValue != 0)
392 allInsnClasses.insert(UnitBitValue);
393 }
394}
395
396
397//
Sebastian Popf6f77e92011-12-06 17:34:11 +0000398// Run the worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000399//
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000400void DFAPacketizerEmitter::run(raw_ostream &OS) {
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000401
Sebastian Popf6f77e92011-12-06 17:34:11 +0000402 // Collect processor iteraries.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000403 std::vector<Record*> ProcItinList =
404 Records.getAllDerivedDefinitions("ProcessorItineraries");
405
406 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000407 // Collect the instruction classes.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000408 //
409 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
410 Record *Proc = ProcItinList[i];
411
Sebastian Popf6f77e92011-12-06 17:34:11 +0000412 // Get processor itinerary name.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000413 const std::string &Name = Proc->getName();
414
Sebastian Popf6f77e92011-12-06 17:34:11 +0000415 // Skip default.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000416 if (Name == "NoItineraries")
417 continue;
418
Sebastian Popf6f77e92011-12-06 17:34:11 +0000419 // Sanity check for at least one instruction itinerary class.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000420 unsigned NItinClasses =
421 Records.getAllDerivedDefinitions("InstrItinClass").size();
422 if (NItinClasses == 0)
423 return;
424
Sebastian Popf6f77e92011-12-06 17:34:11 +0000425 // Get itinerary data list.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000426 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
427
Sebastian Popf6f77e92011-12-06 17:34:11 +0000428 // Collect instruction classes for all itinerary data.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000429 for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) {
430 Record *ItinData = ItinDataList[j];
431 unsigned NStages;
432 collectAllInsnClasses(Name, ItinData, NStages, OS);
433 }
434 }
435
436
437 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000438 // Run a worklist algorithm to generate the DFA.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000439 //
440 DFA D;
Sebastian Pop464f3a32011-12-06 17:34:16 +0000441 State *Initial = new State;
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000442 Initial->isInitial = true;
443 Initial->stateInfo.insert(0x0);
444 D.addState(Initial);
445 SmallVector<State*, 32> WorkList;
446 std::map<std::set<unsigned>, State*> Visited;
447
448 WorkList.push_back(Initial);
449
450 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000451 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000452 // C = {set of InsnClasses}
453 // Begin with initial node in worklist. Initial node does not have
454 // any consumed resources,
455 // ResourceState = 0x0
456 // Visited = {}
457 // While worklist != empty
458 // S = first element of worklist
459 // For every instruction class C
460 // if we can accommodate C in S:
461 // S' = state with resource states = {S Union C}
462 // Add a new transition: S x C -> S'
463 // If S' is not in Visited:
464 // Add S' to worklist
465 // Add S' to Visited
466 //
467 while (!WorkList.empty()) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000468 State *current = WorkList.pop_back_val();
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000469 for (DenseSet<unsigned>::iterator CI = allInsnClasses.begin(),
470 CE = allInsnClasses.end(); CI != CE; ++CI) {
471 unsigned InsnClass = *CI;
472
473 std::set<unsigned> NewStateResources;
474 //
475 // If we haven't already created a transition for this input
Sebastian Popf6f77e92011-12-06 17:34:11 +0000476 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000477 //
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000478 if (!current->hasTransition(InsnClass) &&
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000479 current->canAddInsnClass(InsnClass)) {
Sebastian Pop464f3a32011-12-06 17:34:16 +0000480 State *NewState = NULL;
Anshuman Dasguptae2529dc2012-06-27 19:38:29 +0000481 current->AddInsnClass(InsnClass, NewStateResources);
482 assert(NewStateResources.size() && "New states must be generated");
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000483
484 //
Sebastian Popf6f77e92011-12-06 17:34:11 +0000485 // If we have seen this state before, then do not create a new state.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000486 //
487 //
488 std::map<std::set<unsigned>, State*>::iterator VI;
489 if ((VI = Visited.find(NewStateResources)) != Visited.end())
490 NewState = VI->second;
491 else {
492 NewState = new State;
493 NewState->stateInfo = NewStateResources;
494 D.addState(NewState);
495 Visited[NewStateResources] = NewState;
496 WorkList.push_back(NewState);
497 }
Anshuman Dasgupta3adf3b02012-09-07 21:35:43 +0000498
499 current->addTransition(InsnClass, NewState);
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000500 }
501 }
502 }
503
Sebastian Popf6f77e92011-12-06 17:34:11 +0000504 // Print out the table.
Anshuman Dasguptadc81e5d2011-12-01 21:10:21 +0000505 D.writeTableAndAPI(OS, TargetName);
506}
Jakob Stoklund Olesen6f36fa92012-06-11 15:37:55 +0000507
508namespace llvm {
509
510void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
511 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
512 DFAPacketizerEmitter(RK).run(OS);
513}
514
515} // End llvm namespace