DFAPacketizer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/CodeGen/MachineInstrBundle.h"
  28. #include "llvm/CodeGen/ScheduleDAGInstrs.h"
  29. #include "llvm/MC/MCInstrItineraries.h"
  30. #include "llvm/Target/TargetInstrInfo.h"
  31. using namespace llvm;
  32. DFAPacketizer::DFAPacketizer(const InstrItineraryData *I,
  33. const DFAStateInput (*SIT)[2],
  34. const unsigned *SET):
  35. InstrItins(I), CurrentState(0), DFAStateInputTable(SIT),
  36. DFAStateEntryTable(SET) {
  37. // Make sure DFA types are large enough for the number of terms & resources.
  38. assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAInput))
  39. && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAInput");
  40. assert((DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) <= (8 * sizeof(DFAStateInput))
  41. && "(DFA_MAX_RESTERMS * DFA_MAX_RESOURCES) too big for DFAStateInput");
  42. }
  43. //
  44. // ReadTable - Read the DFA transition table and update CachedTable.
  45. //
  46. // Format of the transition tables:
  47. // DFAStateInputTable[][2] = pairs of <Input, Transition> for all valid
  48. // transitions
  49. // DFAStateEntryTable[i] = Index of the first entry in DFAStateInputTable
  50. // for the ith state
  51. //
  52. void DFAPacketizer::ReadTable(unsigned int state) {
  53. unsigned ThisState = DFAStateEntryTable[state];
  54. unsigned NextStateInTable = DFAStateEntryTable[state+1];
  55. // Early exit in case CachedTable has already contains this
  56. // state's transitions.
  57. if (CachedTable.count(UnsignPair(state,
  58. DFAStateInputTable[ThisState][0])))
  59. return;
  60. for (unsigned i = ThisState; i < NextStateInTable; i++)
  61. CachedTable[UnsignPair(state, DFAStateInputTable[i][0])] =
  62. DFAStateInputTable[i][1];
  63. }
  64. //
  65. // getInsnInput - Return the DFAInput for an instruction class.
  66. //
  67. DFAInput DFAPacketizer::getInsnInput(unsigned InsnClass) {
  68. // note: this logic must match that in DFAPacketizer.h for input vectors
  69. DFAInput InsnInput = 0;
  70. unsigned i = 0;
  71. for (const InstrStage *IS = InstrItins->beginStage(InsnClass),
  72. *IE = InstrItins->endStage(InsnClass); IS != IE; ++IS, ++i) {
  73. unsigned FuncUnits = IS->getUnits();
  74. InsnInput <<= DFA_MAX_RESOURCES; // Shift over any previous AND'ed terms.
  75. InsnInput |= FuncUnits;
  76. assert ((i < DFA_MAX_RESTERMS) && "Exceeded maximum number of DFA inputs");
  77. }
  78. return InsnInput;
  79. }
  80. // canReserveResources - Check if the resources occupied by a MCInstrDesc
  81. // are available in the current state.
  82. bool DFAPacketizer::canReserveResources(const llvm::MCInstrDesc *MID) {
  83. unsigned InsnClass = MID->getSchedClass();
  84. DFAInput InsnInput = getInsnInput(InsnClass);
  85. UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
  86. ReadTable(CurrentState);
  87. return (CachedTable.count(StateTrans) != 0);
  88. }
  89. // reserveResources - Reserve the resources occupied by a MCInstrDesc and
  90. // change the current state to reflect that change.
  91. void DFAPacketizer::reserveResources(const llvm::MCInstrDesc *MID) {
  92. unsigned InsnClass = MID->getSchedClass();
  93. DFAInput InsnInput = getInsnInput(InsnClass);
  94. UnsignPair StateTrans = UnsignPair(CurrentState, InsnInput);
  95. ReadTable(CurrentState);
  96. assert(CachedTable.count(StateTrans) != 0);
  97. CurrentState = CachedTable[StateTrans];
  98. }
  99. // canReserveResources - Check if the resources occupied by a machine
  100. // instruction are available in the current state.
  101. bool DFAPacketizer::canReserveResources(llvm::MachineInstr *MI) {
  102. const llvm::MCInstrDesc &MID = MI->getDesc();
  103. return canReserveResources(&MID);
  104. }
  105. // reserveResources - Reserve the resources occupied by a machine
  106. // instruction and change the current state to reflect that change.
  107. void DFAPacketizer::reserveResources(llvm::MachineInstr *MI) {
  108. const llvm::MCInstrDesc &MID = MI->getDesc();
  109. reserveResources(&MID);
  110. }
  111. namespace llvm {
  112. // DefaultVLIWScheduler - This class extends ScheduleDAGInstrs and overrides
  113. // Schedule method to build the dependence graph.
  114. class DefaultVLIWScheduler : public ScheduleDAGInstrs {
  115. public:
  116. DefaultVLIWScheduler(MachineFunction &MF, MachineLoopInfo &MLI);
  117. // Schedule - Actual scheduling work.
  118. void schedule() override;
  119. };
  120. }
  121. DefaultVLIWScheduler::DefaultVLIWScheduler(MachineFunction &MF,
  122. MachineLoopInfo &MLI)
  123. : ScheduleDAGInstrs(MF, &MLI) {
  124. CanHandleTerminators = true;
  125. }
  126. void DefaultVLIWScheduler::schedule() {
  127. // Build the scheduling graph.
  128. buildSchedGraph(nullptr);
  129. }
  130. // VLIWPacketizerList Ctor
  131. VLIWPacketizerList::VLIWPacketizerList(MachineFunction &MF,
  132. MachineLoopInfo &MLI)
  133. : MF(MF) {
  134. TII = MF.getSubtarget().getInstrInfo();
  135. ResourceTracker = TII->CreateTargetScheduleState(MF.getSubtarget());
  136. VLIWScheduler = new DefaultVLIWScheduler(MF, MLI);
  137. }
  138. // VLIWPacketizerList Dtor
  139. VLIWPacketizerList::~VLIWPacketizerList() {
  140. if (VLIWScheduler)
  141. delete VLIWScheduler;
  142. if (ResourceTracker)
  143. delete ResourceTracker;
  144. }
  145. // endPacket - End the current packet, bundle packet instructions and reset
  146. // DFA state.
  147. void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
  148. MachineInstr *MI) {
  149. if (CurrentPacketMIs.size() > 1) {
  150. MachineInstr *MIFirst = CurrentPacketMIs.front();
  151. finalizeBundle(*MBB, MIFirst->getIterator(), MI->getIterator());
  152. }
  153. CurrentPacketMIs.clear();
  154. ResourceTracker->clearResources();
  155. }
  156. // PacketizeMIs - Bundle machine instructions into packets.
  157. void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
  158. MachineBasicBlock::iterator BeginItr,
  159. MachineBasicBlock::iterator EndItr) {
  160. assert(VLIWScheduler && "VLIW Scheduler is not initialized!");
  161. VLIWScheduler->startBlock(MBB);
  162. VLIWScheduler->enterRegion(MBB, BeginItr, EndItr,
  163. std::distance(BeginItr, EndItr));
  164. VLIWScheduler->schedule();
  165. // Generate MI -> SU map.
  166. MIToSUnit.clear();
  167. for (unsigned i = 0, e = VLIWScheduler->SUnits.size(); i != e; ++i) {
  168. SUnit *SU = &VLIWScheduler->SUnits[i];
  169. MIToSUnit[SU->getInstr()] = SU;
  170. }
  171. // The main packetizer loop.
  172. for (; BeginItr != EndItr; ++BeginItr) {
  173. MachineInstr *MI = BeginItr;
  174. this->initPacketizerState();
  175. // End the current packet if needed.
  176. if (this->isSoloInstruction(MI)) {
  177. endPacket(MBB, MI);
  178. continue;
  179. }
  180. // Ignore pseudo instructions.
  181. if (this->ignorePseudoInstruction(MI, MBB))
  182. continue;
  183. SUnit *SUI = MIToSUnit[MI];
  184. assert(SUI && "Missing SUnit Info!");
  185. // Ask DFA if machine resource is available for MI.
  186. bool ResourceAvail = ResourceTracker->canReserveResources(MI);
  187. if (ResourceAvail) {
  188. // Dependency check for MI with instructions in CurrentPacketMIs.
  189. for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
  190. VE = CurrentPacketMIs.end(); VI != VE; ++VI) {
  191. MachineInstr *MJ = *VI;
  192. SUnit *SUJ = MIToSUnit[MJ];
  193. assert(SUJ && "Missing SUnit Info!");
  194. // Is it legal to packetize SUI and SUJ together.
  195. if (!this->isLegalToPacketizeTogether(SUI, SUJ)) {
  196. // Allow packetization if dependency can be pruned.
  197. if (!this->isLegalToPruneDependencies(SUI, SUJ)) {
  198. // End the packet if dependency cannot be pruned.
  199. endPacket(MBB, MI);
  200. break;
  201. } // !isLegalToPruneDependencies.
  202. } // !isLegalToPacketizeTogether.
  203. } // For all instructions in CurrentPacketMIs.
  204. } else {
  205. // End the packet if resource is not available.
  206. endPacket(MBB, MI);
  207. }
  208. // Add MI to the current packet.
  209. BeginItr = this->addToPacket(MI);
  210. } // For all instructions in BB.
  211. // End any packet left behind.
  212. endPacket(MBB, EndItr);
  213. VLIWScheduler->exitRegion();
  214. VLIWScheduler->finishBlock();
  215. }