blob: 9f6c7d65592d4031fb6200a1c435ba370c63850e [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- SparcFrameLowering.cpp - Sparc Frame Information ------------------===//
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov2f931282011-01-10 12:39:04 +000010// This file contains the Sparc implementation of TargetFrameLowering class.
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000011//
12//===----------------------------------------------------------------------===//
13
Anton Korobeynikov2f931282011-01-10 12:39:04 +000014#include "SparcFrameLowering.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000015#include "SparcInstrInfo.h"
16#include "SparcMachineFunctionInfo.h"
Eric Christopher55414d42014-06-26 22:33:50 +000017#include "SparcSubtarget.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000018#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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Function.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000025#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetOptions.h"
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000027
28using namespace llvm;
29
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000030static cl::opt<bool>
31DisableLeafProc("disable-sparc-leaf-proc",
Venkatraman Govindaraju3e8c7d92013-06-02 02:24:27 +000032 cl::init(false),
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000033 cl::desc("Disable Sparc leaf procedure optimization."),
34 cl::Hidden);
35
Eric Christopher55414d42014-06-26 22:33:50 +000036SparcFrameLowering::SparcFrameLowering(const SparcSubtarget &ST)
37 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown,
38 ST.is64Bit() ? 16 : 8, 0, ST.is64Bit() ? 16 : 8) {}
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000039
Venkatraman Govindaraju11168682013-11-24 20:23:25 +000040void SparcFrameLowering::emitSPAdjustment(MachineFunction &MF,
41 MachineBasicBlock &MBB,
42 MachineBasicBlock::iterator MBBI,
43 int NumBytes,
Tim Northover775aaeb2015-11-05 21:54:58 +000044 unsigned ADDrr,
45 unsigned ADDri) const {
46
47 DebugLoc dl;
48 const SparcInstrInfo &TII =
49 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
50
Venkatraman Govindaraju11168682013-11-24 20:23:25 +000051 if (NumBytes >= -4096 && NumBytes < 4096) {
52 BuildMI(MBB, MBBI, dl, TII.get(ADDri), SP::O6)
53 .addReg(SP::O6).addImm(NumBytes);
54 return;
55 }
56
57 // Emit this the hard way. This clobbers G1 which we always know is
58 // available here.
59 if (NumBytes >= 0) {
60 // Emit nonnegative numbers with sethi + or.
61 // sethi %hi(NumBytes), %g1
62 // or %g1, %lo(NumBytes), %g1
63 // add %sp, %g1, %sp
64 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1)
65 .addImm(HI22(NumBytes));
66 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
67 .addReg(SP::G1).addImm(LO10(NumBytes));
68 BuildMI(MBB, MBBI, dl, TII.get(ADDrr), SP::O6)
69 .addReg(SP::O6).addReg(SP::G1);
70 return ;
71 }
72
73 // Emit negative numbers with sethi + xor.
74 // sethi %hix(NumBytes), %g1
75 // xor %g1, %lox(NumBytes), %g1
76 // add %sp, %g1, %sp
77 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1)
78 .addImm(HIX22(NumBytes));
79 BuildMI(MBB, MBBI, dl, TII.get(SP::XORri), SP::G1)
80 .addReg(SP::G1).addImm(LOX10(NumBytes));
81 BuildMI(MBB, MBBI, dl, TII.get(ADDrr), SP::O6)
82 .addReg(SP::O6).addReg(SP::G1);
83}
84
Quentin Colombet61b305e2015-05-05 17:38:16 +000085void SparcFrameLowering::emitPrologue(MachineFunction &MF,
86 MachineBasicBlock &MBB) const {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000087 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +000088
Quentin Colombet61b305e2015-05-05 17:38:16 +000089 assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
Matthias Braun941a7052016-07-28 18:40:00 +000090 MachineFrameInfo &MFI = MF.getFrameInfo();
Jonas Devlieghere865de572018-01-29 12:10:32 +000091 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000092 const SparcInstrInfo &TII =
Jonas Devlieghere865de572018-01-29 12:10:32 +000093 *static_cast<const SparcInstrInfo *>(Subtarget.getInstrInfo());
James Y Knight667395f2015-08-21 04:17:56 +000094 const SparcRegisterInfo &RegInfo =
Jonas Devlieghere865de572018-01-29 12:10:32 +000095 *static_cast<const SparcRegisterInfo *>(Subtarget.getRegisterInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +000096 MachineBasicBlock::iterator MBBI = MBB.begin();
Oleg Ranevskyy057c5a6b2015-11-05 17:50:17 +000097 // Debug location must be unknown since the first debug location is used
98 // to determine the end of the prologue.
99 DebugLoc dl;
James Y Knight667395f2015-08-21 04:17:56 +0000100 bool NeedsStackRealignment = RegInfo.needsStackRealignment(MF);
101
102 // FIXME: unfortunately, returning false from canRealignStack
103 // actually just causes needsStackRealignment to return false,
104 // rather than reporting an error, as would be sensible. This is
105 // poor, but fixing that bogosity is going to be a large project.
106 // For now, just see if it's lied, and report an error here.
Matthias Braun941a7052016-07-28 18:40:00 +0000107 if (!NeedsStackRealignment && MFI.getMaxAlignment() > getStackAlignment())
James Y Knight667395f2015-08-21 04:17:56 +0000108 report_fatal_error("Function \"" + Twine(MF.getName()) + "\" required "
109 "stack re-alignment, but LLVM couldn't handle it "
110 "(probably because it has a dynamic alloca).");
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000111
112 // Get the number of bytes to allocate from the FrameInfo
Matthias Braun941a7052016-07-28 18:40:00 +0000113 int NumBytes = (int) MFI.getStackSize();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000114
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000115 unsigned SAVEri = SP::SAVEri;
116 unsigned SAVErr = SP::SAVErr;
117 if (FuncInfo->isLeafProc()) {
118 if (NumBytes == 0)
119 return;
120 SAVEri = SP::ADDri;
121 SAVErr = SP::ADDrr;
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000122 }
James Y Knight667395f2015-08-21 04:17:56 +0000123
James Y Knight36022862015-08-26 17:57:51 +0000124 // The SPARC ABI is a bit odd in that it requires a reserved 92-byte
125 // (128 in v9) area in the user's stack, starting at %sp. Thus, the
126 // first part of the stack that can actually be used is located at
127 // %sp + 92.
128 //
129 // We therefore need to add that offset to the total stack size
130 // after all the stack objects are placed by
131 // PrologEpilogInserter calculateFrameObjectOffsets. However, since the stack needs to be
132 // aligned *after* the extra size is added, we need to disable
133 // calculateFrameObjectOffsets's built-in stack alignment, by having
134 // targetHandlesStackFrameRounding return true.
135
136
137 // Add the extra call frame stack size, if needed. (This is the same
138 // code as in PrologEpilogInserter, but also gets disabled by
139 // targetHandlesStackFrameRounding)
Matthias Braun941a7052016-07-28 18:40:00 +0000140 if (MFI.adjustsStack() && hasReservedCallFrame(MF))
141 NumBytes += MFI.getMaxCallFrameSize();
James Y Knight36022862015-08-26 17:57:51 +0000142
143 // Adds the SPARC subtarget-specific spill area to the stack
144 // size. Also ensures target-required alignment.
Jonas Devlieghere865de572018-01-29 12:10:32 +0000145 NumBytes = Subtarget.getAdjustedFrameSize(NumBytes);
James Y Knight36022862015-08-26 17:57:51 +0000146
147 // Finally, ensure that the size is sufficiently aligned for the
148 // data on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +0000149 if (MFI.getMaxAlignment() > 0) {
150 NumBytes = alignTo(NumBytes, MFI.getMaxAlignment());
James Y Knight36022862015-08-26 17:57:51 +0000151 }
152
153 // Update stack size with corrected value.
Matthias Braun941a7052016-07-28 18:40:00 +0000154 MFI.setStackSize(NumBytes);
James Y Knight667395f2015-08-21 04:17:56 +0000155
156 emitSPAdjustment(MF, MBB, MBBI, -NumBytes, SAVErr, SAVEri);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000157
James Y Knight667395f2015-08-21 04:17:56 +0000158 unsigned regFP = RegInfo.getDwarfRegNum(SP::I6, true);
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000159
160 // Emit ".cfi_def_cfa_register 30".
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000161 unsigned CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +0000162 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, regFP));
Eric Christopher612bb692014-04-29 00:16:46 +0000163 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
164 .addCFIIndex(CFIIndex);
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000165
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000166 // Emit ".cfi_window_save".
Matthias Braunf23ef432016-11-30 23:48:42 +0000167 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
Eric Christopher612bb692014-04-29 00:16:46 +0000168 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
169 .addCFIIndex(CFIIndex);
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000170
James Y Knight667395f2015-08-21 04:17:56 +0000171 unsigned regInRA = RegInfo.getDwarfRegNum(SP::I7, true);
172 unsigned regOutRA = RegInfo.getDwarfRegNum(SP::O7, true);
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +0000173 // Emit ".cfi_register 15, 31".
Matthias Braunf23ef432016-11-30 23:48:42 +0000174 CFIIndex = MF.addFrameInst(
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000175 MCCFIInstruction::createRegister(nullptr, regOutRA, regInRA));
Eric Christopher612bb692014-04-29 00:16:46 +0000176 BuildMI(MBB, MBBI, dl, TII.get(TargetOpcode::CFI_INSTRUCTION))
177 .addCFIIndex(CFIIndex);
James Y Knight667395f2015-08-21 04:17:56 +0000178
179 if (NeedsStackRealignment) {
Jonas Devlieghere865de572018-01-29 12:10:32 +0000180 int64_t Bias = Subtarget.getStackPointerBias();
181 unsigned regUnbiased;
182 if (Bias) {
183 // This clobbers G1 which we always know is available here.
184 regUnbiased = SP::G1;
185 // add %o6, BIAS, %g1
186 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), regUnbiased)
187 .addReg(SP::O6).addImm(Bias);
188 } else
189 regUnbiased = SP::O6;
190
191 // andn %regUnbiased, MaxAlign-1, %regUnbiased
Matthias Braun941a7052016-07-28 18:40:00 +0000192 int MaxAlign = MFI.getMaxAlignment();
Jonas Devlieghere865de572018-01-29 12:10:32 +0000193 BuildMI(MBB, MBBI, dl, TII.get(SP::ANDNri), regUnbiased)
194 .addReg(regUnbiased).addImm(MaxAlign - 1);
195
196 if (Bias) {
197 // add %g1, -BIAS, %o6
198 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), SP::O6)
199 .addReg(regUnbiased).addImm(-Bias);
200 }
James Y Knight667395f2015-08-21 04:17:56 +0000201 }
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000202}
203
Hans Wennborge1a2e902016-03-31 18:33:38 +0000204MachineBasicBlock::iterator SparcFrameLowering::
Eli Bendersky8da87162013-02-21 20:05:00 +0000205eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
206 MachineBasicBlock::iterator I) const {
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000207 if (!hasReservedCallFrame(MF)) {
208 MachineInstr &MI = *I;
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000209 int Size = MI.getOperand(0).getImm();
210 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
211 Size = -Size;
Venkatraman Govindaraju11168682013-11-24 20:23:25 +0000212
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000213 if (Size)
Venkatraman Govindaraju11168682013-11-24 20:23:25 +0000214 emitSPAdjustment(MF, MBB, I, Size, SP::ADDrr, SP::ADDri);
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +0000215 }
Hans Wennborge1a2e902016-03-31 18:33:38 +0000216 return MBB.erase(I);
Eli Bendersky8da87162013-02-21 20:05:00 +0000217}
218
219
Anton Korobeynikov2f931282011-01-10 12:39:04 +0000220void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000221 MachineBasicBlock &MBB) const {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000222 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Jakob Stoklund Olesen4bc5e382011-01-13 21:28:52 +0000223 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000224 const SparcInstrInfo &TII =
Eric Christopherfc6de422014-08-05 02:39:49 +0000225 *static_cast<const SparcInstrInfo *>(MF.getSubtarget().getInstrInfo());
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000226 DebugLoc dl = MBBI->getDebugLoc();
227 assert(MBBI->getOpcode() == SP::RETL &&
228 "Can only put epilog before 'retl' instruction!");
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000229 if (!FuncInfo->isLeafProc()) {
230 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
231 .addReg(SP::G0);
232 return;
233 }
Matthias Braun941a7052016-07-28 18:40:00 +0000234 MachineFrameInfo &MFI = MF.getFrameInfo();
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000235
Matthias Braun941a7052016-07-28 18:40:00 +0000236 int NumBytes = (int) MFI.getStackSize();
Venkatraman Govindaraju3521dcd2013-06-01 04:51:18 +0000237 if (NumBytes == 0)
238 return;
239
Venkatraman Govindaraju11168682013-11-24 20:23:25 +0000240 emitSPAdjustment(MF, MBB, MBBI, NumBytes, SP::ADDrr, SP::ADDri);
Anton Korobeynikovf7183ed2010-11-15 00:06:54 +0000241}
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000242
243bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000244 // Reserve call frame if there are no variable sized objects on the stack.
Matthias Braun941a7052016-07-28 18:40:00 +0000245 return !MF.getFrameInfo().hasVarSizedObjects();
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000246}
247
248// hasFP - Return true if the specified function should have a dedicated frame
249// pointer register. This is true if the function has variable sized allocas or
250// if frame pointer elimination is disabled.
251bool SparcFrameLowering::hasFP(const MachineFunction &MF) const {
James Y Knight667395f2015-08-21 04:17:56 +0000252 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
253
Matthias Braun941a7052016-07-28 18:40:00 +0000254 const MachineFrameInfo &MFI = MF.getFrameInfo();
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000255 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
James Y Knight667395f2015-08-21 04:17:56 +0000256 RegInfo->needsStackRealignment(MF) ||
Matthias Braun941a7052016-07-28 18:40:00 +0000257 MFI.hasVarSizedObjects() ||
258 MFI.isFrameAddressTaken();
Venkatraman Govindaraju641b0b52013-05-17 15:14:34 +0000259}
260
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000261
James Y Knight667395f2015-08-21 04:17:56 +0000262int SparcFrameLowering::getFrameIndexReference(const MachineFunction &MF, int FI,
263 unsigned &FrameReg) const {
264 const SparcSubtarget &Subtarget = MF.getSubtarget<SparcSubtarget>();
Matthias Braun941a7052016-07-28 18:40:00 +0000265 const MachineFrameInfo &MFI = MF.getFrameInfo();
James Y Knight667395f2015-08-21 04:17:56 +0000266 const SparcRegisterInfo *RegInfo = Subtarget.getRegisterInfo();
267 const SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Matthias Braun941a7052016-07-28 18:40:00 +0000268 bool isFixed = MFI.isFixedObjectIndex(FI);
James Y Knight667395f2015-08-21 04:17:56 +0000269
270 // Addressable stack objects are accessed using neg. offsets from
271 // %fp, or positive offsets from %sp.
272 bool UseFP;
273
274 // Sparc uses FP-based references in general, even when "hasFP" is
275 // false. That function is rather a misnomer, because %fp is
276 // actually always available, unless isLeafProc.
277 if (FuncInfo->isLeafProc()) {
278 // If there's a leaf proc, all offsets need to be %sp-based,
279 // because we haven't caused %fp to actually point to our frame.
280 UseFP = false;
281 } else if (isFixed) {
282 // Otherwise, argument access should always use %fp.
283 UseFP = true;
284 } else if (RegInfo->needsStackRealignment(MF)) {
285 // If there is dynamic stack realignment, all local object
286 // references need to be via %sp, to take account of the
287 // re-alignment.
288 UseFP = false;
289 } else {
290 // Finally, default to using %fp.
291 UseFP = true;
292 }
293
Matthias Braun941a7052016-07-28 18:40:00 +0000294 int64_t FrameOffset = MF.getFrameInfo().getObjectOffset(FI) +
James Y Knight667395f2015-08-21 04:17:56 +0000295 Subtarget.getStackPointerBias();
296
297 if (UseFP) {
298 FrameReg = RegInfo->getFrameRegister(MF);
299 return FrameOffset;
300 } else {
301 FrameReg = SP::O6; // %sp
Matthias Braun941a7052016-07-28 18:40:00 +0000302 return FrameOffset + MF.getFrameInfo().getStackSize();
James Y Knight667395f2015-08-21 04:17:56 +0000303 }
304}
305
NAKAMURA Takumidbd3bbe2013-05-29 12:10:42 +0000306static bool LLVM_ATTRIBUTE_UNUSED verifyLeafProcRegUse(MachineRegisterInfo *MRI)
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000307{
308
309 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
Daniel Cederman9db582a2017-03-08 15:23:10 +0000310 if (MRI->isPhysRegUsed(reg))
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000311 return false;
312
313 for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
Daniel Cederman9db582a2017-03-08 15:23:10 +0000314 if (MRI->isPhysRegUsed(reg))
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000315 return false;
316
317 return true;
318}
319
320bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const
321{
322
323 MachineRegisterInfo &MRI = MF.getRegInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000324 MachineFrameInfo &MFI = MF.getFrameInfo();
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000325
Matthias Braun941a7052016-07-28 18:40:00 +0000326 return !(MFI.hasCalls() // has calls
Daniel Cederman9db582a2017-03-08 15:23:10 +0000327 || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +0000328 || MRI.isPhysRegUsed(SP::O6) // %sp is used
329 || hasFP(MF)); // need %fp
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000330}
331
332void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000333 MachineRegisterInfo &MRI = MF.getRegInfo();
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000334 // Remap %i[0-7] to %o[0-7].
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000335 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
Daniel Cederman9db582a2017-03-08 15:23:10 +0000336 if (!MRI.isPhysRegUsed(reg))
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000337 continue;
James Y Knight3994be82015-08-10 19:11:39 +0000338
339 unsigned mapped_reg = reg - SP::I0 + SP::O0;
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000340
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000341 // Replace I register with O register.
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000342 MRI.replaceRegWith(reg, mapped_reg);
James Y Knight3994be82015-08-10 19:11:39 +0000343
344 // Also replace register pair super-registers.
345 if ((reg - SP::I0) % 2 == 0) {
346 unsigned preg = (reg - SP::I0) / 2 + SP::I0_I1;
347 unsigned mapped_preg = preg - SP::I0_I1 + SP::O0_O1;
348 MRI.replaceRegWith(preg, mapped_preg);
349 }
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000350 }
351
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +0000352 // Rewrite MBB's Live-ins.
353 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
354 MBB != E; ++MBB) {
James Y Knight3994be82015-08-10 19:11:39 +0000355 for (unsigned reg = SP::I0_I1; reg <= SP::I6_I7; ++reg) {
356 if (!MBB->isLiveIn(reg))
357 continue;
358 MBB->removeLiveIn(reg);
359 MBB->addLiveIn(reg - SP::I0_I1 + SP::O0_O1);
360 }
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +0000361 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
362 if (!MBB->isLiveIn(reg))
363 continue;
364 MBB->removeLiveIn(reg);
365 MBB->addLiveIn(reg - SP::I0 + SP::O0);
366 }
367 }
368
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000369 assert(verifyLeafProcRegUse(&MRI));
Filipe Cabecinhas0da99372016-04-29 15:22:48 +0000370#ifdef EXPENSIVE_CHECKS
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000371 MF.verify(0, "After LeafProc Remapping");
372#endif
373}
374
Matthias Braun02564862015-07-14 17:17:13 +0000375void SparcFrameLowering::determineCalleeSaves(MachineFunction &MF,
376 BitVector &SavedRegs,
377 RegScavenger *RS) const {
378 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
Venkatraman Govindarajuca0fe2f52013-05-29 04:46:31 +0000379 if (!DisableLeafProc && isLeafProc(MF)) {
380 SparcMachineFunctionInfo *MFI = MF.getInfo<SparcMachineFunctionInfo>();
381 MFI->setLeafProc(true);
382
383 remapRegsForLeafProc(MF);
384 }
385
386}