blob: dabcc8f8ed552b302b68748374bde358588350f7 [file] [log] [blame]
Eugene Zelenko6ac3f732016-01-26 18:48:36 +00001//===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class parses the Schedule.td file and produces an API that can be used
10// to reason about whether an instruction can be added to a packet on a VLIW
11// architecture. The class internally generates a deterministic finite
12// automaton (DFA) that models all possible mappings of machine instructions
13// to functional units as instructions are added to a packet.
14//
15//===----------------------------------------------------------------------===//
16
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000017#define DEBUG_TYPE "dfa-emitter"
18
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000019#include "CodeGenTarget.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000020#include "llvm/ADT/DenseSet.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000021#include "llvm/ADT/SmallVector.h"
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000022#include "llvm/ADT/StringExtras.h"
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000023#include "llvm/TableGen/Record.h"
24#include "llvm/TableGen/TableGenBackend.h"
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000025#include "llvm/Support/Debug.h"
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000026#include "llvm/Support/raw_ostream.h"
27#include <cassert>
28#include <cstdint>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000029#include <map>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000030#include <set>
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +000031#include <string>
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000032#include <vector>
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000033
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +000034using namespace llvm;
35
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000036// --------------------------------------------------------------------
37// Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
38
39// DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
40// This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
41//
42// e.g. terms x resource bit combinations that fit in uint32_t:
43// 4 terms x 8 bits = 32 bits
44// 3 terms x 10 bits = 30 bits
45// 2 terms x 16 bits = 32 bits
46//
47// e.g. terms x resource bit combinations that fit in uint64_t:
48// 8 terms x 8 bits = 64 bits
49// 7 terms x 9 bits = 63 bits
50// 6 terms x 10 bits = 60 bits
51// 5 terms x 12 bits = 60 bits
52// 4 terms x 16 bits = 64 bits <--- current
53// 3 terms x 21 bits = 63 bits
54// 2 terms x 32 bits = 64 bits
55//
56#define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
57#define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
58
59typedef uint64_t DFAInput;
60typedef int64_t DFAStateInput;
61#define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
62
63namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000064
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000065 DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) {
66 return (Inp << DFA_MAX_RESOURCES) | FuncUnits;
67 }
68
69 /// Return the DFAInput for an instruction class input vector.
70 /// This function is used in both DFAPacketizer.cpp and in
71 /// DFAPacketizerEmitter.cpp.
72 DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) {
73 DFAInput InsnInput = 0;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000074 assert((InsnClass.size() <= DFA_MAX_RESTERMS) &&
75 "Exceeded maximum number of DFA terms");
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000076 for (auto U : InsnClass)
77 InsnInput = addDFAFuncUnits(InsnInput, U);
78 return InsnInput;
79 }
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +000080
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000081} // end anonymous namespace
82
Krzysztof Parzyszek6753f332015-11-22 15:20:19 +000083// --------------------------------------------------------------------
84
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000085#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +000086// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
87//
88// dbgsInsnClass - When debugging, print instruction class stages.
89//
90void dbgsInsnClass(const std::vector<unsigned> &InsnClass);
91//
92// dbgsStateInfo - When debugging, print the set of state info.
93//
94void dbgsStateInfo(const std::set<unsigned> &stateInfo);
95//
96// dbgsIndent - When debugging, indent by the specified amount.
97//
98void dbgsIndent(unsigned indent);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +000099#endif
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000100
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000101//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000102// class DFAPacketizerEmitter: class that generates and prints out the DFA
103// for resource tracking.
104//
105namespace {
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000106
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000107class DFAPacketizerEmitter {
108private:
109 std::string TargetName;
110 //
111 // allInsnClasses is the set of all possible resources consumed by an
112 // InstrStage.
113 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000114 std::vector<std::vector<unsigned>> allInsnClasses;
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000115 RecordKeeper &Records;
116
117public:
118 DFAPacketizerEmitter(RecordKeeper &R);
119
120 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000121 // collectAllFuncUnits - Construct a map of function unit names to bits.
122 //
123 int collectAllFuncUnits(std::vector<Record*> &ProcItinList,
124 std::map<std::string, unsigned> &FUNameToBitsMap,
125 int &maxResources,
126 raw_ostream &OS);
127
128 //
129 // collectAllComboFuncs - Construct a map from a combo function unit bit to
130 // the bits of all included functional units.
131 //
132 int collectAllComboFuncs(std::vector<Record*> &ComboFuncList,
133 std::map<std::string, unsigned> &FUNameToBitsMap,
134 std::map<unsigned, unsigned> &ComboBitToBitsMap,
135 raw_ostream &OS);
136
137 //
138 // collectOneInsnClass - Populate allInsnClasses with one instruction class.
139 //
140 int collectOneInsnClass(const std::string &ProcName,
141 std::vector<Record*> &ProcItinList,
142 std::map<std::string, unsigned> &FUNameToBitsMap,
143 Record *ItinData,
144 raw_ostream &OS);
145
146 //
147 // collectAllInsnClasses - Populate allInsnClasses which is a set of units
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000148 // used in each stage.
149 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000150 int collectAllInsnClasses(const std::string &ProcName,
151 std::vector<Record*> &ProcItinList,
152 std::map<std::string, unsigned> &FUNameToBitsMap,
153 std::vector<Record*> &ItinDataList,
154 int &maxStages,
155 raw_ostream &OS);
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000156
157 void run(raw_ostream &OS);
158};
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000159
160//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000161// State represents the usage of machine resources if the packet contains
162// a set of instruction classes.
163//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000164// Specifically, currentState is a set of bit-masks.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000165// The nth bit in a bit-mask indicates whether the nth resource is being used
166// by this state. The set of bit-masks in a state represent the different
167// possible outcomes of transitioning to this state.
Sebastian Pop9aa61372011-12-06 17:34:11 +0000168// For example: consider a two resource architecture: resource L and resource M
169// with three instruction classes: L, M, and L_or_M.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000170// From the initial state (currentState = 0x00), if we add instruction class
171// L_or_M we will transition to a state with currentState = [0x01, 0x10]. This
172// represents the possible resource states that can result from adding a L_or_M
173// instruction
174//
175// Another way of thinking about this transition is we are mapping a NDFA with
Sebastian Pop9aa61372011-12-06 17:34:11 +0000176// two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10].
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000177//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000178// A State instance also contains a collection of transitions from that state:
179// a map from inputs to new states.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000180//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000181class State {
182 public:
183 static int currentStateNum;
David Blaikied43046b2014-04-21 22:35:11 +0000184 // stateNum is the only member used for equality/ordering, all other members
185 // can be mutated even in const State objects.
186 const int stateNum;
187 mutable bool isInitial;
188 mutable std::set<unsigned> stateInfo;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000189 typedef std::map<std::vector<unsigned>, const State *> TransitionMap;
David Blaikied43046b2014-04-21 22:35:11 +0000190 mutable TransitionMap Transitions;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000191
192 State();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000193
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000194 bool operator<(const State &s) const {
195 return stateNum < s.stateNum;
196 }
197
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000198 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000199 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
200 // may be a valid transition from this state i.e., can an instruction of type
201 // InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000202 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000203 // Note that for multiple stages, this quick check does not take into account
204 // any possible resource competition between the stages themselves. That is
205 // enforced in AddInsnClassStages which checks the cross product of all
206 // stages for resource availability (which is a more involved check).
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000207 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000208 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
209 std::map<unsigned, unsigned> &ComboBitToBitsMap) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000210
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000211 //
Krzysztof Parzyszek4ca21fc2015-11-21 17:38:33 +0000212 // AddInsnClass - Return all combinations of resource reservation
Krzysztof Parzyszek220a9bc2015-11-21 17:23:52 +0000213 // which are possible from this state (PossibleStates).
214 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000215 // PossibleStates is the set of valid resource states that ensue from valid
216 // transitions.
217 //
218 void AddInsnClass(std::vector<unsigned> &InsnClass,
219 std::map<unsigned, unsigned> &ComboBitToBitsMap,
220 std::set<unsigned> &PossibleStates) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000221
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000222 //
223 // AddInsnClassStages - Return all combinations of resource reservation
224 // resulting from the cross product of all stages for this InsnClass
225 // which are possible from this state (PossibleStates).
226 //
227 void AddInsnClassStages(std::vector<unsigned> &InsnClass,
228 std::map<unsigned, unsigned> &ComboBitToBitsMap,
229 unsigned chkstage, unsigned numstages,
230 unsigned prevState, unsigned origState,
231 DenseSet<unsigned> &VisitedResourceStates,
232 std::set<unsigned> &PossibleStates) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000233
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000234 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000235 // addTransition - Add a transition from this state given the input InsnClass
236 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000237 void addTransition(std::vector<unsigned> InsnClass, const State *To) const;
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000238
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000239 //
240 // hasTransition - Returns true if there is a transition from this state
241 // given the input InsnClass
242 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000243 bool hasTransition(std::vector<unsigned> InsnClass) const;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000244};
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000245
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000246//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000247// class DFA: deterministic finite automaton for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000248//
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000249class DFA {
250public:
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000251 DFA() = default;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000252
Sebastian Pop9aa61372011-12-06 17:34:11 +0000253 // Set of states. Need to keep this sorted to emit the transition table.
David Blaikied43046b2014-04-21 22:35:11 +0000254 typedef std::set<State> StateSet;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000255 StateSet states;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000256
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000257 State *currentState = nullptr;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000258
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000259 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000260 // Modify the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000261 //
David Blaikied43046b2014-04-21 22:35:11 +0000262 const State &newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000263
264 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000265 // writeTable: Print out a table representing the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000266 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000267 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName,
268 int numInsnClasses = 0,
269 int maxResources = 0, int numCombos = 0, int maxStages = 0);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000270};
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000271
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000272} // end anonymous namespace
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000273
Krzysztof Parzyszek8dd552d2015-11-21 22:19:50 +0000274#ifndef NDEBUG
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000275// To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter".
276//
277// dbgsInsnClass - When debugging, print instruction class stages.
278//
279void dbgsInsnClass(const std::vector<unsigned> &InsnClass) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000280 LLVM_DEBUG(dbgs() << "InsnClass: ");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000281 for (unsigned i = 0; i < InsnClass.size(); ++i) {
282 if (i > 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000283 LLVM_DEBUG(dbgs() << ", ");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000284 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000285 LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(InsnClass[i]));
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000286 }
287 DFAInput InsnInput = getDFAInsnInput(InsnClass);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000288 LLVM_DEBUG(dbgs() << " (input: 0x" << Twine::utohexstr(InsnInput) << ")");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000289}
290
291//
292// dbgsStateInfo - When debugging, print the set of state info.
293//
294void dbgsStateInfo(const std::set<unsigned> &stateInfo) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000295 LLVM_DEBUG(dbgs() << "StateInfo: ");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000296 unsigned i = 0;
297 for (std::set<unsigned>::iterator SI = stateInfo.begin();
298 SI != stateInfo.end(); ++SI, ++i) {
299 unsigned thisState = *SI;
300 if (i > 0) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000301 LLVM_DEBUG(dbgs() << ", ");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000302 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000303 LLVM_DEBUG(dbgs() << "0x" << Twine::utohexstr(thisState));
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000304 }
305}
306
307//
308// dbgsIndent - When debugging, indent by the specified amount.
309//
310void dbgsIndent(unsigned indent) {
311 for (unsigned i = 0; i < indent; ++i) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000312 LLVM_DEBUG(dbgs() << " ");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000313 }
314}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000315#endif // NDEBUG
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000316
317//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000318// Constructors and destructors for State and DFA
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000319//
320State::State() :
321 stateNum(currentStateNum++), isInitial(false) {}
322
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000323//
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000324// addTransition - Add a transition from this state given the input InsnClass
325//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000326void State::addTransition(std::vector<unsigned> InsnClass, const State *To)
327 const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000328 assert(!Transitions.count(InsnClass) &&
329 "Cannot have multiple transitions for the same input");
330 Transitions[InsnClass] = To;
331}
332
333//
334// hasTransition - Returns true if there is a transition from this state
335// given the input InsnClass
336//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000337bool State::hasTransition(std::vector<unsigned> InsnClass) const {
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000338 return Transitions.count(InsnClass) > 0;
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000339}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000340
341//
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000342// AddInsnClass - Return all combinations of resource reservation
343// which are possible from this state (PossibleStates).
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000344//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000345// PossibleStates is the set of valid resource states that ensue from valid
346// transitions.
347//
348void State::AddInsnClass(std::vector<unsigned> &InsnClass,
349 std::map<unsigned, unsigned> &ComboBitToBitsMap,
350 std::set<unsigned> &PossibleStates) const {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000351 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000352 // Iterate over all resource states in currentState.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000353 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000354 unsigned numstages = InsnClass.size();
355 assert((numstages > 0) && "InsnClass has no stages");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000356
357 for (std::set<unsigned>::iterator SI = stateInfo.begin();
358 SI != stateInfo.end(); ++SI) {
359 unsigned thisState = *SI;
360
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000361 DenseSet<unsigned> VisitedResourceStates;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000362
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000363 LLVM_DEBUG(dbgs() << " thisState: 0x" << Twine::utohexstr(thisState)
364 << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000365 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
366 numstages - 1, numstages,
367 thisState, thisState,
368 VisitedResourceStates, PossibleStates);
369 }
370}
371
372void State::AddInsnClassStages(std::vector<unsigned> &InsnClass,
373 std::map<unsigned, unsigned> &ComboBitToBitsMap,
374 unsigned chkstage, unsigned numstages,
375 unsigned prevState, unsigned origState,
376 DenseSet<unsigned> &VisitedResourceStates,
377 std::set<unsigned> &PossibleStates) const {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000378 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range");
379 unsigned thisStage = InsnClass[chkstage];
380
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000381 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000382 dbgsIndent((1 + numstages - chkstage) << 1);
383 dbgs() << "AddInsnClassStages " << chkstage << " (0x"
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000384 << Twine::utohexstr(thisStage) << ") from ";
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000385 dbgsInsnClass(InsnClass);
386 dbgs() << "\n";
387 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000388
389 //
390 // Iterate over all possible resources used in thisStage.
391 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}.
392 //
393 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) {
394 unsigned resourceMask = (0x1 << j);
395 if (resourceMask & thisStage) {
396 unsigned combo = ComboBitToBitsMap[resourceMask];
397 if (combo && ((~prevState & combo) != combo)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000398 LLVM_DEBUG(dbgs() << "\tSkipped Add 0x" << Twine::utohexstr(prevState)
399 << " - combo op 0x" << Twine::utohexstr(resourceMask)
400 << " (0x" << Twine::utohexstr(combo)
401 << ") cannot be scheduled\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000402 continue;
403 }
404 //
405 // For each possible resource used in thisStage, generate the
406 // resource state if that resource was used.
407 //
408 unsigned ResultingResourceState = prevState | resourceMask | combo;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000409 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000410 dbgsIndent((2 + numstages - chkstage) << 1);
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000411 dbgs() << "0x" << Twine::utohexstr(prevState) << " | 0x"
412 << Twine::utohexstr(resourceMask);
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000413 if (combo)
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000414 dbgs() << " | 0x" << Twine::utohexstr(combo);
415 dbgs() << " = 0x" << Twine::utohexstr(ResultingResourceState) << " ";
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000416 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000417
418 //
419 // If this is the final stage for this class
420 //
421 if (chkstage == 0) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000422 //
423 // Check if the resulting resource state can be accommodated in this
Sebastian Pop9aa61372011-12-06 17:34:11 +0000424 // packet.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000425 // We compute resource OR prevState (originally started as origState).
426 // If the result of the OR is different than origState, it implies
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000427 // that there is at least one resource that can be used to schedule
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000428 // thisStage in the current packet.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000429 // Insert ResultingResourceState into PossibleStates only if we haven't
Sebastian Pop9aa61372011-12-06 17:34:11 +0000430 // processed ResultingResourceState before.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000431 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000432 if (ResultingResourceState != prevState) {
433 if (VisitedResourceStates.count(ResultingResourceState) == 0) {
434 VisitedResourceStates.insert(ResultingResourceState);
435 PossibleStates.insert(ResultingResourceState);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000436 LLVM_DEBUG(dbgs()
437 << "\tResultingResourceState: 0x"
438 << Twine::utohexstr(ResultingResourceState) << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000439 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000440 LLVM_DEBUG(dbgs() << "\tSkipped Add - state already seen\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000441 }
442 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000443 LLVM_DEBUG(dbgs()
444 << "\tSkipped Add - no final resources available\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000445 }
446 } else {
447 //
448 // If the current resource can be accommodated, check the next
449 // stage in InsnClass for available resources.
450 //
451 if (ResultingResourceState != prevState) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000452 LLVM_DEBUG(dbgs() << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000453 AddInsnClassStages(InsnClass, ComboBitToBitsMap,
454 chkstage - 1, numstages,
455 ResultingResourceState, origState,
456 VisitedResourceStates, PossibleStates);
457 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000458 LLVM_DEBUG(dbgs() << "\tSkipped Add - no resources available\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000459 }
460 }
461 }
462 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000463}
464
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000465//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000466// canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass
467// may be a valid transition from this state i.e., can an instruction of type
468// InsnClass be added to the packet represented by this state.
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000469//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000470// Note that this routine is performing conservative checks that can be
471// quickly executed acting as a filter before calling AddInsnClassStages.
472// Any cases allowed through here will be caught later in AddInsnClassStages
473// which performs the more expensive exact check.
474//
475bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass,
476 std::map<unsigned, unsigned> &ComboBitToBitsMap) const {
Alexey Samsonov420a4ed2012-06-28 07:47:50 +0000477 for (std::set<unsigned>::const_iterator SI = stateInfo.begin();
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000478 SI != stateInfo.end(); ++SI) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000479 // Check to see if all required resources are available.
480 bool available = true;
481
482 // Inspect each stage independently.
483 // note: This is a conservative check as we aren't checking for
484 // possible resource competition between the stages themselves
485 // The full cross product is examined later in AddInsnClass.
486 for (unsigned i = 0; i < InsnClass.size(); ++i) {
487 unsigned resources = *SI;
488 if ((~resources & InsnClass[i]) == 0) {
489 available = false;
490 break;
491 }
492 // Make sure _all_ resources for a combo function are available.
493 // note: This is a quick conservative check as it won't catch an
494 // unscheduleable combo if this stage is an OR expression
495 // containing a combo.
496 // These cases are caught later in AddInsnClass.
497 unsigned combo = ComboBitToBitsMap[InsnClass[i]];
498 if (combo && ((~resources & combo) != combo)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000499 LLVM_DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x"
500 << Twine::utohexstr(resources) << " - combo op 0x"
501 << Twine::utohexstr(InsnClass[i]) << " (0x"
502 << Twine::utohexstr(combo)
503 << ") cannot be scheduled\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000504 available = false;
505 break;
506 }
507 }
508
509 if (available) {
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000510 return true;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000511 }
Anshuman Dasgupta20013f12012-06-27 19:38:29 +0000512 }
513 return false;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000514}
515
David Blaikied43046b2014-04-21 22:35:11 +0000516const State &DFA::newState() {
David Blaikie4bcfcc12014-04-21 22:46:09 +0000517 auto IterPair = states.insert(State());
David Blaikied43046b2014-04-21 22:35:11 +0000518 assert(IterPair.second && "State already exists");
519 return *IterPair.first;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000520}
521
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000522int State::currentStateNum = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000523
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000524DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R):
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000525 TargetName(CodeGenTarget(R).getName()), Records(R) {}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000526
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000527//
528// writeTableAndAPI - Print out a table representing the DFA and the
Sebastian Pop9aa61372011-12-06 17:34:11 +0000529// associated API to create a DFA packetizer.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000530//
531// Format:
532// DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
Sebastian Pop9aa61372011-12-06 17:34:11 +0000533// transitions.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000534// DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for
Sebastian Pop9aa61372011-12-06 17:34:11 +0000535// the ith state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000536//
537//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000538void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName,
539 int numInsnClasses,
540 int maxResources, int numCombos, int maxStages) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000541 unsigned numStates = states.size();
542
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000543 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
544 "----------------------\n");
545 LLVM_DEBUG(dbgs() << "writeTableAndAPI\n");
546 LLVM_DEBUG(dbgs() << "Total states: " << numStates << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000547
548 OS << "namespace llvm {\n";
549
550 OS << "\n// Input format:\n";
551 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS
552 << "\t// maximum AND'ed resource terms\n";
553 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES
554 << "\t// maximum resource bits in one term\n";
555
556 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = "
557 << "pairs of <Input, NextState> for all valid\n";
558 OS << "// transitions.\n";
559 OS << "// " << numStates << "\tstates\n";
560 OS << "// " << numInsnClasses << "\tinstruction classes\n";
561 OS << "// " << maxResources << "\tresources max\n";
562 OS << "// " << numCombos << "\tcombo resources\n";
563 OS << "// " << maxStages << "\tstages max\n";
564 OS << "const " << DFA_TBLTYPE << " "
565 << TargetName << "DFAStateInputTable[][2] = {\n";
566
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000567 // This table provides a map to the beginning of the transitions for State s
Sebastian Pop9aa61372011-12-06 17:34:11 +0000568 // in DFAStateInputTable.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000569 std::vector<int> StateEntry(numStates+1);
570 static const std::string SentinelEntry = "{-1, -1}";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000571
572 // Tracks the total valid transitions encountered so far. It is used
Sebastian Pop9aa61372011-12-06 17:34:11 +0000573 // to construct the StateEntry table.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000574 int ValidTransitions = 0;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000575 DFA::StateSet::iterator SI = states.begin();
576 for (unsigned i = 0; i < numStates; ++i, ++SI) {
David Blaikied43046b2014-04-21 22:35:11 +0000577 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000578 StateEntry[i] = ValidTransitions;
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000579 for (State::TransitionMap::iterator
David Blaikied43046b2014-04-21 22:35:11 +0000580 II = SI->Transitions.begin(), IE = SI->Transitions.end();
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000581 II != IE; ++II) {
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000582 OS << "{0x" << Twine::utohexstr(getDFAInsnInput(II->first)) << ", "
583 << II->second->stateNum << "},\t";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000584 }
David Blaikied43046b2014-04-21 22:35:11 +0000585 ValidTransitions += SI->Transitions.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000586
Sebastian Pop9aa61372011-12-06 17:34:11 +0000587 // If there are no valid transitions from this stage, we need a sentinel
588 // transition.
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000589 if (ValidTransitions == StateEntry[i]) {
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000590 OS << SentinelEntry << ",\t";
Brendon Cahoone9b60aa2012-02-03 21:08:25 +0000591 ++ValidTransitions;
592 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000593
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000594 OS << " // state " << i << ": " << StateEntry[i];
595 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition.
596 OS << "-" << (ValidTransitions-1);
597 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000598 OS << "\n";
599 }
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000600
601 // Print out a sentinel entry at the end of the StateInputTable. This is
602 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable()
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000603 OS << SentinelEntry << "\t";
604 OS << " // state " << numStates << ": " << ValidTransitions;
605 OS << "\n";
606
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000607 OS << "};\n\n";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000608 OS << "// " << TargetName << "DFAStateEntryTable[i] = "
609 << "Index of the first entry in DFAStateInputTable for\n";
610 OS << "// "
611 << "the ith state.\n";
612 OS << "// " << numStates << " states\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000613 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n";
614
615 // Multiply i by 2 since each entry in DFAStateInputTable is a set of
Sebastian Pop9aa61372011-12-06 17:34:11 +0000616 // two numbers.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000617 unsigned lastState = 0;
618 for (unsigned i = 0; i < numStates; ++i) {
619 if (i && ((i % 10) == 0)) {
620 lastState = i-1;
621 OS << " // states " << (i-10) << ":" << lastState << "\n";
622 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000623 OS << StateEntry[i] << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000624 }
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000625
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000626 // Print out the index to the sentinel entry in StateInputTable
627 OS << ValidTransitions << ", ";
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000628 OS << " // states " << (lastState+1) << ":" << numStates << "\n";
Anshuman Dasgupta3923e282012-12-10 22:45:57 +0000629
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000630 OS << "};\n";
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000631 OS << "} // namespace\n";
632
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000633 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000634 // Emit DFA Packetizer tables if the target is a VLIW machine.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000635 //
636 std::string SubTargetClassName = TargetName + "GenSubtargetInfo";
637 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n";
638 OS << "namespace llvm {\n";
Sebastian Popac35a4d2011-12-06 17:34:16 +0000639 OS << "DFAPacketizer *" << SubTargetClassName << "::"
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000640 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n"
641 << " return new DFAPacketizer(IID, " << TargetName
642 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n";
643 OS << "} // End llvm namespace \n";
644}
645
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000646//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000647// collectAllFuncUnits - Construct a map of function unit names to bits.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000648//
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000649int DFAPacketizerEmitter::collectAllFuncUnits(
650 std::vector<Record*> &ProcItinList,
651 std::map<std::string, unsigned> &FUNameToBitsMap,
652 int &maxFUs,
653 raw_ostream &OS) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000654 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
655 "----------------------\n");
656 LLVM_DEBUG(dbgs() << "collectAllFuncUnits");
657 LLVM_DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000658
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000659 int totalFUs = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000660 // Parse functional units for all the itineraries.
661 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) {
662 Record *Proc = ProcItinList[i];
663 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU");
664
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000665 LLVM_DEBUG(dbgs() << " FU:" << i << " (" << FUs.size() << " FUs) "
666 << Proc->getName());
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000667
Sebastian Pop9aa61372011-12-06 17:34:11 +0000668 // Convert macros to bits for each stage.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000669 unsigned numFUs = FUs.size();
670 for (unsigned j = 0; j < numFUs; ++j) {
671 assert ((j < DFA_MAX_RESOURCES) &&
672 "Exceeded maximum number of representable resources");
673 unsigned FuncResources = (unsigned) (1U << j);
674 FUNameToBitsMap[FUs[j]->getName()] = FuncResources;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000675 LLVM_DEBUG(dbgs() << " " << FUs[j]->getName() << ":0x"
676 << Twine::utohexstr(FuncResources));
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000677 }
678 if (((int) numFUs) > maxFUs) {
679 maxFUs = numFUs;
680 }
681 totalFUs += numFUs;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000682 LLVM_DEBUG(dbgs() << "\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000683 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000684 return totalFUs;
685}
686
687//
688// collectAllComboFuncs - Construct a map from a combo function unit bit to
689// the bits of all included functional units.
690//
691int DFAPacketizerEmitter::collectAllComboFuncs(
692 std::vector<Record*> &ComboFuncList,
693 std::map<std::string, unsigned> &FUNameToBitsMap,
694 std::map<unsigned, unsigned> &ComboBitToBitsMap,
695 raw_ostream &OS) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000696 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
697 "----------------------\n");
698 LLVM_DEBUG(dbgs() << "collectAllComboFuncs");
699 LLVM_DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000700
701 int numCombos = 0;
702 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) {
703 Record *Func = ComboFuncList[i];
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000704 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD");
705
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000706 LLVM_DEBUG(dbgs() << " CFD:" << i << " (" << FUs.size() << " combo FUs) "
707 << Func->getName() << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000708
709 // Convert macros to bits for each stage.
710 for (unsigned j = 0, N = FUs.size(); j < N; ++j) {
711 assert ((j < DFA_MAX_RESOURCES) &&
712 "Exceeded maximum number of DFA resources");
713 Record *FuncData = FUs[j];
714 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc");
715 const std::vector<Record*> &FuncList =
716 FuncData->getValueAsListOfDefs("FuncList");
Benjamin Kramer4ca41fd2016-06-12 17:30:47 +0000717 const std::string &ComboFuncName = ComboFunc->getName();
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000718 unsigned ComboBit = FUNameToBitsMap[ComboFuncName];
719 unsigned ComboResources = ComboBit;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000720 LLVM_DEBUG(dbgs() << " combo: " << ComboFuncName << ":0x"
721 << Twine::utohexstr(ComboResources) << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000722 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) {
723 std::string FuncName = FuncList[k]->getName();
724 unsigned FuncResources = FUNameToBitsMap[FuncName];
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000725 LLVM_DEBUG(dbgs() << " " << FuncName << ":0x"
726 << Twine::utohexstr(FuncResources) << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000727 ComboResources |= FuncResources;
728 }
729 ComboBitToBitsMap[ComboBit] = ComboResources;
730 numCombos++;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000731 LLVM_DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x"
732 << Twine::utohexstr(ComboBit) << " = 0x"
733 << Twine::utohexstr(ComboResources) << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000734 }
735 }
736 return numCombos;
737}
738
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000739//
740// collectOneInsnClass - Populate allInsnClasses with one instruction class
741//
742int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName,
743 std::vector<Record*> &ProcItinList,
744 std::map<std::string, unsigned> &FUNameToBitsMap,
745 Record *ItinData,
746 raw_ostream &OS) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000747 const std::vector<Record*> &StageList =
748 ItinData->getValueAsListOfDefs("Stages");
749
Sebastian Pop9aa61372011-12-06 17:34:11 +0000750 // The number of stages.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000751 unsigned NStages = StageList.size();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000752
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000753 LLVM_DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName()
754 << "\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000755
756 std::vector<unsigned> UnitBits;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000757
Sebastian Pop9aa61372011-12-06 17:34:11 +0000758 // Compute the bitwise or of each unit used in this stage.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000759 for (unsigned i = 0; i < NStages; ++i) {
760 const Record *Stage = StageList[i];
761
Sebastian Pop9aa61372011-12-06 17:34:11 +0000762 // Get unit list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000763 const std::vector<Record*> &UnitList =
764 Stage->getValueAsListOfDefs("Units");
765
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000766 LLVM_DEBUG(dbgs() << " stage:" << i << " [" << UnitList.size()
767 << " units]:");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000768 unsigned dbglen = 26; // cursor after stage dbgs
769
770 // Compute the bitwise or of each unit used in this stage.
771 unsigned UnitBitValue = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000772 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000773 // Conduct bitwise or.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000774 std::string UnitName = UnitList[j]->getName();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000775 LLVM_DEBUG(dbgs() << " " << j << ":" << UnitName);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000776 dbglen += 3 + UnitName.length();
777 assert(FUNameToBitsMap.count(UnitName));
778 UnitBitValue |= FUNameToBitsMap[UnitName];
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000779 }
780
781 if (UnitBitValue != 0)
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000782 UnitBits.push_back(UnitBitValue);
783
784 while (dbglen <= 64) { // line up bits dbgs
785 dbglen += 8;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000786 LLVM_DEBUG(dbgs() << "\t");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000787 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000788 LLVM_DEBUG(dbgs() << " (bits: 0x" << Twine::utohexstr(UnitBitValue)
789 << ")\n");
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000790 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000791
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000792 if (!UnitBits.empty())
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000793 allInsnClasses.push_back(UnitBits);
794
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000795 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000796 dbgs() << " ";
797 dbgsInsnClass(UnitBits);
798 dbgs() << "\n";
799 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000800
801 return NStages;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000802}
803
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000804//
805// collectAllInsnClasses - Populate allInsnClasses which is a set of units
806// used in each stage.
807//
808int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName,
809 std::vector<Record*> &ProcItinList,
810 std::map<std::string, unsigned> &FUNameToBitsMap,
811 std::vector<Record*> &ItinDataList,
812 int &maxStages,
813 raw_ostream &OS) {
814 // Collect all instruction classes.
815 unsigned M = ItinDataList.size();
816
817 int numInsnClasses = 0;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000818 LLVM_DEBUG(dbgs() << "-------------------------------------------------------"
819 "----------------------\n"
820 << "collectAllInsnClasses " << ProcName << " (" << M
821 << " classes)\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000822
823 // Collect stages for each instruction class for all itinerary data
824 for (unsigned j = 0; j < M; j++) {
825 Record *ItinData = ItinDataList[j];
826 int NStages = collectOneInsnClass(ProcName, ProcItinList,
827 FUNameToBitsMap, ItinData, OS);
828 if (NStages > maxStages) {
829 maxStages = NStages;
830 }
831 numInsnClasses++;
832 }
833 return numInsnClasses;
834}
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000835
836//
Sebastian Pop9aa61372011-12-06 17:34:11 +0000837// Run the worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000838//
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000839void DFAPacketizerEmitter::run(raw_ostream &OS) {
Sebastian Pop9aa61372011-12-06 17:34:11 +0000840 // Collect processor iteraries.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000841 std::vector<Record*> ProcItinList =
842 Records.getAllDerivedDefinitions("ProcessorItineraries");
843
844 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000845 // Collect the Functional units.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000846 //
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000847 std::map<std::string, unsigned> FUNameToBitsMap;
848 int maxResources = 0;
849 collectAllFuncUnits(ProcItinList,
850 FUNameToBitsMap, maxResources, OS);
851
852 //
853 // Collect the Combo Functional units.
854 //
855 std::map<unsigned, unsigned> ComboBitToBitsMap;
856 std::vector<Record*> ComboFuncList =
857 Records.getAllDerivedDefinitions("ComboFuncUnits");
858 int numCombos = collectAllComboFuncs(ComboFuncList,
859 FUNameToBitsMap, ComboBitToBitsMap, OS);
860
861 //
862 // Collect the itineraries.
863 //
864 int maxStages = 0;
865 int numInsnClasses = 0;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000866 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) {
867 Record *Proc = ProcItinList[i];
868
Sebastian Pop9aa61372011-12-06 17:34:11 +0000869 // Get processor itinerary name.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000870 const std::string &ProcName = Proc->getName();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000871
Sebastian Pop9aa61372011-12-06 17:34:11 +0000872 // Skip default.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000873 if (ProcName == "NoItineraries")
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000874 continue;
875
Sebastian Pop9aa61372011-12-06 17:34:11 +0000876 // Sanity check for at least one instruction itinerary class.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000877 unsigned NItinClasses =
878 Records.getAllDerivedDefinitions("InstrItinClass").size();
879 if (NItinClasses == 0)
880 return;
881
Sebastian Pop9aa61372011-12-06 17:34:11 +0000882 // Get itinerary data list.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000883 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID");
884
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000885 // Collect all instruction classes
886 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList,
887 FUNameToBitsMap, ItinDataList, maxStages, OS);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000888 }
889
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000890 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000891 // Run a worklist algorithm to generate the DFA.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000892 //
893 DFA D;
David Blaikied43046b2014-04-21 22:35:11 +0000894 const State *Initial = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000895 Initial->isInitial = true;
896 Initial->stateInfo.insert(0x0);
David Blaikied43046b2014-04-21 22:35:11 +0000897 SmallVector<const State*, 32> WorkList;
898 std::map<std::set<unsigned>, const State*> Visited;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000899
900 WorkList.push_back(Initial);
901
902 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000903 // Worklist algorithm to create a DFA for processor resource tracking.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000904 // C = {set of InsnClasses}
905 // Begin with initial node in worklist. Initial node does not have
906 // any consumed resources,
907 // ResourceState = 0x0
908 // Visited = {}
909 // While worklist != empty
910 // S = first element of worklist
911 // For every instruction class C
912 // if we can accommodate C in S:
913 // S' = state with resource states = {S Union C}
914 // Add a new transition: S x C -> S'
915 // If S' is not in Visited:
916 // Add S' to worklist
917 // Add S' to Visited
918 //
919 while (!WorkList.empty()) {
David Blaikied43046b2014-04-21 22:35:11 +0000920 const State *current = WorkList.pop_back_val();
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000921 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000922 dbgs() << "---------------------\n";
923 dbgs() << "Processing state: " << current->stateNum << " - ";
924 dbgsStateInfo(current->stateInfo);
925 dbgs() << "\n";
926 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000927 for (unsigned i = 0; i < allInsnClasses.size(); i++) {
928 std::vector<unsigned> InsnClass = allInsnClasses[i];
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000929 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000930 dbgs() << i << " ";
931 dbgsInsnClass(InsnClass);
932 dbgs() << "\n";
933 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000934
935 std::set<unsigned> NewStateResources;
936 //
937 // If we haven't already created a transition for this input
Sebastian Pop9aa61372011-12-06 17:34:11 +0000938 // and the state can accommodate this InsnClass, create a transition.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000939 //
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000940 if (!current->hasTransition(InsnClass) &&
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000941 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000942 const State *NewState = nullptr;
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000943 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources);
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000944 if (NewStateResources.empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000945 LLVM_DEBUG(dbgs() << " Skipped - no new states generated\n");
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000946 continue;
947 }
948
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000949 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000950 dbgs() << "\t";
951 dbgsStateInfo(NewStateResources);
952 dbgs() << "\n";
953 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000954
955 //
Sebastian Pop9aa61372011-12-06 17:34:11 +0000956 // If we have seen this state before, then do not create a new state.
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000957 //
David Blaikied43046b2014-04-21 22:35:11 +0000958 auto VI = Visited.find(NewStateResources);
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000959 if (VI != Visited.end()) {
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000960 NewState = VI->second;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000961 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000962 dbgs() << "\tFound existing state: " << NewState->stateNum
963 << " - ";
964 dbgsStateInfo(NewState->stateInfo);
965 dbgs() << "\n";
966 });
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000967 } else {
David Blaikied43046b2014-04-21 22:35:11 +0000968 NewState = &D.newState();
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000969 NewState->stateInfo = NewStateResources;
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000970 Visited[NewStateResources] = NewState;
971 WorkList.push_back(NewState);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000972 LLVM_DEBUG({
Krzysztof Parzyszekdd135242015-11-21 22:46:52 +0000973 dbgs() << "\tAccepted new state: " << NewState->stateNum << " - ";
974 dbgsStateInfo(NewState->stateInfo);
975 dbgs() << "\n";
976 });
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000977 }
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000978
Anshuman Dasguptaf16a4432012-09-07 21:35:43 +0000979 current->addTransition(InsnClass, NewState);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000980 }
981 }
982 }
983
Sebastian Pop9aa61372011-12-06 17:34:11 +0000984 // Print out the table.
Krzysztof Parzyszekb4655722015-11-21 20:00:45 +0000985 D.writeTableAndAPI(OS, TargetName,
986 numInsnClasses, maxResources, numCombos, maxStages);
Anshuman Dasgupta08ebdc12011-12-01 21:10:21 +0000987}
Jakob Stoklund Olesene6aed132012-06-11 15:37:55 +0000988
989namespace llvm {
990
991void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) {
992 emitSourceFileHeader("Target DFA Packetizer Tables", OS);
993 DFAPacketizerEmitter(RK).run(OS);
994}
995
Eugene Zelenkoa3fe70d2016-11-30 17:48:10 +0000996} // end namespace llvm