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