[llvm] 80fd9f3 - [CSKY] Fix error of underestimated function size by save/restore R15(LR) when we use BSR far jump.

Zi Xuan Wu via llvm-commits llvm-commits at lists.llvm.org
Tue May 31 20:22:56 PDT 2022


Author: Zi Xuan Wu (Zeson)
Date: 2022-06-01T11:05:19+08:00
New Revision: 80fd9f3e0a185fb285a9b597e020d60bb5dfb9a2

URL: https://github.com/llvm/llvm-project/commit/80fd9f3e0a185fb285a9b597e020d60bb5dfb9a2
DIFF: https://github.com/llvm/llvm-project/commit/80fd9f3e0a185fb285a9b597e020d60bb5dfb9a2.diff

LOG: [CSKY] Fix error of underestimated function size by save/restore R15(LR) when we use BSR far jump.

In CSKYConstantIslands, when fix up an unconditional branch(CSKY::BR32) whose destination is
too far away to fit in its displacement field, and if the R15(LR) register has been
spilled in the prologue, then we can use BSR to implement a far jump. So we need estimate function
size, and spill R15(LR) when the function size >= unconditional branch(CSKY::BR32) can reach.

EstimateFunctionSizeInBytes function adds up all instructions and constant pool entries(each entry is 4 bytes).

Added: 
    

Modified: 
    llvm/lib/Target/CSKY/CSKYFrameLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp b/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp
index 3bf001c2cee71..9907f39b3f902 100644
--- a/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp
+++ b/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp
@@ -13,6 +13,7 @@
 #include "CSKYFrameLowering.h"
 #include "CSKYMachineFunctionInfo.h"
 #include "CSKYSubtarget.h"
+#include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -270,6 +271,17 @@ void CSKYFrameLowering::emitEpilogue(MachineFunction &MF,
             MachineInstr::FrameDestroy);
 }
 
+static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF,
+                                            const CSKYInstrInfo &TII) {
+  unsigned FnSize = 0;
+  for (auto &MBB : MF) {
+    for (auto &MI : MBB)
+      FnSize += TII.getInstSizeInBytes(MI);
+  }
+  FnSize += MF.getConstantPool()->getConstants().size() * 4;
+  return FnSize;
+}
+
 static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
                                          const CSKYSubtarget &STI) {
   unsigned Limit = (1 << 12) - 1;
@@ -349,6 +361,7 @@ void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
 
   CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
+  const CSKYInstrInfo *TII = STI.getInstrInfo();
   const MachineRegisterInfo &MRI = MF.getRegInfo();
   MachineFrameInfo &MFI = MF.getFrameInfo();
 
@@ -411,8 +424,6 @@ void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
     }
   }
 
-  CFI->setLRIsSpilled(SavedRegs.test(CSKY::R15));
-
   unsigned CSStackSize = 0;
   for (unsigned Reg : SavedRegs.set_bits()) {
     auto RegSize = TRI->getRegSizeInBits(Reg, MRI) / 8;
@@ -432,6 +443,14 @@ void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
 
     RS->addScavengingFrameIndex(MFI.CreateStackObject(size, align, false));
   }
+
+  unsigned FnSize = EstimateFunctionSizeInBytes(MF, *TII);
+  // Force R15 to be spilled if the function size is > 65534. This enables
+  // use of BSR to implement far jump.
+  if (FnSize >= ((1 << (16 - 1)) * 2))
+    SavedRegs.set(CSKY::R15);
+
+  CFI->setLRIsSpilled(SavedRegs.test(CSKY::R15));
 }
 
 // Not preserve stack space within prologue for outgoing variables when the


        


More information about the llvm-commits mailing list