[llvm-mca] Views are now independent from resource masks. NFCI

This change removes method Backend::getProcResourceMasks() and simplifies some
logic in the Views. This effectively removes yet another dependency between the
views and the Backend.
No functional change intended.

llvm-svn: 327214
diff --git a/llvm/tools/llvm-mca/Backend.h b/llvm/tools/llvm-mca/Backend.h
index a000d16..dadab2d 100644
--- a/llvm/tools/llvm-mca/Backend.h
+++ b/llvm/tools/llvm-mca/Backend.h
@@ -70,7 +70,7 @@
             this, MRI, Subtarget.getSchedModel().MicroOpBufferSize,
             RegisterFileSize, MaxRetirePerCycle, DispatchWidth, HWS.get())),
         SM(Source), Cycles(0) {
-    IB = llvm::make_unique<InstrBuilder>(MCII, getProcResourceMasks());
+    IB = llvm::make_unique<InstrBuilder>(MCII, HWS->getProcResourceMasks());
   }
 
   void run() {
@@ -93,9 +93,6 @@
   const llvm::MCSchedModel &getSchedModel() const {
     return STI.getSchedModel();
   }
-  const llvm::ArrayRef<uint64_t> getProcResourceMasks() const {
-    return HWS->getProcResourceMasks();
-  }
 
   double getRThroughput(const InstrDesc &ID) const {
     return HWS->getRThroughput(ID);
diff --git a/llvm/tools/llvm-mca/BackendStatistics.cpp b/llvm/tools/llvm-mca/BackendStatistics.cpp
index 358ce28..46981d5 100644
--- a/llvm/tools/llvm-mca/BackendStatistics.cpp
+++ b/llvm/tools/llvm-mca/BackendStatistics.cpp
@@ -121,14 +121,13 @@
   std::string Buffer;
   raw_string_ostream TempStream(Buffer);
   TempStream << "\n\nScheduler's queue usage:\n";
-  const ArrayRef<uint64_t> ResourceMasks = B.getProcResourceMasks();
   for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) {
     const MCProcResourceDesc &ProcResource = *SM.getProcResource(I);
     if (!ProcResource.BufferSize)
       continue;
 
     for (const BufferUsageEntry &Entry : Usage)
-      if (ResourceMasks[I] == Entry.first)
+      if (I == Entry.first)
         TempStream << ProcResource.Name << ",  " << Entry.second << '/'
                    << ProcResource.BufferSize << '\n';
   }
diff --git a/llvm/tools/llvm-mca/ResourcePressureView.cpp b/llvm/tools/llvm-mca/ResourcePressureView.cpp
index 1dd15e0..1b9620f 100644
--- a/llvm/tools/llvm-mca/ResourcePressureView.cpp
+++ b/llvm/tools/llvm-mca/ResourcePressureView.cpp
@@ -19,8 +19,7 @@
 
 using namespace llvm;
 
-void ResourcePressureView::initialize(
-    const ArrayRef<uint64_t> ProcResourceMasks) {
+void ResourcePressureView::initialize() {
   // Populate the map of resource descriptors.
   unsigned R2VIndex = 0;
   const MCSchedModel &SM = STI.getSchedModel();
@@ -31,9 +30,7 @@
     if (ProcResource.SubUnitsIdxBegin || !NumUnits)
       continue;
 
-    uint64_t ResourceMask = ProcResourceMasks[I];
-    Resource2VecIndex.insert(
-        std::pair<uint64_t, unsigned>(ResourceMask, R2VIndex));
+    Resource2VecIndex.insert(std::pair<uint64_t, unsigned>(I, R2VIndex));
     R2VIndex += ProcResource.NumUnits;
   }
 
diff --git a/llvm/tools/llvm-mca/ResourcePressureView.h b/llvm/tools/llvm-mca/ResourcePressureView.h
index 77c07be..105aa40 100644
--- a/llvm/tools/llvm-mca/ResourcePressureView.h
+++ b/llvm/tools/llvm-mca/ResourcePressureView.h
@@ -87,14 +87,13 @@
                                          unsigned Executions) const;
   void printResourcePressurePerInstruction(llvm::raw_ostream &OS,
                                            unsigned Executions) const;
-  void initialize(const llvm::ArrayRef<uint64_t> ProcResoureMasks);
+  void initialize();
 
 public:
   ResourcePressureView(const llvm::MCSubtargetInfo &ST,
-                       llvm::MCInstPrinter &Printer, const SourceMgr &SM,
-                       const llvm::ArrayRef<uint64_t> ProcResourceMasks)
+                       llvm::MCInstPrinter &Printer, const SourceMgr &SM)
       : STI(ST), MCIP(Printer), Source(SM) {
-    initialize(ProcResourceMasks);
+    initialize();
   }
 
   void onInstructionIssued(
diff --git a/llvm/tools/llvm-mca/Scheduler.cpp b/llvm/tools/llvm-mca/Scheduler.cpp
index 6c0c44b..26e806d 100644
--- a/llvm/tools/llvm-mca/Scheduler.cpp
+++ b/llvm/tools/llvm-mca/Scheduler.cpp
@@ -46,9 +46,10 @@
 // ResourceDescriptor. Map 'Resources' allows to quickly obtain ResourceState
 // objects from resource mask identifiers.
 void ResourceManager::addResource(const MCProcResourceDesc &Desc,
+                                  unsigned Index,
                                   uint64_t Mask) {
   assert(Resources.find(Mask) == Resources.end() && "Resource already added!");
-  Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Mask);
+  Resources[Mask] = llvm::make_unique<ResourceState>(Desc, Index, Mask);
 }
 
 // Populate vector ProcResID2Mask with resource masks. One per each processor
