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