[llvm] 3eb0f76 - LiveVariables: Only visit tracked physical registers (#230151)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 9 00:53:44 PDT 2026


Author: Matt Arsenault
Date: 2026-10-09T09:53:36+02:00
New Revision: 3eb0f76c614a6df0afed3b34ca26fd8968823855

URL: https://github.com/llvm/llvm-project/commit/3eb0f76c614a6df0afed3b34ca26fd8968823855
DIFF: https://github.com/llvm/llvm-project/commit/3eb0f76c614a6df0afed3b34ca26fd8968823855.diff

LOG: LiveVariables: Only visit tracked physical registers (#230151)

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>

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/LiveVariables.h
    llvm/lib/CodeGen/LiveVariables.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/LiveVariables.h b/llvm/include/llvm/CodeGen/LiveVariables.h
index 94a85e1c87bfd..5f89dc2993da7 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,10 @@ class LiveVariables {
   // register references are presumed dead across basic blocks.
   std::vector<MachineInstr *> PhysRegUse;
 
+  /// Track physical registers referenced in the current block, used as a
+  /// compile-time guard.
+  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-commits mailing list