blob: 1834a6fd861da182d8a1b1282e1ca762452047a3 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- SparcFrameLowering.cpp - Sparc Frame Information ------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00006//
7//===----------------------------------------------------------------------===//
8//
Anton Korobeynikov2f931282011-01-10 12:39:04 +00009// This file contains the Sparc implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000010//
11//===----------------------------------------------------------------------===//
12
Anton Korobeynikov2f931282011-01-10 12:39:04 +000013#include "SparcFrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000014#include "SparcInstrInfo.h"
15#include "SparcMachineFunctionInfo.h"
Eric Christopher55414d42014-06-26 22:33:50 +000016#include "SparcSubtarget.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000024#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000026
27using namespace llvm;
28
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000029static cl::opt<bool>
30DisableLeafProc("disable-sparc-leaf-proc",
Venkatraman Govindaraju3e8c7d92013-06-02 02:24:27 +000031 cl::init(false),
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000032 cl::desc("Disable Sparc leaf procedure optimization."),
33 cl::Hidden);
34
Eric Christopher55414d42014-06-26 22:33:50 +000035SparcFrameLowering::SparcFrameLowering(const SparcSubtarget &ST)
36 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
37 ST.is64Bit() ? 16 : 8, 0, ST.is64Bit() ? 16 : 8) {}
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000038
Venkatraman Govindaraju11168682013-11-24 20:23:25 +000039void SparcFrameLowering::emitSPAdjustment(MachineFunction &MF,
40 MachineBasicBlock &MBB,
41 MachineBasicBlock::iterator MBBI,
42 int NumBytes,
Tim Northover775aaeb2015-11-05 21:54:58 +000043 unsigned ADDrr,
44 unsigned ADDri) const {
45
46 DebugLoc dl;
47 const SparcInstrInfo &TII =
48 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
49
Venkatraman Govindaraju11168682013-11-24 20:23:25 +000050 if (NumBytes >= -4096 && NumBytes < 4096) {
51 BuildMI(MBB, MBBI, dl, TII.get(ADDri), SP::O6)
52 .addReg(SP::O6).addImm(NumBytes);
53 return;
54 }
55
56 // Emit this the hard way. This clobbers G1 which we always know is
57 // available here.
58 if (NumBytes >= 0) {
59 // Emit nonnegative numbers with sethi + or.
60 // sethi %hi(NumBytes), %g1
61 // or %g1, %lo(NumBytes), %g1
62 // add %sp, %g1, %sp
63 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1)
64 .addImm(HI22(NumBytes));
65 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
66 .addReg(SP::G1).addImm(LO10(NumBytes));
67 BuildMI(MBB, MBBI, dl, TII.get(ADDrr), SP::O6)
68 .addReg(SP::O6).addReg(SP::G1);
69 return ;
70 }
71
72 // Emit negative numbers with sethi + xor.
73 // sethi %hix(NumBytes), %g1
74 // xor %g1, %lox(NumBytes), %g1
75 // add %sp, %g1, %sp
76 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1)
77 .addImm(HIX22(NumBytes));
78 BuildMI(MBB, MBBI, dl, TII.get(SP::XORri), SP::G1)
79 .addReg(SP::G1).addImm(LOX10(NumBytes));
80 BuildMI(MBB, MBBI, dl, TII.get(ADDrr), SP::O6)
81 .addReg(SP::O6).addReg(SP::G1);
82}
83
Quentin Colombet61b305e2015-05-05 17:38:16 +000084void SparcFrameLowering::emitPrologue(MachineFunction &MF,
85 MachineBasicBlock &MBB) const {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000086 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000087
Quentin Colombet61b305e2015-05-05 17:38:16 +000088 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Matthias Braun941a7052016-07-28 18:40:00 +000089 MachineFrameInfo &MFI = MF.getFrameInfo();
Jonas Devlieghere865de572018-01-29 12:10:32 +000090 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000091 const SparcInstrInfo &TII =
Jonas Devlieghere865de572018-01-29 12:10:32 +000092 *static_cast<const SparcInstrInfo *>(Subtarget.getInstrInfo());
James Y Knight667395f2015-08-21 04:17:56 +000093 const SparcRegisterInfo &RegInfo =
Jonas Devlieghere865de572018-01-29 12:10:32 +000094 *static_cast<const SparcRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000095 MachineBasicBlock::iterator MBBI = MBB.begin();
Oleg Ranevskyy057c5a6b2015-11-05 17:50:17 +000096 // Debug location must be unknown since the first debug location is used
97 // to determine the end of the prologue.
98 DebugLoc dl;
James Y Knight667395f2015-08-21 04:17:56 +000099 bool NeedsStackRealignment = RegInfo.needsStackRealignment(MF);
100
101 // FIXME: unfortunately, returning false from canRealignStack
102 // actually just causes needsStackRealignment to return false,
103 // rather than reporting an error, as would be sensible. This is
104 // poor, but fixing that bogosity is going to be a large project.
105 // For now, just see if it's lied, and report an error here.
Matthias Braun941a7052016-07-28 18:40:00 +0000106 if (!NeedsStackRealignment && MFI.getMaxAlignment() > getStackAlignment())
James Y Knight667395f2015-08-21 04:17:56 +0000107 report_fatal_error("Function \"" + Twine(MF.getName()) + "\" required "
108 "stack re-alignment, but LLVM couldn't handle it "
109 "(probably because it has a dynamic alloca).");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000110
111 // Get the number of bytes to allocate from the FrameInfo
Matthias Braun941a7052016-07-28 18:40:00 +0000112 int NumBytes = (int) MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000113
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000114 unsigned SAVEri = SP::SAVEri;
115 unsigned SAVErr = SP::SAVErr;
116 if (FuncInfo->isLeafProc()) {
117 if (NumBytes == 0)
118 return;
119 SAVEri = SP::ADDri;
120 SAVErr = SP::ADDrr;
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000121 }
James Y Knight667395f2015-08-21 04:17:56 +0000122
James Y Knight36022862015-08-26 17:57:51 +0000123 // The SPARC ABI is a bit odd in that it requires a reserved 92-byte
124 // (128 in v9) area in the user's stack, starting at %sp. Thus, the
125 // first part of the stack that can actually be used is located at
126 // %sp + 92.
127 //
128 // We therefore need to add that offset to the total stack size
129 // after all the stack objects are placed by
130 // PrologEpilogInserter calculateFrameObjectOffsets. However, since the stack needs to be
131 // aligned *after* the extra size is added, we need to disable
132 // calculateFrameObjectOffsets's built-in stack alignment, by having
133 // targetHandlesStackFrameRounding return true.
134
135
136 // Add the extra call frame stack size, if needed. (This is the same
137 // code as in PrologEpilogInserter, but also gets disabled by
138 // targetHandlesStackFrameRounding)
Matthias Braun941a7052016-07-28 18:40:00 +0000139 if (MFI.adjustsStack() && hasReservedCallFrame(MF))
140 NumBytes += MFI.getMaxCallFrameSize();
James Y Knight36022862015-08-26 17:57:51 +0000141
142 // Adds the SPARC subtarget-specific spill area to the stack
143 // size. Also ensures target-required alignment.
Jonas Devlieghere865de572018-01-29 12:10:32 +0000144 NumBytes = Subtarget.getAdjustedFrameSize(NumBytes);
James Y Knight36022862015-08-26 17:57:51 +0000145
146 // Finally, ensure that the size is sufficiently aligned for the
147 // data on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +0000148 if (MFI.getMaxAlignment() > 0) {
149 NumBytes = alignTo(NumBytes, MFI.getMaxAlignment());
James Y Knight36022862015-08-26 17:57:51 +0000150 }
151
152 // Update stack size with corrected value.
Matthias Braun941a7052016-07-28 18:40:00 +0000153 MFI.setStackSize(NumBytes);
James Y Knight667395f2015-08-21 04:17:56 +0000154
155 emitSPAdjustment(MF, MBB, MBBI, -NumBytes, SAVErr, SAVEri);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000156
James Y Knight667395f2015-08-21 04:17:56 +0000157 unsigned regFP = RegInfo.getDwarfRegNum(SP::I6, true);
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000158
159 // Emit ".cfi_def_cfa_register 30".
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000160 unsigned CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +0000161 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, regFP));
Eric Christopher612bb692014-04-29 00:16:46 +0000162 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
163 .addCFIIndex(CFIIndex);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000164
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000165 // Emit ".cfi_window_save".
Matthias Braunf23ef432016-11-30 23:48:42 +0000166 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
Eric Christopher612bb692014-04-29 00:16:46 +0000167 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
168 .addCFIIndex(CFIIndex);
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000169
James Y Knight667395f2015-08-21 04:17:56 +0000170 unsigned regInRA = RegInfo.getDwarfRegNum(SP::I7, true);
171 unsigned regOutRA = RegInfo.getDwarfRegNum(SP::O7, true);
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000172 // Emit ".cfi_register 15, 31".
Matthias Braunf23ef432016-11-30 23:48:42 +0000173 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000174 MCCFIInstruction::createRegister(nullptr, regOutRA, regInRA));
Eric Christopher612bb692014-04-29 00:16:46 +0000175 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
176 .addCFIIndex(CFIIndex);
James Y Knight667395f2015-08-21 04:17:56 +0000177
178 if (NeedsStackRealignment) {
Jonas Devlieghere865de572018-01-29 12:10:32 +0000179 int64_t Bias = Subtarget.getStackPointerBias();
180 unsigned regUnbiased;
181 if (Bias) {
182 // This clobbers G1 which we always know is available here.
183 regUnbiased = SP::G1;
184 // add %o6, BIAS, %g1
185 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), regUnbiased)
186 .addReg(SP::O6).addImm(Bias);
187 } else
188 regUnbiased = SP::O6;
189
190 // andn %regUnbiased, MaxAlign-1, %regUnbiased
Matthias Braun941a7052016-07-28 18:40:00 +0000191 int MaxAlign = MFI.getMaxAlignment();
Jonas Devlieghere865de572018-01-29 12:10:32 +0000192 BuildMI(MBB, MBBI, dl, TII.get(SP::ANDNri), regUnbiased)
193 .addReg(regUnbiased).addImm(MaxAlign - 1);
194
195 if (Bias) {
196 // add %g1, -BIAS, %o6
197 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), SP::O6)
198 .addReg(regUnbiased).addImm(-Bias);
199 }
James Y Knight667395f2015-08-21 04:17:56 +0000200 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000201}
202
Hans Wennborge1a2e902016-03-31 18:33:38 +0000203MachineBasicBlock::iterator SparcFrameLowering::
Eli Bendersky8da87162013-02-21 20:05:00 +0000204eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
205 MachineBasicBlock::iterator I) const {
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000206 if (!hasReservedCallFrame(MF)) {
207 MachineInstr &MI = *I;
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000208 int Size = MI.getOperand(0).getImm();
209 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
210 Size = -Size;
Venkatraman Govindaraju11168682013-11-24 20:23:25 +0000211
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000212 if (Size)
Venkatraman Govindaraju11168682013-11-24 20:23:25 +0000213 emitSPAdjustment(MF, MBB, I, Size, SP::ADDrr, SP::ADDri);
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000214 }
Hans Wennborge1a2e902016-03-31 18:33:38 +0000215 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +0000216}
217
218
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000219void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000220 MachineBasicBlock &MBB) const {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000221 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000222 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000223 const SparcInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000224 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000225 DebugLoc dl = MBBI->getDebugLoc();
226 assert(MBBI->getOpcode() == SP::RETL &&
227 "Can only put epilog before 'retl' instruction!");
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000228 if (!FuncInfo->isLeafProc()) {
229 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
230 .addReg(SP::G0);
231 return;
232 }
Matthias Braun941a7052016-07-28 18:40:00 +0000233 MachineFrameInfo &MFI = MF.getFrameInfo();
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000234
Matthias Braun941a7052016-07-28 18:40:00 +0000235 int NumBytes = (int) MFI.getStackSize();
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000236 if (NumBytes == 0)
237 return;
238
Venkatraman Govindaraju11168682013-11-24 20:23:25 +0000239 emitSPAdjustment(MF, MBB, MBBI, NumBytes, SP::ADDrr, SP::ADDri);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000240}
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000241
242bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000243 // Reserve call frame if there are no variable sized objects on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +0000244 return !MF.getFrameInfo().hasVarSizedObjects();
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000245}
246
247// hasFP - Return true if the specified function should have a dedicated frame
248// pointer register. This is true if the function has variable sized allocas or
249// if frame pointer elimination is disabled.
250bool SparcFrameLowering::hasFP(const MachineFunction &MF) const {
James Y Knight667395f2015-08-21 04:17:56 +0000251 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
252
Matthias Braun941a7052016-07-28 18:40:00 +0000253 const MachineFrameInfo &MFI = MF.getFrameInfo();
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000254 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
James Y Knight667395f2015-08-21 04:17:56 +0000255 RegInfo->needsStackRealignment(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000256 MFI.hasVarSizedObjects() ||
257 MFI.isFrameAddressTaken();
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000258}
259
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000260
James Y Knight667395f2015-08-21 04:17:56 +0000261int SparcFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
262 unsigned &FrameReg) const {
263 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
Matthias Braun941a7052016-07-28 18:40:00 +0000264 const MachineFrameInfo &MFI = MF.getFrameInfo();
James Y Knight667395f2015-08-21 04:17:56 +0000265 const SparcRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
266 const SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000267 bool isFixed = MFI.isFixedObjectIndex(FI);
James Y Knight667395f2015-08-21 04:17:56 +0000268
269 // Addressable stack objects are accessed using neg. offsets from
270 // %fp, or positive offsets from %sp.
271 bool UseFP;
272
273 // Sparc uses FP-based references in general, even when "hasFP" is
274 // false. That function is rather a misnomer, because %fp is
275 // actually always available, unless isLeafProc.
276 if (FuncInfo->isLeafProc()) {
277 // If there's a leaf proc, all offsets need to be %sp-based,
278 // because we haven't caused %fp to actually point to our frame.
279 UseFP = false;
280 } else if (isFixed) {
281 // Otherwise, argument access should always use %fp.
282 UseFP = true;
283 } else if (RegInfo->needsStackRealignment(MF)) {
284 // If there is dynamic stack realignment, all local object
285 // references need to be via %sp, to take account of the
286 // re-alignment.
287 UseFP = false;
288 } else {
289 // Finally, default to using %fp.
290 UseFP = true;
291 }
292
Matthias Braun941a7052016-07-28 18:40:00 +0000293 int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(FI) +
James Y Knight667395f2015-08-21 04:17:56 +0000294 Subtarget.getStackPointerBias();
295
296 if (UseFP) {
297 FrameReg = RegInfo->getFrameRegister(MF);
298 return FrameOffset;
299 } else {
300 FrameReg = SP::O6; // %sp
Matthias Braun941a7052016-07-28 18:40:00 +0000301 return FrameOffset + MF.getFrameInfo().getStackSize();
James Y Knight667395f2015-08-21 04:17:56 +0000302 }
303}
304
NAKAMURA Takumidbd3bbe2013-05-29 12:10:42 +0000305static bool LLVM_ATTRIBUTE_UNUSED verifyLeafProcRegUse(MachineRegisterInfo *MRI)
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000306{
307
308 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
Daniel Cederman9db582a2017-03-08 15:23:10 +0000309 if (MRI->isPhysRegUsed(reg))
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000310 return false;
311
312 for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
Daniel Cederman9db582a2017-03-08 15:23:10 +0000313 if (MRI->isPhysRegUsed(reg))
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000314 return false;
315
316 return true;
317}
318
319bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const
320{
321
322 MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000323 MachineFrameInfo &MFI = MF.getFrameInfo();
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000324
Matthias Braun941a7052016-07-28 18:40:00 +0000325 return !(MFI.hasCalls() // has calls
Daniel Cederman9db582a2017-03-08 15:23:10 +0000326 || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +0000327 || MRI.isPhysRegUsed(SP::O6) // %sp is used
328 || hasFP(MF)); // need %fp
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000329}
330
331void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000332 MachineRegisterInfo &MRI = MF.getRegInfo();
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000333 // Remap %i[0-7] to %o[0-7].
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000334 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
Daniel Cederman9db582a2017-03-08 15:23:10 +0000335 if (!MRI.isPhysRegUsed(reg))
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000336 continue;
James Y Knight3994be82015-08-10 19:11:39 +0000337
338 unsigned mapped_reg = reg - SP::I0 + SP::O0;
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000339
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000340 // Replace I register with O register.
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000341 MRI.replaceRegWith(reg, mapped_reg);
James Y Knight3994be82015-08-10 19:11:39 +0000342
343 // Also replace register pair super-registers.
344 if ((reg - SP::I0) % 2 == 0) {
345 unsigned preg = (reg - SP::I0) / 2 + SP::I0_I1;
346 unsigned mapped_preg = preg - SP::I0_I1 + SP::O0_O1;
347 MRI.replaceRegWith(preg, mapped_preg);
348 }
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000349 }
350
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +0000351 // Rewrite MBB's Live-ins.
352 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
353 MBB != E; ++MBB) {
James Y Knight3994be82015-08-10 19:11:39 +0000354 for (unsigned reg = SP::I0_I1; reg <= SP::I6_I7; ++reg) {
355 if (!MBB->isLiveIn(reg))
356 continue;
357 MBB->removeLiveIn(reg);
358 MBB->addLiveIn(reg - SP::I0_I1 + SP::O0_O1);
359 }
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +0000360 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
361 if (!MBB->isLiveIn(reg))
362 continue;
363 MBB->removeLiveIn(reg);
364 MBB->addLiveIn(reg - SP::I0 + SP::O0);
365 }
366 }
367
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000368 assert(verifyLeafProcRegUse(&MRI));
Filipe Cabecinhas0da99372016-04-29 15:22:48 +0000369#ifdef EXPENSIVE_CHECKS
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000370 MF.verify(0, "After LeafProc Remapping");
371#endif
372}
373
Matthias Braun02564862015-07-14 17:17:13 +0000374void SparcFrameLowering::determineCalleeSaves(MachineFunction &MF,
375 BitVector &SavedRegs,
376 RegScavenger *RS) const {
377 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000378 if (!DisableLeafProc && isLeafProc(MF)) {
379 SparcMachineFunctionInfo *MFI = MF.getInfo<SparcMachineFunctionInfo>();
380 MFI->setLeafProc(true);
381
382 remapRegsForLeafProc(MF);
383 }
384
385}