ScheduleDFS.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //===- ScheduleDAGILP.h - ILP metric for ScheduleDAGInstrs ------*- 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. // Definition of an ILP metric for machine level instruction scheduling.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_SCHEDULEDFS_H
  14. #define LLVM_CODEGEN_SCHEDULEDFS_H
  15. #include "llvm/CodeGen/ScheduleDAG.h"
  16. #include "llvm/Support/DataTypes.h"
  17. #include <vector>
  18. namespace llvm {
  19. class raw_ostream;
  20. class IntEqClasses;
  21. class ScheduleDAGInstrs;
  22. class SUnit;
  23. /// \brief Represent the ILP of the subDAG rooted at a DAG node.
  24. ///
  25. /// ILPValues summarize the DAG subtree rooted at each node. ILPValues are
  26. /// valid for all nodes regardless of their subtree membership.
  27. ///
  28. /// When computed using bottom-up DFS, this metric assumes that the DAG is a
  29. /// forest of trees with roots at the bottom of the schedule branching upward.
  30. struct ILPValue {
  31. unsigned InstrCount;
  32. /// Length may either correspond to depth or height, depending on direction,
  33. /// and cycles or nodes depending on context.
  34. unsigned Length;
  35. ILPValue(unsigned count, unsigned length):
  36. InstrCount(count), Length(length) {}
  37. // Order by the ILP metric's value.
  38. bool operator<(ILPValue RHS) const {
  39. return (uint64_t)InstrCount * RHS.Length
  40. < (uint64_t)Length * RHS.InstrCount;
  41. }
  42. bool operator>(ILPValue RHS) const {
  43. return RHS < *this;
  44. }
  45. bool operator<=(ILPValue RHS) const {
  46. return (uint64_t)InstrCount * RHS.Length
  47. <= (uint64_t)Length * RHS.InstrCount;
  48. }
  49. bool operator>=(ILPValue RHS) const {
  50. return RHS <= *this;
  51. }
  52. void print(raw_ostream &OS) const;
  53. void dump() const;
  54. };
  55. /// \brief Compute the values of each DAG node for various metrics during DFS.
  56. class SchedDFSResult {
  57. friend class SchedDFSImpl;
  58. static const unsigned InvalidSubtreeID = ~0u;
  59. /// \brief Per-SUnit data computed during DFS for various metrics.
  60. ///
  61. /// A node's SubtreeID is set to itself when it is visited to indicate that it
  62. /// is the root of a subtree. Later it is set to its parent to indicate an
  63. /// interior node. Finally, it is set to a representative subtree ID during
  64. /// finalization.
  65. struct NodeData {
  66. unsigned InstrCount;
  67. unsigned SubtreeID;
  68. NodeData(): InstrCount(0), SubtreeID(InvalidSubtreeID) {}
  69. };
  70. /// \brief Per-Subtree data computed during DFS.
  71. struct TreeData {
  72. unsigned ParentTreeID;
  73. unsigned SubInstrCount;
  74. TreeData(): ParentTreeID(InvalidSubtreeID), SubInstrCount(0) {}
  75. };
  76. /// \brief Record a connection between subtrees and the connection level.
  77. struct Connection {
  78. unsigned TreeID;
  79. unsigned Level;
  80. Connection(unsigned tree, unsigned level): TreeID(tree), Level(level) {}
  81. };
  82. bool IsBottomUp;
  83. unsigned SubtreeLimit;
  84. /// DFS results for each SUnit in this DAG.
  85. std::vector<NodeData> DFSNodeData;
  86. // Store per-tree data indexed on tree ID,
  87. SmallVector<TreeData, 16> DFSTreeData;
  88. // For each subtree discovered during DFS, record its connections to other
  89. // subtrees.
  90. std::vector<SmallVector<Connection, 4> > SubtreeConnections;
  91. /// Cache the current connection level of each subtree.
  92. /// This mutable array is updated during scheduling.
  93. std::vector<unsigned> SubtreeConnectLevels;
  94. public:
  95. SchedDFSResult(bool IsBU, unsigned lim)
  96. : IsBottomUp(IsBU), SubtreeLimit(lim) {}
  97. /// \brief Get the node cutoff before subtrees are considered significant.
  98. unsigned getSubtreeLimit() const { return SubtreeLimit; }
  99. /// \brief Return true if this DFSResult is uninitialized.
  100. ///
  101. /// resize() initializes DFSResult, while compute() populates it.
  102. bool empty() const { return DFSNodeData.empty(); }
  103. /// \brief Clear the results.
  104. void clear() {
  105. DFSNodeData.clear();
  106. DFSTreeData.clear();
  107. SubtreeConnections.clear();
  108. SubtreeConnectLevels.clear();
  109. }
  110. /// \brief Initialize the result data with the size of the DAG.
  111. void resize(unsigned NumSUnits) {
  112. DFSNodeData.resize(NumSUnits);
  113. }
  114. /// \brief Compute various metrics for the DAG with given roots.
  115. void compute(ArrayRef<SUnit> SUnits);
  116. /// \brief Get the number of instructions in the given subtree and its
  117. /// children.
  118. unsigned getNumInstrs(const SUnit *SU) const {
  119. return DFSNodeData[SU->NodeNum].InstrCount;
  120. }
  121. /// \brief Get the number of instructions in the given subtree not including
  122. /// children.
  123. unsigned getNumSubInstrs(unsigned SubtreeID) const {
  124. return DFSTreeData[SubtreeID].SubInstrCount;
  125. }
  126. /// \brief Get the ILP value for a DAG node.
  127. ///
  128. /// A leaf node has an ILP of 1/1.
  129. ILPValue getILP(const SUnit *SU) const {
  130. return ILPValue(DFSNodeData[SU->NodeNum].InstrCount, 1 + SU->getDepth());
  131. }
  132. /// \brief The number of subtrees detected in this DAG.
  133. unsigned getNumSubtrees() const { return SubtreeConnectLevels.size(); }
  134. /// \brief Get the ID of the subtree the given DAG node belongs to.
  135. ///
  136. /// For convenience, if DFSResults have not been computed yet, give everything
  137. /// tree ID 0.
  138. unsigned getSubtreeID(const SUnit *SU) const {
  139. if (empty())
  140. return 0;
  141. assert(SU->NodeNum < DFSNodeData.size() && "New Node");
  142. return DFSNodeData[SU->NodeNum].SubtreeID;
  143. }
  144. /// \brief Get the connection level of a subtree.
  145. ///
  146. /// For bottom-up trees, the connection level is the latency depth (in cycles)
  147. /// of the deepest connection to another subtree.
  148. unsigned getSubtreeLevel(unsigned SubtreeID) const {
  149. return SubtreeConnectLevels[SubtreeID];
  150. }
  151. /// \brief Scheduler callback to update SubtreeConnectLevels when a tree is
  152. /// initially scheduled.
  153. void scheduleTree(unsigned SubtreeID);
  154. };
  155. raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val);
  156. } // namespace llvm
  157. #endif