Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 1 | //===- DFAPacketizerEmitter.cpp - Packetization DFA for a VLIW machine-----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class parses the Schedule.td file and produces an API that can be used |
| 11 | // to reason about whether an instruction can be added to a packet on a VLIW |
| 12 | // architecture. The class internally generates a deterministic finite |
| 13 | // automaton (DFA) that models all possible mappings of machine instructions |
| 14 | // to functional units as instructions are added to a packet. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 18 | #include "CodeGenTarget.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 21 | #include "llvm/TableGen/Record.h" |
| 22 | #include "llvm/TableGen/TableGenBackend.h" |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 23 | #include <list> |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 24 | #include <map> |
| 25 | #include <string> |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | // |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 29 | // class DFAPacketizerEmitter: class that generates and prints out the DFA |
| 30 | // for resource tracking. |
| 31 | // |
| 32 | namespace { |
| 33 | class DFAPacketizerEmitter { |
| 34 | private: |
| 35 | std::string TargetName; |
| 36 | // |
| 37 | // allInsnClasses is the set of all possible resources consumed by an |
| 38 | // InstrStage. |
| 39 | // |
| 40 | DenseSet<unsigned> allInsnClasses; |
| 41 | RecordKeeper &Records; |
| 42 | |
| 43 | public: |
| 44 | DFAPacketizerEmitter(RecordKeeper &R); |
| 45 | |
| 46 | // |
| 47 | // collectAllInsnClasses: Populate allInsnClasses which is a set of units |
| 48 | // used in each stage. |
| 49 | // |
| 50 | void collectAllInsnClasses(const std::string &Name, |
| 51 | Record *ItinData, |
| 52 | unsigned &NStages, |
| 53 | raw_ostream &OS); |
| 54 | |
| 55 | void run(raw_ostream &OS); |
| 56 | }; |
| 57 | } // End anonymous namespace. |
| 58 | |
| 59 | // |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 60 | // |
| 61 | // State represents the usage of machine resources if the packet contains |
| 62 | // a set of instruction classes. |
| 63 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 64 | // Specifically, currentState is a set of bit-masks. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 65 | // The nth bit in a bit-mask indicates whether the nth resource is being used |
| 66 | // by this state. The set of bit-masks in a state represent the different |
| 67 | // possible outcomes of transitioning to this state. |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 68 | // For example: consider a two resource architecture: resource L and resource M |
| 69 | // with three instruction classes: L, M, and L_or_M. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 70 | // From the initial state (currentState = 0x00), if we add instruction class |
| 71 | // L_or_M we will transition to a state with currentState = [0x01, 0x10]. This |
| 72 | // represents the possible resource states that can result from adding a L_or_M |
| 73 | // instruction |
| 74 | // |
| 75 | // 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] | 76 | // 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] | 77 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 78 | // A State instance also contains a collection of transitions from that state: |
| 79 | // a map from inputs to new states. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 80 | // |
| 81 | namespace { |
| 82 | class State { |
| 83 | public: |
| 84 | static int currentStateNum; |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 85 | // stateNum is the only member used for equality/ordering, all other members |
| 86 | // can be mutated even in const State objects. |
| 87 | const int stateNum; |
| 88 | mutable bool isInitial; |
| 89 | mutable std::set<unsigned> stateInfo; |
| 90 | typedef std::map<unsigned, const State *> TransitionMap; |
| 91 | mutable TransitionMap Transitions; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 92 | |
| 93 | State(); |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 94 | State(const State &S); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 95 | |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 96 | bool operator<(const State &s) const { |
| 97 | return stateNum < s.stateNum; |
| 98 | } |
| 99 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 100 | // |
| 101 | // canAddInsnClass - Returns true if an instruction of type InsnClass is a |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 102 | // valid transition from this state, i.e., can an instruction of type InsnClass |
| 103 | // be added to the packet represented by this state. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 104 | // |
| 105 | // PossibleStates is the set of valid resource states that ensue from valid |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 106 | // transitions. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 107 | // |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 108 | bool canAddInsnClass(unsigned InsnClass) const; |
| 109 | // |
| 110 | // AddInsnClass - Return all combinations of resource reservation |
| 111 | // which are possible from this state (PossibleStates). |
| 112 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 113 | void AddInsnClass(unsigned InsnClass, std::set<unsigned> &PossibleStates) const; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 114 | // |
| 115 | // addTransition - Add a transition from this state given the input InsnClass |
| 116 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 117 | void addTransition(unsigned InsnClass, const State *To) const; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 118 | // |
| 119 | // hasTransition - Returns true if there is a transition from this state |
| 120 | // given the input InsnClass |
| 121 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 122 | bool hasTransition(unsigned InsnClass) const; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 123 | }; |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 124 | } // End anonymous namespace. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 125 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 126 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 127 | // class DFA: deterministic finite automaton for processor resource tracking. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 128 | // |
| 129 | namespace { |
| 130 | class DFA { |
| 131 | public: |
| 132 | DFA(); |
| 133 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 134 | // 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^] | 135 | typedef std::set<State> StateSet; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 136 | StateSet states; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 137 | |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 138 | State *currentState; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 139 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 140 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 141 | // Modify the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 142 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 143 | const State &newState(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 144 | |
| 145 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 146 | // writeTable: Print out a table representing the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 147 | // |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 148 | void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 149 | }; |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 150 | } // End anonymous namespace. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 154 | // Constructors and destructors for State and DFA |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 155 | // |
| 156 | State::State() : |
| 157 | stateNum(currentStateNum++), isInitial(false) {} |
| 158 | |
| 159 | |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 160 | State::State(const State &S) : |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 161 | stateNum(currentStateNum++), isInitial(S.isInitial), |
| 162 | stateInfo(S.stateInfo) {} |
| 163 | |
Craig Topper | 2406477 | 2014-04-15 07:20:03 +0000 | [diff] [blame] | 164 | DFA::DFA(): currentState(nullptr) {} |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 165 | |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 166 | // |
| 167 | // addTransition - Add a transition from this state given the input InsnClass |
| 168 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 169 | void State::addTransition(unsigned InsnClass, const State *To) const { |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 170 | assert(!Transitions.count(InsnClass) && |
| 171 | "Cannot have multiple transitions for the same input"); |
| 172 | Transitions[InsnClass] = To; |
| 173 | } |
| 174 | |
| 175 | // |
| 176 | // hasTransition - Returns true if there is a transition from this state |
| 177 | // given the input InsnClass |
| 178 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 179 | bool State::hasTransition(unsigned InsnClass) const { |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 180 | return Transitions.count(InsnClass) > 0; |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 181 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 182 | |
| 183 | // |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 184 | // AddInsnClass - Return all combinations of resource reservation |
| 185 | // which are possible from this state (PossibleStates). |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 186 | // |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 187 | void State::AddInsnClass(unsigned InsnClass, |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 188 | std::set<unsigned> &PossibleStates) const { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 189 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 190 | // Iterate over all resource states in currentState. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 191 | // |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 192 | |
| 193 | for (std::set<unsigned>::iterator SI = stateInfo.begin(); |
| 194 | SI != stateInfo.end(); ++SI) { |
| 195 | unsigned thisState = *SI; |
| 196 | |
| 197 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 198 | // Iterate over all possible resources used in InsnClass. |
| 199 | // For ex: for InsnClass = 0x11, all resources = {0x01, 0x10}. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 200 | // |
| 201 | |
| 202 | DenseSet<unsigned> VisitedResourceStates; |
| 203 | for (unsigned int j = 0; j < sizeof(InsnClass) * 8; ++j) { |
| 204 | if ((0x1 << j) & InsnClass) { |
| 205 | // |
| 206 | // For each possible resource used in InsnClass, generate the |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 207 | // resource state if that resource was used. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 208 | // |
| 209 | unsigned ResultingResourceState = thisState | (0x1 << j); |
| 210 | // |
| 211 | // Check if the resulting resource state can be accommodated in this |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 212 | // packet. |
| 213 | // We compute ResultingResourceState OR thisState. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 214 | // If the result of the OR is different than thisState, it implies |
| 215 | // that there is at least one resource that can be used to schedule |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 216 | // InsnClass in the current packet. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 217 | // Insert ResultingResourceState into PossibleStates only if we haven't |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 218 | // processed ResultingResourceState before. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 219 | // |
| 220 | if ((ResultingResourceState != thisState) && |
| 221 | (VisitedResourceStates.count(ResultingResourceState) == 0)) { |
| 222 | VisitedResourceStates.insert(ResultingResourceState); |
| 223 | PossibleStates.insert(ResultingResourceState); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | // |
| 233 | // canAddInsnClass - Quickly verifies if an instruction of type InsnClass is a |
| 234 | // valid transition from this state i.e., can an instruction of type InsnClass |
| 235 | // be added to the packet represented by this state. |
| 236 | // |
| 237 | bool State::canAddInsnClass(unsigned InsnClass) const { |
Alexey Samsonov | 420a4ed | 2012-06-28 07:47:50 +0000 | [diff] [blame] | 238 | for (std::set<unsigned>::const_iterator SI = stateInfo.begin(); |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 239 | SI != stateInfo.end(); ++SI) { |
| 240 | if (~*SI & InsnClass) |
| 241 | return true; |
| 242 | } |
| 243 | return false; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 247 | const State &DFA::newState() { |
| 248 | auto IterPair = states.emplace(); |
| 249 | assert(IterPair.second && "State already exists"); |
| 250 | return *IterPair.first; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 254 | int State::currentStateNum = 0; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 255 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 256 | DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R): |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 257 | TargetName(CodeGenTarget(R).getName()), |
| 258 | allInsnClasses(), Records(R) {} |
| 259 | |
| 260 | |
| 261 | // |
| 262 | // writeTableAndAPI - Print out a table representing the DFA and the |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 263 | // associated API to create a DFA packetizer. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 264 | // |
| 265 | // Format: |
| 266 | // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 267 | // transitions. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 268 | // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 269 | // the ith state. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 270 | // |
| 271 | // |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 272 | void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) { |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 273 | static const std::string SentinelEntry = "{-1, -1}"; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 274 | DFA::StateSet::iterator SI = states.begin(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 275 | // 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] | 276 | // in DFAStateInputTable. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 277 | std::vector<int> StateEntry(states.size()); |
| 278 | |
| 279 | OS << "namespace llvm {\n\n"; |
| 280 | OS << "const int " << TargetName << "DFAStateInputTable[][2] = {\n"; |
| 281 | |
| 282 | // Tracks the total valid transitions encountered so far. It is used |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 283 | // to construct the StateEntry table. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 284 | int ValidTransitions = 0; |
| 285 | for (unsigned i = 0; i < states.size(); ++i, ++SI) { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 286 | assert ((SI->stateNum == (int) i) && "Mismatch in state numbers"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 287 | StateEntry[i] = ValidTransitions; |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 288 | for (State::TransitionMap::iterator |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 289 | II = SI->Transitions.begin(), IE = SI->Transitions.end(); |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 290 | II != IE; ++II) { |
| 291 | OS << "{" << II->first << ", " |
| 292 | << II->second->stateNum |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 293 | << "}, "; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 294 | } |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 295 | ValidTransitions += SI->Transitions.size(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 296 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 297 | // If there are no valid transitions from this stage, we need a sentinel |
| 298 | // transition. |
Brendon Cahoon | e9b60aa | 2012-02-03 21:08:25 +0000 | [diff] [blame] | 299 | if (ValidTransitions == StateEntry[i]) { |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 300 | OS << SentinelEntry << ","; |
Brendon Cahoon | e9b60aa | 2012-02-03 21:08:25 +0000 | [diff] [blame] | 301 | ++ValidTransitions; |
| 302 | } |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 303 | |
| 304 | OS << "\n"; |
| 305 | } |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 306 | |
| 307 | // Print out a sentinel entry at the end of the StateInputTable. This is |
| 308 | // needed to iterate over StateInputTable in DFAPacketizer::ReadTable() |
| 309 | OS << SentinelEntry << "\n"; |
| 310 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 311 | OS << "};\n\n"; |
| 312 | OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n"; |
| 313 | |
| 314 | // 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] | 315 | // two numbers. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 316 | for (unsigned i = 0; i < states.size(); ++i) |
| 317 | OS << StateEntry[i] << ", "; |
| 318 | |
Anshuman Dasgupta | 3923e28 | 2012-12-10 22:45:57 +0000 | [diff] [blame] | 319 | // Print out the index to the sentinel entry in StateInputTable |
| 320 | OS << ValidTransitions << ", "; |
| 321 | |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 322 | OS << "\n};\n"; |
| 323 | OS << "} // namespace\n"; |
| 324 | |
| 325 | |
| 326 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 327 | // Emit DFA Packetizer tables if the target is a VLIW machine. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 328 | // |
| 329 | std::string SubTargetClassName = TargetName + "GenSubtargetInfo"; |
| 330 | OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n"; |
| 331 | OS << "namespace llvm {\n"; |
Sebastian Pop | ac35a4d | 2011-12-06 17:34:16 +0000 | [diff] [blame] | 332 | OS << "DFAPacketizer *" << SubTargetClassName << "::" |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 333 | << "createDFAPacketizer(const InstrItineraryData *IID) const {\n" |
| 334 | << " return new DFAPacketizer(IID, " << TargetName |
| 335 | << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n"; |
| 336 | OS << "} // End llvm namespace \n"; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // |
| 341 | // collectAllInsnClasses - Populate allInsnClasses which is a set of units |
| 342 | // used in each stage. |
| 343 | // |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 344 | void DFAPacketizerEmitter::collectAllInsnClasses(const std::string &Name, |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 345 | Record *ItinData, |
| 346 | unsigned &NStages, |
| 347 | raw_ostream &OS) { |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 348 | // Collect processor itineraries. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 349 | std::vector<Record*> ProcItinList = |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 350 | Records.getAllDerivedDefinitions("ProcessorItineraries"); |
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 | // If just no itinerary then don't bother. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 353 | if (ProcItinList.size() < 2) |
| 354 | return; |
| 355 | std::map<std::string, unsigned> NameToBitsMap; |
| 356 | |
| 357 | // Parse functional units for all the itineraries. |
| 358 | for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) { |
| 359 | Record *Proc = ProcItinList[i]; |
| 360 | std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU"); |
| 361 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 362 | // Convert macros to bits for each stage. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 363 | for (unsigned i = 0, N = FUs.size(); i < N; ++i) |
| 364 | NameToBitsMap[FUs[i]->getName()] = (unsigned) (1U << i); |
| 365 | } |
| 366 | |
| 367 | const std::vector<Record*> &StageList = |
| 368 | ItinData->getValueAsListOfDefs("Stages"); |
| 369 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 370 | // The number of stages. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 371 | NStages = StageList.size(); |
| 372 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 373 | // For each unit. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 374 | unsigned UnitBitValue = 0; |
| 375 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 376 | // Compute the bitwise or of each unit used in this stage. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 377 | for (unsigned i = 0; i < NStages; ++i) { |
| 378 | const Record *Stage = StageList[i]; |
| 379 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 380 | // Get unit list. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 381 | const std::vector<Record*> &UnitList = |
| 382 | Stage->getValueAsListOfDefs("Units"); |
| 383 | |
| 384 | for (unsigned j = 0, M = UnitList.size(); j < M; ++j) { |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 385 | // Conduct bitwise or. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 386 | std::string UnitName = UnitList[j]->getName(); |
| 387 | assert(NameToBitsMap.count(UnitName)); |
| 388 | UnitBitValue |= NameToBitsMap[UnitName]; |
| 389 | } |
| 390 | |
| 391 | if (UnitBitValue != 0) |
| 392 | allInsnClasses.insert(UnitBitValue); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | |
| 397 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 398 | // Run the worklist algorithm to generate the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 399 | // |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 400 | void DFAPacketizerEmitter::run(raw_ostream &OS) { |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 401 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 402 | // Collect processor iteraries. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 403 | std::vector<Record*> ProcItinList = |
| 404 | Records.getAllDerivedDefinitions("ProcessorItineraries"); |
| 405 | |
| 406 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 407 | // Collect the instruction classes. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 408 | // |
| 409 | for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) { |
| 410 | Record *Proc = ProcItinList[i]; |
| 411 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 412 | // Get processor itinerary name. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 413 | const std::string &Name = Proc->getName(); |
| 414 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 415 | // Skip default. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 416 | if (Name == "NoItineraries") |
| 417 | continue; |
| 418 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 419 | // Sanity check for at least one instruction itinerary class. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 420 | unsigned NItinClasses = |
| 421 | Records.getAllDerivedDefinitions("InstrItinClass").size(); |
| 422 | if (NItinClasses == 0) |
| 423 | return; |
| 424 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 425 | // Get itinerary data list. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 426 | std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID"); |
| 427 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 428 | // Collect instruction classes for all itinerary data. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 429 | for (unsigned j = 0, M = ItinDataList.size(); j < M; j++) { |
| 430 | Record *ItinData = ItinDataList[j]; |
| 431 | unsigned NStages; |
| 432 | collectAllInsnClasses(Name, ItinData, NStages, OS); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | |
| 437 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 438 | // Run a worklist algorithm to generate the DFA. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 439 | // |
| 440 | DFA D; |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 441 | const State *Initial = &D.newState(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 442 | Initial->isInitial = true; |
| 443 | Initial->stateInfo.insert(0x0); |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 444 | SmallVector<const State*, 32> WorkList; |
| 445 | std::map<std::set<unsigned>, const State*> Visited; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 446 | |
| 447 | WorkList.push_back(Initial); |
| 448 | |
| 449 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 450 | // Worklist algorithm to create a DFA for processor resource tracking. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 451 | // C = {set of InsnClasses} |
| 452 | // Begin with initial node in worklist. Initial node does not have |
| 453 | // any consumed resources, |
| 454 | // ResourceState = 0x0 |
| 455 | // Visited = {} |
| 456 | // While worklist != empty |
| 457 | // S = first element of worklist |
| 458 | // For every instruction class C |
| 459 | // if we can accommodate C in S: |
| 460 | // S' = state with resource states = {S Union C} |
| 461 | // Add a new transition: S x C -> S' |
| 462 | // If S' is not in Visited: |
| 463 | // Add S' to worklist |
| 464 | // Add S' to Visited |
| 465 | // |
| 466 | while (!WorkList.empty()) { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 467 | const State *current = WorkList.pop_back_val(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 468 | for (DenseSet<unsigned>::iterator CI = allInsnClasses.begin(), |
| 469 | CE = allInsnClasses.end(); CI != CE; ++CI) { |
| 470 | unsigned InsnClass = *CI; |
| 471 | |
| 472 | std::set<unsigned> NewStateResources; |
| 473 | // |
| 474 | // If we haven't already created a transition for this input |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 475 | // and the state can accommodate this InsnClass, create a transition. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 476 | // |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 477 | if (!current->hasTransition(InsnClass) && |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 478 | current->canAddInsnClass(InsnClass)) { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 479 | const State *NewState; |
Anshuman Dasgupta | 20013f1 | 2012-06-27 19:38:29 +0000 | [diff] [blame] | 480 | current->AddInsnClass(InsnClass, NewStateResources); |
| 481 | assert(NewStateResources.size() && "New states must be generated"); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 482 | |
| 483 | // |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 484 | // 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] | 485 | // |
| 486 | // |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 487 | auto VI = Visited.find(NewStateResources); |
| 488 | if (VI != Visited.end()) |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 489 | NewState = VI->second; |
| 490 | else { |
David Blaikie | d43046b | 2014-04-21 22:35:11 +0000 | [diff] [blame^] | 491 | NewState = &D.newState(); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 492 | NewState->stateInfo = NewStateResources; |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 493 | Visited[NewStateResources] = NewState; |
| 494 | WorkList.push_back(NewState); |
| 495 | } |
Anshuman Dasgupta | f16a443 | 2012-09-07 21:35:43 +0000 | [diff] [blame] | 496 | |
| 497 | current->addTransition(InsnClass, NewState); |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Sebastian Pop | 9aa6137 | 2011-12-06 17:34:11 +0000 | [diff] [blame] | 502 | // Print out the table. |
Anshuman Dasgupta | 08ebdc1 | 2011-12-01 21:10:21 +0000 | [diff] [blame] | 503 | D.writeTableAndAPI(OS, TargetName); |
| 504 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 505 | |
| 506 | namespace llvm { |
| 507 | |
| 508 | void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) { |
| 509 | emitSourceFileHeader("Target DFA Packetizer Tables", OS); |
| 510 | DFAPacketizerEmitter(RK).run(OS); |
| 511 | } |
| 512 | |
| 513 | } // End llvm namespace |