Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.9 KiB

  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. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  53. void print(raw_ostream &OS) const;
  54. void dump() const;
  55. #endif
  56. };
  57. /// \brief Compute the values of each DAG node for various metrics during DFS.
  58. class SchedDFSResult {
  59. friend class SchedDFSImpl;
  60. static const unsigned InvalidSubtreeID = ~0u;
  61. /// \brief Per-SUnit data computed during DFS for various metrics.
  62. ///
  63. /// A node's SubtreeID is set to itself when it is visited to indicate that it
  64. /// is the root of a subtree. Later it is set to its parent to indicate an
  65. /// interior node. Finally, it is set to a representative subtree ID during
  66. /// finalization.
  67. struct NodeData {
  68. unsigned InstrCount;
  69. unsigned SubtreeID;
  70. NodeData(): InstrCount(0), SubtreeID(InvalidSubtreeID) {}
  71. };
  72. /// \brief Per-Subtree data computed during DFS.
  73. struct TreeData {
  74. unsigned ParentTreeID;
  75. unsigned SubInstrCount;
  76. TreeData(): ParentTreeID(InvalidSubtreeID), SubInstrCount(0) {}
  77. };
  78. /// \brief Record a connection between subtrees and the connection level.
  79. struct Connection {
  80. unsigned TreeID;
  81. unsigned Level;
  82. Connection(unsigned tree, unsigned level): TreeID(tree), Level(level) {}
  83. };
  84. bool IsBottomUp;
  85. unsigned SubtreeLimit;
  86. /// DFS results for each SUnit in this DAG.
  87. std::vector<NodeData> DFSNodeData;
  88. // Store per-tree data indexed on tree ID,
  89. SmallVector<TreeData, 16> DFSTreeData;
  90. // For each subtree discovered during DFS, record its connections to other
  91. // subtrees.
  92. std::vector<SmallVector<Connection, 4> > SubtreeConnections;
  93. /// Cache the current connection level of each subtree.
  94. /// This mutable array is updated during scheduling.
  95. std::vector<unsigned> SubtreeConnectLevels;
  96. public:
  97. SchedDFSResult(bool IsBU, unsigned lim)
  98. : IsBottomUp(IsBU), SubtreeLimit(lim) {}
  99. /// \brief Get the node cutoff before subtrees are considered significant.
  100. unsigned getSubtreeLimit() const { return SubtreeLimit; }
  101. /// \brief Return true if this DFSResult is uninitialized.
  102. ///
  103. /// resize() initializes DFSResult, while compute() populates it.
  104. bool empty() const { return DFSNodeData.empty(); }
  105. /// \brief Clear the results.
  106. void clear() {
  107. DFSNodeData.clear();
  108. DFSTreeData.clear();
  109. SubtreeConnections.clear();
  110. SubtreeConnectLevels.clear();
  111. }
  112. /// \brief Initialize the result data with the size of the DAG.
  113. void resize(unsigned NumSUnits) {
  114. DFSNodeData.resize(NumSUnits);
  115. }
  116. /// \brief Compute various metrics for the DAG with given roots.
  117. void compute(ArrayRef<SUnit> SUnits);
  118. /// \brief Get the number of instructions in the given subtree and its
  119. /// children.
  120. unsigned getNumInstrs(const SUnit *SU) const {
  121. return DFSNodeData[SU->NodeNum].InstrCount;
  122. }
  123. /// \brief Get the number of instructions in the given subtree not including
  124. /// children.
  125. unsigned getNumSubInstrs(unsigned SubtreeID) const {
  126. return DFSTreeData[SubtreeID].SubInstrCount;
  127. }
  128. /// \brief Get the ILP value for a DAG node.
  129. ///
  130. /// A leaf node has an ILP of 1/1.
  131. ILPValue getILP(const SUnit *SU) const {
  132. return ILPValue(DFSNodeData[SU->NodeNum].InstrCount, 1 + SU->getDepth());
  133. }
  134. /// \brief The number of subtrees detected in this DAG.
  135. unsigned getNumSubtrees() const { return SubtreeConnectLevels.size(); }
  136. /// \brief Get the ID of the subtree the given DAG node belongs to.
  137. ///
  138. /// For convenience, if DFSResults have not been computed yet, give everything
  139. /// tree ID 0.
  140. unsigned getSubtreeID(const SUnit *SU) const {
  141. if (empty())
  142. return 0;
  143. assert(SU->NodeNum < DFSNodeData.size() && "New Node");
  144. return DFSNodeData[SU->NodeNum].SubtreeID;
  145. }
  146. /// \brief Get the connection level of a subtree.
  147. ///
  148. /// For bottom-up trees, the connection level is the latency depth (in cycles)
  149. /// of the deepest connection to another subtree.
  150. unsigned getSubtreeLevel(unsigned SubtreeID) const {
  151. return SubtreeConnectLevels[SubtreeID];
  152. }
  153. /// \brief Scheduler callback to update SubtreeConnectLevels when a tree is
  154. /// initially scheduled.
  155. void scheduleTree(unsigned SubtreeID);
  156. };
  157. raw_ostream &operator<<(raw_ostream &OS, const ILPValue &Val);
  158. } // namespace llvm
  159. #endif