[llvm-branch-commits] [llvm] [RISCV] Update the getCSRCost from 5 to 32768 (PR #239534)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 8 20:58:19 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: Piyou Chen (BeMg)

<details>
<summary>Changes</summary>

This is patch 4 of 4. Together, they adjust `tryAssignCSRFirstTime` and the `getCSRCost` value to improve prologue/epilogue codegen in benchmarks.

- https://github.com/llvm/llvm-project/pull/239526
- https://github.com/llvm/llvm-project/pull/239530
- https://github.com/llvm/llvm-project/pull/239532
- https://github.com/llvm/llvm-project/pull/239534

-----

This patch update the getCSRCost value in RISC-V target. It choose 32768 because the (1 << 14) * 2.

-----

After increasing CSRCost from 5 to 32768 and giving the greedy register allocator more choices for unused CSRs, SPEC shows an reduction (Geomean -4.63%) in dynamic instruction count.

  | ICount Redcue %
-- | --
706.stockfish_r | 0.07%
707.ntest_r | 1.92%
708.sqlite_r | -2.71%
710.omnetpp_r | -4.42%
714.cpython_r | -9.71%
721.gcc_r | -6.89%
723.llvm_r | -5.75%
727.cppcheck_r | -23.18%
729.abc_r | -2.26%
734.vpr_r | -2.93%
735.gem5_r | -3.99%
750.sealcrypto_r | -4.38%
753.ns3_r | -2.61%
777.zstd_r | -0.14%
Geomean | -4.63%

> Note: 707.ntest_r generate the extra spill and could be root cause of regression.

---

Patch is 1.29 MiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/239534.diff


90 Files Affected:

- (modified) llvm/include/llvm/CodeGen/TargetRegisterInfo.h (+8) 
- (modified) llvm/lib/CodeGen/RegAllocGreedy.cpp (+178-2) 
- (modified) llvm/lib/CodeGen/RegAllocGreedy.h (+15) 
- (modified) llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp (+5) 
- (modified) llvm/lib/Target/RISCV/RISCVRegisterInfo.h (+4-3) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/double-convert.ll (+60-75) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/fastcc-float.ll (+33-43) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/float-convert.ll (+146-177) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/fp128.ll (+496-460) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/rotl-rotr.ll (+33-18) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/rv64-double-convert.ll (+26-30) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/rv64-float-convert.ll (+26-30) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/rv64zbb.ll (+11-15) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/shifts.ll (+92-87) 
- (modified) llvm/test/CodeGen/RISCV/GlobalISel/wide-scalar-shift-by-byte-multiple-legalization.ll (+300-300) 
- (modified) llvm/test/CodeGen/RISCV/atomicrmw-cond-sub-clamp.ll (+4-4) 
- (modified) llvm/test/CodeGen/RISCV/atomicrmw-uinc-udec-wrap.ll (+4-4) 
- (modified) llvm/test/CodeGen/RISCV/bfloat-convert.ll (+30-27) 
- (modified) llvm/test/CodeGen/RISCV/bitint-fp-conv-200.ll (+1170-1030) 
- (modified) llvm/test/CodeGen/RISCV/calling-conv-half.ll (+10-18) 
- (modified) llvm/test/CodeGen/RISCV/clmulh.ll (+51-51) 
- (modified) llvm/test/CodeGen/RISCV/clmulr.ll (+224-222) 
- (modified) llvm/test/CodeGen/RISCV/condops.ll (+40-45) 
- (modified) llvm/test/CodeGen/RISCV/csr-first-use-cost.ll (+9-9) 
- (modified) llvm/test/CodeGen/RISCV/ctlz-cttz-ctpop.ll (+55-58) 
- (modified) llvm/test/CodeGen/RISCV/ctz_zero_return_test.ll (+42-44) 
- (modified) llvm/test/CodeGen/RISCV/double-convert.ll (+127-140) 
- (modified) llvm/test/CodeGen/RISCV/double-round-conv-sat.ll (+73-66) 
- (modified) llvm/test/CodeGen/RISCV/double-stack-spill-restore.ll (+12-17) 
- (modified) llvm/test/CodeGen/RISCV/exception-pointer-register.ll (+18-38) 
- (modified) llvm/test/CodeGen/RISCV/float-convert.ll (+144-165) 
- (modified) llvm/test/CodeGen/RISCV/float-round-conv-sat.ll (+165-153) 
- (modified) llvm/test/CodeGen/RISCV/fp128.ll (+182-161) 
- (modified) llvm/test/CodeGen/RISCV/fpclamptosat.ll (+2529-2377) 
- (modified) llvm/test/CodeGen/RISCV/fpenv.ll (+20-22) 
- (modified) llvm/test/CodeGen/RISCV/half-convert.ll (+274-289) 
- (modified) llvm/test/CodeGen/RISCV/half-round-conv-sat.ll (+240-216) 
- (modified) llvm/test/CodeGen/RISCV/idiv_large.ll (+804-803) 
- (modified) llvm/test/CodeGen/RISCV/interrupt-attr-nocall.ll (+44-56) 
- (modified) llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll (+10-6) 
- (modified) llvm/test/CodeGen/RISCV/machine-sink-load-immediate.ll (+64-64) 
- (modified) llvm/test/CodeGen/RISCV/miss-sp-restore-eh.ll (+3-6) 
- (modified) llvm/test/CodeGen/RISCV/overflow-intrinsics.ll (+50-87) 
- (modified) llvm/test/CodeGen/RISCV/qci-interrupt-attr-fpr.ll (+144-136) 
- (modified) llvm/test/CodeGen/RISCV/qci-interrupt-attr.ll (+20-20) 
- (added) llvm/test/CodeGen/RISCV/regalloc-csr-cost-patterns.ll (+1060) 
- (modified) llvm/test/CodeGen/RISCV/rv32xtheadbb.ll (+34-36) 
- (modified) llvm/test/CodeGen/RISCV/rv32zbb.ll (+34-36) 
- (modified) llvm/test/CodeGen/RISCV/rv64-double-convert.ll (+47-46) 
- (modified) llvm/test/CodeGen/RISCV/rv64-float-convert.ll (+47-46) 
- (modified) llvm/test/CodeGen/RISCV/rv64-half-convert.ll (+47-46) 
- (modified) llvm/test/CodeGen/RISCV/rv64-statepoint-call-lowering.ll (+2-2) 
- (modified) llvm/test/CodeGen/RISCV/rvv/clmulh-sdnode.ll (+84-84) 
- (modified) llvm/test/CodeGen/RISCV/rvv/fixed-vectors-fp-buildvec.ll (+107-219) 
- (modified) llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-scatter.ll (+659-507) 
- (modified) llvm/test/CodeGen/RISCV/rvv/fixed-vectors-shuffle-rotate.ll (+32-48) 
- (modified) llvm/test/CodeGen/RISCV/rvv/fpclamptosat_vec.ll (+91-84) 
- (modified) llvm/test/CodeGen/RISCV/rvv/interrupt-attr-nocall.ll (+2-4) 
- (modified) llvm/test/CodeGen/RISCV/rvv/localvar.ll (+24-32) 
- (modified) llvm/test/CodeGen/RISCV/rvv/nontemporal-vp-scalable.ll (+670-670) 
- (modified) llvm/test/CodeGen/RISCV/rvv/pr95865.ll (+51-55) 
- (modified) llvm/test/CodeGen/RISCV/rvv/shrinkwrap.ll (+28-32) 
- (modified) llvm/test/CodeGen/RISCV/rvv/vsetvli-insert-crossbb.ll (+33-32) 
- (modified) llvm/test/CodeGen/RISCV/rvv/vxrm-insert-out-of-loop.ll (+30-30) 
- (modified) llvm/test/CodeGen/RISCV/select-cc.ll (+31-35) 
- (modified) llvm/test/CodeGen/RISCV/sext-zext-trunc.ll (+12-20) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-load-atomic-acquire-seq_cst.ll (+34-50) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-load.ll (+17-25) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_32_eq.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_32_ne.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_32_sge.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_32_slt.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_32_uge.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_32_ult.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_48_eq.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_48_ne.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_48_sge.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_48_slt.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_48_uge.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/short-forward-branch-opt-with-branch-with-immediates_48_ult.ll (+4-8) 
- (modified) llvm/test/CodeGen/RISCV/shrinkwrap-save-restore-debug.ll (+16-12) 
- (modified) llvm/test/CodeGen/RISCV/shrinkwrap.ll (+68-82) 
- (modified) llvm/test/CodeGen/RISCV/simplify-condbr.ll (+4-6) 
- (modified) llvm/test/CodeGen/RISCV/smulh.ll (+213-211) 
- (modified) llvm/test/CodeGen/RISCV/umulh.ll (+94-88) 
- (modified) llvm/test/CodeGen/RISCV/xqcibm-cto-clo-brev.ll (+58-58) 
- (modified) llvm/test/CodeGen/RISCV/xqcilo-xqcilia-frame-index.ll (+43-53) 
- (modified) llvm/test/CodeGen/RISCV/zicond-fp-select-zfinx.ll (+4-8) 
- (modified) llvm/test/DebugInfo/RISCV/dw_op_entry_value_32bit.ll (+1-1) 
- (modified) llvm/test/DebugInfo/RISCV/dw_op_entry_value_64bit.ll (+1-1) 


``````````diff
diff --git a/llvm/include/llvm/CodeGen/TargetRegisterInfo.h b/llvm/include/llvm/CodeGen/TargetRegisterInfo.h
index fc3b4d6dda844b..3888f141b5370c 100644
--- a/llvm/include/llvm/CodeGen/TargetRegisterInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetRegisterInfo.h
@@ -899,6 +899,14 @@ class LLVM_ABI TargetRegisterInfo : public MCRegisterInfo {
     return 30;
   }
 
+  /// Return true if the greedy register allocator should take
+  /// the lowest-cost choice before using an unused CSR.
+  /// This uses tryAssignCSRFirstTimeMinCost instead of
+  /// tryAssignCSRFirstTime.
+  virtual bool enableCSRFirstTimeMinCost(const MachineFunction &MF) const {
+    return false;
+  }
+
   /// Returns true if the target requires (and can make use of) the register
   /// scavenger.
   virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
diff --git a/llvm/lib/CodeGen/RegAllocGreedy.cpp b/llvm/lib/CodeGen/RegAllocGreedy.cpp
index f7655e97b99427..acce796d5ff651 100644
--- a/llvm/lib/CodeGen/RegAllocGreedy.cpp
+++ b/llvm/lib/CodeGen/RegAllocGreedy.cpp
@@ -2471,6 +2471,49 @@ BlockFrequency RAGreedy::calcRematCost(const LiveInterval &VirtReg,
   return HasUse ? RematCost : BlockFrequency::max();
 }
 
+/// Return true if the spiller can rematerialize each use of \p LI.
+/// And it can rematerialize for only one def.
+static bool isRematerializableFromOriginal(const LiveInterval &LI,
+                                           const LiveIntervals &LIS,
+                                           const VirtRegMap &VRM,
+                                           const MachineRegisterInfo &MRI,
+                                           const TargetInstrInfo &TII) {
+  const LiveInterval &OrigLI = LIS.getInterval(VRM.getOriginal(LI.reg()));
+  const MachineInstr *Def = nullptr;
+  for (const VNInfo *VNI : OrigLI.valnos) {
+    if (VNI->isUnused())
+      continue;
+    // Maybe more than one value def for VirtReg
+    if (VNI->isPHIDef() || Def)
+      return false;
+    Def = LIS.getInstructionFromIndex(VNI->def);
+    if (!Def || !TII.isReMaterializable(*Def))
+      return false;
+  }
+  if (!Def)
+    return false;
+  for (const MachineInstr &MI : MRI.use_nodbg_instructions(LI.reg()))
+    if (!VirtRegAuxInfo::allUsesAvailableAt(Def, LIS.getInstructionIndex(MI),
+                                            LIS, MRI, TII))
+      return false;
+  return true;
+}
+
+/// The cost of rematerializing \p LI before each of its uses.
+static BlockFrequency calcRematCostAtUses(const LiveInterval &LI,
+                                          const MachineRegisterInfo &MRI,
+                                          const SpillPlacement &SpillPlacer) {
+  uint64_t RematCost = 0;
+  SmallPtrSet<const MachineInstr *, 8> Seen;
+  for (const MachineInstr &MI : MRI.use_nodbg_instructions(LI.reg())) {
+    if (MI.isMetaInstruction() || !Seen.insert(&MI).second)
+      continue;
+    RematCost += SpillPlacer.getBlockFrequency(MI.getParent()->getNumber())
+                     .getFrequency();
+  }
+  return BlockFrequency(RematCost);
+}
+
 /// Using a CSR for the first time has a cost because it causes push|pop
 /// to be added to prologue|epilogue. Splitting a cold section of the live
 /// range can have lower cost than using the CSR for the first time;
@@ -2524,6 +2567,135 @@ MCRegister RAGreedy::tryAssignCSRFirstTime(
   return PhysReg;
 }
 
+// Like tryAssignCSRFirstTime before assign the unused CSR to VirtReg, try
+// other choices. There are four results:
+//   1. Pre-split VirtReg
+//   2. Remat/Spill VirtReg
+//   3. Evict rematerializable live ranges if that reg is not unused CSR.
+//   4. Use the CSR for this VirtReg
+// The goal is using the less cost register.
+MCRegister RAGreedy::tryAssignCSRFirstTimeMinCost(
+    const LiveInterval &VirtReg, AllocationOrder &Order, MCRegister PhysReg,
+    uint8_t &CostPerUseLimit, SmallVectorImpl<Register> &NewVRegs) {
+  // Compute Remat cost.
+  std::optional<BlockFrequency> RematCost;
+  if (VirtReg.isSpillable() &&
+      isRematerializableFromOriginal(VirtReg, *LIS, *VRM, *MRI, *TII))
+    RematCost = calcRematCostAtUses(VirtReg, *MRI, *SpillPlacer);
+
+  std::optional<BlockFrequency> SpillRematCost;
+  if (VirtReg.isSpillable()) {
+    SpillRematCost = calcSpillCost(VirtReg);
+    if (RematCost)
+      SpillRematCost = std::min(*SpillRematCost, *RematCost);
+  }
+
+  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.
+    if (*SpillRematCost >= CSRCost)
+      return PhysReg;
+
+    // We are going to spill or rematerialize, set CostPerUseLimit to 1 to make
+    // sure that we will not use a callee-saved register in tryEvict.
+    CostPerUseLimit = 1;
+    return MCRegister();
+  }
+
+  if (Stage < RS_Split) {
+    SA->analyze(&VirtReg);
+    // A pre-split must cost less than CSRCost.
+    // CSRCost is the cost for place the CSR save/restore. And it be set by
+    // target.
+    BlockFrequency SplitCost = CSRCost;
+    unsigned NumCands = 0;
+    unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, SplitCost,
+                                                 NumCands, true /*IgnoreCSR*/);
+
+    bool SpillRematCostsLess = SpillRematCost && *SpillRematCost < SplitCost;
+    if (BestCand != NoCand && !SpillRematCostsLess) {
+      doRegionSplit(VirtReg, BestCand, false /*HasCompact*/, NewVRegs);
+      return MCRegister();
+    }
+  }
+
+  bool SpillCostsLess = SpillRematCost && *SpillRematCost < CSRCost;
+  BlockFrequency EvictLimit = SpillCostsLess ? *SpillRematCost : CSRCost;
+  // Use Evict Reg
+  if (MCRegister Reg =
+          tryEvictRematForCSR(VirtReg, Order, NewVRegs, EvictLimit))
+    return Reg;
+
+  // Use CSR
+  if (!SpillCostsLess)
+    return PhysReg;
+
+  // Use Spill/Remat
+  // We are going to spill, set CostPerUseLimit to 1 to make sure that
+  // we will not use a callee-saved register in tryEvict.
+  ExtraInfo->setStage(VirtReg, RS_Spill);
+  CostPerUseLimit = 1;
+  return MCRegister();
+}
+
+/// Return the cost of evicting and rematerializing all the live ranges that
+/// interfere with \p VirtReg in \p PhysReg.
+/// Return std::nullopt when
+///  1. has lower Cascade than \p Cascade.
+///  2. can't be spilled.
+///  3. can't be rematerialized.
+std::optional<BlockFrequency>
+RAGreedy::calcEvictRematCost(const LiveInterval &VirtReg, MCRegister PhysReg,
+                             unsigned Cascade) const {
+  BlockFrequency Cost(0);
+  SmallPtrSet<const LiveInterval *, 4> Seen;
+  for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
+    for (const LiveInterval *Intf :
+         Matrix->query(VirtReg, Unit).interferingVRegs()) {
+      if (!Seen.insert(Intf).second)
+        continue;
+      if (ExtraInfo->getCascade(Intf->reg()) >= Cascade ||
+          !Intf->isSpillable() ||
+          !isRematerializableFromOriginal(*Intf, *LIS, *VRM, *MRI, *TII))
+        return std::nullopt;
+      Cost += calcRematCostAtUses(*Intf, *MRI, *SpillPlacer);
+    }
+  }
+  return Cost;
+}
+
+/// Before \p VirtReg uses a CSR for the first time, look for a register that
+/// is not an unused CSR and that only rematerializable live ranges occupy.
+/// If rematerializing them costs less than \p MaxCost, evict them and
+/// return the register.
+MCRegister RAGreedy::tryEvictRematForCSR(const LiveInterval &VirtReg,
+                                         AllocationOrder &Order,
+                                         SmallVectorImpl<Register> &NewVRegs,
+                                         BlockFrequency MaxCost) {
+  unsigned Cascade = ExtraInfo->getCascadeOrCurrentNext(VirtReg.reg());
+  MCRegister BestReg;
+  BlockFrequency BestCost = MaxCost;
+  for (MCRegister PhysReg : Order.getOrder()) {
+    if (EvictAdvisor->getUnusedCalleeSavedReg(VirtReg, PhysReg) ||
+        Matrix->checkInterference(VirtReg, PhysReg) !=
+            LiveRegMatrix::IK_VirtReg)
+      continue;
+    std::optional<BlockFrequency> Cost =
+        calcEvictRematCost(VirtReg, PhysReg, Cascade);
+    if (Cost && *Cost < BestCost) {
+      BestCost = *Cost;
+      BestReg = PhysReg;
+    }
+  }
+  if (!BestReg)
+    return MCRegister();
+
+  evictInterference(VirtReg, BestReg, NewVRegs);
+  return BestReg;
+}
+
 void RAGreedy::aboutToRemoveInterval(const LiveInterval &LI) {
   // Do not keep invalid information around.
   SetOfBrokenHints.remove(&LI);
@@ -2791,8 +2963,12 @@ MCRegister RAGreedy::selectOrSplitImpl(const LiveInterval &VirtReg,
     if (CSRCost.getFrequency() &&
         EvictAdvisor->getUnusedCalleeSavedReg(VirtReg, PhysReg) &&
         NewVRegs.empty()) {
-      MCRegister CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg,
-                                                CostPerUseLimit, NewVRegs);
+      MCRegister CSRReg =
+          TRI->enableCSRFirstTimeMinCost(*MF)
+              ? tryAssignCSRFirstTimeMinCost(VirtReg, Order, PhysReg,
+                                             CostPerUseLimit, NewVRegs)
+              : tryAssignCSRFirstTime(VirtReg, Order, PhysReg, CostPerUseLimit,
+                                      NewVRegs);
       if (CSRReg || !NewVRegs.empty())
         // Return now if we decide to use a CSR or create new vregs due to
         // pre-splitting.
diff --git a/llvm/lib/CodeGen/RegAllocGreedy.h b/llvm/lib/CodeGen/RegAllocGreedy.h
index 138da362a1e76f..43353439423206 100644
--- a/llvm/lib/CodeGen/RegAllocGreedy.h
+++ b/llvm/lib/CodeGen/RegAllocGreedy.h
@@ -365,6 +365,21 @@ class LLVM_LIBRARY_VISIBILITY RAGreedy : public RegAllocBase,
   /// rematerialization is not possible.
   BlockFrequency calcRematCost(const LiveInterval &VirtReg,
                                AllocationOrder &Order) const;
+  /// Like tryAssignCSRFirstTime, but take the choice with the lowest cost of:
+  /// a pre-split, spilling or rematerializing VirtReg, evicting
+  /// rematerializable live ranges, and the first use of the CSR.
+  MCRegister tryAssignCSRFirstTimeMinCost(const LiveInterval &VirtReg,
+                                          AllocationOrder &Order,
+                                          MCRegister PhysReg,
+                                          uint8_t &CostPerUseLimit,
+                                          SmallVectorImpl<Register> &NewVRegs);
+  std::optional<BlockFrequency> calcEvictRematCost(const LiveInterval &VirtReg,
+                                                   MCRegister PhysReg,
+                                                   unsigned Cascade) const;
+  MCRegister tryEvictRematForCSR(const LiveInterval &VirtReg,
+                                 AllocationOrder &Order,
+                                 SmallVectorImpl<Register> &NewVRegs,
+                                 BlockFrequency MaxCost);
   BlockFrequency calcSpillCost(const LiveInterval &LI);
   void initializeCSRCost();
   MCRegister tryBlockSplit(const LiveInterval &, AllocationOrder &,
diff --git a/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp b/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
index 00c89367fc3ecb..eb3e597f15f5df 100644
--- a/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
@@ -66,6 +66,11 @@ RISCVRegisterInfo::getIPRACSRegs(const MachineFunction *MF) const {
   return CSR_IPRA_SaveList;
 }
 
+bool RISCVRegisterInfo::enableCSRFirstTimeMinCost(
+    const MachineFunction &MF) const {
+  return true;
+}
+
 const MCPhysReg *
 RISCVRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
   auto &Subtarget = MF->getSubtarget<RISCVSubtarget>();
diff --git a/llvm/lib/Target/RISCV/RISCVRegisterInfo.h b/llvm/lib/Target/RISCV/RISCVRegisterInfo.h
index 5d5441c75f5a41..56cc3871399011 100644
--- a/llvm/lib/Target/RISCV/RISCVRegisterInfo.h
+++ b/llvm/lib/Target/RISCV/RISCVRegisterInfo.h
@@ -73,11 +73,12 @@ struct RISCVRegisterInfo : public RISCVGenRegisterInfo {
 
   unsigned getCSRCost() const override {
     // The cost will be compared against BlockFrequency where entry has the
-    // value of 1 << 14. A value of 5 will choose to spill or split cold
-    // path instead of using a callee-saved register.
-    return 5;
+    // value of 1 << 14. 32768 mean (1 << 14) * 2 instruction.
+    return 32768;
   }
 
+  bool enableCSRFirstTimeMinCost(const MachineFunction &MF) const override;
+
   const MCPhysReg *getCalleeSavedRegs(const MachineFunction *MF) const override;
 
   const TargetRegisterClass *
diff --git a/llvm/test/CodeGen/RISCV/GlobalISel/double-convert.ll b/llvm/test/CodeGen/RISCV/GlobalISel/double-convert.ll
index f781a97f8dab54..96fda144dabfa2 100644
--- a/llvm/test/CodeGen/RISCV/GlobalISel/double-convert.ll
+++ b/llvm/test/CodeGen/RISCV/GlobalISel/double-convert.ll
@@ -1159,71 +1159,62 @@ define i64 @fcvt_l_d_sat(double %a) nounwind {
 ;
 ; RV32I-LABEL: fcvt_l_d_sat:
 ; RV32I:       # %bb.0: # %start
-; RV32I-NEXT:    addi sp, sp, -48
-; RV32I-NEXT:    sw ra, 44(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s0, 40(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s1, 36(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s2, 32(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s3, 28(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s4, 24(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s5, 20(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s6, 16(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s7, 12(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    addi sp, sp, -32
+; RV32I-NEXT:    sw ra, 28(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw s0, 24(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw s1, 20(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw s2, 16(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw s3, 12(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw s4, 8(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    mv s1, a0
 ; RV32I-NEXT:    mv s2, a1
 ; RV32I-NEXT:    call __fixdfdi
-; RV32I-NEXT:    mv s3, a0
-; RV32I-NEXT:    mv s4, a1
+; RV32I-NEXT:    sw a0, 4(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw a1, 0(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    li s0, 0
 ; RV32I-NEXT:    lui a3, 802304
 ; RV32I-NEXT:    mv a0, s1
 ; RV32I-NEXT:    mv a1, s2
 ; RV32I-NEXT:    li a2, 0
 ; RV32I-NEXT:    call __gedf2
-; RV32I-NEXT:    lui s6, 524288
-; RV32I-NEXT:    li s7, 0
-; RV32I-NEXT:    lui s5, 524288
+; RV32I-NEXT:    li s4, 0
+; RV32I-NEXT:    lui s3, 524288
 ; RV32I-NEXT:    bltz a0, .LBB27_2
 ; RV32I-NEXT:  # %bb.1: # %start
-; RV32I-NEXT:    mv s7, s3
-; RV32I-NEXT:    mv s5, s4
+; RV32I-NEXT:    lw s4, 4(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw s3, 0(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:  .LBB27_2: # %start
 ; RV32I-NEXT:    lui a3, 278016
-; RV32I-NEXT:    li s3, -1
 ; RV32I-NEXT:    addi a3, a3, -1
 ; RV32I-NEXT:    li a2, -1
 ; RV32I-NEXT:    mv a0, s1
 ; RV32I-NEXT:    mv a1, s2
 ; RV32I-NEXT:    call __gtdf2
-; RV32I-NEXT:    blt s0, a0, .LBB27_4
-; RV32I-NEXT:  # %bb.3: # %start
-; RV32I-NEXT:    mv s3, s7
-; RV32I-NEXT:    j .LBB27_5
-; RV32I-NEXT:  .LBB27_4:
-; RV32I-NEXT:    addi s5, s6, -1
-; RV32I-NEXT:  .LBB27_5: # %start
+; RV32I-NEXT:    bge s0, a0, .LBB27_4
+; RV32I-NEXT:  # %bb.3:
+; RV32I-NEXT:    lui s3, 524288
+; RV32I-NEXT:    addi s3, s3, -1
+; RV32I-NEXT:    li s4, -1
+; RV32I-NEXT:  .LBB27_4: # %start
 ; RV32I-NEXT:    mv a0, s1
 ; RV32I-NEXT:    mv a1, s2
 ; RV32I-NEXT:    mv a2, s1
 ; RV32I-NEXT:    mv a3, s2
 ; RV32I-NEXT:    call __unorddf2
 ; RV32I-NEXT:    mv a1, s0
-; RV32I-NEXT:    bne a0, s0, .LBB27_7
-; RV32I-NEXT:  # %bb.6: # %start
-; RV32I-NEXT:    mv s0, s3
-; RV32I-NEXT:    mv a1, s5
-; RV32I-NEXT:  .LBB27_7: # %start
+; RV32I-NEXT:    bne a0, s0, .LBB27_6
+; RV32I-NEXT:  # %bb.5: # %start
+; RV32I-NEXT:    mv s0, s4
+; RV32I-NEXT:    mv a1, s3
+; RV32I-NEXT:  .LBB27_6: # %start
 ; RV32I-NEXT:    mv a0, s0
-; RV32I-NEXT:    lw ra, 44(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s0, 40(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s1, 36(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s2, 32(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s3, 28(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s4, 24(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s5, 20(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s6, 16(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s7, 12(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    addi sp, sp, 48
+; RV32I-NEXT:    lw ra, 28(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw s0, 24(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw s1, 20(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw s2, 16(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw s3, 12(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw s4, 8(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    addi sp, sp, 32
 ; RV32I-NEXT:    ret
 ;
 ; RV64I-LABEL: fcvt_l_d_sat:
@@ -1232,7 +1223,6 @@ define i64 @fcvt_l_d_sat(double %a) nounwind {
 ; RV64I-NEXT:    sd ra, 24(sp) # 8-byte Folded Spill
 ; RV64I-NEXT:    sd s0, 16(sp) # 8-byte Folded Spill
 ; RV64I-NEXT:    sd s1, 8(sp) # 8-byte Folded Spill
-; RV64I-NEXT:    sd s2, 0(sp) # 8-byte Folded Spill
 ; RV64I-NEXT:    mv s1, a0
 ; RV64I-NEXT:    call __fixdfdi
 ; RV64I-NEXT:    mv s0, a0
@@ -1241,10 +1231,10 @@ define i64 @fcvt_l_d_sat(double %a) nounwind {
 ; RV64I-NEXT:    mv a0, s1
 ; RV64I-NEXT:    call __gedf2
 ; RV64I-NEXT:    sext.w a0, a0
-; RV64I-NEXT:    li s2, -1
 ; RV64I-NEXT:    bgez a0, .LBB27_2
 ; RV64I-NEXT:  # %bb.1:
-; RV64I-NEXT:    slli s0, s2, 63
+; RV64I-NEXT:    li s0, -1
+; RV64I-NEXT:    slli s0, s0, 63
 ; RV64I-NEXT:  .LBB27_2: # %start
 ; RV64I-NEXT:    li a0, 543
 ; RV64I-NEXT:    slli a0, a0, 53
@@ -1254,7 +1244,8 @@ define i64 @fcvt_l_d_sat(double %a) nounwind {
 ; RV64I-NEXT:    sext.w a0, a0
 ; RV64I-NEXT:    blez a0, .LBB27_4
 ; RV64I-NEXT:  # %bb.3:
-; RV64I-NEXT:    srli s0, s2, 1
+; RV64I-NEXT:    li s0, -1
+; RV64I-NEXT:    srli s0, s0, 1
 ; RV64I-NEXT:  .LBB27_4: # %start
 ; RV64I-NEXT:    mv a0, s1
 ; RV64I-NEXT:    mv a1, s1
@@ -1268,7 +1259,6 @@ define i64 @fcvt_l_d_sat(double %a) nounwind {
 ; RV64I-NEXT:    ld ra, 24(sp) # 8-byte Folded Reload
 ; RV64I-NEXT:    ld s0, 16(sp) # 8-byte Folded Reload
 ; RV64I-NEXT:    ld s1, 8(sp) # 8-byte Folded Reload
-; RV64I-NEXT:    ld s2, 0(sp) # 8-byte Folded Reload
 ; RV64I-NEXT:    addi sp, sp, 32
 ; RV64I-NEXT:    ret
 start:
@@ -1335,26 +1325,25 @@ define i64 @fcvt_lu_d_sat(double %a) nounwind {
 ; RV32I-NEXT:    sw s0, 24(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    sw s1, 20(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    sw s2, 16(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s3, 12(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s4, 8(sp) # 4-byte Folded Spill
-; RV32I-NEXT:    sw s5, 4(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    mv s0, a0
 ; RV32I-NEXT:    mv s1, a1
 ; RV32I-NEXT:    call __fixunsdfdi
 ; RV32I-NEXT:    mv s2, a0
-; RV32I-NEXT:    mv s3, a1
+; RV32I-NEXT:    sw a1, 12(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    mv a0, s0
 ; RV32I-NEXT:    mv a1, s1
 ; RV32I-NEXT:    li a2, 0
 ; RV32I-NEXT:    li a3, 0
 ; RV32I-NEXT:    call __gedf2
-; RV32I-NEXT:    li s4, 0
-; RV32I-NEXT:    li s5, 0
+; RV32I-NEXT:    li a1, 0
+; RV32I-NEXT:    li a2, 0
 ; RV32I-NEXT:    bltz a0, .LBB28_2
 ; RV32I-NEXT:  # %bb.1: # %start
-; RV32I-NEXT:    mv s4, s2
-; RV32I-NEXT:    mv s5, s3
+; RV32I-NEXT:    mv a1, s2
+; RV32I-NEXT:    lw a2, 12(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:  .LBB28_2: # %start
+; RV32I-NEXT:    sw a1, 8(sp) # 4-byte Folded Spill
+; RV32I-NEXT:    sw a2, 12(sp) # 4-byte Folded Spill
 ; RV32I-NEXT:    lui a3, 278272
 ; RV32I-NEXT:    li s2, -1
 ; RV32I-NEXT:    addi a3, a3, -1
@@ -1365,17 +1354,14 @@ define i64 @fcvt_lu_d_sat(double %a) nounwind {
 ; RV32I-NEXT:    li a1, -1
 ; RV32I-NEXT:    bgtz a0, .LBB28_4
 ; RV32I-NEXT:  # %bb.3: # %start
-; RV32I-NEXT:    mv s2, s4
-; RV32I-NEXT:    mv a1, s5
+; RV32I-NEXT:    lw s2, 8(sp) # 4-byte Folded Reload
+; RV32I-NEXT:    lw a1, 12(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:  .LBB28_4: # %start
 ; RV32I-NEXT:    mv a0, s2
 ; RV32I-NEXT:    lw ra, 28(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:    lw s0, 24(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:    lw s1, 20(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:    lw s2, 16(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s3, 12(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s4, 8(sp) # 4-byte Folded Reload
-; RV32I-NEXT:    lw s5, 4(sp) # 4-byte Folded Reload
 ; RV32I-NEXT:    addi sp, sp, 32
 ; RV32I-NEXT:    ret
 ;
@@ -1384,35 +1370,34 @@ define i64 @fcvt_lu_d...
[truncated]

``````````

</details>


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


More information about the llvm-branch-commits mailing list