[llvm] [RegAlloc] Consider rematerialization over CSR use (PR #206756)

Lukas Sommer via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 21 03:02:15 PDT 2026


================
@@ -2381,34 +2462,47 @@ BlockFrequency RAGreedy::calcSpillCost(const LiveInterval &LI) {
 MCRegister RAGreedy::tryAssignCSRFirstTime(
     const LiveInterval &VirtReg, AllocationOrder &Order, MCRegister PhysReg,
     uint8_t &CostPerUseLimit, SmallVectorImpl<Register> &NewVRegs) {
-  if (ExtraInfo->getStage(VirtReg) == RS_Spill && VirtReg.isSpillable()) {
-    // We choose spill over using the CSR for the first time if the spill cost
-    // is lower than CSRCost.
+  LiveRangeStage Stage = ExtraInfo->getStage(VirtReg);
+
+  if (Stage == RS_Spill && VirtReg.isSpillable()) {
+    // We choose spill or rematerialize over using the CSR for the first time if
+    // the spill cost is lower than CSRCost.
     SA->analyze(&VirtReg);
-    if (calcSpillCost(VirtReg) >= CSRCost)
+    if (calcSpillCost(VirtReg) >= CSRCost &&
+        !shouldAvoidCSRForRemat(VirtReg, Order)) {
----------------
sommerlukas wrote:

See the explanation in the comment above as to why we currently can't compare the cost directly with CSR cost.

After #202007, we could change `shouldAvoidSRForRemat` to `calcRematCost` and directly compare with`CSRCost`.

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


More information about the llvm-commits mailing list