LLVM API Documentation

SparcRegisterInfo.cpp

Go to the documentation of this file.
00001 //===- SparcRegisterInfo.cpp - SPARC Register Information -------*- C++ -*-===//
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 the TargetRegisterInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "Sparc.h"
00015 #include "SparcRegisterInfo.h"
00016 #include "SparcSubtarget.h"
00017 #include "llvm/CodeGen/MachineInstrBuilder.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 #include "llvm/CodeGen/MachineFrameInfo.h"
00020 #include "llvm/CodeGen/MachineLocation.h"
00021 #include "llvm/Target/TargetInstrInfo.h"
00022 #include "llvm/Type.h"
00023 #include "llvm/ADT/BitVector.h"
00024 #include "llvm/ADT/STLExtras.h"
00025 using namespace llvm;
00026 
00027 SparcRegisterInfo::SparcRegisterInfo(SparcSubtarget &st,
00028                                      const TargetInstrInfo &tii)
00029   : SparcGenRegisterInfo(SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
00030     Subtarget(st), TII(tii) {
00031 }
00032 
00033 const unsigned* SparcRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
00034                                                                          const {
00035   static const unsigned CalleeSavedRegs[] = { 0 };
00036   return CalleeSavedRegs;
00037 }
00038 
00039 BitVector SparcRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00040   BitVector Reserved(getNumRegs());
00041   Reserved.set(SP::G2);
00042   Reserved.set(SP::G3);
00043   Reserved.set(SP::G4);
00044   Reserved.set(SP::O6);
00045   Reserved.set(SP::I6);
00046   Reserved.set(SP::I7);
00047   Reserved.set(SP::G0);
00048   Reserved.set(SP::G5);
00049   Reserved.set(SP::G6);
00050   Reserved.set(SP::G7);
00051   return Reserved;
00052 }
00053 
00054 
00055 const TargetRegisterClass* const*
00056 SparcRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
00057   static const TargetRegisterClass * const CalleeSavedRegClasses[] = { 0 };
00058   return CalleeSavedRegClasses;
00059 }
00060 
00061 bool SparcRegisterInfo::hasFP(const MachineFunction &MF) const {
00062   return false;
00063 }
00064 
00065 void SparcRegisterInfo::
00066 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00067                               MachineBasicBlock::iterator I) const {
00068   MachineInstr &MI = *I;
00069   int Size = MI.getOperand(0).getImm();
00070   if (MI.getOpcode() == SP::ADJCALLSTACKDOWN)
00071     Size = -Size;
00072   if (Size)
00073     BuildMI(MBB, I, TII.get(SP::ADDri), SP::O6).addReg(SP::O6).addImm(Size);
00074   MBB.erase(I);
00075 }
00076 
00077 void SparcRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00078                                             int SPAdj, RegScavenger *RS) const {
00079   assert(SPAdj == 0 && "Unexpected");
00080 
00081   unsigned i = 0;
00082   MachineInstr &MI = *II;
00083   while (!MI.getOperand(i).isFI()) {
00084     ++i;
00085     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
00086   }
00087 
00088   int FrameIndex = MI.getOperand(i).getIndex();
00089 
00090   // Addressable stack objects are accessed using neg. offsets from %fp
00091   MachineFunction &MF = *MI.getParent()->getParent();
00092   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
00093                MI.getOperand(i+1).getImm();
00094 
00095   // Replace frame index with a frame pointer reference.
00096   if (Offset >= -4096 && Offset <= 4095) {
00097     // If the offset is small enough to fit in the immediate field, directly
00098     // encode it.
00099     MI.getOperand(i).ChangeToRegister(SP::I6, false);
00100     MI.getOperand(i+1).ChangeToImmediate(Offset);
00101   } else {
00102     // Otherwise, emit a G1 = SETHI %hi(offset).  FIXME: it would be better to 
00103     // scavenge a register here instead of reserving G1 all of the time.
00104     unsigned OffHi = (unsigned)Offset >> 10U;
00105     BuildMI(*MI.getParent(), II, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
00106     // Emit G1 = G1 + I6
00107     BuildMI(*MI.getParent(), II, TII.get(SP::ADDrr), SP::G1).addReg(SP::G1)
00108       .addReg(SP::I6);
00109     // Insert: G1+%lo(offset) into the user.
00110     MI.getOperand(i).ChangeToRegister(SP::G1, false);
00111     MI.getOperand(i+1).ChangeToImmediate(Offset & ((1 << 10)-1));
00112   }
00113 }
00114 
00115 void SparcRegisterInfo::
00116 processFunctionBeforeFrameFinalized(MachineFunction &MF) const {}
00117 
00118 void SparcRegisterInfo::emitPrologue(MachineFunction &MF) const {
00119   MachineBasicBlock &MBB = MF.front();
00120   MachineFrameInfo *MFI = MF.getFrameInfo();
00121 
00122   // Get the number of bytes to allocate from the FrameInfo
00123   int NumBytes = (int) MFI->getStackSize();
00124 
00125   // Emit the correct save instruction based on the number of bytes in
00126   // the frame. Minimum stack frame size according to V8 ABI is:
00127   //   16 words for register window spill
00128   //    1 word for address of returned aggregate-value
00129   // +  6 words for passing parameters on the stack
00130   // ----------
00131   //   23 words * 4 bytes per word = 92 bytes
00132   NumBytes += 92;
00133   // Round up to next doubleword boundary -- a double-word boundary
00134   // is required by the ABI.
00135   NumBytes = (NumBytes + 7) & ~7;
00136   NumBytes = -NumBytes;
00137   
00138   if (NumBytes >= -4096) {
00139     BuildMI(MBB, MBB.begin(), TII.get(SP::SAVEri),
00140             SP::O6).addReg(SP::O6).addImm(NumBytes);
00141   } else {
00142     MachineBasicBlock::iterator InsertPt = MBB.begin();
00143     // Emit this the hard way.  This clobbers G1 which we always know is 
00144     // available here.
00145     unsigned OffHi = (unsigned)NumBytes >> 10U;
00146     BuildMI(MBB, InsertPt, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
00147     // Emit G1 = G1 + I6
00148     BuildMI(MBB, InsertPt, TII.get(SP::ORri), SP::G1)
00149       .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
00150     BuildMI(MBB, InsertPt, TII.get(SP::SAVErr), SP::O6)
00151       .addReg(SP::O6).addReg(SP::G1);
00152   }
00153 }
00154 
00155 void SparcRegisterInfo::emitEpilogue(MachineFunction &MF,
00156                                      MachineBasicBlock &MBB) const {
00157   MachineBasicBlock::iterator MBBI = prior(MBB.end());
00158   assert(MBBI->getOpcode() == SP::RETL &&
00159          "Can only put epilog before 'retl' instruction!");
00160   BuildMI(MBB, MBBI, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
00161     .addReg(SP::G0);
00162 }
00163 
00164 unsigned SparcRegisterInfo::getRARegister() const {
00165   assert(0 && "What is the return address register");
00166   return 0;
00167 }
00168 
00169 unsigned SparcRegisterInfo::getFrameRegister(MachineFunction &MF) const {
00170   assert(0 && "What is the frame register");
00171   return SP::G1;
00172 }
00173 
00174 unsigned SparcRegisterInfo::getEHExceptionRegister() const {
00175   assert(0 && "What is the exception register");
00176   return 0;
00177 }
00178 
00179 unsigned SparcRegisterInfo::getEHHandlerRegister() const {
00180   assert(0 && "What is the exception handler register");
00181   return 0;
00182 }
00183 
00184 int SparcRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
00185   assert(0 && "What is the dwarf register number");
00186   return -1;
00187 }
00188 
00189 #include "SparcGenRegisterInfo.inc"
00190