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