DFAPacketizer.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/Target/TargetInstrInfo.h"
  30. #include "llvm/MC/MCInstrItineraries.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 {
  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. MachineDominatorTree &MDT, bool IsPostRA);
  97. // Schedule - Actual scheduling work.
  98. void schedule();
  99. };
  100. } // end anonymous namespace
  101. DefaultVLIWScheduler::DefaultVLIWScheduler(
  102. MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
  103. bool IsPostRA) :
  104. ScheduleDAGInstrs(MF, MLI, MDT, IsPostRA) {
  105. }
  106. void DefaultVLIWScheduler::schedule() {
  107. // Build the scheduling graph.
  108. buildSchedGraph(0);
  109. }
  110. // VLIWPacketizerList Ctor
  111. VLIWPacketizerList::VLIWPacketizerList(
  112. MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree &MDT,
  113. bool IsPostRA) : TM(MF.getTarget()), MF(MF) {
  114. TII = TM.getInstrInfo();
  115. ResourceTracker = TII->CreateTargetScheduleState(&TM, 0);
  116. SchedulerImpl = new DefaultVLIWScheduler(MF, MLI, MDT, IsPostRA);
  117. }
  118. // VLIWPacketizerList Dtor
  119. VLIWPacketizerList::~VLIWPacketizerList() {
  120. delete SchedulerImpl;
  121. delete ResourceTracker;
  122. }
  123. // ignorePseudoInstruction - ignore pseudo instructions.
  124. bool VLIWPacketizerList::ignorePseudoInstruction(MachineInstr *MI,
  125. MachineBasicBlock *MBB) {
  126. if (MI->isDebugValue())
  127. return true;
  128. if (TII->isSchedulingBoundary(MI, MBB, MF))
  129. return true;
  130. return false;
  131. }
  132. // isSoloInstruction - return true if instruction I must end previous
  133. // packet.
  134. bool VLIWPacketizerList::isSoloInstruction(MachineInstr *I) {
  135. if (I->isInlineAsm())
  136. return true;
  137. return false;
  138. }
  139. // addToPacket - Add I to the current packet and reserve resource.
  140. void VLIWPacketizerList::addToPacket(MachineInstr *MI) {
  141. CurrentPacketMIs.push_back(MI);
  142. ResourceTracker->reserveResources(MI);
  143. }
  144. // endPacket - End the current packet, bundle packet instructions and reset
  145. // DFA state.
  146. void VLIWPacketizerList::endPacket(MachineBasicBlock *MBB,
  147. MachineInstr *I) {
  148. if (CurrentPacketMIs.size() > 1) {
  149. MachineInstr *MIFirst = CurrentPacketMIs.front();
  150. finalizeBundle(*MBB, MIFirst, I);
  151. }
  152. CurrentPacketMIs.clear();
  153. ResourceTracker->clearResources();
  154. }
  155. // PacketizeMIs - Bundle machine instructions into packets.
  156. void VLIWPacketizerList::PacketizeMIs(MachineBasicBlock *MBB,
  157. MachineBasicBlock::iterator BeginItr,
  158. MachineBasicBlock::iterator EndItr) {
  159. assert(MBB->end() == EndItr && "Bad EndIndex");
  160. SchedulerImpl->enterRegion(MBB, BeginItr, EndItr, MBB->size());
  161. // Build the DAG without reordering instructions.
  162. SchedulerImpl->schedule();
  163. // Remember scheduling units.
  164. SUnits = SchedulerImpl->SUnits;
  165. // The main packetizer loop.
  166. for (; BeginItr != EndItr; ++BeginItr) {
  167. MachineInstr *MI = BeginItr;
  168. // Ignore pseudo instructions.
  169. if (ignorePseudoInstruction(MI, MBB))
  170. continue;
  171. // End the current packet if needed.
  172. if (isSoloInstruction(MI)) {
  173. endPacket(MBB, MI);
  174. continue;
  175. }
  176. SUnit *SUI = SchedulerImpl->getSUnit(MI);
  177. assert(SUI && "Missing SUnit Info!");
  178. // Ask DFA if machine resource is available for MI.
  179. bool ResourceAvail = ResourceTracker->canReserveResources(MI);
  180. if (ResourceAvail) {
  181. // Dependency check for MI with instructions in CurrentPacketMIs.
  182. for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
  183. VE = CurrentPacketMIs.end(); VI != VE; ++VI) {
  184. MachineInstr *MJ = *VI;
  185. SUnit *SUJ = SchedulerImpl->getSUnit(MJ);
  186. assert(SUJ && "Missing SUnit Info!");
  187. // Is it legal to packetize SUI and SUJ together.
  188. if (!isLegalToPacketizeTogether(SUI, SUJ)) {
  189. // Allow packetization if dependency can be pruned.
  190. if (!isLegalToPruneDependencies(SUI, SUJ)) {
  191. // End the packet if dependency cannot be pruned.
  192. endPacket(MBB, MI);
  193. break;
  194. } // !isLegalToPruneDependencies.
  195. } // !isLegalToPacketizeTogether.
  196. } // For all instructions in CurrentPacketMIs.
  197. } else {
  198. // End the packet if resource is not available.
  199. endPacket(MBB, MI);
  200. }
  201. // Add MI to the current packet.
  202. addToPacket(MI);
  203. } // For all instructions in BB.
  204. // End any packet left behind.
  205. endPacket(MBB, EndItr);
  206. SchedulerImpl->exitRegion();
  207. }