[MC][Tablegen] Allow the definition of processor register files in the scheduling model for llvm-mca

This patch allows the description of register files in processor scheduling
models. This addresses PR36662.

A new tablegen class named 'RegisterFile' has been added to TargetSchedule.td.
Targets can optionally describe register files for their processors using that
class. In particular, class RegisterFile allows to specify:
 - The total number of physical registers.
 - Which target registers are accessible through the register file.
 - The cost of allocating a register at register renaming stage.

Example (from this patch - see file X86/X86ScheduleBtVer2.td)

  def FpuPRF : RegisterFile<72, [VR64, VR128, VR256], [1, 1, 2]>

Here, FpuPRF describes a register file for MMX/XMM/YMM registers. On Jaguar
(btver2), a YMM register definition consumes 2 physical registers, while MMX/XMM
register definitions only cost 1 physical register.

The syntax allows to specify an empty set of register classes.  An empty set of
register classes means: this register file models all the registers specified by
the Target.  For each register class, users can specify an optional register
cost. By default, register costs default to 1.  A value of 0 for the number of
physical registers means: "this register file has an unbounded number of
physical registers".

This patch is structured in two parts.

* Part 1 - MC/Tablegen *

A first part adds the tablegen definition of RegisterFile, and teaches the
SubtargetEmitter how to emit information related to register files.

Information about register files is accessible through an instance of
MCExtraProcessorInfo.
The idea behind this design is to logically partition the processor description
which is only used by external tools (like llvm-mca) from the processor
information used by the llvm machine schedulers.
I think that this design would make easier for targets to get rid of the extra
processor information if they don't want it.

* Part 2 - llvm-mca related *

The second part of this patch is related to changes to llvm-mca.

The main differences are:
 1) class RegisterFile now needs to take into account the "cost of a register"
when allocating physical registers at register renaming stage.
 2) Point 1. triggered a minor refactoring which lef to the removal of the
"maximum 32 register files" restriction.
 3) The BackendStatistics view has been updated so that we can print out extra
details related to each register file implemented by the processor.

The effect of point 3. is also visible in tests register-files-[1..5].s.

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

