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"
15#include "SparcMachineFunctionInfo.h"
16#include "SparcSubtarget.h"
17#include "llvm/CodeGen/CFIInstBuilder.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/RegisterScavenging.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "sparc-frame-lowering"
28
29SparcFrameLowering::SparcFrameLowering(const SparcSubtarget &ST)
30 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
31 ST.is64Bit() ? Align(16) : Align(8), 0,
32 ST.is64Bit() ? Align(16) : Align(8),
33 /*StackRealignable=*/false),
34 STI(ST) {}
35
36void SparcFrameLowering::emitSPAdjustment(MachineFunction &MF,
37 MachineBasicBlock &MBB,
38 MachineBasicBlock::iterator MBBI,
39 const DebugLoc &dl, int NumBytes,
40 unsigned ADDrr,
41 unsigned ADDri) const {
42
43 const SparcInstrInfo &TII =
44 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
45
46 if (NumBytes >= -4096 && NumBytes < 4096) {
47 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: ADDri), DestReg: SP::O6)
48 .addReg(RegNo: SP::O6).addImm(Val: NumBytes);
49 return;
50 }
51
52 // Emit this the hard way. This clobbers G1 which we always know is
53 // available here.
54 if (NumBytes >= 0) {
55 // Emit nonnegative numbers with sethi + or.
56 // sethi %hi(NumBytes), %g1
57 // or %g1, %lo(NumBytes), %g1
58 // add %sp, %g1, %sp
59 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::SETHIi), DestReg: SP::G1)
60 .addImm(Val: HI22(imm: NumBytes));
61 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::ORri), DestReg: SP::G1)
62 .addReg(RegNo: SP::G1).addImm(Val: LO10(imm: NumBytes));
63 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: ADDrr), DestReg: SP::O6)
64 .addReg(RegNo: SP::O6).addReg(RegNo: SP::G1);
65 return ;
66 }
67
68 // Emit negative numbers with sethi + xor.
69 // sethi %hix(NumBytes), %g1
70 // xor %g1, %lox(NumBytes), %g1
71 // add %sp, %g1, %sp
72 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::SETHIi), DestReg: SP::G1)
73 .addImm(Val: HIX22(imm: NumBytes));
74 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::XORri), DestReg: SP::G1)
75 .addReg(RegNo: SP::G1).addImm(Val: LOX10(imm: NumBytes));
76 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: ADDrr), DestReg: SP::O6)
77 .addReg(RegNo: SP::O6).addReg(RegNo: SP::G1);
78}
79
80void SparcFrameLowering::emitPrologue(MachineFunction &MF,
81 MachineBasicBlock &MBB) const {
82 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
83
84 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
85 MachineFrameInfo &MFI = MF.getFrameInfo();
86 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
87 MachineBasicBlock::iterator MBBI = MBB.begin();
88
89 // Get the number of bytes to allocate from the FrameInfo
90 int NumBytes = (int) MFI.getStackSize();
91
92 unsigned SAVEri = SP::SAVEri;
93 unsigned SAVErr = SP::SAVErr;
94 if (FuncInfo->isLeafProc()) {
95 if (NumBytes == 0)
96 return;
97 SAVEri = SP::ADDri;
98 SAVErr = SP::ADDrr;
99 }
100
101 // The SPARC ABI is a bit odd in that it requires a reserved 92-byte
102 // (128 in v9) area in the user's stack, starting at %sp. Thus, the
103 // first part of the stack that can actually be used is located at
104 // %sp + 92.
105 //
106 // We therefore need to add that offset to the total stack size
107 // after all the stack objects are placed by
108 // PrologEpilogInserter calculateFrameObjectOffsets. However, since the stack needs to be
109 // aligned *after* the extra size is added, we need to disable
110 // calculateFrameObjectOffsets's built-in stack alignment, by having
111 // targetHandlesStackFrameRounding return true.
112
113
114 // Add the extra call frame stack size, if needed. (This is the same
115 // code as in PrologEpilogInserter, but also gets disabled by
116 // targetHandlesStackFrameRounding)
117 if (MFI.adjustsStack() && hasReservedCallFrame(MF))
118 NumBytes += MFI.getMaxCallFrameSize();
119
120 // Adds the SPARC subtarget-specific spill area to the stack
121 // size. Also ensures target-required alignment.
122 NumBytes = Subtarget.getAdjustedFrameSize(stackSize: NumBytes);
123
124 // Finally, ensure that the size is sufficiently aligned for the
125 // data on the stack.
126 NumBytes = alignTo(Size: NumBytes, A: MFI.getMaxAlign());
127
128 // Update stack size with corrected value.
129 MFI.setStackSize(NumBytes);
130
131 emitSPAdjustment(MF, MBB, MBBI, dl: DebugLoc(), NumBytes: -NumBytes, ADDrr: SAVErr, ADDri: SAVEri);
132
133 if (MF.needsFrameMoves()) {
134 CFIInstBuilder CFIBuilder(MBB, MBBI, MachineInstr::NoFlags);
135 if (FuncInfo->isLeafProc()) {
136 CFIBuilder.buildDefCFAOffset(Offset: NumBytes + Subtarget.getStackPointerBias());
137 } else {
138 CFIBuilder.buildDefCFARegister(Reg: SP::I6);
139 CFIBuilder.buildWindowSave();
140 CFIBuilder.buildRegister(Reg1: SP::O7, Reg2: SP::I7);
141 }
142 }
143}
144
145MachineBasicBlock::iterator SparcFrameLowering::
146eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
147 MachineBasicBlock::iterator I) const {
148 if (!hasReservedCallFrame(MF)) {
149 MachineInstr &MI = *I;
150 int Size = MI.getOperand(i: 0).getImm();
151 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
152 Size = -Size;
153
154 if (Size)
155 emitSPAdjustment(MF, MBB, MBBI: I, dl: DebugLoc(), NumBytes: Size, ADDrr: SP::ADDrr, ADDri: SP::ADDri);
156 }
157 return MBB.erase(I);
158}
159
160
161void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
162 MachineBasicBlock &MBB) const {
163 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
164 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
165 const SparcInstrInfo &TII =
166 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
167 DebugLoc dl = MBBI->getDebugLoc();
168 assert((MBBI->getOpcode() == SP::RETL || MBBI->getOpcode() == SP::TAIL_CALL ||
169 MBBI->getOpcode() == SP::TAIL_CALLri) &&
170 "Can only put epilog before 'retl' or 'tail_call' instruction!");
171 if (!FuncInfo->isLeafProc()) {
172 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::RESTORErr), DestReg: SP::G0)
173 .addReg(RegNo: SP::G0)
174 .addReg(RegNo: SP::G0)
175 .setMIFlag(MachineInstr::FrameDestroy);
176 return;
177 }
178 MachineFrameInfo &MFI = MF.getFrameInfo();
179
180 int NumBytes = (int) MFI.getStackSize();
181 if (NumBytes != 0)
182 emitSPAdjustment(MF, MBB, MBBI, dl, NumBytes, ADDrr: SP::ADDrr, ADDri: SP::ADDri);
183
184 // Preserve return address in %o7
185 if (MBBI->getOpcode() == SP::TAIL_CALL) {
186 MBB.addLiveIn(PhysReg: SP::O7);
187 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::ORrr), DestReg: SP::G1)
188 .addReg(RegNo: SP::G0)
189 .addReg(RegNo: SP::O7);
190 BuildMI(BB&: MBB, I: MBBI, MIMD: dl, MCID: TII.get(Opcode: SP::ORrr), DestReg: SP::O7)
191 .addReg(RegNo: SP::G0)
192 .addReg(RegNo: SP::G1)
193 .setMIFlag(MachineInstr::FrameDestroy);
194 }
195}
196
197bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
198 // Reserve call frame if there are no variable sized objects on the stack.
199 return !MF.getFrameInfo().hasVarSizedObjects();
200}
201
202// hasFPImpl - Return true if the specified function should have a dedicated
203// frame pointer register. This is true if the function has variable sized
204// allocas or if frame pointer elimination is disabled.
205bool SparcFrameLowering::hasFPImpl(const MachineFunction &MF) const {
206 const MachineFrameInfo &MFI = MF.getFrameInfo();
207 return MF.disableFramePointerElim() || MFI.hasVarSizedObjects() ||
208 MFI.isFrameAddressTaken();
209}
210
211StackOffset
212SparcFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
213 Register &FrameReg) const {
214 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
215 const MachineFrameInfo &MFI = MF.getFrameInfo();
216 const SparcRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
217 const SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
218 bool isFixed = MFI.isFixedObjectIndex(ObjectIdx: FI);
219
220 // Addressable stack objects are accessed using neg. offsets from
221 // %fp, or positive offsets from %sp.
222 bool UseFP;
223
224 // Sparc uses FP-based references in general, even when "hasFP" is
225 // false. That function is rather a misnomer, because %fp is
226 // actually always available, unless isLeafProc.
227 if (FuncInfo->isLeafProc()) {
228 // If there's a leaf proc, all offsets need to be %sp-based,
229 // because we haven't caused %fp to actually point to our frame.
230 UseFP = false;
231 } else if (isFixed) {
232 // Otherwise, argument access should always use %fp.
233 UseFP = true;
234 } else {
235 // Finally, default to using %fp.
236 UseFP = true;
237 }
238
239 int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(ObjectIdx: FI) +
240 Subtarget.getStackPointerBias();
241
242 if (UseFP) {
243 FrameReg = RegInfo->getFrameRegister(MF);
244 return StackOffset::getFixed(Fixed: FrameOffset);
245 } else {
246 FrameReg = SP::O6; // %sp
247 return StackOffset::getFixed(Fixed: FrameOffset + MF.getFrameInfo().getStackSize());
248 }
249}
250
251[[maybe_unused]] static bool verifyLeafProcRegUse(MachineRegisterInfo *MRI) {
252
253 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
254 if (MRI->isPhysRegUsed(PhysReg: reg))
255 return false;
256
257 for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
258 if (MRI->isPhysRegUsed(PhysReg: reg))
259 return false;
260
261 return true;
262}
263
264bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const
265{
266
267 MachineRegisterInfo &MRI = MF.getRegInfo();
268 MachineFrameInfo &MFI = MF.getFrameInfo();
269 SparcMachineFunctionInfo *SMFI = MF.getInfo<SparcMachineFunctionInfo>();
270
271 return !(MFI.hasCalls() // has calls
272 || MRI.isPhysRegUsed(PhysReg: SP::L0) // Too many registers needed
273 || MRI.isPhysRegUsed(PhysReg: SP::O6) // %sp is used
274 || hasFP(MF) // need %fp
275 || MF.hasInlineAsm() // has inline assembly
276 || SMFI->isLargeProc()); // is a large procedure
277}
278
279void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
280 MachineRegisterInfo &MRI = MF.getRegInfo();
281 SmallVector<MCRegister, 2> UntrackedLiveIns;
282
283 // Remap %i[0-7] to %o[0-7].
284 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
285 if (!MRI.isPhysRegUsed(PhysReg: reg))
286 continue;
287
288 unsigned mapped_reg = reg - SP::I0 + SP::O0;
289
290 // Reserved registers are not tracked by liveness
291 if (MRI.isReserved(PhysReg: reg) && !MRI.isReserved(PhysReg: mapped_reg))
292 UntrackedLiveIns.push_back(Elt: mapped_reg);
293
294 // Replace I register with O register.
295 MRI.replaceRegWith(FromReg: reg, ToReg: mapped_reg);
296
297 // Also replace register pair super-registers.
298 if ((reg - SP::I0) % 2 == 0) {
299 unsigned preg = (reg - SP::I0) / 2 + SP::I0_I1;
300 unsigned mapped_preg = preg - SP::I0_I1 + SP::O0_O1;
301 MRI.replaceRegWith(FromReg: preg, ToReg: mapped_preg);
302 }
303 }
304
305 // Rewrite MBB's Live-ins.
306 for (MachineBasicBlock &MBB : MF) {
307 for (unsigned reg = SP::I0_I1; reg <= SP::I6_I7; ++reg) {
308 if (!MBB.isLiveIn(Reg: reg))
309 continue;
310 MBB.removeLiveIn(Reg: reg);
311 MBB.addLiveIn(PhysReg: reg - SP::I0_I1 + SP::O0_O1);
312 }
313 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
314 if (!MBB.isLiveIn(Reg: reg))
315 continue;
316 MBB.removeLiveIn(Reg: reg);
317 MBB.addLiveIn(PhysReg: reg - SP::I0 + SP::O0);
318 }
319 for (MCRegister Reg : UntrackedLiveIns)
320 if (!MBB.isLiveIn(Reg))
321 MBB.addLiveIn(PhysReg: Reg);
322 }
323 for (MCRegister Reg : UntrackedLiveIns)
324 MRI.clearKillFlags(Reg);
325
326 assert(verifyLeafProcRegUse(&MRI));
327#ifdef EXPENSIVE_CHECKS
328 MF.verify(0, "After LeafProc Remapping");
329#endif
330}
331
332static uint64_t estimateFunctionSizeInBytes(const SparcInstrInfo *TII,
333 const MachineFunction &MF) {
334 uint64_t FuncSize = 0;
335 for (auto &MBB : MF)
336 for (auto &MI : MBB) {
337 // Be conservative here and count the DS too.
338 uint64_t Multiplier = MI.hasDelaySlot() ? 2 : 1;
339 FuncSize += (TII->getInstSizeInBytes(MI) * Multiplier);
340 }
341 return FuncSize;
342}
343
344void SparcFrameLowering::determineCalleeSaves(MachineFunction &MF,
345 BitVector &SavedRegs,
346 RegScavenger *RS) const {
347 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
348 SparcMachineFunctionInfo *MFI = MF.getInfo<SparcMachineFunctionInfo>();
349 const SparcInstrInfo *TII = STI.getInstrInfo();
350 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
351
352 bool IsLargeFunction = !isInt<23>(x: estimateFunctionSizeInBytes(TII, MF));
353 MFI->setLargeProc(IsLargeFunction);
354
355 if (!Subtarget.getCLOpts().disable_sparc_leaf_proc && isLeafProc(MF)) {
356 MFI->setLeafProc(true);
357
358 remapRegsForLeafProc(MF);
359 }
360}
361
362void SparcFrameLowering::processFunctionBeforeFrameFinalized(
363 MachineFunction &MF, RegScavenger *RS) const {
364 const SparcRegisterInfo *RI = STI.getRegisterInfo();
365 const TargetRegisterClass &RC =
366 STI.is64Bit() ? SP::I64RegsRegClass : SP::IntRegsRegClass;
367 MachineFrameInfo &MFI = MF.getFrameInfo();
368 SparcMachineFunctionInfo *SMFI = MF.getInfo<SparcMachineFunctionInfo>();
369
370 // Far branches beyond 23-bit offset require a spill slot for scratch
371 // register.
372 if (SMFI->isLargeProc()) {
373 int FI =
374 MFI.CreateSpillStackObject(Size: RI->getSpillSize(RC), Alignment: RI->getSpillAlign(RC));
375 RS->addScavengingFrameIndex(FI);
376 if (SMFI->getBranchRelaxationSpillFrameIndex() == -1)
377 SMFI->setBranchRelaxationSpillFrameIndex(FI);
378 LLVM_DEBUG(dbgs() << "Allocated FI(" << FI
379 << ") as the emergency spill slot.\n");
380 }
381}
382
383void SparcFrameLowering::processFunctionBeforeFrameIndicesReplaced(
384 MachineFunction &MF, RegScavenger *RS) const {
385 if (!MF.getInfo<SparcMachineFunctionInfo>()->isLeafProc())
386 return;
387
388 // PEI takes the entry block's parameter DBG_VALUEs out of the function
389 // while determineCalleeSaves runs, so remapRegsForLeafProc missed them.
390 MachineRegisterInfo &MRI = MF.getRegInfo();
391 auto Remap = [&MRI](unsigned Reg, unsigned MappedReg) {
392 for (MachineOperand &MO : make_early_inc_range(Range: MRI.reg_operands(Reg)))
393 if (MO.isDebug())
394 MO.setReg(MappedReg);
395 };
396 for (unsigned Reg = SP::I0; Reg <= SP::I7; ++Reg)
397 Remap(Reg, Reg - SP::I0 + SP::O0);
398 for (unsigned Reg = SP::I0_I1; Reg <= SP::I6_I7; ++Reg)
399 Remap(Reg, Reg - SP::I0_I1 + SP::O0_O1);
400}
401