LLVM API Documentation

AlphaRegisterInfo.cpp

Go to the documentation of this file.
00001 //===- AlphaRegisterInfo.cpp - Alpha 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 Alpha implementation of the TargetRegisterInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "reginfo"
00015 #include "Alpha.h"
00016 #include "AlphaRegisterInfo.h"
00017 #include "llvm/Constants.h"
00018 #include "llvm/Type.h"
00019 #include "llvm/Function.h"
00020 #include "llvm/CodeGen/ValueTypes.h"
00021 #include "llvm/CodeGen/MachineInstrBuilder.h"
00022 #include "llvm/CodeGen/MachineFunction.h"
00023 #include "llvm/CodeGen/MachineFrameInfo.h"
00024 #include "llvm/CodeGen/MachineLocation.h"
00025 #include "llvm/Target/TargetFrameInfo.h"
00026 #include "llvm/Target/TargetMachine.h"
00027 #include "llvm/Target/TargetOptions.h"
00028 #include "llvm/Target/TargetInstrInfo.h"
00029 #include "llvm/Support/CommandLine.h"
00030 #include "llvm/Support/Debug.h"
00031 #include "llvm/Support/ErrorHandling.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 #include "llvm/ADT/BitVector.h"
00034 #include "llvm/ADT/STLExtras.h"
00035 #include <cstdlib>
00036 using namespace llvm;
00037 
00038 //These describe LDAx
00039 static const int IMM_LOW  = -32768;
00040 static const int IMM_HIGH = 32767;
00041 static const int IMM_MULT = 65536;
00042 
00043 static long getUpper16(long l)
00044 {
00045   long y = l / IMM_MULT;
00046   if (l % IMM_MULT > IMM_HIGH)
00047     ++y;
00048   return y;
00049 }
00050 
00051 static long getLower16(long l)
00052 {
00053   long h = getUpper16(l);
00054   return l - h * IMM_MULT;
00055 }
00056 
00057 AlphaRegisterInfo::AlphaRegisterInfo(const TargetInstrInfo &tii)
00058   : AlphaGenRegisterInfo(Alpha::ADJUSTSTACKDOWN, Alpha::ADJUSTSTACKUP),
00059     TII(tii), curgpdist(0)
00060 {
00061 }
00062 
00063 const unsigned* AlphaRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF)
00064                                                                          const {
00065   static const unsigned CalleeSavedRegs[] = {
00066     Alpha::R9, Alpha::R10,
00067     Alpha::R11, Alpha::R12,
00068     Alpha::R13, Alpha::R14,
00069     Alpha::F2, Alpha::F3,
00070     Alpha::F4, Alpha::F5,
00071     Alpha::F6, Alpha::F7,
00072     Alpha::F8, Alpha::F9,  0
00073   };
00074   return CalleeSavedRegs;
00075 }
00076 
00077 const TargetRegisterClass* const*
00078 AlphaRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
00079   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
00080     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
00081     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
00082     &Alpha::GPRCRegClass, &Alpha::GPRCRegClass,
00083     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
00084     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
00085     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,
00086     &Alpha::F8RCRegClass, &Alpha::F8RCRegClass,  0
00087   };
00088   return CalleeSavedRegClasses;
00089 }
00090 
00091 BitVector AlphaRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00092   BitVector Reserved(getNumRegs());
00093   Reserved.set(Alpha::R15);
00094   Reserved.set(Alpha::R30);
00095   Reserved.set(Alpha::R31);
00096   return Reserved;
00097 }
00098 
00099 //===----------------------------------------------------------------------===//
00100 // Stack Frame Processing methods
00101 //===----------------------------------------------------------------------===//
00102 
00103 // hasFP - Return true if the specified function should have a dedicated frame
00104 // pointer register.  This is true if the function has variable sized allocas or
00105 // if frame pointer elimination is disabled.
00106 //
00107 bool AlphaRegisterInfo::hasFP(const MachineFunction &MF) const {
00108   const MachineFrameInfo *MFI = MF.getFrameInfo();
00109   return MFI->hasVarSizedObjects();
00110 }
00111 
00112 void AlphaRegisterInfo::
00113 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00114                               MachineBasicBlock::iterator I) const {
00115   if (hasFP(MF)) {
00116     // If we have a frame pointer, turn the adjcallstackup instruction into a
00117     // 'sub ESP, <amt>' and the adjcallstackdown instruction into 'add ESP,
00118     // <amt>'
00119     MachineInstr *Old = I;
00120     uint64_t Amount = Old->getOperand(0).getImm();
00121     if (Amount != 0) {
00122       // We need to keep the stack aligned properly.  To do this, we round the
00123       // amount of space needed for the outgoing arguments up to the next
00124       // alignment boundary.
00125       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
00126       Amount = (Amount+Align-1)/Align*Align;
00127 
00128       MachineInstr *New;
00129       if (Old->getOpcode() == Alpha::ADJUSTSTACKDOWN) {
00130         New=BuildMI(MF, Old->getDebugLoc(), TII.get(Alpha::LDA), Alpha::R30)
00131           .addImm(-Amount).addReg(Alpha::R30);
00132       } else {
00133          assert(Old->getOpcode() == Alpha::ADJUSTSTACKUP);
00134          New=BuildMI(MF, Old->getDebugLoc(), TII.get(Alpha::LDA), Alpha::R30)
00135           .addImm(Amount).addReg(Alpha::R30);
00136       }
00137 
00138       // Replace the pseudo instruction with a new instruction...
00139       MBB.insert(I, New);
00140     }
00141   }
00142 
00143   MBB.erase(I);
00144 }
00145 
00146 //Alpha has a slightly funny stack:
00147 //Args
00148 //<- incoming SP
00149 //fixed locals (and spills, callee saved, etc)
00150 //<- FP
00151 //variable locals
00152 //<- SP
00153 
00154 void AlphaRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00155                                             int SPAdj, RegScavenger *RS) const {
00156   assert(SPAdj == 0 && "Unexpected");
00157 
00158   unsigned i = 0;
00159   MachineInstr &MI = *II;
00160   MachineBasicBlock &MBB = *MI.getParent();
00161   MachineFunction &MF = *MBB.getParent();
00162   bool FP = hasFP(MF);
00163 
00164   while (!MI.getOperand(i).isFI()) {
00165     ++i;
00166     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
00167   }
00168 
00169   int FrameIndex = MI.getOperand(i).getIndex();
00170 
00171   // Add the base register of R30 (SP) or R15 (FP).
00172   MI.getOperand(i + 1).ChangeToRegister(FP ? Alpha::R15 : Alpha::R30, false);
00173 
00174   // Now add the frame object offset to the offset from the virtual frame index.
00175   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
00176 
00177   DOUT << "FI: " << FrameIndex << " Offset: " << Offset << "\n";
00178 
00179   Offset += MF.getFrameInfo()->getStackSize();
00180 
00181   DOUT << "Corrected Offset " << Offset
00182        << " for stack size: " << MF.getFrameInfo()->getStackSize() << "\n";
00183 
00184   if (Offset > IMM_HIGH || Offset < IMM_LOW) {
00185     DOUT << "Unconditionally using R28 for evil purposes Offset: "
00186          << Offset << "\n";
00187     //so in this case, we need to use a temporary register, and move the
00188     //original inst off the SP/FP
00189     //fix up the old:
00190     MI.getOperand(i + 1).ChangeToRegister(Alpha::R28, false);
00191     MI.getOperand(i).ChangeToImmediate(getLower16(Offset));
00192     //insert the new
00193     MachineInstr* nMI=BuildMI(MF, MI.getDebugLoc(),
00194                               TII.get(Alpha::LDAH), Alpha::R28)
00195       .addImm(getUpper16(Offset)).addReg(FP ? Alpha::R15 : Alpha::R30);
00196     MBB.insert(II, nMI);
00197   } else {
00198     MI.getOperand(i).ChangeToImmediate(Offset);
00199   }
00200 }
00201 
00202 
00203 void AlphaRegisterInfo::emitPrologue(MachineFunction &MF) const {
00204   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
00205   MachineBasicBlock::iterator MBBI = MBB.begin();
00206   MachineFrameInfo *MFI = MF.getFrameInfo();
00207   DebugLoc dl = (MBBI != MBB.end() ?
00208                  MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
00209   bool FP = hasFP(MF);
00210 
00211   //handle GOP offset
00212   BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAHg), Alpha::R29)
00213     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
00214     .addReg(Alpha::R27).addImm(++curgpdist);
00215   BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAg), Alpha::R29)
00216     .addGlobalAddress(const_cast<Function*>(MF.getFunction()))
00217     .addReg(Alpha::R29).addImm(curgpdist);
00218 
00219   //evil const_cast until MO stuff setup to handle const
00220   BuildMI(MBB, MBBI, dl, TII.get(Alpha::ALTENT))
00221     .addGlobalAddress(const_cast<Function*>(MF.getFunction()));
00222 
00223   // Get the number of bytes to allocate from the FrameInfo
00224   long NumBytes = MFI->getStackSize();
00225 
00226   if (FP)
00227     NumBytes += 8; //reserve space for the old FP
00228 
00229   // Do we need to allocate space on the stack?
00230   if (NumBytes == 0) return;
00231 
00232   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
00233   NumBytes = (NumBytes+Align-1)/Align*Align;
00234 
00235   // Update frame info to pretend that this is part of the stack...
00236   MFI->setStackSize(NumBytes);
00237 
00238   // adjust stack pointer: r30 -= numbytes
00239   NumBytes = -NumBytes;
00240   if (NumBytes >= IMM_LOW) {
00241     BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
00242       .addReg(Alpha::R30);
00243   } else if (getUpper16(NumBytes) >= IMM_LOW) {
00244     BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAH), Alpha::R30)
00245       .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
00246     BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30)
00247       .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
00248   } else {
00249     std::string msg;
00250     raw_string_ostream Msg(msg); 
00251     Msg << "Too big a stack frame at " + NumBytes;
00252     llvm_report_error(Msg.str());
00253   }
00254 
00255   //now if we need to, save the old FP and set the new
00256   if (FP)
00257   {
00258     BuildMI(MBB, MBBI, dl, TII.get(Alpha::STQ))
00259       .addReg(Alpha::R15).addImm(0).addReg(Alpha::R30);
00260     //this must be the last instr in the prolog
00261     BuildMI(MBB, MBBI, dl, TII.get(Alpha::BISr), Alpha::R15)
00262       .addReg(Alpha::R30).addReg(Alpha::R30);
00263   }
00264 
00265 }
00266 
00267 void AlphaRegisterInfo::emitEpilogue(MachineFunction &MF,
00268                                      MachineBasicBlock &MBB) const {
00269   const MachineFrameInfo *MFI = MF.getFrameInfo();
00270   MachineBasicBlock::iterator MBBI = prior(MBB.end());
00271   assert((MBBI->getOpcode() == Alpha::RETDAG ||
00272           MBBI->getOpcode() == Alpha::RETDAGp)
00273          && "Can only insert epilog into returning blocks");
00274   DebugLoc dl = MBBI->getDebugLoc();
00275 
00276   bool FP = hasFP(MF);
00277 
00278   // Get the number of bytes allocated from the FrameInfo...
00279   long NumBytes = MFI->getStackSize();
00280 
00281   //now if we need to, restore the old FP
00282   if (FP) {
00283     //copy the FP into the SP (discards allocas)
00284     BuildMI(MBB, MBBI, dl, TII.get(Alpha::BISr), Alpha::R30).addReg(Alpha::R15)
00285       .addReg(Alpha::R15);
00286     //restore the FP
00287     BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDQ), Alpha::R15)
00288       .addImm(0).addReg(Alpha::R15);
00289   }
00290 
00291   if (NumBytes != 0) {
00292     if (NumBytes <= IMM_HIGH) {
00293       BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30).addImm(NumBytes)
00294         .addReg(Alpha::R30);
00295     } else if (getUpper16(NumBytes) <= IMM_HIGH) {
00296       BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDAH), Alpha::R30)
00297         .addImm(getUpper16(NumBytes)).addReg(Alpha::R30);
00298       BuildMI(MBB, MBBI, dl, TII.get(Alpha::LDA), Alpha::R30)
00299         .addImm(getLower16(NumBytes)).addReg(Alpha::R30);
00300     } else {
00301       std::string msg;
00302       raw_string_ostream Msg(msg); 
00303       Msg << "Too big a stack frame at " + NumBytes;
00304       llvm_report_error(Msg.str());
00305     }
00306   }
00307 }
00308 
00309 unsigned AlphaRegisterInfo::getRARegister() const {
00310   llvm_unreachable("What is the return address register");
00311   return 0;
00312 }
00313 
00314 unsigned AlphaRegisterInfo::getFrameRegister(MachineFunction &MF) const {
00315   return hasFP(MF) ? Alpha::R15 : Alpha::R30;
00316 }
00317 
00318 unsigned AlphaRegisterInfo::getEHExceptionRegister() const {
00319   llvm_unreachable("What is the exception register");
00320   return 0;
00321 }
00322 
00323 unsigned AlphaRegisterInfo::getEHHandlerRegister() const {
00324   llvm_unreachable("What is the exception handler register");
00325   return 0;
00326 }
00327 
00328 int AlphaRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
00329   llvm_unreachable("What is the dwarf register number");
00330   return -1;
00331 }
00332 
00333 #include "AlphaGenRegisterInfo.inc"
00334 
00335 std::string AlphaRegisterInfo::getPrettyName(unsigned reg)
00336 {
00337   std::string s(RegisterDescriptors[reg].Name);
00338   return s;
00339 }