1 //===-- SparcFrameLowering.cpp - Sparc Frame Information ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Sparc implementation of TargetFrameLowering class.
12 //===----------------------------------------------------------------------===//
14 #include "SparcFrameLowering.h"
15 #include "SparcInstrInfo.h"
16 #include "SparcMachineFunctionInfo.h"
17 #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"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Target/TargetOptions.h"
30 DisableLeafProc("disable-sparc-leaf-proc",
32 cl::desc("Disable Sparc leaf procedure optimization."),
36 void SparcFrameLowering::emitPrologue(MachineFunction &MF) const {
37 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
39 MachineBasicBlock &MBB = MF.front();
40 MachineFrameInfo *MFI = MF.getFrameInfo();
41 const SparcInstrInfo &TII =
42 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
43 MachineBasicBlock::iterator MBBI = MBB.begin();
44 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
46 // Get the number of bytes to allocate from the FrameInfo
47 int NumBytes = (int) MFI->getStackSize();
49 unsigned SAVEri = SP::SAVEri;
50 unsigned SAVErr = SP::SAVErr;
51 if (FuncInfo->isLeafProc()) {
57 NumBytes = - SubTarget.getAdjustedFrameSize(NumBytes);
59 if (NumBytes >= -4096) {
60 BuildMI(MBB, MBBI, dl, TII.get(SAVEri), SP::O6)
61 .addReg(SP::O6).addImm(NumBytes);
63 // Emit this the hard way. This clobbers G1 which we always know is
65 unsigned OffHi = (unsigned)NumBytes >> 10U;
66 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
68 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
69 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
70 BuildMI(MBB, MBBI, dl, TII.get(SAVErr), SP::O6)
71 .addReg(SP::O6).addReg(SP::G1);
75 void SparcFrameLowering::
76 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
77 MachineBasicBlock::iterator I) const {
78 if (!hasReservedCallFrame(MF)) {
79 MachineInstr &MI = *I;
80 DebugLoc DL = MI.getDebugLoc();
81 int Size = MI.getOperand(0).getImm();
82 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
84 const SparcInstrInfo &TII =
85 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
87 BuildMI(MBB, I, DL, TII.get(SP::ADDri), SP::O6).addReg(SP::O6)
94 void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
95 MachineBasicBlock &MBB) const {
96 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
97 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
98 const SparcInstrInfo &TII =
99 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
100 DebugLoc dl = MBBI->getDebugLoc();
101 assert(MBBI->getOpcode() == SP::RETL &&
102 "Can only put epilog before 'retl' instruction!");
103 if (!FuncInfo->isLeafProc()) {
104 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
108 MachineFrameInfo *MFI = MF.getFrameInfo();
110 int NumBytes = (int) MFI->getStackSize();
114 NumBytes = SubTarget.getAdjustedFrameSize(NumBytes);
116 if (NumBytes < 4096) {
117 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), SP::O6)
118 .addReg(SP::O6).addImm(NumBytes);
120 // Emit this the hard way. This clobbers G1 which we always know is
122 unsigned OffHi = (unsigned)NumBytes >> 10U;
123 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
125 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
126 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
127 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDrr), SP::O6)
128 .addReg(SP::O6).addReg(SP::G1);
132 bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
133 //Reserve call frame if there are no variable sized objects on the stack
134 return !MF.getFrameInfo()->hasVarSizedObjects();
137 // hasFP - Return true if the specified function should have a dedicated frame
138 // pointer register. This is true if the function has variable sized allocas or
139 // if frame pointer elimination is disabled.
140 bool SparcFrameLowering::hasFP(const MachineFunction &MF) const {
141 const MachineFrameInfo *MFI = MF.getFrameInfo();
142 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
143 MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
147 static bool LLVM_ATTRIBUTE_UNUSED verifyLeafProcRegUse(MachineRegisterInfo *MRI)
150 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
151 if (MRI->isPhysRegUsed(reg))
154 for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
155 if (MRI->isPhysRegUsed(reg))
161 bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const
164 MachineRegisterInfo &MRI = MF.getRegInfo();
165 MachineFrameInfo *MFI = MF.getFrameInfo();
167 return !(MFI->hasCalls() // has calls
168 || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
169 || MRI.isPhysRegUsed(SP::O6) // %SP is used
170 || hasFP(MF)); // need %FP
173 void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
175 MachineRegisterInfo &MRI = MF.getRegInfo();
177 //remap %i[0-7] to %o[0-7]
178 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
179 if (!MRI.isPhysRegUsed(reg))
181 unsigned mapped_reg = (reg - SP::I0 + SP::O0);
182 assert(!MRI.isPhysRegUsed(mapped_reg));
184 //Replace I register with O register
185 MRI.replaceRegWith(reg, mapped_reg);
187 //mark the reg unused.
188 MRI.setPhysRegUnused(reg);
191 assert(verifyLeafProcRegUse(&MRI));
193 MF.verify(0, "After LeafProc Remapping");
197 void SparcFrameLowering::processFunctionBeforeCalleeSavedScan
198 (MachineFunction &MF, RegScavenger *RS) const {
200 if (!DisableLeafProc && isLeafProc(MF)) {
201 SparcMachineFunctionInfo *MFI = MF.getInfo<SparcMachineFunctionInfo>();
202 MFI->setLeafProc(true);
204 remapRegsForLeafProc(MF);