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 18 #define DEBUG_TYPE "dfa-emitter" 19 20 #include "CodeGenTarget.h" 21 #include "llvm/ADT/DenseSet.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/StringExtras.h" 24 #include "llvm/CodeGen/DFAPacketizerDefs.h" 25 #include "llvm/TableGen/Record.h" 26 #include "llvm/TableGen/TableGenBackend.h" 27 #include "llvm/Support/Debug.h" 28 #include <list> 29 #include <map> 30 #include <string> 31 #include <queue> 32 using namespace llvm; 33 34 #ifndef NDEBUG 35 // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter". 36 // 37 // dbgsInsnClass - When debugging, print instruction class stages. 38 // 39 void dbgsInsnClass(const std::vector<unsigned> &InsnClass); 40 // 41 // dbgsStateInfo - When debugging, print the set of state info. 42 // 43 void dbgsStateInfo(const std::set<unsigned> &stateInfo); 44 // 45 // dbgsIndent - When debugging, indent by the specified amount. 46 // 47 void dbgsIndent(unsigned indent); 48 #endif 49 50 // 51 // class DFAPacketizerEmitter: class that generates and prints out the DFA 52 // for resource tracking. 53 // 54 namespace { 55 class DFAPacketizerEmitter { 56 private: 57 std::string TargetName; 58 // 59 // allInsnClasses is the set of all possible resources consumed by an 60 // InstrStage. 61 // 62 std::vector<std::vector<unsigned>> allInsnClasses; 63 RecordKeeper &Records; 64 65 public: 66 DFAPacketizerEmitter(RecordKeeper &R); 67 68 // 69 // collectAllFuncUnits - Construct a map of function unit names to bits. 70 // 71 int collectAllFuncUnits(std::vector<Record*> &ProcItinList, 72 std::map<std::string, unsigned> &FUNameToBitsMap, 73 int &maxResources, 74 raw_ostream &OS); 75 76 // 77 // collectAllComboFuncs - Construct a map from a combo function unit bit to 78 // the bits of all included functional units. 79 // 80 int collectAllComboFuncs(std::vector<Record*> &ComboFuncList, 81 std::map<std::string, unsigned> &FUNameToBitsMap, 82 std::map<unsigned, unsigned> &ComboBitToBitsMap, 83 raw_ostream &OS); 84 85 // 86 // collectOneInsnClass - Populate allInsnClasses with one instruction class. 87 // 88 int collectOneInsnClass(const std::string &ProcName, 89 std::vector<Record*> &ProcItinList, 90 std::map<std::string, unsigned> &FUNameToBitsMap, 91 Record *ItinData, 92 raw_ostream &OS); 93 94 // 95 // collectAllInsnClasses - Populate allInsnClasses which is a set of units 96 // used in each stage. 97 // 98 int collectAllInsnClasses(const std::string &ProcName, 99 std::vector<Record*> &ProcItinList, 100 std::map<std::string, unsigned> &FUNameToBitsMap, 101 std::vector<Record*> &ItinDataList, 102 int &maxStages, 103 raw_ostream &OS); 104 105 void run(raw_ostream &OS); 106 }; 107 } // End anonymous namespace. 108 109 // 110 // 111 // State represents the usage of machine resources if the packet contains 112 // a set of instruction classes. 113 // 114 // Specifically, currentState is a set of bit-masks. 115 // The nth bit in a bit-mask indicates whether the nth resource is being used 116 // by this state. The set of bit-masks in a state represent the different 117 // possible outcomes of transitioning to this state. 118 // For example: consider a two resource architecture: resource L and resource M 119 // with three instruction classes: L, M, and L_or_M. 120 // From the initial state (currentState = 0x00), if we add instruction class 121 // L_or_M we will transition to a state with currentState = [0x01, 0x10]. This 122 // represents the possible resource states that can result from adding a L_or_M 123 // instruction 124 // 125 // Another way of thinking about this transition is we are mapping a NDFA with 126 // two states [0x01] and [0x10] into a DFA with a single state [0x01, 0x10]. 127 // 128 // A State instance also contains a collection of transitions from that state: 129 // a map from inputs to new states. 130 // 131 namespace { 132 class State { 133 public: 134 static int currentStateNum; 135 // stateNum is the only member used for equality/ordering, all other members 136 // can be mutated even in const State objects. 137 const int stateNum; 138 mutable bool isInitial; 139 mutable std::set<unsigned> stateInfo; 140 typedef std::map<std::vector<unsigned>, const State *> TransitionMap; 141 mutable TransitionMap Transitions; 142 143 State(); 144 145 bool operator<(const State &s) const { 146 return stateNum < s.stateNum; 147 } 148 149 // 150 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass 151 // may be a valid transition from this state i.e., can an instruction of type 152 // InsnClass be added to the packet represented by this state. 153 // 154 // Note that for multiple stages, this quick check does not take into account 155 // any possible resource competition between the stages themselves. That is 156 // enforced in AddInsnClassStages which checks the cross product of all 157 // stages for resource availability (which is a more involved check). 158 // 159 bool canMaybeAddInsnClass(std::vector<unsigned> &InsnClass, 160 std::map<unsigned, unsigned> &ComboBitToBitsMap) const; 161 // 162 // AddInsnClass - Return all combinations of resource reservation 163 // which are possible from this state (PossibleStates). 164 // 165 // PossibleStates is the set of valid resource states that ensue from valid 166 // transitions. 167 // 168 void AddInsnClass(std::vector<unsigned> &InsnClass, 169 std::map<unsigned, unsigned> &ComboBitToBitsMap, 170 std::set<unsigned> &PossibleStates) const; 171 // 172 // AddInsnClassStages - Return all combinations of resource reservation 173 // resulting from the cross product of all stages for this InsnClass 174 // which are possible from this state (PossibleStates). 175 // 176 void AddInsnClassStages(std::vector<unsigned> &InsnClass, 177 std::map<unsigned, unsigned> &ComboBitToBitsMap, 178 unsigned chkstage, unsigned numstages, 179 unsigned prevState, unsigned origState, 180 DenseSet<unsigned> &VisitedResourceStates, 181 std::set<unsigned> &PossibleStates) const; 182 // 183 // addTransition - Add a transition from this state given the input InsnClass 184 // 185 void addTransition(std::vector<unsigned> InsnClass, const State *To) const; 186 // 187 // hasTransition - Returns true if there is a transition from this state 188 // given the input InsnClass 189 // 190 bool hasTransition(std::vector<unsigned> InsnClass) const; 191 }; 192 } // End anonymous namespace. 193 194 // 195 // class DFA: deterministic finite automaton for processor resource tracking. 196 // 197 namespace { 198 class DFA { 199 public: 200 DFA(); 201 202 // Set of states. Need to keep this sorted to emit the transition table. 203 typedef std::set<State> StateSet; 204 StateSet states; 205 206 State *currentState; 207 208 // 209 // Modify the DFA. 210 // 211 const State &newState(); 212 213 // 214 // writeTable: Print out a table representing the DFA. 215 // 216 void writeTableAndAPI(raw_ostream &OS, const std::string &ClassName, 217 int numInsnClasses = 0, 218 int maxResources = 0, int numCombos = 0, int maxStages = 0); 219 }; 220 } // End anonymous namespace. 221 222 #ifndef NDEBUG 223 // To enable debugging, run llvm-tblgen with: "-debug-only dfa-emitter". 224 // 225 // dbgsInsnClass - When debugging, print instruction class stages. 226 // 227 void dbgsInsnClass(const std::vector<unsigned> &InsnClass) { 228 DEBUG(dbgs() << "InsnClass: "); 229 for (unsigned i = 0; i < InsnClass.size(); ++i) { 230 if (i > 0) { 231 DEBUG(dbgs() << ", "); 232 } 233 DEBUG(dbgs() << "0x" << utohexstr(InsnClass[i])); 234 } 235 DFAInput InsnInput = getDFAInsnInput(InsnClass); 236 DEBUG(dbgs() << " (input: 0x" << utohexstr(InsnInput) << ")"); 237 } 238 239 // 240 // dbgsStateInfo - When debugging, print the set of state info. 241 // 242 void dbgsStateInfo(const std::set<unsigned> &stateInfo) { 243 DEBUG(dbgs() << "StateInfo: "); 244 unsigned i = 0; 245 for (std::set<unsigned>::iterator SI = stateInfo.begin(); 246 SI != stateInfo.end(); ++SI, ++i) { 247 unsigned thisState = *SI; 248 if (i > 0) { 249 DEBUG(dbgs() << ", "); 250 } 251 DEBUG(dbgs() << "0x" << utohexstr(thisState)); 252 } 253 } 254 255 // 256 // dbgsIndent - When debugging, indent by the specified amount. 257 // 258 void dbgsIndent(unsigned indent) { 259 for (unsigned i = 0; i < indent; ++i) { 260 DEBUG(dbgs() << " "); 261 } 262 } 263 #endif 264 265 // 266 // Constructors and destructors for State and DFA 267 // 268 State::State() : 269 stateNum(currentStateNum++), isInitial(false) {} 270 271 DFA::DFA(): currentState(nullptr) {} 272 273 // 274 // addTransition - Add a transition from this state given the input InsnClass 275 // 276 void State::addTransition(std::vector<unsigned> InsnClass, const State *To) 277 const { 278 assert(!Transitions.count(InsnClass) && 279 "Cannot have multiple transitions for the same input"); 280 Transitions[InsnClass] = To; 281 } 282 283 // 284 // hasTransition - Returns true if there is a transition from this state 285 // given the input InsnClass 286 // 287 bool State::hasTransition(std::vector<unsigned> InsnClass) const { 288 return Transitions.count(InsnClass) > 0; 289 } 290 291 // 292 // AddInsnClass - Return all combinations of resource reservation 293 // which are possible from this state (PossibleStates). 294 // 295 // PossibleStates is the set of valid resource states that ensue from valid 296 // transitions. 297 // 298 void State::AddInsnClass(std::vector<unsigned> &InsnClass, 299 std::map<unsigned, unsigned> &ComboBitToBitsMap, 300 std::set<unsigned> &PossibleStates) const { 301 // 302 // Iterate over all resource states in currentState. 303 // 304 unsigned numstages = InsnClass.size(); 305 assert((numstages > 0) && "InsnClass has no stages"); 306 307 for (std::set<unsigned>::iterator SI = stateInfo.begin(); 308 SI != stateInfo.end(); ++SI) { 309 unsigned thisState = *SI; 310 311 DenseSet<unsigned> VisitedResourceStates; 312 313 DEBUG(dbgs() << " thisState: 0x" << utohexstr(thisState) << "\n"); 314 AddInsnClassStages(InsnClass, ComboBitToBitsMap, 315 numstages - 1, numstages, 316 thisState, thisState, 317 VisitedResourceStates, PossibleStates); 318 } 319 } 320 321 void State::AddInsnClassStages(std::vector<unsigned> &InsnClass, 322 std::map<unsigned, unsigned> &ComboBitToBitsMap, 323 unsigned chkstage, unsigned numstages, 324 unsigned prevState, unsigned origState, 325 DenseSet<unsigned> &VisitedResourceStates, 326 std::set<unsigned> &PossibleStates) const { 327 328 assert((chkstage < numstages) && "AddInsnClassStages: stage out of range"); 329 unsigned thisStage = InsnClass[chkstage]; 330 331 DEBUG({ 332 dbgsIndent((1 + numstages - chkstage) << 1); 333 dbgs() << "AddInsnClassStages " << chkstage << " (0x" 334 << utohexstr(thisStage) << ") from "; 335 dbgsInsnClass(InsnClass); 336 dbgs() << "\n"; 337 }); 338 339 // 340 // Iterate over all possible resources used in thisStage. 341 // For ex: for thisStage = 0x11, all resources = {0x01, 0x10}. 342 // 343 for (unsigned int j = 0; j < DFA_MAX_RESOURCES; ++j) { 344 unsigned resourceMask = (0x1 << j); 345 if (resourceMask & thisStage) { 346 unsigned combo = ComboBitToBitsMap[resourceMask]; 347 if (combo && ((~prevState & combo) != combo)) { 348 DEBUG(dbgs() << "\tSkipped Add 0x" << utohexstr(prevState) 349 << " - combo op 0x" << utohexstr(resourceMask) 350 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n"); 351 continue; 352 } 353 // 354 // For each possible resource used in thisStage, generate the 355 // resource state if that resource was used. 356 // 357 unsigned ResultingResourceState = prevState | resourceMask | combo; 358 DEBUG({ 359 dbgsIndent((2 + numstages - chkstage) << 1); 360 dbgs() << "0x" << utohexstr(prevState) 361 << " | 0x" << utohexstr(resourceMask); 362 if (combo) 363 dbgs() << " | 0x" << utohexstr(combo); 364 dbgs() << " = 0x" << utohexstr(ResultingResourceState) << " "; 365 }); 366 367 // 368 // If this is the final stage for this class 369 // 370 if (chkstage == 0) { 371 // 372 // Check if the resulting resource state can be accommodated in this 373 // packet. 374 // We compute resource OR prevState (originally started as origState). 375 // If the result of the OR is different than origState, it implies 376 // that there is at least one resource that can be used to schedule 377 // thisStage in the current packet. 378 // Insert ResultingResourceState into PossibleStates only if we haven't 379 // processed ResultingResourceState before. 380 // 381 if (ResultingResourceState != prevState) { 382 if (VisitedResourceStates.count(ResultingResourceState) == 0) { 383 VisitedResourceStates.insert(ResultingResourceState); 384 PossibleStates.insert(ResultingResourceState); 385 DEBUG(dbgs() << "\tResultingResourceState: 0x" 386 << utohexstr(ResultingResourceState) << "\n"); 387 } else { 388 DEBUG(dbgs() << "\tSkipped Add - state already seen\n"); 389 } 390 } else { 391 DEBUG(dbgs() << "\tSkipped Add - no final resources available\n"); 392 } 393 } else { 394 // 395 // If the current resource can be accommodated, check the next 396 // stage in InsnClass for available resources. 397 // 398 if (ResultingResourceState != prevState) { 399 DEBUG(dbgs() << "\n"); 400 AddInsnClassStages(InsnClass, ComboBitToBitsMap, 401 chkstage - 1, numstages, 402 ResultingResourceState, origState, 403 VisitedResourceStates, PossibleStates); 404 } else { 405 DEBUG(dbgs() << "\tSkipped Add - no resources available\n"); 406 } 407 } 408 } 409 } 410 } 411 412 413 // 414 // canMaybeAddInsnClass - Quickly verifies if an instruction of type InsnClass 415 // may be a valid transition from this state i.e., can an instruction of type 416 // InsnClass be added to the packet represented by this state. 417 // 418 // Note that this routine is performing conservative checks that can be 419 // quickly executed acting as a filter before calling AddInsnClassStages. 420 // Any cases allowed through here will be caught later in AddInsnClassStages 421 // which performs the more expensive exact check. 422 // 423 bool State::canMaybeAddInsnClass(std::vector<unsigned> &InsnClass, 424 std::map<unsigned, unsigned> &ComboBitToBitsMap) const { 425 for (std::set<unsigned>::const_iterator SI = stateInfo.begin(); 426 SI != stateInfo.end(); ++SI) { 427 428 // Check to see if all required resources are available. 429 bool available = true; 430 431 // Inspect each stage independently. 432 // note: This is a conservative check as we aren't checking for 433 // possible resource competition between the stages themselves 434 // The full cross product is examined later in AddInsnClass. 435 for (unsigned i = 0; i < InsnClass.size(); ++i) { 436 unsigned resources = *SI; 437 if ((~resources & InsnClass[i]) == 0) { 438 available = false; 439 break; 440 } 441 // Make sure _all_ resources for a combo function are available. 442 // note: This is a quick conservative check as it won't catch an 443 // unscheduleable combo if this stage is an OR expression 444 // containing a combo. 445 // These cases are caught later in AddInsnClass. 446 unsigned combo = ComboBitToBitsMap[InsnClass[i]]; 447 if (combo && ((~resources & combo) != combo)) { 448 DEBUG(dbgs() << "\tSkipped canMaybeAdd 0x" << utohexstr(resources) 449 << " - combo op 0x" << utohexstr(InsnClass[i]) 450 << " (0x" << utohexstr(combo) <<") cannot be scheduled\n"); 451 available = false; 452 break; 453 } 454 } 455 456 if (available) { 457 return true; 458 } 459 } 460 return false; 461 } 462 463 464 const State &DFA::newState() { 465 auto IterPair = states.insert(State()); 466 assert(IterPair.second && "State already exists"); 467 return *IterPair.first; 468 } 469 470 int State::currentStateNum = 0; 471 472 DFAPacketizerEmitter::DFAPacketizerEmitter(RecordKeeper &R): 473 TargetName(CodeGenTarget(R).getName()), 474 allInsnClasses(), Records(R) {} 475 476 477 // 478 // writeTableAndAPI - Print out a table representing the DFA and the 479 // associated API to create a DFA packetizer. 480 // 481 // Format: 482 // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid 483 // transitions. 484 // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable for 485 // the ith state. 486 // 487 // 488 void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName, 489 int numInsnClasses, 490 int maxResources, int numCombos, int maxStages) { 491 492 unsigned numStates = states.size(); 493 494 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"); 495 DEBUG(dbgs() << "writeTableAndAPI\n"); 496 DEBUG(dbgs() << "Total states: " << numStates << "\n"); 497 498 OS << "namespace llvm {\n"; 499 500 OS << "\n// Input format:\n"; 501 OS << "#define DFA_MAX_RESTERMS " << DFA_MAX_RESTERMS 502 << "\t// maximum AND'ed resource terms\n"; 503 OS << "#define DFA_MAX_RESOURCES " << DFA_MAX_RESOURCES 504 << "\t// maximum resource bits in one term\n"; 505 506 OS << "\n// " << TargetName << "DFAStateInputTable[][2] = " 507 << "pairs of <Input, NextState> for all valid\n"; 508 OS << "// transitions.\n"; 509 OS << "// " << numStates << "\tstates\n"; 510 OS << "// " << numInsnClasses << "\tinstruction classes\n"; 511 OS << "// " << maxResources << "\tresources max\n"; 512 OS << "// " << numCombos << "\tcombo resources\n"; 513 OS << "// " << maxStages << "\tstages max\n"; 514 OS << "const " << DFA_TBLTYPE << " " 515 << TargetName << "DFAStateInputTable[][2] = {\n"; 516 517 // This table provides a map to the beginning of the transitions for State s 518 // in DFAStateInputTable. 519 std::vector<int> StateEntry(numStates+1); 520 static const std::string SentinelEntry = "{-1, -1}"; 521 522 // Tracks the total valid transitions encountered so far. It is used 523 // to construct the StateEntry table. 524 int ValidTransitions = 0; 525 DFA::StateSet::iterator SI = states.begin(); 526 for (unsigned i = 0; i < numStates; ++i, ++SI) { 527 assert ((SI->stateNum == (int) i) && "Mismatch in state numbers"); 528 StateEntry[i] = ValidTransitions; 529 for (State::TransitionMap::iterator 530 II = SI->Transitions.begin(), IE = SI->Transitions.end(); 531 II != IE; ++II) { 532 OS << "{0x" << utohexstr(getDFAInsnInput(II->first)) << ", " 533 << II->second->stateNum 534 << "},\t"; 535 } 536 ValidTransitions += SI->Transitions.size(); 537 538 // If there are no valid transitions from this stage, we need a sentinel 539 // transition. 540 if (ValidTransitions == StateEntry[i]) { 541 OS << SentinelEntry << ",\t"; 542 ++ValidTransitions; 543 } 544 545 OS << " // state " << i << ": " << StateEntry[i]; 546 if (StateEntry[i] != (ValidTransitions-1)) { // More than one transition. 547 OS << "-" << (ValidTransitions-1); 548 } 549 OS << "\n"; 550 } 551 552 // Print out a sentinel entry at the end of the StateInputTable. This is 553 // needed to iterate over StateInputTable in DFAPacketizer::ReadTable() 554 OS << SentinelEntry << "\t"; 555 OS << " // state " << numStates << ": " << ValidTransitions; 556 OS << "\n"; 557 558 OS << "};\n\n"; 559 OS << "// " << TargetName << "DFAStateEntryTable[i] = " 560 << "Index of the first entry in DFAStateInputTable for\n"; 561 OS << "// " 562 << "the ith state.\n"; 563 OS << "// " << numStates << " states\n"; 564 OS << "const unsigned int " << TargetName << "DFAStateEntryTable[] = {\n"; 565 566 // Multiply i by 2 since each entry in DFAStateInputTable is a set of 567 // two numbers. 568 unsigned lastState = 0; 569 for (unsigned i = 0; i < numStates; ++i) { 570 if (i && ((i % 10) == 0)) { 571 lastState = i-1; 572 OS << " // states " << (i-10) << ":" << lastState << "\n"; 573 } 574 OS << StateEntry[i] << ", "; 575 } 576 577 // Print out the index to the sentinel entry in StateInputTable 578 OS << ValidTransitions << ", "; 579 OS << " // states " << (lastState+1) << ":" << numStates << "\n"; 580 581 OS << "};\n"; 582 OS << "} // namespace\n"; 583 584 585 // 586 // Emit DFA Packetizer tables if the target is a VLIW machine. 587 // 588 std::string SubTargetClassName = TargetName + "GenSubtargetInfo"; 589 OS << "\n" << "#include \"llvm/CodeGen/DFAPacketizer.h\"\n"; 590 OS << "namespace llvm {\n"; 591 OS << "DFAPacketizer *" << SubTargetClassName << "::" 592 << "createDFAPacketizer(const InstrItineraryData *IID) const {\n" 593 << " return new DFAPacketizer(IID, " << TargetName 594 << "DFAStateInputTable, " << TargetName << "DFAStateEntryTable);\n}\n\n"; 595 OS << "} // End llvm namespace \n"; 596 } 597 598 599 // 600 // collectAllFuncUnits - Construct a map of function unit names to bits. 601 // 602 int DFAPacketizerEmitter::collectAllFuncUnits( 603 std::vector<Record*> &ProcItinList, 604 std::map<std::string, unsigned> &FUNameToBitsMap, 605 int &maxFUs, 606 raw_ostream &OS) { 607 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"); 608 DEBUG(dbgs() << "collectAllFuncUnits"); 609 DEBUG(dbgs() << " (" << ProcItinList.size() << " itineraries)\n"); 610 611 int totalFUs = 0; 612 // Parse functional units for all the itineraries. 613 for (unsigned i = 0, N = ProcItinList.size(); i < N; ++i) { 614 Record *Proc = ProcItinList[i]; 615 std::vector<Record*> FUs = Proc->getValueAsListOfDefs("FU"); 616 617 DEBUG(dbgs() << " FU:" << i 618 << " (" << FUs.size() << " FUs) " 619 << Proc->getName()); 620 621 622 // Convert macros to bits for each stage. 623 unsigned numFUs = FUs.size(); 624 for (unsigned j = 0; j < numFUs; ++j) { 625 assert ((j < DFA_MAX_RESOURCES) && 626 "Exceeded maximum number of representable resources"); 627 unsigned FuncResources = (unsigned) (1U << j); 628 FUNameToBitsMap[FUs[j]->getName()] = FuncResources; 629 DEBUG(dbgs() << " " << FUs[j]->getName() 630 << ":0x" << utohexstr(FuncResources)); 631 } 632 if (((int) numFUs) > maxFUs) { 633 maxFUs = numFUs; 634 } 635 totalFUs += numFUs; 636 DEBUG(dbgs() << "\n"); 637 } 638 return totalFUs; 639 } 640 641 // 642 // collectAllComboFuncs - Construct a map from a combo function unit bit to 643 // the bits of all included functional units. 644 // 645 int DFAPacketizerEmitter::collectAllComboFuncs( 646 std::vector<Record*> &ComboFuncList, 647 std::map<std::string, unsigned> &FUNameToBitsMap, 648 std::map<unsigned, unsigned> &ComboBitToBitsMap, 649 raw_ostream &OS) { 650 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n"); 651 DEBUG(dbgs() << "collectAllComboFuncs"); 652 DEBUG(dbgs() << " (" << ComboFuncList.size() << " sets)\n"); 653 654 int numCombos = 0; 655 for (unsigned i = 0, N = ComboFuncList.size(); i < N; ++i) { 656 Record *Func = ComboFuncList[i]; 657 std::vector<Record*> FUs = Func->getValueAsListOfDefs("CFD"); 658 659 DEBUG(dbgs() << " CFD:" << i 660 << " (" << FUs.size() << " combo FUs) " 661 << Func->getName() << "\n"); 662 663 // Convert macros to bits for each stage. 664 for (unsigned j = 0, N = FUs.size(); j < N; ++j) { 665 assert ((j < DFA_MAX_RESOURCES) && 666 "Exceeded maximum number of DFA resources"); 667 Record *FuncData = FUs[j]; 668 Record *ComboFunc = FuncData->getValueAsDef("TheComboFunc"); 669 const std::vector<Record*> &FuncList = 670 FuncData->getValueAsListOfDefs("FuncList"); 671 std::string ComboFuncName = ComboFunc->getName(); 672 unsigned ComboBit = FUNameToBitsMap[ComboFuncName]; 673 unsigned ComboResources = ComboBit; 674 DEBUG(dbgs() << " combo: " << ComboFuncName 675 << ":0x" << utohexstr(ComboResources) << "\n"); 676 for (unsigned k = 0, M = FuncList.size(); k < M; ++k) { 677 std::string FuncName = FuncList[k]->getName(); 678 unsigned FuncResources = FUNameToBitsMap[FuncName]; 679 DEBUG(dbgs() << " " << FuncName 680 << ":0x" << utohexstr(FuncResources) << "\n"); 681 ComboResources |= FuncResources; 682 } 683 ComboBitToBitsMap[ComboBit] = ComboResources; 684 numCombos++; 685 DEBUG(dbgs() << " => combo bits: " << ComboFuncName << ":0x" 686 << utohexstr(ComboBit) << " = 0x" 687 << utohexstr(ComboResources) << "\n"); 688 } 689 } 690 return numCombos; 691 } 692 693 694 // 695 // collectOneInsnClass - Populate allInsnClasses with one instruction class 696 // 697 int DFAPacketizerEmitter::collectOneInsnClass(const std::string &ProcName, 698 std::vector<Record*> &ProcItinList, 699 std::map<std::string, unsigned> &FUNameToBitsMap, 700 Record *ItinData, 701 raw_ostream &OS) { 702 const std::vector<Record*> &StageList = 703 ItinData->getValueAsListOfDefs("Stages"); 704 705 // The number of stages. 706 unsigned NStages = StageList.size(); 707 708 DEBUG(dbgs() << " " << ItinData->getValueAsDef("TheClass")->getName() 709 << "\n"); 710 711 std::vector<unsigned> UnitBits; 712 713 // Compute the bitwise or of each unit used in this stage. 714 for (unsigned i = 0; i < NStages; ++i) { 715 const Record *Stage = StageList[i]; 716 717 // Get unit list. 718 const std::vector<Record*> &UnitList = 719 Stage->getValueAsListOfDefs("Units"); 720 721 DEBUG(dbgs() << " stage:" << i 722 << " [" << UnitList.size() << " units]:"); 723 unsigned dbglen = 26; // cursor after stage dbgs 724 725 // Compute the bitwise or of each unit used in this stage. 726 unsigned UnitBitValue = 0; 727 for (unsigned j = 0, M = UnitList.size(); j < M; ++j) { 728 // Conduct bitwise or. 729 std::string UnitName = UnitList[j]->getName(); 730 DEBUG(dbgs() << " " << j << ":" << UnitName); 731 dbglen += 3 + UnitName.length(); 732 assert(FUNameToBitsMap.count(UnitName)); 733 UnitBitValue |= FUNameToBitsMap[UnitName]; 734 } 735 736 if (UnitBitValue != 0) 737 UnitBits.push_back(UnitBitValue); 738 739 while (dbglen <= 64) { // line up bits dbgs 740 dbglen += 8; 741 DEBUG(dbgs() << "\t"); 742 } 743 DEBUG(dbgs() << " (bits: 0x" << utohexstr(UnitBitValue) << ")\n"); 744 } 745 746 if (UnitBits.size() > 0) 747 allInsnClasses.push_back(UnitBits); 748 749 DEBUG({ 750 dbgs() << " "; 751 dbgsInsnClass(UnitBits); 752 dbgs() << "\n"; 753 }); 754 755 return NStages; 756 } 757 758 // 759 // collectAllInsnClasses - Populate allInsnClasses which is a set of units 760 // used in each stage. 761 // 762 int DFAPacketizerEmitter::collectAllInsnClasses(const std::string &ProcName, 763 std::vector<Record*> &ProcItinList, 764 std::map<std::string, unsigned> &FUNameToBitsMap, 765 std::vector<Record*> &ItinDataList, 766 int &maxStages, 767 raw_ostream &OS) { 768 // Collect all instruction classes. 769 unsigned M = ItinDataList.size(); 770 771 int numInsnClasses = 0; 772 DEBUG(dbgs() << "-----------------------------------------------------------------------------\n" 773 << "collectAllInsnClasses " 774 << ProcName 775 << " (" << M << " classes)\n"); 776 777 // Collect stages for each instruction class for all itinerary data 778 for (unsigned j = 0; j < M; j++) { 779 Record *ItinData = ItinDataList[j]; 780 int NStages = collectOneInsnClass(ProcName, ProcItinList, 781 FUNameToBitsMap, ItinData, OS); 782 if (NStages > maxStages) { 783 maxStages = NStages; 784 } 785 numInsnClasses++; 786 } 787 return numInsnClasses; 788 } 789 790 // 791 // Run the worklist algorithm to generate the DFA. 792 // 793 void DFAPacketizerEmitter::run(raw_ostream &OS) { 794 795 // Collect processor iteraries. 796 std::vector<Record*> ProcItinList = 797 Records.getAllDerivedDefinitions("ProcessorItineraries"); 798 799 // 800 // Collect the Functional units. 801 // 802 std::map<std::string, unsigned> FUNameToBitsMap; 803 int maxResources = 0; 804 collectAllFuncUnits(ProcItinList, 805 FUNameToBitsMap, maxResources, OS); 806 807 // 808 // Collect the Combo Functional units. 809 // 810 std::map<unsigned, unsigned> ComboBitToBitsMap; 811 std::vector<Record*> ComboFuncList = 812 Records.getAllDerivedDefinitions("ComboFuncUnits"); 813 int numCombos = collectAllComboFuncs(ComboFuncList, 814 FUNameToBitsMap, ComboBitToBitsMap, OS); 815 816 // 817 // Collect the itineraries. 818 // 819 int maxStages = 0; 820 int numInsnClasses = 0; 821 for (unsigned i = 0, N = ProcItinList.size(); i < N; i++) { 822 Record *Proc = ProcItinList[i]; 823 824 // Get processor itinerary name. 825 const std::string &ProcName = Proc->getName(); 826 827 // Skip default. 828 if (ProcName == "NoItineraries") 829 continue; 830 831 // Sanity check for at least one instruction itinerary class. 832 unsigned NItinClasses = 833 Records.getAllDerivedDefinitions("InstrItinClass").size(); 834 if (NItinClasses == 0) 835 return; 836 837 // Get itinerary data list. 838 std::vector<Record*> ItinDataList = Proc->getValueAsListOfDefs("IID"); 839 840 // Collect all instruction classes 841 numInsnClasses += collectAllInsnClasses(ProcName, ProcItinList, 842 FUNameToBitsMap, ItinDataList, maxStages, OS); 843 } 844 845 // 846 // Run a worklist algorithm to generate the DFA. 847 // 848 DFA D; 849 const State *Initial = &D.newState(); 850 Initial->isInitial = true; 851 Initial->stateInfo.insert(0x0); 852 SmallVector<const State*, 32> WorkList; 853 // std::queue<State*> WorkList; 854 std::map<std::set<unsigned>, const State*> Visited; 855 856 WorkList.push_back(Initial); 857 858 // 859 // Worklist algorithm to create a DFA for processor resource tracking. 860 // C = {set of InsnClasses} 861 // Begin with initial node in worklist. Initial node does not have 862 // any consumed resources, 863 // ResourceState = 0x0 864 // Visited = {} 865 // While worklist != empty 866 // S = first element of worklist 867 // For every instruction class C 868 // if we can accommodate C in S: 869 // S' = state with resource states = {S Union C} 870 // Add a new transition: S x C -> S' 871 // If S' is not in Visited: 872 // Add S' to worklist 873 // Add S' to Visited 874 // 875 while (!WorkList.empty()) { 876 const State *current = WorkList.pop_back_val(); 877 DEBUG({ 878 dbgs() << "---------------------\n"; 879 dbgs() << "Processing state: " << current->stateNum << " - "; 880 dbgsStateInfo(current->stateInfo); 881 dbgs() << "\n"; 882 }); 883 for (unsigned i = 0; i < allInsnClasses.size(); i++) { 884 std::vector<unsigned> InsnClass = allInsnClasses[i]; 885 DEBUG({ 886 dbgs() << i << " "; 887 dbgsInsnClass(InsnClass); 888 dbgs() << "\n"; 889 }); 890 891 std::set<unsigned> NewStateResources; 892 // 893 // If we haven't already created a transition for this input 894 // and the state can accommodate this InsnClass, create a transition. 895 // 896 if (!current->hasTransition(InsnClass) && 897 current->canMaybeAddInsnClass(InsnClass, ComboBitToBitsMap)) { 898 const State *NewState = NULL; 899 current->AddInsnClass(InsnClass, ComboBitToBitsMap, NewStateResources); 900 if (NewStateResources.size() == 0) { 901 DEBUG(dbgs() << " Skipped - no new states generated\n"); 902 continue; 903 } 904 905 DEBUG({ 906 dbgs() << "\t"; 907 dbgsStateInfo(NewStateResources); 908 dbgs() << "\n"; 909 }); 910 911 // 912 // If we have seen this state before, then do not create a new state. 913 // 914 auto VI = Visited.find(NewStateResources); 915 if (VI != Visited.end()) { 916 NewState = VI->second; 917 DEBUG({ 918 dbgs() << "\tFound existing state: " << NewState->stateNum 919 << " - "; 920 dbgsStateInfo(NewState->stateInfo); 921 dbgs() << "\n"; 922 }); 923 } else { 924 NewState = &D.newState(); 925 NewState->stateInfo = NewStateResources; 926 Visited[NewStateResources] = NewState; 927 WorkList.push_back(NewState); 928 DEBUG({ 929 dbgs() << "\tAccepted new state: " << NewState->stateNum << " - "; 930 dbgsStateInfo(NewState->stateInfo); 931 dbgs() << "\n"; 932 }); 933 } 934 935 current->addTransition(InsnClass, NewState); 936 } 937 } 938 } 939 940 // Print out the table. 941 D.writeTableAndAPI(OS, TargetName, 942 numInsnClasses, maxResources, numCombos, maxStages); 943 } 944 945 namespace llvm { 946 947 void EmitDFAPacketizer(RecordKeeper &RK, raw_ostream &OS) { 948 emitSourceFileHeader("Target DFA Packetizer Tables", OS); 949 DFAPacketizerEmitter(RK).run(OS); 950 } 951 952 } // End llvm namespace 953