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
13#include "llvm/MC/MCRegisterInfo.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/Support/ErrorHandling.h"
17#include <algorithm>
18#include <cassert>
19#include <cstdint>
20
21using namespace llvm;
22
23MCRegister
24MCRegisterInfo::getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
25 const MCRegisterClass *RC) const {
26 for (MCPhysReg Super : superregs(Reg))
27 if (RC->contains(Reg: Super) && Reg == getSubReg(Reg: Super, Idx: SubIdx))
28 return Super;
29 return 0;
30}
31
32MCRegister MCRegisterInfo::getSubReg(MCRegister Reg, unsigned Idx) const {
33 assert(Idx && Idx < getNumSubRegIndices() &&
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(RegNo: Reg).SubRegIndices;
38 for (MCPhysReg Sub : subregs(Reg)) {
39 if (*SRI == Idx)
40 return Sub;
41 ++SRI;
42 }
43 return 0;
44}
45
46unsigned MCRegisterInfo::getSubRegIndex(MCRegister Reg,
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(RegNo: 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 = { .FromReg: RegNum, .ToReg: 0 };
67 const DwarfLLVMRegPair *I = std::lower_bound(first: M, last: M+Size, val: 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 = { .FromReg: RegNum, .ToReg: 0 };
81 const DwarfLLVMRegPair *I = std::lower_bound(first: M, last: M+Size, val: Key);
82 if (I != M + Size && I->FromReg == RegNum)
83 return I->ToReg;
84 return std::nullopt;
85}
86
87int MCRegisterInfo::getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const {
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, isEH: true)) {
97 int DwarfRegNum = getDwarfRegNum(RegNum: *LRegNum, isEH: false);
98 if (DwarfRegNum == -1)
99 return RegNum;
100 else
101 return DwarfRegNum;
102 }
103 return RegNum;
104}
105
106int MCRegisterInfo::getSEHRegNum(MCRegister RegNum) const {
107 const DenseMap<MCRegister, int>::const_iterator I = L2SEHRegs.find(Val: RegNum);
108 if (I == L2SEHRegs.end()) return (int)RegNum;
109 return I->second;
110}
111
112int MCRegisterInfo::getCodeViewRegNum(MCRegister RegNum) const {
113 if (L2CVRegs.empty())
114 report_fatal_error(reason: "target does not implement codeview register mapping");
115 const DenseMap<MCRegister, int>::const_iterator I = L2CVRegs.find(Val: RegNum);
116 if (I == L2CVRegs.end())
117 report_fatal_error(reason: "unknown codeview register " + (RegNum < getNumRegs()
118 ? getName(RegNo: RegNum)
119 : Twine(RegNum)));
120 return I->second;
121}
122
123bool MCRegisterInfo::regsOverlap(MCRegister RegA, MCRegister RegB) const {
124 // Regunits are numerically ordered. Find a common unit.
125 auto RangeA = regunits(Reg: RegA);
126 MCRegUnitIterator IA = RangeA.begin(), EA = RangeA.end();
127 auto RangeB = regunits(Reg: 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}
135

source code of llvm/lib/MC/MCRegisterInfo.cpp