[llvm-branch-commits] [llvm] LiveVariables: Only visit tracked physical registers (PR #230151)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 8 14:55:23 PDT 2026


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/230151

>From 3f8986632985a107761f7b04450e12817ca6b778 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 8 Oct 2026 16:27:33 +0200
Subject: [PATCH] LiveVariables: Only visit tracked physical registers

Keep a bitvector of physical registers with a recorded def or use in
the current block. Register mask handling, the end of block scan, and
the per-block reset now only visit those registers instead of every
register. This is significant for targets with many registers, such as
AMDGPU.

Co-authored-by: Claude Opus 5.5 <noreply at anthropic.com>
---
 llvm/include/llvm/CodeGen/LiveVariables.h |  3 +++
 llvm/lib/CodeGen/LiveVariables.cpp        | 27 ++++++++++++++++-------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/LiveVariables.h b/llvm/include/llvm/CodeGen/LiveVariables.h
index 94a85e1c87bfd..9f2601c75e0cf 100644
--- a/llvm/include/llvm/CodeGen/LiveVariables.h
+++ b/llvm/include/llvm/CodeGen/LiveVariables.h
@@ -15,6 +15,7 @@
 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
 #define LLVM_CODEGEN_LIVEVARIABLES_H
 
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
@@ -46,6 +47,8 @@ class LiveVariables {
   // register references are presumed dead across basic blocks.
   std::vector<MachineInstr *> PhysRegUse;
 
+  BitVector TrackedRegs;
+
   // DistanceMap - Keep track the distance of a MI from the start of the
   // current basic block.
   DenseMap<MachineInstr*, unsigned> DistanceMap;
diff --git a/llvm/lib/CodeGen/LiveVariables.cpp b/llvm/lib/CodeGen/LiveVariables.cpp
index 8546ccad1dcb1..7ef2addc2e85e 100644
--- a/llvm/lib/CodeGen/LiveVariables.cpp
+++ b/llvm/lib/CodeGen/LiveVariables.cpp
@@ -99,8 +99,10 @@ void LiveVariables::HandlePhysRegUse(Register Reg, MachineInstr &MI) {
                                                   true/*IsImp*/));
 
   // Remember this use.
-  for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg))
+  for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg)) {
     PhysRegUse[SubReg] = &MI;
+    TrackedRegs.set(SubReg);
+  }
 }
 
 /// FindLastRefOrPartRef - Return the last reference or partial reference of
@@ -201,8 +203,10 @@ void LiveVariables::HandlePhysRegKill(Register Reg, MachineInstr *MI) {
         PhysRegDef[Reg.id()]->addOperand(
             MachineOperand::CreateReg(SubReg, true /*IsDef*/, true /*IsImp*/));
       if (!FindLastRefOrPartRef(SubReg)) {
-        for (MCPhysReg SS : TRI->subregs_inclusive(SubReg))
+        for (MCPhysReg SS : TRI->subregs_inclusive(SubReg)) {
           PhysRegUse[SS] = LastRefOrPartRef;
+          TrackedRegs.set(SS);
+        }
       }
       for (MCPhysReg SS : TRI->subregs(SubReg))
         PartUses.erase(SS);
@@ -229,7 +233,7 @@ void LiveVariables::HandleRegMask(const MachineOperand &MO, unsigned NumRegs) {
   // Call HandlePhysRegKill() for all live registers clobbered by Mask.
   // Clobbered registers are always dead, sp there is no need to use
   // HandlePhysRegDef().
-  for (unsigned Reg = 1; Reg != NumRegs; ++Reg) {
+  for (unsigned Reg : TrackedRegs.set_bits()) {
     // Skip dead regs.
     if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
       continue;
@@ -285,6 +289,7 @@ void LiveVariables::UpdatePhysRegDefs(MachineInstr &MI,
     for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg)) {
       PhysRegDef[SubReg] = &MI;
       PhysRegUse[SubReg] = nullptr;
+      TrackedRegs.set(SubReg);
     }
   }
 }
@@ -358,9 +363,9 @@ void LiveVariables::runOnBlock(MachineBasicBlock *MBB, unsigned NumRegs) {
 
   // Loop over PhysRegDef / PhysRegUse, killing any registers that are
   // available at the end of the basic block.
-  for (unsigned i = 0; i != NumRegs; ++i)
-    if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i))
-      HandlePhysRegDef(i, nullptr);
+  for (unsigned Reg : TrackedRegs.set_bits())
+    if ((PhysRegDef[Reg] || PhysRegUse[Reg]) && !LiveOuts.count(Reg))
+      HandlePhysRegDef(Reg, nullptr);
 }
 
 void LiveVariables::analyze(MachineFunction &mf) {
@@ -376,12 +381,17 @@ void LiveVariables::analyze(MachineFunction &mf) {
   const unsigned NumRegs = TRI->getNumSupportedRegs(mf);
   PhysRegDef.assign(NumRegs, nullptr);
   PhysRegUse.assign(NumRegs, nullptr);
+  TrackedRegs.clear();
+  TrackedRegs.resize(NumRegs);
 
   for (MachineBasicBlock &MBB : mf) {
     runOnBlock(&MBB, NumRegs);
 
-    PhysRegDef.assign(NumRegs, nullptr);
-    PhysRegUse.assign(NumRegs, nullptr);
+    for (unsigned Reg : TrackedRegs.set_bits()) {
+      PhysRegDef[Reg] = nullptr;
+      PhysRegUse[Reg] = nullptr;
+    }
+    TrackedRegs.reset();
   }
 
   for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
@@ -394,4 +404,5 @@ void LiveVariables::analyze(MachineFunction &mf) {
 
   PhysRegDef.clear();
   PhysRegUse.clear();
+  TrackedRegs.clear();
 }



More information about the llvm-branch-commits mailing list