MCRegisterInfo.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "llvm/MC/MCRegisterInfo.h"
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <cstdint>
  19. using namespace llvm;
  20. MCRegister
  21. MCRegisterInfo::getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,
  22. const MCRegisterClass *RC) const {
  23. for (MCSuperRegIterator Supers(Reg, this); Supers.isValid(); ++Supers)
  24. if (RC->contains(*Supers) && Reg == getSubReg(*Supers, SubIdx))
  25. return *Supers;
  26. return 0;
  27. }
  28. MCRegister MCRegisterInfo::getSubReg(MCRegister Reg, unsigned Idx) const {
  29. assert(Idx && Idx < getNumSubRegIndices() &&
  30. "This is not a subregister index");
  31. // Get a pointer to the corresponding SubRegIndices list. This list has the
  32. // name of each sub-register in the same order as MCSubRegIterator.
  33. const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
  34. for (MCSubRegIterator Subs(Reg, this); Subs.isValid(); ++Subs, ++SRI)
  35. if (*SRI == Idx)
  36. return *Subs;
  37. return 0;
  38. }
  39. unsigned MCRegisterInfo::getSubRegIndex(MCRegister Reg,
  40. MCRegister SubReg) const {
  41. assert(SubReg && SubReg < getNumRegs() && "This is not a register");
  42. // Get a pointer to the corresponding SubRegIndices list. This list has the
  43. // name of each sub-register in the same order as MCSubRegIterator.
  44. const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;
  45. for (MCSubRegIterator Subs(Reg, this); Subs.isValid(); ++Subs, ++SRI)
  46. if (*Subs == SubReg)
  47. return *SRI;
  48. return 0;
  49. }
  50. unsigned MCRegisterInfo::getSubRegIdxSize(unsigned Idx) const {
  51. assert(Idx && Idx < getNumSubRegIndices() &&
  52. "This is not a subregister index");
  53. return SubRegIdxRanges[Idx].Size;
  54. }
  55. unsigned MCRegisterInfo::getSubRegIdxOffset(unsigned Idx) const {
  56. assert(Idx && Idx < getNumSubRegIndices() &&
  57. "This is not a subregister index");
  58. return SubRegIdxRanges[Idx].Offset;
  59. }
  60. int MCRegisterInfo::getDwarfRegNum(MCRegister RegNum, bool isEH) const {
  61. const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
  62. unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
  63. if (!M)
  64. return -1;
  65. DwarfLLVMRegPair Key = { RegNum, 0 };
  66. const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
  67. if (I == M+Size || I->FromReg != RegNum)
  68. return -1;
  69. return I->ToReg;
  70. }
  71. std::optional<unsigned> MCRegisterInfo::getLLVMRegNum(unsigned RegNum,
  72. bool isEH) const {
  73. const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;
  74. unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;
  75. if (!M)
  76. return std::nullopt;
  77. DwarfLLVMRegPair Key = { RegNum, 0 };
  78. const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
  79. if (I != M + Size && I->FromReg == RegNum)
  80. return I->ToReg;
  81. return std::nullopt;
  82. }
  83. int MCRegisterInfo::getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const {
  84. // On ELF platforms, DWARF EH register numbers are the same as DWARF
  85. // other register numbers. On Darwin x86, they differ and so need to be
  86. // mapped. The .cfi_* directives accept integer literals as well as
  87. // register names and should generate exactly what the assembly code
  88. // asked for, so there might be DWARF/EH register numbers that don't have
  89. // a corresponding LLVM register number at all. So if we can't map the
  90. // EH register number to an LLVM register number, assume it's just a
  91. // valid DWARF register number as is.
  92. if (std::optional<unsigned> LRegNum = getLLVMRegNum(RegNum, true))
  93. return getDwarfRegNum(*LRegNum, false);
  94. return RegNum;
  95. }
  96. int MCRegisterInfo::getSEHRegNum(MCRegister RegNum) const {
  97. const DenseMap<MCRegister, int>::const_iterator I = L2SEHRegs.find(RegNum);
  98. if (I == L2SEHRegs.end()) return (int)RegNum;
  99. return I->second;
  100. }
  101. int MCRegisterInfo::getCodeViewRegNum(MCRegister RegNum) const {
  102. if (L2CVRegs.empty())
  103. report_fatal_error("target does not implement codeview register mapping");
  104. const DenseMap<MCRegister, int>::const_iterator I = L2CVRegs.find(RegNum);
  105. if (I == L2CVRegs.end())
  106. report_fatal_error("unknown codeview register " + (RegNum < getNumRegs()
  107. ? getName(RegNum)
  108. : Twine(RegNum)));
  109. return I->second;
  110. }
  111. bool MCRegisterInfo::regsOverlap(MCRegister RegA, MCRegister RegB) const {
  112. // Regunits are numerically ordered. Find a common unit.
  113. MCRegUnitIterator RUA(RegA, this);
  114. MCRegUnitIterator RUB(RegB, this);
  115. do {
  116. if (*RUA == *RUB)
  117. return true;
  118. } while (*RUA < *RUB ? (++RUA).isValid() : (++RUB).isValid());
  119. return false;
  120. }