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>();
38 if (FuncInfo->isLeafProc())
41 MachineBasicBlock &MBB = MF.front();
42 MachineFrameInfo *MFI = MF.getFrameInfo();
43 const SparcInstrInfo &TII =
44 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
45 MachineBasicBlock::iterator MBBI = MBB.begin();
46 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
48 // Get the number of bytes to allocate from the FrameInfo
49 int NumBytes = (int) MFI->getStackSize();
51 if (SubTarget.is64Bit()) {
52 // All 64-bit stack frames must be 16-byte aligned, and must reserve space
53 // for spilling the 16 window registers at %sp+BIAS..%sp+BIAS+128.
55 // Frames with calls must also reserve space for 6 outgoing arguments
56 // whether they are used or not. LowerCall_64 takes care of that.
57 assert(NumBytes % 16 == 0 && "Stack size not 16-byte aligned");
59 // Emit the correct save instruction based on the number of bytes in
60 // the frame. Minimum stack frame size according to V8 ABI is:
61 // 16 words for register window spill
62 // 1 word for address of returned aggregate-value
63 // + 6 words for passing parameters on the stack
65 // 23 words * 4 bytes per word = 92 bytes
68 // Round up to next doubleword boundary -- a double-word boundary
69 // is required by the ABI.
70 NumBytes = RoundUpToAlignment(NumBytes, 8);
74 if (NumBytes >= -4096) {
75 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVEri), SP::O6)
76 .addReg(SP::O6).addImm(NumBytes);
78 // Emit this the hard way. This clobbers G1 which we always know is
80 unsigned OffHi = (unsigned)NumBytes >> 10U;
81 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
83 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
84 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
85 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVErr), SP::O6)
86 .addReg(SP::O6).addReg(SP::G1);
90 void SparcFrameLowering::
91 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator I) const {
93 if (!hasReservedCallFrame(MF)) {
94 MachineInstr &MI = *I;
95 DebugLoc DL = MI.getDebugLoc();
96 int Size = MI.getOperand(0).getImm();
97 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
99 const SparcInstrInfo &TII =
100 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
102 BuildMI(MBB, I, DL, TII.get(SP::ADDri), SP::O6).addReg(SP::O6)
109 void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
110 MachineBasicBlock &MBB) const {
111 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
112 if (FuncInfo->isLeafProc())
114 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
115 const SparcInstrInfo &TII =
116 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
117 DebugLoc dl = MBBI->getDebugLoc();
118 assert(MBBI->getOpcode() == SP::RETL &&
119 "Can only put epilog before 'retl' instruction!");
120 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
124 bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
125 //Reserve call frame if there are no variable sized objects on the stack
126 return !MF.getFrameInfo()->hasVarSizedObjects();
129 // hasFP - Return true if the specified function should have a dedicated frame
130 // pointer register. This is true if the function has variable sized allocas or
131 // if frame pointer elimination is disabled.
132 bool SparcFrameLowering::hasFP(const MachineFunction &MF) const {
133 const MachineFrameInfo *MFI = MF.getFrameInfo();
134 return MF.getTarget().Options.DisableFramePointerElim(MF) ||
135 MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken();
139 static bool verifyLeafProcRegUse(MachineRegisterInfo *MRI)
142 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg)
143 if (MRI->isPhysRegUsed(reg))
146 for (unsigned reg = SP::L0; reg <= SP::L7; ++reg)
147 if (MRI->isPhysRegUsed(reg))
153 bool SparcFrameLowering::isLeafProc(MachineFunction &MF) const
156 MachineRegisterInfo &MRI = MF.getRegInfo();
157 MachineFrameInfo *MFI = MF.getFrameInfo();
159 return !(MFI->hasCalls() // has calls
160 || MRI.isPhysRegUsed(SP::L0) // Too many registers needed
161 || MRI.isPhysRegUsed(SP::O6) // %SP is used
162 || hasFP(MF)); // need %FP
165 void SparcFrameLowering::remapRegsForLeafProc(MachineFunction &MF) const {
167 MachineRegisterInfo &MRI = MF.getRegInfo();
169 //remap %i[0-7] to %o[0-7]
170 for (unsigned reg = SP::I0; reg <= SP::I7; ++reg) {
171 if (!MRI.isPhysRegUsed(reg))
173 unsigned mapped_reg = (reg - SP::I0 + SP::O0);
174 assert(!MRI.isPhysRegUsed(mapped_reg));
176 //Replace I register with O register
177 MRI.replaceRegWith(reg, mapped_reg);
179 //mark the reg unused.
180 MRI.setPhysRegUnused(reg);
183 assert(verifyLeafProcRegUse(&MRI));
185 MF.verify(0, "After LeafProc Remapping");
189 void SparcFrameLowering::processFunctionBeforeCalleeSavedScan
190 (MachineFunction &MF, RegScavenger *RS) const {
192 if (!DisableLeafProc && isLeafProc(MF)) {
193 SparcMachineFunctionInfo *MFI = MF.getInfo<SparcMachineFunctionInfo>();
194 MFI->setLeafProc(true);
196 remapRegsForLeafProc(MF);