DFAPacketizer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. class DFAPacketizer {
  39. private:
  40. typedef std::pair<unsigned, unsigned> UnsignPair;
  41. const InstrItineraryData *InstrItins;
  42. int CurrentState;
  43. const int (*DFAStateInputTable)[2];
  44. const unsigned *DFAStateEntryTable;
  45. // CachedTable is a map from <FromState, Input> to ToState.
  46. DenseMap<UnsignPair, unsigned> CachedTable;
  47. // ReadTable - Read the DFA transition table and update CachedTable.
  48. void ReadTable(unsigned int state);
  49. public:
  50. DFAPacketizer(const InstrItineraryData *I, const int (*SIT)[2],
  51. const unsigned *SET);
  52. // Reset the current state to make all resources available.
  53. void clearResources() {
  54. CurrentState = 0;
  55. }
  56. // canReserveResources - Check if the resources occupied by a MCInstrDesc
  57. // are available in the current state.
  58. bool canReserveResources(const llvm::MCInstrDesc *MID);
  59. // reserveResources - Reserve the resources occupied by a MCInstrDesc and
  60. // change the current state to reflect that change.
  61. void reserveResources(const llvm::MCInstrDesc *MID);
  62. // canReserveResources - Check if the resources occupied by a machine
  63. // instruction are available in the current state.
  64. bool canReserveResources(llvm::MachineInstr *MI);
  65. // reserveResources - Reserve the resources occupied by a machine
  66. // instruction and change the current state to reflect that change.
  67. void reserveResources(llvm::MachineInstr *MI);
  68. const InstrItineraryData *getInstrItins() const { return InstrItins; }
  69. };
  70. // VLIWPacketizerList - Implements a simple VLIW packetizer using DFA. The
  71. // packetizer works on machine basic blocks. For each instruction I in BB, the
  72. // packetizer consults the DFA to see if machine resources are available to
  73. // execute I. If so, the packetizer checks if I depends on any instruction J in
  74. // the current packet. If no dependency is found, I is added to current packet
  75. // and machine resource is marked as taken. If any dependency is found, a target
  76. // API call is made to prune the dependence.
  77. class VLIWPacketizerList {
  78. protected:
  79. MachineFunction &MF;
  80. const TargetInstrInfo *TII;
  81. // The VLIW Scheduler.
  82. DefaultVLIWScheduler *VLIWScheduler;
  83. // Vector of instructions assigned to the current packet.
  84. std::vector<MachineInstr*> CurrentPacketMIs;
  85. // DFA resource tracker.
  86. DFAPacketizer *ResourceTracker;
  87. // Generate MI -> SU map.
  88. std::map<MachineInstr*, SUnit*> MIToSUnit;
  89. public:
  90. VLIWPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI, bool IsPostRA);
  91. virtual ~VLIWPacketizerList();
  92. // PacketizeMIs - Implement this API in the backend to bundle instructions.
  93. void PacketizeMIs(MachineBasicBlock *MBB,
  94. MachineBasicBlock::iterator BeginItr,
  95. MachineBasicBlock::iterator EndItr);
  96. // getResourceTracker - return ResourceTracker
  97. DFAPacketizer *getResourceTracker() {return ResourceTracker;}
  98. // addToPacket - Add MI to the current packet.
  99. virtual MachineBasicBlock::iterator addToPacket(MachineInstr *MI) {
  100. MachineBasicBlock::iterator MII = MI;
  101. CurrentPacketMIs.push_back(MI);
  102. ResourceTracker->reserveResources(MI);
  103. return MII;
  104. }
  105. // endPacket - End the current packet.
  106. void endPacket(MachineBasicBlock *MBB, MachineInstr *MI);
  107. // initPacketizerState - perform initialization before packetizing
  108. // an instruction. This function is supposed to be overrided by
  109. // the target dependent packetizer.
  110. virtual void initPacketizerState() { return; }
  111. // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
  112. virtual bool ignorePseudoInstruction(MachineInstr *I,
  113. MachineBasicBlock *MBB) {
  114. return false;
  115. }
  116. // isSoloInstruction - return true if instruction MI can not be packetized
  117. // with any other instruction, which means that MI itself is a packet.
  118. virtual bool isSoloInstruction(MachineInstr *MI) {
  119. return true;
  120. }
  121. // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
  122. // together.
  123. virtual bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
  124. return false;
  125. }
  126. // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
  127. // and SUJ.
  128. virtual bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
  129. return false;
  130. }
  131. };
  132. }
  133. #endif