DFAPacketizer.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //=- llvm/CodeGen/DFAPacketizer.cpp - DFA Packetizer for VLIW -*- C++ -*-=====//
  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. // This class implements a deterministic finite automaton (DFA) based
  10. // packetizing mechanism for VLIW architectures. It provides APIs to
  11. // determine whether there exists a legal mapping of instructions to
  12. // functional unit assignments in a packet. The DFA is auto-generated from
  13. // the target's Schedule.td file.
  14. //
  15. // A DFA consists of 3 major elements: states, inputs, and transitions. For
  16. // the packetizing mechanism, the input is the set of instruction classes for
  17. // a target. The state models all possible combinations of functional unit
  18. // consumption for a given set of instructions in a packet. A transition
  19. // models the addition of an instruction to a packet. In the DFA constructed
  20. // by this class, if an instruction can be added to a packet, then a valid
  21. // transition exists from the corresponding state. Invalid transitions
  22. // indicate that the instruction cannot be added to the current packet.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #include "llvm/CodeGen/DFAPacketizer.h"
  26. #include "llvm/CodeGen/MachineInstr.h"
  27. #include "llvm/MC/MCInstrItineraries.h"
  28. using namespace llvm;
  29. DFAPacketizer::DFAPacketizer(const InstrItineraryData *I, const int (*SIT)[2],
  30. const unsigned* SET):
  31. InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
  32. DFAStateEntryTable(SET) {}
  33. //
  34. // ReadTable - Read the DFA transition table and update CachedTable.
  35. //
  36. // Format of the transition tables:
  37. // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
  38. // transitions
  39. // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
  40. // for the ith state
  41. //
  42. void DFAPacketizer::ReadTable(unsigned int state) {
  43. unsigned ThisState = DFAStateEntryTable[state];
  44. unsigned NextStateInTable = DFAStateEntryTable[state+1];
  45. // Early exit in case CachedTable has already contains this
  46. // state's transitions.
  47. if (CachedTable.count(UnsignPair(state,
  48. DFAStateInputTable[ThisState][0])))
  49. return;
  50. for (unsigned i = ThisState; i < NextStateInTable; i++)
  51. CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
  52. DFAStateInputTable[i][1];
  53. }
  54. // canReserveResources - Check if the resources occupied by a MCInstrDesc
  55. // are available in the current state.
  56. bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc* MID) {
  57. unsigned InsnClass = MID->getSchedClass();
  58. const llvm::InstrStage* IS = InstrItins->beginStage(InsnClass);
  59. unsigned FuncUnits = IS->getUnits();
  60. UnsignPair StateTrans = UnsignPair(CurrentState, FuncUnits);
  61. ReadTable(CurrentState);
  62. return (CachedTable.count(StateTrans) != 0);
  63. }
  64. // reserveResources - Reserve the resources occupied by a MCInstrDesc and
  65. // change the current state to reflect that change.
  66. void DFAPacketizer::reserveResources(const llvm::MCInstrDesc* MID) {
  67. unsigned InsnClass = MID->getSchedClass();
  68. const llvm::InstrStage* IS = InstrItins->beginStage(InsnClass);
  69. unsigned FuncUnits = IS->getUnits();
  70. UnsignPair StateTrans = UnsignPair(CurrentState, FuncUnits);
  71. ReadTable(CurrentState);
  72. assert(CachedTable.count(StateTrans) != 0);
  73. CurrentState = CachedTable[StateTrans];
  74. }
  75. // canReserveResources - Check if the resources occupied by a machine
  76. // instruction are available in the current state.
  77. bool DFAPacketizer::canReserveResources(llvm::MachineInstr* MI) {
  78. const llvm::MCInstrDesc& MID = MI->getDesc();
  79. return canReserveResources(&MID);
  80. }
  81. // reserveResources - Reserve the resources occupied by a machine
  82. // instruction and change the current state to reflect that change.
  83. void DFAPacketizer::reserveResources(llvm::MachineInstr* MI) {
  84. const llvm::MCInstrDesc& MID = MI->getDesc();
  85. reserveResources(&MID);
  86. }