1 //===- MC/MCRegisterInfo.cpp - Target Register Description ----------------===//
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 MCRegisterInfo functions.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/MC/MCRegisterInfo.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/ErrorHandling.h"
24 /// MCRegAliasIterator enumerates all registers aliasing Reg. This iterator
25 /// does not guarantee any ordering or that entries are unique.
26 class MCRegAliasIteratorImpl
{
29 const MCRegisterInfo
*MCRI
;
32 MCRegUnitRootIterator RRI
;
33 MCSuperRegIterator SI
;
36 MCRegAliasIteratorImpl(MCRegister Reg
, const MCRegisterInfo
*MCRI
)
37 : Reg(Reg
), MCRI(MCRI
) {
39 // Initialize the iterators.
40 for (RI
= MCRegUnitIterator(Reg
, MCRI
); RI
.isValid(); ++RI
) {
41 for (RRI
= MCRegUnitRootIterator(*RI
, MCRI
); RRI
.isValid(); ++RRI
) {
42 for (SI
= MCSuperRegIterator(*RRI
, MCRI
, true); SI
.isValid(); ++SI
) {
50 bool isValid() const { return RI
.isValid(); }
52 MCRegister
operator*() const {
53 assert(SI
.isValid() && "Cannot dereference an invalid iterator.");
58 // Assuming SI is valid.
65 SI
= MCSuperRegIterator(*RRI
, MCRI
, true);
71 RRI
= MCRegUnitRootIterator(*RI
, MCRI
);
72 SI
= MCSuperRegIterator(*RRI
, MCRI
, true);
76 MCRegAliasIteratorImpl
&operator++() {
77 assert(isValid() && "Cannot move off the end of the list.");
80 while (isValid() && *SI
== Reg
);
86 ArrayRef
<MCPhysReg
> MCRegisterInfo::getCachedAliasesOf(MCPhysReg R
) const {
87 auto &Aliases
= RegAliasesCache
[R
];
91 for (MCRegAliasIteratorImpl
It(R
, this); It
.isValid(); ++It
)
92 Aliases
.push_back(*It
);
95 Aliases
.erase(unique(Aliases
), Aliases
.end());
96 assert(none_of(Aliases
, [&](auto &Cur
) { return R
== Cur
; }) &&
97 "MCRegAliasIteratorImpl includes Self!");
99 // Always put "self" at the end, so the iterator can choose to ignore it.
100 // For registers without aliases, it also serves as a sentinel value that
101 // tells us to not recompute the alias set.
102 Aliases
.push_back(R
);
103 Aliases
.shrink_to_fit();
108 MCRegisterInfo::getMatchingSuperReg(MCRegister Reg
, unsigned SubIdx
,
109 const MCRegisterClass
*RC
) const {
110 for (MCPhysReg Super
: superregs(Reg
))
111 if (RC
->contains(Super
) && Reg
== getSubReg(Super
, SubIdx
))
116 MCRegister
MCRegisterInfo::getSubReg(MCRegister Reg
, unsigned Idx
) const {
117 assert(Idx
&& Idx
< getNumSubRegIndices() &&
118 "This is not a subregister index");
119 // Get a pointer to the corresponding SubRegIndices list. This list has the
120 // name of each sub-register in the same order as MCSubRegIterator.
121 const uint16_t *SRI
= SubRegIndices
+ get(Reg
).SubRegIndices
;
122 for (MCPhysReg Sub
: subregs(Reg
)) {
130 unsigned MCRegisterInfo::getSubRegIndex(MCRegister Reg
,
131 MCRegister SubReg
) const {
132 assert(SubReg
&& SubReg
< getNumRegs() && "This is not a register");
133 // Get a pointer to the corresponding SubRegIndices list. This list has the
134 // name of each sub-register in the same order as MCSubRegIterator.
135 const uint16_t *SRI
= SubRegIndices
+ get(Reg
).SubRegIndices
;
136 for (MCPhysReg Sub
: subregs(Reg
)) {
144 int MCRegisterInfo::getDwarfRegNum(MCRegister RegNum
, bool isEH
) const {
145 const DwarfLLVMRegPair
*M
= isEH
? EHL2DwarfRegs
: L2DwarfRegs
;
146 unsigned Size
= isEH
? EHL2DwarfRegsSize
: L2DwarfRegsSize
;
150 DwarfLLVMRegPair Key
= { RegNum
, 0 };
151 const DwarfLLVMRegPair
*I
= std::lower_bound(M
, M
+Size
, Key
);
152 if (I
== M
+Size
|| I
->FromReg
!= RegNum
)
157 std::optional
<unsigned> MCRegisterInfo::getLLVMRegNum(unsigned RegNum
,
159 const DwarfLLVMRegPair
*M
= isEH
? EHDwarf2LRegs
: Dwarf2LRegs
;
160 unsigned Size
= isEH
? EHDwarf2LRegsSize
: Dwarf2LRegsSize
;
164 DwarfLLVMRegPair Key
= { RegNum
, 0 };
165 const DwarfLLVMRegPair
*I
= std::lower_bound(M
, M
+Size
, Key
);
166 if (I
!= M
+ Size
&& I
->FromReg
== RegNum
)
171 int MCRegisterInfo::getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum
) const {
172 // On ELF platforms, DWARF EH register numbers are the same as DWARF
173 // other register numbers. On Darwin x86, they differ and so need to be
174 // mapped. The .cfi_* directives accept integer literals as well as
175 // register names and should generate exactly what the assembly code
176 // asked for, so there might be DWARF/EH register numbers that don't have
177 // a corresponding LLVM register number at all. So if we can't map the
178 // EH register number to an LLVM register number, assume it's just a
179 // valid DWARF register number as is.
180 if (std::optional
<unsigned> LRegNum
= getLLVMRegNum(RegNum
, true)) {
181 int DwarfRegNum
= getDwarfRegNum(*LRegNum
, false);
182 if (DwarfRegNum
== -1)
190 int MCRegisterInfo::getSEHRegNum(MCRegister RegNum
) const {
191 const DenseMap
<MCRegister
, int>::const_iterator I
= L2SEHRegs
.find(RegNum
);
192 if (I
== L2SEHRegs
.end()) return (int)RegNum
;
196 int MCRegisterInfo::getCodeViewRegNum(MCRegister RegNum
) const {
197 if (L2CVRegs
.empty())
198 report_fatal_error("target does not implement codeview register mapping");
199 const DenseMap
<MCRegister
, int>::const_iterator I
= L2CVRegs
.find(RegNum
);
200 if (I
== L2CVRegs
.end())
201 report_fatal_error("unknown codeview register " + (RegNum
< getNumRegs()
207 bool MCRegisterInfo::regsOverlap(MCRegister RegA
, MCRegister RegB
) const {
208 // Regunits are numerically ordered. Find a common unit.
209 auto RangeA
= regunits(RegA
);
210 MCRegUnitIterator IA
= RangeA
.begin(), EA
= RangeA
.end();
211 auto RangeB
= regunits(RegB
);
212 MCRegUnitIterator IB
= RangeB
.begin(), EB
= RangeB
.end();
216 } while (*IA
< *IB
? ++IA
!= EA
: ++IB
!= EB
);