1218885Sdim//=- llvm/CodeGen/ScoreboardHazardRecognizer.h - Schedule Support -*- C++ -*-=//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This file defines the ScoreboardHazardRecognizer class, which
11218885Sdim// encapsulates hazard-avoidance heuristics for scheduling, based on the
12218885Sdim// scheduling itineraries specified for the target.
13218885Sdim//
14218885Sdim//===----------------------------------------------------------------------===//
15218885Sdim
16218885Sdim#ifndef LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
17218885Sdim#define LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
18218885Sdim
19218885Sdim#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
20218885Sdim#include "llvm/Support/DataTypes.h"
21218885Sdim#include <cassert>
22218885Sdim#include <cstring>
23218885Sdim
24218885Sdimnamespace llvm {
25218885Sdim
26218885Sdimclass InstrItineraryData;
27218885Sdimclass ScheduleDAG;
28218885Sdimclass SUnit;
29218885Sdim
30218885Sdimclass ScoreboardHazardRecognizer : public ScheduleHazardRecognizer {
31218885Sdim  // Scoreboard to track function unit usage. Scoreboard[0] is a
32218885Sdim  // mask of the FUs in use in the cycle currently being
33218885Sdim  // schedule. Scoreboard[1] is a mask for the next cycle. The
34218885Sdim  // Scoreboard is used as a circular buffer with the current cycle
35218885Sdim  // indicated by Head.
36218885Sdim  //
37218885Sdim  // Scoreboard always counts cycles in forward execution order. If used by a
38218885Sdim  // bottom-up scheduler, then the scoreboard cycles are the inverse of the
39218885Sdim  // scheduler's cycles.
40218885Sdim  class Scoreboard {
41218885Sdim    unsigned *Data;
42218885Sdim
43218885Sdim    // The maximum number of cycles monitored by the Scoreboard. This
44218885Sdim    // value is determined based on the target itineraries to ensure
45218885Sdim    // that all hazards can be tracked.
46218885Sdim    size_t Depth;
47218885Sdim    // Indices into the Scoreboard that represent the current cycle.
48218885Sdim    size_t Head;
49218885Sdim  public:
50218885Sdim    Scoreboard():Data(NULL), Depth(0), Head(0) { }
51218885Sdim    ~Scoreboard() {
52218885Sdim      delete[] Data;
53218885Sdim    }
54218885Sdim
55218885Sdim    size_t getDepth() const { return Depth; }
56218885Sdim    unsigned& operator[](size_t idx) const {
57218885Sdim      // Depth is expected to be a power-of-2.
58218885Sdim      assert(Depth && !(Depth & (Depth - 1)) &&
59218885Sdim             "Scoreboard was not initialized properly!");
60218885Sdim
61218885Sdim      return Data[(Head + idx) & (Depth-1)];
62218885Sdim    }
63218885Sdim
64218885Sdim    void reset(size_t d = 1) {
65218885Sdim      if (Data == NULL) {
66218885Sdim        Depth = d;
67218885Sdim        Data = new unsigned[Depth];
68218885Sdim      }
69218885Sdim
70218885Sdim      memset(Data, 0, Depth * sizeof(Data[0]));
71218885Sdim      Head = 0;
72218885Sdim    }
73218885Sdim
74218885Sdim    void advance() {
75218885Sdim      Head = (Head + 1) & (Depth-1);
76218885Sdim    }
77218885Sdim
78218885Sdim    void recede() {
79218885Sdim      Head = (Head - 1) & (Depth-1);
80218885Sdim    }
81218885Sdim
82218885Sdim    // Print the scoreboard.
83218885Sdim    void dump() const;
84218885Sdim  };
85218885Sdim
86218885Sdim#ifndef NDEBUG
87218885Sdim  // Support for tracing ScoreboardHazardRecognizer as a component within
88218885Sdim  // another module. Follows the current thread-unsafe model of tracing.
89218885Sdim  static const char *DebugType;
90218885Sdim#endif
91218885Sdim
92218885Sdim  // Itinerary data for the target.
93218885Sdim  const InstrItineraryData *ItinData;
94218885Sdim
95218885Sdim  const ScheduleDAG *DAG;
96218885Sdim
97218885Sdim  /// IssueWidth - Max issue per cycle. 0=Unknown.
98218885Sdim  unsigned IssueWidth;
99218885Sdim
100218885Sdim  /// IssueCount - Count instructions issued in this cycle.
101218885Sdim  unsigned IssueCount;
102218885Sdim
103218885Sdim  Scoreboard ReservedScoreboard;
104218885Sdim  Scoreboard RequiredScoreboard;
105218885Sdim
106218885Sdimpublic:
107218885Sdim  ScoreboardHazardRecognizer(const InstrItineraryData *ItinData,
108218885Sdim                             const ScheduleDAG *DAG,
109218885Sdim                             const char *ParentDebugType = "");
110218885Sdim
111218885Sdim  /// atIssueLimit - Return true if no more instructions may be issued in this
112218885Sdim  /// cycle.
113218885Sdim  virtual bool atIssueLimit() const;
114218885Sdim
115218885Sdim  // Stalls provides an cycle offset at which SU will be scheduled. It will be
116218885Sdim  // negative for bottom-up scheduling.
117218885Sdim  virtual HazardType getHazardType(SUnit *SU, int Stalls);
118218885Sdim  virtual void Reset();
119218885Sdim  virtual void EmitInstruction(SUnit *SU);
120218885Sdim  virtual void AdvanceCycle();
121218885Sdim  virtual void RecedeCycle();
122218885Sdim};
123218885Sdim
124218885Sdim}
125218885Sdim
126218885Sdim#endif //!LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
127