LLVM 24.0.0git
SparcFrameLowering.cpp
Go to the documentation of this file.
1//===-- SparcFrameLowering.cpp - Sparc Frame Information ------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the Sparc implementation of TargetFrameLowering class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SparcFrameLowering.h"
14#include "SparcInstrInfo.h"
16#include "SparcSubtarget.h"
23
24using namespace llvm;
25
28 ST.is64Bit() ? Align(16) : Align(8), 0,
29 ST.is64Bit() ? Align(16) : Align(8),
30 /*StackRealignable=*/false) {}
31
32void SparcFrameLowering::emitSPAdjustment(MachineFunction &MF,
35 int NumBytes,
36 unsigned ADDrr,
37 unsigned ADDri) const {
38
39 DebugLoc dl;
40 const SparcInstrInfo &TII =
41 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
42
43 if (NumBytes >= -4096 && NumBytes < 4096) {
44 BuildMI(MBB, MBBI, dl, TII.get(ADDri), SP::O6)
45 .addReg(SP::O6).addImm(NumBytes);
46 return;
47 }
48
49 // Emit this the hard way. This clobbers G1 which we always know is
50 // available here.
51 if (NumBytes >= 0) {
52 // Emit nonnegative numbers with sethi + or.
53 // sethi %hi(NumBytes), %g1
54 // or %g1, %lo(NumBytes), %g1
55 // add %sp, %g1, %sp
56 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1)
57 .addImm(HI22(NumBytes));
58 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
59 .addReg(SP::G1).addImm(LO10(NumBytes));
60 BuildMI(MBB, MBBI, dl, TII.get(ADDrr), SP::O6)
61 .addReg(SP::O6).addReg(SP::G1);
62 return ;
63 }
64
65 // Emit negative numbers with sethi + xor.
66 // sethi %hix(NumBytes), %g1
67 // xor %g1, %lox(NumBytes), %g1
68 // add %sp, %g1, %sp
69 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1)
70 .addImm(HIX22(NumBytes));
71 BuildMI(MBB, MBBI, dl, TII.get(SP::XORri), SP::G1)
72 .addReg(SP::G1).addImm(LOX10(NumBytes));
73 BuildMI(MBB, MBBI, dl, TII.get(ADDrr), SP::O6)
74 .addReg(SP::O6).addReg(SP::G1);
75}
76
78 MachineBasicBlock &MBB) const {
80
81 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
83 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
85
86 // Get the number of bytes to allocate from the FrameInfo
87 int NumBytes = (int) MFI.getStackSize();
88
89 unsigned SAVEri = SP::SAVEri;
90 unsigned SAVErr = SP::SAVErr;
91 if (FuncInfo->isLeafProc()) {
92 if (NumBytes == 0)
93 return;
94 SAVEri = SP::ADDri;
95 SAVErr = SP::ADDrr;
96 }
97
98 // The SPARC ABI is a bit odd in that it requires a reserved 92-byte
99 // (128 in v9) area in the user's stack, starting at %sp. Thus, the
100 // first part of the stack that can actually be used is located at
101 // %sp + 92.
102 //
103 // We therefore need to add that offset to the total stack size
104 // after all the stack objects are placed by
105 // PrologEpilogInserter calculateFrameObjectOffsets. However, since the stack needs to be
106 // aligned *after* the extra size is added, we need to disable
107 // calculateFrameObjectOffsets's built-in stack alignment, by having
108 // targetHandlesStackFrameRounding return true.
109
110
111 // Add the extra call frame stack size, if needed. (This is the same
112 // code as in PrologEpilogInserter, but also gets disabled by
113 // targetHandlesStackFrameRounding)
114 if (MFI.adjustsStack() && hasReservedCallFrame(MF))
115 NumBytes += MFI.getMaxCallFrameSize();
116
117 // Adds the SPARC subtarget-specific spill area to the stack
118 // size. Also ensures target-required alignment.
119 NumBytes = Subtarget.getAdjustedFrameSize(NumBytes);
120
121 // Finally, ensure that the size is sufficiently aligned for the
122 // data on the stack.
123 NumBytes = alignTo(NumBytes, MFI.getMaxAlign());
124
125 // Update stack size with corrected value.
126 MFI.setStackSize(NumBytes);
127
128 emitSPAdjustment(MF, MBB, MBBI, -NumBytes, SAVErr, SAVEri);
129
130 if (MF.needsFrameMoves()) {
132 CFIBuilder.buildDefCFARegister(SP::I6);
133 CFIBuilder.buildWindowSave();
134 CFIBuilder.buildRegister(SP::O7, SP::I7);
135 }
136}
137
141 if (!hasReservedCallFrame(MF)) {
142 MachineInstr &MI = *I;
143 int Size = MI.getOperand(0).getImm();
144 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
145 Size = -Size;
146
147 if (Size)
148 emitSPAdjustment(MF, MBB, I, Size, SP::ADDrr, SP::ADDri);
149 }
150 return MBB.erase(I);
151}
152
153
155 MachineBasicBlock &MBB) const {
157 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
158 const SparcInstrInfo &TII =
159 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
160 DebugLoc dl = MBBI->getDebugLoc();
161 assert((MBBI->getOpcode() == SP::RETL || MBBI->getOpcode() == SP::TAIL_CALL ||
162 MBBI->getOpcode() == SP::TAIL_CALLri) &&
163 "Can only put epilog before 'retl' or 'tail_call' instruction!");
164 if (!FuncInfo->isLeafProc()) {
165 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
166 .addReg(SP::G0);
167 return;
168 }
169 MachineFrameInfo &MFI = MF.getFrameInfo();
170
171 int NumBytes = (int) MFI.getStackSize();
172 if (NumBytes != 0)
173 emitSPAdjustment(MF, MBB, MBBI, NumBytes, SP::ADDrr, SP::ADDri);
174
175 // Preserve return address in %o7
176 if (MBBI->getOpcode() == SP::TAIL_CALL) {
177 MBB.addLiveIn(SP::O7);
178 BuildMI(MBB, MBBI, dl, TII.get(SP::ORrr), SP::G1)
179 .addReg(SP::G0)
180 .addReg(SP::O7);
181 BuildMI(MBB, MBBI, dl, TII.get(SP::ORrr), SP::O7)
182 .addReg(SP::G0)
183 .addReg(SP::G1);
184 }
185}
186
188 // Reserve call frame if there are no variable sized objects on the stack.
189 return !MF.getFrameInfo().hasVarSizedObjects();
190}
191
192// hasFPImpl - Return true if the specified function should have a dedicated
193// frame pointer register. This is true if the function has variable sized
194// allocas or if frame pointer elimination is disabled.
196 const MachineFrameInfo &MFI = MF.getFrameInfo();
197 return MF.disableFramePointerElim() || MFI.hasVarSizedObjects() ||
199}
200
203 Register &FrameReg) const {
204 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
205 const MachineFrameInfo &MFI = MF.getFrameInfo();
206 const SparcRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
208 bool isFixed = MFI.isFixedObjectIndex(FI);
209
210 // Addressable stack objects are accessed using neg. offsets from
211 // %fp, or positive offsets from %sp.
212 bool UseFP;
213
214 // Sparc uses FP-based references in general, even when "hasFP" is
215 // false. That function is rather a misnomer, because %fp is
216 // actually always available, unless isLeafProc.
217 if (FuncInfo->isLeafProc()) {
218 // If there's a leaf proc, all offsets need to be %sp-based,
219 // because we haven't caused %fp to actually point to our frame.
220 UseFP = false;
221 } else if (isFixed) {
222 // Otherwise, argument access should always use %fp.
223 UseFP = true;
224 } else {
225 // Finally, default to using %fp.
226 UseFP = true;
227 }
228
229 int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(FI) +
230 Subtarget.getStackPointerBias();
231
232 if (UseFP) {
233 FrameReg = RegInfo->getFrameRegister(MF);
234 return StackOffset::getFixed(FrameOffset);
235 } else {
236 FrameReg = SP::O6; // %sp
237 return StackOffset::getFixed(FrameOffset + MF.getFrameInfo().getStackSize());
238 }
239}
240
241[[maybe_unused]] static bool verifyLeafProcRegUse(MachineRegisterInfo *MRI) {
242
243 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
244 if (MRI->isPhysRegUsed(reg))
245 return false;
246
247 for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
248 if (MRI->isPhysRegUsed(reg))
249 return false;
250
251 return true;
252}
253
254bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const
255{
256
257 MachineRegisterInfo &MRI = MF.getRegInfo();
258 MachineFrameInfo &MFI = MF.getFrameInfo();
259
260 return !(MFI.hasCalls() // has calls
261 || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
262 || MRI.isPhysRegUsed(SP::O6) // %sp is used
263 || hasFP(MF) // need %fp
264 || MF.hasInlineAsm()); // has inline assembly
265}
266
267void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
268 MachineRegisterInfo &MRI = MF.getRegInfo();
269 // Remap %i[0-7] to %o[0-7].
270 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
271 if (!MRI.isPhysRegUsed(reg))
272 continue;
273
274 unsigned mapped_reg = reg - SP::I0 + SP::O0;
275
276 // Replace I register with O register.
277 MRI.replaceRegWith(reg, mapped_reg);
278
279 // Also replace register pair super-registers.
280 if ((reg - SP::I0) % 2 == 0) {
281 unsigned preg = (reg - SP::I0) / 2 + SP::I0_I1;
282 unsigned mapped_preg = preg - SP::I0_I1 + SP::O0_O1;
283 MRI.replaceRegWith(preg, mapped_preg);
284 }
285 }
286
287 // Rewrite MBB's Live-ins.
288 for (MachineBasicBlock &MBB : MF) {
289 for (unsigned reg = SP::I0_I1; reg <= SP::I6_I7; ++reg) {
290 if (!MBB.isLiveIn(reg))
291 continue;
292 MBB.removeLiveIn(reg);
293 MBB.addLiveIn(reg - SP::I0_I1 + SP::O0_O1);
294 }
295 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
296 if (!MBB.isLiveIn(reg))
297 continue;
298 MBB.removeLiveIn(reg);
299 MBB.addLiveIn(reg - SP::I0 + SP::O0);
300 }
301 }
302
304#ifdef EXPENSIVE_CHECKS
305 MF.verify(0, "After LeafProc Remapping");
306#endif
307}
308
310 BitVector &SavedRegs,
311 RegScavenger *RS) const {
313 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
314 if (!Subtarget.getCLOpts().disable_sparc_leaf_proc && isLeafProc(MF)) {
316 MFI->setLeafProc(true);
317
318 remapRegsForLeafProc(MF);
319 }
320}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
static bool verifyLeafProcRegUse(MachineRegisterInfo *MRI)
static bool is64Bit(const char *name)
Helper class for creating CFI instructions and inserting them into MIR.
void buildWindowSave() const
void buildRegister(MCRegister Reg1, MCRegister Reg2) const
void buildDefCFARegister(MCRegister Reg) const
A debug info location.
Definition DebugLoc.h:126
LLVM_ABI void removeLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
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 ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
bool adjustsStack() const
Return true if this function adjusts the stack – e.g., when calling another function.
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 alignment of this function's frame.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
void setStackSize(uint64_t Size)
Set the size of the stack.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool hasInlineAsm() const
Returns true if the function contains any inline assembly.
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool disableFramePointerElim() const
Returns true if frame pointer elimination should be disabled for this function.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
LLVM_ABI bool isPhysRegUsed(MCRegister PhysReg, bool SkipRegMaskTest=false) const
Return true if the specified register is modified or read in this function.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
SparcFrameLowering(const SparcSubtarget &ST)
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
void determineCalleeSaves(MachineFunction &MF, BitVector &SavedRegs, RegScavenger *RS=nullptr) const override
This method determines which of the registers reported by TargetRegisterInfo::getCalleeSavedRegs() sh...
bool hasFPImpl(const MachineFunction &MF) const override
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 I) 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,...
const SparcRegisterInfo * getRegisterInfo() const override
int64_t getStackPointerBias() const
The 64-bit ABI uses biased stack and frame pointers, so the stack frame of the current function is th...
int getAdjustedFrameSize(int stackSize) const
Given a actual stack size as determined by FrameInfo, this function returns adjusted framesize which ...
const SparcOptions & getCLOpts() const
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:30
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:46
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...
TargetFrameLowering(StackDirection D, Align StackAl, int LAO, Align TransAl=Align(1), bool StackReal=true)
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.
static unsigned HI22(int64_t imm)
Definition Sparc.h:173
static unsigned HIX22(int64_t imm)
Definition Sparc.h:181
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
static unsigned LOX10(int64_t imm)
Definition Sparc.h:185
static unsigned LO10(int64_t imm)
Definition Sparc.h:177
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39