LLVM API Documentation

SystemZRegisterInfo.cpp

Go to the documentation of this file.
00001 //===- SystemZRegisterInfo.cpp - SystemZ 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 SystemZ implementation of the TargetRegisterInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "SystemZ.h"
00015 #include "SystemZInstrInfo.h"
00016 #include "SystemZMachineFunctionInfo.h"
00017 #include "SystemZRegisterInfo.h"
00018 #include "SystemZSubtarget.h"
00019 #include "llvm/CodeGen/MachineInstrBuilder.h"
00020 #include "llvm/CodeGen/MachineFrameInfo.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineRegisterInfo.h"
00023 #include "llvm/Target/TargetFrameInfo.h"
00024 #include "llvm/Target/TargetInstrInfo.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 #include "llvm/Target/TargetOptions.h"
00027 #include "llvm/ADT/BitVector.h"
00028 using namespace llvm;
00029 
00030 SystemZRegisterInfo::SystemZRegisterInfo(SystemZTargetMachine &tm,
00031                                          const SystemZInstrInfo &tii)
00032   : SystemZGenRegisterInfo(SystemZ::ADJCALLSTACKUP, SystemZ::ADJCALLSTACKDOWN),
00033     TM(tm), TII(tii) {
00034 }
00035 
00036 const unsigned*
00037 SystemZRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
00038   static const unsigned CalleeSavedRegs[] = {
00039     SystemZ::R6D,  SystemZ::R7D,  SystemZ::R8D,  SystemZ::R9D,
00040     SystemZ::R10D, SystemZ::R11D, SystemZ::R12D, SystemZ::R13D,
00041     SystemZ::R14D, SystemZ::R15D,
00042     SystemZ::F8L,  SystemZ::F9L,  SystemZ::F10L, SystemZ::F11L,
00043     SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
00044     0
00045   };
00046 
00047   return CalleeSavedRegs;
00048 }
00049 
00050 const TargetRegisterClass* const*
00051 SystemZRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
00052   static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
00053     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
00054     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
00055     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
00056     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
00057     &SystemZ::GR64RegClass, &SystemZ::GR64RegClass,
00058     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
00059     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
00060     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass,
00061     &SystemZ::FP64RegClass, &SystemZ::FP64RegClass, 0
00062   };
00063   return CalleeSavedRegClasses;
00064 }
00065 
00066 BitVector SystemZRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
00067   BitVector Reserved(getNumRegs());
00068   if (hasFP(MF))
00069     Reserved.set(SystemZ::R11D);
00070   Reserved.set(SystemZ::R14D);
00071   Reserved.set(SystemZ::R15D);
00072   return Reserved;
00073 }
00074 
00075 /// needsFP - Return true if the specified function should have a dedicated
00076 /// frame pointer register.  This is true if the function has variable sized
00077 /// allocas or if frame pointer elimination is disabled.
00078 bool SystemZRegisterInfo::hasFP(const MachineFunction &MF) const {
00079   const MachineFrameInfo *MFI = MF.getFrameInfo();
00080   return NoFramePointerElim || MFI->hasVarSizedObjects();
00081 }
00082 
00083 void SystemZRegisterInfo::
00084 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00085                               MachineBasicBlock::iterator I) const {
00086   MBB.erase(I);
00087 }
00088 
00089 int SystemZRegisterInfo::getFrameIndexOffset(MachineFunction &MF, int FI) const {
00090   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
00091   MachineFrameInfo *MFI = MF.getFrameInfo();
00092   SystemZMachineFunctionInfo *SystemZMFI =
00093     MF.getInfo<SystemZMachineFunctionInfo>();
00094   int Offset = MFI->getObjectOffset(FI) + MFI->getOffsetAdjustment();
00095   uint64_t StackSize = MFI->getStackSize();
00096 
00097   // Fixed objects are really located in the "previous" frame.
00098   if (FI < 0)
00099     StackSize -= SystemZMFI->getCalleeSavedFrameSize();
00100 
00101   Offset += StackSize - TFI.getOffsetOfLocalArea();
00102 
00103   // Skip the register save area if we generated the stack frame.
00104   if (StackSize || MFI->hasCalls())
00105     Offset -= TFI.getOffsetOfLocalArea();
00106 
00107   return Offset;
00108 }
00109 
00110 void SystemZRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
00111                                             int SPAdj, RegScavenger *RS) const {
00112   assert(SPAdj == 0 && "Unxpected");
00113 
00114   unsigned i = 0;
00115   MachineInstr &MI = *II;
00116   MachineFunction &MF = *MI.getParent()->getParent();
00117   while (!MI.getOperand(i).isFI()) {
00118     ++i;
00119     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
00120   }
00121 
00122   int FrameIndex = MI.getOperand(i).getIndex();
00123 
00124   unsigned BasePtr = (hasFP(MF) ? SystemZ::R11D : SystemZ::R15D);
00125 
00126   // This must be part of a rri or ri operand memory reference.  Replace the
00127   // FrameIndex with base register with BasePtr.  Add an offset to the
00128   // displacement field.
00129   MI.getOperand(i).ChangeToRegister(BasePtr, false);
00130 
00131   // Offset is a either 12-bit unsigned or 20-bit signed integer.
00132   // FIXME: handle "too long" displacements.
00133   int Offset = getFrameIndexOffset(MF, FrameIndex) + MI.getOperand(i+1).getImm();
00134 
00135   // Check whether displacement is too long to fit into 12 bit zext field.
00136   MI.setDesc(TII.getMemoryInstr(MI.getOpcode(), Offset));
00137 
00138   MI.getOperand(i+1).ChangeToImmediate(Offset);
00139 }
00140 
00141 void
00142 SystemZRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
00143                                                        RegScavenger *RS) const {
00144   // Determine whether R15/R14 will ever be clobbered inside the function. And
00145   // if yes - mark it as 'callee' saved.
00146   MachineFrameInfo *FFI = MF.getFrameInfo();
00147   MachineRegisterInfo &MRI = MF.getRegInfo();
00148 
00149   // Check whether high FPRs are ever used, if yes - we need to save R15 as
00150   // well.
00151   static const unsigned HighFPRs[] = {
00152     SystemZ::F8L,  SystemZ::F9L,  SystemZ::F10L, SystemZ::F11L,
00153     SystemZ::F12L, SystemZ::F13L, SystemZ::F14L, SystemZ::F15L,
00154     SystemZ::F8S,  SystemZ::F9S,  SystemZ::F10S, SystemZ::F11S,
00155     SystemZ::F12S, SystemZ::F13S, SystemZ::F14S, SystemZ::F15S,
00156   };
00157 
00158   bool HighFPRsUsed = false;
00159   for (unsigned i = 0, e = array_lengthof(HighFPRs); i != e; ++i)
00160     HighFPRsUsed |= MRI.isPhysRegUsed(HighFPRs[i]);
00161 
00162   if (FFI->hasCalls())
00163     /* FIXME: function is varargs */
00164     /* FIXME: function grabs RA */
00165     /* FIXME: function calls eh_return */
00166     MRI.setPhysRegUsed(SystemZ::R14D);
00167 
00168   if (HighFPRsUsed ||
00169       FFI->hasCalls() ||
00170       FFI->getObjectIndexEnd() != 0 || // Contains automatic variables
00171       FFI->hasVarSizedObjects() // Function calls dynamic alloca's
00172       /* FIXME: function is varargs */)
00173     MRI.setPhysRegUsed(SystemZ::R15D);
00174 }
00175 
00176 /// emitSPUpdate - Emit a series of instructions to increment / decrement the
00177 /// stack pointer by a constant value.
00178 static
00179 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
00180                   int64_t NumBytes, const TargetInstrInfo &TII) {
00181   unsigned Opc; uint64_t Chunk;
00182   bool isSub = NumBytes < 0;
00183   uint64_t Offset = isSub ? -NumBytes : NumBytes;
00184 
00185   if (Offset >= (1LL << 15) - 1) {
00186     Opc = SystemZ::ADD64ri32;
00187     Chunk = (1LL << 31) - 1;
00188   } else {
00189     Opc = SystemZ::ADD64ri16;
00190     Chunk = (1LL << 15) - 1;
00191   }
00192 
00193   DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
00194                  DebugLoc::getUnknownLoc());
00195 
00196   while (Offset) {
00197     uint64_t ThisVal = (Offset > Chunk) ? Chunk : Offset;
00198     MachineInstr *MI =
00199       BuildMI(MBB, MBBI, DL, TII.get(Opc), SystemZ::R15D)
00200       .addReg(SystemZ::R15D).addImm((isSub ? -(int64_t)ThisVal : ThisVal));
00201     // The PSW implicit def is dead.
00202     MI->getOperand(3).setIsDead();
00203     Offset -= ThisVal;
00204   }
00205 }
00206 
00207 void SystemZRegisterInfo::emitPrologue(MachineFunction &MF) const {
00208   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
00209   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
00210   MachineFrameInfo *MFI = MF.getFrameInfo();
00211   SystemZMachineFunctionInfo *SystemZMFI =
00212     MF.getInfo<SystemZMachineFunctionInfo>();
00213   MachineBasicBlock::iterator MBBI = MBB.begin();
00214   DebugLoc DL = (MBBI != MBB.end() ? MBBI->getDebugLoc() :
00215                  DebugLoc::getUnknownLoc());
00216 
00217   // Get the number of bytes to allocate from the FrameInfo.
00218   // Note that area for callee-saved stuff is already allocated, thus we need to
00219   // 'undo' the stack movement.
00220   uint64_t StackSize = MFI->getStackSize();
00221   StackSize -= SystemZMFI->getCalleeSavedFrameSize();
00222 
00223   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
00224 
00225   // Skip the callee-saved push instructions.
00226   while (MBBI != MBB.end() &&
00227          (MBBI->getOpcode() == SystemZ::MOV64mr ||
00228           MBBI->getOpcode() == SystemZ::MOV64mrm))
00229     ++MBBI;
00230 
00231   if (MBBI != MBB.end())
00232     DL = MBBI->getDebugLoc();
00233 
00234   // adjust stack pointer: R15 -= numbytes
00235   if (StackSize || MFI->hasCalls()) {
00236     assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
00237            "Invalid stack frame calculation!");
00238     emitSPUpdate(MBB, MBBI, -(int64_t)NumBytes, TII);
00239   }
00240 
00241   if (hasFP(MF)) {
00242     // Update R11 with the new base value...
00243     BuildMI(MBB, MBBI, DL, TII.get(SystemZ::MOV64rr), SystemZ::R11D)
00244       .addReg(SystemZ::R15D);
00245 
00246     // Mark the FramePtr as live-in in every block except the entry.
00247     for (MachineFunction::iterator I = next(MF.begin()), E = MF.end();
00248          I != E; ++I)
00249       I->addLiveIn(SystemZ::R11D);
00250 
00251   }
00252 }
00253 
00254 void SystemZRegisterInfo::emitEpilogue(MachineFunction &MF,
00255                                      MachineBasicBlock &MBB) const {
00256   const MachineFrameInfo *MFI = MF.getFrameInfo();
00257   const TargetFrameInfo &TFI = *MF.getTarget().getFrameInfo();
00258   MachineBasicBlock::iterator MBBI = prior(MBB.end());
00259   SystemZMachineFunctionInfo *SystemZMFI =
00260     MF.getInfo<SystemZMachineFunctionInfo>();
00261   unsigned RetOpcode = MBBI->getOpcode();
00262   DebugLoc DL = MBBI->getDebugLoc();
00263 
00264   switch (RetOpcode) {
00265   case SystemZ::RET: break;  // These are ok
00266   default:
00267     assert(0 && "Can only insert epilog into returning blocks");
00268   }
00269 
00270   // Get the number of bytes to allocate from the FrameInfo
00271   // Note that area for callee-saved stuff is already allocated, thus we need to
00272   // 'undo' the stack movement.
00273   uint64_t StackSize =
00274     MFI->getStackSize() - SystemZMFI->getCalleeSavedFrameSize();
00275   uint64_t NumBytes = StackSize - TFI.getOffsetOfLocalArea();
00276 
00277   // Skip the final terminator instruction.
00278   while (MBBI != MBB.begin()) {
00279     MachineBasicBlock::iterator PI = prior(MBBI);
00280     --MBBI;
00281     if (!PI->getDesc().isTerminator())
00282       break;
00283   }
00284 
00285   // During callee-saved restores emission stack frame was not yet finialized
00286   // (and thus - the stack size was unknown). Tune the offset having full stack
00287   // size in hands.
00288   if (StackSize || MFI->hasCalls()) {
00289     assert((MBBI->getOpcode() == SystemZ::MOV64rmm ||
00290             MBBI->getOpcode() == SystemZ::MOV64rm) &&
00291            "Expected to see callee-save register restore code");
00292     assert(MF.getRegInfo().isPhysRegUsed(SystemZ::R15D) &&
00293            "Invalid stack frame calculation!");
00294 
00295     unsigned i = 0;
00296     MachineInstr &MI = *MBBI;
00297     while (!MI.getOperand(i).isImm()) {
00298       ++i;
00299       assert(i < MI.getNumOperands() && "Unexpected restore code!");
00300     }
00301 
00302     uint64_t Offset = NumBytes + MI.getOperand(i).getImm();
00303     // If Offset does not fit into 20-bit signed displacement field we need to
00304     // emit some additional code...
00305     if (Offset > 524287) {
00306       // Fold the displacement into load instruction as much as possible.
00307       NumBytes = Offset - 524287;
00308       Offset = 524287;
00309       emitSPUpdate(MBB, MBBI, NumBytes, TII);
00310     }
00311 
00312     MI.getOperand(i).ChangeToImmediate(Offset);
00313   }
00314 }
00315 
00316 unsigned SystemZRegisterInfo::getRARegister() const {
00317   assert(0 && "What is the return address register");
00318   return 0;
00319 }
00320 
00321 unsigned SystemZRegisterInfo::getFrameRegister(MachineFunction &MF) const {
00322   assert(0 && "What is the frame register");
00323   return 0;
00324 }
00325 
00326 unsigned SystemZRegisterInfo::getEHExceptionRegister() const {
00327   assert(0 && "What is the exception register");
00328   return 0;
00329 }
00330 
00331 unsigned SystemZRegisterInfo::getEHHandlerRegister() const {
00332   assert(0 && "What is the exception handler register");
00333   return 0;
00334 }
00335 
00336 int SystemZRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
00337   assert(0 && "What is the dwarf register number");
00338   return -1;
00339 }
00340 
00341 #include "SystemZGenRegisterInfo.inc"