1 //===- llvm/CodeGen/GlobalISel/RegisterBank.cpp - Register Bank --*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 /// This file implements the RegisterBank class.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/CodeGen/RegisterBank.h"
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/CodeGen/RegisterBankInfo.h"
15 #include "llvm/CodeGen/TargetRegisterInfo.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/Debug.h"
19 #define DEBUG_TYPE "registerbank"
23 bool RegisterBank::verify(const RegisterBankInfo
&RBI
,
24 const TargetRegisterInfo
&TRI
) const {
25 for (unsigned RCId
= 0, End
= TRI
.getNumRegClasses(); RCId
!= End
; ++RCId
) {
26 const TargetRegisterClass
&RC
= *TRI
.getRegClass(RCId
);
30 // Verify that the register bank covers all the sub classes of the
33 // Use a different (slow in that case) method than
34 // RegisterBankInfo to find the subclasses of RC, to make sure
35 // both agree on the covers.
36 for (unsigned SubRCId
= 0; SubRCId
!= End
; ++SubRCId
) {
37 const TargetRegisterClass
&SubRC
= *TRI
.getRegClass(RCId
);
39 if (!RC
.hasSubClassEq(&SubRC
))
42 // Verify that the Size of the register bank is big enough to cover
43 // all the register classes it covers.
44 assert(RBI
.getMaximumSize(getID()) >= TRI
.getRegSizeInBits(SubRC
) &&
45 "Size is not big enough for all the subclasses!");
46 assert(covers(SubRC
) && "Not all subclasses are covered");
52 bool RegisterBank::covers(const TargetRegisterClass
&RC
) const {
53 return (CoveredClasses
[RC
.getID() / 32] & (1U << RC
.getID() % 32)) != 0;
56 bool RegisterBank::operator==(const RegisterBank
&OtherRB
) const {
57 // There must be only one instance of a given register bank alive
58 // for the whole compilation.
59 // The RegisterBankInfo is supposed to enforce that.
60 assert((OtherRB
.getID() != getID() || &OtherRB
== this) &&
61 "ID does not uniquely identify a RegisterBank");
62 return &OtherRB
== this;
65 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
66 LLVM_DUMP_METHOD
void RegisterBank::dump(const TargetRegisterInfo
*TRI
) const {
67 print(dbgs(), /* IsForDebug */ true, TRI
);
71 void RegisterBank::print(raw_ostream
&OS
, bool IsForDebug
,
72 const TargetRegisterInfo
*TRI
) const {
78 for (int i
= 0, e
= ((NumRegClasses
+ 31) / 32); i
!= e
; ++i
)
79 Count
+= llvm::popcount(CoveredClasses
[i
]);
81 OS
<< "(ID:" << getID() << ")\n"
82 << "Number of Covered register classes: " << Count
<< '\n';
83 // Print all the subclasses if we can.
84 // This register classes may not be properly initialized yet.
85 if (!TRI
|| NumRegClasses
== 0)
87 assert(NumRegClasses
== TRI
->getNumRegClasses() &&
88 "TRI does not match the initialization process?");
89 OS
<< "Covered register classes:\n";
91 for (unsigned RCId
= 0, End
= TRI
->getNumRegClasses(); RCId
!= End
; ++RCId
) {
92 const TargetRegisterClass
&RC
= *TRI
->getRegClass(RCId
);
95 OS
<< LS
<< TRI
->getRegClassName(&RC
);