ScoreboardHazardRecognizer.h 3.9 KB

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