LLVM API Documentation
00001 //===-- SparcFrameLowering.cpp - Sparc Frame Information ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the Sparc implementation of TargetFrameLowering class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "SparcFrameLowering.h" 00015 #include "SparcInstrInfo.h" 00016 #include "SparcMachineFunctionInfo.h" 00017 #include "llvm/CodeGen/MachineFrameInfo.h" 00018 #include "llvm/CodeGen/MachineFunction.h" 00019 #include "llvm/CodeGen/MachineInstrBuilder.h" 00020 #include "llvm/CodeGen/MachineModuleInfo.h" 00021 #include "llvm/CodeGen/MachineRegisterInfo.h" 00022 #include "llvm/IR/DataLayout.h" 00023 #include "llvm/IR/Function.h" 00024 #include "llvm/Support/CommandLine.h" 00025 #include "llvm/Target/TargetOptions.h" 00026 00027 using namespace llvm; 00028 00029 void SparcFrameLowering::emitPrologue(MachineFunction &MF) const { 00030 MachineBasicBlock &MBB = MF.front(); 00031 MachineFrameInfo *MFI = MF.getFrameInfo(); 00032 const SparcInstrInfo &TII = 00033 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo()); 00034 MachineBasicBlock::iterator MBBI = MBB.begin(); 00035 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); 00036 00037 // Get the number of bytes to allocate from the FrameInfo 00038 int NumBytes = (int) MFI->getStackSize(); 00039 00040 if (SubTarget.is64Bit()) { 00041 // All 64-bit stack frames must be 16-byte aligned, and must reserve space 00042 // for spilling the 16 window registers at %sp+BIAS..%sp+BIAS+128. 00043 NumBytes += 128; 00044 // Frames with calls must also reserve space for 6 outgoing arguments 00045 // whether they are used or not. LowerCall_64 takes care of that. 00046 assert(NumBytes % 16 == 0 && "Stack size not 16-byte aligned"); 00047 } else { 00048 // Emit the correct save instruction based on the number of bytes in 00049 // the frame. Minimum stack frame size according to V8 ABI is: 00050 // 16 words for register window spill 00051 // 1 word for address of returned aggregate-value 00052 // + 6 words for passing parameters on the stack 00053 // ---------- 00054 // 23 words * 4 bytes per word = 92 bytes 00055 NumBytes += 92; 00056 00057 // Round up to next doubleword boundary -- a double-word boundary 00058 // is required by the ABI. 00059 NumBytes = RoundUpToAlignment(NumBytes, 8); 00060 } 00061 NumBytes = -NumBytes; 00062 00063 if (NumBytes >= -4096) { 00064 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVEri), SP::O6) 00065 .addReg(SP::O6).addImm(NumBytes); 00066 } else { 00067 // Emit this the hard way. This clobbers G1 which we always know is 00068 // available here. 00069 unsigned OffHi = (unsigned)NumBytes >> 10U; 00070 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi); 00071 // Emit G1 = G1 + I6 00072 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1) 00073 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1)); 00074 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVErr), SP::O6) 00075 .addReg(SP::O6).addReg(SP::G1); 00076 } 00077 } 00078 00079 void SparcFrameLowering:: 00080 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB, 00081 MachineBasicBlock::iterator I) const { 00082 if (!hasReservedCallFrame(MF)) { 00083 MachineInstr &MI = *I; 00084 DebugLoc DL = MI.getDebugLoc(); 00085 int Size = MI.getOperand(0).getImm(); 00086 if (MI.getOpcode() == SP::ADJCALLSTACKDOWN) 00087 Size = -Size; 00088 const SparcInstrInfo &TII = 00089 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo()); 00090 if (Size) 00091 BuildMI(MBB, I, DL, TII.get(SP::ADDri), SP::O6).addReg(SP::O6) 00092 .addImm(Size); 00093 } 00094 MBB.erase(I); 00095 } 00096 00097 00098 void SparcFrameLowering::emitEpilogue(MachineFunction &MF, 00099 MachineBasicBlock &MBB) const { 00100 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr(); 00101 const SparcInstrInfo &TII = 00102 *static_cast<const SparcInstrInfo*>(MF.getTarget().getInstrInfo()); 00103 DebugLoc dl = MBBI->getDebugLoc(); 00104 assert(MBBI->getOpcode() == SP::RETL && 00105 "Can only put epilog before 'retl' instruction!"); 00106 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0) 00107 .addReg(SP::G0); 00108 } 00109 00110 bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const { 00111 //Reserve call frame if there are no variable sized objects on the stack 00112 return !MF.getFrameInfo()->hasVarSizedObjects(); 00113 } 00114 00115 // hasFP - Return true if the specified function should have a dedicated frame 00116 // pointer register. This is true if the function has variable sized allocas or 00117 // if frame pointer elimination is disabled. 00118 bool SparcFrameLowering::hasFP(const MachineFunction &MF) const { 00119 const MachineFrameInfo *MFI = MF.getFrameInfo(); 00120 return MF.getTarget().Options.DisableFramePointerElim(MF) || 00121 MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken(); 00122 } 00123