Skip to content

[GISel] Remove BitVector from RegBank. Use tablegen CoverageData tables directly. NFC #71105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Nov 2, 2023

RegBanks are allocated as global variables. The use of BitVector causes a static global constructor to be used. The BitVector is initialized from a table of bits that is created by tablegen. We can keep a pointer to that data and use it as the bit vector instead.

This does require a little bit of manual indexing and reimplementation of BitVector::count.

@llvmbot
Copy link
Member

llvmbot commented Nov 2, 2023

@llvm/pr-subscribers-llvm-regalloc

Author: Craig Topper (topperc)

Changes

RegBanks are allocated as global variables. The use of BitVector causes a static global constructor to be used. The BitVector is initialized from a table of bits that is created by tablegen. We can keep a pointer to that data and use it as the bit vector instead.

This does require a little bit of manual indexing and reimplementation of BitVector::count.


Full diff: https://github.com/llvm/llvm-project/pull/71105.diff

2 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/RegisterBank.h (+6-3)
  • (modified) llvm/lib/CodeGen/RegisterBank.cpp (+10-14)
diff --git a/llvm/include/llvm/CodeGen/RegisterBank.h b/llvm/include/llvm/CodeGen/RegisterBank.h
index ee295c7cdde0048..9e0c3bd884a39ae 100644
--- a/llvm/include/llvm/CodeGen/RegisterBank.h
+++ b/llvm/include/llvm/CodeGen/RegisterBank.h
@@ -13,7 +13,7 @@
 #ifndef LLVM_CODEGEN_REGISTERBANK_H
 #define LLVM_CODEGEN_REGISTERBANK_H
 
-#include "llvm/ADT/BitVector.h"
+#include <cstdint>
 
 namespace llvm {
 // Forward declarations.
@@ -28,8 +28,9 @@ class TargetRegisterInfo;
 class RegisterBank {
 private:
   unsigned ID;
+  unsigned NumRegClasses;
   const char *Name;
-  BitVector ContainedRegClasses;
+  const uint32_t *CoveredClasses;
 
   /// Sentinel value used to recognize register bank not properly
   /// initialized yet.
@@ -40,7 +41,9 @@ class RegisterBank {
 
 public:
   RegisterBank(unsigned ID, const char *Name, const uint32_t *CoveredClasses,
-               unsigned NumRegClasses);
+               unsigned NumRegClasses)
+      : ID(ID), NumRegClasses(NumRegClasses), Name(Name),
+        CoveredClasses(CoveredClasses) {}
 
   /// Get the identifier of this register bank.
   unsigned getID() const { return ID; }
diff --git a/llvm/lib/CodeGen/RegisterBank.cpp b/llvm/lib/CodeGen/RegisterBank.cpp
index 8e0a0b0dc2824a0..a30e4ac6cfeb317 100644
--- a/llvm/lib/CodeGen/RegisterBank.cpp
+++ b/llvm/lib/CodeGen/RegisterBank.cpp
@@ -22,14 +22,6 @@ using namespace llvm;
 
 const unsigned RegisterBank::InvalidID = UINT_MAX;
 
-RegisterBank::RegisterBank(unsigned ID, const char *Name,
-                           const uint32_t *CoveredClasses,
-                           unsigned NumRegClasses)
-    : ID(ID), Name(Name) {
-  ContainedRegClasses.resize(NumRegClasses);
-  ContainedRegClasses.setBitsInMask(CoveredClasses);
-}
-
 bool RegisterBank::verify(const RegisterBankInfo &RBI,
                           const TargetRegisterInfo &TRI) const {
   assert(isValid() && "Invalid register bank");
@@ -62,13 +54,13 @@ bool RegisterBank::verify(const RegisterBankInfo &RBI,
 
 bool RegisterBank::covers(const TargetRegisterClass &RC) const {
   assert(isValid() && "RB hasn't been initialized yet");
-  return ContainedRegClasses.test(RC.getID());
+  return (CoveredClasses[RC.getID() / 32] & (1U << RC.getID() % 32)) != 0;
 }
 
 bool RegisterBank::isValid() const {
   return ID != InvalidID && Name != nullptr &&
          // A register bank that does not cover anything is useless.
-         !ContainedRegClasses.empty();
+         CoveredClasses != nullptr && NumRegClasses != 0;
 }
 
 bool RegisterBank::operator==(const RegisterBank &OtherRB) const {
@@ -91,15 +83,19 @@ void RegisterBank::print(raw_ostream &OS, bool IsForDebug,
   OS << getName();
   if (!IsForDebug)
     return;
+
+  unsigned Count = 0;
+  for (int i = 0, e = ((NumRegClasses + 31) / 32); i != e; ++i)
+    Count += llvm::popcount(CoveredClasses[i]);
+
   OS << "(ID:" << getID() << ")\n"
      << "isValid:" << isValid() << '\n'
-     << "Number of Covered register classes: " << ContainedRegClasses.count()
-     << '\n';
+     << "Number of Covered register classes: " << Count << '\n';
   // Print all the subclasses if we can.
   // This register classes may not be properly initialized yet.
-  if (!TRI || ContainedRegClasses.empty())
+  if (!TRI || NumRegClasses == 0)
     return;
-  assert(ContainedRegClasses.size() == TRI->getNumRegClasses() &&
+  assert(NumRegClasses == TRI->getNumRegClasses() &&
          "TRI does not match the initialization process?");
   OS << "Covered register classes:\n";
   ListSeparator LS;

…es directly. NFC

RegBanks are allocated as global variables. The use of BitVector
causes a static global constructor to be used. The BitVector is
initialized from a table of bits that is created by tablegen. We
can keep a pointer to that data and use it as the bit vector instead.

This does require a little bit of manual indexing and reimplementation
of BitVector::count.
@topperc topperc force-pushed the pr/regbank-bitvector branch from ff06f82 to 1d3e50f Compare November 3, 2023 00:50
@topperc topperc merged commit 46732e2 into llvm:main Nov 3, 2023
@topperc topperc deleted the pr/regbank-bitvector branch November 3, 2023 00:54
@qcolombet
Copy link
Collaborator

Thanks for the clean-up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants