LLVM  8.0.0svn
MCRegisterInfo.cpp
Go to the documentation of this file.
1 //===- MC/MCRegisterInfo.cpp - Target Register Description ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements MCRegisterInfo functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/MC/MCRegisterInfo.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/Twine.h"
18 #include <algorithm>
19 #include <cassert>
20 #include <cstdint>
21 
22 using namespace llvm;
23 
24 unsigned MCRegisterInfo::getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
25  const MCRegisterClass *RC) const {
26  for (MCSuperRegIterator Supers(Reg, this); Supers.isValid(); ++Supers)
27  if (RC->contains(*Supers) && Reg == getSubReg(*Supers, SubIdx))
28  return *Supers;
29  return 0;
30 }
31 
32 unsigned MCRegisterInfo::getSubReg(unsigned 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(Reg).SubRegIndices;
38  for (MCSubRegIterator Subs(Reg, this); Subs.isValid(); ++Subs, ++SRI)
39  if (*SRI == Idx)
40  return *Subs;
41  return 0;
42 }
43 
44 unsigned MCRegisterInfo::getSubRegIndex(unsigned Reg, unsigned SubReg) const {
45  assert(SubReg && SubReg < getNumRegs() && "This is not a register");
46  // Get a pointer to the corresponding SubRegIndices list. This list has the
47  // name of each sub-register in the same order as MCSubRegIterator.
48  const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
49  for (MCSubRegIterator Subs(Reg, this); Subs.isValid(); ++Subs, ++SRI)
50  if (*Subs == SubReg)
51  return *SRI;
52  return 0;
53 }
54 
55 unsigned MCRegisterInfo::getSubRegIdxSize(unsigned Idx) const {
56  assert(Idx && Idx < getNumSubRegIndices() &&
57  "This is not a subregister index");
58  return SubRegIdxRanges[Idx].Size;
59 }
60 
61 unsigned MCRegisterInfo::getSubRegIdxOffset(unsigned Idx) const {
62  assert(Idx && Idx < getNumSubRegIndices() &&
63  "This is not a subregister index");
64  return SubRegIdxRanges[Idx].Offset;
65 }
66 
67 int MCRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
68  const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
69  unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
70 
71  if (!M)
72  return -1;
73  DwarfLLVMRegPair Key = { RegNum, 0 };
74  const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
75  if (I == M+Size || I->FromReg != RegNum)
76  return -1;
77  return I->ToReg;
78 }
79 
80 int MCRegisterInfo::getLLVMRegNum(unsigned RegNum, bool isEH) const {
81  const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
82  unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
83 
84  if (!M)
85  return -1;
86  DwarfLLVMRegPair Key = { RegNum, 0 };
87  const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
88  assert(I != M+Size && I->FromReg == RegNum && "Invalid RegNum");
89  return I->ToReg;
90 }
91 
92 int MCRegisterInfo::getLLVMRegNumFromEH(unsigned RegNum) const {
93  const DwarfLLVMRegPair *M = EHDwarf2LRegs;
94  unsigned Size = EHDwarf2LRegsSize;
95 
96  if (!M)
97  return -1;
98  DwarfLLVMRegPair Key = { RegNum, 0 };
99  const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
100  if (I == M+Size || I->FromReg != RegNum)
101  return -1;
102  return I->ToReg;
103 }
104 
106  // On ELF platforms, DWARF EH register numbers are the same as DWARF
107  // other register numbers. On Darwin x86, they differ and so need to be
108  // mapped. The .cfi_* directives accept integer literals as well as
109  // register names and should generate exactly what the assembly code
110  // asked for, so there might be DWARF/EH register numbers that don't have
111  // a corresponding LLVM register number at all. So if we can't map the
112  // EH register number to an LLVM register number, assume it's just a
113  // valid DWARF register number as is.
114  int LRegNum = getLLVMRegNumFromEH(RegNum);
115  if (LRegNum != -1)
116  return getDwarfRegNum(LRegNum, false);
117  return RegNum;
118 }
119 
120 int MCRegisterInfo::getSEHRegNum(unsigned RegNum) const {
121  const DenseMap<unsigned, int>::const_iterator I = L2SEHRegs.find(RegNum);
122  if (I == L2SEHRegs.end()) return (int)RegNum;
123  return I->second;
124 }
125 
126 int MCRegisterInfo::getCodeViewRegNum(unsigned RegNum) const {
127  if (L2CVRegs.empty())
128  report_fatal_error("target does not implement codeview register mapping");
129  const DenseMap<unsigned, int>::const_iterator I = L2CVRegs.find(RegNum);
130  if (I == L2CVRegs.end())
131  report_fatal_error("unknown codeview register " + (RegNum < getNumRegs()
132  ? getName(RegNum)
133  : Twine(RegNum)));
134  return I->second;
135 }
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const
For a given register pair, return the sub-register index if the second register is a sub-register of ...
unsigned Reg
int getLLVMRegNumFromEH(unsigned RegNum) const
Map a DWARF EH register back to a target register (same as getLLVMRegNum(RegNum, true)) but return -1...
int getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const
Map a target EH register number to an equivalent DWARF register number.
MCSuperRegIterator enumerates all super-registers of Reg.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool contains(unsigned Reg) const
contains - Return true if the specified register is included in this register class.
unsigned SubReg
int getDwarfRegNum(unsigned RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
Key
PAL metadata keys.
auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range))
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1138
MCRegisterClass - Base class of TargetRegisterClass.
const char * getName(unsigned RegNo) const
Return the human-readable symbolic target-specific name for the specified physical register...
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:151
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
unsigned getMatchingSuperReg(unsigned 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...
unsigned getSubReg(unsigned Reg, unsigned Idx) const
Returns the physical register number of sub-register "Index" for physical register RegNo...
unsigned getSubRegIdxOffset(unsigned Idx) const
Get the offset of the bit range covered by a sub-register index.
MCSubRegIterator enumerates all sub-registers of Reg.
DwarfLLVMRegPair - Emitted by tablegen so Dwarf<->LLVM reg mappings can be performed with a binary se...
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
int getCodeViewRegNum(unsigned RegNum) const
Map a target register to an equivalent CodeView register number.
#define I(x, y, z)
Definition: MD5.cpp:58
iterator end()
Definition: DenseMap.h:84
unsigned getNumSubRegIndices() const
Return the number of sub-register indices understood by the target.
uint32_t Size
Definition: Profile.cpp:47
LLVM_NODISCARD bool empty() const
Definition: DenseMap.h:98
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
int getSEHRegNum(unsigned RegNum) const
Map a target register to an equivalent SEH register number.
unsigned getSubRegIdxSize(unsigned Idx) const
Get the size of the bit range covered by a sub-register index.
int getLLVMRegNum(unsigned RegNum, bool isEH) const
Map a dwarf register back to a target register.