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

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 20:54:49 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());
----------------
weiguozhi wrote:

Ideally we should consider register class. Usually spilling a subregister has the same cost as spilling the full register, very occasionally a register class composed physical register pairs needs more cost. But here we don't need it to be 100% precise, so I prefer it to be simple and fast compile time. The function VirtRegAuxInfo::weightCalcHelper() also uses the same method.

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


More information about the llvm-commits mailing list