[llvm-mca] Move the logic that computes the scheduler's queue usage to the BackendStatistics view.

This patch introduces two new callbacks in the event listener interface to
handle the "buffered resource reserved" event and the "buffered resource
released" event. Every time a buffered resource is used, an event is generated.

Before this patch, the Scheduler (with the help of the ResourceManager) was
responsible for tracking the scheduler's queue usage. However, that design
forced the Scheduler to 'publish' scheduler's queue pressure information through
the Backend interface.

The goal of this patch is to break the dependency between the BackendStatistics
view, and the Backend. Now the Scheduler knows how to notify "buffer
reserved/released" events.  The scheduler's queue usage analysis has been moved
to the BackendStatistics.

Differential Revision: https://reviews.llvm.org/D44686

llvm-svn: 328011
diff --git a/llvm/tools/llvm-mca/BackendStatistics.h b/llvm/tools/llvm-mca/BackendStatistics.h
index dacf5b0..0def722 100644
--- a/llvm/tools/llvm-mca/BackendStatistics.h
+++ b/llvm/tools/llvm-mca/BackendStatistics.h
@@ -85,6 +85,17 @@
   // is one counter for every generic stall kind (see class HWStallEvent).
   llvm::SmallVector<unsigned, 8> HWStalls;
 
+  // Tracks the usage of a scheduler's queue.
+  struct BufferUsage {
+    unsigned SlotsInUse;
+    unsigned MaxUsedSlots;
+  };
+
+  // There is a map entry for each buffered resource in the scheduling model.
+  // Every time a buffer is consumed/freed, this view updates the corresponding
+  // entry.
+  llvm::DenseMap<unsigned, BufferUsage> BufferedResources;
+
   void updateHistograms() {
     DispatchGroupSizePerCycle[NumDispatched]++;
     IssuedPerCycle[NumIssued]++;
@@ -107,8 +118,8 @@
                               unsigned Cycles) const;
   void printIssuePerCycle(const Histogram &IssuePerCycle,
                           unsigned TotalCycles) const;
-  void printSchedulerUsage(llvm::raw_ostream &OS, const llvm::MCSchedModel &SM,
-                           const llvm::ArrayRef<BufferUsageEntry> &Usage) const;
+  void printSchedulerUsage(llvm::raw_ostream &OS,
+                           const llvm::MCSchedModel &SM) const;
 
 public:
   BackendStatistics(const Backend &backend, const llvm::MCSubtargetInfo &sti)
@@ -126,6 +137,14 @@
       HWStalls[Event.Type]++;
   }
 
+  // Increases the number of used scheduler queue slots of every buffered
+  // resource in the Buffers set.
+  void onReservedBuffers(llvm::ArrayRef<unsigned> Buffers);
+
+  // Decreases by one the number of used scheduler queue slots of every
+  // buffered resource in the Buffers set.
+  void onReleasedBuffers(llvm::ArrayRef<unsigned> Buffers);
+
   void printView(llvm::raw_ostream &OS) const override {
     printDispatchStalls(OS);
     printRATStatistics(OS, B.getTotalRegisterMappingsCreated(),
@@ -134,12 +153,9 @@
     printSchedulerStatistics(OS);
     printRetireUnitStatistics(OS);
 
-    std::vector<BufferUsageEntry> Usage;
-    B.getBuffersUsage(Usage);
-    printSchedulerUsage(OS, STI.getSchedModel(), Usage);
+    printSchedulerUsage(OS, STI.getSchedModel());
   }
 };
-
 } // namespace mca
 
 #endif