llvm-svn: 329067
diff --git a/llvm/tools/llvm-mca/Backend.h b/llvm/tools/llvm-mca/Backend.h
index 902327a..3f4fda7 100644
--- a/llvm/tools/llvm-mca/Backend.h
+++ b/llvm/tools/llvm-mca/Backend.h
@@ -69,7 +69,7 @@
                                          LoadQueueSize, StoreQueueSize,
                                          AssumeNoAlias)),
         DU(llvm::make_unique<DispatchUnit>(
-            this, MRI, Subtarget.getSchedModel().MicroOpBufferSize,
+            this, STI, MRI, Subtarget.getSchedModel().MicroOpBufferSize,
             RegisterFileSize, MaxRetirePerCycle, DispatchWidth, HWS.get())),
         SM(Source), Cycles(0) {
     HWS->setDispatchUnit(DU.get());
diff --git a/llvm/tools/llvm-mca/BackendStatistics.cpp b/llvm/tools/llvm-mca/BackendStatistics.cpp
index c542550..d89570b 100644
--- a/llvm/tools/llvm-mca/BackendStatistics.cpp
+++ b/llvm/tools/llvm-mca/BackendStatistics.cpp
@@ -20,6 +20,26 @@
 
 namespace mca {
 
+void BackendStatistics::initializeRegisterFileInfo() {
+  const MCSchedModel &SM = STI.getSchedModel();
+  RegisterFileUsage Empty = {0, 0, 0};
+  if (!SM.hasExtraProcessorInfo()) {
+    // Assume a single register file.
+    RegisterFiles.emplace_back(Empty);
+    return;
+  }
+
+  // Initialize a RegisterFileUsage for every user defined register file, plus
+  // the default register file which is always at index #0.
+  const MCExtraProcessorInfo &PI = SM.getExtraProcessorInfo();
+  // There is always an "InvalidRegisterFile" entry in tablegen. That entry can
+  // be skipped. If there are no user defined register files, then reserve a
+  // single entry for the default register file at index #0.
+  unsigned NumRegFiles = std::max(PI.NumRegisterFiles, 1U);
+  RegisterFiles.resize(NumRegFiles);
+  std::fill(RegisterFiles.begin(), RegisterFiles.end(), Empty);
+}
+
 void BackendStatistics::onInstructionEvent(const HWInstructionEvent &Event) {
   switch (Event.Type) {
   default:
@@ -134,13 +154,36 @@
   std::string Buffer;
   raw_string_ostream TempStream(Buffer);
 
-  TempStream << "\n\nRegister File statistics.";
-  for (unsigned I = 0, E = RegisterFiles.size(); I < E; ++I) {
+  TempStream << "\n\nRegister File statistics:";
+  const RegisterFileUsage &GlobalUsage = RegisterFiles[0];
+  TempStream << "\nTotal number of mappings created:   "
+             << GlobalUsage.TotalMappings;
+  TempStream << "\nMax number of mappings used:        "
+             << GlobalUsage.MaxUsedMappings << '\n';
+
+  for (unsigned I = 1, E = RegisterFiles.size(); I < E; ++I) {
     const RegisterFileUsage &RFU = RegisterFiles[I];
-    TempStream << "\nRegister File #" << I;
-    TempStream << "\n  Total number of mappings created: " << RFU.TotalMappings;
-    TempStream << "\n  Max number of mappings used:      "
-               << RFU.MaxUsedMappings;
+    // Obtain the register file descriptor from the scheduling model.
+    assert(STI.getSchedModel().hasExtraProcessorInfo() &&
+           "Unable to find register file info!");
+    const MCExtraProcessorInfo &PI =
+        STI.getSchedModel().getExtraProcessorInfo();
+    assert(I <= PI.NumRegisterFiles && "Unexpected register file index!");
+    const MCRegisterFileDesc &RFDesc = PI.RegisterFiles[I];
+    // Skip invalid register files.
+    if (!RFDesc.NumPhysRegs)
+      continue;
+
+    TempStream << "\n*  Register File #" << I;
+    TempStream << " -- " << StringRef(RFDesc.Name) << ':';
+    TempStream << "\n   Number of physical registers:     ";
+    if (!RFDesc.NumPhysRegs)
+      TempStream << "unbounded";
+    else
+      TempStream << RFDesc.NumPhysRegs;
+    TempStream << "\n   Total number of mappings created: " << RFU.TotalMappings;
+    TempStream << "\n   Max number of mappings used:      "
+               << RFU.MaxUsedMappings << '\n';
   }
 
   TempStream.flush();
diff --git a/llvm/tools/llvm-mca/BackendStatistics.h b/llvm/tools/llvm-mca/BackendStatistics.h
index 5dc0c88..eba8f25 100644
--- a/llvm/tools/llvm-mca/BackendStatistics.h
+++ b/llvm/tools/llvm-mca/BackendStatistics.h
@@ -113,6 +113,8 @@
   // There is one entry for each register file implemented by the processor.
   llvm::SmallVector<RegisterFileUsage, 4> RegisterFiles;
 
+  void initializeRegisterFileInfo();
+
   void printRetireUnitStatistics(llvm::raw_ostream &OS) const;
   void printDispatchUnitStatistics(llvm::raw_ostream &OS) const;
   void printSchedulerStatistics(llvm::raw_ostream &OS) const;
@@ -131,10 +133,9 @@
 public:
   BackendStatistics(const llvm::MCSubtargetInfo &sti)
       : STI(sti), NumDispatched(0), NumIssued(0), NumRetired(0), NumCycles(0),
-        HWStalls(HWStallEvent::LastGenericEvent),
-        // TODO: The view currently assumes a single register file. This will
-        // change in future.
-        RegisterFiles(1) {}
+        HWStalls(HWStallEvent::LastGenericEvent) {
+    initializeRegisterFileInfo();
+  }
 
   void onInstructionEvent(const HWInstructionEvent &Event) override;
 
diff --git a/llvm/tools/llvm-mca/Dispatch.cpp b/llvm/tools/llvm-mca/Dispatch.cpp
index b7aa9ee..383737a 100644
--- a/llvm/tools/llvm-mca/Dispatch.cpp
+++ b/llvm/tools/llvm-mca/Dispatch.cpp
@@ -25,53 +25,99 @@
 
 namespace mca {
 
-void RegisterFile::addRegisterFile(ArrayRef<unsigned> RegisterClasses,
-                                   unsigned NumTemps) {
-  unsigned RegisterFileIndex = RegisterFiles.size();
-  assert(RegisterFileIndex < 32 && "Too many register files!");
-  RegisterFiles.emplace_back(NumTemps);
+void RegisterFile::initialize(const MCSchedModel &SM, unsigned NumRegs) {
+  // Create a default register file that "sees" all the machine registers
+  // declared by the target. The number of physical registers in the default
+  // register file is set equal to `NumRegs`. A value of zero for `NumRegs`
+  // means: this register file has an unbounded number of physical registers.
+  addRegisterFile({} /* all registers */, NumRegs);
+  if (!SM.hasExtraProcessorInfo())
+    return;
 
-  // Special case where there are no register classes specified.
-  // An empty register class set means *all* registers.
-  if (RegisterClasses.empty()) {
-    for (std::pair<WriteState *, unsigned> &Mapping : RegisterMappings)
-      Mapping.second |= 1U << RegisterFileIndex;
-  } else {
-    for (const unsigned RegClassIndex : RegisterClasses) {
-      const MCRegisterClass &RC = MRI.getRegClass(RegClassIndex);
-      for (const MCPhysReg Reg : RC)
-        RegisterMappings[Reg].second |= 1U << RegisterFileIndex;
+  // For each user defined register file, allocate a RegisterMappingTracker
+  // object. The size of every register file, as well as the mapping between
+  // register files and register classes is specified via tablegen.
+  const MCExtraProcessorInfo &Info = SM.getExtraProcessorInfo();
+  for (unsigned I = 0, E = Info.NumRegisterFiles; I < E; ++I) {
+    const MCRegisterFileDesc &RF = Info.RegisterFiles[I];
+    // Skip invalid register files with zero physical registers.
+    unsigned Length = RF.NumRegisterCostEntries;
+    if (!RF.NumPhysRegs)
+      continue;
+    // The cost of a register definition is equivalent to the number of
+    // physical registers that are allocated at register renaming stage.
+    const MCRegisterCostEntry *FirstElt =
+        &Info.RegisterCostTable[RF.RegisterCostEntryIdx];
+    addRegisterFile(ArrayRef<MCRegisterCostEntry>(FirstElt, Length),
+                    RF.NumPhysRegs);
+  }
+}
+
+void RegisterFile::addRegisterFile(ArrayRef<MCRegisterCostEntry> Entries,
+                                   unsigned NumPhysRegs) {
+  // A default register file is always allocated at index #0. That register file
+  // is mainly used to count the total number of mappings created by all
+  // register files at runtime. Users can limit the number of available physical
+  // registers in register file #0 through the command line flag
+  // `-register-file-size`.
+  unsigned RegisterFileIndex = RegisterFiles.size();
+  RegisterFiles.emplace_back(NumPhysRegs);
+
+  // Special case where there is no register class identifier in the set.
+  // An empty set of register classes means: this register file contains all
+  // the physical registers specified by the target.
+  if (Entries.empty()) {
+    for (std::pair<WriteState *, IndexPlusCostPairTy> &Mapping : RegisterMappings)
+      Mapping.second = std::make_pair(RegisterFileIndex, 1U);
+    return;
+  }
+
+  // Now update the cost of individual registers.
+  for (const MCRegisterCostEntry &RCE : Entries) {
+    const MCRegisterClass &RC = MRI.getRegClass(RCE.RegisterClassID);
+    for (const MCPhysReg Reg : RC) {
+      IndexPlusCostPairTy &Entry = RegisterMappings[Reg].second;
+      if (Entry.first) {
+        // The only register file that is allowed to overlap is the default
+        // register file at index #0. The analysis is inaccurate if register
+        // files overlap.
+        errs() << "warning: register " << MRI.getName(Reg)
+               << " defined in multiple register files.";
+      }
+      Entry.first = RegisterFileIndex;
+      Entry.second = RCE.Cost;
     }
   }
 }
 
-void RegisterFile::createNewMappings(unsigned RegisterFileMask,
+void RegisterFile::createNewMappings(IndexPlusCostPairTy Entry,
                                      MutableArrayRef<unsigned> UsedPhysRegs) {
-  assert(RegisterFileMask && "RegisterFileMask cannot be zero!");
-  // Notify each register file that contains RegID.
-  do {
-    unsigned NextRegisterFile = llvm::PowerOf2Floor(RegisterFileMask);
-    unsigned RegisterFileIndex = llvm::countTrailingZeros(NextRegisterFile);
+  unsigned RegisterFileIndex = Entry.first;
+  unsigned Cost = Entry.second;
+  if (RegisterFileIndex) {
     RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex];
-    RMT.NumUsedMappings++;
-    UsedPhysRegs[RegisterFileIndex]++;
-    RegisterFileMask ^= NextRegisterFile;
-  } while (RegisterFileMask);
+    RMT.NumUsedMappings += Cost;
+    UsedPhysRegs[RegisterFileIndex] += Cost;
+  }
+
+  // Now update the default register mapping tracker.
+  RegisterFiles[0].NumUsedMappings += Cost;
+  UsedPhysRegs[0] += Cost;
 }
 
-void RegisterFile::removeMappings(unsigned RegisterFileMask,
+void RegisterFile::removeMappings(IndexPlusCostPairTy Entry,
                                   MutableArrayRef<unsigned> FreedPhysRegs) {
-  assert(RegisterFileMask && "RegisterFileMask cannot be zero!");
-  // Notify each register file that contains RegID.
-  do {
-    unsigned NextRegisterFile = llvm::PowerOf2Floor(RegisterFileMask);
-    unsigned RegisterFileIndex = llvm::countTrailingZeros(NextRegisterFile);
+  unsigned RegisterFileIndex = Entry.first;
+  unsigned Cost = Entry.second;
+  if (RegisterFileIndex) {
     RegisterMappingTracker &RMT = RegisterFiles[RegisterFileIndex];
-    assert(RMT.NumUsedMappings);
-    RMT.NumUsedMappings--;
-    FreedPhysRegs[RegisterFileIndex]++;
-    RegisterFileMask ^= NextRegisterFile;
-  } while (RegisterFileMask);
+    RMT.NumUsedMappings -= Cost;
+    FreedPhysRegs[RegisterFileIndex] += Cost;
+  }
+
+  // Now update the default register mapping tracker.
+  RegisterFiles[0].NumUsedMappings -= Cost;
+  FreedPhysRegs[0] += Cost;
 }
 
 void RegisterFile::addRegisterMapping(WriteState &WS,
@@ -145,32 +191,30 @@
 }
 
 unsigned RegisterFile::isAvailable(ArrayRef<unsigned> Regs) const {
-  SmallVector<unsigned, 4> NumTemporaries(getNumRegisterFiles());
+  SmallVector<unsigned, 4> NumPhysRegs(getNumRegisterFiles());
 
   // Find how many new mappings must be created for each register file.
   for (const unsigned RegID : Regs) {
-    unsigned RegisterFileMask = RegisterMappings[RegID].second;
-    do {
-      unsigned NextRegisterFileID = llvm::PowerOf2Floor(RegisterFileMask);
-      NumTemporaries[llvm::countTrailingZeros(NextRegisterFileID)]++;
-      RegisterFileMask ^= NextRegisterFileID;
-    } while (RegisterFileMask);
+    const IndexPlusCostPairTy &Entry = RegisterMappings[RegID].second;
+    if (Entry.first)
+      NumPhysRegs[Entry.first] += Entry.second;
+    NumPhysRegs[0] += Entry.second;
   }
 
   unsigned Response = 0;
   for (unsigned I = 0, E = getNumRegisterFiles(); I < E; ++I) {
-    unsigned Temporaries = NumTemporaries[I];
-    if (!Temporaries)
+    unsigned NumRegs = NumPhysRegs[I];
+    if (!NumRegs)
       continue;
 
     const RegisterMappingTracker &RMT = RegisterFiles[I];
     if (!RMT.TotalMappings) {
-      // The register file has an unbound number of microarchitectural
+      // The register file has an unbounded number of microarchitectural
       // registers.
       continue;
     }
 
-    if (RMT.TotalMappings < Temporaries) {
+    if (RMT.TotalMappings < NumRegs) {
       // The current register file is too small. This may occur if the number of
       // microarchitectural registers in register file #0 was changed by the
       // users via flag -reg-file-size. Alternatively, the scheduling model
@@ -179,7 +223,7 @@
           "Not enough microarchitectural registers in the register file");
     }
 
-    if (RMT.TotalMappings < RMT.NumUsedMappings + Temporaries)
+    if (RMT.TotalMappings < (RMT.NumUsedMappings + NumRegs))
       Response |= (1U << I);
   }
 
@@ -190,7 +234,8 @@
 void RegisterFile::dump() const {
   for (unsigned I = 0, E = MRI.getNumRegs(); I < E; ++I) {
     const RegisterMapping &RM = RegisterMappings[I];
-    dbgs() << MRI.getName(I) << ", " << I << ", Map=" << RM.second << ", ";
+    dbgs() << MRI.getName(I) << ", " << I << ", Map=" << RM.second.first
+           << ", ";
     if (RM.first)
       RM.first->dump();
     else
diff --git a/llvm/tools/llvm-mca/Dispatch.h b/llvm/tools/llvm-mca/Dispatch.h
index 9ac7c3c..c5a0741 100644
--- a/llvm/tools/llvm-mca/Dispatch.h
+++ b/llvm/tools/llvm-mca/Dispatch.h
@@ -51,27 +51,29 @@
   // This is where information related to the various register files is kept.
   // This set always contains at least one register file at index #0. That
   // register file "sees" all the physical registers declared by the target, and
-  // (by default) it allows an unbound number of mappings.
+  // (by default) it allows an unbounded number of mappings.
   // Users can limit the number of mappings that can be created by register file
   // #0 through the command line flag `-register-file-size`.
   llvm::SmallVector<RegisterMappingTracker, 4> RegisterFiles;
 
+  // This pair is used to identify the owner of a physical register, as well as
+  // the cost of using that register file.
+  using IndexPlusCostPairTy = std::pair<unsigned, unsigned>;
+
   // RegisterMapping objects are mainly used to track physical register
   // definitions. A WriteState object describes a register definition, and it is
   // used to track RAW dependencies (see Instruction.h).  A RegisterMapping
   // object also specifies the set of register files.  The mapping between
   // physreg and register files is done using a "register file mask".
   //
-  // A register file mask identifies a set of register files. Each bit of the
-  // mask representation references a specific register file.
-  // For example:
-  //    0b0001     -->  Register file #0
-  //    0b0010     -->  Register file #1
-  //    0b0100     -->  Register file #2
+  // A register file index identifies a user defined register file.
+  // There is one index per RegisterMappingTracker, and index #0 is reserved to
+  // the default unified register file.
   //
-  // Note that this implementation allows register files to overlap.
-  // The maximum number of register files allowed by this implementation is 32.
-  using RegisterMapping = std::pair<WriteState *, unsigned>;
+  // This implementation does not allow overlapping register files. The only
+  // register file that is allowed to overlap with other register files is
+  // register file #0.
+  using RegisterMapping = std::pair<WriteState *, IndexPlusCostPairTy>;
 
   // This map contains one entry for each physical register defined by the
   // processor scheduling model.
@@ -95,24 +97,32 @@
   // The list of register classes is then converted by the tablegen backend into
   // a list of register class indices. That list, along with the number of
   // available mappings, is then used to create a new RegisterMappingTracker.
-  void addRegisterFile(llvm::ArrayRef<unsigned> RegisterClasses,
-                       unsigned NumTemps);
+  void
+  addRegisterFile(llvm::ArrayRef<llvm::MCRegisterCostEntry> RegisterClasses,
+                  unsigned NumPhysRegs);
 
-  // Allocates a new register mapping in every register file specified by the
-  // register file mask. This method is called from addRegisterMapping.
-  void createNewMappings(unsigned RegisterFileMask,
+  // Allocates register mappings in register file specified by the
+  // IndexPlusCostPairTy object. This method is called from addRegisterMapping.
+  void createNewMappings(IndexPlusCostPairTy IPC,
                          llvm::MutableArrayRef<unsigned> UsedPhysRegs);
 
-  // Removes a previously allocated mapping from each register file in the
-  // RegisterFileMask set. This method is called from invalidateRegisterMapping.
-  void removeMappings(unsigned RegisterFileMask,
+  // Removes a previously allocated mapping from the register file referenced
+  // by the IndexPlusCostPairTy object. This method is called from
+  // invalidateRegisterMapping.
+  void removeMappings(IndexPlusCostPairTy IPC,
                       llvm::MutableArrayRef<unsigned> FreedPhysRegs);
 
+  // Create an instance of RegisterMappingTracker for every register file
+  // specified by the processor model.
+  // If no register file is specified, then this method creates a single
+  // register file with an unbounded number of registers.
+  void initialize(const llvm::MCSchedModel &SM, unsigned NumRegs);
+
 public:
-  RegisterFile(const llvm::MCRegisterInfo &mri, unsigned TempRegs = 0)
-      : MRI(mri), RegisterMappings(MRI.getNumRegs(), {nullptr, 0U}) {
-    addRegisterFile({}, TempRegs);
-    // TODO: teach the scheduling models how to specify multiple register files.
+  RegisterFile(const llvm::MCSchedModel &SM, const llvm::MCRegisterInfo &mri,
+               unsigned NumRegs = 0)
+      : MRI(mri), RegisterMappings(mri.getNumRegs(), {nullptr, {0, 0}}) {
+    initialize(SM, NumRegs);
   }
 
   // Creates a new register mapping for RegID.
@@ -245,7 +255,7 @@
   std::unique_ptr<RetireControlUnit> RCU;
   Backend *Owner;
 
-  bool checkRAT(unsigned Index, const Instruction &Desc);
+  bool checkRAT(unsigned Index, const Instruction &Inst);
   bool checkRCU(unsigned Index, const InstrDesc &Desc);
   bool checkScheduler(unsigned Index, const InstrDesc &Desc);
 
@@ -254,13 +264,14 @@
                                    llvm::ArrayRef<unsigned> UsedPhysRegs);
 
 public:
-  DispatchUnit(Backend *B, const llvm::MCRegisterInfo &MRI,
-               unsigned MicroOpBufferSize, unsigned RegisterFileSize,
-               unsigned MaxRetirePerCycle, unsigned MaxDispatchWidth,
-               Scheduler *Sched)
+  DispatchUnit(Backend *B, const llvm::MCSubtargetInfo &STI,
+               const llvm::MCRegisterInfo &MRI, unsigned MicroOpBufferSize,
+               unsigned RegisterFileSize, unsigned MaxRetirePerCycle,
+               unsigned MaxDispatchWidth, Scheduler *Sched)
       : DispatchWidth(MaxDispatchWidth), AvailableEntries(MaxDispatchWidth),
         CarryOver(0U), SC(Sched),
-        RAT(llvm::make_unique<RegisterFile>(MRI, RegisterFileSize)),
+        RAT(llvm::make_unique<RegisterFile>(STI.getSchedModel(), MRI,
+                                            RegisterFileSize)),
         RCU(llvm::make_unique<RetireControlUnit>(MicroOpBufferSize,
                                                  MaxRetirePerCycle, this)),
         Owner(B) {}