DFAPacketizer.cpp 8.2 KB

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