From: Zi Xuan Wu (Zeson) Date: Wed, 1 Jun 2022 02:42:33 +0000 (+0800) Subject: [CSKY] Fix error of underestimated function size by save/restore R15(LR) when we... X-Git-Tag: upstream/15.0.7~6252 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=80fd9f3e0a185fb285a9b597e020d60bb5dfb9a2;p=platform%2Fupstream%2Fllvm.git [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). --- diff --git a/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp b/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp index 3bf001c..9907f39 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(); 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