Skip to content

Commit 80fd9f3

Browse files
committed
[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).
1 parent 9441103 commit 80fd9f3

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

llvm/lib/Target/CSKY/CSKYFrameLowering.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "CSKYFrameLowering.h"
1414
#include "CSKYMachineFunctionInfo.h"
1515
#include "CSKYSubtarget.h"
16+
#include "llvm/CodeGen/MachineConstantPool.h"
1617
#include "llvm/CodeGen/MachineFrameInfo.h"
1718
#include "llvm/CodeGen/MachineFunction.h"
1819
#include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -270,6 +271,17 @@ void CSKYFrameLowering::emitEpilogue(MachineFunction &MF,
270271
MachineInstr::FrameDestroy);
271272
}
272273

274+
static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF,
275+
const CSKYInstrInfo &TII) {
276+
unsigned FnSize = 0;
277+
for (auto &MBB : MF) {
278+
for (auto &MI : MBB)
279+
FnSize += TII.getInstSizeInBytes(MI);
280+
}
281+
FnSize += MF.getConstantPool()->getConstants().size() * 4;
282+
return FnSize;
283+
}
284+
273285
static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
274286
const CSKYSubtarget &STI) {
275287
unsigned Limit = (1 << 12) - 1;
@@ -349,6 +361,7 @@ void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
349361

350362
CSKYMachineFunctionInfo *CFI = MF.getInfo<CSKYMachineFunctionInfo>();
351363
const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
364+
const CSKYInstrInfo *TII = STI.getInstrInfo();
352365
const MachineRegisterInfo &MRI = MF.getRegInfo();
353366
MachineFrameInfo &MFI = MF.getFrameInfo();
354367

@@ -411,8 +424,6 @@ void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
411424
}
412425
}
413426

414-
CFI->setLRIsSpilled(SavedRegs.test(CSKY::R15));
415-
416427
unsigned CSStackSize = 0;
417428
for (unsigned Reg : SavedRegs.set_bits()) {
418429
auto RegSize = TRI->getRegSizeInBits(Reg, MRI) / 8;
@@ -432,6 +443,14 @@ void CSKYFrameLowering::determineCalleeSaves(MachineFunction &MF,
432443

433444
RS->addScavengingFrameIndex(MFI.CreateStackObject(size, align, false));
434445
}
446+
447+
unsigned FnSize = EstimateFunctionSizeInBytes(MF, *TII);
448+
// Force R15 to be spilled if the function size is > 65534. This enables
449+
// use of BSR to implement far jump.
450+
if (FnSize >= ((1 << (16 - 1)) * 2))
451+
SavedRegs.set(CSKY::R15);
452+
453+
CFI->setLRIsSpilled(SavedRegs.test(CSKY::R15));
435454
}
436455

437456
// Not preserve stack space within prologue for outgoing variables when the

0 commit comments

Comments
 (0)