DFAPacketizer.h 7.6 KB

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