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