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