[llvm] [DwarfDebug] Slightly optimize computeKeyInstructions() (NFC) (PR #146357)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 30 07:27:11 PDT 2025


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/146357

Fetch the DILocation once, instead of many times. This is pretty trivial, but goes through out-of-line code.

It's a very minor improvement: https://llvm-compile-time-tracker.com/compare.php?from=652630b3c918c807fca8785eabdb92393b87043a&to=ee035faadfa6f68f6447ae7e0051d44965d1556c&stat=instructions:u

>From bbc126a6ae5df4e453c267878289e3214c01dcd3 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 30 Jun 2025 15:13:24 +0200
Subject: [PATCH] [DwarfDebug] Slightly optimize computeKeyInstructions() (NFC)

Fetch the DILocation once, instead of many times. This is pretty
trivial, but goes through out-of-line code.
---
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 25 ++++++++++------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 47c44efa8e73c..11b85763a3fb1 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -2383,6 +2383,8 @@ void DwarfDebug::computeKeyInstructions(const MachineFunction *MF) {
            std::pair<uint8_t, SmallVector<const MachineInstr *, 2>>>
       GroupCandidates;
 
+  const auto &TII = *MF->getSubtarget().getInstrInfo();
+
   // For each instruction:
   //   * Skip insts without DebugLoc, AtomGroup or AtomRank, and line zeros.
   //   * Check if insts in this group have been seen already in GroupCandidates.
@@ -2411,24 +2413,20 @@ void DwarfDebug::computeKeyInstructions(const MachineFunction *MF) {
       if (MI.isMetaInstruction())
         continue;
 
-      if (!MI.getDebugLoc() || !MI.getDebugLoc().getLine())
+      const DILocation *Loc = MI.getDebugLoc().get();
+      if (!Loc || !Loc->getLine())
         continue;
 
       // Reset the Buoy to this instruction if it has a different line number.
-      if (!Buoy ||
-          Buoy->getDebugLoc().getLine() != MI.getDebugLoc().getLine()) {
+      if (!Buoy || Buoy->getDebugLoc().getLine() != Loc->getLine()) {
         Buoy = &MI;
         BuoyAtom = 0; // Set later when we know which atom the buoy is used by.
       }
 
       // Call instructions are handled specially - we always mark them as key
       // regardless of atom info.
-      const auto &TII =
-          *MI.getParent()->getParent()->getSubtarget().getInstrInfo();
       bool IsCallLike = MI.isCall() || TII.isTailCall(MI);
       if (IsCallLike) {
-        assert(MI.getDebugLoc() && "Unexpectedly missing DL");
-
         // Calls are always key. Put the buoy (may not be the call) into
         // KeyInstructions directly rather than the candidate map to avoid it
         // being erased (and we may not have a group number for the call).
@@ -2438,14 +2436,13 @@ void DwarfDebug::computeKeyInstructions(const MachineFunction *MF) {
         Buoy = nullptr;
         BuoyAtom = 0;
 
-        if (!MI.getDebugLoc()->getAtomGroup() ||
-            !MI.getDebugLoc()->getAtomRank())
+        if (!Loc->getAtomGroup() || !Loc->getAtomRank())
           continue;
       }
 
-      auto *InlinedAt = MI.getDebugLoc()->getInlinedAt();
-      uint64_t Group = MI.getDebugLoc()->getAtomGroup();
-      uint8_t Rank = MI.getDebugLoc()->getAtomRank();
+      auto *InlinedAt = Loc->getInlinedAt();
+      uint64_t Group = Loc->getAtomGroup();
+      uint8_t Rank = Loc->getAtomRank();
       if (!Group || !Rank)
         continue;
 
@@ -2487,8 +2484,8 @@ void DwarfDebug::computeKeyInstructions(const MachineFunction *MF) {
         CandidateInsts.push_back(Buoy);
         CandidateRank = Rank;
 
-        assert(!BuoyAtom || BuoyAtom == MI.getDebugLoc()->getAtomGroup());
-        BuoyAtom = MI.getDebugLoc()->getAtomGroup();
+        assert(!BuoyAtom || BuoyAtom == Loc->getAtomGroup());
+        BuoyAtom = Loc->getAtomGroup();
       } else {
         // Don't add calls, because they've been dealt with already. This means
         // CandidateInsts might now be empty - handle that.



More information about the llvm-commits mailing list