DFAPacketizer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //=- llvm/CodeGen/DFAPacketizer.h - 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. #ifndef LLVM_CODEGEN_DFAPACKETIZER_H
  26. #define LLVM_CODEGEN_DFAPACKETIZER_H
  27. #include "llvm/ADT/DenseMap.h"
  28. #include "llvm/CodeGen/MachineBasicBlock.h"
  29. #include <map>
  30. namespace llvm {
  31. class MCInstrDesc;
  32. class MachineInstr;
  33. class MachineLoopInfo;
  34. class MachineDominatorTree;
  35. class InstrItineraryData;
  36. class DefaultVLIWScheduler;
  37. class SUnit;
  38. // --------------------------------------------------------------------
  39. // Definitions shared between DFAPacketizer.cpp and DFAPacketizerEmitter.cpp
  40. // DFA_MAX_RESTERMS * DFA_MAX_RESOURCES must fit within sizeof DFAInput.
  41. // This is verified in DFAPacketizer.cpp:DFAPacketizer::DFAPacketizer.
  42. //
  43. // e.g. terms x resource bit combinations that fit in uint32_t:
  44. // 4 terms x 8 bits = 32 bits
  45. // 3 terms x 10 bits = 30 bits
  46. // 2 terms x 16 bits = 32 bits
  47. //
  48. // e.g. terms x resource bit combinations that fit in uint64_t:
  49. // 8 terms x 8 bits = 64 bits
  50. // 7 terms x 9 bits = 63 bits
  51. // 6 terms x 10 bits = 60 bits
  52. // 5 terms x 12 bits = 60 bits
  53. // 4 terms x 16 bits = 64 bits <--- current
  54. // 3 terms x 21 bits = 63 bits
  55. // 2 terms x 32 bits = 64 bits
  56. //
  57. #define DFA_MAX_RESTERMS 4 // The max # of AND'ed resource terms.
  58. #define DFA_MAX_RESOURCES 16 // The max # of resource bits in one term.
  59. typedef uint64_t DFAInput;
  60. typedef int64_t DFAStateInput;
  61. #define DFA_TBLTYPE "int64_t" // For generating DFAStateInputTable.
  62. // --------------------------------------------------------------------
  63. class DFAPacketizer {
  64. private:
  65. typedef std::pair<unsigned, DFAInput> UnsignPair;
  66. const InstrItineraryData *InstrItins;
  67. int CurrentState;
  68. const DFAStateInput (*DFAStateInputTable)[2];
  69. const unsigned *DFAStateEntryTable;
  70. // CachedTable is a map from <FromState, Input> to ToState.
  71. DenseMap<UnsignPair, unsigned> CachedTable;
  72. // ReadTable - Read the DFA transition table and update CachedTable.
  73. void ReadTable(unsigned state);
  74. public:
  75. DFAPacketizer(const InstrItineraryData *I, const DFAStateInput (*SIT)[2],
  76. const unsigned *SET);
  77. // Reset the current state to make all resources available.
  78. void clearResources() {
  79. CurrentState = 0;
  80. }
  81. // getInsnInput - Return the DFAInput for an instruction class.
  82. DFAInput getInsnInput(unsigned InsnClass);
  83. // getInsnInput - Return the DFAInput for an instruction class input vector.
  84. static DFAInput getInsnInput(const std::vector<unsigned> &InsnClass);
  85. // canReserveResources - Check if the resources occupied by a MCInstrDesc
  86. // are available in the current state.
  87. bool canReserveResources(const llvm::MCInstrDesc *MID);
  88. // reserveResources - Reserve the resources occupied by a MCInstrDesc and
  89. // change the current state to reflect that change.
  90. void reserveResources(const llvm::MCInstrDesc *MID);
  91. // canReserveResources - Check if the resources occupied by a machine
  92. // instruction are available in the current state.
  93. bool canReserveResources(llvm::MachineInstr *MI);
  94. // reserveResources - Reserve the resources occupied by a machine
  95. // instruction and change the current state to reflect that change.
  96. void reserveResources(llvm::MachineInstr *MI);
  97. const InstrItineraryData *getInstrItins() const { return InstrItins; }
  98. };
  99. // VLIWPacketizerList - Implements a simple VLIW packetizer using DFA. The
  100. // packetizer works on machine basic blocks. For each instruction I in BB, the
  101. // packetizer consults the DFA to see if machine resources are available to
  102. // execute I. If so, the packetizer checks if I depends on any instruction J in
  103. // the current packet. If no dependency is found, I is added to current packet
  104. // and machine resource is marked as taken. If any dependency is found, a target
  105. // API call is made to prune the dependence.
  106. class VLIWPacketizerList {
  107. protected:
  108. MachineFunction &MF;
  109. const TargetInstrInfo *TII;
  110. // The VLIW Scheduler.
  111. DefaultVLIWScheduler *VLIWScheduler;
  112. // Vector of instructions assigned to the current packet.
  113. std::vector<MachineInstr*> CurrentPacketMIs;
  114. // DFA resource tracker.
  115. DFAPacketizer *ResourceTracker;
  116. // Generate MI -> SU map.
  117. std::map<MachineInstr*, SUnit*> MIToSUnit;
  118. public:
  119. VLIWPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI);
  120. virtual ~VLIWPacketizerList();
  121. // PacketizeMIs - Implement this API in the backend to bundle instructions.
  122. void PacketizeMIs(MachineBasicBlock *MBB,
  123. MachineBasicBlock::iterator BeginItr,
  124. MachineBasicBlock::iterator EndItr);
  125. // getResourceTracker - return ResourceTracker
  126. DFAPacketizer *getResourceTracker() {return ResourceTracker;}
  127. // addToPacket - Add MI to the current packet.
  128. virtual MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
  129. MachineBasicBlock::iterator MII = MI;
  130. CurrentPacketMIs.push_back(MI);
  131. ResourceTracker->reserveResources(MI);
  132. return MII;
  133. }
  134. // endPacket - End the current packet.
  135. void endPacket(MachineBasicBlock *MBB, MachineInstr *MI);
  136. // initPacketizerState - perform initialization before packetizing
  137. // an instruction. This function is supposed to be overrided by
  138. // the target dependent packetizer.
  139. virtual void initPacketizerState() { return; }
  140. // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
  141. virtual bool ignorePseudoInstruction(MachineInstr *I,
  142. MachineBasicBlock *MBB) {
  143. return false;
  144. }
  145. // isSoloInstruction - return true if instruction MI can not be packetized
  146. // with any other instruction, which means that MI itself is a packet.
  147. virtual bool isSoloInstruction(MachineInstr *MI) {
  148. return true;
  149. }
  150. // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
  151. // together.
  152. virtual bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
  153. return false;
  154. }
  155. // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
  156. // and SUJ.
  157. virtual bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
  158. return false;
  159. }
  160. };
  161. }
  162. #endif