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