Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 1 | //===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine ----===// |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 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 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "dfa-emitter" |
| 19 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 20 | #include "CodeGenTarget.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseSet.h" |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 24 | #include "llvm/TableGen/Record.h" |
| 25 | #include "llvm/TableGen/TableGenBackend.h" |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 27 | #include <map> |
| 28 | #include <string> |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 29 | #include <queue> |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 30 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Krzysztof Parzyszek | 6753f33 | 2015-11-22 15:20:19 +0000 | [diff] [blame] | 33 | // -------------------------------------------------------------------- |
| 34 | // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp |
| 35 | |
| 36 | // DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput. |
| 37 | // This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer. |
| 38 | // |
| 39 | // e.g. terms x resource bit combinations that fit in uint32_t: |
| 40 | // 4 terms x 8 bits = 32 bits |
| 41 | // 3 terms x 10 bits = 30 bits |
| 42 | // 2 terms x 16 bits = 32 bits |
| 43 | // |
| 44 | // e.g. terms x resource bit combinations that fit in uint64_t: |
| 45 | // 8 terms x 8 bits = 64 bits |
| 46 | // 7 terms x 9 bits = 63 bits |
| 47 | // 6 terms x 10 bits = 60 bits |
| 48 | // 5 terms x 12 bits = 60 bits |
| 49 | // 4 terms x 16 bits = 64 bits <--- current |
| 50 | // 3 terms x 21 bits = 63 bits |
| 51 | // 2 terms x 32 bits = 64 bits |
| 52 | // |
| 53 | #define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms. |
| 54 | #define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term. |
| 55 | |
| 56 | typedef uint64_t DFAInput; |
| 57 | typedef int64_t DFAStateInput; |
| 58 | #define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable. |
| 59 | |
| 60 | namespace { |
| 61 | DFAInput addDFAFuncUnits(DFAInput Inp, unsigned FuncUnits) { |
| 62 | return (Inp << DFA_MAX_RESOURCES) | FuncUnits; |
| 63 | } |
| 64 | |
| 65 | /// Return the DFAInput for an instruction class input vector. |
| 66 | /// This function is used in both DFAPacketizer.cpp and in |
| 67 | /// DFAPacketizerEmitter.cpp. |
| 68 | DFAInput getDFAInsnInput(const std::vector<unsigned> &InsnClass) { |
| 69 | DFAInput InsnInput = 0; |
| 70 | assert ((InsnClass.size() <= DFA_MAX_RESTERMS) && |
| 71 | "Exceeded maximum number of DFA terms"); |
| 72 | for (auto U : InsnClass) |
| 73 | InsnInput = addDFAFuncUnits(InsnInput, U); |
| 74 | return InsnInput; |
| 75 | } |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 76 | } // end anonymous namespace |
| 77 | |
Krzysztof Parzyszek | 6753f33 | 2015-11-22 15:20:19 +0000 | [diff] [blame] | 78 | // -------------------------------------------------------------------- |
| 79 | |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 80 | #ifndef NDEBUG |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 81 | // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter". |
| 82 | // |
| 83 | // dbgsInsnClass - When debugging, print instruction class stages. |
| 84 | // |
| 85 | void dbgsInsnClass(const std::vector<unsigned> &InsnClass); |
| 86 | // |
| 87 | // dbgsStateInfo - When debugging, print the set of state info. |
| 88 | // |
| 89 | void dbgsStateInfo(const std::set<unsigned> &stateInfo); |
| 90 | // |
| 91 | // dbgsIndent - When debugging, indent by the specified amount. |
| 92 | // |
| 93 | void dbgsIndent(unsigned indent); |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 94 | #endif |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 95 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 96 | // |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 97 | // class DFAPacketizerEmitter: class that generates and prints out the DFA |
| 98 | // for resource tracking. |
| 99 | // |
| 100 | namespace { |
| 101 | class DFAPacketizerEmitter { |
| 102 | private: |
| 103 | std::string TargetName; |
| 104 | // |
| 105 | // allInsnClasses is the set of all possible resources consumed by an |
| 106 | // InstrStage. |
| 107 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 108 | std::vector<std::vector<unsigned>> allInsnClasses; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 109 | RecordKeeper &Records; |
| 110 | |
| 111 | public: |
| 112 | DFAPacketizerEmitter(RecordKeeper &R); |
| 113 | |
| 114 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 115 | // collectAllFuncUnits - Construct a map of function unit names to bits. |
| 116 | // |
| 117 | int collectAllFuncUnits(std::vector<Record*> &ProcItinList, |
| 118 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 119 | int &maxResources, |
| 120 | raw_ostream &OS); |
| 121 | |
| 122 | // |
| 123 | // collectAllComboFuncs - Construct a map from a combo function unit bit to |
| 124 | // the bits of all included functional units. |
| 125 | // |
| 126 | int collectAllComboFuncs(std::vector<Record*> &ComboFuncList, |
| 127 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 128 | std::map<unsigned, unsigned> &ComboBitToBitsMap, |
| 129 | raw_ostream &OS); |
| 130 | |
| 131 | // |
| 132 | // collectOneInsnClass - Populate allInsnClasses with one instruction class. |
| 133 | // |
| 134 | int collectOneInsnClass(const std::string &ProcName, |
| 135 | std::vector<Record*> &ProcItinList, |
| 136 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 137 | Record *ItinData, |
| 138 | raw_ostream &OS); |
| 139 | |
| 140 | // |
| 141 | // collectAllInsnClasses - Populate allInsnClasses which is a set of units |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 142 | // used in each stage. |
| 143 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 144 | int collectAllInsnClasses(const std::string &ProcName, |
| 145 | std::vector<Record*> &ProcItinList, |
| 146 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 147 | std::vector<Record*> &ItinDataList, |
| 148 | int &maxStages, |
| 149 | raw_ostream &OS); |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 150 | |
| 151 | void run(raw_ostream &OS); |
| 152 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 153 | } // end anonymous namespace |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 154 | |
| 155 | // |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 156 | // |
| 157 | // State represents the usage of machine resources if the packet contains |
| 158 | // a set of instruction classes. |
| 159 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 160 | // Specifically, currentState is a set of bit-masks. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 161 | // The nth bit in a bit-mask indicates whether the nth resource is being used |
| 162 | // by this state. The set of bit-masks in a state represent the different |
| 163 | // possible outcomes of transitioning to this state. |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 164 | // For example: consider a two resource architecture: resource L and resource M |
| 165 | // with three instruction classes: L, M, and L_or_M. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 166 | // From the initial state (currentState = 0x00), if we add instruction class |
| 167 | // L_or_M we will transition to a state with currentState = [0x01, 0x10]. This |
| 168 | // represents the possible resource states that can result from adding a L_or_M |
| 169 | // instruction |
| 170 | // |
| 171 | // Another way of thinking about this transition is we are mapping a NDFA with |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 172 | // two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10]. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 173 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 174 | // A State instance also contains a collection of transitions from that state: |
| 175 | // a map from inputs to new states. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 176 | // |
| 177 | namespace { |
| 178 | class State { |
| 179 | public: |
| 180 | static int currentStateNum; |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 181 | // stateNum is the only member used for equality/ordering, all other members |
| 182 | // can be mutated even in const State objects. |
| 183 | const int stateNum; |
| 184 | mutable bool isInitial; |
| 185 | mutable std::set<unsigned> stateInfo; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 186 | typedef std::map<std::vector<unsigned>, const State *> TransitionMap; |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 187 | mutable TransitionMap Transitions; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 188 | |
| 189 | State(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 190 | |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 191 | bool operator<(const State &s) const { |
| 192 | return stateNum < s.stateNum; |
| 193 | } |
| 194 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 195 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 196 | // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass |
| 197 | // may be a valid transition from this state i.e., can an instruction of type |
| 198 | // InsnClass be added to the packet represented by this state. |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 199 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 200 | // Note that for multiple stages, this quick check does not take into account |
| 201 | // any possible resource competition between the stages themselves. That is |
| 202 | // enforced in AddInsnClassStages which checks the cross product of all |
| 203 | // stages for resource availability (which is a more involved check). |
Krzysztof Parzyszek | 220a9bc | 2015-11-21 17:23:52 +0000 | [diff] [blame] | 204 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 205 | bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass, |
| 206 | std::map<unsigned, unsigned> &ComboBitToBitsMap) const; |
Krzysztof Parzyszek | 220a9bc | 2015-11-21 17:23:52 +0000 | [diff] [blame] | 207 | // |
Krzysztof Parzyszek | 4ca21fc | 2015-11-21 17:38:33 +0000 | [diff] [blame] | 208 | // AddInsnClass - Return all combinations of resource reservation |
Krzysztof Parzyszek | 220a9bc | 2015-11-21 17:23:52 +0000 | [diff] [blame] | 209 | // which are possible from this state (PossibleStates). |
| 210 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 211 | // PossibleStates is the set of valid resource states that ensue from valid |
| 212 | // transitions. |
| 213 | // |
| 214 | void AddInsnClass(std::vector<unsigned> &InsnClass, |
| 215 | std::map<unsigned, unsigned> &ComboBitToBitsMap, |
| 216 | std::set<unsigned> &PossibleStates) const; |
| 217 | // |
| 218 | // AddInsnClassStages - Return all combinations of resource reservation |
| 219 | // resulting from the cross product of all stages for this InsnClass |
| 220 | // which are possible from this state (PossibleStates). |
| 221 | // |
| 222 | void AddInsnClassStages(std::vector<unsigned> &InsnClass, |
| 223 | std::map<unsigned, unsigned> &ComboBitToBitsMap, |
| 224 | unsigned chkstage, unsigned numstages, |
| 225 | unsigned prevState, unsigned origState, |
| 226 | DenseSet<unsigned> &VisitedResourceStates, |
| 227 | std::set<unsigned> &PossibleStates) const; |
| 228 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 229 | // addTransition - Add a transition from this state given the input InsnClass |
| 230 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 231 | void addTransition(std::vector<unsigned> InsnClass, const State *To) const; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 232 | // |
| 233 | // hasTransition - Returns true if there is a transition from this state |
| 234 | // given the input InsnClass |
| 235 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 236 | bool hasTransition(std::vector<unsigned> InsnClass) const; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 237 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 238 | } // end anonymous namespace |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 239 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 240 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 241 | // class DFA: deterministic finite automaton for processor resource tracking. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 242 | // |
| 243 | namespace { |
| 244 | class DFA { |
| 245 | public: |
| 246 | DFA(); |
| 247 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 248 | // Set of states. Need to keep this sorted to emit the transition table. |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 249 | typedef std::set<State> StateSet; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 250 | StateSet states; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 251 | |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 252 | State *currentState; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 253 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 254 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 255 | // Modify the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 256 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 257 | const State &newState(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 258 | |
| 259 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 260 | // writeTable: Print out a table representing the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 261 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 262 | void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName, |
| 263 | int numInsnClasses = 0, |
| 264 | int maxResources = 0, int numCombos = 0, int maxStages = 0); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 265 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 266 | } // end anonymous namespace |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 267 | |
Krzysztof Parzyszek | 8dd552d | 2015-11-21 22:19:50 +0000 | [diff] [blame] | 268 | #ifndef NDEBUG |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 269 | // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter". |
| 270 | // |
| 271 | // dbgsInsnClass - When debugging, print instruction class stages. |
| 272 | // |
| 273 | void dbgsInsnClass(const std::vector<unsigned> &InsnClass) { |
| 274 | DEBUG(dbgs() << "InsnClass: "); |
| 275 | for (unsigned i = 0; i < InsnClass.size(); ++i) { |
| 276 | if (i > 0) { |
| 277 | DEBUG(dbgs() << ", "); |
| 278 | } |
| 279 | DEBUG(dbgs() << "0x" << utohexstr(InsnClass[i])); |
| 280 | } |
| 281 | DFAInput InsnInput = getDFAInsnInput(InsnClass); |
| 282 | DEBUG(dbgs() << " (input: 0x" << utohexstr(InsnInput) << ")"); |
| 283 | } |
| 284 | |
| 285 | // |
| 286 | // dbgsStateInfo - When debugging, print the set of state info. |
| 287 | // |
| 288 | void dbgsStateInfo(const std::set<unsigned> &stateInfo) { |
| 289 | DEBUG(dbgs() << "StateInfo: "); |
| 290 | unsigned i = 0; |
| 291 | for (std::set<unsigned>::iterator SI = stateInfo.begin(); |
| 292 | SI != stateInfo.end(); ++SI, ++i) { |
| 293 | unsigned thisState = *SI; |
| 294 | if (i > 0) { |
| 295 | DEBUG(dbgs() << ", "); |
| 296 | } |
| 297 | DEBUG(dbgs() << "0x" << utohexstr(thisState)); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // |
| 302 | // dbgsIndent - When debugging, indent by the specified amount. |
| 303 | // |
| 304 | void dbgsIndent(unsigned indent) { |
| 305 | for (unsigned i = 0; i < indent; ++i) { |
| 306 | DEBUG(dbgs() << " "); |
| 307 | } |
| 308 | } |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 309 | #endif // NDEBUG |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 310 | |
| 311 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 312 | // Constructors and destructors for State and DFA |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 313 | // |
| 314 | State::State() : |
| 315 | stateNum(currentStateNum++), isInitial(false) {} |
| 316 | |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 317 | DFA::DFA(): currentState(nullptr) {} |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 318 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 319 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 320 | // addTransition - Add a transition from this state given the input InsnClass |
| 321 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 322 | void State::addTransition(std::vector<unsigned> InsnClass, const State *To) |
| 323 | const { |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 324 | assert(!Transitions.count(InsnClass) && |
| 325 | "Cannot have multiple transitions for the same input"); |
| 326 | Transitions[InsnClass] = To; |
| 327 | } |
| 328 | |
| 329 | // |
| 330 | // hasTransition - Returns true if there is a transition from this state |
| 331 | // given the input InsnClass |
| 332 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 333 | bool State::hasTransition(std::vector<unsigned> InsnClass) const { |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 334 | return Transitions.count(InsnClass) > 0; |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 335 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 336 | |
| 337 | // |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 338 | // AddInsnClass - Return all combinations of resource reservation |
| 339 | // which are possible from this state (PossibleStates). |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 340 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 341 | // PossibleStates is the set of valid resource states that ensue from valid |
| 342 | // transitions. |
| 343 | // |
| 344 | void State::AddInsnClass(std::vector<unsigned> &InsnClass, |
| 345 | std::map<unsigned, unsigned> &ComboBitToBitsMap, |
| 346 | std::set<unsigned> &PossibleStates) const { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 347 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 348 | // Iterate over all resource states in currentState. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 349 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 350 | unsigned numstages = InsnClass.size(); |
| 351 | assert((numstages > 0) && "InsnClass has no stages"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 352 | |
| 353 | for (std::set<unsigned>::iterator SI = stateInfo.begin(); |
| 354 | SI != stateInfo.end(); ++SI) { |
| 355 | unsigned thisState = *SI; |
| 356 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 357 | DenseSet<unsigned> VisitedResourceStates; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 358 | |
| 359 | DEBUG(dbgs() << " thisState: 0x" << utohexstr(thisState) << "\n"); |
| 360 | AddInsnClassStages(InsnClass, ComboBitToBitsMap, |
| 361 | numstages - 1, numstages, |
| 362 | thisState, thisState, |
| 363 | VisitedResourceStates, PossibleStates); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void State::AddInsnClassStages(std::vector<unsigned> &InsnClass, |
| 368 | std::map<unsigned, unsigned> &ComboBitToBitsMap, |
| 369 | unsigned chkstage, unsigned numstages, |
| 370 | unsigned prevState, unsigned origState, |
| 371 | DenseSet<unsigned> &VisitedResourceStates, |
| 372 | std::set<unsigned> &PossibleStates) const { |
| 373 | |
| 374 | assert((chkstage < numstages) && "AddInsnClassStages: stage out of range"); |
| 375 | unsigned thisStage = InsnClass[chkstage]; |
| 376 | |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 377 | DEBUG({ |
| 378 | dbgsIndent((1 + numstages - chkstage) << 1); |
| 379 | dbgs() << "AddInsnClassStages " << chkstage << " (0x" |
| 380 | << utohexstr(thisStage) << ") from "; |
| 381 | dbgsInsnClass(InsnClass); |
| 382 | dbgs() << "\n"; |
| 383 | }); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 384 | |
| 385 | // |
| 386 | // Iterate over all possible resources used in thisStage. |
| 387 | // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}. |
| 388 | // |
| 389 | for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) { |
| 390 | unsigned resourceMask = (0x1 << j); |
| 391 | if (resourceMask & thisStage) { |
| 392 | unsigned combo = ComboBitToBitsMap[resourceMask]; |
| 393 | if (combo && ((~prevState & combo) != combo)) { |
| 394 | DEBUG(dbgs() << "\tSkipped Add 0x" << utohexstr(prevState) |
| 395 | << " - combo op 0x" << utohexstr(resourceMask) |
| 396 | << " (0x" << utohexstr(combo) <<") cannot be scheduled\n"); |
| 397 | continue; |
| 398 | } |
| 399 | // |
| 400 | // For each possible resource used in thisStage, generate the |
| 401 | // resource state if that resource was used. |
| 402 | // |
| 403 | unsigned ResultingResourceState = prevState | resourceMask | combo; |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 404 | DEBUG({ |
| 405 | dbgsIndent((2 + numstages - chkstage) << 1); |
| 406 | dbgs() << "0x" << utohexstr(prevState) |
| 407 | << " | 0x" << utohexstr(resourceMask); |
| 408 | if (combo) |
| 409 | dbgs() << " | 0x" << utohexstr(combo); |
| 410 | dbgs() << " = 0x" << utohexstr(ResultingResourceState) << " "; |
| 411 | }); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 412 | |
| 413 | // |
| 414 | // If this is the final stage for this class |
| 415 | // |
| 416 | if (chkstage == 0) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 417 | // |
| 418 | // Check if the resulting resource state can be accommodated in this |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 419 | // packet. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 420 | // We compute resource OR prevState (originally started as origState). |
| 421 | // If the result of the OR is different than origState, it implies |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 422 | // that there is at least one resource that can be used to schedule |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 423 | // thisStage in the current packet. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 424 | // Insert ResultingResourceState into PossibleStates only if we haven't |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 425 | // processed ResultingResourceState before. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 426 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 427 | if (ResultingResourceState != prevState) { |
| 428 | if (VisitedResourceStates.count(ResultingResourceState) == 0) { |
| 429 | VisitedResourceStates.insert(ResultingResourceState); |
| 430 | PossibleStates.insert(ResultingResourceState); |
| 431 | DEBUG(dbgs() << "\tResultingResourceState: 0x" |
| 432 | << utohexstr(ResultingResourceState) << "\n"); |
| 433 | } else { |
| 434 | DEBUG(dbgs() << "\tSkipped Add - state already seen\n"); |
| 435 | } |
| 436 | } else { |
| 437 | DEBUG(dbgs() << "\tSkipped Add - no final resources available\n"); |
| 438 | } |
| 439 | } else { |
| 440 | // |
| 441 | // If the current resource can be accommodated, check the next |
| 442 | // stage in InsnClass for available resources. |
| 443 | // |
| 444 | if (ResultingResourceState != prevState) { |
| 445 | DEBUG(dbgs() << "\n"); |
| 446 | AddInsnClassStages(InsnClass, ComboBitToBitsMap, |
| 447 | chkstage - 1, numstages, |
| 448 | ResultingResourceState, origState, |
| 449 | VisitedResourceStates, PossibleStates); |
| 450 | } else { |
| 451 | DEBUG(dbgs() << "\tSkipped Add - no resources available\n"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 458 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 459 | // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass |
| 460 | // may be a valid transition from this state i.e., can an instruction of type |
| 461 | // InsnClass be added to the packet represented by this state. |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 462 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 463 | // Note that this routine is performing conservative checks that can be |
| 464 | // quickly executed acting as a filter before calling AddInsnClassStages. |
| 465 | // Any cases allowed through here will be caught later in AddInsnClassStages |
| 466 | // which performs the more expensive exact check. |
| 467 | // |
| 468 | bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass, |
| 469 | std::map<unsigned, unsigned> &ComboBitToBitsMap) const { |
Alexey Samsonov | 420a4ed | 2012-06-28 07:47:50 +0000 | [diff] [blame] | 470 | for (std::set<unsigned>::const_iterator SI = stateInfo.begin(); |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 471 | SI != stateInfo.end(); ++SI) { |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 472 | |
| 473 | // Check to see if all required resources are available. |
| 474 | bool available = true; |
| 475 | |
| 476 | // Inspect each stage independently. |
| 477 | // note: This is a conservative check as we aren't checking for |
| 478 | // possible resource competition between the stages themselves |
| 479 | // The full cross product is examined later in AddInsnClass. |
| 480 | for (unsigned i = 0; i < InsnClass.size(); ++i) { |
| 481 | unsigned resources = *SI; |
| 482 | if ((~resources & InsnClass[i]) == 0) { |
| 483 | available = false; |
| 484 | break; |
| 485 | } |
| 486 | // Make sure _all_ resources for a combo function are available. |
| 487 | // note: This is a quick conservative check as it won't catch an |
| 488 | // unscheduleable combo if this stage is an OR expression |
| 489 | // containing a combo. |
| 490 | // These cases are caught later in AddInsnClass. |
| 491 | unsigned combo = ComboBitToBitsMap[InsnClass[i]]; |
| 492 | if (combo && ((~resources & combo) != combo)) { |
| 493 | DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x" << utohexstr(resources) |
| 494 | << " - combo op 0x" << utohexstr(InsnClass[i]) |
| 495 | << " (0x" << utohexstr(combo) <<") cannot be scheduled\n"); |
| 496 | available = false; |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | if (available) { |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 502 | return true; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 503 | } |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 504 | } |
| 505 | return false; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 506 | } |
| 507 | |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 508 | const State &DFA::newState() { |
David Blaikie | 4bcfcc1 | 2014-04-21 22:46:09 +0000 | [diff] [blame] | 509 | auto IterPair = states.insert(State()); |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 510 | assert(IterPair.second && "State already exists"); |
| 511 | return *IterPair.first; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 514 | int State::currentStateNum = 0; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 515 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 516 | DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R): |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 517 | TargetName(CodeGenTarget(R).getName()), |
| 518 | allInsnClasses(), Records(R) {} |
| 519 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 520 | // |
| 521 | // writeTableAndAPI - Print out a table representing the DFA and the |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 522 | // associated API to create a DFA packetizer. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 523 | // |
| 524 | // Format: |
| 525 | // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 526 | // transitions. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 527 | // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 528 | // the ith state. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 529 | // |
| 530 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 531 | void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName, |
| 532 | int numInsnClasses, |
| 533 | int maxResources, int numCombos, int maxStages) { |
| 534 | |
| 535 | unsigned numStates = states.size(); |
| 536 | |
| 537 | DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"); |
| 538 | DEBUG(dbgs() << "writeTableAndAPI\n"); |
| 539 | DEBUG(dbgs() << "Total states: " << numStates << "\n"); |
| 540 | |
| 541 | OS << "namespace llvm {\n"; |
| 542 | |
| 543 | OS << "\n// Input format:\n"; |
| 544 | OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS |
| 545 | << "\t// maximum AND'ed resource terms\n"; |
| 546 | OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES |
| 547 | << "\t// maximum resource bits in one term\n"; |
| 548 | |
| 549 | OS << "\n// " << TargetName << "DFAStateInputTable[][2] = " |
| 550 | << "pairs of <Input, NextState> for all valid\n"; |
| 551 | OS << "// transitions.\n"; |
| 552 | OS << "// " << numStates << "\tstates\n"; |
| 553 | OS << "// " << numInsnClasses << "\tinstruction classes\n"; |
| 554 | OS << "// " << maxResources << "\tresources max\n"; |
| 555 | OS << "// " << numCombos << "\tcombo resources\n"; |
| 556 | OS << "// " << maxStages << "\tstages max\n"; |
| 557 | OS << "const " << DFA_TBLTYPE << " " |
| 558 | << TargetName << "DFAStateInputTable[][2] = {\n"; |
| 559 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 560 | // This table provides a map to the beginning of the transitions for State s |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 561 | // in DFAStateInputTable. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 562 | std::vector<int> StateEntry(numStates+1); |
| 563 | static const std::string SentinelEntry = "{-1, -1}"; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 564 | |
| 565 | // Tracks the total valid transitions encountered so far. It is used |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 566 | // to construct the StateEntry table. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 567 | int ValidTransitions = 0; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 568 | DFA::StateSet::iterator SI = states.begin(); |
| 569 | for (unsigned i = 0; i < numStates; ++i, ++SI) { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 570 | assert ((SI->stateNum == (int) i) && "Mismatch in state numbers"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 571 | StateEntry[i] = ValidTransitions; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 572 | for (State::TransitionMap::iterator |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 573 | II = SI->Transitions.begin(), IE = SI->Transitions.end(); |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 574 | II != IE; ++II) { |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 575 | OS << "{0x" << utohexstr(getDFAInsnInput(II->first)) << ", " |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 576 | << II->second->stateNum |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 577 | << "},\t"; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 578 | } |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 579 | ValidTransitions += SI->Transitions.size(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 580 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 581 | // If there are no valid transitions from this stage, we need a sentinel |
| 582 | // transition. |
Brendon Cahoon | e9b60aa | 2012-02-03 21:08:25 +0000 | [diff] [blame] | 583 | if (ValidTransitions == StateEntry[i]) { |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 584 | OS << SentinelEntry << ",\t"; |
Brendon Cahoon | e9b60aa | 2012-02-03 21:08:25 +0000 | [diff] [blame] | 585 | ++ValidTransitions; |
| 586 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 587 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 588 | OS << " // state " << i << ": " << StateEntry[i]; |
| 589 | if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition. |
| 590 | OS << "-" << (ValidTransitions-1); |
| 591 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 592 | OS << "\n"; |
| 593 | } |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 594 | |
| 595 | // Print out a sentinel entry at the end of the StateInputTable. This is |
| 596 | // needed to iterate over StateInputTable in DFAPacketizer::ReadTable() |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 597 | OS << SentinelEntry << "\t"; |
| 598 | OS << " // state " << numStates << ": " << ValidTransitions; |
| 599 | OS << "\n"; |
| 600 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 601 | OS << "};\n\n"; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 602 | OS << "// " << TargetName << "DFAStateEntryTable[i] = " |
| 603 | << "Index of the first entry in DFAStateInputTable for\n"; |
| 604 | OS << "// " |
| 605 | << "the ith state.\n"; |
| 606 | OS << "// " << numStates << " states\n"; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 607 | OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n"; |
| 608 | |
| 609 | // Multiply i by 2 since each entry in DFAStateInputTable is a set of |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 610 | // two numbers. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 611 | unsigned lastState = 0; |
| 612 | for (unsigned i = 0; i < numStates; ++i) { |
| 613 | if (i && ((i % 10) == 0)) { |
| 614 | lastState = i-1; |
| 615 | OS << " // states " << (i-10) << ":" << lastState << "\n"; |
| 616 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 617 | OS << StateEntry[i] << ", "; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 618 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 619 | |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 620 | // Print out the index to the sentinel entry in StateInputTable |
| 621 | OS << ValidTransitions << ", "; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 622 | OS << " // states " << (lastState+1) << ":" << numStates << "\n"; |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 623 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 624 | OS << "};\n"; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 625 | OS << "} // namespace\n"; |
| 626 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 627 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 628 | // Emit DFA Packetizer tables if the target is a VLIW machine. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 629 | // |
| 630 | std::string SubTargetClassName = TargetName + "GenSubtargetInfo"; |
| 631 | OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n"; |
| 632 | OS << "namespace llvm {\n"; |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 633 | OS << "DFAPacketizer *" << SubTargetClassName << "::" |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 634 | << "createDFAPacketizer(const InstrItineraryData *IID) const {\n" |
| 635 | << " return new DFAPacketizer(IID, " << TargetName |
| 636 | << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n"; |
| 637 | OS << "} // End llvm namespace \n"; |
| 638 | } |
| 639 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 640 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 641 | // collectAllFuncUnits - Construct a map of function unit names to bits. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 642 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 643 | int DFAPacketizerEmitter::collectAllFuncUnits( |
| 644 | std::vector<Record*> &ProcItinList, |
| 645 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 646 | int &maxFUs, |
| 647 | raw_ostream &OS) { |
| 648 | DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"); |
| 649 | DEBUG(dbgs() << "collectAllFuncUnits"); |
| 650 | DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 651 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 652 | int totalFUs = 0; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 653 | // Parse functional units for all the itineraries. |
| 654 | for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) { |
| 655 | Record *Proc = ProcItinList[i]; |
| 656 | std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU"); |
| 657 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 658 | DEBUG(dbgs() << " FU:" << i |
| 659 | << " (" << FUs.size() << " FUs) " |
Krzysztof Parzyszek | 8dd552d | 2015-11-21 22:19:50 +0000 | [diff] [blame] | 660 | << Proc->getName()); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 661 | |
| 662 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 663 | // Convert macros to bits for each stage. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 664 | unsigned numFUs = FUs.size(); |
| 665 | for (unsigned j = 0; j < numFUs; ++j) { |
| 666 | assert ((j < DFA_MAX_RESOURCES) && |
| 667 | "Exceeded maximum number of representable resources"); |
| 668 | unsigned FuncResources = (unsigned) (1U << j); |
| 669 | FUNameToBitsMap[FUs[j]->getName()] = FuncResources; |
| 670 | DEBUG(dbgs() << " " << FUs[j]->getName() |
| 671 | << ":0x" << utohexstr(FuncResources)); |
| 672 | } |
| 673 | if (((int) numFUs) > maxFUs) { |
| 674 | maxFUs = numFUs; |
| 675 | } |
| 676 | totalFUs += numFUs; |
| 677 | DEBUG(dbgs() << "\n"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 678 | } |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 679 | return totalFUs; |
| 680 | } |
| 681 | |
| 682 | // |
| 683 | // collectAllComboFuncs - Construct a map from a combo function unit bit to |
| 684 | // the bits of all included functional units. |
| 685 | // |
| 686 | int DFAPacketizerEmitter::collectAllComboFuncs( |
| 687 | std::vector<Record*> &ComboFuncList, |
| 688 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 689 | std::map<unsigned, unsigned> &ComboBitToBitsMap, |
| 690 | raw_ostream &OS) { |
| 691 | DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"); |
| 692 | DEBUG(dbgs() << "collectAllComboFuncs"); |
| 693 | DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n"); |
| 694 | |
| 695 | int numCombos = 0; |
| 696 | for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) { |
| 697 | Record *Func = ComboFuncList[i]; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 698 | std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD"); |
| 699 | |
| 700 | DEBUG(dbgs() << " CFD:" << i |
| 701 | << " (" << FUs.size() << " combo FUs) " |
Krzysztof Parzyszek | 8dd552d | 2015-11-21 22:19:50 +0000 | [diff] [blame] | 702 | << Func->getName() << "\n"); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 703 | |
| 704 | // Convert macros to bits for each stage. |
| 705 | for (unsigned j = 0, N = FUs.size(); j < N; ++j) { |
| 706 | assert ((j < DFA_MAX_RESOURCES) && |
| 707 | "Exceeded maximum number of DFA resources"); |
| 708 | Record *FuncData = FUs[j]; |
| 709 | Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc"); |
| 710 | const std::vector<Record*> &FuncList = |
| 711 | FuncData->getValueAsListOfDefs("FuncList"); |
| 712 | std::string ComboFuncName = ComboFunc->getName(); |
| 713 | unsigned ComboBit = FUNameToBitsMap[ComboFuncName]; |
| 714 | unsigned ComboResources = ComboBit; |
| 715 | DEBUG(dbgs() << " combo: " << ComboFuncName |
| 716 | << ":0x" << utohexstr(ComboResources) << "\n"); |
| 717 | for (unsigned k = 0, M = FuncList.size(); k < M; ++k) { |
| 718 | std::string FuncName = FuncList[k]->getName(); |
| 719 | unsigned FuncResources = FUNameToBitsMap[FuncName]; |
| 720 | DEBUG(dbgs() << " " << FuncName |
| 721 | << ":0x" << utohexstr(FuncResources) << "\n"); |
| 722 | ComboResources |= FuncResources; |
| 723 | } |
| 724 | ComboBitToBitsMap[ComboBit] = ComboResources; |
| 725 | numCombos++; |
| 726 | DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x" |
| 727 | << utohexstr(ComboBit) << " = 0x" |
| 728 | << utohexstr(ComboResources) << "\n"); |
| 729 | } |
| 730 | } |
| 731 | return numCombos; |
| 732 | } |
| 733 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 734 | // |
| 735 | // collectOneInsnClass - Populate allInsnClasses with one instruction class |
| 736 | // |
| 737 | int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName, |
| 738 | std::vector<Record*> &ProcItinList, |
| 739 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 740 | Record *ItinData, |
| 741 | raw_ostream &OS) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 742 | const std::vector<Record*> &StageList = |
| 743 | ItinData->getValueAsListOfDefs("Stages"); |
| 744 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 745 | // The number of stages. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 746 | unsigned NStages = StageList.size(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 747 | |
Krzysztof Parzyszek | 8dd552d | 2015-11-21 22:19:50 +0000 | [diff] [blame] | 748 | DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName() |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 749 | << "\n"); |
| 750 | |
| 751 | std::vector<unsigned> UnitBits; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 752 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 753 | // Compute the bitwise or of each unit used in this stage. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 754 | for (unsigned i = 0; i < NStages; ++i) { |
| 755 | const Record *Stage = StageList[i]; |
| 756 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 757 | // Get unit list. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 758 | const std::vector<Record*> &UnitList = |
| 759 | Stage->getValueAsListOfDefs("Units"); |
| 760 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 761 | DEBUG(dbgs() << " stage:" << i |
| 762 | << " [" << UnitList.size() << " units]:"); |
| 763 | unsigned dbglen = 26; // cursor after stage dbgs |
| 764 | |
| 765 | // Compute the bitwise or of each unit used in this stage. |
| 766 | unsigned UnitBitValue = 0; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 767 | for (unsigned j = 0, M = UnitList.size(); j < M; ++j) { |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 768 | // Conduct bitwise or. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 769 | std::string UnitName = UnitList[j]->getName(); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 770 | DEBUG(dbgs() << " " << j << ":" << UnitName); |
| 771 | dbglen += 3 + UnitName.length(); |
| 772 | assert(FUNameToBitsMap.count(UnitName)); |
| 773 | UnitBitValue |= FUNameToBitsMap[UnitName]; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (UnitBitValue != 0) |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 777 | UnitBits.push_back(UnitBitValue); |
| 778 | |
| 779 | while (dbglen <= 64) { // line up bits dbgs |
| 780 | dbglen += 8; |
| 781 | DEBUG(dbgs() << "\t"); |
| 782 | } |
| 783 | DEBUG(dbgs() << " (bits: 0x" << utohexstr(UnitBitValue) << ")\n"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 784 | } |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 785 | |
| 786 | if (UnitBits.size() > 0) |
| 787 | allInsnClasses.push_back(UnitBits); |
| 788 | |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 789 | DEBUG({ |
| 790 | dbgs() << " "; |
| 791 | dbgsInsnClass(UnitBits); |
| 792 | dbgs() << "\n"; |
| 793 | }); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 794 | |
| 795 | return NStages; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 798 | // |
| 799 | // collectAllInsnClasses - Populate allInsnClasses which is a set of units |
| 800 | // used in each stage. |
| 801 | // |
| 802 | int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName, |
| 803 | std::vector<Record*> &ProcItinList, |
| 804 | std::map<std::string, unsigned> &FUNameToBitsMap, |
| 805 | std::vector<Record*> &ItinDataList, |
| 806 | int &maxStages, |
| 807 | raw_ostream &OS) { |
| 808 | // Collect all instruction classes. |
| 809 | unsigned M = ItinDataList.size(); |
| 810 | |
| 811 | int numInsnClasses = 0; |
| 812 | DEBUG(dbgs() << "-----------------------------------------------------------------------------\n" |
| 813 | << "collectAllInsnClasses " |
| 814 | << ProcName |
| 815 | << " (" << M << " classes)\n"); |
| 816 | |
| 817 | // Collect stages for each instruction class for all itinerary data |
| 818 | for (unsigned j = 0; j < M; j++) { |
| 819 | Record *ItinData = ItinDataList[j]; |
| 820 | int NStages = collectOneInsnClass(ProcName, ProcItinList, |
| 821 | FUNameToBitsMap, ItinData, OS); |
| 822 | if (NStages > maxStages) { |
| 823 | maxStages = NStages; |
| 824 | } |
| 825 | numInsnClasses++; |
| 826 | } |
| 827 | return numInsnClasses; |
| 828 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 829 | |
| 830 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 831 | // Run the worklist algorithm to generate the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 832 | // |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 833 | void DFAPacketizerEmitter::run(raw_ostream &OS) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 834 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 835 | // Collect processor iteraries. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 836 | std::vector<Record*> ProcItinList = |
| 837 | Records.getAllDerivedDefinitions("ProcessorItineraries"); |
| 838 | |
| 839 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 840 | // Collect the Functional units. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 841 | // |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 842 | std::map<std::string, unsigned> FUNameToBitsMap; |
| 843 | int maxResources = 0; |
| 844 | collectAllFuncUnits(ProcItinList, |
| 845 | FUNameToBitsMap, maxResources, OS); |
| 846 | |
| 847 | // |
| 848 | // Collect the Combo Functional units. |
| 849 | // |
| 850 | std::map<unsigned, unsigned> ComboBitToBitsMap; |
| 851 | std::vector<Record*> ComboFuncList = |
| 852 | Records.getAllDerivedDefinitions("ComboFuncUnits"); |
| 853 | int numCombos = collectAllComboFuncs(ComboFuncList, |
| 854 | FUNameToBitsMap, ComboBitToBitsMap, OS); |
| 855 | |
| 856 | // |
| 857 | // Collect the itineraries. |
| 858 | // |
| 859 | int maxStages = 0; |
| 860 | int numInsnClasses = 0; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 861 | for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) { |
| 862 | Record *Proc = ProcItinList[i]; |
| 863 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 864 | // Get processor itinerary name. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 865 | const std::string &ProcName = Proc->getName(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 866 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 867 | // Skip default. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 868 | if (ProcName == "NoItineraries") |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 869 | continue; |
| 870 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 871 | // Sanity check for at least one instruction itinerary class. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 872 | unsigned NItinClasses = |
| 873 | Records.getAllDerivedDefinitions("InstrItinClass").size(); |
| 874 | if (NItinClasses == 0) |
| 875 | return; |
| 876 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 877 | // Get itinerary data list. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 878 | std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID"); |
| 879 | |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 880 | // Collect all instruction classes |
| 881 | numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList, |
| 882 | FUNameToBitsMap, ItinDataList, maxStages, OS); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 885 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 886 | // Run a worklist algorithm to generate the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 887 | // |
| 888 | DFA D; |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 889 | const State *Initial = &D.newState(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 890 | Initial->isInitial = true; |
| 891 | Initial->stateInfo.insert(0x0); |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 892 | SmallVector<const State*, 32> WorkList; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 893 | // std::queue<State*> WorkList; |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 894 | std::map<std::set<unsigned>, const State*> Visited; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 895 | |
| 896 | WorkList.push_back(Initial); |
| 897 | |
| 898 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 899 | // Worklist algorithm to create a DFA for processor resource tracking. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 900 | // C = {set of InsnClasses} |
| 901 | // Begin with initial node in worklist. Initial node does not have |
| 902 | // any consumed resources, |
| 903 | // ResourceState = 0x0 |
| 904 | // Visited = {} |
| 905 | // While worklist != empty |
| 906 | // S = first element of worklist |
| 907 | // For every instruction class C |
| 908 | // if we can accommodate C in S: |
| 909 | // S' = state with resource states = {S Union C} |
| 910 | // Add a new transition: S x C -> S' |
| 911 | // If S' is not in Visited: |
| 912 | // Add S' to worklist |
| 913 | // Add S' to Visited |
| 914 | // |
| 915 | while (!WorkList.empty()) { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 916 | const State *current = WorkList.pop_back_val(); |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 917 | DEBUG({ |
| 918 | dbgs() << "---------------------\n"; |
| 919 | dbgs() << "Processing state: " << current->stateNum << " - "; |
| 920 | dbgsStateInfo(current->stateInfo); |
| 921 | dbgs() << "\n"; |
| 922 | }); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 923 | for (unsigned i = 0; i < allInsnClasses.size(); i++) { |
| 924 | std::vector<unsigned> InsnClass = allInsnClasses[i]; |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 925 | DEBUG({ |
| 926 | dbgs() << i << " "; |
| 927 | dbgsInsnClass(InsnClass); |
| 928 | dbgs() << "\n"; |
| 929 | }); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 930 | |
| 931 | std::set<unsigned> NewStateResources; |
| 932 | // |
| 933 | // If we haven't already created a transition for this input |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 934 | // and the state can accommodate this InsnClass, create a transition. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 935 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 936 | if (!current->hasTransition(InsnClass) && |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 937 | current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) { |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 938 | const State *NewState = nullptr; |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 939 | current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources); |
| 940 | if (NewStateResources.size() == 0) { |
| 941 | DEBUG(dbgs() << " Skipped - no new states generated\n"); |
| 942 | continue; |
| 943 | } |
| 944 | |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 945 | DEBUG({ |
| 946 | dbgs() << "\t"; |
| 947 | dbgsStateInfo(NewStateResources); |
| 948 | dbgs() << "\n"; |
| 949 | }); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 950 | |
| 951 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 952 | // If we have seen this state before, then do not create a new state. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 953 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 954 | auto VI = Visited.find(NewStateResources); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 955 | if (VI != Visited.end()) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 956 | NewState = VI->second; |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 957 | DEBUG({ |
| 958 | dbgs() << "\tFound existing state: " << NewState->stateNum |
| 959 | << " - "; |
| 960 | dbgsStateInfo(NewState->stateInfo); |
| 961 | dbgs() << "\n"; |
| 962 | }); |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 963 | } else { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame] | 964 | NewState = &D.newState(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 965 | NewState->stateInfo = NewStateResources; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 966 | Visited[NewStateResources] = NewState; |
| 967 | WorkList.push_back(NewState); |
Krzysztof Parzyszek | dd13524 | 2015-11-21 22:46:52 +0000 | [diff] [blame] | 968 | DEBUG({ |
| 969 | dbgs() << "\tAccepted new state: " << NewState->stateNum << " - "; |
| 970 | dbgsStateInfo(NewState->stateInfo); |
| 971 | dbgs() << "\n"; |
| 972 | }); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 973 | } |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 974 | |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 975 | current->addTransition(InsnClass, NewState); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 980 | // Print out the table. |
Krzysztof Parzyszek | b465572 | 2015-11-21 20:00:45 +0000 | [diff] [blame] | 981 | D.writeTableAndAPI(OS, TargetName, |
| 982 | numInsnClasses, maxResources, numCombos, maxStages); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 983 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 984 | |
| 985 | namespace llvm { |
| 986 | |
| 987 | void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) { |
| 988 | emitSourceFileHeader("Target DFA Packetizer Tables", OS); |
| 989 | DFAPacketizerEmitter(RK).run(OS); |
| 990 | } |
| 991 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 992 | } // end namespaec llvm |