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