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