RegisterBank.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===- llvm/CodeGen/GlobalISel/RegisterBank.cpp - Register Bank --*- C++ -*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. /// This file implements the RegisterBank class.
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/CodeGen/RegisterBank.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/CodeGen/TargetRegisterInfo.h"
  14. #include "llvm/Config/llvm-config.h"
  15. #include "llvm/Support/Debug.h"
  16. #define DEBUG_TYPE "registerbank"
  17. using namespace llvm;
  18. const unsigned RegisterBank::InvalidID = UINT_MAX;
  19. RegisterBank::RegisterBank(
  20. unsigned ID, const char *Name, unsigned Size,
  21. const uint32_t *CoveredClasses, unsigned NumRegClasses)
  22. : ID(ID), Name(Name), Size(Size) {
  23. ContainedRegClasses.resize(NumRegClasses);
  24. ContainedRegClasses.setBitsInMask(CoveredClasses);
  25. }
  26. bool RegisterBank::verify(const TargetRegisterInfo &TRI) const {
  27. assert(isValid() && "Invalid register bank");
  28. for (unsigned RCId = 0, End = TRI.getNumRegClasses(); RCId != End; ++RCId) {
  29. const TargetRegisterClass &RC = *TRI.getRegClass(RCId);
  30. if (!covers(RC))
  31. continue;
  32. // Verify that the register bank covers all the sub classes of the
  33. // classes it covers.
  34. // Use a different (slow in that case) method than
  35. // RegisterBankInfo to find the subclasses of RC, to make sure
  36. // both agree on the covers.
  37. for (unsigned SubRCId = 0; SubRCId != End; ++SubRCId) {
  38. const TargetRegisterClass &SubRC = *TRI.getRegClass(RCId);
  39. if (!RC.hasSubClassEq(&SubRC))
  40. continue;
  41. // Verify that the Size of the register bank is big enough to cover
  42. // all the register classes it covers.
  43. assert(getSize() >= TRI.getRegSizeInBits(SubRC) &&
  44. "Size is not big enough for all the subclasses!");
  45. assert(covers(SubRC) && "Not all subclasses are covered");
  46. }
  47. }
  48. return true;
  49. }
  50. bool RegisterBank::covers(const TargetRegisterClass &RC) const {
  51. assert(isValid() && "RB hasn't been initialized yet");
  52. return ContainedRegClasses.test(RC.getID());
  53. }
  54. bool RegisterBank::isValid() const {
  55. return ID != InvalidID && Name != nullptr && Size != 0 &&
  56. // A register bank that does not cover anything is useless.
  57. !ContainedRegClasses.empty();
  58. }
  59. bool RegisterBank::operator==(const RegisterBank &OtherRB) const {
  60. // There must be only one instance of a given register bank alive
  61. // for the whole compilation.
  62. // The RegisterBankInfo is supposed to enforce that.
  63. assert((OtherRB.getID() != getID() || &OtherRB == this) &&
  64. "ID does not uniquely identify a RegisterBank");
  65. return &OtherRB == this;
  66. }
  67. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  68. LLVM_DUMP_METHOD void RegisterBank::dump(const TargetRegisterInfo *TRI) const {
  69. print(dbgs(), /* IsForDebug */ true, TRI);
  70. }
  71. #endif
  72. void RegisterBank::print(raw_ostream &OS, bool IsForDebug,
  73. const TargetRegisterInfo *TRI) const {
  74. OS << getName();
  75. if (!IsForDebug)
  76. return;
  77. OS << "(ID:" << getID() << ", Size:" << getSize() << ")\n"
  78. << "isValid:" << isValid() << '\n'
  79. << "Number of Covered register classes: " << ContainedRegClasses.count()
  80. << '\n';
  81. // Print all the subclasses if we can.
  82. // This register classes may not be properly initialized yet.
  83. if (!TRI || ContainedRegClasses.empty())
  84. return;
  85. assert(ContainedRegClasses.size() == TRI->getNumRegClasses() &&
  86. "TRI does not match the initialization process?");
  87. OS << "Covered register classes:\n";
  88. ListSeparator LS;
  89. for (unsigned RCId = 0, End = TRI->getNumRegClasses(); RCId != End; ++RCId) {
  90. const TargetRegisterClass &RC = *TRI->getRegClass(RCId);
  91. if (covers(RC))
  92. OS << LS << TRI->getRegClassName(&RC);
  93. }
  94. }