23 #define DEBUG_TYPE "arc-frame-lowering" 29 cl::desc(
"Use arc callee save/restore functions"),
33 "__st_r13_to_r15",
"__st_r13_to_r16",
"__st_r13_to_r17",
"__st_r13_to_r18",
34 "__st_r13_to_r19",
"__st_r13_to_r20",
"__st_r13_to_r21",
"__st_r13_to_r22",
35 "__st_r13_to_r23",
"__st_r13_to_r24",
"__st_r13_to_r25",
39 "__ld_r13_to_r15",
"__ld_r13_to_r16",
"__ld_r13_to_r17",
"__ld_r13_to_r18",
40 "__ld_r13_to_r19",
"__ld_r13_to_r20",
"__ld_r13_to_r21",
"__ld_r13_to_r22",
41 "__ld_r13_to_r23",
"__ld_r13_to_r24",
"__ld_r13_to_r25",
47 int Amount,
int StackPtr) {
61 LLVM_DEBUG(
dbgs() <<
"Internal: adjust stack by: " << Amount <<
"," 62 << AbsAmount <<
"\n");
64 assert((AbsAmount % 4 == 0) &&
"Stack adjustments must be 4-byte aligned.");
65 if (isUInt<6>(AbsAmount))
66 AdjOp = Positive ? ARC::ADD_rru6 : ARC::SUB_rru6;
67 else if (isInt<12>(AbsAmount))
68 AdjOp = Positive ? ARC::ADD_rrs12 : ARC::SUB_rrs12;
70 AdjOp = Positive ? ARC::ADD_rrlimm : ARC::SUB_rrlimm;
72 BuildMI(MBB, MBBI, dl, TII.get(AdjOp), StackPtr)
80 for (
auto Reg : CSI) {
81 assert(
Reg.getReg() >= ARC::R13 &&
Reg.getReg() <= ARC::R25 &&
82 "Unexpected callee saved reg.");
83 if (
Reg.getReg() > Last)
94 SavedRegs.
set(ARC::BLINK);
97 void ARCFrameLowering::adjustStackToMatchRecords(
99 bool Allocate)
const {
106 ScalarAlloc = -ScalarAlloc;
110 ScalarAlloc, ARC::SP);
131 unsigned StackSlotsUsedByFunclet = 0;
132 bool SavedBlink =
false;
133 unsigned AlreadyAdjusted = 0;
137 unsigned VarArgsBytes = MFI.
getObjectSize(AFI->getVarArgsFrameIndex());
138 unsigned Opc = ARC::SUB_rrlimm;
139 if (isUInt<6>(VarArgsBytes))
141 else if (isInt<12>(VarArgsBytes))
142 Opc = ARC::SUB_rrs12;
143 BuildMI(MBB, MBBI, dl, TII->get(Opc), ARC::SP)
149 BuildMI(MBB, MBBI, dl, TII->get(ARC::ST_AW_rs9))
154 AlreadyAdjusted += 4;
159 StackSlotsUsedByFunclet = Last - ARC::R12;
160 BuildMI(MBB, MBBI, dl, TII->get(ARC::PUSH_S_BLINK));
161 BuildMI(MBB, MBBI, dl, TII->get(ARC::SUB_rru6))
164 .
addImm(4 * StackSlotsUsedByFunclet);
166 .addExternalSymbol(store_funclet_name[Last - ARC::R15])
168 AlreadyAdjusted += 4 * (StackSlotsUsedByFunclet + 1);
172 if (MFI.
hasCalls() && !SavedBlink) {
174 BuildMI(MBB, MBBI, dl, TII->get(ARC::PUSH_S_BLINK));
175 AlreadyAdjusted += 4;
177 if (AFI->MaxCallStackReq > 0)
201 BuildMI(MBB, MBBI, dl, TII->get(TargetOpcode::CFI_INSTRUCTION))
202 .addCFIIndex(CFIIndex)
209 BuildMI(MBB, MBBI, dl, TII->get(TargetOpcode::CFI_INSTRUCTION))
210 .addCFIIndex(CFIIndex)
218 BuildMI(MBB, MBBI, dl, TII->get(TargetOpcode::CFI_INSTRUCTION))
219 .addCFIIndex(CFIIndex)
223 for (
const auto &
Entry : CSI) {
225 int FI =
Entry.getFrameIdx();
227 if ((
hasFP(MF) && Reg == ARC::FP) || (MFI.
hasCalls() && Reg == ARC::BLINK))
231 BuildMI(MBB, MBBI, dl, TII->get(TargetOpcode::CFI_INSTRUCTION))
232 .addCFIIndex(CFIIndex)
248 bool SavedBlink =
false;
249 unsigned AmountAboveFunclet = 0;
255 unsigned Opc = ARC::SUB_rrlimm;
256 if (isUInt<6>(StackSize))
261 AmountAboveFunclet += 4;
267 unsigned StackSlotsUsedByFunclet = 0;
271 StackSlotsUsedByFunclet = Last - ARC::R12;
272 AmountAboveFunclet += 4 * (StackSlotsUsedByFunclet + 1);
276 if (MFI.
hasCalls() && !SavedBlink) {
277 AmountAboveFunclet += 4;
282 if (
unsigned MoveAmount = StackSize - AmountAboveFunclet) {
283 unsigned Opc = ARC::ADD_rrlimm;
284 if (isUInt<6>(MoveAmount))
286 else if (isInt<12>(MoveAmount))
287 Opc = ARC::ADD_rrs12;
290 .
addImm(StackSize - AmountAboveFunclet);
293 if (StackSlotsUsedByFunclet) {
296 .addExternalSymbol(load_funclet_name[Last - ARC::R15])
298 unsigned Opc = ARC::ADD_rrlimm;
299 if (isUInt<6>(4 * StackSlotsUsedByFunclet))
301 else if (isInt<12>(4 * StackSlotsUsedByFunclet))
302 Opc = ARC::ADD_rrs12;
305 .
addImm(4 * (StackSlotsUsedByFunclet));
324 unsigned VarArgsBytes = MFI.
getObjectSize(AFI->getVarArgsFrameIndex());
325 unsigned Opc = ARC::ADD_rrlimm;
326 if (isUInt<6>(VarArgsBytes))
328 else if (isInt<12>(VarArgsBytes))
329 Opc = ARC::ADD_rrs12;
337 static std::vector<CalleeSavedInfo>::iterator
339 for (
auto I = V.begin(),
E = V.end();
I !=
E; ++
I) {
340 if (reg ==
I->getReg())
348 std::vector<CalleeSavedInfo> &CSI)
const {
358 LLVM_DEBUG(
dbgs() <<
"Creating fixed object (" << StackObj <<
") for FP at " 359 << CurOffset <<
"\n");
367 <<
") for BLINK at " << CurOffset <<
"\n");
373 for (
unsigned Which = Last; Which > ARC::R12; Which--) {
375 if (RegI == CSI.end() || RegI->getFrameIdx() == 0) {
379 if (RegI != CSI.end())
380 RegI->setFrameIdx(FI);
385 for (
auto &
I : CSI) {
386 if (
I.getReg() > ARC::R12)
388 if (
I.getFrameIdx() == 0) {
391 <<
") for other register at " << CurOffset <<
"\n");
395 <<
") for other register at " << CurOffset <<
"\n");
404 const std::vector<CalleeSavedInfo> &CSI,
441 LLVM_DEBUG(
dbgs() <<
"Current stack size: " << MFI.getStackSize() <<
"\n");
443 if (MFI.hasStackObjects()) {
444 int RegScavFI = MFI.CreateStackObject(
447 LLVM_DEBUG(
dbgs() <<
"Created scavenging index RegScavFI=" << RegScavFI
454 unsigned Reg,
int NumBytes,
bool IsAdd,
457 if (isUInt<6>(NumBytes))
458 Opc = IsAdd ? ARC::ADD_rru6 : ARC::SUB_rru6;
459 else if (isInt<12>(NumBytes))
460 Opc = IsAdd ? ARC::ADD_rrs12 : ARC::SUB_rrs12;
462 Opc = IsAdd ? ARC::ADD_rrlimm : ARC::SUB_rrlimm;
479 if (Amt > AFI->MaxCallStackReq && Old.
getOpcode() == ARC::ADJCALLSTACKDOWN)
484 Old.
getOpcode() == ARC::ADJCALLSTACKUP) &&
485 "Unknown Frame Pseudo.");
486 bool IsAdd = (Old.
getOpcode() == ARC::ADJCALLSTACKUP);
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
ARCFunctionInfo - This class is derived from MachineFunction private ARC target-specific information ...
This class represents lattice values for constants.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_NODISCARD unsigned addFrameInst(const MCCFIInstruction &Inst)
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Insert Prologue into the function.
static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register, int Offset)
.cfi_offset Previous value of Register is saved at offset Offset from CFA.
static unsigned determineLastCalleeSave(const std::vector< CalleeSavedInfo > &CSI)
unsigned const TargetRegisterInfo * TRI
MachineModuleInfo & getMMI() const
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS) const override
Add explicit callee save registers.
static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset)
.cfi_def_cfa_offset modifies a rule for computing CFA.
unsigned getSpillSize(const TargetRegisterClass &RC) const
Return the size in bytes of the stack slot allocated to hold a spilled copy of a register from class ...
static cl::opt< bool > UseSaveRestoreFunclet("arc-save-restore-funclet", cl::Hidden, cl::desc("Use arc callee save/restore functions"), cl::init(true))
virtual void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
unsigned getSpillAlignment(const TargetRegisterClass &RC) const
Return the minimum required alignment in bytes for a spill slot for a register of this class...
bool isFrameAddressTaken() const
This method may be called any time after instruction selection is complete to determine if there is a...
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
const HexagonInstrInfo * TII
MachineBasicBlock iterator that automatically skips over MIs that are inside bundles (i...
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static const char * load_funclet_name[]
Context object for machine code objects.
int getDwarfRegNum(MCRegister RegNum, bool isEH) const
Map a target register to an equivalent dwarf register number.
bool DisableFramePointerElim(const MachineFunction &MF) const
DisableFramePointerElim - This returns true if frame pointer elimination optimization should be disab...
MachineBasicBlock::iterator eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const override
This method is called during prolog/epilog code insertion to eliminate call frame setup and destroy p...
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
static const char * store_funclet_name[]
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const ARCInstrInfo * getInstrInfo() const override
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
initializer< Ty > init(const Ty &Val)
This file declares the machine register scavenger class.
unsigned const MachineRegisterInfo * MRI
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool assignCalleeSavedSpillSlots(llvm::MachineFunction &, const llvm::TargetRegisterInfo *, std::vector< llvm::CalleeSavedInfo > &) const override
assignCalleeSavedSpillSlots - Allows target to override spill slot assignment logic.
void setStackSize(uint64_t Size)
Set the size of the stack.
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE and DBG_LABEL instructions...
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
bool restoreCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
restoreCalleeSavedRegisters - Issues instruction(s) to restore all callee saved registers and returns...
void addScavengingFrameIndex(int FI)
Add a scavenging frame index.
static std::vector< CalleeSavedInfo >::iterator getSavedReg(std::vector< CalleeSavedInfo > &V, unsigned reg)
int CreateFixedSpillStackObject(uint64_t Size, int64_t SPOffset, bool IsImmutable=false)
Create a spill slot at a fixed location on the stack.
bool spillCalleeSavedRegisters(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const std::vector< CalleeSavedInfo > &CSI, const TargetRegisterInfo *TRI) const override
spillCalleeSavedRegisters - Issues instruction(s) to spill all callee saved registers and returns tru...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static void generateStackAdjustment(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const ARCInstrInfo &TII, DebugLoc dl, int Amount, int StackPtr)
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
Insert Epilogue into the function.
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCRegisterInfo * getRegisterInfo() const
bool needsStackRealignment(const MachineFunction &MF) const
True if storage within the function requires the stack pointer to be aligned more than the normal cal...
void setObjectOffset(int ObjectIdx, int64_t SPOffset)
Set the stack frame offset of the specified object.
const MachineOperand & getOperand(unsigned i) const
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
This class contains meta information specific to a module.
bool hasCalls() const
Return true if the current function has any function calls.
static void emitRegUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, DebugLoc dl, unsigned Reg, int NumBytes, bool IsAdd, const ARCInstrInfo *TII)