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