Path: blob/main/contrib/llvm-project/llvm/lib/MC/MCRegisterInfo.cpp
96334 views
//===- MC/MCRegisterInfo.cpp - Target Register Description ----------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements MCRegisterInfo functions.9//10//===----------------------------------------------------------------------===//1112#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>1920using namespace llvm;2122namespace {23/// MCRegAliasIterator enumerates all registers aliasing Reg. This iterator24/// does not guarantee any ordering or that entries are unique.25class MCRegAliasIteratorImpl {26private:27MCRegister Reg;28const MCRegisterInfo *MCRI;2930MCRegUnitIterator RI;31MCRegUnitRootIterator RRI;32MCSuperRegIterator SI;3334public:35MCRegAliasIteratorImpl(MCRegister Reg, const MCRegisterInfo *MCRI)36: Reg(Reg), MCRI(MCRI) {3738// Initialize the iterators.39for (RI = MCRegUnitIterator(Reg, MCRI); RI.isValid(); ++RI) {40for (RRI = MCRegUnitRootIterator(*RI, MCRI); RRI.isValid(); ++RRI) {41for (SI = MCSuperRegIterator(*RRI, MCRI, true); SI.isValid(); ++SI) {42if (Reg != *SI)43return;44}45}46}47}4849bool isValid() const { return RI.isValid(); }5051MCRegister operator*() const {52assert(SI.isValid() && "Cannot dereference an invalid iterator.");53return *SI;54}5556void advance() {57// Assuming SI is valid.58++SI;59if (SI.isValid())60return;6162++RRI;63if (RRI.isValid()) {64SI = MCSuperRegIterator(*RRI, MCRI, true);65return;66}6768++RI;69if (RI.isValid()) {70RRI = MCRegUnitRootIterator(*RI, MCRI);71SI = MCSuperRegIterator(*RRI, MCRI, true);72}73}7475MCRegAliasIteratorImpl &operator++() {76assert(isValid() && "Cannot move off the end of the list.");77do78advance();79while (isValid() && *SI == Reg);80return *this;81}82};83} // namespace8485ArrayRef<MCPhysReg> MCRegisterInfo::getCachedAliasesOf(MCPhysReg R) const {86auto &Aliases = RegAliasesCache[R];87if (!Aliases.empty())88return Aliases;8990for (MCRegAliasIteratorImpl It(R, this); It.isValid(); ++It)91Aliases.push_back(*It);9293sort(Aliases);94Aliases.erase(unique(Aliases), Aliases.end());95assert(none_of(Aliases, [&](auto &Cur) { return R == Cur; }) &&96"MCRegAliasIteratorImpl includes Self!");9798// Always put "self" at the end, so the iterator can choose to ignore it.99// For registers without aliases, it also serves as a sentinel value that100// tells us to not recompute the alias set.101Aliases.push_back(R);102Aliases.shrink_to_fit();103return Aliases;104}105106MCRegister107MCRegisterInfo::getMatchingSuperReg(MCRegister Reg, unsigned SubIdx,108const MCRegisterClass *RC) const {109for (MCPhysReg Super : superregs(Reg))110if (RC->contains(Super) && Reg == getSubReg(Super, SubIdx))111return Super;112return 0;113}114115MCRegister MCRegisterInfo::getSubReg(MCRegister Reg, unsigned Idx) const {116assert(Idx && Idx < getNumSubRegIndices() &&117"This is not a subregister index");118// Get a pointer to the corresponding SubRegIndices list. This list has the119// name of each sub-register in the same order as MCSubRegIterator.120const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;121for (MCPhysReg Sub : subregs(Reg)) {122if (*SRI == Idx)123return Sub;124++SRI;125}126return 0;127}128129unsigned MCRegisterInfo::getSubRegIndex(MCRegister Reg,130MCRegister SubReg) const {131assert(SubReg && SubReg < getNumRegs() && "This is not a register");132// Get a pointer to the corresponding SubRegIndices list. This list has the133// name of each sub-register in the same order as MCSubRegIterator.134const uint16_t *SRI = SubRegIndices + get(Reg).SubRegIndices;135for (MCPhysReg Sub : subregs(Reg)) {136if (Sub == SubReg)137return *SRI;138++SRI;139}140return 0;141}142143int MCRegisterInfo::getDwarfRegNum(MCRegister RegNum, bool isEH) const {144const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;145unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;146147if (!M)148return -1;149DwarfLLVMRegPair Key = { RegNum, 0 };150const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);151if (I == M+Size || I->FromReg != RegNum)152return -1;153return I->ToReg;154}155156std::optional<unsigned> MCRegisterInfo::getLLVMRegNum(unsigned RegNum,157bool isEH) const {158const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;159unsigned Size = isEH ? EHDwarf2LRegsSize : Dwarf2LRegsSize;160161if (!M)162return std::nullopt;163DwarfLLVMRegPair Key = { RegNum, 0 };164const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);165if (I != M + Size && I->FromReg == RegNum)166return I->ToReg;167return std::nullopt;168}169170int MCRegisterInfo::getDwarfRegNumFromDwarfEHRegNum(unsigned RegNum) const {171// On ELF platforms, DWARF EH register numbers are the same as DWARF172// other register numbers. On Darwin x86, they differ and so need to be173// mapped. The .cfi_* directives accept integer literals as well as174// register names and should generate exactly what the assembly code175// asked for, so there might be DWARF/EH register numbers that don't have176// a corresponding LLVM register number at all. So if we can't map the177// EH register number to an LLVM register number, assume it's just a178// valid DWARF register number as is.179if (std::optional<unsigned> LRegNum = getLLVMRegNum(RegNum, true)) {180int DwarfRegNum = getDwarfRegNum(*LRegNum, false);181if (DwarfRegNum == -1)182return RegNum;183else184return DwarfRegNum;185}186return RegNum;187}188189int MCRegisterInfo::getSEHRegNum(MCRegister RegNum) const {190const DenseMap<MCRegister, int>::const_iterator I = L2SEHRegs.find(RegNum);191if (I == L2SEHRegs.end()) return (int)RegNum;192return I->second;193}194195int MCRegisterInfo::getCodeViewRegNum(MCRegister RegNum) const {196if (L2CVRegs.empty())197report_fatal_error("target does not implement codeview register mapping");198const DenseMap<MCRegister, int>::const_iterator I = L2CVRegs.find(RegNum);199if (I == L2CVRegs.end())200report_fatal_error("unknown codeview register " + (RegNum < getNumRegs()201? getName(RegNum)202: Twine(RegNum)));203return I->second;204}205206bool MCRegisterInfo::regsOverlap(MCRegister RegA, MCRegister RegB) const {207// Regunits are numerically ordered. Find a common unit.208auto RangeA = regunits(RegA);209MCRegUnitIterator IA = RangeA.begin(), EA = RangeA.end();210auto RangeB = regunits(RegB);211MCRegUnitIterator IB = RangeB.begin(), EB = RangeB.end();212do {213if (*IA == *IB)214return true;215} while (*IA < *IB ? ++IA != EA : ++IB != EB);216return false;217}218219220