1 //====- SparcFrameLowering.cpp - Sparc Frame Information -------*- C++ -*-====//
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/Function.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/Target/TargetData.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Support/CommandLine.h"
29 void SparcFrameLowering::emitPrologue(MachineFunction &MF) const {
30 MachineBasicBlock &MBB = MF.front();
31 MachineFrameInfo *MFI = MF.getFrameInfo();
32 const SparcInstrInfo &TII =
33 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
34 MachineBasicBlock::iterator MBBI = MBB.begin();
35 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
37 // Get the number of bytes to allocate from the FrameInfo
38 int NumBytes = (int) MFI->getStackSize();
40 // Emit the correct save instruction based on the number of bytes in
41 // the frame. Minimum stack frame size according to V8 ABI is:
42 // 16 words for register window spill
43 // 1 word for address of returned aggregate-value
44 // + 6 words for passing parameters on the stack
46 // 23 words * 4 bytes per word = 92 bytes
49 // Round up to next doubleword boundary -- a double-word boundary
50 // is required by the ABI.
51 NumBytes = (NumBytes + 7) & ~7;
54 if (NumBytes >= -4096) {
55 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVEri), SP::O6)
56 .addReg(SP::O6).addImm(NumBytes);
58 // Emit this the hard way. This clobbers G1 which we always know is
60 unsigned OffHi = (unsigned)NumBytes >> 10U;
61 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
63 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
64 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
65 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVErr), SP::O6)
66 .addReg(SP::O6).addReg(SP::G1);
70 void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
71 MachineBasicBlock &MBB) const {
72 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
73 const SparcInstrInfo &TII =
74 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo());
75 DebugLoc dl = MBBI->getDebugLoc();
76 assert(MBBI->getOpcode() == SP::RETL &&
77 "Can only put epilog before 'retl' instruction!");
78 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)