[llvm] [RegAlloc] Change the computation of CSRCost (PR #177226)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 21 11:57:11 PST 2026


================
@@ -2339,6 +2345,31 @@ MCRegister RAGreedy::selectOrSplit(const LiveInterval &VirtReg,
   return Reg;
 }
 
+/// calcSpillCost - Compute how expensive it would be to spill the live range in
+/// LI into memory.
+BlockFrequency RAGreedy::calcSpillCost(const LiveInterval &LI) {
+  uint64_t SpillCost = 0;
+  SmallPtrSet<MachineInstr *, 8> Visited;
+
+  for (MachineRegisterInfo::reg_instr_nodbg_iterator
+           I = MRI->reg_instr_nodbg_begin(LI.reg()),
+           E = MRI->reg_instr_nodbg_end();
+       I != E;) {
+    MachineInstr *MI = &*(I++);
+    if (MI->isImplicitDef())
+      continue;
+    if (!Visited.insert(MI).second)
+      continue;
+
+    bool Reads, Writes;
+    std::tie(Reads, Writes) = MI->readsWritesVirtualRegister(LI.reg());
+    auto MBBFreq = SpillPlacer->getBlockFrequency(MI->getParent()->getNumber());
----------------
arsenm wrote:

Should this also weight by the register class? I have problems from subregister being weighted equal to the full register (this could also account for subranges, but that's harder)

https://github.com/llvm/llvm-project/pull/177226


More information about the llvm-commits mailing list