36#define DEBUG_TYPE "m68k-frame"
40 TII(*STI.getInstrInfo()),
TRI(STI.getRegisterInfo()) {
51 TRI->hasStackRealignment(MF);
63 (
hasFP(MF) && !TRI->hasStackRealignment(MF)) ||
87 else if (TRI->hasStackRealignment(MF))
99 bool HasFP =
hasFP(MF);
103 assert(HasFP &&
"VLAs and dynamic stack realign, but no FP?!");
112 if (TRI->hasStackRealignment(MF)) {
130 if (TailCallReturnAddrDelta < 0)
131 Offset -= TailCallReturnAddrDelta;
151 switch (
MBBI->getOpcode()) {
154 case TargetOpcode::PATCHABLE_RET:
158 for (
unsigned i = 0, e =
MBBI->getNumOperands(); i != e; ++i) {
169 for (
auto CS : AvailableRegs)
181 return RegMask.PhysReg == Reg;
186M68kFrameLowering::calculateMaxStackAlign(
const MachineFunction &MF)
const {
192 MaxAlign = (StackAlign > MaxAlign) ? StackAlign : MaxAlign;
193 else if (MaxAlign < SlotSize)
204 unsigned AndOp = M68k::AND32di;
205 unsigned MovOp = M68k::MOV32rr;
210 unsigned Tmp = M68k::D0;
222 MI->getOperand(3).setIsDead();
233 unsigned Opcode =
I->getOpcode();
236 uint64_t Amount =
I->getOperand(0).getImm();
237 uint64_t InternalAmt = (IsDestroy || Amount) ?
I->getOperand(1).getImm() : 0;
240 if (!ReserveCallFrame) {
249 Amount =
alignTo(Amount, StackAlign);
262 if (HasDwarfEHHandlers && !IsDestroy &&
273 Amount -= InternalAmt;
283 int64_t StackAdjustment = IsDestroy ? Amount : -Amount;
284 int64_t CfaAdjustment = -StackAdjustment;
286 if (StackAdjustment) {
293 if (StackAdjustment) {
294 BuildStackAdjustment(
MBB,
I,
DL, StackAdjustment,
false);
318 if (IsDestroy && InternalAmt) {
327 while (CI !=
B && !std::prev(CI)->isCall())
329 BuildStackAdjustment(
MBB, CI,
DL, -InternalAmt,
false);
339 int64_t NumBytes,
bool InEpilogue)
const {
340 bool IsSub = NumBytes < 0;
358 unsigned Opc = M68k::MOV32ri;
360 Opc = IsSub ? M68k::SUB32ar : M68k::ADD32ar;
365 MI->getOperand(3).setIsDead();
374 MBB,
MBBI,
DL, IsSub ? -ThisVal : ThisVal, InEpilogue);
386 bool MergeWithPrevious)
const {
393 MergeWithPrevious ? nullptr : std::next(
MBBI);
394 unsigned Opc = PI->getOpcode();
397 if (!MergeWithPrevious && NI !=
MBB.
end() &&
398 NI->getOpcode() == TargetOpcode::CFI_INSTRUCTION) {
403 if (Opc == M68k::ADD32ai && PI->getOperand(0).getReg() == StackPtr) {
404 assert(PI->getOperand(1).getReg() == StackPtr);
405 Offset += PI->getOperand(2).getImm();
407 if (!MergeWithPrevious)
409 }
else if (Opc == M68k::SUB32ai && PI->getOperand(0).getReg() == StackPtr) {
410 assert(PI->getOperand(1).getReg() == StackPtr);
411 Offset -= PI->getOperand(2).getImm();
413 if (!MergeWithPrevious)
423 assert(
Offset != 0 &&
"zero offset stack adjustment requested");
429 unsigned Opc = IsSub ? M68k::SUB32ai : M68k::ADD32ai;
436 MI->getOperand(3).setIsDead();
450void M68kFrameLowering::emitPrologueCalleeSavedFrameMoves(
463 for (
const auto &
I : CSI) {
467 unsigned DwarfReg =
MRI->getDwarfRegNum(Reg,
true);
476 "MF used frame lowering for wrong subtarget");
481 uint64_t MaxAlign = calculateMaxStackAlign(MF);
483 bool HasFP =
hasFP(MF);
486 const unsigned MachineFramePtr =
FramePtr;
496 if (TailCallReturnAddrDelta < 0) {
498 TailCallReturnAddrDelta);
504 if (TailCallReturnAddrDelta < 0) {
505 BuildStackAdjustment(
MBB,
MBBI,
DL, TailCallReturnAddrDelta,
525 int stackGrowth = -SlotSize;
529 uint64_t FrameSize = StackSize - SlotSize;
533 FrameSize += SlotSize;
538 if (TRI->hasStackRealignment(MF))
539 NumBytes =
alignTo(NumBytes, MaxAlign);
559 int DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr,
true);
560 assert(DwarfFramePtr > 0);
569 unsigned DwarfFramePtr = TRI->getDwarfRegNum(MachineFramePtr,
true);
577 EveryMBB.addLiveIn(MachineFramePtr);
583 bool PushedRegs =
false;
587 MBBI->getOpcode() == M68k::PUSH32r) {
591 if (!HasFP && NeedsDwarfCFI) {
603 if (TRI->hasStackRealignment(MF)) {
604 assert(HasFP &&
"There should be a frame pointer if stack is realigned.");
605 BuildStackAlignAND(
MBB,
MBBI,
DL, StackPtr, MaxAlign);
617 unsigned SPOrEstablisher = StackPtr;
631 unsigned Opm = M68k::MOV32ja;
640 if (((!HasFP && NumBytes) || PushedRegs) && NeedsDwarfCFI) {
642 if (!HasFP && NumBytes) {
652 emitPrologueCalleeSavedFrameMoves(
MBB,
MBBI,
DL);
665 return Opc == M68k::TCRETURNj || Opc == M68k::TCRETURNq;
673 std::optional<unsigned> RetOpcode;
675 RetOpcode =
MBBI->getOpcode();
680 unsigned MachineFramePtr =
FramePtr;
684 uint64_t MaxAlign = calculateMaxStackAlign(MF);
690 uint64_t FrameSize = StackSize - SlotSize;
691 NumBytes = FrameSize - CSSize;
695 if (TRI->hasStackRealignment(MF))
696 NumBytes =
alignTo(FrameSize, MaxAlign);
699 NumBytes = StackSize - CSSize;
705 unsigned Opc = PI->getOpcode();
708 Opc != M68k::DBG_VALUE && !PI->isTerminator())
728 if (TRI->hasStackRealignment(MF))
735 if (LEAAmount != 0) {
736 unsigned Opc = M68k::LEA32p;
747 }
else if (
hasFP(MF)) {
751 }
else if (NumBytes) {
760 assert(
Offset >= 0 &&
"TCDelta should never be positive");
781 if (TailCallReturnAddrDelta < 0) {
792 TailCallReturnAddrDelta - SlotSize,
true);
803 std::vector<CalleeSavedInfo> &CSI)
const {
811 SpillSlotOffset -= SlotSize;
818 for (
unsigned i = 0, e = CSI.size(); i < e; ++i) {
819 if (
TRI->regsOverlap(CSI[i].getReg(),
FPReg)) {
820 CSI.erase(CSI.begin() + i);
838 for (
const auto &
Info : CSI) {
839 FI = std::max(FI,
Info.getFrameIdx());
841 unsigned Shift =
MRI.getSpillRegisterOrder(Reg);
853 for (
const auto &
Info : CSI) {
873 for (
const auto &
Info : CSI) {
874 FI = std::max(FI,
Info.getFrameIdx());
876 unsigned Shift =
MRI.getSpillRegisterOrder(Reg);
885 for (
const auto &
Info : CSI) {
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
const HexagonInstrInfo * TII
static bool isRegLiveIn(MachineBasicBlock &MBB, unsigned Reg)
static bool isTailCallOpcode(unsigned Opc)
static unsigned findDeadCallerSavedReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, const M68kRegisterInfo *TRI)
Return a caller-saved register that isn't live when it reaches the "return" instruction.
This file contains the M68k declaration of TargetFrameLowering class.
This file exposes functions that may be used with BuildMI from the MachineInstrBuilder....
This file contains the M68k implementation of the TargetInstrInfo class.
This file declares the M68k specific subclass of MachineFunctionInfo.
This file declares the M68k specific subclass of TargetSubtargetInfo.
unsigned const TargetRegisterInfo * TRI
static constexpr Register FPReg
Remove Loads Into Fake Uses
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallSet class.
static const unsigned FramePtr
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
bool hasFPImpl(const MachineFunction &MF) const override
Return true if the specified function should have a dedicated frame pointer register.
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Insert epilog code into the function.
void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, int64_t NumBytes, bool InEpilogue) const
Emit a series of instructions to increment / decrement the stack pointer by a constant value.
bool assignCalleeSavedSpillSlots(MachineFunction &MF, const TargetRegisterInfo *TRI, std::vector< CalleeSavedInfo > &CSI) const override
Allows target to override spill slot assignment logic.
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, ArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
Issues instruction(s) to spill all callee saved registers and returns true if it isn't possible / pro...
bool hasReservedCallFrame(const MachineFunction &MF) const override
Under normal circumstances, when a frame pointer is not required, we reserve argument space for call ...
M68kFrameLowering(const M68kSubtarget &sti, Align Alignment)
int mergeSPUpdates(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, bool doMergeWithPrevious) const
Check the instruction before/after the passed instruction.
StackOffset getFrameIndexReference(const MachineFunction &MF, int FI, Register &FrameReg) const override
This method should return the base register and offset used to reference a frame index location.
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...
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Insert prolog code into the function.
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, MutableArrayRef< CalleeSavedInfo > CSI, const TargetRegisterInfo *TRI) const override
Issues instruction(s) to restore all callee saved registers and returns true if it isn't possible / p...
bool canSimplifyCallFramePseudos(const MachineFunction &MF) const override
If there is a reserved call frame, the call frame pseudos can be simplified.
bool needsFrameIndexResolution(const MachineFunction &MF) const override
int getRestoreBasePointerOffset() const
unsigned getCalleeSavedFrameSize() const
bool getRestoreBasePointer() const
void setCalleeSavedFrameSize(unsigned bytes)
bool getHasPushSequences() const
int getTCReturnAddrDelta() const
Register getFrameRegister(const MachineFunction &MF) const override
unsigned getBaseRegister() const
bool hasBasePointer(const MachineFunction &MF) const
unsigned getStackRegister() const
unsigned getSlotSize() const
getSlotSize - Stack slot size in bytes.
const M68kRegisterInfo * getRegisterInfo() const override
static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register, SMLoc Loc={})
.cfi_def_cfa_register modifies a rule for computing CFA.
static MCCFIInstruction createGnuArgsSize(MCSymbol *L, int64_t Size, SMLoc Loc={})
A special wrapper for .cfi_escape that indicates GNU_ARGS_SIZE.
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.
static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int64_t Adjustment, SMLoc Loc={})
.cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but Offset is a relative value that is added/subt...
const MCRegisterInfo * getRegisterInfo() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
MCRegAliasIterator enumerates all registers aliasing Reg.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
iterator_range< livein_iterator > liveins() const
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
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.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool IsImmutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
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.
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...
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool hasStackObjects() const
Return true if there are any stack objects in this function.
int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setOffsetAdjustment(int64_t Adj)
Set the correction for frame offsets.
unsigned addFrameInst(const MCCFIInstruction &Inst)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool needsFrameMoves() const
True if this function needs frame moves for debug or exceptions.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MCContext & getContext() const
bool callsEHReturn() const
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
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.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isLiveIn(Register Reg) const
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Wrapper class representing virtual and physical registers.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
StackOffset holds a fixed and a scalable offset in bytes.
int64_t getFixed() const
Returns the fixed component of the stack.
Information about stack frame layout on the target.
unsigned getStackAlignment() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
bool hasFP(const MachineFunction &MF) const
hasFP - Return true if the specified function should have a dedicated frame pointer register.
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
int getOffsetOfLocalArea() const
getOffsetOfLocalArea - This method returns the offset of the local area from the stack pointer on ent...
unsigned getCallFrameDestroyOpcode() const
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...
static const MachineInstrBuilder & addMemOperand(const MachineInstrBuilder &MIB, int FI, int Offset=0)
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
static const MachineInstrBuilder & addRegIndirectWithDisp(const MachineInstrBuilder &MIB, Register Reg, bool IsKill, int Offset)
addRegIndirectWithDisp - This function is used to add a memory reference of the form (Offset,...
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Kill
The last use of a register.
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ DwarfCFI
DWARF-like instruction based exceptions.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
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.
Pair of physical register and lane mask.