diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h --- a/llvm/include/llvm/CodeGen/MachineFunction.h +++ b/llvm/include/llvm/CodeGen/MachineFunction.h @@ -743,6 +743,10 @@ /// True if this function needs frame moves for debug or exceptions. bool needsFrameMoves() const; + /// Estimate the total size of instructions, constants and jump table entries + /// in bytes. This may overestimate the function size. + unsigned estimateSizeInBytes() const; + /// Get the function properties const MachineFunctionProperties &getProperties() const { return Properties; } MachineFunctionProperties &getProperties() { return Properties; } diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -614,6 +614,25 @@ F.needsUnwindTableEntry(); } +/// Estimate the total size of instructions, constants and jump table entries +/// in bytes. This may overestimate the function size. +unsigned MachineFunction::estimateSizeInBytes() const { + unsigned FnSize = 0; + const TargetInstrInfo *TII = getSubtarget().getInstrInfo(); + const DataLayout &DL = getDataLayout(); + for (auto &MBB : *this) + for (auto &MI : MBB) + FnSize += TII->getInstSizeInBytes(MI); + for (auto &C : getConstantPool()->getConstants()) + FnSize += std::max(C.getSizeInBytes(DL), (unsigned)C.getAlign().value()); + const MachineJumpTableInfo *JTI = getJumpTableInfo(); + if (JTI) + for (auto &Table : JTI->getJumpTables()) + FnSize += Table.MBBs.size() * + std::max(JTI->getEntrySize(DL), JTI->getEntryAlignment(DL)); + return FnSize; +} + namespace llvm { template<> diff --git a/llvm/lib/Target/ARM/ARMFrameLowering.cpp b/llvm/lib/Target/ARM/ARMFrameLowering.cpp --- a/llvm/lib/Target/ARM/ARMFrameLowering.cpp +++ b/llvm/lib/Target/ARM/ARMFrameLowering.cpp @@ -2042,21 +2042,6 @@ return true; } -// FIXME: Make generic? -static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF, - const ARMBaseInstrInfo &TII) { - unsigned FnSize = 0; - for (auto &MBB : MF) { - for (auto &MI : MBB) - FnSize += TII.getInstSizeInBytes(MI); - } - if (MF.getJumpTableInfo()) - for (auto &Table: MF.getJumpTableInfo()->getJumpTables()) - FnSize += Table.MBBs.size() * 4; - FnSize += MF.getConstantPool()->getConstants().size() * 4; - return FnSize; -} - /// estimateRSStackSizeLimit - Look at each instruction that references stack /// frames and return the stack size limit beyond which some of these /// instructions will require a scratch register during their expansion later. @@ -2251,8 +2236,6 @@ SmallVector UnspilledCS2GPRs; const ARMBaseRegisterInfo *RegInfo = static_cast( MF.getSubtarget().getRegisterInfo()); - const ARMBaseInstrInfo &TII = - *static_cast(MF.getSubtarget().getInstrInfo()); ARMFunctionInfo *AFI = MF.getInfo(); MachineFrameInfo &MFI = MF.getFrameInfo(); MachineRegisterInfo &MRI = MF.getRegInfo(); @@ -2377,7 +2360,7 @@ bool ForceLRSpill = false; if (!LRSpilled && AFI->isThumb1OnlyFunction()) { - unsigned FnSize = EstimateFunctionSizeInBytes(MF, TII); + unsigned FnSize = MF.estimateSizeInBytes(); // Force LR to be spilled if the Thumb function size is > 2048. This enables // use of BL to implement far jump. if (FnSize >= (1 << 11)) { diff --git a/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp b/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp --- a/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp +++ b/llvm/lib/Target/CSKY/CSKYFrameLowering.cpp @@ -271,17 +271,6 @@ 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; @@ -361,7 +350,6 @@ CSKYMachineFunctionInfo *CFI = MF.getInfo(); const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); - const CSKYInstrInfo *TII = STI.getInstrInfo(); const MachineRegisterInfo &MRI = MF.getRegInfo(); MachineFrameInfo &MFI = MF.getFrameInfo(); @@ -444,7 +432,7 @@ RS->addScavengingFrameIndex(MFI.CreateStackObject(size, align, false)); } - unsigned FnSize = EstimateFunctionSizeInBytes(MF, *TII); + unsigned FnSize = MF.estimateSizeInBytes(); // 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))