ScoreboardHazardRecognizer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //=- llvm/CodeGen/ScoreboardHazardRecognizer.h - Schedule Support -*- 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 the ScoreboardHazardRecognizer class, which
  11. // encapsulates hazard-avoidance heuristics for scheduling, based on the
  12. // scheduling itineraries specified for the target.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
  16. #define LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
  17. #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
  18. #include "llvm/Support/DataTypes.h"
  19. #include <cassert>
  20. #include <cstring>
  21. namespace llvm {
  22. class InstrItineraryData;
  23. class ScheduleDAG;
  24. class SUnit;
  25. class ScoreboardHazardRecognizer : public ScheduleHazardRecognizer {
  26. // Scoreboard to track function unit usage. Scoreboard[0] is a
  27. // mask of the FUs in use in the cycle currently being
  28. // schedule. Scoreboard[1] is a mask for the next cycle. The
  29. // Scoreboard is used as a circular buffer with the current cycle
  30. // indicated by Head.
  31. //
  32. // Scoreboard always counts cycles in forward execution order. If used by a
  33. // bottom-up scheduler, then the scoreboard cycles are the inverse of the
  34. // scheduler's cycles.
  35. class Scoreboard {
  36. unsigned *Data;
  37. // The maximum number of cycles monitored by the Scoreboard. This
  38. // value is determined based on the target itineraries to ensure
  39. // that all hazards can be tracked.
  40. size_t Depth;
  41. // Indices into the Scoreboard that represent the current cycle.
  42. size_t Head;
  43. public:
  44. Scoreboard():Data(nullptr), Depth(0), Head(0) { }
  45. ~Scoreboard() {
  46. delete[] Data;
  47. }
  48. size_t getDepth() const { return Depth; }
  49. unsigned& operator[](size_t idx) const {
  50. // Depth is expected to be a power-of-2.
  51. assert(Depth && !(Depth & (Depth - 1)) &&
  52. "Scoreboard was not initialized properly!");
  53. return Data[(Head + idx) & (Depth-1)];
  54. }
  55. void reset(size_t d = 1) {
  56. if (!Data) {
  57. Depth = d;
  58. Data = new unsigned[Depth];
  59. }
  60. memset(Data, 0, Depth * sizeof(Data[0]));
  61. Head = 0;
  62. }
  63. void advance() {
  64. Head = (Head + 1) & (Depth-1);
  65. }
  66. void recede() {
  67. Head = (Head - 1) & (Depth-1);
  68. }
  69. // Print the scoreboard.
  70. void dump() const;
  71. };
  72. #ifndef NDEBUG
  73. // Support for tracing ScoreboardHazardRecognizer as a component within
  74. // another module. Follows the current thread-unsafe model of tracing.
  75. static const char *DebugType;
  76. #endif
  77. // Itinerary data for the target.
  78. const InstrItineraryData *ItinData;
  79. const ScheduleDAG *DAG;
  80. /// IssueWidth - Max issue per cycle. 0=Unknown.
  81. unsigned IssueWidth;
  82. /// IssueCount - Count instructions issued in this cycle.
  83. unsigned IssueCount;
  84. Scoreboard ReservedScoreboard;
  85. Scoreboard RequiredScoreboard;
  86. public:
  87. ScoreboardHazardRecognizer(const InstrItineraryData *ItinData,
  88. const ScheduleDAG *DAG,
  89. const char *ParentDebugType = "");
  90. /// atIssueLimit - Return true if no more instructions may be issued in this
  91. /// cycle.
  92. bool atIssueLimit() const override;
  93. // Stalls provides an cycle offset at which SU will be scheduled. It will be
  94. // negative for bottom-up scheduling.
  95. HazardType getHazardType(SUnit *SU, int Stalls) override;
  96. void Reset() override;
  97. void EmitInstruction(SUnit *SU) override;
  98. void AdvanceCycle() override;
  99. void RecedeCycle() override;
  100. };
  101. }
  102. #endif //!LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H