LLVM 19.0.0git
MCRegisterInfo.cpp
Go to the documentation of this file.
1//===- MC/MCRegisterInfo.cpp - Target Register Description ----------------===//
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//
9// This file implements MCRegisterInfo functions.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/Twine.h"
17#include <algorithm>
18#include <cassert>
19#include <cstdint>
20
21using namespace llvm;
22
25 const MCRegisterClass *RC) const {
26 for (MCPhysReg Super : superregs(Reg))
27 if (RC->contains(Super) && Reg == getSubReg(Super, SubIdx))
28 return Super;
29 return 0;
30}
31
34 "This is not a subregister index");
35 // Get a pointer to the corresponding SubRegIndices list. This list has the
36 // name of each sub-register in the same order as MCSubRegIterator.
37 const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
38 for (MCPhysReg Sub : subregs(Reg)) {
39 if (*SRI == Idx)
40 return Sub;
41 ++SRI;
42 }
43 return 0;
44}
45
47 MCRegister SubReg) const {
48 assert(SubReg && SubReg < getNumRegs() && "This is not a register");
49 // Get a pointer to the corresponding SubRegIndices list. This list has the
50 // name of each sub-register in the same order as MCSubRegIterator.
51 const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
52 for (MCPhysReg Sub : subregs(Reg)) {
53 if (Sub == SubReg)
54 return *SRI;
55 ++SRI;
56 }
57 return 0;
58}
59
60int MCRegisterInfo::getDwarfRegNum(MCRegister RegNum, bool isEH) const {
61 const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
62 unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
63
64 if (!M)
65 return -1;
66 DwarfLLVMRegPair Key = { RegNum, 0 };
67 const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
68 if (I == M+Size || I->FromReg != RegNum)
69 return -1;
70 return I->ToReg;
71}
72
73std::optional<unsigned> MCRegisterInfo::getLLVMRegNum(unsigned RegNum,
74 bool isEH) const {
75 const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
76 unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
77
78 if (!M)
79 return std::nullopt;
80 DwarfLLVMRegPair Key = { RegNum, 0 };
81 const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
82 if (I != M + Size && I->FromReg == RegNum)
83 return I->ToReg;
84 return std::nullopt;
85}
86
88 // On ELF platforms, DWARF EH register numbers are the same as DWARF
89 // other register numbers. On Darwin x86, they differ and so need to be
90 // mapped. The .cfi_* directives accept integer literals as well as
91 // register names and should generate exactly what the assembly code
92 // asked for, so there might be DWARF/EH register numbers that don't have
93 // a corresponding LLVM register number at all. So if we can't map the
94 // EH register number to an LLVM register number, assume it's just a
95 // valid DWARF register number as is.
96 if (std::optional<unsigned> LRegNum = getLLVMRegNum(RegNum, true)) {
97 int DwarfRegNum = getDwarfRegNum(*LRegNum, false);
98 if (DwarfRegNum == -1)
99 return RegNum;
100 else
101 return DwarfRegNum;
102 }
103 return RegNum;
104}
105
107 const DenseMap<MCRegister, int>::const_iterator I = L2SEHRegs.find(RegNum);
108 if (I == L2SEHRegs.end()) return (int)RegNum;
109 return I->second;
110}
111
113 if (L2CVRegs.empty())
114 report_fatal_error("target does not implement codeview register mapping");
115 const DenseMap<MCRegister, int>::const_iterator I = L2CVRegs.find(RegNum);
116 if (I == L2CVRegs.end())
117 report_fatal_error("unknown codeview register " + (RegNum < getNumRegs()
118 ? getName(RegNum)
119 : Twine(RegNum)));
120 return I->second;
121}
122
124 // Regunits are numerically ordered. Find a common unit.
125 auto RangeA = regunits(RegA);
126 MCRegUnitIterator IA = RangeA.begin(), EA = RangeA.end();
127 auto RangeB = regunits(RegB);
128 MCRegUnitIterator IB = RangeB.begin(), EB = RangeB.end();
129 do {
130 if (*IA == *IB)
131 return true;
132 } while (*IA < *IB ? ++IA != EA : ++IB != EB);
133 return false;
134}
unsigned SubReg
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
uint64_t Size
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCRegisterClass - Base class of TargetRegisterClass.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
unsigned getNumSubRegIndices() const
Return the number of sub-register indices understood by the target.
int getDwarfRegNum(MCRegister RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
bool regsOverlap(MCRegister RegA, MCRegister RegB) const
Returns true if the two registers are equal or alias each other.
MCRegister getMatchingSuperReg(MCRegister Reg, unsigned SubIdx, const MCRegisterClass *RC) const
Return a super-register of the specified register Reg so its sub-register of index SubIdx is Reg.
int getCodeViewRegNum(MCRegister RegNum) const
Map a target register to an equivalent CodeView register number.
std::optional< unsigned > getLLVMRegNum(unsigned RegNum, bool isEH) const
Map a dwarf register back to a target register.
const MCRegisterDesc & get(MCRegister RegNo) const
Provide a get method, equivalent to [], but more useful with a pointer to this object.
int getSEHRegNum(MCRegister RegNum) const
Map a target register to an equivalent SEH register number.
iterator_range< MCSuperRegIterator > superregs(MCRegister Reg) const
Return an iterator range over all super-registers of Reg, excluding Reg.
const char * getName(MCRegister RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register.
int getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const
Map a target EH register number to an equivalent DWARF register number.
iterator_range< MCSubRegIterator > subregs(MCRegister Reg) const
Return an iterator range over all sub-registers of Reg, excluding Reg.
unsigned getSubRegIndex(MCRegister RegNo, MCRegister SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
iterator_range< MCRegUnitIterator > regunits(MCRegister Reg) const
Returns an iterator range over all regunits for Reg.
MCRegister getSubReg(MCRegister Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be performed with a binary se...