LLVM API Documentation

ScoreboardHazardRecognizer.h
Go to the documentation of this file.
00001 //=- llvm/CodeGen/ScoreboardHazardRecognizer.h - Schedule Support -*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ScoreboardHazardRecognizer class, which
00011 // encapsulates hazard-avoidance heuristics for scheduling, based on the
00012 // scheduling itineraries specified for the target.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
00017 #define LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H
00018 
00019 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
00020 #include "llvm/Support/DataTypes.h"
00021 #include <cassert>
00022 #include <cstring>
00023 
00024 namespace llvm {
00025 
00026 class InstrItineraryData;
00027 class ScheduleDAG;
00028 class SUnit;
00029 
00030 class ScoreboardHazardRecognizer : public ScheduleHazardRecognizer {
00031   // Scoreboard to track function unit usage. Scoreboard[0] is a
00032   // mask of the FUs in use in the cycle currently being
00033   // schedule. Scoreboard[1] is a mask for the next cycle. The
00034   // Scoreboard is used as a circular buffer with the current cycle
00035   // indicated by Head.
00036   //
00037   // Scoreboard always counts cycles in forward execution order. If used by a
00038   // bottom-up scheduler, then the scoreboard cycles are the inverse of the
00039   // scheduler's cycles.
00040   class Scoreboard {
00041     unsigned *Data;
00042 
00043     // The maximum number of cycles monitored by the Scoreboard. This
00044     // value is determined based on the target itineraries to ensure
00045     // that all hazards can be tracked.
00046     size_t Depth;
00047     // Indices into the Scoreboard that represent the current cycle.
00048     size_t Head;
00049   public:
00050     Scoreboard():Data(nullptr), Depth(0), Head(0) { }
00051     ~Scoreboard() {
00052       delete[] Data;
00053     }
00054 
00055     size_t getDepth() const { return Depth; }
00056     unsigned& operator[](size_t idx) const {
00057       // Depth is expected to be a power-of-2.
00058       assert(Depth && !(Depth & (Depth - 1)) &&
00059              "Scoreboard was not initialized properly!");
00060 
00061       return Data[(Head + idx) & (Depth-1)];
00062     }
00063 
00064     void reset(size_t d = 1) {
00065       if (!Data) {
00066         Depth = d;
00067         Data = new unsigned[Depth];
00068       }
00069 
00070       memset(Data, 0, Depth * sizeof(Data[0]));
00071       Head = 0;
00072     }
00073 
00074     void advance() {
00075       Head = (Head + 1) & (Depth-1);
00076     }
00077 
00078     void recede() {
00079       Head = (Head - 1) & (Depth-1);
00080     }
00081 
00082     // Print the scoreboard.
00083     void dump() const;
00084   };
00085 
00086 #ifndef NDEBUG
00087   // Support for tracing ScoreboardHazardRecognizer as a component within
00088   // another module. Follows the current thread-unsafe model of tracing.
00089   static const char *DebugType;
00090 #endif
00091 
00092   // Itinerary data for the target.
00093   const InstrItineraryData *ItinData;
00094 
00095   const ScheduleDAG *DAG;
00096 
00097   /// IssueWidth - Max issue per cycle. 0=Unknown.
00098   unsigned IssueWidth;
00099 
00100   /// IssueCount - Count instructions issued in this cycle.
00101   unsigned IssueCount;
00102 
00103   Scoreboard ReservedScoreboard;
00104   Scoreboard RequiredScoreboard;
00105 
00106 public:
00107   ScoreboardHazardRecognizer(const InstrItineraryData *ItinData,
00108                              const ScheduleDAG *DAG,
00109                              const char *ParentDebugType = "");
00110 
00111   /// atIssueLimit - Return true if no more instructions may be issued in this
00112   /// cycle.
00113   bool atIssueLimit() const override;
00114 
00115   // Stalls provides an cycle offset at which SU will be scheduled. It will be
00116   // negative for bottom-up scheduling.
00117   HazardType getHazardType(SUnit *SU, int Stalls) override;
00118   void Reset() override;
00119   void EmitInstruction(SUnit *SU) override;
00120   void AdvanceCycle() override;
00121   void RecedeCycle() override;
00122 };
00123 
00124 }
00125 
00126 #endif //!LLVM_CODEGEN_SCOREBOARDHAZARDRECOGNIZER_H