TargetSchedule.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //===-- llvm/CodeGen/TargetSchedule.h - Sched Machine Model -----*- 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. //
  10. // This file defines a wrapper around MCSchedModel that allows the interface to
  11. // benefit from information currently only available in TargetInstrInfo.
  12. // Ideally, the scheduling interface would be fully defined in the MC layer.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CODEGEN_TARGETSCHEDULE_H
  16. #define LLVM_CODEGEN_TARGETSCHEDULE_H
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/MC/MCInstrItineraries.h"
  19. #include "llvm/MC/MCSchedule.h"
  20. #include "llvm/Target/TargetSubtargetInfo.h"
  21. namespace llvm {
  22. class TargetRegisterInfo;
  23. class TargetSubtargetInfo;
  24. class TargetInstrInfo;
  25. class MachineInstr;
  26. /// Provide an instruction scheduling machine model to CodeGen passes.
  27. class TargetSchedModel {
  28. // For efficiency, hold a copy of the statically defined MCSchedModel for this
  29. // processor.
  30. MCSchedModel SchedModel;
  31. InstrItineraryData InstrItins;
  32. const TargetSubtargetInfo *STI;
  33. const TargetInstrInfo *TII;
  34. SmallVector<unsigned, 16> ResourceFactors;
  35. unsigned MicroOpFactor; // Multiply to normalize microops to resource units.
  36. unsigned ResourceLCM; // Resource units per cycle. Latency normalization factor.
  37. unsigned computeInstrLatency(const MCSchedClassDesc &SCDesc) const;
  38. public:
  39. TargetSchedModel(): SchedModel(MCSchedModel::GetDefaultSchedModel()), STI(nullptr), TII(nullptr) {}
  40. /// \brief Initialize the machine model for instruction scheduling.
  41. ///
  42. /// The machine model API keeps a copy of the top-level MCSchedModel table
  43. /// indices and may query TargetSubtargetInfo and TargetInstrInfo to resolve
  44. /// dynamic properties.
  45. void init(const MCSchedModel &sm, const TargetSubtargetInfo *sti,
  46. const TargetInstrInfo *tii);
  47. /// Return the MCSchedClassDesc for this instruction.
  48. const MCSchedClassDesc *resolveSchedClass(const MachineInstr *MI) const;
  49. /// \brief TargetInstrInfo getter.
  50. const TargetInstrInfo *getInstrInfo() const { return TII; }
  51. /// \brief Return true if this machine model includes an instruction-level
  52. /// scheduling model.
  53. ///
  54. /// This is more detailed than the course grain IssueWidth and default
  55. /// latency properties, but separate from the per-cycle itinerary data.
  56. bool hasInstrSchedModel() const;
  57. const MCSchedModel *getMCSchedModel() const { return &SchedModel; }
  58. /// \brief Return true if this machine model includes cycle-to-cycle itinerary
  59. /// data.
  60. ///
  61. /// This models scheduling at each stage in the processor pipeline.
  62. bool hasInstrItineraries() const;
  63. const InstrItineraryData *getInstrItineraries() const {
  64. if (hasInstrItineraries())
  65. return &InstrItins;
  66. return nullptr;
  67. }
  68. /// \brief Identify the processor corresponding to the current subtarget.
  69. unsigned getProcessorID() const { return SchedModel.getProcessorID(); }
  70. /// \brief Maximum number of micro-ops that may be scheduled per cycle.
  71. unsigned getIssueWidth() const { return SchedModel.IssueWidth; }
  72. /// \brief Return the number of issue slots required for this MI.
  73. unsigned getNumMicroOps(const MachineInstr *MI,
  74. const MCSchedClassDesc *SC = nullptr) const;
  75. /// \brief Get the number of kinds of resources for this target.
  76. unsigned getNumProcResourceKinds() const {
  77. return SchedModel.getNumProcResourceKinds();
  78. }
  79. /// \brief Get a processor resource by ID for convenience.
  80. const MCProcResourceDesc *getProcResource(unsigned PIdx) const {
  81. return SchedModel.getProcResource(PIdx);
  82. }
  83. #ifndef NDEBUG
  84. const char *getResourceName(unsigned PIdx) const {
  85. if (!PIdx)
  86. return "MOps";
  87. return SchedModel.getProcResource(PIdx)->Name;
  88. }
  89. #endif
  90. typedef const MCWriteProcResEntry *ProcResIter;
  91. // \brief Get an iterator into the processor resources consumed by this
  92. // scheduling class.
  93. ProcResIter getWriteProcResBegin(const MCSchedClassDesc *SC) const {
  94. // The subtarget holds a single resource table for all processors.
  95. return STI->getWriteProcResBegin(SC);
  96. }
  97. ProcResIter getWriteProcResEnd(const MCSchedClassDesc *SC) const {
  98. return STI->getWriteProcResEnd(SC);
  99. }
  100. /// \brief Multiply the number of units consumed for a resource by this factor
  101. /// to normalize it relative to other resources.
  102. unsigned getResourceFactor(unsigned ResIdx) const {
  103. return ResourceFactors[ResIdx];
  104. }
  105. /// \brief Multiply number of micro-ops by this factor to normalize it
  106. /// relative to other resources.
  107. unsigned getMicroOpFactor() const {
  108. return MicroOpFactor;
  109. }
  110. /// \brief Multiply cycle count by this factor to normalize it relative to
  111. /// other resources. This is the number of resource units per cycle.
  112. unsigned getLatencyFactor() const {
  113. return ResourceLCM;
  114. }
  115. /// \brief Number of micro-ops that may be buffered for OOO execution.
  116. unsigned getMicroOpBufferSize() const { return SchedModel.MicroOpBufferSize; }
  117. /// \brief Number of resource units that may be buffered for OOO execution.
  118. /// \return The buffer size in resource units or -1 for unlimited.
  119. int getResourceBufferSize(unsigned PIdx) const {
  120. return SchedModel.getProcResource(PIdx)->BufferSize;
  121. }
  122. /// \brief Compute operand latency based on the available machine model.
  123. ///
  124. /// Compute and return the latency of the given data dependent def and use
  125. /// when the operand indices are already known. UseMI may be NULL for an
  126. /// unknown user.
  127. unsigned computeOperandLatency(const MachineInstr *DefMI, unsigned DefOperIdx,
  128. const MachineInstr *UseMI, unsigned UseOperIdx)
  129. const;
  130. /// \brief Compute the instruction latency based on the available machine
  131. /// model.
  132. ///
  133. /// Compute and return the expected latency of this instruction independent of
  134. /// a particular use. computeOperandLatency is the preferred API, but this is
  135. /// occasionally useful to help estimate instruction cost.
  136. ///
  137. /// If UseDefaultDefLatency is false and no new machine sched model is
  138. /// present this method falls back to TII->getInstrLatency with an empty
  139. /// instruction itinerary (this is so we preserve the previous behavior of the
  140. /// if converter after moving it to TargetSchedModel).
  141. unsigned computeInstrLatency(const MachineInstr *MI,
  142. bool UseDefaultDefLatency = true) const;
  143. unsigned computeInstrLatency(unsigned Opcode) const;
  144. /// \brief Output dependency latency of a pair of defs of the same register.
  145. ///
  146. /// This is typically one cycle.
  147. unsigned computeOutputLatency(const MachineInstr *DefMI, unsigned DefIdx,
  148. const MachineInstr *DepMI) const;
  149. };
  150. } // namespace llvm
  151. #endif