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