DFAPacketizer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //===- llvm/CodeGen/DFAPacketizer.h - DFA Packetizer for VLIW ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // This class implements a deterministic finite automaton (DFA) based
  9. // packetizing mechanism for VLIW architectures. It provides APIs to
  10. // determine whether there exists a legal mapping of instructions to
  11. // functional unit assignments in a packet. The DFA is auto-generated from
  12. // the target's Schedule.td file.
  13. //
  14. // A DFA consists of 3 major elements: states, inputs, and transitions. For
  15. // the packetizing mechanism, the input is the set of instruction classes for
  16. // a target. The state models all possible combinations of functional unit
  17. // consumption for a given set of instructions in a packet. A transition
  18. // models the addition of an instruction to a packet. In the DFA constructed
  19. // by this class, if an instruction can be added to a packet, then a valid
  20. // transition exists from the corresponding state. Invalid transitions
  21. // indicate that the instruction cannot be added to the current packet.
  22. //
  23. //===----------------------------------------------------------------------===//
  24. #ifndef LLVM_CODEGEN_DFAPACKETIZER_H
  25. #define LLVM_CODEGEN_DFAPACKETIZER_H
  26. #include "llvm/ADT/DenseMap.h"
  27. #include "llvm/CodeGen/MachineBasicBlock.h"
  28. #include "llvm/CodeGen/ScheduleDAGMutation.h"
  29. #include "llvm/Support/Automaton.h"
  30. #include <cstdint>
  31. #include <map>
  32. #include <memory>
  33. #include <utility>
  34. #include <vector>
  35. namespace llvm {
  36. class DefaultVLIWScheduler;
  37. class InstrItineraryData;
  38. class MachineFunction;
  39. class MachineInstr;
  40. class MachineLoopInfo;
  41. class MCInstrDesc;
  42. class SUnit;
  43. class TargetInstrInfo;
  44. class DFAPacketizer {
  45. private:
  46. const InstrItineraryData *InstrItins;
  47. Automaton<uint64_t> A;
  48. /// For every itinerary, an "action" to apply to the automaton. This removes
  49. /// the redundancy in actions between itinerary classes.
  50. ArrayRef<unsigned> ItinActions;
  51. public:
  52. DFAPacketizer(const InstrItineraryData *InstrItins, Automaton<uint64_t> a,
  53. ArrayRef<unsigned> ItinActions)
  54. : InstrItins(InstrItins), A(std::move(a)), ItinActions(ItinActions) {
  55. // Start off with resource tracking disabled.
  56. A.enableTranscription(false);
  57. }
  58. // Reset the current state to make all resources available.
  59. void clearResources() {
  60. A.reset();
  61. }
  62. // Set whether this packetizer should track not just whether instructions
  63. // can be packetized, but also which functional units each instruction ends up
  64. // using after packetization.
  65. void setTrackResources(bool Track) {
  66. A.enableTranscription(Track);
  67. }
  68. // Check if the resources occupied by a MCInstrDesc are available in
  69. // the current state.
  70. bool canReserveResources(const MCInstrDesc *MID);
  71. // Reserve the resources occupied by a MCInstrDesc and change the current
  72. // state to reflect that change.
  73. void reserveResources(const MCInstrDesc *MID);
  74. // Check if the resources occupied by a machine instruction are available
  75. // in the current state.
  76. bool canReserveResources(MachineInstr &MI);
  77. // Reserve the resources occupied by a machine instruction and change the
  78. // current state to reflect that change.
  79. void reserveResources(MachineInstr &MI);
  80. // Return the resources used by the InstIdx'th instruction added to this
  81. // packet. The resources are returned as a bitvector of functional units.
  82. //
  83. // Note that a bundle may be packed in multiple valid ways. This function
  84. // returns one arbitary valid packing.
  85. //
  86. // Requires setTrackResources(true) to have been called.
  87. unsigned getUsedResources(unsigned InstIdx);
  88. const InstrItineraryData *getInstrItins() const { return InstrItins; }
  89. };
  90. // VLIWPacketizerList implements a simple VLIW packetizer using DFA. The
  91. // packetizer works on machine basic blocks. For each instruction I in BB,
  92. // the packetizer consults the DFA to see if machine resources are available
  93. // to execute I. If so, the packetizer checks if I depends on any instruction
  94. // in the current packet. If no dependency is found, I is added to current
  95. // packet and the machine resource is marked as taken. If any dependency is
  96. // found, a target API call is made to prune the dependence.
  97. class VLIWPacketizerList {
  98. protected:
  99. MachineFunction &MF;
  100. const TargetInstrInfo *TII;
  101. AAResults *AA;
  102. // The VLIW Scheduler.
  103. DefaultVLIWScheduler *VLIWScheduler;
  104. // Vector of instructions assigned to the current packet.
  105. std::vector<MachineInstr*> CurrentPacketMIs;
  106. // DFA resource tracker.
  107. DFAPacketizer *ResourceTracker;
  108. // Map: MI -> SU.
  109. std::map<MachineInstr*, SUnit*> MIToSUnit;
  110. public:
  111. // The AAResults parameter can be nullptr.
  112. VLIWPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
  113. AAResults *AA);
  114. virtual ~VLIWPacketizerList();
  115. // Implement this API in the backend to bundle instructions.
  116. void PacketizeMIs(MachineBasicBlock *MBB,
  117. MachineBasicBlock::iterator BeginItr,
  118. MachineBasicBlock::iterator EndItr);
  119. // Return the ResourceTracker.
  120. DFAPacketizer *getResourceTracker() {return ResourceTracker;}
  121. // addToPacket - Add MI to the current packet.
  122. virtual MachineBasicBlock::iterator addToPacket(MachineInstr &MI) {
  123. CurrentPacketMIs.push_back(&MI);
  124. ResourceTracker->reserveResources(MI);
  125. return MI;
  126. }
  127. // End the current packet and reset the state of the packetizer.
  128. // Overriding this function allows the target-specific packetizer
  129. // to perform custom finalization.
  130. virtual void endPacket(MachineBasicBlock *MBB,
  131. MachineBasicBlock::iterator MI);
  132. // Perform initialization before packetizing an instruction. This
  133. // function is supposed to be overrided by the target dependent packetizer.
  134. virtual void initPacketizerState() {}
  135. // Check if the given instruction I should be ignored by the packetizer.
  136. virtual bool ignorePseudoInstruction(const MachineInstr &I,
  137. const MachineBasicBlock *MBB) {
  138. return false;
  139. }
  140. // Return true if instruction MI can not be packetized with any other
  141. // instruction, which means that MI itself is a packet.
  142. virtual bool isSoloInstruction(const MachineInstr &MI) { return true; }
  143. // Check if the packetizer should try to add the given instruction to
  144. // the current packet. One reasons for which it may not be desirable
  145. // to include an instruction in the current packet could be that it
  146. // would cause a stall.
  147. // If this function returns "false", the current packet will be ended,
  148. // and the instruction will be added to the next packet.
  149. virtual bool shouldAddToPacket(const MachineInstr &MI) { return true; }
  150. // Check if it is legal to packetize SUI and SUJ together.
  151. virtual bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
  152. return false;
  153. }
  154. // Check if it is legal to prune dependece between SUI and SUJ.
  155. virtual bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
  156. return false;
  157. }
  158. // Add a DAG mutation to be done before the packetization begins.
  159. void addMutation(std::unique_ptr<ScheduleDAGMutation> Mutation);
  160. bool alias(const MachineInstr &MI1, const MachineInstr &MI2,
  161. bool UseTBAA = true) const;
  162. private:
  163. bool alias(const MachineMemOperand &Op1, const MachineMemOperand &Op2,
  164. bool UseTBAA = true) const;
  165. };
  166. } // end namespace llvm
  167. #endif // LLVM_CODEGEN_DFAPACKETIZER_H