@@ -219,6 +220,9 @@
       ResourceRef Pipe = selectPipe(R.first);
       use(Pipe);
       BusyResources[Pipe] += CS.size();
+      // Replace the resource mask with a valid processor resource index.
+      const ResourceState &RS = *Resources[Pipe.first];
+      Pipe.first = RS.getProcResourceID();
       Pipes.emplace_back(std::pair<ResourceRef, unsigned>(Pipe, CS.size()));
     } else {
       assert((countPopulation(R.first) > 1) && "Expected a group!");
diff --git a/llvm/tools/llvm-mca/Scheduler.h b/llvm/tools/llvm-mca/Scheduler.h
index 2b58437..7bc2873 100644
--- a/llvm/tools/llvm-mca/Scheduler.h
+++ b/llvm/tools/llvm-mca/Scheduler.h
@@ -56,6 +56,8 @@
 /// Internally, ResourceState uses a round-robin selector to identify
 /// which unit of the group shall be used next.
 class ResourceState {
+  // Index to the MCProcResourceDesc in the processor Model.
+  unsigned ProcResourceDescIndex;
   // A resource unique identifier generated by the tool.
   // For processor resource groups, the number of number of bits set in this
   // mask is equivalent to the cardinality of the group plus one.
@@ -181,8 +183,9 @@
   }
 
 public:
-  ResourceState(const llvm::MCProcResourceDesc &Desc, uint64_t Mask)
-      : ResourceMask(Mask) {
+  ResourceState(const llvm::MCProcResourceDesc &Desc, unsigned Index,
+                uint64_t Mask)
+      : ProcResourceDescIndex(Index), ResourceMask(Mask) {
     bool IsAGroup = llvm::countPopulation(ResourceMask) > 1;
     ResourceSizeMask = IsAGroup ? computeResourceSizeMaskForGroup(ResourceMask)
                                 : ((1ULL << Desc.NumUnits) - 1);
@@ -195,6 +198,7 @@
     Unavailable = false;
   }
 
+  unsigned getProcResourceID() const { return ProcResourceDescIndex; }
   uint64_t getResourceMask() const { return ResourceMask; }
   int getBufferSize() const { return BufferSize; }
   unsigned getMaxUsedSlots() const { return MaxUsedSlots; }
@@ -274,9 +278,9 @@
 /// 'second' index is an index for a "sub-resource" (i.e. unit).
 typedef std::pair<uint64_t, uint64_t> ResourceRef;
 
-// First: a resource mask identifying a buffered resource.
+// First: a MCProcResourceDesc index identifying a buffered resource.
 // Second: max number of buffer entries used in this resource.
-typedef std::pair<uint64_t, unsigned> BufferUsageEntry;
+typedef std::pair<unsigned, unsigned> BufferUsageEntry;
 
 /// A resource manager for processor resource units and groups.
 ///
@@ -300,7 +304,8 @@
 
   // Adds a new resource state in Resources, as well as a new descriptor in
   // ResourceDescriptor.
-  void addResource(const llvm::MCProcResourceDesc &Desc, uint64_t Mask);
+  void addResource(const llvm::MCProcResourceDesc &Desc, unsigned Index,
+                   uint64_t Mask);
 
   // Compute processor resource masks for each processor resource declared by
   // the scheduling model.
@@ -310,7 +315,7 @@
   void initialize(const llvm::MCSchedModel &SM) {
     computeProcResourceMasks(SM);
     for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I)
-      addResource(*SM.getProcResource(I), ProcResID2Mask[I]);
+      addResource(*SM.getProcResource(I), I, ProcResID2Mask[I]);
   }
 
   // Returns the actual resource unit that will be used.
@@ -400,7 +405,7 @@
     for (const std::pair<uint64_t, UniqueResourceState> &Resource : Resources) {
       const ResourceState &RS = *Resource.second;
       if (RS.isBuffered())
-        Usage.emplace_back(std::pair<uint64_t, unsigned>(RS.getResourceMask(),
+        Usage.emplace_back(std::pair<unsigned, unsigned>(RS.getProcResourceID(),
                                                          RS.getMaxUsedSlots()));
     }
   }
diff --git a/llvm/tools/llvm-mca/llvm-mca.cpp b/llvm/tools/llvm-mca/llvm-mca.cpp
index 6e14f0b..9492e2d 100644
--- a/llvm/tools/llvm-mca/llvm-mca.cpp
+++ b/llvm/tools/llvm-mca/llvm-mca.cpp
@@ -335,8 +335,7 @@
   }
 
   std::unique_ptr<mca::ResourcePressureView> RPV =
-      llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, *S,
-                                                   B->getProcResourceMasks());
+      llvm::make_unique<mca::ResourcePressureView>(*STI, *IP, *S);
   Printer->addView(std::move(RPV));
 
   if (PrintTimelineView) {