27#define DEBUG_TYPE "csky-frame-lowering"
52void CSKYFrameLowering::determineFrameLayout(
MachineFunction &MF)
const {
61 if (RI->hasStackRealignment(MF)) {
63 FrameSize += (MaxStackAlign.
value() - StackAlign.
value());
64 StackAlign = MaxStackAlign;
72 FrameSize =
alignTo(FrameSize, StackAlign);
100 determineFrameLayout(MF);
104 uint64_t StackSize = MFI.getStackSize();
107 if (StackSize == 0 && !MFI.adjustsStack())
110 const auto &CSI = MFI.getCalleeSavedInfo();
117 adjustReg(
MBB,
MBBI,
DL, SPReg, SPReg, -(
static_cast<int64_t
>(ActualSize)),
136 for (
const auto &Entry : CSI) {
137 int64_t
Offset = MFI.getObjectOffset(Entry.getFrameIdx());
140 unsigned Num =
TRI->getRegSizeInBits(Reg,
MRI) / 32;
141 for (
unsigned i = 0; i < Num; i++) {
143 nullptr, RI->getDwarfRegNum(Reg,
true) + i,
Offset + i * 4));
157 nullptr, RI->getDwarfRegNum(FPReg,
true)));
163 -(
static_cast<int64_t
>(StackSize - ActualSize)),
168 if (RI->hasStackRealignment(MF)) {
169 Align MaxAlignment = MFI.getMaxAlign();
172 if (STI.
hasE2() && isUInt<12>(~(-(
int)MaxAlignment.
value()))) {
177 unsigned ShiftAmount =
Log2(MaxAlignment);
213 -(
static_cast<int64_t
>(StackSize - ActualSize)),
243 if (!
MBBI->isTerminator())
247 const auto &CSI = MFI.getCalleeSavedInfo();
248 uint64_t StackSize = MFI.getStackSize();
256 auto LastFrameDestroy =
MBBI;
258 LastFrameDestroy = std::prev(
MBBI, CSI.
size());
262 BuildMI(
MBB, LastFrameDestroy,
DL,
TII->get(TargetOpcode::COPY), SPReg)
266 adjustReg(
MBB, LastFrameDestroy,
DL, SPReg, SPReg, (StackSize - ActualSize),
270 adjustReg(
MBB,
MBBI,
DL, SPReg, SPReg, ActualSize,
277 for (
auto &
MBB : MF) {
279 FnSize +=
TII.getInstSizeInBytes(
MI);
287 unsigned Limit = (1 << 12) - 1;
289 for (
auto &
MBB : MF) {
290 for (
auto &
MI :
MBB) {
291 if (
MI.isDebugInstr())
294 for (
unsigned i = 0, e =
MI.getNumOperands(); i != e; ++i) {
295 if (!
MI.getOperand(i).isFI())
298 if (
MI.getOpcode() == CSKY::SPILL_CARRY ||
299 MI.getOpcode() == CSKY::RESTORE_CARRY ||
300 MI.getOpcode() == CSKY::STORE_PAIR ||
301 MI.getOpcode() == CSKY::LOAD_PAIR) {
302 Limit = std::min(Limit, ((1U << 12) - 1) * 4);
306 if (
MI.getOpcode() == CSKY::ADDI32) {
307 Limit = std::min(Limit, (1U << 12));
311 if (
MI.getOpcode() == CSKY::ADDI16XZ) {
312 Limit = std::min(Limit, (1U << 3));
318 if (
MI.getOpcode() == CSKY::ADDI16)
326 "Unhandled addressing mode in stack size limit calculation");
328 Limit = std::min(Limit, (1U << 12) - 1);
331 Limit = std::min(Limit, ((1U << 12) - 1) * 2);
334 Limit = std::min(Limit, ((1U << 12) - 1) * 4);
337 Limit = std::min(Limit, (1U << 5) - 1);
340 Limit = std::min(Limit, ((1U << 5) - 1) * 2);
343 Limit = std::min(Limit, ((1U << 5) - 1) * 4);
346 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
369 SavedRegs.
set(CSKY::R8);
373 SavedRegs.
set(CSKY::R7);
380 static const MCPhysReg CSRegs[] = {CSKY::R0, CSKY::R1, CSKY::R2, CSKY::R3,
381 CSKY::R12, CSKY::R13, 0};
383 for (
unsigned i = 0; CSRegs[i]; ++i)
384 SavedRegs.
set(CSRegs[i]);
388 static const MCPhysReg CSHRegs[] = {CSKY::R18, CSKY::R19, CSKY::R20,
389 CSKY::R21, CSKY::R22, CSKY::R23,
390 CSKY::R24, CSKY::R25, 0};
392 for (
unsigned i = 0; CSHRegs[i]; ++i)
393 SavedRegs.
set(CSHRegs[i]);
397 CSKY::F8_32, CSKY::F9_32, CSKY::F10_32,
398 CSKY::F11_32, CSKY::F12_32, CSKY::F13_32,
399 CSKY::F14_32, CSKY::F15_32, 0};
401 CSKY::F8_64, CSKY::F9_64, CSKY::F10_64,
402 CSKY::F11_64, CSKY::F12_64, CSKY::F13_64,
403 CSKY::F14_64, CSKY::F15_64, 0};
414 for (
unsigned i = 0; Regs[i]; ++i)
415 if (CSKY::FPR32RegClass.
contains(Regs[i]) ||
416 CSKY::FPR64RegClass.contains(Regs[i])) {
418 for (; FRegs[x]; ++x)
419 if (FRegs[x] == Regs[i])
422 SavedRegs.
set(Regs[i]);
427 unsigned CSStackSize = 0;
428 for (
unsigned Reg : SavedRegs.
set_bits()) {
441 unsigned size =
TRI->getSpillSize(*RC);
442 Align align =
TRI->getSpillAlign(*RC);
450 if (FnSize >= ((1 << (16 - 1)) * 2))
451 SavedRegs.
set(CSKY::R15);
473 DL =
MI->getDebugLoc();
475 for (
auto &CS : CSI) {
496 DL =
MI->getDebugLoc();
498 for (
auto &CS :
reverse(CSI)) {
503 assert(
MI !=
MBB.
begin() &&
"loadRegFromStackSlot didn't insert any code!");
522 int64_t Amount =
MI->getOperand(0).getImm();
528 if (
MI->getOpcode() == CSKY::ADJCALLSTACKDOWN)
545 if (DestReg == SrcReg && Val == 0)
549 if (STI.
hasE2() && isUInt<12>(std::abs(Val) - 1)) {
555 }
else if (!STI.
hasE2() && isShiftedUInt<7, 2>(std::abs(Val))) {
557 TII->get(Val < 0 ? CSKY::SUBI16SPSP : CSKY::ADDI16SPSP), CSKY::R14)
566 Op = Val < 0 ? CSKY::SUBU32 : CSKY::ADDU32;
568 assert(SrcReg == DestReg);
569 Op = Val < 0 ? CSKY::SUBU16XZ : CSKY::ADDU16XZ;
587 const auto &CSI = MFI.getCalleeSavedInfo();
592 int Offset = MFI.getObjectOffset(FI) + MFI.getOffsetAdjustment();
595 MinCSFI = CSI[0].getFrameIdx();
596 MaxCSFI = CSI[CSI.size() - 1].getFrameIdx();
599 if (FI >= MinCSFI && FI <= MaxCSFI) {
600 FrameReg = CSKY::R14;
604 if (!MFI.isFixedObjectIndex(FI)) {
606 Offset += MFI.getStackSize();
612 if (MFI.isFixedObjectIndex(FI) &&
hasFP(MF)) {
617 Offset += MFI.getStackSize();
unsigned const MachineRegisterInfo * MRI
static unsigned estimateRSStackSizeLimit(MachineFunction &MF)
Look at each instruction that references stack frames and return the stack size limit beyond which so...
static unsigned EstimateFunctionSizeInBytes(const MachineFunction &MF, const ARMBaseInstrInfo &TII)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static Register getBPReg(const CSKYSubtarget &STI)
static Register getFPReg(const CSKYSubtarget &STI)
const HexagonInstrInfo * TII
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
unsigned const TargetRegisterInfo * TRI
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
empty - Check if the array is empty.
bool test(unsigned Idx) const
iterator_range< const_set_bits_iterator > set_bits() const
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
bool hasReservedCallFrame(const MachineFunction &MF) const override
hasReservedCallFrame - Under normal circumstances, when a frame pointer is not required,...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
bool hasBP(const MachineFunction &MF) const
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register.
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
void setCalleeSaveAreaSize(int v)
int getCalleeSaveAreaSize() const
void setLRIsSpilled(bool s)
unsigned getVarArgsSaveSize() const
bool hasHighRegisters() const
bool hasFPUv2SingleFloat() const
bool hasFPUv3SingleFloat() const
const CSKYRegisterInfo * getRegisterInfo() const override
bool hasFPUv2DoubleFloat() const
bool hasFPUv3DoubleFloat() const
const CSKYInstrInfo * getInstrInfo() const override
This class represents an Operation in the Expression.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg) const override
Store the specified register of the given register class to the specified stack frame index.
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, const TargetRegisterInfo *TRI, Register VReg) const override
Load the specified register of the given register class from the specified stack frame index.
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int64_t Offset, SMLoc Loc={})
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
static MCCFIInstruction cfiDefCfaOffset(MCSymbol *L, int64_t Offset, SMLoc Loc={})
.cfi_def_cfa_offset modifies a rule for computing CFA.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const std::vector< MachineConstantPoolEntry > & getConstants() const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
void setMaxCallFrameSize(uint64_t S)
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
bool hasCalls() const
Return true if the current function has any function calls.
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
Align getMaxAlign() const
Return the alignment in bytes that this function must be aligned to, which is greater than the defaul...
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
uint64_t estimateStackSize(const MachineFunction &MF) const
Estimate and return the size of the stack frame.
void setStackSize(uint64_t Size)
Set the size of the stack.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
MachineConstantPool * getConstantPool()
getConstantPool - Return the constant pool object for the current function.
const MachineInstrBuilder & addCFIIndex(unsigned CFIIndex) const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
void addScavengingFrameIndex(int FI)
Add a scavenging frame index.
Wrapper class representing virtual and physical registers.
StackOffset holds a fixed and a scalable offset in bytes.
int64_t getFixed() const
Returns the fixed component of the stack.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
int alignSPAdjust(int SPAdj) const
alignSPAdjust - This method aligns the stack adjustment to the correct alignment.
TargetInstrInfo - Interface to description of machine instruction set.
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool hasStackRealignment(const MachineFunction &MF) const
True if stack realignment is required and still possible.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Kill
The last use of a register.
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto reverse(ContainerTy &&C)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
unsigned Log2(Align A)
Returns the log2 of the alignment.
This struct is a compact representation of a valid (non-zero power of two) alignment.
uint64_t value() const
This is a hole in the type system and should not be abused.