[llvm] [Codegen] [DebugInfo] use the getDwarfRegNum() defined in MCRegisterInfo.h instead of defining a new one (PR #97157)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 29 01:54:53 PDT 2024


https://github.com/phyBrackets created https://github.com/llvm/llvm-project/pull/97157

Fixing #96129 

>From 02b1e00ac108af58292c2862cc774236726a0782 Mon Sep 17 00:00:00 2001
From: Shivam Kunwar <shivam.kunwar at kdab.com>
Date: Sat, 29 Jun 2024 14:21:03 +0530
Subject: [PATCH] use the getDwarfRegNum() defined in MCRegisterInfo.h instead
 of defining a new one StackMaps.cpp

---
 llvm/include/llvm/MC/MCRegisterInfo.h | 13 +++++---
 llvm/lib/CodeGen/StackMaps.cpp        | 23 ++++---------
 llvm/lib/MC/MCRegisterInfo.cpp        | 47 +++++++++++++++++++++++----
 3 files changed, 55 insertions(+), 28 deletions(-)

diff --git a/llvm/include/llvm/MC/MCRegisterInfo.h b/llvm/include/llvm/MC/MCRegisterInfo.h
index 11205a5a44c86..bd1fafd423901 100644
--- a/llvm/include/llvm/MC/MCRegisterInfo.h
+++ b/llvm/include/llvm/MC/MCRegisterInfo.h
@@ -190,6 +190,10 @@ class MCRegisterInfo {
   mutable std::vector<std::vector<MCPhysReg>> RegAliasesCache;
   ArrayRef<MCPhysReg> getCachedAliasesOf(MCPhysReg R) const;
 
+  /// Look up a DWARF register number in the mapping table. 
+  /// Returns the DWARF register number if found, or -1 if not found.
+  int getDwarfRegNumFromTable(MCRegister RegNum, const DwarfLLVMRegPair *M, unsigned Size) const;
+
   /// Iterator class that can traverse the differentially encoded values in
   /// DiffLists. Don't use this class directly, use one of the adaptors below.
   class DiffListIterator
@@ -414,10 +418,11 @@ class MCRegisterInfo {
     return NumRegUnits;
   }
 
-  /// Map a target register to an equivalent dwarf register
-  /// number.  Returns -1 if there is no equivalent value.  The second
-  /// parameter allows targets to use different numberings for EH info and
-  /// debugging info.
+  /// Map a target register to an equivalent DWARF register number.
+  /// This function searches for a DWARF register number corresponding to the given
+  /// MCRegister. It checks the register itself, its super-registers, and its
+  /// sub-registers in that order.
+  /// Returns the DWARF register number if found, or -1 if there is no equivalent value.
   int getDwarfRegNum(MCRegister RegNum, bool isEH) const;
 
   /// Map a dwarf register back to a target register. Returns std::nullopt is
diff --git a/llvm/lib/CodeGen/StackMaps.cpp b/llvm/lib/CodeGen/StackMaps.cpp
index 90aa93e442cf3..673b470d8d77e 100644
--- a/llvm/lib/CodeGen/StackMaps.cpp
+++ b/llvm/lib/CodeGen/StackMaps.cpp
@@ -191,19 +191,6 @@ unsigned StackMaps::getNextMetaArgIdx(const MachineInstr *MI, unsigned CurIdx) {
   return CurIdx;
 }
 
-/// Go up the super-register chain until we hit a valid dwarf register number.
-static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
-  int RegNum;
-  for (MCPhysReg SR : TRI->superregs_inclusive(Reg)) {
-    RegNum = TRI->getDwarfRegNum(SR, false);
-    if (RegNum >= 0)
-      break;
-  }
-
-  assert(RegNum >= 0 && isUInt<16>(RegNum) && "Invalid Dwarf register number.");
-  return (unsigned)RegNum;
-}
-
 MachineInstr::const_mop_iterator
 StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
                         MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
@@ -221,8 +208,9 @@ StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
       Size /= 8;
       Register Reg = (++MOI)->getReg();
       int64_t Imm = (++MOI)->getImm();
+      unsigned DwarfRegNum = static_cast<unsigned>(TRI->getDwarfRegNum(Reg, false));  // false for non-EH context
       Locs.emplace_back(StackMaps::Location::Direct, Size,
-                        getDwarfRegNum(Reg, TRI), Imm);
+                        DwarfRegNum, Imm);
       break;
     }
     case StackMaps::IndirectMemRefOp: {
@@ -230,8 +218,9 @@ StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
       assert(Size > 0 && "Need a valid size for indirect memory locations.");
       Register Reg = (++MOI)->getReg();
       int64_t Imm = (++MOI)->getImm();
+      unsigned DwarfRegNum = static_cast<unsigned>(TRI->getDwarfRegNum(Reg, false));  // false for non-EH context
       Locs.emplace_back(StackMaps::Location::Indirect, Size,
-                        getDwarfRegNum(Reg, TRI), Imm);
+                       DwarfRegNum, Imm);
       break;
     }
     case StackMaps::ConstantOp: {
@@ -281,7 +270,7 @@ StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
     assert(!MOI->getSubReg() && "Physical subreg still around.");
 
     unsigned Offset = 0;
-    unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
+    unsigned DwarfRegNum = static_cast<unsigned>(TRI->getDwarfRegNum(MOI->getReg(), false));  // false for non-EH context
     unsigned LLVMRegNum = *TRI->getLLVMRegNum(DwarfRegNum, false);
     unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
     if (SubRegIdx)
@@ -372,7 +361,7 @@ void StackMaps::print(raw_ostream &OS) {
 /// Create a live-out register record for the given register Reg.
 StackMaps::LiveOutReg
 StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
-  unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
+  unsigned DwarfRegNum = static_cast<unsigned>(TRI->getDwarfRegNum(Reg, false));  // false for non-EH context
   unsigned Size = TRI->getSpillSize(*TRI->getMinimalPhysRegClass(Reg));
   return LiveOutReg(Reg, DwarfRegNum, Size);
 }
diff --git a/llvm/lib/MC/MCRegisterInfo.cpp b/llvm/lib/MC/MCRegisterInfo.cpp
index fde770a9c376c..262141480e515 100644
--- a/llvm/lib/MC/MCRegisterInfo.cpp
+++ b/llvm/lib/MC/MCRegisterInfo.cpp
@@ -141,19 +141,52 @@ unsigned MCRegisterInfo::getSubRegIndex(MCRegister Reg,
   return 0;
 }
 
-int MCRegisterInfo::getDwarfRegNum(MCRegister RegNum, bool isEH) const {
-  const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
-  unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
-
+int MCRegisterInfo::getDwarfRegNumFromTable(MCRegister RegNum,
+                                            const DwarfLLVMRegPair *M,
+                                            unsigned Size) const {
   if (!M)
     return -1;
-  DwarfLLVMRegPair Key = { RegNum, 0 };
-  const DwarfLLVMRegPair *I = std::lower_bound(M, M+Size, Key);
-  if (I == M+Size || I->FromReg != RegNum)
+  DwarfLLVMRegPair Key = {RegNum, 0};
+  const DwarfLLVMRegPair *I = std::lower_bound(M, M + Size, Key);
+  if (I == M + Size || I->FromReg != RegNum)
     return -1;
   return I->ToReg;
 }
 
+int MCRegisterInfo::getDwarfRegNum(MCRegister RegNum, bool isEH) const {
+  const DwarfLLVMRegPair *M = isEH ? EHL2DwarfRegs : L2DwarfRegs;
+  unsigned Size = isEH ? EHL2DwarfRegsSize : L2DwarfRegsSize;
+
+  // First, check the register itself
+  int RegDwarfNum = getDwarfRegNumFromTable(RegNum, M, Size);
+  if (RegDwarfNum >= 0)
+    return RegDwarfNum;
+
+  // If not found, check super-registers
+  for (MCRegister SR : superregs_inclusive(RegNum)) {
+    if (SR == RegNum)
+      continue; // Skip the register itself
+    RegDwarfNum = getDwarfRegNumFromTable(SR, M, Size);
+    if (RegDwarfNum >= 0)
+      break;
+  }
+
+  // If still not found, check sub-registers
+  if (RegDwarfNum < 0) {
+    for (MCRegister SubR : subregs_inclusive(RegNum)) {
+      if (SubR == RegNum)
+        continue; // Skip the register itself
+      RegDwarfNum = getDwarfRegNumFromTable(SubR, M, Size);
+      if (RegDwarfNum >= 0)
+        break;
+    }
+  }
+
+  // Assert if no valid DWARF number found or if it's not a 16-bit unsigned integer
+  assert(RegDwarfNum >= 0 && isUInt<16>(RegDwarfNum) && "Invalid Dwarf register number");
+  return RegDwarfNum;
+}
+
 std::optional<unsigned> MCRegisterInfo::getLLVMRegNum(unsigned RegNum,
                                                       bool isEH) const {
   const DwarfLLVMRegPair *M = isEH ? EHDwarf2LRegs : Dwarf2LRegs;



More information about the llvm-commits mailing list