[llvm] [RegAlloc] Consider rematerialization over CSR use (PR #206756)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 21 07:40:23 PDT 2026
================
@@ -2372,6 +2372,86 @@ BlockFrequency RAGreedy::calcSpillCost(const LiveInterval &LI) {
return BlockFrequency(SpillCost);
}
+bool RAGreedy::shouldAvoidCSRForRemat(const LiveInterval &VirtReg,
+ AllocationOrder &Order) const {
+ if (!VirtReg.isSpillable())
+ return false;
+
+ // This logic is intentionally narrow: handle a single concrete value
+ // whose def can be cheaply rematerialized at every use.
+ const VNInfo *OnlyVNI = nullptr;
+ for (const VNInfo *VNI : VirtReg.vnis()) {
+ if (!VNI || VNI->isUnused())
+ continue;
+ if (VNI->isPHIDef())
+ return false;
+ if (OnlyVNI)
+ return false;
+ OnlyVNI = VNI;
+ }
+ if (!OnlyVNI)
+ return false;
+
+ MachineInstr *DefMI = LIS->getInstructionFromIndex(OnlyVNI->def);
+ if (!DefMI || DefMI->isImplicitDef() || !TII->isReMaterializable(*DefMI) ||
+ !TII->isAsCheapAsAMove(*DefMI))
+ return false;
+
+ // Only consider live ranges with register-mask interference. This limits the
+ // heuristic to avoiding a first-use CSR required by a call rather than by
+ // register pressure.
+ if (!Matrix->checkRegMaskInterference(VirtReg))
+ return false;
+
+ SmallPtrSet<MachineInstr *, 8> VisitedUses;
+ unsigned NumUses = 0;
+ for (MachineOperand &MO : MRI->use_nodbg_operands(VirtReg.reg())) {
+ if (MO.isUndef())
+ continue;
+
+ MachineInstr *UseMI = MO.getParent();
+ if (!VisitedUses.insert(UseMI).second)
----------------
arsenm wrote:
user iterator instead of use iterator?
https://github.com/llvm/llvm-project/pull/206756
More information about the llvm-commits
mailing list